예제 #1
0
int xmlrpcsrv_process(char *data, size_t size, char **response, size_t *reslen) {
	
	xmlrpc_env env;
	xmlrpc_env_init(&env);

	xmlrpc_mem_block *output = NULL;
#ifdef HAVE_XMLRPC_REGISTRY_PROCESS_CALL2
	xmlrpc_registry_process_call2(&env, xmlrpcsrv_registry, data, size, NULL, &output);
#else
	output = xmlrpc_registry_process_call(&env, xmlrpcsrv_registry, "localhost", data, size);
#endif
	if (!output)
		return POM_ERR;

	*reslen = xmlrpc_mem_block_size(output);
	*response = malloc(*reslen);
	if (!*response) {
		pomlog(POMLOG_ERR "Not enough memory to allocate %u bytes for response", *reslen);
		xmlrpc_mem_block_free(output);
		xmlrpc_env_clean(&env);
		return POM_ERR;
	}
	memcpy(*response, xmlrpc_mem_block_contents(output), *reslen);

	xmlrpc_mem_block_free(output);
	xmlrpc_env_clean(&env);

	return POM_OK;
}
예제 #2
0
void
xmlrpc_authcookie_set(xmlrpc_env *const envP,
                      const char *const username,
                      const char *const password) {

    char *unencoded;
    xmlrpc_mem_block *token;

    XMLRPC_ASSERT_ENV_OK(envP);
    XMLRPC_ASSERT_PTR_OK(username);
    XMLRPC_ASSERT_PTR_OK(password);

    /* Create unencoded string/hash. */

    MALLOCARRAY(unencoded, (strlen(username) + strlen(password) + 1 + 1));
    sprintf(unencoded, "%s:%s", username, password);

    /* Create encoded string. */
    token = xmlrpc_base64_encode_without_newlines(
            envP, (unsigned char *) unencoded, strlen(unencoded));
    if (!envP->fault_occurred) {
        /* Set HTTP_COOKIE_AUTH to the character representation of the
           encoded string.
        */
#if HAVE_SETENV
        setenv("HTTP_COOKIE_AUTH",
               XMLRPC_MEMBLOCK_CONTENTS(char, token),
               1);
#endif
        xmlrpc_mem_block_free(token);
    }
예제 #3
0
/* set cookie function */
void xmlrpc_authcookie_set ( xmlrpc_env *env, 
                        const char *username, 
                        const char *password ) {
    char *unencoded;
    xmlrpc_mem_block *token;
    static char env_buffer[1024];
    const char* block;

    /* Check asserts. */
    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT_PTR_OK(username);
    XMLRPC_ASSERT_PTR_OK(password);

    /* Clear out memory. */
    unencoded = (char *) malloc ( sizeof ( char * ) );

    /* Create unencoded string/hash. */
    sprintf(unencoded, "%s:%s", username, password);

    /* Create encoded string. */
    token = xmlrpc_base64_encode_without_newlines(env, (unsigned char*)unencoded,
                                strlen(unencoded));
    XMLRPC_FAIL_IF_FAULT(env);

    /* Set HTTP_COOKIE_AUTH to the character representation of the
    ** encoded string. */
    block = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, token);
    sprintf(env_buffer, "HTTP_COOKIE_AUTH=%s", block);
    putenv(env_buffer);

 cleanup:
    if (token) xmlrpc_mem_block_free(token);
}
void
xmlrpc_destroyString(xmlrpc_value * const valueP) {

    if (valueP->_wcs_block)
        xmlrpc_mem_block_free(valueP->_wcs_block);

    xmlrpc_mem_block_clean(&valueP->_block);
}
예제 #5
0
파일: rcd-rpc.c 프로젝트: joeshaw/rcd
static void
soup_rpc_callback (SoupServerContext *context, SoupMessage *msg, gpointer data)
{
    xmlrpc_env env;
    xmlrpc_mem_block *output;
    const char *username;
    RCDIdentity *identity = NULL;
    RCDRPCMethodData *method_data;

    xmlrpc_env_init (&env);

    method_data = g_new0 (RCDRPCMethodData, 1);

    /* Get the username from the auth context and get its identity */
    username = soup_server_auth_get_user (context->auth);

    if (strcmp (username, rcd_prefs_get_mid ()) == 0) {
        identity = rcd_identity_new ();
        identity->username = g_strdup ("server");
        identity->privileges = rcd_privileges_from_string ("superuser");
    }
    else
        identity = rcd_identity_lookup (username);

    g_assert (identity != NULL);

    method_data->host = soup_server_context_get_client_host (context);
    method_data->identity = identity;

    output = process_rpc_call (
        &env, msg->request.body, msg->request.length, method_data);

    rcd_identity_free (method_data->identity);
    g_free (method_data);

    soup_message_add_header (msg->response_headers,
                             "Server", "Red Carpet Daemon/"VERSION);

    if (env.fault_occurred) {
        soup_message_set_status (msg, SOUP_STATUS_BAD_REQUEST);
        return;
    }

    /* Let Soup free the data for us */
    msg->response.owner = SOUP_BUFFER_SYSTEM_OWNED;
    msg->response.length = XMLRPC_TYPED_MEM_BLOCK_SIZE(char, output);
    msg->response.body = g_memdup(
        XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, output), msg->response.length);

    soup_message_set_status (msg, SOUP_STATUS_OK);

    xmlrpc_mem_block_free(output);
} /* soup_rpc_callback */
예제 #6
0
static void
getBody(xmlrpc_env *        const envP,
        TSession *          const abyssSessionP,
        size_t              const contentSize,
        const char *        const trace,
        xmlrpc_mem_block ** const bodyP) {
/*----------------------------------------------------------------------------
   Get the entire body, which is of size 'contentSize' bytes, from the
   Abyss session and return it as the new memblock *bodyP.

   The first chunk of the body may already be in Abyss's buffer.  We
   retrieve that before reading more.
-----------------------------------------------------------------------------*/
    xmlrpc_mem_block * body;

    if (trace)
        fprintf(stderr, "XML-RPC handler processing body.  "
                "Content Size = %u bytes\n", (unsigned)contentSize);

    body = xmlrpc_mem_block_new(envP, 0);
    if (!envP->fault_occurred) {
        size_t bytesRead;
        const char * chunkPtr;
        size_t chunkLen;

        bytesRead = 0;

        while (!envP->fault_occurred && bytesRead < contentSize) {
            SessionGetReadData(abyssSessionP, contentSize - bytesRead,
                               &chunkPtr, &chunkLen);
            bytesRead += chunkLen;

            assert(bytesRead <= contentSize);

            XMLRPC_MEMBLOCK_APPEND(char, envP, body, chunkPtr, chunkLen);
            if (bytesRead < contentSize)
                refillBufferFromConnection(envP, abyssSessionP, trace);
        }
        if (envP->fault_occurred)
            xmlrpc_mem_block_free(body);
        else
            *bodyP = body;
    }
}
예제 #7
0
static void
test_utf8_coding(void) {

#if HAVE_UNICODE_WCHAR
    xmlrpc_env env, env2;
    utf8_and_wcs *good_data;
    char **bad_data;
    char *utf8;
    wchar_t *wcs;
    xmlrpc_mem_block *output;

    xmlrpc_env_init(&env);

    /* Test each of our valid UTF-8 sequences. */
    for (good_data = good_utf8; good_data->utf8 != NULL; good_data++) {
        utf8 = good_data->utf8;
        wcs = good_data->wcs;

        /* Attempt to validate the UTF-8 string. */
        xmlrpc_validate_utf8(&env, utf8, strlen(utf8));
        TEST_NO_FAULT(&env);

        /* Attempt to decode the UTF-8 string. */
        output = xmlrpc_utf8_to_wcs(&env, utf8, strlen(utf8));
        TEST_NO_FAULT(&env);
        TEST(output != NULL);
        TEST(wcslen(wcs) == XMLRPC_TYPED_MEM_BLOCK_SIZE(wchar_t, output));
        TEST(0 ==
             wcsncmp(wcs, XMLRPC_TYPED_MEM_BLOCK_CONTENTS(wchar_t, output),
                     wcslen(wcs)));
        xmlrpc_mem_block_free(output);

        /* Test the UTF-8 encoder, too. */
        output = xmlrpc_wcs_to_utf8(&env, wcs, wcslen(wcs));
        TEST_NO_FAULT(&env);
        TEST(output != NULL);
        TEST(strlen(utf8) == XMLRPC_TYPED_MEM_BLOCK_SIZE(char, output));
        TEST(0 ==
             strncmp(utf8, XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, output),
                     strlen(utf8)));
        xmlrpc_mem_block_free(output);
    }

    /* Test each of our illegal UTF-8 sequences. */
    for (bad_data = bad_utf8; *bad_data != NULL; bad_data++) {
        utf8 = *bad_data;
    
        /* Attempt to validate the UTF-8 string. */
        xmlrpc_env_init(&env2);
        xmlrpc_validate_utf8(&env2, utf8, strlen(utf8));
        TEST_FAULT(&env2, XMLRPC_INVALID_UTF8_ERROR);
        /* printf("Fault: %s\n", env2.fault_string); --Hand-checked */
        xmlrpc_env_clean(&env2);

        /* Attempt to decode the UTF-8 string. */
        xmlrpc_env_init(&env2);
        output = xmlrpc_utf8_to_wcs(&env2, utf8, strlen(utf8));
        TEST_FAULT(&env2, XMLRPC_INVALID_UTF8_ERROR);
        TEST(output == NULL);
        xmlrpc_env_clean(&env2);
    }
    xmlrpc_env_clean(&env);
