Example #1
0
APR_DECLARE(apr_status_t) apr_file_pipe_create_pools(apr_file_t **in,
                                                     apr_file_t **out,
                                                     apr_int32_t blocking,
                                                     apr_pool_t *pool_in,
                                                     apr_pool_t *pool_out)
{
#ifdef _WIN32_WCE
    return APR_ENOTIMPL;
#else
    SECURITY_ATTRIBUTES sa;
    static unsigned long id = 0;
    DWORD dwPipeMode;
    DWORD dwOpenMode;

    sa.nLength = sizeof(sa);

#if APR_HAS_UNICODE_FS
    IF_WIN_OS_IS_UNICODE
        sa.bInheritHandle = FALSE;
#endif
#if APR_HAS_ANSI_FS
    ELSE_WIN_OS_IS_ANSI
        sa.bInheritHandle = TRUE;
#endif
    sa.lpSecurityDescriptor = NULL;

    (*in) = (apr_file_t *)apr_pcalloc(pool_in, sizeof(apr_file_t));
    (*in)->pool = pool_in;
    (*in)->fname = NULL;
    (*in)->pipe = 1;
    (*in)->timeout = -1;
    (*in)->ungetchar = -1;
    (*in)->eof_hit = 0;
    (*in)->filePtr = 0;
    (*in)->bufpos = 0;
    (*in)->dataRead = 0;
    (*in)->direction = 0;
    (*in)->pOverlapped = NULL;
#if APR_FILES_AS_SOCKETS
    (void) apr_pollset_create(&(*in)->pollset, 1, p, 0);
#endif
    (*out) = (apr_file_t *)apr_pcalloc(pool_out, sizeof(apr_file_t));
    (*out)->pool = pool_out;
    (*out)->fname = NULL;
    (*out)->pipe = 1;
    (*out)->timeout = -1;
    (*out)->ungetchar = -1;
    (*out)->eof_hit = 0;
    (*out)->filePtr = 0;
    (*out)->bufpos = 0;
    (*out)->dataRead = 0;
    (*out)->direction = 0;
    (*out)->pOverlapped = NULL;
#if APR_FILES_AS_SOCKETS
    (void) apr_pollset_create(&(*out)->pollset, 1, p, 0);
#endif
    if (apr_os_level >= APR_WIN_NT) {
        char rand[8];
        int pid = getpid();
#define FMT_PIPE_NAME "\\\\.\\pipe\\apr-pipe-%x.%lx."
        /*                                    ^   ^ ^
         *                                  pid   | |
         *                                        | |
         *                                       id |
         *                                          |
         *                        hex-escaped rand[8] (16 bytes)
         */
        char name[sizeof FMT_PIPE_NAME + 2 * sizeof(pid)
                                       + 2 * sizeof(id)
                                       + 2 * sizeof(rand)];
        apr_size_t pos;

        /* Create the read end of the pipe */
        dwOpenMode = PIPE_ACCESS_INBOUND;
#ifdef FILE_FLAG_FIRST_PIPE_INSTANCE
        dwOpenMode |= FILE_FLAG_FIRST_PIPE_INSTANCE;
#endif
        if (blocking == APR_WRITE_BLOCK /* READ_NONBLOCK */
               || blocking == APR_FULL_NONBLOCK) {
            dwOpenMode |= FILE_FLAG_OVERLAPPED;
            (*in)->pOverlapped =
                    (OVERLAPPED*) apr_pcalloc((*in)->pool, sizeof(OVERLAPPED));
            (*in)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
            (*in)->timeout = 0;
        }
        dwPipeMode = 0;

        apr_generate_random_bytes(rand, sizeof rand);
        pos = apr_snprintf(name, sizeof name, FMT_PIPE_NAME, pid, id++);
        apr_escape_hex(name + pos, rand, sizeof rand, 0, NULL);

        (*in)->filehand = CreateNamedPipe(name,
                                          dwOpenMode,
                                          dwPipeMode,
                                          1,            /* nMaxInstances,   */
                                          0,            /* nOutBufferSize,  */
                                          65536,        /* nInBufferSize,   */
                                          1,            /* nDefaultTimeOut, */
                                          &sa);
        if ((*in)->filehand == INVALID_HANDLE_VALUE) {
            apr_status_t rv = apr_get_os_error();
            file_cleanup(*in);
            return rv;
        }

        /* Create the write end of the pipe */
        dwOpenMode = FILE_ATTRIBUTE_NORMAL;
        if (blocking == APR_READ_BLOCK /* WRITE_NONBLOCK */
                || blocking == APR_FULL_NONBLOCK) {
            dwOpenMode |= FILE_FLAG_OVERLAPPED;
            (*out)->pOverlapped =
                    (OVERLAPPED*) apr_pcalloc((*out)->pool, sizeof(OVERLAPPED));
            (*out)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
            (*out)->timeout = 0;
        }

        (*out)->filehand = CreateFile(name,
                                      GENERIC_WRITE,   /* access mode             */
                                      0,               /* share mode              */
                                      &sa,             /* Security attributes     */
                                      OPEN_EXISTING,   /* dwCreationDisposition   */
                                      dwOpenMode,      /* Pipe attributes         */
                                      NULL);           /* handle to template file */
        if ((*out)->filehand == INVALID_HANDLE_VALUE) {
            apr_status_t rv = apr_get_os_error();
            file_cleanup(*out);
            file_cleanup(*in);
            return rv;
        }
    }
    else {
        /* Pipes on Win9* are blocking. Live with it. */
        if (!CreatePipe(&(*in)->filehand, &(*out)->filehand, &sa, 65536)) {
            return apr_get_os_error();
        }
    }

    apr_pool_cleanup_register((*in)->pool, (void *)(*in), file_cleanup,
                        apr_pool_cleanup_null);
    apr_pool_cleanup_register((*out)->pool, (void *)(*out), file_cleanup,
                        apr_pool_cleanup_null);
    return APR_SUCCESS;
#endif /* _WIN32_WCE */
}
Example #2
0
static void test_escape(abts_case *tc, void *data)
{
    apr_pool_t *pool;
    const char *src, *target;
    const char *dest;
    const void *vdest;
    apr_size_t len, vlen;

    apr_pool_create(&pool, NULL);

    src = "Hello World &;`'\"|*?~<>^()[]{}$\\";
    target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\";
    dest = apr_pescape_shell(pool, src);
    ABTS_ASSERT(tc,
                apr_psprintf(pool, "shell escaped (%s) does not match expected output (%s)",
                             dest, target),
                (strcmp(dest, target) == 0));
    apr_escape_shell(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

#if !(defined(OS2) || defined(WIN32))
    /* Now try with newline, which is converted to a space on OS/2 and Windows.
     */
    src = "Hello World &;`'\"|*?~<>^()[]{}$\\\n";
    target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\\\\n";
    dest = apr_pescape_shell(pool, src);
    ABTS_ASSERT(tc,
                apr_psprintf(pool, "shell escaped (%s) does not match expected output (%s)",
                             dest, target),
                (strcmp(dest, target) == 0));
    apr_escape_shell(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));
#endif

    src = "Hello";
    dest = apr_punescape_url(pool, src, NULL, NULL, 0);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "Hello";
    dest = apr_punescape_url(pool, src, NULL, NULL, 1);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "Hello%20";
    dest = apr_punescape_url(pool, src, " ", NULL, 0);
    ABTS_PTR_EQUAL(tc, NULL, dest);

    src = "Hello%20World";
    target = "Hello World";
    dest = apr_punescape_url(pool, src, NULL, NULL, 0);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_unescape_url(NULL, src, APR_ESCAPE_STRING, NULL, NULL, 0, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello+World";
    target = "Hello World";
    dest = apr_punescape_url(pool, src, NULL, NULL, 1);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_unescape_url(NULL, src, APR_ESCAPE_STRING, NULL, NULL, 1, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello%20World";
    target = "Hello%20World";
    dest = apr_punescape_url(pool, src, NULL, " ", 0);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_unescape_url(NULL, src, APR_ESCAPE_STRING, NULL, " ", 0, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello";
    dest = apr_pescape_path_segment(pool, src);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "$-_.+!*'(),:@&=/~Hello World";
    target = "$-_.+!*'(),:@&=%2f~Hello%20World";
    dest = apr_pescape_path_segment(pool, src);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_path_segment(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello";
    dest = apr_pescape_path(pool, src, 0);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "$-_.+!*'(),:@&=/~Hello World";
    target = "./$-_.+!*'(),:@&=/~Hello%20World";
    dest = apr_pescape_path(pool, src, 0);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_path(NULL, src, APR_ESCAPE_STRING, 0, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello";
    dest = apr_pescape_path(pool, src, 1);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "$-_.+!*'(),:@&=/~Hello World";
    target = "$-_.+!*'(),:@&=/~Hello%20World";
    dest = apr_pescape_path(pool, src, 1);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_path(NULL, src, APR_ESCAPE_STRING, 1, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello";
    dest = apr_pescape_urlencoded(pool, src);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "$-_.+!*'(),:@&=/~Hello World";
    target = "%24-_.%2b%21*%27%28%29%2c%3a%40%26%3d%2f%7eHello+World";
    dest = apr_pescape_urlencoded(pool, src);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_urlencoded(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello";
    dest = apr_pescape_entity(pool, src, 0);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "\xFF<>&\'\"Hello World";
    target = "\xFF&lt;&gt;&amp;'&quot;Hello World";
    dest = apr_pescape_entity(pool, src, 0);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_entity(NULL, src, APR_ESCAPE_STRING, 0, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

#if !APR_CHARSET_EBCDIC
    src = "Hello";
    dest = apr_pescape_entity(pool, src, 1);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "\xFF<>&\'\"Hello World";
    target = "&#255&lt;&gt;&amp;'&quot;Hello World";
    dest = apr_pescape_entity(pool, src, 1);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_entity(NULL, src, APR_ESCAPE_STRING, 1, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "Hello";
    dest = apr_punescape_entity(pool, src);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "\xFF&lt;&gt;&amp;'&quot;Hello World";
    target = "\xFF<>&\'\"Hello World";
    dest = apr_punescape_entity(pool, src);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_unescape_entity(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "&#255;&lt;&gt;&amp;'&quot;Hello World";
    target = "\xFF<>&\'\"Hello World";
    dest = apr_punescape_entity(pool, src);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_unescape_entity(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "&#32;&lt;&gt;&amp;'&quot;Hello World";
    target = " <>&\'\"Hello World";
    dest = apr_punescape_entity(pool, src);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_unescape_entity(NULL, src, APR_ESCAPE_STRING, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));
#endif

    src = "Hello";
    dest = apr_pescape_echo(pool, src, 0);
    ABTS_PTR_EQUAL(tc, src, dest);

    src = "\a\b\f\\n\r\t\v\"Hello World\"";
    target = "\\a\\b\\f\\\\n\\r\\t\\v\"Hello World\"";
    dest = apr_pescape_echo(pool, src, 0);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_echo(NULL, src, APR_ESCAPE_STRING, 0, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "\a\b\f\\n\r\t\v\"Hello World\"";
    target = "\\a\\b\\f\\\\n\\r\\t\\v\\\"Hello World\\\"";
    dest = apr_pescape_echo(pool, src, 1);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_echo(NULL, src, APR_ESCAPE_STRING, 1, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "\xFF\x00\xFF\x00";
    target = "ff00ff00";
    dest = apr_pescape_hex(pool, src, 4, 0);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_hex(NULL, src, 4, 0, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "\xFF\x00\xFF\x00";
    target = "ff:00:ff:00";
    dest = apr_pescape_hex(pool, src, 4, 1);
    ABTS_STR_EQUAL(tc, target, dest);
    apr_escape_hex(NULL, src, 4, 1, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
            (len == strlen(dest) + 1));

    src = "ff:00:ff:00";
    target = "\xFF\x00\xFF\x00";
    vdest = apr_punescape_hex(pool, src, 1, &vlen);
    ABTS_ASSERT(tc, "apr_punescape_hex target!=dest", memcmp(target, vdest, 4) == 0);
    ABTS_INT_EQUAL(tc, (int)vlen, 4);
    apr_unescape_hex(NULL, src, APR_ESCAPE_STRING, 1, &len);
    ABTS_ASSERT(tc,
            apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, (apr_size_t)4),
            (len == 4));

    apr_pool_destroy(pool);
}