summaryrefslogtreecommitdiffstatsabout
path: root/mod_log_sql.c
blob: 84c6115b4b5acdae5dfe8472b697c8eb4f5e67e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
/* $Id: mod_log_sql.c,v 1.12 2002/05/14 21:47:15 helios Exp $ */

/* --------*
 * DEFINES *
 * --------*/

/* The enduser probably won't modify these */
#define MYSQL_ERROR(mysql) ((mysql)?(mysql_error(mysql)):"MySQL server has gone away")
#define ERRLEVEL APLOG_ERR|APLOG_NOERRNO
#define WARNINGLEVEL APLOG_WARNING|APLOG_NOERRNO
#define NOTICELEVEL APLOG_NOTICE|APLOG_NOERRNO
#define DEBUGLEVEL APLOG_DEBUG|APLOG_NOERRNO

/* The enduser may wish to modify these */
#define WANT_SSL_LOGGING
#undef DEBUG


/* ---------*
 * INCLUDES *
 * ---------*/
#include <time.h>
#include <stdio.h>
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_core.h"
#include "mysql.h"

#if MODULE_MAGIC_NUMBER >= 19980324 /* M_M_N is defined in /usr/local/Apache/include/ap_mmn.h, 19990320 as of this writing. */
	#include "ap_compat.h"
#endif

#ifdef WANT_SSL_LOGGING
	#include "mod_ssl.h"
#endif


/* -------------*
 * DECLARATIONS *
 * -------------*/

/* Declare ourselves so the configuration routines can find and know us. */
module mysql_log_module;

/* The contents of these are known 'Apache wide' and are not variable
 * on a per-virtual-server basis.  Every virtual server 'knows' the
 * same versions of these variables.
 */
MYSQL sql_server, *mysql_log = NULL;

int massvirtual = 0;
int create_tables = 0;
char *db_name = NULL;
char *db_host = NULL;
char *db_user = NULL;
char *db_pwd  = NULL;
char *socket_file = "/tmp/mysql.sock";

typedef const char *(*item_key_func) (request_rec *, char *);

/* But the contents of this structure will vary by virtual server.
 * This permits each virtual server to vary its configuration slightly
 * for per-server customization.
 *
 * Each child process has its own segregated copy of this structure.
 */
typedef struct {
	int table_made;
	array_header *referer_ignore_list;
	array_header *transfer_ignore_list;
	array_header *remhost_ignore_list;
	array_header *notes_list;
	char *notes_table_name;
	char *transfer_table_name;
	char *transfer_log_format;
	char *preserve_file;
	char *cookie_name;
} log_mysql_state;


/* -----------------*
 * HELPER FUNCTIONS *
 * -----------------*/
static char *format_integer(pool *p, int i)
{
	char dummy[40];
	ap_snprintf(dummy, sizeof(dummy), "%d", i);
	return pstrdup(p, dummy);
}

static char *pfmt(pool *p, int i)
{
	if (i <= 0) {
		return "-";
	} else {
		return format_integer(p, i);
	}
}

/* Begin the individual functions that, given a request r,
 * extract the needed information from it and return the
 * value to the calling entity.
 */

static const char *extract_remote_host(request_rec *r, char *a)
{
	return (char *) get_remote_host(r->connection, r->per_dir_config, REMOTE_NAME);
}

static const char *extract_remote_logname(request_rec *r, char *a)
{
	return (char *) get_remote_logname(r);
}

static const char *extract_remote_user(request_rec *r, char *a)
{
	char *rvalue = r->connection->user;

	if (rvalue == NULL) {
		rvalue = "-";
	} else if (strlen(rvalue) == 0) {
		rvalue = "\"\"";
	}
	return rvalue;
}

#ifdef WANT_SSL_LOGGING
static const char *extract_ssl_keysize(request_rec *r, char *a)
{
	char *result = NULL;

	if (ap_ctx_get(r->connection->client->ctx, "ssl") != NULL) {
	    result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_USEKEYSIZE");
		#ifdef DEBUG
    	    ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"SSL_KEYSIZE: %s", result);
		#endif
		if (result != NULL && result[0] == '\0')
	      result = NULL;
		return result;
	} else {
		return "0";
	}
}

static const char *extract_ssl_maxkeysize(request_rec *r, char *a)
{
	char *result = NULL;

	if (ap_ctx_get(r->connection->client->ctx, "ssl") != NULL) {
	    result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER_ALGKEYSIZE");
		#ifdef DEBUG
    	    ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"SSL_ALGKEYSIZE: %s", result);
		#endif
		if (result != NULL && result[0] == '\0')
	      result = NULL;
		return result;
	} else {
		return "0";
	}
}

static const char *extract_ssl_cipher(request_rec *r, char *a)
{
	char *result = NULL;

	if (ap_ctx_get(r->connection->client->ctx, "ssl") != NULL) {
	    result = ssl_var_lookup(r->pool, r->server, r->connection, r, "SSL_CIPHER");
		#ifdef DEBUG
    	    ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"SSL_CIPHER: %s", result);
		#endif
		if (result != NULL && result[0] == '\0')
	      result = NULL;
		return result;
	} else {
		return "-";
	}
}
#endif /* WANT_SSL_LOGGING */

