Blame modules/debugging/mod_dumpio.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Originally written @ Covalent by Jim Jagielski
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_dumpio.c:
Packit 90a5c9
 *  Think of this as a filter sniffer for Apache 2.x. It logs
Packit 90a5c9
 *  all filter data right before and after it goes out on the
Packit 90a5c9
 *  wire (BUT right before SSL encoded or after SSL decoded).
Packit 90a5c9
 *  It can produce a *huge* amount of data.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_connection.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA dumpio_module ;
Packit 90a5c9
Packit 90a5c9
typedef struct dumpio_conf_t {
Packit 90a5c9
    int enable_input;
Packit 90a5c9
    int enable_output;
Packit 90a5c9
} dumpio_conf_t;
Packit 90a5c9
Packit 90a5c9
/* consider up to 80 additional characters, and factor the longest
Packit 90a5c9
 * line length of all \xNN sequences; log_error cannot record more
Packit 90a5c9
 * than MAX_STRING_LEN characters.
Packit 90a5c9
 */
Packit 90a5c9
#define dumpio_MAX_STRING_LEN (MAX_STRING_LEN / 4 - 80)
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Workhorse function: simply log to the current error_log
Packit 90a5c9
 * info about the data in the bucket as well as the data itself
Packit 90a5c9
 */
Packit 90a5c9
static void dumpit(ap_filter_t *f, apr_bucket *b, dumpio_conf_t *ptr)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = f->c;
Packit 90a5c9
Packit 90a5c9
    ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c,
Packit 90a5c9
                  "mod_dumpio:  %s (%s-%s): %" APR_SIZE_T_FMT " bytes",
Packit 90a5c9
                  f->frec->name,
Packit 90a5c9
                  (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
Packit 90a5c9
                  b->type->name,
Packit 90a5c9
                  b->length) ;
Packit 90a5c9
Packit 90a5c9
    if (!(APR_BUCKET_IS_METADATA(b)))
