Пример #1
0
globus_bool_t
globus_xio_error_is_canceled(
    globus_result_t                     res)
{
    return globus_error_match(
        globus_error_peek(res), GLOBUS_XIO_MODULE, GLOBUS_XIO_ERROR_CANCELED);
}
Пример #2
0
globus_bool_t
globus_xio_error_is_eof(
    globus_result_t                     res)
{
    return globus_error_match(
        globus_error_peek(res), GLOBUS_XIO_MODULE, GLOBUS_XIO_ERROR_EOF);
}
static
void
gfs_l_dynclient_log(
    globus_result_t                     result,
    int                                 level,
    char *                              fmt,
    ...)
{
    va_list                             ap;

    if(g_quiet)
    {
        return;
    }

    va_start(ap, fmt);

    fprintf(stderr, "[gridftp gfork plugin] : ");
    if(result != GLOBUS_SUCCESS)
    {
        char * err_str = globus_error_print_friendly(
            globus_error_peek(result));

        fprintf(stderr, "ERROR : %s : ", err_str);
        globus_free(err_str);
    }
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fflush(stderr);
}
Пример #4
0
globus_bool_t
globus_xio_error_match(
    globus_result_t                     result,
    int                                 type)
{
    return globus_error_match(
        globus_error_peek(result), GLOBUS_XIO_MODULE, type);
}
Пример #5
0
static
globus_result_t
globus_l_xio_test_write_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size)
{
    globus_byte_t *                     ptr = message;
    globus_size_t                       left = message_size;
    globus_size_t                       to_write;
    globus_size_t                       nbytes;
    globus_result_t                     result = GLOBUS_SUCCESS;
    GlobusXIOName(globus_l_xio_test_write_buffer);

    if (buffer_size == 0)
    {
        buffer_size = 1024;
    }
    while ((left > 0) && (result == GLOBUS_SUCCESS))
    {
        to_write = (left > buffer_size) ? buffer_size : left;
        result = globus_xio_write(
                handle,
                ptr,
                to_write,
                to_write,
                &nbytes,
                NULL);

        if (result == GLOBUS_SUCCESS)
        {
            if (nbytes != to_write)
            {
                fprintf(stderr, "Didn't write all I expected.\n");
                result = GlobusXIOErrorEOF();
            }
            left -= nbytes;
            ptr += nbytes;
        }
        else
        {
            fprintf(stderr, "Error writing data: %s\n",
                    globus_object_printable_to_string(globus_error_peek(result)));
        }
    }

    globus_xio_handle_cntl(
            handle,
            http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);

    return result;
}
Пример #6
0
static
globus_bool_t
result_is_timeout(
    globus_result_t                     result)
{
   return (result != GLOBUS_SUCCESS &&
            globus_error_match(
                globus_error_peek(result),
                GLOBUS_XIO_MODULE,
                GLOBUS_XIO_ERROR_CANCELED));
}
void
test_res(
    globus_result_t                         res)
{
    if(res == GLOBUS_SUCCESS)
    {
        return;
    }

    fprintf(stderr, "ERROR: %s\n", globus_error_print_chain(
        globus_error_peek(res)));

    globus_assert(0);
}
Пример #8
0
static globus_bool_t
result_is_cancel(
    globus_result_t                             res)
{
    if(res == GLOBUS_SUCCESS ||
        !globus_error_match(
            globus_error_peek(res),
            GLOBUS_XIO_MODULE,
            GLOBUS_XIO_ERROR_CANCELED))
    {
        return GLOBUS_FALSE;
    }

    return GLOBUS_TRUE;
}
Пример #9
0
static
globus_result_t
globus_l_xio_test_read_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     msg,
    globus_size_t                       msg_size)
{
    globus_size_t                       nbytes;
    globus_result_t                     result = GLOBUS_SUCCESS;
    globus_byte_t *			temp;
    int					i;
    GlobusXIOName(globus_l_xio_test_read_buffer);
   
    temp = globus_libc_malloc(msg_size+1);
    for (i=0; i<=msg_size; i++)
	temp[i] = '\0';
    result = globus_xio_read(
	    handle,
	    temp,
	    msg_size,
	    msg_size,
	    &nbytes,
	    NULL);

    if (result != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error reading from http: %s\n",
                globus_object_printable_to_string(globus_error_peek(result)));
    }

    result = globus_xio_read(
	    handle,
	    msg,
	    msg_size,
	    1,
	    &nbytes,
	    NULL);
    
    if (http_is_eof(result))
    {
        result = GLOBUS_SUCCESS;
    }

    return result;
}
Пример #10
0
static
globus_result_t
globus_l_xio_test_write_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     msg,
    globus_size_t                       msg_size,
    globus_xio_driver_t			http_driver)
{
    globus_size_t                       nbytes;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_test_write_buffer);

    result = globus_xio_write(
	    handle,
	    msg,
	    msg_size,
	    msg_size,
	    &nbytes,
	    NULL);

    if (result == GLOBUS_SUCCESS)
    {
	if (nbytes != msg_size)
	{
	    fprintf(stderr, "Didn't write all I expected.\n");
	    result = GlobusXIOErrorEOF();
	}
    }
    else
    {
	fprintf(stderr, "Error writing data: %s\n",
		globus_object_printable_to_string(globus_error_peek(result)));
    }

    globus_xio_handle_cntl(
            handle,
            http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);

    return result;
}
int
main(
    int					argc,
    char *				argv[])
{
    globus_io_handle_t			listener;
    globus_io_handle_t			server_handle;
    globus_io_handle_t			client_handle;
    globus_io_attr_t			attr;
    unsigned short			port = 0;
    globus_result_t			result;
    globus_io_secure_authorization_data_t
					auth_data;
    globus_l_io_authorization_test_monitor_t
					monitor;
    char				greeting[] = "Hello, my friend.";
    char 				reply_buffer[256];
    globus_size_t			written;
    globus_size_t			read_amt;

    LTDL_SET_PRELOADED_SYMBOLS();

    globus_module_activate(GLOBUS_COMMON_MODULE);
    globus_module_activate(GLOBUS_IO_MODULE);

    /* Initialize monitor */
    globus_mutex_init(&monitor.mutex, GLOBUS_NULL);
    globus_cond_init(&monitor.cond, GLOBUS_NULL);
    monitor.connected = GLOBUS_FALSE;

    /* Prepare attributes */
    globus_io_secure_authorization_data_initialize(&auth_data);
    globus_io_tcpattr_init(&attr);
    globus_io_attr_set_secure_authentication_mode(
	    &attr,
	    GLOBUS_IO_SECURE_AUTHENTICATION_MODE_GSSAPI,
	    GSS_C_NO_CREDENTIAL);


    if(argc >= 2)
    {
	if(! strcasecmp(argv[1], "self"))
	{
	    globus_io_attr_set_secure_authorization_mode(
		    &attr,
		    GLOBUS_IO_SECURE_AUTHORIZATION_MODE_SELF,
		    &auth_data);
	}
	else if(argc > 2 && ! strcasecmp(argv[1], "identity") )
	{
	    globus_io_secure_authorization_data_set_identity(&auth_data,
		                                             argv[2]);
	    globus_io_attr_set_secure_authorization_mode(
		    &attr,
		    GLOBUS_IO_SECURE_AUTHORIZATION_MODE_IDENTITY,
		    &auth_data);
	}
	else if(! strcasecmp(argv[1], "callback"))
	{
	    globus_io_secure_authorization_data_set_callback(
		    &auth_data,
		    globus_l_io_authorization_test_callback,
		    GLOBUS_NULL);

	    globus_io_attr_set_secure_authorization_mode(
		    &attr,
		    GLOBUS_IO_SECURE_AUTHORIZATION_MODE_CALLBACK,
		    &auth_data);
	}
	else if(! strcasecmp(argv[1], "-callback"))
	{
	    globus_io_secure_authorization_data_set_callback(
		    &auth_data,
		    globus_l_io_authorization_test_callback,
		    (void *) 0x1);

	    globus_io_attr_set_secure_authorization_mode(
		    &attr,
		    GLOBUS_IO_SECURE_AUTHORIZATION_MODE_CALLBACK,
		    &auth_data);
	}
	else
	{
	    goto no_authorization_mode;
	}
    }
    else
    {
	goto no_authorization_mode;
    }

    result = globus_io_tcp_create_listener(
	    &port,
	    -1,
	    &attr,
	    &listener);

    if(result != GLOBUS_SUCCESS)
    {
        char *msg = globus_error_print_friendly(globus_error_peek(result));
	globus_libc_fprintf(stderr, "# Could not create listener: %s\n", msg);
        free(msg);

	goto error_exit;
    }

    result = globus_io_tcp_register_connect(
	    "localhost",
	    port,
	    &attr,
	    globus_l_io_authorization_test_connect_callback,
	    &monitor,
	    &client_handle);

    if(result != GLOBUS_SUCCESS)
    {
	globus_libc_printf("# Could not register connect\n");
	goto error_exit;
    }

    result = globus_io_tcp_listen(&listener);
    if(result != GLOBUS_SUCCESS)
    {
	globus_libc_printf("# Could not listen for connections\n");
	goto error_exit;
    }
    result = globus_io_tcp_accept(&listener,
	                          &attr,
			          &server_handle);
    if(result != GLOBUS_SUCCESS)
    {
	if(strcasecmp(argv[1], "-callback") == 0)
	{
	    globus_module_deactivate_all();
	    exit(0);
	}
	else
	{
	    globus_libc_printf("# Could not accept connection\n");
	    goto error_exit;
	}
    }

    globus_mutex_lock(&monitor.mutex);
    while(! monitor.connected)
    {
	globus_cond_wait(&monitor.cond, &monitor.mutex);
    }

    result = globus_io_close(&listener);
    if(result != GLOBUS_SUCCESS)
    {
	globus_libc_printf("# Could not close listener\n");
	goto error_exit;
    }

    result = globus_io_write(&server_handle,
			     greeting,
		             sizeof(greeting),
		             &written);
    if(result != GLOBUS_SUCCESS)
    {
	globus_libc_printf("# Could not write greeting\n");
	goto error_exit;
    }
    result = globus_io_close(&server_handle);
    if(result != GLOBUS_SUCCESS)
    {
	globus_libc_printf("# Could not close server\n");
	goto error_exit;
    }
    result = globus_io_read(&client_handle,
	                    reply_buffer,
		            sizeof(reply_buffer),
		            sizeof(reply_buffer),
		            &read_amt);
    if(result != GLOBUS_SUCCESS)
    {
	globus_object_t * err;

	err = globus_error_get(result);

	if(! globus_io_eof(err))
	{
	    globus_libc_printf("# Could not read greeting\n");
	    goto error_exit;
	}
    }
    result = globus_io_close(&client_handle);
    if(result != GLOBUS_SUCCESS)
    {
	globus_libc_printf("# Could not close client\n");
	goto error_exit;
    }

    if(!memcmp(greeting, reply_buffer, sizeof(greeting)) == 0)
    {
	result = GLOBUS_FAILURE;
        goto error_exit;
    }

    globus_module_deactivate_all();
    exit(0);


no_authorization_mode:
    globus_libc_printf(
    "Usage: %s AUTHORIZATION\n"
    "      AUTHORIZATION is one of\n"
    "      self                 use Globus I/O's self-authorization mode\n"
    "      identity \"subject\"   use Globus I/O's subject-based authorization\n"
    "      callback             use Globus I/O's callback authorization\n"
    "      -callback            use Globus I/O's callback authorization with\n"
    "                           a failure callback\n",
    argv[0]);

error_exit:
    globus_module_deactivate_all();
    exit(1);
}
Пример #12
0
static
void
globus_l_xio_test_server_request_callback(
    void *                              user_arg,
    globus_result_t                     result,
    const char *                        method,
    const char *                        uri,
    globus_xio_http_version_t           http_version,
    globus_hashtable_t                  headers)
{
    http_test_server_t *                test_server;
    http_test_info_t *			info;
    globus_xio_http_header_t            response_headers[2];
    globus_size_t                       header_cnt=0;
    char                                content_length_buffer[64];
    int                                 rc=0;

    test_server = (http_test_server_t*) user_arg;
    info = test_server->info;
    if (result == GLOBUS_SUCCESS &&
            method != NULL && uri != NULL &&
            (strcmp(method, "POST") == 0) &&
            (strcmp(uri, "/post-test") == 0))
    {
        result = globus_l_xio_test_read_buffer(
            test_server->handle,
            info->buffer,
            info->size);

        if (result != GLOBUS_SUCCESS)
        {
            fprintf(stderr, "Error reading buffer: %s\n",
                    globus_object_printable_to_string(
                        globus_error_peek(result)));
            rc = 404;
            goto error_respond_exit;
        }

        if (info->transfer_encoding != NULL)
        {
            response_headers[header_cnt].name = "Transfer-Encoding";
            response_headers[header_cnt].value = info->transfer_encoding;

            header_cnt++;
        }

        if ((http_version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
                ((info->transfer_encoding != NULL)
                    && strcmp(info->transfer_encoding, IDENTITY) == 0))
        {
                sprintf(content_length_buffer, "%lu",
                        (unsigned long) info->size);

                response_headers[header_cnt].name = "Content-Length";
                response_headers[header_cnt].value = &content_length_buffer[0];

                header_cnt++;
        }

        result = http_test_server_respond(
                test_server,
                rc,
                NULL,
                response_headers,
                header_cnt);

        if (result != GLOBUS_SUCCESS)
        {
            goto error_exit;
        }
    }
    else
    {
        rc = 404;
        goto error_respond_exit;
    } 

    result = globus_l_xio_test_write_buffer(
            test_server->handle,
            info->buffer,
            info->size,
	    info->http_driver);

    if (result != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error writing buffer: %s\n",
                globus_object_printable_to_string(globus_error_peek(result)));
        goto error_exit;
    }

    if (--info->temp_iterations == 0)
    {
	info->size = pingpong_next_size(info->size);
	info->temp_iterations = info->iterations;
    }
    if (info->size == -1)
    {
        http_test_server_close_handle(test_server);
        http_test_server_shutdown(test_server);
    }

    return;

error_respond_exit:
    http_test_server_respond(
            test_server,
            rc,
            NULL,
            NULL,
            0);

error_exit:
    http_test_server_close_handle(test_server);
    http_test_server_shutdown(test_server);

}
Пример #13
0
static
globus_result_t
globus_l_xio_test_read_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size)
{
    globus_size_t                       offset=0;
    globus_size_t                       left = message_size;
    globus_size_t                       to_read;
    globus_size_t                       nbytes;
    globus_result_t                     result = GLOBUS_SUCCESS;
    globus_byte_t *                     buffer;
    GlobusXIOName(globus_l_xio_test_read_buffer);

    if (buffer_size == 0)
    {
        buffer_size = 1024;
    }

    buffer = malloc(buffer_size);

    if (buffer == NULL)
    {
        result = GlobusXIOErrorMemory("buffer");
    }
    while ((left > 0) || (result == GLOBUS_SUCCESS))
    {
        nbytes = 0;
        to_read = (left > buffer_size) ? buffer_size : 
                (left > 0 ? left : buffer_size);
        result = globus_xio_read(
                handle,
                buffer,
                buffer_size,
                1,
                &nbytes,
                NULL);

        if (nbytes > 0)
        {
            if (left > 0)
            {
                if (memcmp(message+offset, buffer, nbytes) != 0)
                {
                    fprintf(stderr, "File doesn't match\n");
                    result = GlobusXIOErrorParameter("buffer");
                }

                left -= nbytes;
            }
            else
            {
                fprintf(stderr, "File doesn't match\n");
                result = GlobusXIOErrorParameter("buffer");
            }
            offset += nbytes;
        }
    }
    if (offset == message_size && http_is_eof(result))
    {
        result = GLOBUS_SUCCESS;
    }
    else if (result != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error reading from http: %s\n",
                globus_object_printable_to_string(globus_error_peek(result)));

        fprintf(stderr, "after reading %lu of %lu bytes\n",
                (unsigned long) offset, (unsigned long) message_size);
    }

    return result;
}
Пример #14
0
/**
 * Calls the SSLeay error print routines to produce a printable
 * message. This may need some work, as the SSLeay error messages 
 * are more of a trace, and my not be the best for the user. 
 * Also don't take advantage of being called in a loop. 
 *
 * @param minor_status
 * @param status_value
 * @param status_type
 * @param mech_type
 * @param message_context
 * @param status_string
 *
 * @return
 */