static const char *extract_request_method(request_rec *r, char *a)
{
	return r->method;
}

static const char *extract_request_protocol(request_rec *r, char *a)
{
	return r->protocol;
}

static const char *extract_request_line(request_rec *r, char *a)
{
	return r->the_request;
}

static const char *extract_request_file(request_rec *r, char *a)
{
	return r->filename;
}

static const char *extract_request_uri(request_rec *r, char *a)
{
	return r->uri;
}

static const char *extract_status(request_rec *r, char *a)
{
	return pfmt(r->pool, r->status);
}

static const char *extract_bytes_sent(request_rec *r, char *a)
{
	if (!r->sent_bodyct) {
		return "-";
	} else {
		long int bs;
		char dummy[40];
		bgetopt(r->connection->client, BO_BYTECT, &bs);
		ap_snprintf(dummy, sizeof(dummy), "%ld", bs);
		return pstrdup(r->pool, dummy);
	}
}

static const char *extract_header_in(request_rec *r, char *a)
{
	return table_get(r->headers_in, a);
}

static const char *extract_header_out(request_rec *r, char *a)
{
	const char *cp = table_get(r->headers_out, a);
	if (!strcasecmp(a, "Content-type") && r->content_type) {
		cp = r->content_type;
	}
	if (cp) {
		return cp;
	}
	return table_get(r->err_headers_out, a);
}

static const char *extract_request_time(request_rec *r, char *a)
{
	int timz;
	struct tm *t;
	char tstr[MAX_STRING_LEN];

	t = get_gmtoff(&timz);

	if (a && *a) {     /* Custom format */
		strftime(tstr, MAX_STRING_LEN, a, t);
	} else {		   /* CLF format */
		char sign = (timz < 0 ? '-' : '+');

		if (timz < 0) {
			timz = -timz;
		}
		strftime(tstr, MAX_STRING_LEN, "[%d/%b/%Y:%H:%M:%S ", t);
		ap_snprintf(tstr + strlen(tstr), sizeof(tstr) - strlen(tstr), "%c%.2d%.2d]", sign, timz / 60, timz % 60);
	}

	return pstrdup(r->pool, tstr);
}

static const char *extract_request_duration(request_rec *r, char *a)
{
	char duration[22];			 /* Long enough for 2^64 */

	ap_snprintf(duration, sizeof(duration), "%ld", time(NULL) - r->request_time);
	return pstrdup(r->pool, duration);
}

static const char *extract_virtual_host(request_rec *r, char *a)
{
    return ap_get_server_name(r);
}

static const char *extract_server_port(request_rec *r, char *a)
{
	char portnum[22];

	ap_snprintf(portnum, sizeof(portnum), "%u", r->server->port);
	return pstrdup(r->pool, portnum);
}

static const char *extract_child_pid(request_rec *r, char *a)
{
	char pidnum[22];
	ap_snprintf(pidnum, sizeof(pidnum), "%ld", (long) getpid());
	return pstrdup(r->pool, pidnum);
}

static const char *extract_referer(request_rec *r, char *a)
{
	const char *tempref;

	tempref = table_get(r->headers_in, "Referer");
	if (!tempref)
	{
		return "-";
	} else {
		return tempref;
	}
}

static const char *extract_agent(request_rec *r, char *a)
{
    const char *tempag;

    tempag = table_get(r->headers_in, "User-Agent");
    if (!tempag)
    {
        return "-";
    } else {
        return tempag;
    }
}

static const char *extract_cookie(request_rec *r, char *a)
{
    const char *cookiestr;
    char *cookieend;
	char *isvalid;
	char *cookiebuf;

	log_mysql_state *cls = get_module_config(r->server->module_config, &mysql_log_module);

	if (cls->cookie_name != NULL) {
		#ifdef DEBUG
		  	ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"watching for cookie '%s'", cls->cookie_name);
		#endif

		/* Fetch out the cookie header */
	 	cookiestr  = (char *)table_get(r->headers_in,  "cookie2");
	    if (cookiestr != NULL) {
			#ifdef DEBUG
				ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"Cookie2: [%s]", cookiestr);
			#endif
			/* Does the cookie string contain one with our name? */
			isvalid = strstr(cookiestr, cls->cookie_name);
			if (isvalid != NULL) {
				/* Move past the cookie name and equal sign */
				isvalid += strlen(cls->cookie_name) + 1;
				/* Duplicate it into the pool */
			    cookiebuf = ap_pstrdup(r->pool, isvalid);
				/* Segregate just this cookie out of the string
				 * with a terminating nul at the first semicolon */
			    cookieend = strchr(cookiebuf, ';');
			    if (cookieend != NULL)
			       *cookieend = '\0';
			  	return cookiebuf;
			}
		}

	 	cookiestr  = (char *)table_get(r->headers_in,  "cookie");
	    if (cookiestr != NULL) {
			#ifdef DEBUG
				ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"Cookie: [%s]", cookiestr);
			#endif
			isvalid = strstr(cookiestr, cls->cookie_name);
			if (isvalid != NULL) {
				isvalid += strlen(cls->cookie_name) + 1;
			    cookiebuf = ap_pstrdup(r->pool, isvalid);
			    cookieend = strchr(cookiebuf, ';');
			    if (cookieend != NULL)
			       *cookieend = '\0';
			  	return cookiebuf;
			}
		}

	 	cookiestr = table_get(r->headers_out,  "set-cookie");
	    if (cookiestr != NULL) {
			#ifdef DEBUG
			     ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"Set-Cookie: [%s]", cookiestr);
			#endif
			isvalid = strstr(cookiestr, cls->cookie_name);
			if (isvalid != NULL) {
			    isvalid += strlen(cls->cookie_name) + 1;
			    cookiebuf = ap_pstrdup(r->pool, isvalid);
			    cookieend = strchr(cookiebuf, ';');
			    if (cookieend != NULL)
			       *cookieend = '\0';
			  	return cookiebuf;
			}
		}
	}

	return "-";
}