Packit 90a5c9
    {
Packit 90a5c9
#if APR_CHARSET_EBCDIC
Packit 90a5c9
        char xlatebuf[dumpio_MAX_STRING_LEN + 1];
Packit 90a5c9
#endif
Packit 90a5c9
        const char *buf;
Packit 90a5c9
        apr_size_t nbytes;
Packit 90a5c9
        apr_status_t rv = apr_bucket_read(b, &buf, &nbytes, APR_BLOCK_READ);
Packit 90a5c9
Packit 90a5c9
        if (rv == APR_SUCCESS)
Packit 90a5c9
        {
Packit 90a5c9
            while (nbytes)
Packit 90a5c9
            {
Packit 90a5c9
                apr_size_t logbytes = nbytes;
Packit 90a5c9
                if (logbytes > dumpio_MAX_STRING_LEN)
Packit 90a5c9
                    logbytes = dumpio_MAX_STRING_LEN;
Packit 90a5c9
                nbytes -= logbytes;
Packit 90a5c9
Packit 90a5c9
#if APR_CHARSET_EBCDIC
Packit 90a5c9
                memcpy(xlatebuf, buf, logbytes);
Packit 90a5c9
                ap_xlate_proto_from_ascii(xlatebuf, logbytes);
Packit 90a5c9
                xlatebuf[logbytes] = '\0';
Packit 90a5c9
                ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c,
Packit 90a5c9
                              "mod_dumpio:  %s (%s-%s): %s", f->frec->name,
Packit 90a5c9
                              (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
Packit 90a5c9
                              b->type->name, xlatebuf);
Packit 90a5c9
#else
Packit 90a5c9
                /* XXX: Seriously flawed; we do not pay attention to embedded
Packit 90a5c9
                 * \0's in the request body, these should be escaped; however,
Packit 90a5c9
                 * the logging function already performs a significant amount
Packit 90a5c9
                 * of escaping, and so any escaping would be double-escaped.
Packit 90a5c9
                 * The coding solution is to throw away the current logic
Packit 90a5c9
                 * within ap_log_error, and introduce new vformatter %-escapes
Packit 90a5c9
                 * for escaping text, and for binary text (fixed len strings).
Packit 90a5c9
                 */
Packit 90a5c9
                ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c,
Packit 90a5c9
                              "mod_dumpio:  %s (%s-%s): %.*s", f->frec->name,
Packit 90a5c9
                              (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
Packit 90a5c9
                              b->type->name, (int)logbytes, buf);
Packit 90a5c9
#endif
Packit 90a5c9
                buf += logbytes;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            ap_log_cerror(APLOG_MARK, APLOG_TRACE7, rv, c,
Packit 90a5c9
                          "mod_dumpio:  %s (%s-%s): %s", f->frec->name,
Packit 90a5c9
                          (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
Packit 90a5c9
                          b->type->name, "error reading data");
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#define whichmode( mode ) \
Packit 90a5c9
 ( (( mode ) == AP_MODE_READBYTES) ? "readbytes" : \
Packit 90a5c9
   (( mode ) == AP_MODE_GETLINE) ? "getline" : \
Packit 90a5c9
   (( mode ) == AP_MODE_EATCRLF) ? "eatcrlf" : \
Packit 90a5c9
   (( mode ) == AP_MODE_SPECULATIVE) ? "speculative" : \
Packit 90a5c9
   (( mode ) == AP_MODE_EXHAUSTIVE) ? "exhaustive" : \
Packit 90a5c9
   (( mode ) == AP_MODE_INIT) ? "init" : "unknown" \
Packit 90a5c9
 )
Packit 90a5c9
Packit 90a5c9
static int dumpio_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
Packit 90a5c9
    ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
Packit 90a5c9
{
Packit 90a5c9
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
    apr_status_t ret;
Packit 90a5c9
    conn_rec *c = f->c;
Packit 90a5c9
    dumpio_conf_t *ptr = f->ctx;
Packit 90a5c9
Packit 90a5c9
    ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c,
Packit 90a5c9
                  "mod_dumpio: %s [%s-%s] %" APR_OFF_T_FMT " readbytes",
Packit 90a5c9
                  f->frec->name,
Packit 90a5c9
                  whichmode(mode),
Packit 90a5c9
                  ((block) == APR_BLOCK_READ) ? "blocking" : "nonblocking",
Packit 90a5c9
                  readbytes);
Packit 90a5c9
Packit 90a5c9
    ret = ap_get_brigade(f->next, bb, mode, block, readbytes);
Packit 90a5c9
Packit 90a5c9
    if (ret == APR_SUCCESS) {
Packit 90a5c9
        for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
Packit 90a5c9
          dumpit(f, b, ptr);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c,
Packit 90a5c9
                      "mod_dumpio: %s - %d", f->frec->name, ret) ;
Packit 90a5c9
        return ret;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS ;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int dumpio_output_filter (ap_filter_t *f, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
    conn_rec *c = f->c;
Packit 90a5c9
    dumpio_conf_t *ptr = f->ctx;
Packit 90a5c9
Packit 90a5c9
    ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c, "mod_dumpio: %s", f->frec->name);
Packit 90a5c9
Packit 90a5c9
    for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
Packit 90a5c9
        /*
Packit 90a5c9
         * If we ever see an EOS, make sure to FLUSH.
Packit 90a5c9
         */
Packit 90a5c9
        if (APR_BUCKET_IS_EOS(b)) {
Packit 90a5c9
            apr_bucket *flush = apr_bucket_flush_create(f->c->bucket_alloc);
Packit 90a5c9
            APR_BUCKET_INSERT_BEFORE(b, flush);
Packit 90a5c9
        }
Packit 90a5c9
        dumpit(f, b, ptr);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return ap_pass_brigade(f->next, bb) ;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int dumpio_pre_conn(conn_rec *c, void *csd)
Packit 90a5c9
{
Packit 90a5c9
    dumpio_conf_t *ptr;
Packit 90a5c9
Packit 90a5c9
    if (!APLOGctrace7(c)) {
Packit 90a5c9
        /* Nothing to do below TRACE7 */
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ptr = (dumpio_conf_t *) ap_get_module_config(c->base_server->module_config,
Packit 90a5c9
                                                 &dumpio_module);
Packit 90a5c9
Packit 90a5c9
    if (ptr->enable_input)
Packit 90a5c9
        ap_add_input_filter("DUMPIO_IN", ptr, NULL, c);
Packit 90a5c9
    if (ptr->enable_output)
Packit 90a5c9
        ap_add_output_filter("DUMPIO_OUT", ptr, NULL, c);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void dumpio_register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
/*
Packit 90a5c9
 * We know that SSL is CONNECTION + 5
Packit 90a5c9
 */
Packit 90a5c9
  ap_register_output_filter("DUMPIO_OUT", dumpio_output_filter,
Packit 90a5c9
        NULL, AP_FTYPE_CONNECTION + 3) ;
Packit 90a5c9
Packit 90a5c9
  ap_register_input_filter("DUMPIO_IN", dumpio_input_filter,
Packit 90a5c9
        NULL, AP_FTYPE_CONNECTION + 3) ;
Packit 90a5c9
Packit 90a5c9
  ap_hook_pre_connection(dumpio_pre_conn, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *dumpio_create_sconfig(apr_pool_t *p, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    dumpio_conf_t *ptr = apr_pcalloc(p, sizeof *ptr);
Packit 90a5c9
    ptr->enable_input = 0;
Packit 90a5c9
    ptr->enable_output = 0;
Packit 90a5c9
    return ptr;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *dumpio_enable_input(cmd_parms *cmd, void *dummy, int arg)
Packit 90a5c9
{
Packit 90a5c9
    dumpio_conf_t *ptr = ap_get_module_config(cmd->server->module_config,
Packit 90a5c9
                                              &dumpio_module);
Packit 90a5c9
Packit 90a5c9
    ptr->enable_input = arg;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *dumpio_enable_output(cmd_parms *cmd, void *dummy, int arg)
Packit 90a5c9
{
Packit 90a5c9
    dumpio_conf_t *ptr = ap_get_module_config(cmd->server->module_config,
Packit 90a5c9
                                              &dumpio_module);
Packit 90a5c9
Packit 90a5c9
    ptr->enable_output = arg;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec dumpio_cmds[] = {
Packit 90a5c9
    AP_INIT_FLAG("DumpIOInput", dumpio_enable_input, NULL,
Packit 90a5c9
                 RSRC_CONF, "Enable I/O Dump on Input Data"),
Packit 90a5c9
    AP_INIT_FLAG("DumpIOOutput", dumpio_enable_output, NULL,
Packit 90a5c9
                 RSRC_CONF, "Enable I/O Dump on Output Data"),
Packit 90a5c9
    { NULL }
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(dumpio) = {
Packit 90a5c9
        STANDARD20_MODULE_STUFF,
Packit 90a5c9
        NULL,                   /* create per-dir    config structures */
Packit 90a5c9
        NULL,                   /* merge  per-dir    config structures */
Packit 90a5c9
        dumpio_create_sconfig,  /* create per-server config structures */
Packit 90a5c9
        NULL,                   /* merge  per-server config structures */
Packit 90a5c9
        dumpio_cmds,            /* table of config file commands       */
Packit 90a5c9
        dumpio_register_hooks   /* register hooks                      */
Packit 90a5c9
};