OM_uint32 
GSS_CALLCONV gss_display_status(
    OM_uint32 *                         minor_status,
    OM_uint32                           status_value,
    int                                 status_type,
    const gss_OID                       mech_type,
    OM_uint32 *                         message_context,
    gss_buffer_t   	                status_string)
{
    globus_object_t *                   error_obj = NULL;
    char *                              error_chain_string = NULL;
    OM_uint32                           major_status = GSS_S_COMPLETE;
    char *                              reason;
    static char *                       _function_name_ =
        "gss_display_status";
    GLOBUS_I_GSI_GSSAPI_DEBUG_ENTER;

    status_string->length = 0;
    status_string->value = NULL;

    *minor_status = (OM_uint32) GLOBUS_SUCCESS;
 
    if (status_type == GSS_C_GSS_CODE)
    {
        if (!GSS_ERROR(status_value)) 
        {
            reason = "GSS COMPLETE";
        }
        else switch (GSS_ERROR(status_value)) 
        {
        
        case GSS_S_FAILURE:
            reason = "General failure";
            break;
        
        case GSS_S_DEFECTIVE_TOKEN:
            reason = "Communications Error";
            break;
        
        case GSS_S_DEFECTIVE_CREDENTIAL:
            reason = "Authentication Failed";
            break;
        
        case GSS_S_CREDENTIALS_EXPIRED:
            reason = "Credentials Expired";
            break;
        
        case GSS_S_BAD_NAME:
            reason = "Service or hostname could "
                "not be understood";
            break;
        
        case GSS_S_UNAUTHORIZED:
            reason = "Unexpected Gatekeeper or Service Name";
            break;
        
        case GSS_S_NO_CRED:
            reason = "Problem with local credentials";			
            break;
        
        case GSS_S_BAD_SIG:
            reason = "Invalid signature on message";
            break;
        
        default:
            reason = "Some Other GSS failure";
            break;
        } 

        status_string->value = globus_common_create_string(
            "GSS Major Status: %s\n",reason);

        status_string->length = strlen(status_string->value);
        major_status = GSS_S_COMPLETE;
        goto exit;
    }
    else if(status_type == GSS_C_MECH_CODE)
    {
        error_obj = globus_error_peek((globus_result_t) status_value);
        error_chain_string = globus_error_print_friendly(error_obj);

        status_string->value = globus_common_create_string(
            "GSS Minor Status Error Chain:\n%s",
            error_chain_string == NULL ? "(null)" : error_chain_string);
        
        globus_libc_free(error_chain_string);

        status_string->length = strlen(status_string->value);
        major_status = GSS_S_COMPLETE;
        goto exit;
    }
    else 
    {
        major_status = GSS_S_BAD_STATUS;
        goto exit;
    }

 exit:
    GLOBUS_I_GSI_GSSAPI_DEBUG_EXIT;
    return major_status;
}
Пример #15
0
static
void *
globus_l_dsi_rest_thread(
    void                               *arg)
{
    globus_l_dsi_rest_handle_t         *dsi_rest_handle = arg;
    globus_xio_data_descriptor_t        descriptor;
    size_t                              buf_size = 256;
    unsigned char                      *buf = malloc(buf_size);
    globus_size_t                       nbytes;
    char                               *encoded_uri;

    globus_mutex_lock(&dsi_rest_handle->mutex);
    while (!dsi_rest_handle->terminate)
    {
        char                       *method;
        char                       *uri;
        globus_xio_http_version_t   http_version;
        globus_hashtable_t          headers;
        globus_result_t             result;

        globus_mutex_unlock(&dsi_rest_handle->mutex);
        result = globus_xio_server_accept(
                &dsi_rest_handle->xio_handle,
                dsi_rest_handle->xio_server);
        globus_mutex_lock(&dsi_rest_handle->mutex);

        if (result != GLOBUS_SUCCESS)
        {
            continue;
        }
        result = globus_xio_open(
                dsi_rest_handle->xio_handle,
                NULL,
                NULL);
        if (result != GLOBUS_SUCCESS)
        {
            goto end_this_socket;
        }

        result = globus_xio_data_descriptor_init(&descriptor, dsi_rest_handle->xio_handle);
        if (result != GLOBUS_SUCCESS)
        {
            goto end_this_socket;
        }

        result = globus_xio_read(
                dsi_rest_handle->xio_handle,
                buf,
                0,
                0,
                &nbytes,
                descriptor);

        if (result != GLOBUS_SUCCESS)
        {
            goto end_this_socket;
        }

        result = globus_xio_data_descriptor_cntl(
                descriptor,
                dsi_rest_handle->http_driver,
                GLOBUS_XIO_HTTP_GET_REQUEST,
                &method,
                &uri,
                &http_version,
                &headers);

        globus_dsi_rest_uri_escape(uri, &encoded_uri);

        char *uripath = globus_common_create_string("%s/%s", dsi_rest_handle->root, encoded_uri);
        if (strcmp(method, "GET") == 0)
        {
            int fd;
            fd = open(uripath, O_RDONLY);
            if (fd < 0)
            {
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Content-Length",
                        "0");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        404);
            }
            else
            {
                globus_size_t read_amt = 0;
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        200);

                do
                {
                    read_amt = read(fd, buf, buf_size);
                    if (read_amt > 0)
                    {
                        globus_size_t written_amt = 0;

                        while (written_amt < read_amt)
                        {
                            globus_size_t this_write;

                            result = globus_xio_write(
                                dsi_rest_handle->xio_handle,
                                buf+written_amt,
                                read_amt-written_amt,
                                read_amt-written_amt,
                                &this_write,
                                NULL);
                            if (this_write > 0)
                            {
                                written_amt += this_write;
                            }
                            else if (result != GLOBUS_SUCCESS)
                            {
                                break;
                            }
                        }
                    }
                }
                while (read_amt > 0);
                close(fd);
            }
        }
        else
        {
            int fd = open(uripath, O_WRONLY|O_CREAT, 0700);

            if (fd < 0)
            {
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        500);
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Content-Length",
                        "0");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
            }
            else
            {
                off_t total_written=0;

                if (nbytes > 0)
                {
                    globus_size_t written_amt = 0;

                    while (written_amt < nbytes)
                    {
                        globus_size_t this_write;

                        this_write = write(fd, buf+written_amt, nbytes-written_amt);
                        if (this_write > 0)
                        {
                            written_amt += this_write;
                            total_written += this_write;
                        }
                    }
                }
                do
                {

                    result = globus_xio_read(
                        dsi_rest_handle->xio_handle,
                        buf,
                        buf_size,
                        1,
                        &nbytes,
                        NULL);
                    if (nbytes > 0)
                    {
                        globus_size_t written_amt = 0;

                        while (written_amt < nbytes)
                        {
                            globus_size_t this_write;

                            this_write = write(fd, buf+written_amt, nbytes-written_amt);
                            if (this_write > 0)
                            {
                                written_amt += this_write;
                                total_written += this_write;
                            }
                        }
                    }
                    if (result != GLOBUS_SUCCESS)
                    {
                        if (globus_error_match(
                                globus_error_peek(result),
                                GLOBUS_XIO_MODULE,
                                GLOBUS_XIO_ERROR_EOF)
                            || globus_xio_driver_error_match(
                                    dsi_rest_handle->http_driver,
                                    globus_error_peek(result),
                                    GLOBUS_XIO_HTTP_ERROR_EOF))
                        {
                            result = GLOBUS_SUCCESS;
                            break;
                        }
                        else
                        {
                            globus_xio_handle_cntl(
                                    dsi_rest_handle->xio_handle,
                                    dsi_rest_handle->http_driver,
                                    GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                                    500);
                            globus_xio_handle_cntl(
                                    dsi_rest_handle->xio_handle,
                                    dsi_rest_handle->http_driver,
                                    GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                                    "Content-Length",
                                    "0");
                            globus_xio_handle_cntl(
                                    dsi_rest_handle->xio_handle,
                                    dsi_rest_handle->http_driver,
                                    GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                                    "Connection",
                                    "Close");
                            goto xio_error;
                        }
                    }
                }
                while (nbytes > 0);
                close(fd);
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        204);
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Content-Length",
                        "0");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
            }
        }
