summaryrefslogtreecommitdiffstats
path: root/mod_log_sql.c
diff options
context:
space:
mode:
authorGravatar Christopher Powell 2001-12-03 19:54:02 +0000
committerGravatar Christopher Powell 2001-12-03 19:54:02 +0000
commit6eff371018e78925510ff71cb255a64a441a9fc8 (patch)
tree3dc78ec12b8cc45ef79528d4ed271eef8129b410 /mod_log_sql.c
parent69fa0ad0d21465573b9dcb07c87ae27f993dfe31 (diff)
Big changes including reworked cookie code, new directives, SSL logging,
new formatting characters, bugfixes, etc.
Diffstat (limited to 'mod_log_sql.c')
-rw-r--r--mod_log_sql.c258
1 files changed, 187 insertions, 71 deletions
diff --git a/mod_log_sql.c b/mod_log_sql.c
index 0739372..5bb992b 100644
--- a/mod_log_sql.c
+++ b/mod_log_sql.c
@@ -1,4 +1,4 @@
1/* $Id: mod_log_sql.c,v 1.2 2001/11/30 08:29:04 helios Stab $ 1/* $Id: mod_log_sql.c,v 1.3 2001/12/03 19:54:02 helios Exp $
2 * 2 *
3 * mod_log_mysql.c 3 * mod_log_mysql.c
4 * Release v 1.10 4 * Release v 1.10
@@ -71,22 +71,41 @@
71 */ 71 */
72 72
73 73
74/* DEFINES */
75#define MYSQL_ERROR(mysql) ((mysql)?(mysql_error(mysql)):"MySQL server has gone away")
76
77#define ERRLEVEL APLOG_ERR|APLOG_NOERRNO
78
79#undef DEBUG
80#ifdef DEBUG
81 #define DEBUGLEVEL APLOG_INFO|APLOG_NOERRNO
82#endif
83
84
85
86/* INCLUDES */
74#include <time.h> 87#include <time.h>
75#include <mysql/mysql.h> 88#include <mysql/mysql.h>
89#include <stdio.h>
76 90
77#include "httpd.h" 91#include "httpd.h"
78#include "http_config.h" 92#include "http_config.h"
79#include "http_log.h" 93#include "http_log.h"
80#include "http_core.h" 94#include "http_core.h"
81#if MODULE_MAGIC_NUMBER >= 19980324 95#if MODULE_MAGIC_NUMBER >= 19980324
82 #include "ap_compat.h" 96 #include "ap_compat.h"
83#endif 97#endif
84 98
99#ifdef WANT_SSL_LOGGING
100 #include "/usr/local/src/apache_1.3.22/src/modules/ssl/mod_ssl.h"
101#endif
102
103
104
105/* DECLARATIONS */
85module mysql_log_module; 106module mysql_log_module;
86MYSQL log_sql_server, *mysql_log = NULL; 107MYSQL log_sql_server, *mysql_log = NULL;
87char *log_db_name = NULL, *db_host = NULL, *db_user = NULL, *db_pwd = NULL; 108char *log_db_name = NULL, *db_host = NULL, *db_user = NULL, *db_pwd = NULL, *cookie_name = NULL;
88
89#define MYSQL_ERROR(mysql) ((mysql)?(mysql_error(mysql)):"MySQL server has gone away")
90 109
91typedef const char *(*item_key_func) (request_rec *, char *); 110typedef const char *(*item_key_func) (request_rec *, char *);
92typedef struct { 111typedef struct {
@@ -97,8 +116,11 @@ typedef struct {
97 char *transfer_log_format; 116 char *transfer_log_format;
98} log_mysql_state; 117} log_mysql_state;
99 118
100/* Defined in /usr/local/Apache/include/ap_mmn.h, 19990320 as of this writing. */ 119
101#if MODULE_MAGIC_NUMBER < 19970103 120
121#if MODULE_MAGIC_NUMBER < 19970103 /* Defined in /usr/local/Apache/include/ap_mmn.h, 19990320 as of this writing. */
122extern const char *log_request_protocol(request_rec *r, char *a);
123extern const char *log_request_method(request_rec *r, char *a);
102extern const char *log_remote_host(request_rec *r, char *a); 124extern const char *log_remote_host(request_rec *r, char *a);
103extern const char *log_remote_logname(request_rec *r, char *a); 125extern const char *log_remote_logname(request_rec *r, char *a);
104extern const char *log_remote_user(request_rec *r, char *a); 126extern const char *log_remote_user(request_rec *r, char *a);
@@ -118,6 +140,7 @@ extern const char *log_virtual_host(request_rec *r, char *a);
118extern const char *log_server_port(request_rec *r, char *a); 140extern const char *log_server_port(request_rec *r, char *a);
119extern const char *log_child_pid(request_rec *r, char *a); 141extern const char *log_child_pid(request_rec *r, char *a);
120#else 142#else
143
121static char *format_integer(pool *p, int i) 144static char *format_integer(pool *p, int i)
122{ 145{
123 char dummy[40]; 146 char dummy[40];
@@ -161,6 +184,69 @@ static const char *log_remote_user(request_rec *r, char *a)
161 return rvalue; 184 return rvalue;
162} 185}
163 186
187#ifdef WANT_SSL_LOGGING
188static const char *log_ssl_keysize(request_rec *r, char *a)
189{
190 char *result = NULL;
191
192 if (ap_ctx_get(r->connection->client->ctx, "ssl") != NULL) {
193 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_USEKEYSIZE");
194 #ifdef DEBUG
195 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_mysql: SSL_KEYSIZE: %s", result);
196 #endif
197 if (result != NULL && result[0] == '\0')
198 result = NULL;
199 return result;
200 } else {
201 return "0";
202 }
203}
204
205static const char *log_ssl_maxkeysize(request_rec *r, char *a)
206{
207 char *result = NULL;
208
209 if (ap_ctx_get(r->connection->client->ctx, "ssl") != NULL) {
210 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_ALGKEYSIZE");
211 #ifdef DEBUG
212 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_mysql: SSL_ALGKEYSIZE: %s", result);
213 #endif
214 if (result != NULL && result[0] == '\0')
215 result = NULL;
216 return result;
217 } else {
218 return "0";
219 }
220}
221
222static const char *log_ssl_cipher(request_rec *r, char *a)
223{
224 char *result = NULL;
225
226 if (ap_ctx_get(r->connection->client->ctx, "ssl") != NULL) {
227 result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER");
228 #ifdef DEBUG
229 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_mysql: SSL_CIPHER: %s", result);
230 #endif
231 if (result != NULL && result[0] == '\0')
232 result = NULL;
233 return result;
234 } else {
235 return "0";
236 }
237}
238#endif /* WANT_SSL_LOGGING */
239
240static const char *log_request_method(request_rec *r, char *a)
241{
242 return r->method;
243}
244
245static const char *log_request_protocol(request_rec *r, char *a)
246{
247 return r->protocol;
248}
249
164static const char *log_request_line(request_rec *r, char *a) 250static const char *log_request_line(request_rec *r, char *a)
165{ 251{
166 return r->the_request; 252 return r->the_request;
@@ -292,49 +378,63 @@ static const char *log_cookie(request_rec *r, char *a)
292{ 378{
293 const char *cookiestr; 379 const char *cookiestr;
294 char *cookieend; 380 char *cookieend;
381 char *isvalid;
382 char *cookiebuf;
295 383
296 cookiestr = table_get(r->headers_in, "cookie"); 384 cookiestr = (char *)table_get(r->headers_in, "cookie2");
297 385 if (cookiestr != NULL) {
298 /* First look for Cookie2: header */ 386 #ifdef DEBUG
299 if ( (cookiestr = table_get(r->headers_in, "cookie2")) ) { 387 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_mysql: Cookie2: [%s]", cookiestr);
300 cookieend = strchr(cookiestr, ';'); 388 #endif
301 if (cookieend) 389 isvalid = strstr(cookiestr, cookie_name);
302 *cookieend = '\0'; /* Ignore anything after a ; */ 390 if (isvalid != NULL) {
303 return cookiestr; 391 isvalid += strlen(cookie_name) + 1;
392 cookiebuf = ap_pstrdup(r->pool, isvalid);
393 cookieend = strchr(cookiebuf, ';');
394 if (cookieend != NULL)
395 *cookieend = '\0';
396 return cookiebuf;
397 }
304 } 398 }
305 399
306 /* Then try a Cookie: header */ 400
307 else if ( (cookiestr = table_get(r->headers_in, "cookie")) ) { 401 cookiestr = (char *)table_get(r->headers_in, "cookie");
308 cookieend = strchr(cookiestr, ';'); 402 if (cookiestr != NULL) {
309 if (cookieend) 403 #ifdef DEBUG
310 *cookieend = '\0'; 404 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_mysql: Cookie: [%s]", cookiestr);
311 return cookiestr; 405 #endif
406 isvalid = strstr(cookiestr, cookie_name);
407 if (isvalid != NULL) {
408 isvalid += strlen(cookie_name) + 1;
409 cookiebuf = ap_pstrdup(r->pool, isvalid);
410 cookieend = strchr(cookiebuf, ';');
411 if (cookieend != NULL)
412 *cookieend = '\0';
413 return cookiebuf;
414 }
312 } 415 }
313 416
314 /* Still none? Use the Set-Cookie: header. I feel a little
315 * guilty about this, because some clients refuse cookies. The
316 * log will in their cases log a ton of different Set-Cookie requests
317 * that aren't being honored. However, it's necessary to insert this
318 * check so that the first request of a series doesn't log a - ...
319 */
320 else if ( (cookiestr = table_get(r->headers_out, "set-cookie")) ) {
321 cookieend = strchr(cookiestr, ';');
322 if (cookieend)
323 *cookieend = '\0';
324 return cookiestr;
325 }
326 417
327 /* Okay, fine, no eligible headers. Return a - instead. 418 cookiestr = table_get(r->headers_out, "set-cookie");
328 * I /could/ insert a look for the Set-Cookie2: header here, but I think 419 if (cookiestr != NULL) {
329 * it would be imprudent. It's apparent that the current browsers don't 420 #ifdef DEBUG
330 * support Cookie2 cookies, so why bother logging a bunch of Set-Cookie2: 421 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_mysql: Set-Cookie: [%s]", cookiestr);
331 * requests that aren't even going to be honored? 422 #endif
332 */ 423 isvalid = strstr(cookiestr, cookie_name);
333 else { 424 if (isvalid != NULL) {
334 return "-"; 425 isvalid += strlen(cookie_name) + 1;
335 } 426 cookiebuf = ap_pstrdup(r->pool, isvalid);
427 cookieend = strchr(cookiebuf, ';');
428 if (cookieend != NULL)
429 *cookieend = '\0';
430 return cookiebuf;
431 }
432 }
433
434 return "-";
336} 435}
337 436
437
338const char *log_request_timestamp(request_rec *r, char *a) 438const char *log_request_timestamp(request_rec *r, char *a)
339{ 439{
340 char tstr[32]; 440 char tstr[32];
@@ -352,7 +452,7 @@ static const char *log_env_var(request_rec *r, char *a)
352{ 452{
353 return table_get(r->subprocess_env, a); 453 return table_get(r->subprocess_env, a);
354} 454}
355#endif 455#endif /* MODULE_MAGIC_NUMBER */
356 456
357 457
358/* End declarations of various log_ functions */ 458/* End declarations of various log_ functions */
@@ -371,10 +471,12 @@ struct log_mysql_item_list {
371 { 'c', log_cookie, "cookie", 0, 1 }, 471 { 'c', log_cookie, "cookie", 0, 1 },
372 { 'e', log_env_var, "env_var", 0, 1 }, 472 { 'e', log_env_var, "env_var", 0, 1 },
373 { 'f', log_request_file, "request_file", 0, 1 }, 473 { 'f', log_request_file, "request_file", 0, 1 },
374 { 'h', log_remote_host, "remote_host", 0, 1 }, 474 { 'H', log_request_protocol, "request_protocol", 0, 1 },
475 { 'h', log_remote_host, "remote_host", 0, 1 },
375 { 'i', log_header_in, "header_in", 0, 1 }, 476 { 'i', log_header_in, "header_in", 0, 1 },
376 { 'l', log_remote_logname, "remote_logname", 0, 1 }, 477 { 'l', log_remote_logname, "remote_logname", 0, 1 },
377 { 'n', log_note, "note", 0, 1 }, 478 { 'm', log_request_method, "request_method", 0, 1 },
479 { 'n', log_note, "note", 0, 1 },
378 { 'o', log_header_out, "header_out", 0, 1 }, 480 { 'o', log_header_out, "header_out", 0, 1 },
379 { 'P', log_child_pid, "child_pid", 0, 0 }, 481 { 'P', log_child_pid, "child_pid", 0, 0 },
380 { 'p', log_server_port, "server_port", 0, 0 }, 482 { 'p', log_server_port, "server_port", 0, 0 },
@@ -387,6 +489,11 @@ struct log_mysql_item_list {
387 { 'u', log_remote_user, "remote_user", 0, 1 }, 489 { 'u', log_remote_user, "remote_user", 0, 1 },
388 { 'U', log_request_uri, "request_uri", 1, 1 }, 490 { 'U', log_request_uri, "request_uri", 1, 1 },
389 { 'v', log_virtual_host, "virtual_host", 0, 1 }, 491 { 'v', log_virtual_host, "virtual_host", 0, 1 },
492 #ifdef WANT_SSL_LOGGING
493 { 'q', log_ssl_keysize, "ssl_keysize", 0, 1 },
494 { 'Q', log_ssl_maxkeysize, "ssl_maxkeysize", 0, 1 },
495 { 'z', log_ssl_cipher, "ssl_cipher", 0, 1 },
496 #endif
390 {'\0'} 497 {'\0'}
391}; 498};
392 499
@@ -521,6 +628,12 @@ const char *set_log_mysql_db(cmd_parms *parms, void *dummy, char *arg)
521 return NULL; 628 return NULL;
522} 629}
523 630
631const char *set_log_mysql_cookie(cmd_parms *parms, void *dummy, char *arg)
632{
633 cookie_name = arg;
634 return NULL;
635}
636
524const char *set_log_mysql_info(cmd_parms *parms, void *dummy, char *host, char *user, char *pwd) 637const char *set_log_mysql_info(cmd_parms *parms, void *dummy, char *host, char *user, char *pwd)
525{ 638{
526 if (*host != '.') { 639 if (*host != '.') {
@@ -570,32 +683,35 @@ const char *add_remhost_mysql_ignore(cmd_parms *parms, void *dummy, char *arg)
570} 683}
571 684
572command_rec log_mysql_cmds[] = { 685command_rec log_mysql_cmds[] = {
573 {"RefererLogMySQLTable", set_referer_log_mysql_table, NULL, RSRC_CONF, TAKE1, 686 {"MySQLRefererLogTable", set_referer_log_mysql_table, NULL, RSRC_CONF, TAKE1,
574 "the table of the referer log"} 687 "The MySQL table that holds the referer log"}
688 ,
689 {"MySQLAgentLogTable", set_agent_log_mysql_table, NULL, RSRC_CONF, TAKE1,
690 "The MySQL table that holds the agent log"}
575 , 691 ,
576 {"AgentLogMySQLTable", set_agent_log_mysql_table, NULL, RSRC_CONF, TAKE1, 692 {"MySQLTransferLogTable", set_transfer_log_mysql_table, NULL, RSRC_CONF, TAKE1,
577 "the table of the agent log"} 693 "The MySQL table that holds the transfer log"}
578 , 694 ,
579 {"TransferLogMySQLTable", set_transfer_log_mysql_table, NULL, RSRC_CONF, TAKE1, 695 {"MySQLTransferLogFormat", set_transfer_log_format, NULL, RSRC_CONF, TAKE1,
580 "the table of the transfer log"} 696 "Instruct the module what information to log to the MySQL transfer log"}
581 , 697 ,
582 {"TransferLogMySQLFormat", set_transfer_log_format, NULL, RSRC_CONF, TAKE1, 698 {"MySQLRefererIgnore", add_referer_mysql_ignore, NULL, RSRC_CONF, ITERATE,
583 "specific format for the MySQL transfer log"} 699 "List of referers to ignore, accesses that match will not be logged to MySQL"}
584 , 700 ,
585 {"RefererIgnore", add_referer_mysql_ignore, NULL, RSRC_CONF, ITERATE, 701 {"MySQLRequestIgnore", add_transfer_mysql_ignore, NULL, RSRC_CONF, ITERATE,
586 "referer hostnames to ignore"} 702 "List of URIs to ignore, accesses that match will not be logged to MySQL"}
587 , 703 ,
588 {"RequestIgnore", add_transfer_mysql_ignore, NULL, RSRC_CONF, ITERATE, 704 {"MySQLRemhostIgnore", add_remhost_mysql_ignore, NULL, RSRC_CONF, ITERATE,
589 "transfer log URIs to ignore"} 705 "List of remote hosts to ignore, accesses that match will not be logged to MySQL"}
590 , 706 ,
591 {"RemhostIgnore", add_remhost_mysql_ignore, NULL, RSRC_CONF, ITERATE, 707 {"MySQLDatabase", set_log_mysql_db, NULL, RSRC_CONF, TAKE1,
592 "transfer log remote hosts to ignore"} 708 "The name of the MySQL database for logging"}
593 , 709 ,
594 {"LogMySQLDB", set_log_mysql_db, NULL, RSRC_CONF, TAKE1, 710 {"MySQLWhichCookie", set_log_mysql_cookie, NULL, RSRC_CONF, TAKE1,
595 "the database of the referer log"} 711 "The CookieName that you want logged when using the 'c' config directive"}
596 , 712 ,
597 {"LogMySQLInfo", set_log_mysql_info, NULL, RSRC_CONF, TAKE3, 713 {"MySQLLoginInfo", set_log_mysql_info, NULL, RSRC_CONF, TAKE3,
598 "host, user and password for MySQL link"} 714 "The MySQL host, user-id and password for logging"}
599 , 715 ,
600 {NULL} 716 {NULL}
601}; 717};
@@ -622,17 +738,17 @@ int safe_mysql_query(request_rec *r, const char *query)
622 738
623 { /* We need to restart the server link */ 739 { /* We need to restart the server link */
624 mysql_log = NULL; 740 mysql_log = NULL;
625 log_error("MySQL: connection lost, attempting reconnect", r->server); 741 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"MySQL: connection lost, attempting reconnect");
626 742
627 open_log_dblink(); 743 open_log_dblink();
628 744
629 if (mysql_log == NULL) { /* still unable to link */ 745 if (mysql_log == NULL) { /* still unable to link */
630 signal(SIGPIPE, handler); 746 signal(SIGPIPE, handler);
631 log_error("MySQL: reconnect failed.", r->server); 747 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"MySQL: reconnect failed.");
632 return error; 748 return error;
633 } 749 }
634 750
635 log_error("MySQL: reconnect successful.", r->server); 751 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"MySQL: reconnect successful.");
636 error = mysql_query(mysql_log, query); 752 error = mysql_query(mysql_log, query);
637 } 753 }
638 754
@@ -652,11 +768,11 @@ int safe_mysql_query(request_rec *r, const char *query)
652 768
653 if (error) { 769 if (error) {
654 str = pstrcat(r->pool, "MySQL query failed: ", query, NULL); 770 str = pstrcat(r->pool, "MySQL query failed: ", query, NULL);
655 log_error(str, r->server); 771 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,str);
656 str = pstrcat(r->pool, "MySQL failure reason: ", MYSQL_ERROR(mysql_log), NULL); 772 str = pstrcat(r->pool, "MySQL failure reason: ", MYSQL_ERROR(mysql_log), NULL);
657 log_error(str, r->server); 773 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,str);
658 } else { 774 } else {
659 log_error("MySQL: INSERT successful after a delayed retry.", r->server); 775 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"MySQL: insert successful after a delayed retry.");
660 } 776 }
661 } 777 }
662 return error; 778 return error;
@@ -775,7 +891,7 @@ int log_mysql_transaction(request_rec *orig)
775 891
776 /* If not specified by the user, use the default format */ 892 /* If not specified by the user, use the default format */
777 if (cls->transfer_log_format[0] == '\0') { 893 if (cls->transfer_log_format[0] == '\0') {
778 cls->transfer_log_format = "huSUsbTvRA"; 894 cls->transfer_log_format = "AbHhmRSsTUuv";
779 } 895 }
780 length = strlen(cls->transfer_log_format); 896 length = strlen(cls->transfer_log_format);
781 897