summaryrefslogtreecommitdiffstats
path: root/mod_log_sql.c
blob: 0739372266326054fc639e969f1a451efab12bb0 (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
/* $Id: mod_log_sql.c,v 1.2 2001/11/30 08:29:04 helios Stab $
 *
 * mod_log_mysql.c
 * Release v 1.10
 *
 * Hi, I'm the new maintainer of this code.  If you have any questions,
 * comments or suggestions (which are always welcome), please contact Chris
 * Powell <chris@grubbybaby.com>.  This code still falls under the rules of
 * the Apache license, and all credit for the code up to my changes is still
 * preserved below.
 *
 * ====================================================================
 *
 * The original preface from version 1.05: This module was patched, wrapped
 * and coded by Zeev Suraski <bourbon@netvision.net.il>
 *
 * It may be used freely, with the same restrictions as its predecessors
 * (specified below).  This module is based on code from standard apache
 * modules.  Their copyright notice follows.
 *
 * ====================================================================
 * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * 4. The names "Apache Server" and "Apache Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission.
 *
 * 5. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Group and was originally based
 * on public domain software written at the National Center for
 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
 * For more information on the Apache Group and the Apache HTTP server
 * project, please see <http://www.apache.org/>.
 *
 */


#include <time.h>
#include <mysql/mysql.h>

#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_core.h"
#if MODULE_MAGIC_NUMBER >= 19980324
 #include "ap_compat.h"
#endif

module mysql_log_module;
MYSQL log_sql_server, *mysql_log = NULL;
char *log_db_name = NULL, *db_host = NULL, *db_user = NULL, *db_pwd = NULL;

#define MYSQL_ERROR(mysql) ((mysql)?(mysql_error(mysql)):"MySQL server has gone away")

typedef const char *(*item_key_func) (request_rec *, char *);
typedef struct {
	char *referer_table_name, *agent_table_name, *transfer_table_name;
	array_header *referer_ignore_list;
	array_header *transfer_ignore_list;
	array_header *remhost_ignore_list;
	char *transfer_log_format;
} log_mysql_state;

/* Defined in /usr/local/Apache/include/ap_mmn.h, 19990320 as of this writing. */
#if MODULE_MAGIC_NUMBER < 19970103
extern const char *log_remote_host(request_rec *r, char *a);
extern const char *log_remote_logname(request_rec *r, char *a);
extern const char *log_remote_user(request_rec *r, char *a);
extern const char *log_request_time(request_rec *r, char *a);
extern const char *log_request_timestamp(request_rec *r, char *a);
extern const char *log_request_duration(request_rec *r, char *a);
extern const char *log_request_line(request_rec *r, char *a);
extern const char *log_request_file(request_rec *r, char *a);
extern const char *log_request_uri(request_rec *r, char *a);
extern const char *log_status(request_rec *r, char *a);
extern const char *log_bytes_sent(request_rec *r, char *a);
extern const char *log_header_in(request_rec *r, char *a);
extern const char *log_header_out(request_rec *r, char *a);
extern const char *log_note(request_rec *r, char *a);
extern const char *log_env_var(request_rec *r, char *a);
extern const char *log_virtual_host(request_rec *r, char *a);
extern const char *log_server_port(request_rec *r, char *a);
extern const char *log_child_pid(request_rec *r, char *a);
#else
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 *log_remote_host(request_rec *r, char *a)
{
	return (char *) get_remote_host(r->connection, r->per_dir_config, REMOTE_NAME);
}

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

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

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

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

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

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

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

static const char *log_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 *log_header_in(request_rec *r, char *a)
{
	return table_get(r->headers_in, a);
}

static const char *log_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 *log_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 *log_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 *log_virtual_host(request_rec *r, char *a)
{
	return pstrdup(r->pool, r->server->server_hostname);
}

static const char *log_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 *log_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 *log_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 *log_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 *log_cookie(request_rec *r, char *a)
{
    const char *cookiestr;
    char *cookieend;
    
	cookiestr  = table_get(r->headers_in,  "cookie");

	/* First look for Cookie2: header */
    if ( (cookiestr = table_get(r->headers_in,  "cookie2")) ) {
    	cookieend = strchr(cookiestr, ';');
    	if (cookieend)
       		*cookieend = '\0';      /* Ignore anything after a ; */
    	return cookiestr;
	}
	
	/* Then try a Cookie: header */
    else if ( (cookiestr = table_get(r->headers_in,  "cookie")) ) {
    	cookieend = strchr(cookiestr, ';');
    	if (cookieend)
       		*cookieend = '\0';
    	return cookiestr;
	}
	
	/* Still none?  Use the Set-Cookie: header.  I feel a little
	 * guilty about this, because some clients refuse cookies.  The
	 * log will in their cases log a ton of different Set-Cookie requests
	 * that aren't being honored.  However, it's necessary to insert this
	 * check so that the first request of a series doesn't log a - ...
	 */
    else if ( (cookiestr  = table_get(r->headers_out,  "set-cookie")) ) {
		cookieend = strchr(cookiestr, ';');
		if (cookieend)
	    	*cookieend = '\0';
		return cookiestr;
	}

	/* Okay, fine, no eligible headers.  Return a - instead.
	 * I /could/ insert a look for the Set-Cookie2: header here, but I think
	 * it would be imprudent.  It's apparent that the current browsers don't
	 * support Cookie2 cookies, so why bother logging a bunch of Set-Cookie2:
	 * requests that aren't even going to be honored?
	 */
	else {    
		return "-"; 
    }
}
	   
const char *log_request_timestamp(request_rec *r, char *a)
{
	char tstr[32];

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

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

static const char *log_env_var(request_rec *r, char *a)
{
	return table_get(r->subprocess_env, a);
}
#endif


/* End declarations of various log_ functions */


struct log_mysql_item_list {
	  char ch;
	  item_key_func func;
	  const char *sql_field_name;
	  int want_orig_default;
	  int string_contents;
    } log_mysql_item_keys[] = {

	{   'A', log_agent,             "agent",            1, 1    },
    {   'b', log_bytes_sent,        "bytes_sent",       0, 0    },
    {   'c', log_cookie,            "cookie",           0, 1    },
    {   'e', log_env_var,           "env_var",          0, 1    },
    {   'f', log_request_file,      "request_file",     0, 1    },
    {   'h', log_remote_host,       "remote_host",      0, 1    },
    {   'i', log_header_in,         "header_in",        0, 1    },
    {   'l', log_remote_logname,    "remote_logname",   0, 1    },
    {   'n', log_note,              "note",             0, 1    },
    {   'o', log_header_out,        "header_out",       0, 1    },
    {   'P', log_child_pid,         "child_pid",        0, 0    },
    {   'p', log_server_port,       "server_port",      0, 0    },
    {   'R', log_referer,           "referer",          1, 1    },
    {   'r', log_request_line,      "request_line",     1, 1    },
    {   'S', log_request_timestamp, "time_stamp",       0, 0    },
    {   's', log_status,            "status",           1, 0    },
    {   'T', log_request_duration,  "request_duration", 1, 0    },
    {   't', log_request_time,      "request_time",     0, 1    },
    {   'u', log_remote_user,       "remote_user",      0, 1    },
    {   'U', log_request_uri,       "request_uri",      1, 1    },
    {   'v', log_virtual_host,      "virtual_host",     0, 1    },
	{'\0'}
};


/* Routine to escape 'dangerous' characters that would otherwise
 * corrupt the INSERT string: ', \, and "
 */
const char *mysql_escape_log(const char *str, pool *p)
{
	register int i = 0, j = 0;
	int need_to_escape = 0;

	if (!str) {
		return NULL;
	}

	/* First find out if we need to escape.   */
	i = 0;
	while (str[i]) {
		/* WAS THIS WRONG in 1.05?!?   if (str[i] != '\'' || str[i] != '\\' || str[i] != '\"') { */
		if (str[i] == '\'' || str[i] == '\\' || str[i] == '\"') {
			need_to_escape = 1;
			break;
		}
		i++;
	}

	if (need_to_escape) {
		char *tmp_str;
		int length = strlen(str);

		/* Pre-allocate a new string that could hold twice the original, which would only
		 * happen if the whole original string was 'dangerous' characters.
		 */
		tmp_str = (char *) palloc(p, length *2 + 1);
		if (!tmp_str) {
			return str;
		}
		
		/* Walk through character-by-character, escaping any dangerous characters found. */
		for (i = 0, j = 0; i < length; i++, j++) {
			switch (str[i]) {
			    case '\'':
			    case '\"':
			    case '\\':
				    tmp_str[j] = '\\';
				    j++;
			    default:
				    tmp_str[j] = str[i];
			}
		}
		tmp_str[j] = '\0';
		return tmp_str;
	} else {
		return str;
	}
}


void open_log_dblink()
{
	if (mysql_log != NULL) {		 /* virtual database link shared with main server */
		return;
	}
	if (log_db_name) {			 /* open an SQL link */
		mysql_log = mysql_connect(&log_sql_server, db_host, db_user, db_pwd);
		if (mysql_log) {		 /* link opened */
			if (mysql_select_db(mysql_log, log_db_name) != 0) {	/* unable to select database */
				mysql_close(mysql_log);
				mysql_log = NULL;
			}
		}
	}
}


void *make_log_mysql_state(pool *p, server_rec *s)
{
	log_mysql_state *cls = (log_mysql_state *) palloc(p, sizeof(log_mysql_state));

	cls->referer_table_name = cls->agent_table_name = cls->transfer_table_name = "";
	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->transfer_log_format = "";
	return (void *) cls;
}

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

	cls->referer_table_name = arg;
	return NULL;
}


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

	cls->agent_table_name = arg;
	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);

	cls->transfer_table_name = arg;
	return NULL;
}


