Esempio n. 1
0
static void test_xthread(abts_case *tc, void *data)
{
    apr_file_t *f;
    const char *fname = "data/testxthread.dat";
    apr_status_t rv;
    apr_int32_t flags = APR_FOPEN_CREATE|APR_FOPEN_READ|APR_FOPEN_WRITE|APR_FOPEN_APPEND|APR_FOPEN_XTHREAD;
    char buf[128] = { 0 };

    /* Test for bug 38438, opening file with append + xthread and seeking to
       the end of the file resulted in writes going to the beginning not the
       end. */

    apr_file_remove(fname, p);

    APR_ASSERT_SUCCESS(tc, "open test file",
                       apr_file_open(&f, fname, flags,
                                     APR_UREAD|APR_UWRITE, p));

    APR_ASSERT_SUCCESS(tc, "write should succeed",
                       apr_file_puts("hello", f));

    apr_file_close(f);

    APR_ASSERT_SUCCESS(tc, "open test file",
                       apr_file_open(&f, fname, flags,
                                     APR_UREAD|APR_UWRITE, p));

    /* Seek to the end. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_END, &offset);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    }

    APR_ASSERT_SUCCESS(tc, "more writes should succeed",
                       apr_file_puts("world", f));

    /* Back to the beginning. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_SET, &offset);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    }

    apr_file_read_full(f, buf, sizeof(buf), NULL);

    ABTS_STR_EQUAL(tc, "helloworld", buf);

    apr_file_close(f);
}
Esempio n. 2
0
static void do_write(apr_file_t *file, tee_saved_request *sr)
{
	apr_file_printf(file, "%s\n", sr->rec_line);
	apr_table_do(write_header, file, sr->headers_in, NULL);
	apr_file_puts("\n", file);
	dump_data(file, sr->in->first);
	apr_file_puts("\n<<<<---->>>>\n\n", file);
	apr_file_printf(file, "%d (%s)\n", sr->status, sr->status_line);
	apr_table_do(write_header, file, sr->headers_out, NULL);
	apr_table_do(write_header, file, sr->err_headers_out, NULL);
	apr_file_puts("\n", file);
	dump_data(file, sr->out->first);
}
Esempio n. 3
0
static void test_xthread(CuTest *tc)
{
    apr_file_t *f;
    const char *fname = "data/testxthread.dat";
    apr_status_t rv;
    apr_int32_t flags = APR_CREATE|APR_READ|APR_WRITE|APR_APPEND|APR_XTHREAD;
    char buf[128] = { 0 };

    /* Test for bug 38438, opening file with append + xthread and seeking to 
       the end of the file resulted in writes going to the beginning not the
       end. */

    apr_file_remove(fname, p);

    rv = apr_file_open(&f, fname, flags, APR_UREAD|APR_UWRITE, p);
    CuAssert(tc, "open test file", rv == APR_SUCCESS);

    rv = apr_file_puts("hello", f);
    CuAssert(tc, "write should succeed", rv == APR_SUCCESS);

    apr_file_close(f);
    
    rv = apr_file_open(&f, fname, flags, APR_UREAD|APR_UWRITE, p);
    CuAssert(tc, "open test file", rv == APR_SUCCESS);

    /* Seek to the end. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_END, &offset);
    }

    rv = apr_file_puts("world", f);
    CuAssert(tc, "more writes should succeed", rv == APR_SUCCESS);

    /* Back to the beginning. */
    {
        apr_off_t offset = 0;
        
        rv = apr_file_seek(f, APR_SET, &offset);
    }
    
    apr_file_read_full(f, buf, sizeof(buf), NULL);

    CuAssertStrEquals(tc, "helloworld", buf);

    apr_file_close(f);
}
Esempio n. 4
0
/* ------------------------------------------- */
static int vlimit_logging(const char *msg, request_rec *r, vlimit_config *cfg, SHM_DATA *limit_stat)
{
    int len;
    time_t t;
    char *log_time;
    char *vlimit_log_buf;

    if (access(VLIMIT_LOG_FLAG_FILE, F_OK) == 0) {
        time(&t);
        log_time = (char *)ctime(&t);
        len = strlen(log_time);
        log_time[len - 1] = '\0';

        vlimit_log_buf = (char *)apr_psprintf(r->pool
            , "[%s] pid=[%d] name=[%s] client=[%s] %s ip_count: %d/%d file_count: %d/%d file=[%s] \n"
            , log_time
            , getpid()
            , apr_table_get(r->headers_in, "HOST")
            , r->connection->remote_ip
            , msg
            , get_ip_counter(limit_stat, r)
            , cfg->ip_limit
            , get_file_counter(limit_stat, r)
            , cfg->file_limit
            , r->filename
        );

        apr_file_puts(vlimit_log_buf, vlimit_log_fp);
        apr_file_flush(vlimit_log_fp);

        return 0;
    }
    
    return -1;
}
Esempio n. 5
0
/*++

LogFormatV

    Adds an entry to the log file.

Arguments:
    level   - Log severity level.

    format  - Pointer to a buffer containing a printf-style format string.

    argList - Argument list to insert into 'format'.

Return Values:
    None.

Remarks:
    This function could be called before the logging subsystem is initialized.

--*/
void
LogFormatV(
    apr_uint32_t level,
    const char *format,
    va_list argList
    )
{
    apr_time_exp_t now;
    char *message;

    ASSERT(format != NULL);

    if (level <= maxLevel && handle != NULL) {
        // Write local time
        apr_time_exp_lt(&now, apr_time_now());
        apr_file_printf(handle, "%04d-%02d-%02d %02d:%02d:%02d - ",
            now.tm_year+1900, now.tm_mon, now.tm_mday,
            now.tm_hour, now.tm_min, now.tm_sec);

        // Format and write log message
        message = apr_pvsprintf(msgPool, format, argList);
        if (message == NULL) {
            message = "Unable to format message." APR_EOL_STR;
        }
        apr_file_puts(message, handle);
        apr_file_flush(handle);

        // Clear memory allocated when formatting the message
        apr_pool_clear(msgPool);
    }
}
Esempio n. 6
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int jhklog_write(const char *message)
{
    int rc;
    char errmsg[100];
    apr_status_t rv;

    if (jx_log == NULL || message == NULL)
        return -1;

    if (jx_log->fp == NULL) {
        printf("%s", message);
        return 0;
    }

    rc = -1;
    jhklog_lock();

    if (jhklog_rotate() != 0)
        goto error;

    rv = apr_file_puts(message, jx_log->fp);
    if (rv != APR_SUCCESS) {
        apr_strerror(rv, errmsg, sizeof(errmsg));
        fprintf(stderr, "can not write to the file [%s]. %s\n",
                jx_log->logfile, errmsg);
        goto error;
    }
    apr_file_flush(jx_log->fp);

    rc = 0;
  error:
    jhklog_unlock();
    return rc;
}
Esempio n. 7
0
static void test_fail_read_flush(CuTest *tc)
{
    apr_file_t *f;
    const char *fname = "data/testflush.dat";
    apr_status_t rv;
    char buf[2];

    apr_file_remove(fname, p);

    apr_assert_success(tc, "open test file",
                       apr_file_open(&f, fname,
                                     APR_CREATE|APR_READ|APR_BUFFERED,
                                     APR_UREAD|APR_UWRITE, p));

    /* this write should be buffered. */
    apr_assert_success(tc, "buffered write should succeed",
                       apr_file_puts("hello", f));

    /* Now, trying a read should fail since the write must be flushed,
     * and should fail with something other than EOF since the file is
     * opened read-only. */
    rv = apr_file_read_full(f, buf, 2, NULL);

    CuAssert(tc, "read should flush buffered write and fail",
             rv != APR_SUCCESS && rv != APR_EOF);

    /* Likewise for gets */
    rv = apr_file_gets(buf, 2, f);

    CuAssert(tc, "gets should flush buffered write and fail",
             rv != APR_SUCCESS && rv != APR_EOF);

    apr_file_close(f);
    apr_file_remove(fname, p);
}
Esempio n. 8
0
void putline(apr_file_t *f, const char *l)
{
    apr_status_t rv;
    rv = apr_file_puts(l, f);
    if (rv != APR_SUCCESS) {
        apr_file_printf(errfile, "Error writing temp file: %pm", &rv);
        apr_file_close(f);
        exit(ERR_FILEPERM);
    }
}
Esempio n. 9
0
static void test_puts(abts_case *tc, void *data)
{
    apr_file_t *f;
    const char *fname = "data/testputs.txt";

    APR_ASSERT_SUCCESS(tc, "open file for writing",
                       apr_file_open(&f, fname, 
                                     APR_WRITE|APR_CREATE|APR_TRUNCATE, 
                                     APR_OS_DEFAULT, p));
    
    APR_ASSERT_SUCCESS(tc, "write line to file", 
                       apr_file_puts(LINE1, f));
    APR_ASSERT_SUCCESS(tc, "write second line to file", 
                       apr_file_puts(LINE2, f));
    
    APR_ASSERT_SUCCESS(tc, "close for writing",
                       apr_file_close(f));

    file_contents_equal(tc, fname, LINE1 LINE2, strlen(LINE1 LINE2));
}
Esempio n. 10
0
static void create_testfile(apr_pool_t *p, const char *fname)
{
    apr_file_t *f = NULL;
    apr_status_t rv;
    char buf[120];
    int i;
    apr_finfo_t finfo;

    printf("Creating a test file...\n");
    rv = apr_file_open(&f, fname, 
                 APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_TRUNCATE | APR_FOPEN_BUFFERED,
                 APR_UREAD | APR_UWRITE, p);
    if (rv) {
        aprerr("apr_file_open()", rv);
    }
    
    buf[0] = FILE_DATA_CHAR;
    buf[1] = '\0';
    for (i = 0; i < FILE_LENGTH; i++) {
        /* exercise apr_file_putc() and apr_file_puts() on buffered files */
        if ((i % 2) == 0) {
            rv = apr_file_putc(buf[0], f);
            if (rv) {
                aprerr("apr_file_putc()", rv);
            }
        }
        else {
            rv = apr_file_puts(buf, f);
            if (rv) {
                aprerr("apr_file_puts()", rv);
            }
        }
    }

    rv = apr_file_close(f);
    if (rv) {
        aprerr("apr_file_close()", rv);
    }

    rv = apr_stat(&finfo, fname, APR_FINFO_NORM, p);
    if (rv != APR_SUCCESS && ! APR_STATUS_IS_INCOMPLETE(rv)) {
        aprerr("apr_stat()", rv);
    }

    if (finfo.size != FILE_LENGTH) {
        fprintf(stderr, 
                "test file %s should be %ld-bytes long\n"
                "instead it is %ld-bytes long\n",
                fname,
                (long int)FILE_LENGTH,
                (long int)finfo.size);
        exit(1);
    }
}
Esempio n. 11
0
static void putline(apr_file_t *f, const char *l)
{
    apr_status_t rc;
    rc = apr_file_puts(l, f);
    if (rc != APR_SUCCESS) {
        char errstr[MAX_STRING_LEN];
        apr_strerror(rc, errstr, MAX_STRING_LEN);
        apr_file_printf(errfile, "Error writing temp file: %s" NL, errstr);
        apr_file_close(f);
        exit(ERR_FILEPERM);
    }
}
Esempio n. 12
0
static apr_status_t create_dummy_file(apr_pool_t *p, apr_file_t **fd)
{
    apr_status_t rv;
    char *tmpfile;
    int i;
    apr_off_t off = 0L;
    tmpfile = tmpnam(tname_buf);

    if ((tmpfile == NULL) || (*tmpfile == '\0')) {
        fprintf(stderr, "unable to generate temporary filename\n");
        if (errno == 0) {
            errno = ENOENT;
        }
        perror("tmpnam");
        return APR_ENOENT;
    }
    rv = apr_file_open(fd, tmpfile, APR_CREATE|APR_TRUNCATE|APR_DELONCLOSE|
                       APR_READ|APR_WRITE|APR_EXCL, APR_OS_DEFAULT, p);

    if (rv != APR_SUCCESS)
        return rv;
    rv = apr_file_puts("<?xml version=\"1.0\" ?>\n<mary>"
                       "<had a=\"little\"/><lamb its='fleece "
                       "was white as snow' />\n", *fd);
    if (rv != APR_SUCCESS)
        return rv;

    for (i = 0; i < 5000; i++) {
        rv = apr_file_puts("<hmm roast=\"lamb\" "
                           "for=\"dinner\">yummy</hmm>\n", *fd);
        if (rv != APR_SUCCESS)
            return rv;
    }
    rv = apr_file_puts("</mary>\n", *fd);
    if (rv != APR_SUCCESS)
        return rv;

    rv = apr_file_seek(*fd, APR_SET, &off);
    return rv;
}
Esempio n. 13
0
/* Make a test file named FNAME, and write CONTENTS to it. */
static apr_file_t *make_test_file(abts_case *tc, const char *fname,
                                  const char *contents)
{
    apr_file_t *f;

    ABTS_ASSERT(tc, "create test file",
                apr_file_open(&f, fname,
                              APR_READ|APR_WRITE|APR_TRUNCATE|APR_CREATE,
                              APR_OS_DEFAULT, p) == APR_SUCCESS);
    
    ABTS_ASSERT(tc, "write test file contents",
                apr_file_puts(contents, f) == APR_SUCCESS);

    return f;
}
Esempio n. 14
0
static int make_file_slot_list(SHM_DATA *limit_stat, request_rec *r) {

    int i;
    int len;
    time_t t;
    char *log_time;
    char *vlimit_log_buf;

    if (access(VLIMIT_FILE_STAT_FLAG_FILE, F_OK) == 0 && access(VLIMIT_FILE_STAT_FILE, F_OK) != 0) {
        time(&t);
        log_time = (char *)ctime(&t);
        len = strlen(log_time);
        log_time[len - 1] = '\0';

        apr_file_t *vlimit_make_file_slot_fp = NULL;

        if(apr_file_open(&vlimit_make_file_slot_fp, VLIMIT_FILE_STAT_FILE, APR_WRITE|APR_APPEND|APR_CREATE,
            APR_OS_DEFAULT, r->pool) != APR_SUCCESS){
            return OK;
        }

    
        for (i = 0; i < MAX_CLIENTS; i++) { 
            if (limit_stat->file_stat_shm[i].counter > 0) {
                vlimit_log_buf = (char *)apr_psprintf(r->pool
                    , "[%s] slot=[%d] filename=[%s] counter=[%d]\n"
                    , log_time
                    , i
                    , limit_stat->file_stat_shm[i].filename
                    , limit_stat->file_stat_shm[i].counter
                );
                apr_file_puts(vlimit_log_buf, vlimit_make_file_slot_fp);
            }
        }

        apr_file_flush(vlimit_make_file_slot_fp);

        if(apr_file_close(vlimit_make_file_slot_fp) != APR_SUCCESS){
            return OK;
        }

        return 0;
    }
    
    return -1;
}
static int resource_manager_detached(request_rec *r)
{
    mrm_config_t *conf = ap_get_module_config(r->server->module_config, &resource_manager_module);

    if (strcmp(apr_table_get(r->headers_in, "HOST"), conf->host) == 0) {
        manage_dir  = apr_psprintf(r->pool, "%s/%s", CGROUP_APACHE_ROOT, conf->host);

        if(apr_file_open(&manage_fp, manage_cpu, APR_WRITE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS){
            return OK;
        }
        apr_file_puts("100000\n", manage_fp);
        apr_file_flush(manage_fp);
        apr_file_close(manage_fp);
    }

    return OK;
}
Esempio n. 16
0
static void test_fail_read_flush(abts_case *tc, void *data)
{
    apr_file_t *f;
    const char *fname = "data/testflush.dat";
    apr_status_t rv;
    char buf[2];

    apr_file_remove(fname, p);

    APR_ASSERT_SUCCESS(tc, "open test file",
                       apr_file_open(&f, fname,
                                     APR_CREATE|APR_READ|APR_BUFFERED,
                                     APR_UREAD|APR_UWRITE, p));

    /* this write should be buffered. */
    APR_ASSERT_SUCCESS(tc, "buffered write should succeed",
                       apr_file_puts("hello", f));

    /* Now, trying a read should fail since the write must be flushed,
     * and should fail with something other than EOF since the file is
     * opened read-only. */
    rv = apr_file_read_full(f, buf, 2, NULL);

    ABTS_ASSERT(tc, "read should flush buffered write and fail",
                rv != APR_SUCCESS && rv != APR_EOF);

    /* Likewise for gets */
    rv = apr_file_gets(buf, 2, f);

    ABTS_ASSERT(tc, "gets should flush buffered write and fail",
                rv != APR_SUCCESS && rv != APR_EOF);

    /* Likewise for seek. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_SET, &offset);
    }

    ABTS_ASSERT(tc, "seek should flush buffered write and fail",
                rv != APR_SUCCESS && rv != APR_EOF);

    apr_file_close(f);
}
Esempio n. 17
0
APR_DECLARE_NONSTD(int) apr_file_printf(apr_file_t *fptr, 
                                        const char *format, ...)
{
    int cc;
    va_list ap;
    char *buf;
    int len;

    buf = malloc(HUGE_STRING_LEN);
    if (buf == NULL) {
        return 0;
    }
    va_start(ap, format);
    len = apr_vsnprintf(buf, HUGE_STRING_LEN, format, ap);
    cc = apr_file_puts(buf, fptr);
    va_end(ap);
    free(buf);
    return (cc == APR_SUCCESS) ? len : -1;
}
void mod_stlog_logging(json_object *json_obj, const char *func, apr_pool_t *p)
{
    int len;
    time_t t;
    char *log_time, *val;
    char *mod_stlog_buf = NULL;
     
    time(&t);
    log_time = (char *)ctime(&t);
    len = strlen(log_time);
    log_time[len - 1] = '\0';

    json_object_object_add(json_obj, "time", json_object_new_string(ap_mrb_string_check(p, log_time)));
    json_object_object_add(json_obj, "pid", json_object_new_int(getpid()));
    json_object_object_add(json_obj, "hook", json_object_new_string(ap_mrb_string_check(p, func)));

    val = (char *)json_object_to_json_string(json_obj);

    mod_stlog_buf = (char *)apr_psprintf(p, "%s\n", val);
         
    apr_file_puts(mod_stlog_buf, mod_stlog_fp);
    apr_file_flush(mod_stlog_fp);
}
Esempio n. 19
0
static int JK_METHOD jk2_logger_file_log(jk_env_t *env, jk_logger_t *l,                                 
                               int level,
                               const char *what)
{
    apr_status_t rv = APR_SUCCESS;
    apr_file_t *f = (apr_file_t *)l->logger_private;

    if (f == NULL) {
        /* This is usefull to debug what happens before logger is set.
           On apache you need -X option ( no detach, single process ) */
        if (what != NULL)
            fprintf(stderr, what);
        return JK_OK;
    }
    if(l && l->level <= level && l->logger_private && what) {       
        rv = apr_file_puts(what, f);
        /* flush the log for each entry only for debug level */
        if (rv == APR_SUCCESS && l->level == JK_LOG_DEBUG_LEVEL)
            rv = apr_file_flush(f);
        return JK_OK;
    }

    return JK_ERR;
}
Esempio n. 20
0
static void putline(apr_file_t *f, const char *l)
{
    apr_file_puts(l, f);
}
static int log_script(request_rec *r, cgi_server_conf * conf, int ret,
                      char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
                      apr_file_t *script_err)
{
    const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
    const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
    char argsbuffer[HUGE_STRING_LEN];
    apr_file_t *f = NULL;
    apr_bucket *e;
    const char *buf;
    apr_size_t len;
    apr_status_t rv;
    int first;
    int i;
    apr_finfo_t finfo;
    char time_str[APR_CTIME_LEN];

    /* XXX Very expensive mainline case! Open, then getfileinfo! */
    if (!conf->logname ||
        ((apr_stat(&finfo, conf->logname,
                   APR_FINFO_SIZE, r->pool) == APR_SUCCESS) &&
         (finfo.size > conf->logbytes)) ||
        (apr_file_open(&f, conf->logname,
                       APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT,
                       r->pool) != APR_SUCCESS)) {
        /* Soak up script output */
        discard_script_output(bb);
        log_script_err(r, script_err);
        return ret;
    }

    /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
    apr_ctime(time_str, apr_time_now());
    apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
                    r->args ? "?" : "", r->args ? r->args : "", r->protocol);
    /* "%% 500 /usr/local/apache/cgi-bin" */
    apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);

    apr_file_puts("%request\n", f);
    for (i = 0; i < hdrs_arr->nelts; ++i) {
        if (!hdrs[i].key)
            continue;
        apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
    }
    if ((r->method_number == M_POST || r->method_number == M_PUT) &&
        *dbuf) {
        apr_file_printf(f, "\n%s\n", dbuf);
    }

    apr_file_puts("%response\n", f);
    hdrs_arr = apr_table_elts(r->err_headers_out);
    hdrs = (const apr_table_entry_t *) hdrs_arr->elts;

    for (i = 0; i < hdrs_arr->nelts; ++i) {
        if (!hdrs[i].key)
            continue;
        apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
    }

    if (sbuf && *sbuf)
        apr_file_printf(f, "%s\n", sbuf);

    first = 1;
    for (e = APR_BRIGADE_FIRST(bb);
         e != APR_BRIGADE_SENTINEL(bb);
         e = APR_BUCKET_NEXT(e))
    {
        if (APR_BUCKET_IS_EOS(e)) {
            break;
        }
        rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
        if (rv != APR_SUCCESS || (len == 0)) {
            break;
        }
        if (first) {
            apr_file_puts("%stdout\n", f);
            first = 0;
        }
        apr_file_write(f, buf, &len);
        apr_file_puts("\n", f);
    }

    if (apr_file_gets(argsbuffer, HUGE_STRING_LEN, script_err) == APR_SUCCESS) {
        apr_file_puts("%stderr\n", f);
        apr_file_puts(argsbuffer, f);
        while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
                             script_err) == APR_SUCCESS) {
            apr_file_puts(argsbuffer, f);
        }
        apr_file_puts("\n", f);
    }

    apr_brigade_destroy(bb);
    apr_file_close(script_err);

    apr_file_close(f);
    return ret;
}
Esempio n. 22
0
/** Write a key in a file. */
int kdkey_write_key(apr_pool_t *parent_pool, const char *key_file, struct kdkey_info *ki) {
    int r = -1;
    apr_file_t *f;
    apr_status_t s;
    apr_pool_t *pool;
    const char *key_start = NULL, *key_end = NULL;
    char *str, *b64_data;
    size_t b64_sz, sz;

    apr_pool_create(&pool, parent_pool);

    s = apr_file_open(&f, key_file, APR_WRITE | APR_TRUNCATE | APR_CREATE, APR_OS_DEFAULT, pool);
    if (s != APR_SUCCESS) {
        KERROR_SET_APR(_keys_, 0, s);
        apr_pool_destroy(pool);
        return -1;
    }

    do {
        b64_sz = blockify_get_size(72, ki->data_s);
        b64_data = apr_palloc(pool, b64_sz);

        switch (ki->type) {
        case SKEY_ENCRYPTION:
            key_start = start_enc_skey;
            key_end = end_enc_skey;
            break;
        case SKEY_SIGNATURE:
            key_start = start_sig_skey;
            key_end = end_sig_skey;
            break;
        case PKEY_ENCRYPTION:
            key_start = start_enc_pkey;
            key_end = end_enc_pkey;
            break;
        case PKEY_SIGNATURE:
            key_start = start_sig_pkey;
            key_end = end_sig_pkey;
            break;
        default:
            KERROR_SET(_keys_, 0, "wrong key type");
            break;
        }
        if (key_start == NULL) break;
    
        blockify_base64(72, ki->data, ki->data_s, b64_data, b64_sz);

        s = apr_file_puts(key_start, f);
        if (s != APR_SUCCESS) goto write_error;
        
        str = apr_psprintf(pool, PRINTF_64"u\n", ki->key_id);
        s = apr_file_puts(str, f);
        if (s != APR_SUCCESS) goto write_error;

        sz = ki->owner_s;
        s = apr_file_write(f, ki->owner, &sz);
        if (s != APR_SUCCESS) goto write_error;

        s = apr_file_puts("\n", f);
        if (s != APR_SUCCESS) goto write_error;
        
        sz = b64_sz;
        s = apr_file_write(f, b64_data, &sz);
        if (s != APR_SUCCESS) goto write_error;

        if (b64_data[b64_sz - 1] != '\n') {
            s = apr_file_puts("\n", f);
            if (s != APR_SUCCESS) goto write_error;
        }           

        s = apr_file_puts(key_end, f);
        if (s != APR_SUCCESS) goto write_error;

        r = 0;
        break;

    write_error:
        KERROR_SET_APR(_keys_, 0, s);
        KERROR_PUSH(_keys_, 0, "error writing the key");

    } while (0);

    apr_pool_destroy(pool);

    return r;
}
Esempio n. 23
0
static int 
Ganglia_cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc,
                          const char **argv)
{
    char *fname = (char*)argv[0];
    struct stat statbuf;
    DIR *dir;
    struct dirent *entry;

    if(argc != 1)
    {
        cfg_error(cfg, "wrong number of arguments to cfg_include()");
        return 1;
    }

    if (stat (fname, &statbuf) == 0) 
    {
        return cfg_include(cfg, opt, argc, argv);
    }
    else if (has_wildcard(fname))
    {
        int ret;
        char *path = calloc(strlen(fname) + 1, sizeof(char));
        char *pattern = NULL;
        char *idx = strrchr(fname, '/');
        apr_pool_t *p;
        apr_file_t *ftemp;
        char *dirname = NULL;
        char tn[] = "gmond.tmp.XXXXXX";

        if (idx == NULL) {
            idx = strrchr(fname, '\\');
        }

        if (idx == NULL) {
            strncpy (path, ".", 1);
            pattern = fname;
        }
        else {
            strncpy (path, fname, idx - fname);
            pattern = idx + 1;
        }

        apr_pool_create(&p, NULL);
        if (apr_temp_dir_get((const char**)&dirname, p) != APR_SUCCESS) {
#ifndef LINUX
            cfg_error(cfg, "failed to determine the temp dir");
            apr_pool_destroy(p);
            return 1;
#else
            /*
             * workaround APR BUG46297 by using the POSIX shared memory
             * ramdrive that is available since glibc 2.2
             */
            dirname = apr_psprintf(p, "%s", "/dev/shm");
#endif
        }
        dirname = apr_psprintf(p, "%s/%s", dirname, tn);

        if (apr_file_mktemp(&ftemp, dirname, 
                            APR_CREATE | APR_READ | APR_WRITE | APR_DELONCLOSE, 
                            p) != APR_SUCCESS) {
            cfg_error(cfg, "unable to create a temporary file %s", dirname);
            apr_pool_destroy(p);
            return 1;
        }

        dir = opendir(path);

        if(dir != NULL){
            while((entry = readdir(dir)) != NULL) {
                ret = fnmatch(pattern, entry->d_name, 
                              FNM_PATHNAME|FNM_PERIOD);
                if (ret == 0) {
                    char *newpath, *line;

                    newpath = apr_psprintf (p, "%s/%s", path, entry->d_name);
                    line = apr_pstrcat(p, "include ('", newpath, "')\n", NULL);
                    apr_file_puts(line, ftemp);
                }
            }
            closedir(dir);
            free (path);

            argv[0] = dirname;
            if (cfg_include(cfg, opt, argc, argv))
                cfg_error(cfg, "failed to process include file %s", fname);
            else
                debug_msg("processed include file %s\n", fname);
        }

        apr_file_close(ftemp);
        apr_pool_destroy(p);

        argv[0] = fname;
    }
    else 
    {
        cfg_error(cfg, "invalid include path");
        return 1;
    }

    return 0;
}
Esempio n. 24
0
static void log_error_core(const char *file, int line, int level,
                           apr_status_t status, const server_rec *s,
                           const conn_rec *c,
                           const request_rec *r, apr_pool_t *pool,
                           const char *fmt, va_list args)
{
    char errstr[MAX_STRING_LEN];
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
    char scratch[MAX_STRING_LEN];
#endif
    apr_size_t len, errstrlen;
    apr_file_t *logf = NULL;
    const char *referer;
    int level_and_mask = level & APLOG_LEVELMASK;

    if (r && r->connection) {
        c = r->connection;
    }

    if (s == NULL) {
        /*
         * If we are doing stderr logging (startup), don't log messages that are
         * above the default server log level unless it is a startup/shutdown
         * notice
         */
        if ((level_and_mask != APLOG_NOTICE)
            && (level_and_mask > ap_default_loglevel)) {
            return;
        }

        logf = stderr_log;
    }
    else if (s->error_log) {
        /*
         * If we are doing normal logging, don't log messages that are
         * above the server log level unless it is a startup/shutdown notice
         */
        if ((level_and_mask != APLOG_NOTICE)
            && (level_and_mask > s->loglevel)) {
            return;
        }

        logf = s->error_log;
    }
#ifdef TPF
    else if (tpf_child) {
        /*
         * If we are doing normal logging, don't log messages that are
         * above the server log level unless it is a startup/shutdown notice
         */
        if ((level_and_mask != APLOG_NOTICE)
            && (level_and_mask > s->loglevel)) {
            return;
        }

        logf = stderr;
    }
#endif /* TPF */
    else {
        /*
         * If we are doing syslog logging, don't log messages that are
         * above the server log level (including a startup/shutdown notice)
         */
        if (level_and_mask > s->loglevel) {
            return;
        }
    }

    if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
        errstr[0] = '[';
        ap_recent_ctime(errstr + 1, apr_time_now());
        errstr[1 + APR_CTIME_LEN - 1] = ']';
        errstr[1 + APR_CTIME_LEN    ] = ' ';
        len = 1 + APR_CTIME_LEN + 1;
    } else {
        len = 0;
    }

    if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                            "[%s] ", priorities[level_and_mask].t_name);
    }