static const char *extract_request_timestamp(request_rec *r, char *a)
{
	char tstr[32];

	snprintf(tstr, 32, "%ld", time(NULL));
	return pstrdup(r->pool, tstr);
}

static const char *extract_note(request_rec *r, char *a)
{
	return ap_table_get(r->notes, a);

	/*ap_table_do(extract_table, r, r->notes, NULL);*/

}

static const char *extract_env_var(request_rec *r, char *a)
{
	return ap_table_get(r->subprocess_env, a);
}

static const char *extract_unique_id(request_rec *r, char *a)
{
    const char *tempid;

	tempid = ap_table_get(r->subprocess_env, "UNIQUE_ID");
	if (!tempid)
	  return "-";
	else
	  return tempid;
}

/* End declarations of various extract_ functions */



struct log_mysql_item_list {
	  char ch;						/* its letter code */
	  item_key_func func;			/* its extraction function */
	  const char *sql_field_name;	/* its column in SQL */
	  int want_orig_default;		/* if it requires the original request prior to internal redirection */
	  int string_contents;			/* if it returns a string */
    } log_mysql_item_keys[] = {

	{   'A', extract_agent,             "agent",            1, 1    },
    {   'b', extract_bytes_sent,        "bytes_sent",       0, 0    },
    {   'c', extract_cookie,            "cookie",           0, 1    },
    {   'e', extract_env_var,           "env_var",          0, 1    },
    {   'f', extract_request_file,      "request_file",     0, 1    },
	{   'H', extract_request_protocol,  "request_protocol", 0, 1    },
	{   'h', extract_remote_host,       "remote_host",      0, 1    },
    {   'i', extract_header_in,         "header_in",        0, 1    },
	{   'I', extract_unique_id,         "id",               0, 1    },
	{   'l', extract_remote_logname,    "remote_logname",   0, 1    },
	{	'm', extract_request_method,    "request_method",   0, 1    },
	{   'n', extract_note,              "note",             0, 1    },
    {   'o', extract_header_out,        "header_out",       0, 1    },
    {   'P', extract_child_pid,         "child_pid",        0, 0    },
    {   'p', extract_server_port,       "server_port",      0, 0    },
    {   'R', extract_referer,           "referer",          1, 1    },
    {   'r', extract_request_line,      "request_line",     1, 1    },
    {   'S', extract_request_timestamp, "time_stamp",       0, 0    },
    {   's', extract_status,            "status",           1, 0    },
    {   'T', extract_request_duration,  "request_duration", 1, 0    },
    {   't', extract_request_time,      "request_time",     0, 1    },
    {   'u', extract_remote_user,       "remote_user",      0, 1    },
    {   'U', extract_request_uri,       "request_uri",      1, 1    },
    {   'v', extract_virtual_host,      "virtual_host",     0, 1    },
	#ifdef WANT_SSL_LOGGING
    {   'q', extract_ssl_keysize,       "ssl_keysize",      0, 1    },
    {   'Q', extract_ssl_maxkeysize,    "ssl_maxkeysize",   0, 1    },
    {   'z', extract_ssl_cipher,        "ssl_cipher",       0, 1    },
	#endif
	{'\0'}
};


/* Routine to escape the 'dangerous' characters that would otherwise
 * corrupt the INSERT string: ', \, and "
 */
const char *escape_query(const char *from_str, pool *p)
{
	if (!from_str)
		return NULL;
	else {
	  	char *to_str;
		unsigned long length = strlen(from_str);
		unsigned long retval;

		/* Pre-allocate a new string that could hold twice the original, which would only
		 * happen if the whole original string was 'dangerous' characters.
		 */
		to_str = (char *) ap_palloc(p, length * 2 + 1);
		if (!to_str) {
			return from_str;
		}

		if (!mysql_log) {
			/* Well, I would have liked to use the current database charset.  mysql is
			 * unavailable, however, so I fall back to the slightly less respectful
			 * mysql_escape_string() function that uses the default charset.
			 */
			retval = mysql_escape_string(to_str, from_str, length);
		} else {
			/* MySQL is available, so I'll go ahead and respect the current charset when
			 * I perform the escape.
			 */
			retval = mysql_real_escape_string(mysql_log, to_str, from_str, length);
		}

		if (retval)
		  return to_str;
		else
		  return from_str;
	}
}