const char *set_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_log_mysql_db(cmd_parms *parms, void *dummy, char *arg)
{
	log_db_name = 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 *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;
}

command_rec log_mysql_cmds[] = {
	{"RefererLogMySQLTable", set_referer_log_mysql_table, NULL, RSRC_CONF, TAKE1,
	 "the table of the referer log"}
	,
	{"AgentLogMySQLTable", set_agent_log_mysql_table, NULL, RSRC_CONF, TAKE1,
	 "the table of the agent log"}
	,
	{"TransferLogMySQLTable", set_transfer_log_mysql_table, NULL, RSRC_CONF, TAKE1,
	 "the table of the transfer log"}
	,
	{"TransferLogMySQLFormat", set_transfer_log_format, NULL, RSRC_CONF, TAKE1,
	 "specific format for the MySQL transfer log"}
	,
	{"RefererIgnore", add_referer_mysql_ignore, NULL, RSRC_CONF, ITERATE,
	 "referer hostnames to ignore"}
	,
	{"RequestIgnore", add_transfer_mysql_ignore, NULL, RSRC_CONF, ITERATE,
	 "transfer log URIs to ignore"}
	,
	{"RemhostIgnore", add_remhost_mysql_ignore, NULL, RSRC_CONF, ITERATE,
	 "transfer log remote hosts to ignore"}
	,
	{"LogMySQLDB", set_log_mysql_db, NULL, RSRC_CONF, TAKE1,
	 "the database of the referer log"}
	,
	{"LogMySQLInfo", set_log_mysql_info, NULL, RSRC_CONF, TAKE3,
	 "host, user and password for MySQL link"}
	,
	{NULL}
};