xio_error:
        free(uripath);
        result = globus_xio_handle_cntl(
                dsi_rest_handle->xio_handle,
                dsi_rest_handle->http_driver,
                GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);
end_this_socket:
        result = globus_xio_close(
                dsi_rest_handle->xio_handle,
                NULL);
        dsi_rest_handle->xio_handle = NULL;
    }
    dsi_rest_handle->terminate_complete = true;
    globus_cond_signal(&dsi_rest_handle->cond);
    globus_mutex_unlock(&dsi_rest_handle->mutex);

    return NULL;
}
Пример #16
0
static
void
globus_l_xio_test_server_request_callback(
    void *                              user_arg,
    globus_result_t                     result,
    const char *                        method,
    const char *                        uri,
    globus_xio_http_version_t           http_version,
    globus_hashtable_t                  headers)
{
    http_test_server_t *                test_server;
    http_test_info_t *			info;
    globus_xio_http_header_t            response_headers[2];
    globus_size_t                       header_cnt=0;
    char                                content_length_buffer[64];
    int                                 rc=0;
    int					i;
    size_t				nbytes;

    test_server = (http_test_server_t*) user_arg;
    info = test_server->info;
    if (result == GLOBUS_SUCCESS &&
            method != NULL && uri != NULL &&
            (strcmp(method, "POST") == 0) &&
            (strcmp(uri, "/post-test") == 0))
    {
	
	for (i = 0; i < info->iterations; i++)
	{
	    result = globus_xio_read(
		    test_server->handle,
		    info->buffer,
		    info->size,
		    info->size,
		    &nbytes,
		    NULL);

	    if (result != GLOBUS_SUCCESS || nbytes != info->size)
	    {
		fprintf(stderr, "Error reading from http: %s\n",
		    globus_object_printable_to_string(
			globus_error_peek(result)));
	    }
	}
    }
    else
    {
        rc = 404;
        goto error_respond_exit;
    } 
    result = globus_xio_read(
	    test_server->handle,
	    info->buffer,
	    info->size,
	    1,
	    &nbytes,
	    NULL);

    if (result && !http_is_eof(result))
    {
        fprintf(stderr, "Error reading eof from http: %s\n",
                globus_error_print_friendly(globus_error_get(result)));
    }

    if (info->transfer_encoding != NULL)
    {
	response_headers[header_cnt].name = "Transfer-Encoding";
	response_headers[header_cnt].value = info->transfer_encoding;

	header_cnt++;
    }

    if ((http_version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
	    ((info->transfer_encoding != NULL)
		&& strcmp(info->transfer_encoding, IDENTITY) == 0))
    {
	    sprintf(content_length_buffer, "%lu", (unsigned long) info->size);

	    response_headers[header_cnt].name = "Content-Length";
	    response_headers[header_cnt].value = &content_length_buffer[0];

	    header_cnt++;
    }

    result = http_test_server_respond(
	    test_server,
	    rc,
	    NULL,
	    response_headers,
	    header_cnt);

    if (result != GLOBUS_SUCCESS)
    {
	goto error_exit;
    }
    result = globus_xio_write(
            test_server->handle,
            info->buffer,
            1,
            1,
            &nbytes,
            NULL);
    globus_xio_handle_cntl(
            test_server->handle,
            info->http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);
    info->size = throughput_next_size(info->size);
    if (info->size == -1)
    {
        http_test_server_close_handle(test_server);
        http_test_server_shutdown(test_server);
    }

    return;

error_respond_exit:
    http_test_server_respond(
            test_server,
            rc,
            NULL,
            NULL,
            0);

error_exit:
    http_test_server_close_handle(test_server);
    http_test_server_shutdown(test_server);

}
int
main(
    int                                 argc,
    char **                             argv)
{
    int                                 opt;
    globus_gram_streamer_monitor_t      monitor;
    int                                 rc;
    char                                local_path[16];
    globus_result_t                     result;
    globus_reltime_t                    period;
    globus_module_descriptor_t *        modules[] =
    {
        GLOBUS_COMMON_MODULE,
        GLOBUS_GASS_TRANSFER_MODULE,
        NULL
    };
    globus_module_descriptor_t *        failed_module;

    memset(&monitor, 0, sizeof(globus_gram_streamer_monitor_t));
    globus_mutex_init(&monitor.request.mutex, NULL);
    globus_cond_init(&monitor.request.cond, NULL);

    while ((opt = getopt(argc, argv, "s:p:d:h")) != -1)
    {
        switch (opt)
        {
            case 's':
                monitor.request.job_state_file = optarg;
                /*
                 * Assume that the remote I/O file will not be newer than the
                 * current time
                 */
                monitor.remote_io_url_file_time = time(NULL);
                rc = globus_gram_job_manager_state_file_read(&monitor.request);
                if (rc != GLOBUS_SUCCESS)
                {
                    fprintf(stderr, "%d:Error reading state file %s\n",
                            rc, optarg);
                }
                break;

            case 'p':
                if ((monitor.pid_count+1) == STREAMER_MAX)
                {
                    fprintf(stderr, "%d:Too many pids for streamer\n",
                            GLOBUS_GRAM_PROTOCOL_ERROR_NO_RESOURCES);
                    exit(EXIT_FAILURE);
                }
                monitor.pids[monitor.pid_count++] =
                        (pid_t) strtol(optarg, NULL, 10);
                break;

            case 'd':
                rc = chdir(optarg);
                if (rc != 0)
                {
                    int save_errno = errno;
                    fprintf(stderr,
                            "%d:Error accessing job state directory: %s (%d)\n",
                            GLOBUS_GRAM_PROTOCOL_ERROR_BAD_DIRECTORY,
                            strerror(save_errno),
                            save_errno);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'h':
                printf("Usage: %s -s STATE-FILE -p pid [-p pid]...\n", argv[0]);
                exit(EXIT_SUCCESS);
                break;

            case '?':
            default:
                fprintf(stderr, "%d:Unknown option: %c\n",
                        GLOBUS_GRAM_PROTOCOL_ERROR_GATEKEEPER_MISCONFIGURED, 
                        (char) opt);
                exit(EXIT_FAILURE);
        }
    }

    rc = globus_module_activate_array(modules, &failed_module);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "%d:Activation failed: %s %d\n",
                GLOBUS_GRAM_PROTOCOL_ERROR_GATEKEEPER_MISCONFIGURED,
                failed_module->module_name,
                rc);
        exit(EXIT_FAILURE);
    }

    strcpy(local_path, "stdout");
    monitor.output_stream.fd = open(local_path, O_RDONLY);

    strcpy(local_path, "stderr");
    monitor.error_stream.fd = open(local_path, O_RDONLY);

    rc = globus_mutex_init(&monitor.mutex, NULL);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "%d:Mutex init failed\n",
                GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED);
        exit(EXIT_FAILURE);
    }
    rc = globus_cond_init(&monitor.cond, NULL);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "%d:Mutex init failed\n",
                GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED);
        exit(EXIT_FAILURE);
    }

    globus_mutex_lock(&monitor.mutex);

    GlobusTimeReltimeSet(period, 5, 0);
    result = globus_callback_register_periodic(
            &monitor.local_poll_periodic,
            &globus_i_reltime_zero,
            &period,
            globus_l_gram_streamer_local_poll,
            &monitor);
    if (result != GLOBUS_SUCCESS)
    {
        char * errstr = globus_error_print_friendly(globus_error_peek(result));
        fprintf(stderr, "%d:Initialization error: %s\n",
                GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED,
                errstr);
        free(errstr);
        exit(EXIT_FAILURE);
    }

    result = globus_callback_register_periodic(
            &monitor.waitpids_poll_periodic,
            &globus_i_reltime_zero,
            &period,
            globus_l_gram_streamer_waitpids,
            &monitor);
    if (result != GLOBUS_SUCCESS)
    {
        char * errstr = globus_error_print_friendly(globus_error_peek(result));
        fprintf(stderr, "%d:Initialization error: %s\n",
                GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED,
                errstr);
        free(errstr);
        exit(EXIT_FAILURE);
    }

    rc = globus_l_gram_streamer_get_destinations(
            &monitor);
    if (rc != GLOBUS_SUCCESS)
    {
        exit(EXIT_FAILURE);
    }

    if (monitor.output_stream.fd != -1 &&
        monitor.output_stream.destination != NULL)
    {
        rc = globus_l_gram_streamer_open_destination(
                &monitor,
                &monitor.output_stream);
        if (rc != GLOBUS_SUCCESS)
        {
            fprintf(stderr, "%d:Error opening stdout destination %s (%d)\n",
                    GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_STDOUT,
                    monitor.output_stream.destination,
                    rc);
            exit(EXIT_FAILURE);
        }
        monitor.output_stream.state = GLOBUS_GRAM_STREAM_NEW;
    }
    else
    {
        monitor.output_stream.state = GLOBUS_GRAM_STREAM_NONE;
    }
    if (monitor.error_stream.fd != -1 &&
        monitor.error_stream.destination != NULL)
    {
        rc = globus_l_gram_streamer_open_destination(
                &monitor,
                &monitor.error_stream);
        if (rc != GLOBUS_SUCCESS)
        {
            fprintf(stderr, "%d:Error opening stderr destination %s (%d)\n",
                    GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_STDERR,
                    monitor.error_stream.destination,
                    rc);
            exit(EXIT_FAILURE);
        }
        monitor.error_stream.state = GLOBUS_GRAM_STREAM_NEW;
    }
    else
    {
        monitor.error_stream.state = GLOBUS_GRAM_STREAM_NONE;
    }

    while (monitor.pid_count > 0 ||
           (monitor.output_stream.state != GLOBUS_GRAM_STREAM_NONE &&
            monitor.output_stream.state != GLOBUS_GRAM_STREAM_DONE &&
            monitor.output_stream.state != GLOBUS_GRAM_STREAM_FAIL) ||
           (monitor.error_stream.state != GLOBUS_GRAM_STREAM_NONE &&
            monitor.error_stream.state != GLOBUS_GRAM_STREAM_DONE &&
            monitor.error_stream.state != GLOBUS_GRAM_STREAM_FAIL))
    {
        globus_cond_wait(&monitor.cond, &monitor.mutex);
    }
    if (monitor.output_stream.state == GLOBUS_GRAM_STREAM_DONE)
    {
        printf("%s %s\n",
               monitor.output_stream.source,
               monitor.output_stream.destination);
    }
    if (monitor.error_stream.state == GLOBUS_GRAM_STREAM_DONE)
    {
        printf("%s %s\n",
               monitor.error_stream.source,
               monitor.error_stream.destination);
    }
    globus_mutex_unlock(&monitor.mutex);
    globus_module_deactivate(GLOBUS_GASS_TRANSFER_MODULE);
    globus_module_activate(GLOBUS_COMMON_MODULE);

    exit(EXIT_SUCCESS);
}
int
main(
    int                                 argc,
    char **                             argv)
{
    int                                 rc = 0;
    globus_result_t                     result;
    char *                              cs;
    globus_xio_contact_t                parsed_contact;
    char *                              new_banner;
    char *                              old_banner;
    GlobusGFSName(main);

    /* activte globus stuff */    
    if((rc = globus_module_activate(GLOBUS_COMMON_MODULE)) != GLOBUS_SUCCESS ||
        (rc = globus_module_activate(
            GLOBUS_GRIDFTP_SERVER_MODULE)) != GLOBUS_SUCCESS)
    {
        fprintf(stderr,
            "Error: Failed to initialize:\n%s",
            globus_error_print_friendly(globus_error_peek(rc)));
        goto error_activate;
    }
        
    /* initialize global variables */
    globus_mutex_init(&globus_l_gfs_mutex, GLOBUS_NULL);
    globus_cond_init(&globus_l_gfs_cond, GLOBUS_NULL);
    globus_l_gfs_signal_init();

    globus_libc_printf("Embedded server starting.\n");

    globus_mutex_lock(&globus_l_gfs_mutex);
    {
        result = globus_gridftp_server_embed_init(
            &globus_l_gfs_server_handle,
            argv);
        if(result != GLOBUS_SUCCESS)
        {
            rc = 1;
            goto error_lock;
        }
                
        /* add our acl module */
        globus_gfs_acl_add_module(&globus_gfs_acl_test_module);

        /* customize some config */
        old_banner = globus_gridftp_server_embed_config_get_string(
            globus_l_gfs_server_handle, "banner");
        new_banner = globus_common_create_string(
            "%s\nEMBEDDED", old_banner);
        globus_gridftp_server_embed_config_set_ptr(
            globus_l_gfs_server_handle, 
            "banner", 
            new_banner);
        globus_free(old_banner);

        globus_gridftp_server_embed_config_set_int(
            globus_l_gfs_server_handle, 
            "connections_max", 
            10);

        globus_gridftp_server_embed_config_set_int(
            globus_l_gfs_server_handle, 
            "auth_level", 
            1 | /* identity check */
            2 | /* file access checks */
            4 | /* disable setuid (not really needed with gridmap disabled)*/
            8); /* disable gridmap lookup */
                
        result = globus_gridftp_server_embed_start(
            globus_l_gfs_server_handle,
            globus_l_gfs_event_cb,
            NULL);
        if(result != GLOBUS_SUCCESS)
        {
            rc = 1;
            goto error_lock;
        }
        globus_l_gfs_server_active = GLOBUS_TRUE;
        
        cs = globus_gridftp_server_embed_config_get_string(
            globus_l_gfs_server_handle, "contact_string");
            
        globus_xio_contact_parse(&parsed_contact, cs);
        
        globus_libc_printf(
            "Server listening on port %s.\n", parsed_contact.port);
        globus_xio_contact_destroy(&parsed_contact);
        
        /* run until we are done */ 
        while(!globus_l_gfs_terminated || globus_l_gfs_server_active)
        {
            globus_cond_wait(&globus_l_gfs_cond, &globus_l_gfs_mutex);
        }
    }
    globus_mutex_unlock(&globus_l_gfs_mutex);

    globus_module_deactivate_all();

    GlobusGFSDebugExit();
    return 0;

error_lock:
    globus_mutex_unlock(&globus_l_gfs_mutex);

error_activate:
    globus_module_deactivate_all();


    GlobusGFSDebugExitWithError();
    return rc;
}
int main(int argc, char *argv[])
{
    int                                 rc;
    globus_result_t                     result;
    char                                modname[] =
                                        "load_test_module";
    char *                              globus_loc = NULL;
    int                                 notok=3;

    printf("1..3\n");

    rc = globus_module_activate(GLOBUS_SCHEDULER_EVENT_GENERATOR_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        goto error;
    }

    result = globus_location(&globus_loc);
    if (result != GLOBUS_SUCCESS)
    {
        rc = 1;
        goto error;
    }

    result = globus_scheduler_event_generator_load_module(modname);

    if (result != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "# Load %s returned %s\n", modname, globus_error_print_friendly(globus_error_peek(result)));
        rc = 1;
        goto deactivate_error;
    }
    notok--;
    globus_module_deactivate_all();
    notok--;
    rc = globus_module_activate(GLOBUS_SCHEDULER_EVENT_GENERATOR_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        goto error;
    }
    result = globus_scheduler_event_generator_load_module("bogus");

    if (result == GLOBUS_SUCCESS)
    {
        rc = 1;
        goto deactivate_error;
    }
    printf("ok - module load\n");
    notok--;