int open_logdb_link()
{
	/* Returns 2 if already connected, 1 if successful, 0 if unsuccessful */

	if (mysql_log != NULL) {
		return 2;
	}

	if (db_name) {
		mysql_init(&sql_server);
		mysql_log = mysql_real_connect(&sql_server, db_host, db_user, db_pwd, db_name, 0, socket_file, 0);

		if (mysql_log != NULL) {
			return 1;
		} else {
			return 0;
		}
	}
	return 0;
}

#ifdef DEBUG
static int trace(void *data, const char *key, const char *val)
{
	FILE *fp;
    request_rec *r = (request_rec *)data;

	fp = pfopen(r->pool, "/tmp/trace", "a");

	if (fp) {
	   fprintf(fp, "Field '%s' == '%s'\n", key, val);
	}

	pfclose(r->pool, fp);

	return TRUE;
}
#endif

const char *extract_table(void *data, const char *key, const char *val)
{
    request_rec *r = (request_rec *)data;

	return ap_pstrcat(r->pool, key, " = ", val, " ", NULL);
}

void preserve_entry(request_rec *r, const char *query)
{
	FILE *fp;
	log_mysql_state *cls = get_module_config(r->server->module_config, &mysql_log_module);

	fp = pfopen(r->pool, cls->preserve_file, "a");
	if (fp == NULL)
		ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"attempted append of local offline file but failed.");
	else {
		fprintf(fp,"%s;\n", query);
		pfclose(r->pool, fp);
	}
}

/*-----------------------------------------------------*/
/* safe_mysql_query: perform a database insert with    */
/* a degree of safety and error checking.              */
/*                                                     */
/* Parms:   request record, SQL insert statement       */
/* Returns: 0 (OK) on success                          */
/*          mysql return code on error                 */
/*-----------------------------------------------------*/
int safe_mysql_query(request_rec *r, const char *query)
{
	int retval;
	struct timespec delay, remainder;
	int ret;
	char *str;
	void (*handler) (int);


	/* A failed mysql_query() may send a SIGPIPE, so we ignore that signal momentarily. */
	handler = signal(SIGPIPE, SIG_IGN);

	/* First attempt for the query */
	retval = mysql_query(mysql_log, query);

	/* If we ran the query and it returned an error, try to be graceful.
	 * We only reach this point if the module thinks it has a valid connection to the
	 * database (i.e. mysql_log is non-null).  But that connection could go sour
	 * at any time, hence the check. */
	if ( retval != 0 )
    {
			log_mysql_state *cls = get_module_config(r->server->module_config, &mysql_log_module);

			/* Something went wrong, so start by trying to restart the db link. */
		    ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"attempting reconnect because API said: %s", mysql_error(mysql_log));

			mysql_log = NULL;
			open_logdb_link();

    		if (mysql_log == NULL) {	 /* still unable to link */
    			signal(SIGPIPE, handler);
    			ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"httpd child reconnect failed, unable to reach database. SQL logging stopped until an httpd child regains a db connection.");
				ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"log entries are being preserved in %s", cls->preserve_file);
				preserve_entry(r, query);
    			return retval;
    		} else {
	    		ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"reconnect successful.");
			}

		    /* Attempt a single re-try... First sleep for a tiny amount of time. */
	        delay.tv_sec = 0;
	        delay.tv_nsec = 250000000;  /* max is 999999999 (nine nines) */
	        ret = nanosleep(&delay, &remainder);
	        if (ret && errno != EINTR)
				ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"nanosleep unsuccessful.");

	        /* Now make our second attempt */
		    retval = mysql_query(mysql_log,query);

			/* If this one also failed, log that and append to our local offline file */
		    if ( retval != 0 )
		    {
	    		str = ap_pstrcat(r->pool, "delayed insert attempt failed, API said: ", MYSQL_ERROR(mysql_log), NULL);
	    		ap_log_error(APLOG_MARK,ERRLEVEL,r->server,str);

				preserve_entry(r, query);
				ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"entry preserved in %s", cls->preserve_file);
			} else {
	    	    ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"insert successful after a delayed retry.");
	    	}
	}

	/* Restore SIGPIPE to its original handler function */
	signal(SIGPIPE, handler);

	return retval;
}


/* ------------------------------------------------*
 * Command handlers that are called according      *
 * to the directives found at Apache runtime.      *
 * ------------------------------------------------*/

const char *set_mysql_massvirtual(cmd_parms *parms, void *dummy, int flag)
{
	massvirtual = ( flag ? 1 : 0);
	return NULL;
}