int safe_mysql_query(request_rec *r, const char *query)
{
	int error = 1;
	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);	 

	/* If there's no DB link, or if we run the query and it gacks, try to be graceful */
	if ( !mysql_log || 
	     (
	        (error = mysql_query(mysql_log, query)) && 
	        !strcasecmp(mysql_error(mysql_log), "MySQL server has gone away")
	     )
	   ) 
	   
	   {    /* We need to restart the server link */
		    mysql_log = NULL;
		    log_error("MySQL:  connection lost, attempting reconnect", r->server);

    		open_log_dblink();

    		if (mysql_log == NULL) {	 /* still unable to link */
    			signal(SIGPIPE, handler);
    			log_error("MySQL:  reconnect failed.", r->server);
    			return error;
    		}

    		log_error("MySQL:  reconnect successful.", r->server);
    		error = mysql_query(mysql_log, query);
	}

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

	if (error) {
	    /* Attempt a single re-try... First sleep for a tiny amount of time. */
        delay.tv_sec = 0;
        delay.tv_nsec = 500000000;  /* max is 999999999 (nine nines) */
        ret = nanosleep(&delay, &remainder);
        if (ret && errno != EINTR)
           perror("nanosleep");

        /* Now re-attempt */
	    error = mysql_query(mysql_log,query);

	    if (error) {
    		str = pstrcat(r->pool, "MySQL query failed:  ", query, NULL);
    		log_error(str, r->server);
    		str = pstrcat(r->pool, "MySQL failure reason:  ", MYSQL_ERROR(mysql_log), NULL);
    		log_error(str, r->server);
    	} else {
    	    log_error("MySQL:  INSERT successful after a delayed retry.", r->server);
    	}
	}
	return error;
}