#ifndef TPF
    if (file && level_and_mask == APLOG_DEBUG) {
#if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
        char tmp[256];
        char *e = strrchr(file, '/');
#ifdef WIN32
        if (!e) {
            e = strrchr(file, '\\');
        }
#endif

        /* In OSD/POSIX, the compiler returns for __FILE__
         * a string like: __FILE__="*POSIX(/usr/include/stdio.h)"
         * (it even returns an absolute path for sources in
         * the current directory). Here we try to strip this
         * down to the basename.
         */
        if (e != NULL && e[1] != '\0') {
            apr_snprintf(tmp, sizeof(tmp), "%s", &e[1]);
            e = &tmp[strlen(tmp)-1];
            if (*e == ')') {
                *e = '\0';
            }
            file = tmp;
        }
#else /* _OSD_POSIX || WIN32 */
        const char *p;
        /* On Unix, __FILE__ may be an absolute path in a
         * VPATH build. */
        if (file[0] == '/' && (p = ap_strrchr_c(file, '/')) != NULL) {
            file = p + 1;
        }
#endif /*_OSD_POSIX || WIN32 */
        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                            "%s(%d): ", file, line);
    }
#endif /* TPF */

    if (c) {
        /* XXX: TODO: add a method of selecting whether logged client
         * addresses are in dotted quad or resolved form... dotted
         * quad is the most secure, which is why I'm implementing it
         * first. -djg
         */
        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                            "[client %s] ", c->remote_ip);
    }
    if (status != 0) {
        if (status < APR_OS_START_EAIERR) {
            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                                "(%d)", status);
        }
        else if (status < APR_OS_START_SYSERR) {
            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                                "(EAI %d)", status - APR_OS_START_EAIERR);
        }
        else if (status < 100000 + APR_OS_START_SYSERR) {
            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                                "(OS %d)", status - APR_OS_START_SYSERR);
        }
        else {
            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                                "(os 0x%08x)", status - APR_OS_START_SYSERR);
        }
        apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
        len += strlen(errstr + len);
        if (MAX_STRING_LEN - len > 2) {
            errstr[len++] = ':';
            errstr[len++] = ' ';
            errstr[len] = '\0';
        }
    }

    errstrlen = len;
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
    if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
        len += ap_escape_errorlog_item(errstr + len, scratch,
                                       MAX_STRING_LEN - len);
    }
#else
    len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
#endif

    if (   r && (referer = apr_table_get(r->headers_in, "Referer"))
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
        && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
#endif
        ) {
        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
                            ", referer: %s",
#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
                            scratch
#else
                            referer
#endif
                            );
    }

    /* NULL if we are logging to syslog */
    if (logf) {
        /* Truncate for the terminator (as apr_snprintf does) */
        if (len > MAX_STRING_LEN - sizeof(APR_EOL_STR)) {
            len = MAX_STRING_LEN - sizeof(APR_EOL_STR);
        }
        strcpy(errstr + len, APR_EOL_STR);
        apr_file_puts(errstr, logf);
        apr_file_flush(logf);
    }
#ifdef HAVE_SYSLOG
    else {
        syslog(level_and_mask, "%s", errstr);
    }
#endif

    ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
}