const char *set_log_mysql_create(cmd_parms *parms, void *dummy, int flag)
{
	create_tables = ( flag ? 1 : 0);
	return NULL;
}

const char *set_log_mysql_db(cmd_parms *parms, void *dummy, char *arg)
{
	db_name = arg;
	return NULL;
}

const char *set_log_mysql_cookie(cmd_parms *parms, void *dummy, char *arg)
{
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	cls->cookie_name = arg;
	return NULL;
}

const char *set_log_mysql_preserve_file(cmd_parms *parms, void *dummy, char *arg)
{
	/* char *pfile; */
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	/* pfile = ap_pstrcat(parms->pool, "/tmp/", arg, NULL); */
	cls->preserve_file = arg;
	return NULL;
}

const char *set_log_mysql_info(cmd_parms *parms, void *dummy, char *host, char *user, char *pwd)
{
	if (*host != '.') {
		db_host = host;
	}
	if (*user != '.') {
		db_user = user;
	}
	if (*pwd != '.') {
		db_pwd = pwd;
	}
	return NULL;
}

const char *set_transfer_log_mysql_table(cmd_parms *parms, void *dummy, char *arg)
{
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	if (massvirtual != 0)
		ap_log_error(APLOG_MARK,WARNINGLEVEL,parms->server,"do not set MySQLTransferLogTable when MySQLMassVirtualHosting is On. Ignoring.");
	else
		cls->transfer_table_name = arg;
	return NULL;
}

const char *set_notes_log_mysql_table(cmd_parms *parms, void *dummy, char *arg)
{
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	if (massvirtual != 0)
		ap_log_error(APLOG_MARK,WARNINGLEVEL,parms->server,"do not set MySQLNotesLogTable when MySQLMassVirtualHosting is On. Ignoring.");
	else
		cls->notes_table_name = arg;
	return NULL;
}

const char *set_mysql_transfer_log_format(cmd_parms *parms, void *dummy, char *arg)
{
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	cls->transfer_log_format = arg;
	return NULL;
}

const char *set_mysql_socket_file(cmd_parms *parms, void *dummy, char *arg)
{
	socket_file = arg;

	return NULL;
}


const char *add_referer_mysql_ignore(cmd_parms *parms, void *dummy, char *arg)
{
	char **addme;
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	addme = push_array(cls->referer_ignore_list);
	*addme = pstrdup(cls->referer_ignore_list->pool, arg);
	return NULL;
}

const char *add_transfer_mysql_ignore(cmd_parms *parms, void *dummy, char *arg)
{
	char **addme;
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	addme = push_array(cls->transfer_ignore_list);
	*addme = pstrdup(cls->transfer_ignore_list->pool, arg);
	return NULL;
}

const char *add_remhost_mysql_ignore(cmd_parms *parms, void *dummy, char *arg)
{
	char **addme;
	log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

	addme = push_array(cls->remhost_ignore_list);
	*addme = pstrdup(cls->remhost_ignore_list->pool, arg);
	return NULL;
}

const char *add_mysql_note(cmd_parms *parms, void *dummy, char *arg)
{
    char **addme;
    log_mysql_state *cls = get_module_config(parms->server->module_config, &mysql_log_module);

    addme = push_array(cls->notes_list);
    *addme = pstrdup(cls->notes_list->pool, arg);
    return NULL;
}




/*------------------------------------------------------------*
 * Apache-specific hooks into the module code                 *
 * that are defined in the array 'mysql_lgog_module' (at EOF) *
 *------------------------------------------------------------*/


/*
 * This function is called during server initialisation when an heavy-weight
 * process (such as a child) is being initialised.  As with the
 * module-initialisation function, any information that needs to be recorded
 * must be in static cells, since there's no configuration record.
 *
 * There is no return value.
 */
static void log_mysql_child_init(server_rec *s, pool *p)
{
	int retval;

	retval = open_logdb_link();
	#ifdef DEBUG
	if (retval > 0) {
   	    ap_log_error(APLOG_MARK,DEBUGLEVEL,s,"open_logdb_link successful");
	}
	#endif
}

/*
 * This function is called when an heavy-weight process (such as a child) is
 * being run down or destroyed.  As with the child-initialisation function,
 * any information that needs to be recorded must be in static cells, since
 * there's no configuration record.
 *
 * There is no return value.
 */
static void log_mysql_child_exit(server_rec *s, pool *p)
{
	mysql_close(mysql_log);
}


/*
void *log_mysql_initializer(server_rec *main_server, pool *p)
{
	server_rec *s;

    log_mysql_state main_conf = ap_get_module_config(main_server->module_config, &mysql_log_module);

	for (server_rec *s = main_server; s; s = s->next) {
	    conf = ap_get_module_config(s->module_config, &mysql_log_module);
	    if (conf->transfer_log_format == NULL && s != main_server) {
	        *conf = *main_conf;
		}

}
 */

/*
 * This function gets called to create a per-server configuration
 * record.  It will always be called for the main server and
 * for each virtual server that is established.  Each server maintains
 * its own state that is separate from the others' states.
 *
 * The return value is a pointer to the created module-specific
 * structure.
 */