deactivate_error:
    globus_module_deactivate_all();
error:
    if (rc != 0)
    {
        while (notok-- > 0)
        {
            printf("not ok\n");
        }
    }
    return rc;
}
Пример #20
0
int
client_test(
    http_test_info_t *			info,
    int					timer)	
{
    int                                 rc = 0;
    globus_result_t                     result;
    int                                 header_cnt = 0;
    char                                content_length_buffer[64];
    globus_xio_http_header_t            headers[2];
    globus_xio_handle_t                 handle;
    int                                 i;
    size_t                              nbytes;
    globus_xio_data_descriptor_t        descriptor;
    int                                 status_code;
    char *                              reason_phrase;


    globus_utp_start_timer(timer);
    if (info->transfer_encoding != NULL)
    {
        headers[header_cnt].name = "Transfer-Encoding";
        headers[header_cnt].value = info->transfer_encoding;

        header_cnt++;

    }

    if ((info->version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
            ((info->transfer_encoding != NULL)
                && strcmp(info->transfer_encoding, IDENTITY) == 0))
    {
        sprintf(content_length_buffer, "%lu", (unsigned long) info->size);

        headers[header_cnt].name = "Content-Length";
        headers[header_cnt].value = &content_length_buffer[0];

        header_cnt++;
    }

    handle = NULL;

    result = http_test_client_request(
	    &handle,
	    info->tcp_driver,
	    info->http_driver,
	    info->stack,
	    info->contact,
	    "%2fpost-test",
	    "POST",
	    info->version,
	    headers,
	    header_cnt);

    if (result != GLOBUS_SUCCESS)
    {
	fprintf(stderr, "Error making request: %s\n",
		globus_object_printable_to_string(
		globus_error_get(result)));
	rc = 50;
	goto error_exit;
    }

    for (i = 0; i < info->iterations; i++)
    {
	result = globus_xio_write(
		handle,
		info->buffer,
		info->size,
		info->size,
		&nbytes,
		NULL);

	if (result == GLOBUS_SUCCESS)
	{
	    if (nbytes != info->size)
	    {
		fprintf(stderr, "Didn't write all I expected.\n");
	    }
	}
	else
	{
	    fprintf(stderr, "Error writing data: %s\n",
		globus_object_printable_to_string(globus_error_peek(result)));
	}
    }
    globus_xio_handle_cntl(
	    handle,
	    info->http_driver,
	    GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);
    /* READ RESPONSE */
    result = globus_xio_data_descriptor_init(&descriptor, handle);
    if (result != GLOBUS_SUCCESS)
    {
	rc = 51;

        goto close_exit;
    }
    result = globus_xio_read(
            handle,
            info->buffer,
            0,
            0,
            NULL,
            descriptor);
    if (result != GLOBUS_SUCCESS)
    {
        rc = 51;
        goto close_exit;
    }

    result = globus_xio_data_descriptor_cntl(
            descriptor,
            info->http_driver,
            GLOBUS_XIO_HTTP_GET_RESPONSE,
            &status_code,
            &reason_phrase,
            NULL,
            NULL);
    if (result != GLOBUS_SUCCESS || status_code < 200 || status_code > 299)
    {
        fprintf(stderr, "Get failed with \"%03d %s\"\n",
                status_code,
                reason_phrase);

        rc = 51;
        goto close_exit;
    }

    result = globus_xio_read(
            handle,
            info->buffer,
            info->size,
            1,
            &nbytes,
            NULL);
    if (result && !http_is_eof(result))
    {       
        fprintf(stderr, "Error reading eof from http: %s\n",
                globus_error_print_friendly(globus_error_get(result)));
    }

close_exit:
    globus_xio_close(handle, NULL);
    globus_utp_stop_timer(timer);

error_exit:

    return rc;
}
Пример #21
0
int
globus_i_gram_get_tg_gateway_user(
    gss_ctx_id_t                        context,
    globus_gsi_cred_handle_t            peer_cred,
    char **                             gateway_user)
{
#if HAVE_LIBXML2
    OM_uint32                           maj_stat, min_stat;
    gss_buffer_set_t                    data_set;
    ASN1_UTF8STRING *                   asn1_str;
    char *                              assertion_string;
    unsigned char *                     p;
    long                                pl;
    xmlDocPtr                           doc;
    xmlXPathContextPtr                  xpath_ctx;
    xmlXPathObjectPtr                   xresult;
    int                                 rc;
    ASN1_OBJECT *                       asn1_desired_object = NULL;
    int                                 cert_count;
    int                                 found_index;
    int                                 chain_index;
    X509                               *cert;
    X509_EXTENSION *                    extension;
    ASN1_OCTET_STRING                  *asn1_oct_string;
    STACK_OF(X509)                     *chain = NULL;

    *gateway_user = NULL;

    if (context == GSS_C_NO_CONTEXT && peer_cred != NULL)
    {
        globus_result_t result;
        /* This basically duplicates the gss_inquire_sec_context_by_oid(), but
         * instead uses a gsi credential object
         */
        rc = GLOBUS_SUCCESS;
        asn1_desired_object = ASN1_OBJECT_new();
        if (asn1_desired_object == NULL)
        {
            rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
            goto no_extension_in_cred_chain;
        }

        asn1_desired_object->length = globus_l_saml_oid_desc.length;
        asn1_desired_object->data = globus_l_saml_oid_desc.elements;

        result = globus_gsi_cred_get_cert_chain(peer_cred, &chain);
        if (result != GLOBUS_SUCCESS)
        {
            char * msg;
            
            msg = globus_error_print_friendly(
                globus_error_peek(result));
            globus_gram_protocol_error_7_hack_replace_message(
                    msg);

            free(msg);
            rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;

            goto no_extension_in_cred_chain;
        }

        cert_count = sk_X509_num(chain);
        found_index = -1;
        for (chain_index = 0; chain_index < cert_count; chain_index++)
        {
            cert = sk_X509_value(chain, chain_index);
            found_index = X509_get_ext_by_OBJ(cert, asn1_desired_object, found_index);
            if (found_index >= 0)
            {
                extension = X509_get_ext(cert, found_index);
                if (extension == NULL)
                {
                    rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;
                    globus_gram_protocol_error_7_hack_replace_message(
                        "Unable to extract SAML assertion extension from certificate chain");
                    goto no_extension_in_cred_chain;
                }
                asn1_oct_string = X509_EXTENSION_get_data(extension);
                if (asn1_oct_string == NULL)
                {
                    rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;
                    globus_gram_protocol_error_7_hack_replace_message(
                        "Unable to extract SAML assertion extension from certificate chain");
                    goto no_extension_in_cred_chain;
                }
                p = asn1_oct_string->data;

                asn1_str = d2i_ASN1_UTF8STRING(NULL, (void *)&p, asn1_oct_string->length);
                if (asn1_str == NULL)
                {
                    rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;
                    globus_gram_protocol_error_7_hack_replace_message(
                        "Unable to convert SAML assertion text from DER to UTF8");
                    goto no_extension_in_cred_chain;
                }
                assertion_string = malloc(asn1_str->length + 1);
                if (assertion_string == NULL)
                {
                    rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
                    goto no_extension_in_cred_chain;
                }
                memcpy(assertion_string, asn1_str->data, asn1_str->length);
                assertion_string[asn1_str->length] = 0;
                break;
            }
        }
        if (chain_index == cert_count)
        {
            goto no_extension_in_cred_chain;
        }
    }
    else if (context == GSS_C_NO_CONTEXT)
    {
        rc = GLOBUS_SUCCESS;
        goto no_context;
    }
    else
    {
        maj_stat =  gss_inquire_sec_context_by_oid(
                &min_stat,
                context,
                globus_saml_oid,
                &data_set);

        if (GSS_ERROR(maj_stat))
        {
            globus_gram_protocol_error_7_hack_replace_message(
                    "Error extracting SAML assertion");

            rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;

            goto inquire_failed;
        }

        /* We'll process only the first SAML assertion bound in the X.509 chain */
        if (data_set->count < 1)
        {
            rc = GLOBUS_SUCCESS;

            goto empty_data_set;
        }

        p = data_set->elements[0].value;
        pl = data_set->elements[0].length;

        /* Convert DER-Encoded string to UTF8 */
        asn1_str = d2i_ASN1_UTF8STRING(NULL, (void *) &p, pl);
        if (!asn1_str)
        {
            globus_gram_protocol_error_7_hack_replace_message(
                    "Error decoding SAML assertion");
            rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;

            goto utfstring_failed;
        }

        assertion_string = malloc(asn1_str->length + 1);
        if (assertion_string == NULL)
        {
            rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;

            goto assertion_string_malloc_failed;
        }
        memcpy(assertion_string, asn1_str->data, asn1_str->length);
        assertion_string[asn1_str->length] = 0;
    }

    /* Parse SAML assertion */
    doc = xmlParseDoc(BAD_CAST assertion_string);
    if (doc == NULL)
    {
        globus_gram_protocol_error_7_hack_replace_message(
                "Error parsing SAML assertion");
        rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;

        goto parse_assertion_failed;
    }

    xmlXPathInit();

    /* Use XPATH to extract Issuer */
    xpath_ctx = xmlXPathNewContext(doc);
    if (xpath_ctx == NULL)
    {
        rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;

        goto xpath_ctx_init_failed;
    }
    rc = xmlXPathRegisterNs(
            xpath_ctx,
            (xmlChar *) "s",
            (xmlChar *) "urn:oasis:names:tc:SAML:1.0:assertion");

    if (rc != 0)
    {
        rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;

        goto xpath_register_ns_failed;
    }

    xresult = xmlXPathEvalExpression(
            (const xmlChar *) "string(/s:Assertion/@Issuer)",
            xpath_ctx);

    if (xresult == NULL)
    {
        globus_gram_protocol_error_7_hack_replace_message(
                "Error processing SAML assertion: no \"Issuer\" attribute");
        rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;

        goto xpath_eval_issuer_failed;
    }

    if (! globus_l_tg_saml_assertion_is_self_issued(
                context,
                (const char *) xresult->stringval))
    {
        /* Ignore non-self issued assertions */
        rc = GLOBUS_SUCCESS;

        goto non_self_issued;
    }

    xmlXPathFreeObject(xresult);

    /* Use XPATH to extract the sender-vouches, self-issued, TG principal name
     * Subject attribute from the Assertion's AuthenticationStatement
     */
    xresult = xmlXPathEvalExpression(
            (const xmlChar *) "string(/s:Assertion/s:AuthenticationStatement/s:Subject[string(s:SubjectConfirmation/s:ConfirmationMethod) = 'urn:oasis:names:tc:SAML:1.0:cm:sender-vouches' and s:NameIdentifier/@Format = 'http://teragrid.org/names/nameid-format/principalname']/s:NameIdentifier[1])",
            xpath_ctx);

    if (xresult == NULL)
    {
        globus_gram_protocol_error_7_hack_replace_message(
                "Error processing SAML assertion: no teragrid principal");
        rc = GLOBUS_GRAM_PROTOCOL_ERROR_AUTHORIZATION;

        goto get_gateway_name_failed;
    }

    if (xresult != NULL &&
        xresult->stringval != NULL &&
        *(xresult->stringval) != 0)
    {
        *gateway_user = strdup((char *) xresult->stringval);
    }

get_gateway_name_failed:
non_self_issued:
    if (xresult != NULL)
    {
        xmlXPathFreeObject(xresult);
    }
xpath_eval_issuer_failed:
xpath_register_ns_failed:
    xmlXPathFreeContext(xpath_ctx);
xpath_ctx_init_failed:
    xmlFreeDoc(doc);
parse_assertion_failed:
    free(assertion_string);
assertion_string_malloc_failed:
    ASN1_UTF8STRING_free(asn1_str);
utfstring_failed:
empty_data_set:
    gss_release_buffer_set(&min_stat, &data_set);
inquire_failed:
no_extension_in_cred_chain:
no_context:
    if (asn1_desired_object != NULL)
    {
        ASN1_OBJECT_free(asn1_desired_object);
    }
    if (chain != NULL)
    {
        sk_X509_free(chain);
    }
    return rc;
#else
    *gateway_user = NULL;
    return GLOBUS_SUCCESS;
#endif /* HAVE_LIBXML2 */
}