Main Page | Modules | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages | Examples

mod_log_forensic.c

Go to the documentation of this file.
00001 /* Copyright 2004 The Apache Software Foundation
00002  *
00003  * Licensed under the Apache License, Version 2.0 (the "License");
00004  * you may not use this file except in compliance with the License.
00005  * You may obtain a copy of the License at
00006  *
00007  *     http://www.apache.org/licenses/LICENSE-2.0
00008  *
00009  * Unless required by applicable law or agreed to in writing, software
00010  * distributed under the License is distributed on an "AS IS" BASIS,
00011  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012  * See the License for the specific language governing permissions and
00013  * limitations under the License.
00014  */
00015 
00016 /*
00017  * See also support/check_forensic.
00018  * Relate the forensic log to the transfer log by including
00019  * %{forensic-id}n in the custom log format, for example:
00020  * CustomLog logs/custom "%h %l %u %t \"%r\" %>s %b %{forensic-id}n"
00021  *
00022  * Credit is due to Tina Bird <tbird precision-guesswork.com>, whose
00023  * idea this module was.
00024  *
00025  *   Ben Laurie 29/12/2003
00026  */
00027 
00028 #include "httpd.h"
00029 #include "http_config.h"
00030 #include "http_log.h"
00031 #include "apr_strings.h"
00032 #include "apr_atomic.h"
00033 #include "http_protocol.h"
00034 #include "test_char.h"
00035 #if APR_HAVE_UNISTD_H
00036 #include <unistd.h>
00037 #endif
00038 
00039 module AP_MODULE_DECLARE_DATA log_forensic_module;
00040 
00041 typedef struct fcfg {
00042     const char *logname;
00043     apr_file_t *fd;
00044 } fcfg;
00045 
00046 static void *make_forensic_log_scfg(apr_pool_t *p, server_rec *s)
00047 {
00048     fcfg *cfg = apr_pcalloc(p, sizeof *cfg);
00049 
00050     cfg->logname = NULL;
00051     cfg->fd = NULL;
00052 
00053     return cfg;
00054 }
00055 
00056 static void *merge_forensic_log_scfg(apr_pool_t *p, void *parent, void *new)
00057 {
00058     fcfg *cfg = apr_pcalloc(p, sizeof *cfg);
00059     fcfg *pc = parent;
00060     fcfg *nc = new;
00061 
00062     cfg->logname = apr_pstrdup(p, nc->logname ? nc->logname : pc->logname);
00063     cfg->fd = NULL;
00064 
00065     return cfg;
00066 }
00067 
00068 static int open_log(server_rec *s, apr_pool_t *p)
00069 {
00070     fcfg *cfg = ap_get_module_config(s->module_config, &log_forensic_module);
00071 
00072     if (!cfg->logname || cfg->fd)
00073         return 1;
00074 
00075     if (*cfg->logname == '|') {
00076         piped_log *pl;
00077         const char *pname = ap_server_root_relative(p, cfg->logname + 1);
00078 
00079         pl = ap_open_piped_log(p, pname);
00080         if (pl == NULL) {
00081             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
00082                          "couldn't spawn forensic log pipe %s", cfg->logname);
00083             return 0;
00084         }
00085         cfg->fd = ap_piped_log_write_fd(pl);
00086     }
00087     else {
00088         const char *fname = ap_server_root_relative(p, cfg->logname);
00089         apr_status_t rv;
00090 
00091         if ((rv = apr_file_open(&cfg->fd, fname,
00092                                 APR_WRITE | APR_APPEND | APR_CREATE,
00093                                 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
00094             ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
00095                          "could not open forensic log file %s.", fname);
00096             return 0;
00097         }
00098     }
00099 
00100     return 1;
00101 }
00102 
00103 static int log_init(apr_pool_t *pc, apr_pool_t *p, apr_pool_t *pt,
00104                      server_rec *s)
00105 {
00106     for ( ; s ; s = s->next) {
00107         if (!open_log(s, p)) {
00108             return HTTP_INTERNAL_SERVER_ERROR;
00109         }
00110     }
00111 
00112     return OK;
00113 }
00114 
00115   
00116 /* e is the first _invalid_ location in q
00117    N.B. returns the terminating NUL.
00118  */
00119 static char *log_escape(char *q, const char *e, const char *p)
00120 {
00121     for ( ; *p ; ++p) {
00122         ap_assert(q < e);
00123         if (test_char_table[*(unsigned char *)p]&T_ESCAPE_FORENSIC) {
00124             ap_assert(q+2 < e);
00125             *q++ = '%';
00126             sprintf(q, "%02x", *(unsigned char *)p);
00127             q += 2;
00128         }
00129         else
00130             *q++ = *p;
00131     }
00132     ap_assert(q < e);
00133     *q = '\0';
00134 
00135     return q;
00136 }
00137 
00138 typedef struct hlog {
00139     char *log;
00140     char *pos;
00141     char *end;
00142     apr_pool_t *p;
00143     apr_size_t count;
00144 } hlog;
00145 
00146 static int count_string(const char *p)
00147 {
00148     int n;
00149 
00150     for (n = 0 ; *p ; ++p, ++n)
00151         if (test_char_table[*(unsigned char *)p]&T_ESCAPE_FORENSIC)
00152             n += 2;
00153     return n;
00154 }
00155 
00156 static int count_headers(void *h_, const char *key, const char *value)
00157 {
00158     hlog *h = h_;
00159 
00160     h->count += count_string(key)+count_string(value)+2;
00161 
00162     return 1;
00163 }
00164 
00165 static int log_headers(void *h_, const char *key, const char *value)
00166 {
00167     hlog *h = h_;
00168 
00169     /* note that we don't have to check h->pos here, coz its been done
00170        for us by log_escape */
00171     *h->pos++ = '|';
00172     h->pos = log_escape(h->pos, h->end, key);
00173     *h->pos++ = ':';
00174     h->pos = log_escape(h->pos, h->end, value);
00175 
00176     return 1;
00177 }
00178 
00179 static int log_before(request_rec *r)
00180 {
00181     fcfg *cfg = ap_get_module_config(r->server->module_config,
00182                                      &log_forensic_module);
00183     const char *id;
00184     hlog h;
00185     apr_size_t n;
00186     apr_status_t rv;
00187 
00188     if (!cfg->fd || r->prev) {
00189         return DECLINED;
00190     }
00191 
00192     if (!(id = apr_table_get(r->subprocess_env, "UNIQUE_ID"))) {
00193         /* we make the assumption that we can't go through all the PIDs in
00194            under 1 second */
00195         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
00196                     "mod_log_forensic: mod_unique_id must also be active");
00197         return DECLINED;
00198     }
00199     ap_set_module_config(r->request_config, &log_forensic_module, (char *)id);
00200 
00201     h.p = r->pool;
00202     h.count = 0;
00203 
00204     apr_table_do(count_headers, &h, r->headers_in, NULL);
00205 
00206     h.count += 1+strlen(id)+1+count_string(r->the_request)+1+1;
00207     h.log = apr_palloc(r->pool, h.count);
00208     h.pos = h.log;
00209     h.end = h.log+h.count;
00210 
00211     *h.pos++ = '+';
00212     strcpy(h.pos, id);
00213     h.pos += strlen(h.pos);
00214     *h.pos++ = '|';
00215     h.pos = log_escape(h.pos, h.end, r->the_request);
00216 
00217     apr_table_do(log_headers, &h, r->headers_in, NULL);
00218 
00219     ap_assert(h.pos < h.end);
00220     *h.pos++ = '\n';
00221 
00222     n = h.count-1;
00223     rv = apr_file_write(cfg->fd, h.log, &n);
00224     ap_assert(rv == APR_SUCCESS && n == h.count-1);
00225 
00226     apr_table_setn(r->notes, "forensic-id", id);
00227 
00228     return OK;
00229 }
00230 
00231 static int log_after(request_rec *r)
00232 {
00233     fcfg *cfg = ap_get_module_config(r->server->module_config,
00234                                      &log_forensic_module);
00235     const char *id = ap_get_module_config(r->request_config,
00236                                           &log_forensic_module);
00237     char *s;
00238     apr_size_t l, n;
00239     apr_status_t rv;
00240 
00241     if (!cfg->fd) {
00242         return DECLINED;
00243     }
00244 
00245     s = apr_pstrcat(r->pool, "-", id, "\n", NULL);
00246     l = n = strlen(s);
00247     rv = apr_file_write(cfg->fd, s, &n);
00248     ap_assert(rv == APR_SUCCESS && n == l);
00249 
00250     return OK;
00251 }
00252 
00253 static const char *set_forensic_log(cmd_parms *cmd, void *dummy, const char *fn)
00254 {
00255     fcfg *cfg = ap_get_module_config(cmd->server->module_config,
00256                                      &log_forensic_module);
00257 
00258     cfg->logname = fn;
00259     return NULL;
00260 }
00261 
00262 static const command_rec forensic_log_cmds[] =
00263 {
00264     AP_INIT_TAKE1("ForensicLog",  set_forensic_log,  NULL,  RSRC_CONF,
00265                   "the filename of the forensic log"),
00266     { NULL }
00267 };
00268 
00269 static void register_hooks(apr_pool_t *p)
00270 {
00271     static const char * const pre[] = { "mod_unique_id.c", NULL };
00272 
00273     ap_hook_open_logs(log_init,NULL,NULL,APR_HOOK_MIDDLE);
00274     ap_hook_post_read_request(log_before,pre,NULL,APR_HOOK_REALLY_FIRST);
00275     ap_hook_log_transaction(log_after,NULL,NULL,APR_HOOK_REALLY_LAST);
00276 }
00277 
00278 module AP_MODULE_DECLARE_DATA log_forensic_module =
00279 {
00280     STANDARD20_MODULE_STUFF,
00281     NULL,                       /* create per-dir config */
00282     NULL,                       /* merge per-dir config */
00283     make_forensic_log_scfg,     /* server config */
00284     merge_forensic_log_scfg,    /* merge server config */
00285     forensic_log_cmds,          /* command apr_table_t */
00286     register_hooks              /* register hooks */
00287 };