void *log_mysql_make_state(pool *p, server_rec *s)
{

	log_mysql_state *cls = (log_mysql_state *) ap_palloc(p, sizeof(log_mysql_state));

	cls->transfer_table_name = NULL;
	cls->transfer_log_format = NULL;
	cls->notes_table_name    = "notes";

	cls->referer_ignore_list  = make_array(p, 1, sizeof(char *));
	cls->transfer_ignore_list = make_array(p, 1, sizeof(char *));
	cls->remhost_ignore_list  = make_array(p, 1, sizeof(char *));
	cls->notes_list           = make_array(p, 1, sizeof(char *));
	cls->table_made    = 0;

	cls->preserve_file = "/tmp/mysql-preserve";
	cls->cookie_name = NULL;

	return (void *) cls;
}


/* Setup of the available httpd.conf configuration commands.
 * Structure: command, function called, NULL, where available, how many arguments, verbose description
 */
command_rec log_mysql_cmds[] = {
	{"MySQLTransferLogTable", set_transfer_log_mysql_table, NULL, 	RSRC_CONF, 	TAKE1,
	 "The MySQL table that holds the transfer log"}
	,
	{"MySQLNotesLogTable", set_notes_log_mysql_table,	    NULL, 	RSRC_CONF, 	TAKE1,
	 "The MySQL table that holds the notes"}
	,
	{"MySQLTransferLogFormat", set_mysql_transfer_log_format,	NULL, 	RSRC_CONF, 	TAKE1,
	 "Instruct the module what information to log to the MySQL transfer log"}
	,
	{"MySQLRefererIgnore", add_referer_mysql_ignore, 		NULL, 	RSRC_CONF, 	ITERATE,
	 "List of referers to ignore. Accesses that match will not be logged to MySQL"}
	,
	{"MySQLRequestIgnore", add_transfer_mysql_ignore, 		NULL, 	RSRC_CONF, 	ITERATE,
	 "List of URIs to ignore. Accesses that match will not be logged to MySQL"}
	,
	{"MySQLRemhostIgnore", add_remhost_mysql_ignore, 		NULL, 	RSRC_CONF, 	ITERATE,
	 "List of remote hosts to ignore. Accesses that match will not be logged to MySQL"}
	,
	{"MySQLDatabase", set_log_mysql_db, 					NULL, 	RSRC_CONF, 	TAKE1,
	 "The name of the MySQL database for logging"}
	,
	{"MySQLWhichCookie", set_log_mysql_cookie, 				NULL, 	RSRC_CONF, 	TAKE1,
	 "The CookieName that you want logged when using the 'c' config directive"}
	,
	{"MySQLLoginInfo", set_log_mysql_info, 					NULL, 	RSRC_CONF, 	TAKE3,
	 "The MySQL host, user-id and password for logging"}
	,
	{"MySQLCreateTables", set_log_mysql_create,				NULL, 	RSRC_CONF, 	FLAG,
	 "Turn on module's capability to create its SQL tables on the fly"}
	,
	{"MySQLMassVirtualHosting", set_mysql_massvirtual,      NULL,   RSRC_CONF,  FLAG,
	 "Activates option(s) useful for ISPs performing mass virutal hosting"}
	,
	{"MySQLPreserveFile", set_log_mysql_preserve_file,		NULL, 	RSRC_CONF, 	TAKE1,
	 "Name of the file to use for data preservation during database downtime"}
	,
	{"MySQLSocketFile", set_mysql_socket_file,				NULL, 	RSRC_CONF, 	TAKE1,
	 "Name of the file to employ for socket connections to MySQL"}
	,
	{"MySQLWhichNotes", add_mysql_note,						NULL,	RSRC_CONF,	ITERATE,
	 "Members of the 'notes' table that you would like to log"}
	,
	{NULL}
};



/* Routine to perform the actual construction and execution of the relevant
 * INSERT statements.
 */