/* 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);
	char *str;
	const char *referer;
	request_rec *r;
	int retvalue = DECLINED;
	int referer_needed, agent_needed, transfer_needed;

	/* Are there configuration directives for these SQL logs?  For each found
	 * config directive that is found, mark that type as 'needed'.
	 */
	referer_needed = ((cls->referer_table_name[0] != '\0') ? 1 : 0);
	agent_needed = ((cls->agent_table_name[0] != '\0') ? 1 : 0);
	transfer_needed = ((cls->transfer_table_name[0] != '\0') ? 1 : 0);

	if (!referer_needed && !agent_needed && !transfer_needed) {
		return OK;
	}

	if (mysql_log == NULL) {		 /* mysql link not up, hopefully we can do something about it */
		open_log_dblink();
		if (mysql_log == NULL) {
			return OK;
		}
	}

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

	/* Log the 'referer' to its own log if configured to do so. */
	if (referer_needed) {			 
		retvalue = OK;
		referer = table_get(orig->headers_in, "Referer");
		if (referer != NULL) {

			/* The following is an 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->referer_ignore_list->elts + (cls->referer_ignore_list->nelts * cls->referer_ignore_list->elt_size));

			/* Go through each element of the ignore list and compare it to the
			 * referer_host.  If we get a match, return without logging */
			for (ptrptr = (char **) cls->referer_ignore_list->elts; ptrptr < ptrptr2; ptrptr = (char **) ((char *) ptrptr + cls->referer_ignore_list->elt_size)) {
				if (strstr(referer, *ptrptr)) {
					return OK;
				}
			}
			str = pstrcat(orig->pool, "insert into ", cls->referer_table_name, " (referer,url,time_stamp) values ('", mysql_escape_log(referer, orig->pool), "','", mysql_escape_log(r->uri, orig->pool), "',unix_timestamp(now()) )", NULL);
			safe_mysql_query(orig, str);
		}
	}

	/* Log the 'user agent' to its own log if configured to do so. */
	if (agent_needed) {			 
		const char *agent, *str;
		
		retvalue = OK;
		agent = table_get(orig->headers_in, "User-Agent");
		
		if (agent != NULL) {
			str = pstrcat(orig->pool, "insert into ", cls->agent_table_name, "(agent,time_stamp) values ('", mysql_escape_log(agent, orig->pool), "',unix_timestamp(now()) )", NULL);
			safe_mysql_query(orig, str);
		}
	}

	/* Log the transfer to its own log if configured to do so. */
	if (transfer_needed) {
		const char *thehost;

		char *fields = "", *values = "", *query;
		const char *formatted_item;
		int i, j, length;

		retvalue = OK;


		/* 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, and while at it, clean the same mess at the RefererLog part :) */
		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 retvalue;
				}
			}
		}

		/* 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 retvalue;
				}
			}
		}

		/* If not specified by the user, use the default format */
		if (cls->transfer_log_format[0] == '\0') {	
			cls->transfer_log_format = "huSUsbTvRA";
		}
		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 an integer. */
						formatted_item = "0";
					}
				    
				     /* Append the fieldname and value-to-insert to teh appropriate strings, quoting stringvals with ' as appropriate */
					fields = pstrcat(orig->pool, fields, (i > 0 ? "," : ""), log_mysql_item_keys[j].sql_field_name, NULL);
					values = pstrcat(orig->pool, values, (i > 0 ? "," : ""), (log_mysql_item_keys[j].string_contents ? "'" : ""), mysql_escape_log(formatted_item, orig->pool), (log_mysql_item_keys[j].string_contents ? "'" : ""), NULL);
					break;
				}
				j++;
			}
		}

		/* Set up the actual INSERT statement and execute it. */
		query = pstrcat(orig->pool, "insert into ", cls->transfer_table_name, " (", fields, ") values (", values, ")", NULL);
		safe_mysql_query(orig, query);

	}
	return retvalue;
}



module mysql_log_module = {
	STANDARD_MODULE_STUFF,
	NULL,					 /* initializer */
	NULL,					 /* create per-dir config */
	NULL,					 /* merge per-dir config */
	make_log_mysql_state,	 /* server config */
	NULL,					 /* merge server config */
	log_mysql_cmds,			 /* command table */
	NULL,					 /* handlers */
	NULL,					 /* filename translation */
	NULL,					 /* check_user_id */
	NULL,					 /* check auth */
	NULL,					 /* check access */
	NULL,					 /* type_checker */
	NULL,					 /* fixups */
	log_mysql_transaction,	 /* logger */
	NULL					 /* header parser */
};