#endif  /* HAVE_UNICODE_WCHAR */
}
예제 #8
0
static void
test_base64_conversion(void) {
    xmlrpc_env env;
    char ** triplet;

    xmlrpc_env_init(&env);

    for (triplet = base64_triplets; *triplet != NULL; triplet += 3) {
        char * bin_data;
        char * nocrlf_ascii_data;
        char * ascii_data;
        xmlrpc_mem_block * output;

        bin_data = *triplet;
        nocrlf_ascii_data = *(triplet + 1);
        ascii_data = *(triplet + 2);

        /* Test our encoding routine. */
        output = xmlrpc_base64_encode(&env,
                                      (unsigned char*) bin_data,
                                      strlen(bin_data));
        TEST_NO_FAULT(&env);
        TEST(output != NULL);
        TEST(xmlrpc_mem_block_size(output) == strlen(ascii_data));
        TEST(memcmp(xmlrpc_mem_block_contents(output), ascii_data,
                    strlen(ascii_data)) == 0);
        xmlrpc_mem_block_free(output);

        /* Test our newline-free encoding routine. */
        output =
            xmlrpc_base64_encode_without_newlines(&env,
                                                  (unsigned char*) bin_data,
                                                  strlen(bin_data));
        TEST_NO_FAULT(&env);
        TEST(output != NULL);
        TEST(xmlrpc_mem_block_size(output) == strlen(nocrlf_ascii_data));
        TEST(memcmp(xmlrpc_mem_block_contents(output), nocrlf_ascii_data,
                    strlen(nocrlf_ascii_data)) == 0);
        xmlrpc_mem_block_free(output);

        /* Test our decoding routine. */
        output = xmlrpc_base64_decode(&env, ascii_data, strlen(ascii_data));
        TEST_NO_FAULT(&env);
        TEST(output != NULL);
        TEST(xmlrpc_mem_block_size(output) == strlen(bin_data));
        TEST(memcmp(xmlrpc_mem_block_contents(output), bin_data,
                    strlen(bin_data)) == 0);
        xmlrpc_mem_block_free(output);
    }

    /* Now for something broken... */
    {
        xmlrpc_env env2;
        xmlrpc_mem_block * output;

        xmlrpc_env_init(&env2);
        output = xmlrpc_base64_decode(&env2, "====", 4);
        TEST(output == NULL);
        TEST_FAULT(&env2, XMLRPC_PARSE_ERROR);
        xmlrpc_env_clean(&env2);
    }
    /* Now for something broken in a really sneaky way... */
    {
        xmlrpc_env env2;
        xmlrpc_mem_block * output;
        xmlrpc_env_init(&env2);
        output = xmlrpc_base64_decode(&env2, "a==", 4);
        TEST(output == NULL);
        TEST_FAULT(&env2, XMLRPC_PARSE_ERROR);
        xmlrpc_env_clean(&env2);
    }
    xmlrpc_env_clean(&env);
}
예제 #9
0
static void test_mem_block (void)
{
    xmlrpc_env env;
    xmlrpc_mem_block* block;

    xmlrpc_mem_block* typed_heap_block;
    xmlrpc_mem_block typed_auto_block;
    void** typed_contents;

    xmlrpc_env_init(&env);

    /* Allocate a zero-size block. */
    block = xmlrpc_mem_block_new(&env, 0);
    TEST_NO_FAULT(&env);
    TEST(block != NULL);
    TEST(xmlrpc_mem_block_size(block) == 0);

    /* Grow the block a little bit. */
    xmlrpc_mem_block_resize(&env, block, strlen(test_string_1) + 1);
    TEST_NO_FAULT(&env);
    TEST(xmlrpc_mem_block_size(block) == strlen(test_string_1) + 1);
    
    /* Insert a string into the block, and resize it by large amount.
    ** We want to cause a reallocation and copy of the block contents. */
    strcpy(xmlrpc_mem_block_contents(block), test_string_1);
    xmlrpc_mem_block_resize(&env, block, 10000);
    TEST_NO_FAULT(&env);
    TEST(xmlrpc_mem_block_size(block) == 10000);
    TEST(strcmp(xmlrpc_mem_block_contents(block), test_string_1) == 0);

    /* Test cleanup code (with help from memprof). */
    xmlrpc_mem_block_free(block);
    
    /* Allocate a bigger block. */
    block = xmlrpc_mem_block_new(&env, 128);
    TEST_NO_FAULT(&env);
    TEST(block != NULL);
    TEST(xmlrpc_mem_block_size(block) == 128);

    /* Test cleanup code (with help from memprof). */
    xmlrpc_mem_block_free(block);

    /* Allocate a "typed" memory block. */
    typed_heap_block = XMLRPC_TYPED_MEM_BLOCK_NEW(void*, &env, 20);
    TEST_NO_FAULT(&env);
    TEST(typed_heap_block != NULL);
    TEST(XMLRPC_TYPED_MEM_BLOCK_SIZE(void*, typed_heap_block) == 20);
    typed_contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(void*, typed_heap_block);
    TEST(typed_contents != NULL);

    /* Resize a typed memory block. */
    XMLRPC_TYPED_MEM_BLOCK_RESIZE(void*, &env, typed_heap_block, 100);
    TEST_NO_FAULT(&env);
    TEST(XMLRPC_TYPED_MEM_BLOCK_SIZE(void*, typed_heap_block) == 100);

    /* Test cleanup code (with help from memprof). */
    XMLRPC_TYPED_MEM_BLOCK_FREE(void*, typed_heap_block);

    /* Test _INIT and _CLEAN for stack-based memory blocks. */
    XMLRPC_TYPED_MEM_BLOCK_INIT(void*, &env, &typed_auto_block, 30);
    TEST(XMLRPC_TYPED_MEM_BLOCK_SIZE(void*, &typed_auto_block) == 30);
    XMLRPC_TYPED_MEM_BLOCK_CLEAN(void*, &typed_auto_block);

    /* Test xmlrpc_mem_block_append. */
    block = XMLRPC_TYPED_MEM_BLOCK_NEW(int, &env, 5);
    TEST_NO_FAULT(&env);
    memcpy(XMLRPC_TYPED_MEM_BLOCK_CONTENTS(int, block),
           test_int_array_1, sizeof(test_int_array_1));
    XMLRPC_TYPED_MEM_BLOCK_APPEND(int, &env, block, test_int_array_2, 3);
    TEST(XMLRPC_TYPED_MEM_BLOCK_SIZE(int, block) == 8);
    TEST(memcmp(XMLRPC_TYPED_MEM_BLOCK_CONTENTS(int, block),
                test_int_array_3, sizeof(test_int_array_3)) == 0);
    XMLRPC_TYPED_MEM_BLOCK_FREE(int, block);

    xmlrpc_env_clean(&env);
}
예제 #10
0
파일: rcd-rpc.c 프로젝트: joeshaw/rcd
static GByteArray *
unix_rpc_callback (RCDUnixServerHandle *handle)
{
    xmlrpc_env env;
    xmlrpc_mem_block *output;
    GByteArray *out_data;
    RCDIdentity *identity = NULL;
    RCDRPCMethodData *method_data;

    xmlrpc_env_init(&env);

    if (handle->uid != 0) {
        struct passwd *pw;
        
        pw = getpwuid (handle->uid);
        if (!pw) {
            rc_debug (RC_DEBUG_LEVEL_WARNING,
                      "Couldn't get info for UID %d\n", handle->uid);
            identity = NULL;
        }
        else {
            identity = rcd_identity_lookup (pw->pw_name);

            if (!identity) {
                identity = rcd_identity_new ();
                identity->username = g_strdup (pw->pw_name);
                identity->privileges = rcd_privileges_from_string ("view");
            }
        }
    }
    else {
        identity = rcd_identity_new ();
        identity->username = g_strdup ("root");
        identity->privileges = rcd_privileges_from_string ("superuser");
    }
    
    if (!identity) {
        output = serialize_fault (RCD_RPC_FAULT_PERMISSION_DENIED,
                                  "Permission denied");
        goto finish_request;
    }

    method_data = g_new0 (RCDRPCMethodData, 1);
    method_data->host = "local";
    method_data->identity = identity;

    output = process_rpc_call (
        &env, handle->data->data, handle->data->len, method_data);

    rcd_identity_free (method_data->identity);
    g_free (method_data);

    if (env.fault_occurred) {
        rc_debug (RC_DEBUG_LEVEL_ERROR, "Some weird fault during registry processing");
        return NULL;
    }

finish_request:
    out_data = g_byte_array_new ();
    out_data = g_byte_array_append (
        out_data, 
        XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, output),
        XMLRPC_TYPED_MEM_BLOCK_SIZE(char, output));

    xmlrpc_mem_block_free(output);

    return out_data;
} /* unix_rpc_callback */