int log_mysql_transaction(request_rec *orig)
{
	char **ptrptr, **ptrptr2;
	log_mysql_state *cls = get_module_config(orig->server->module_config, &mysql_log_module);
	const char *access_query;
	request_rec *r;

	/* We handle mass virtual hosting differently.  Dynamically determine the name
	 * of the table from the virtual server's name, and flag it for creation.
	 */
	if (massvirtual == 1) {
		char *access_base = "access_";
		char *notes_base = "notes_";
		char *a_tablename;
		char *n_tablename;
		int i;

		/* Find memory long enough to hold the table name + \0. */
		a_tablename = ap_pstrcat(orig->pool, access_base, ap_get_server_name(orig), NULL);
		n_tablename = ap_pstrcat(orig->pool, notes_base,  ap_get_server_name(orig), NULL);

		/* Transform any dots or dashes to underscores */
		for (i = 0; i < strlen(a_tablename); i++) {
			if ( (a_tablename[i] == '.') || (a_tablename[i] == '-') )
			  a_tablename[i] = '_';
		}
		for (i = 0; i < strlen(n_tablename); i++) {
			if ( (n_tablename[i] == '.') || (n_tablename[i] == '-') )
			  n_tablename[i] = '_';
		}

		/* Tell this virtual server its transfer table name, and
		 * turn on create_tables, which is implied by massvirtual.
		 */
		cls->transfer_table_name = a_tablename;
		cls->notes_table_name = n_tablename;
		create_tables = 1;
	}

	/* Do we have enough info to log? */
	if ( ((cls->transfer_table_name == NULL) ? 1 : 0) ) {
		return DECLINED;
	} else {
		const char *thehost;
		const char *thenote;
		char *fields = "", *values = "";
		char *notesets = "";
		char *note_query = "";
		const char *unique_id;
		const char *formatted_item;
		int i, j, length;
		char *create_access = NULL;
		char *create_notes = NULL;

		for (r = orig; r->next; r = r->next) {
			continue;
		}

		#ifdef DEBUG
		 /*ap_table_do(trace, orig, orig->subprocess_env, NULL);*/
		#endif

		/* The following is a stolen upsetting mess of pointers, I'm sorry.
		 * Anyone with the motiviation and/or the time should feel free
		 * to make this cleaner. :) */
		ptrptr2 = (char **) (cls->transfer_ignore_list->elts + (cls->transfer_ignore_list->nelts * cls->transfer_ignore_list->elt_size));

		/* Go through each element of the ignore list and compare it to the
		 * request_uri.  If we get a match, return without logging */
		if (r->uri) {
			for (ptrptr = (char **) cls->transfer_ignore_list->elts; ptrptr < ptrptr2; ptrptr = (char **) ((char *) ptrptr + cls->transfer_ignore_list->elt_size)) {
				if (strstr(r->uri, *ptrptr)) {
					return OK;
				}
			}
		}

		/* Go through each element of the ignore list and compare it to the
		 * remote host.  If we get a match, return without logging */
		ptrptr2 = (char **) (cls->remhost_ignore_list->elts + (cls->remhost_ignore_list->nelts * cls->remhost_ignore_list->elt_size));
		thehost = get_remote_host(r->connection, r->per_dir_config, REMOTE_NAME);
		if (thehost) {
			for (ptrptr = (char **) cls->remhost_ignore_list->elts; ptrptr < ptrptr2; ptrptr = (char **) ((char *) ptrptr + cls->remhost_ignore_list->elt_size)) {
				if (strstr(thehost, *ptrptr)) {
					return OK;
				}
			}
		}

		/* If not specified by the user, use the default format */
		if (cls->transfer_log_format == NULL) {
			cls->transfer_log_format = "AbHhmRSsTUuv";
		}
		length = strlen(cls->transfer_log_format);

		/* Iterate through the format characters and set up the INSERT string according to
		 * what the user has configured. */
		for (i = 0; i < length; i++) {
			j = 0;

			while (log_mysql_item_keys[j].ch) {

				  if (log_mysql_item_keys[j].ch == cls->transfer_log_format[i]) {
					/* Yes, this key is one of the configured keys.
					 * Call the key's function and put the returned value into 'formatted_item' */
					formatted_item = log_mysql_item_keys[j].func(log_mysql_item_keys[j].want_orig_default ? orig : r, "");

				     /* Massage 'formatted_item' for proper SQL eligibility... */
					if (!formatted_item) {
						formatted_item = "";
					} else if (formatted_item[0] == '-' && formatted_item[1] == '\0' && !log_mysql_item_keys[j].string_contents) {
						/* If apache tried to log a '-' character for a numeric field, convert that to a zero
						 * because the database expects a numeral and will reject the '-' character. */
						formatted_item = "0";
					}

				     /* Append the fieldname and value-to-insert to the appropriate strings, quoting stringvals with ' as appropriate */
					fields = pstrcat(r->pool, fields, (i > 0 ? "," : ""),
									 log_mysql_item_keys[j].sql_field_name, NULL);

					values = pstrcat(r->pool, values, (i > 0 ? "," : ""),
									 (log_mysql_item_keys[j].string_contents ? "'" : ""),
								     escape_query(formatted_item, r->pool),
									 (log_mysql_item_keys[j].string_contents ? "'" : ""), NULL);
					break;
				}
				j++;

			}
		}


		/*
	    fields = pstrcat(r->pool, fields, ",note", NULL);
		values = pstrcat(r->pool, values, ",'", NULL);
		*/

		/* Work through the list of notes defined by MySQLNotesToLog */
		i = 0;
		unique_id = extract_unique_id(r, "");

		ptrptr2 = (char **) (cls->notes_list->elts + (cls->notes_list->nelts * cls->notes_list->elt_size));
		for (ptrptr = (char **) cls->notes_list->elts; ptrptr < ptrptr2; ptrptr = (char **) ((char *) ptrptr + cls->notes_list->elt_size)) {
			/* If the specified note (*ptrptr) exists for the current request... */
		    if ((thenote = ap_table_get(r->notes, *ptrptr))) {
				notesets = ap_pstrcat(r->pool, notesets,
									  (i > 0 ? "," : ""),
									  "('",
									  unique_id,
									  "','",
									  escape_query(*ptrptr, r->pool),
									  "','",
									  escape_query(thenote, r->pool),
									  "')",
									  NULL);
				i++;
			}
		}
		note_query = ap_pstrcat(r->pool, note_query, "insert into ", cls->notes_table_name, " (id, item, val) values ", notesets, NULL);
		#ifdef DEBUG
			ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"note string: %s", note_query);
	   	#endif

		/*
		values = pstrcat(r->pool, values, "'", NULL);
		 */



		/* Is this virtual server's table flagged as made?  We flag it as such in order
		 * to avoid extra processing with each request.  If it's not flagged as made,
		 * set up the CREATE string.
		 */
		if ((cls->table_made != 1) && (create_tables != 0)) {
			char *createprefix = "create table if not exists ";
			char *access_suffix =
			 " (id char(19),\
			   agent varchar(255),\
			   bytes_sent int unsigned,\
	           child_pid smallint unsigned,\
	           cookie varchar(255),\
	           request_file varchar(255),\
	           referer varchar(255),\
	           remote_host varchar(50),\
	           remote_logname varchar(50),\
	           remote_user varchar(50),\
	           request_duration smallint unsigned,\
	           request_line varchar(255),\
	           request_method varchar(6),\
	           request_protocol varchar(10),\
	           request_time char(28),\
	           request_uri varchar(50),\
	           server_port smallint unsigned,\
	           ssl_cipher varchar(25),\
	           ssl_keysize smallint unsigned,\
	           ssl_maxkeysize smallint unsigned,\
	           status smallint unsigned,\
	           time_stamp int unsigned,\
	           virtual_host varchar(50))";

			char *notes_suffix =
			 " (id char(19),\
			   item varchar(80),\
			   val varchar(80))";

			/* Find memory long enough to hold the whole CREATE string + \0 */
			create_access = ap_pstrcat(orig->pool, createprefix, cls->transfer_table_name, access_suffix, NULL);
			create_notes  = ap_pstrcat(orig->pool, createprefix, cls->notes_table_name, notes_suffix, NULL);

			#ifdef DEBUG
				ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"create string: %s", create_access);
				ap_log_error(APLOG_MARK,DEBUGLEVEL,orig->server,"create string: %s", create_notes);
			#endif

		}

		/* Set up the actual INSERT statement */
		access_query = ap_pstrcat(r->pool, "insert into ", cls->transfer_table_name, " (", fields, ") values (", values, ")", NULL);

		#ifdef DEBUG
	        ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"insert string: %s", access_query);
	    #endif


		/* How's our mysql link integrity? */
		if (mysql_log == NULL) {

			/* Make a try to establish the link */
			open_logdb_link();

			if (mysql_log == NULL) {
				/* Unable to re-establish a DB link, so assume that it's really
				 * gone and send the entry to the preserve file instead.
				 * Note that we don't keep logging the db error over and over. */
				preserve_entry(orig, access_query);
				preserve_entry(orig, note_query);
				return OK;
			} else {
				/* Whew, we got the DB link back */
				ap_log_error(APLOG_MARK,NOTICELEVEL,orig->server,"httpd child established database connection");
			}
		}

		/* Make the table if we're supposed to */
		if ((cls->table_made != 1) && (create_tables != 0)) {
		  	mysql_query(mysql_log, create_access);
		  	mysql_query(mysql_log, create_notes);
		  	cls->table_made = 1;
		}

  	    /* Make the insert */
		safe_mysql_query(orig, access_query);
		safe_mysql_query(orig, note_query);

		return OK;
	}
}




/* The configuration array that sets up the hooks into the module. */
module mysql_log_module = {
	STANDARD_MODULE_STUFF,
	NULL,					 /* module initializer 				*/
	NULL,					 /* create per-dir config 			*/
	NULL,					 /* merge per-dir config 			*/
	log_mysql_make_state,	 /* create server config 			*/
	NULL,					 /* merge server config 			*/
	log_mysql_cmds,			 /* config directive table 			*/
	NULL,					 /* [9] content handlers 			*/
	NULL,					 /* [2] URI-to-filename translation */
	NULL,					 /* [5] check/validate user_id 		*/
	NULL,					 /* [6] check authorization 		*/
	NULL,					 /* [4] check access by host		*/
	NULL,					 /* [7] MIME type checker/setter 	*/
	NULL,					 /* [8] fixups 						*/
	log_mysql_transaction,	 /* [10] logger 					*/
	NULL					 /* [3] header parser 				*/
#if MODULE_MAGIC_NUMBER >= 19970728 /* 1.3-dev or later support these additionals... */
	,log_mysql_child_init,   /* child process initializer 		*/
	log_mysql_child_exit,    /* process exit/cleanup 			*/
	NULL					 /* [1] post read-request 			*/
#endif

};