From 8ee3532f8b6c454f2600e6c3b47362d22b7cf536 Mon Sep 17 00:00:00 2001 From: Edward Rudd Date: Sat, 10 Apr 2004 04:56:01 +0000 Subject: copied mysql driver to mod_log_sql_pgsql.c to start on PostgreSQL driver added keyword ID expansion to source files and removed old CVS headers from others --- CHANGELOG | 1 - Documentation/README | 2 - INSTALL | 2 - LICENSE | 1 - TODO.in | 2 - apache13.h | 2 +- apache20.h | 2 +- functions.h | 2 +- functions13.h | 2 +- functions20.h | 2 +- mod_log_sql_pgsql.c | 243 +++++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 248 insertions(+), 13 deletions(-) create mode 100644 mod_log_sql_pgsql.c diff --git a/CHANGELOG b/CHANGELOG index fd7dff6..f0540f8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,3 @@ -$Id: CHANGELOG,v 1.18 2004/03/22 20:32:16 urkle Exp $ 1.97: 2004-04-08 * fixed apache.m4 to work with apache 2 setups with different include directories for APR and APU then core Apache diff --git a/Documentation/README b/Documentation/README index e45de11..7ff3241 100644 --- a/Documentation/README +++ b/Documentation/README @@ -1,5 +1,3 @@ -$Id: README,v 1.2 2004/03/05 00:33:50 urkle Exp $ - The "original" document is the Docbook file "manual.xml" -- all other files here are derived from it. diff --git a/INSTALL b/INSTALL index 3cc8a94..033a3ff 100644 --- a/INSTALL +++ b/INSTALL @@ -1,5 +1,3 @@ -$Id: INSTALL,v 1.1 2003/12/20 07:16:05 urkle Exp $ - This document has been superseded by the new documentation in the Documentation/ directory. There you will find the docs in a variety of formats, including PostScript, plaintext, and HTML. diff --git a/LICENSE b/LICENSE index 3057b84..4c98781 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,3 @@ -$Id: LICENSE,v 1.2 2004/02/12 03:44:12 urkle Exp $ Copyright (c) 2004 Edward M. Rudd. All rights reserved. Copyright (c) 2002 Christopher B. Powell. All rights reserved. diff --git a/TODO.in b/TODO.in index c05a8c5..b8bff60 100644 --- a/TODO.in +++ b/TODO.in @@ -1,5 +1,3 @@ -$Id: TODO,v 1.1 2004/02/12 03:21:35 urkle Exp $ - TODO: * Port connection portion to other DBMS? Genericize the module? Start with PostgreSQL. (provider mechanism, and libDBI) diff --git a/apache13.h b/apache13.h index 38a9cae..3b013cc 100644 --- a/apache13.h +++ b/apache13.h @@ -1,4 +1,4 @@ -/* $Header: /home/cvs/mod_log_sql/apache13.h,v 1.6 2004/03/05 00:30:58 urkle Exp $ */ +/* $Id$ */ #ifndef APACHE13_H #define APACHE13_H diff --git a/apache20.h b/apache20.h index 1c6d12c..a20c695 100644 --- a/apache20.h +++ b/apache20.h @@ -1,4 +1,4 @@ -/* $Header: /home/cvs/mod_log_sql/apache20.h,v 1.3 2004/01/21 04:34:21 urkle Exp $ */ +/* $Id$ */ #ifndef APACHE20_H #define APACHE20_H diff --git a/functions.h b/functions.h index 2c06f61..6163b21 100644 --- a/functions.h +++ b/functions.h @@ -1,4 +1,4 @@ -/* $Header: /home/cvs/mod_log_sql/functions.h,v 1.4 2004/03/04 05:43:20 urkle Exp $ */ +/* $Id$ */ /* Begin the individual functions that, given a request r, * extract the needed information from it and return the * value to the calling entity. diff --git a/functions13.h b/functions13.h index 7fe8850..54e3138 100644 --- a/functions13.h +++ b/functions13.h @@ -1,4 +1,4 @@ -/* $Header: /home/cvs/mod_log_sql/functions13.h,v 1.3 2004/02/05 21:59:46 urkle Exp $ */ +/* $Id$ */ static const char *extract_bytes_sent(request_rec *r, char *a) { diff --git a/functions20.h b/functions20.h index 31d9e4b..42fe664 100644 --- a/functions20.h +++ b/functions20.h @@ -1,4 +1,4 @@ -/* $Header: /home/cvs/mod_log_sql/functions20.h,v 1.3 2004/02/05 21:59:46 urkle Exp $ */ +/* $Id$ */ /* functions */ static const char *extract_bytes_sent(request_rec *r, char *a) { diff --git a/mod_log_sql_pgsql.c b/mod_log_sql_pgsql.c new file mode 100644 index 0000000..8fb0f9e --- /dev/null +++ b/mod_log_sql_pgsql.c @@ -0,0 +1,243 @@ +/* $Id$ */ + +#if defined(WITH_APACHE20) +# include "apache20.h" +#elif defined(WITH_APACHE13) +# include "apache13.h" +#else +# error Unsupported Apache version +#endif + +#ifdef HAVE_CONFIG_H +/* Undefine these to prevent conflicts between Apache ap_config_auto.h and + * my config.h. Only really needed for Apache < 2.0.48, but it can't hurt. + */ +#undef PACKAGE_BUGREPORT +#undef PACKAGE_NAME +#undef PACKAGE_STRING +#undef PACKAGE_TARNAME +#undef PACKAGE_VERSION + +#include "config.h" +#endif + +#include "mod_log_sql.h" + +/* Connect to the MYSQL database */ +static logsql_opendb_ret log_sql_pgsql_connect(server_rec *s, logsql_dbconnection *db) +{ + const char *host = apr_table_get(db->parms,"hostname"); + const char *user = apr_table_get(db->parms,"username"); + const char *passwd = apr_table_get(db->parms,"password"); + const char *database = apr_table_get(db->parms,"database"); + const char *s_tcpport = apr_table_get(db->parms,"port"); + unsigned int tcpport = (s_tcpport)?atoi(s_tcpport):3306; + const char *socketfile = apr_table_get(db->parms,"socketfile"); + MYSQL *dblink = db->handle; + + dblink = mysql_init(dblink); + db->handle = (void *)dblink; + + + if (!socketfile) { + socketfile = "/var/lib/mysql/mysql.sock"; + } + + if (mysql_real_connect(dblink, host, user, passwd, database, tcpport, + socketfile, 0)) { + log_error(APLOG_MARK,APLOG_DEBUG,0, s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'", + host, tcpport, database, user, socketfile); + return LOGSQL_OPENDB_SUCCESS; + } else { + log_error(APLOG_MARK,APLOG_DEBUG,0, s,"mod_log_sql: database connection error: %s", + MYSQL_ERROR(dblink)); + log_error(APLOG_MARK,APLOG_DEBUG, 0, s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'", + host, tcpport, database, user, socketfile); + return LOGSQL_OPENDB_FAIL; + } +} + +/* Close the DB link */ +static void log_sql_pgsql_close(logsql_dbconnection *db) +{ + mysql_close((MYSQL *)db->handle); +} + +/* Routine to escape the 'dangerous' characters that would otherwise + * corrupt the INSERT string: ', \, and " + */ +static const char *log_sql_pgsql_escape(const char *from_str, apr_pool_t *p, + logsql_dbconnection *db) +{ + 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 *) apr_palloc(p, length * 2 + 1); + if (!to_str) { + return from_str; + } + + if (!db->connected) { + /* 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 *)db->handle, to_str, from_str, length); + } + + if (retval) + return to_str; + else + return from_str; + } +} + +/* Run a mysql insert query and return a categorized error or success */ +static logsql_query_ret log_sql_pgsql_query(request_rec *r,logsql_dbconnection *db, + const char *query) +{ + int retval; + void (*handler) (int); + unsigned int real_error = 0; + /*const char *real_error_str = NULL;*/ + + MYSQL *dblink = (MYSQL *)db->handle; + + if (!dblink) { + return LOGSQL_QUERY_NOLINK; + } + /* A failed mysql_query() may send a SIGPIPE, so we ignore that signal momentarily. */ + handler = signal(SIGPIPE, SIG_IGN); + + /* Run the query */ + if (!(retval = mysql_query(dblink, query))) { + signal(SIGPIPE, handler); + return LOGSQL_QUERY_SUCCESS; + } + /* Check to see if the error is "nonexistent table" */ + real_error = mysql_errno(dblink); + + if (real_error == ER_NO_SUCH_TABLE) { + log_error(APLOG_MARK,APLOG_ERR,0, r->server,"table does not exist, preserving query"); + /* Restore SIGPIPE to its original handler function */ + signal(SIGPIPE, handler); + return LOGSQL_QUERY_NOTABLE; + } + + /* Restore SIGPIPE to its original handler function */ + signal(SIGPIPE, handler); + return LOGSQL_QUERY_FAIL; +} + +/* Create table table_name of type table_type. */ +static logsql_table_ret log_sql_pgsql_create(request_rec *r, logsql_dbconnection *db, + logsql_tabletype table_type, const char *table_name) +{ + int retval; + const char *tabletype = apr_table_get(db->parms,"tabletype"); + void (*handler) (int); + char *type_suffix = NULL; + + char *create_prefix = "create table if not exists `"; + char *create_suffix = NULL; + char *create_sql; + + MYSQL *dblink = (MYSQL *)db->handle; + +/* if (!global_config.createtables) { + return APR_SUCCESS; + }*/ + + switch (table_type) { + case LOGSQL_TABLE_ACCESS: + create_suffix = + "` (id char(19),\ + agent varchar(255),\ + bytes_sent int unsigned,\ + child_pid smallint unsigned,\ + cookie varchar(255),\ + machine_id varchar(25),\ + 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(10),\ + request_protocol varchar(10),\ + request_time char(28),\ + request_uri varchar(255),\ + request_args varchar(255),\ + 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(255))"; + break; + case LOGSQL_TABLE_COOKIES: + case LOGSQL_TABLE_HEADERSIN: + case LOGSQL_TABLE_HEADERSOUT: + case LOGSQL_TABLE_NOTES: + create_suffix = + "` (id char(19),\ + item varchar(80),\ + val varchar(80))"; + break; + } + + if (tabletype) { + type_suffix = apr_pstrcat(r->pool, " TYPE=", + tabletype, NULL); + } + /* Find memory long enough to hold the whole CREATE string + \0 */ + create_sql = apr_pstrcat(r->pool, create_prefix, table_name, create_suffix, + type_suffix, NULL); + + log_error(APLOG_MARK,APLOG_DEBUG,0, r->server,"create string: %s", create_sql); + + if (!dblink) { + return LOGSQL_QUERY_NOLINK; + } + /* A failed mysql_query() may send a SIGPIPE, so we ignore that signal momentarily. */ + handler = signal(SIGPIPE, SIG_IGN); + + /* Run the create query */ + if ((retval = mysql_query(dblink, create_sql))) { + log_error(APLOG_MARK,APLOG_ERR,0, r->server,"failed to create table: %s", + table_name); + signal(SIGPIPE, handler); + return LOGSQL_TABLE_FAIL; + } + signal(SIGPIPE, handler); + return LOGSQL_TABLE_SUCCESS; +} + +static char *supported_drivers[] = {"pgsql",NULL}; +static logsql_dbdriver pgsql_driver = { + supported_drivers, + log_sql_mysql_connect, /* open DB connection */ + log_sql_mysql_close, /* close DB connection */ + log_sql_mysql_escape, /* escape query */ + log_sql_mysql_query, /* insert query */ + log_sql_mysql_create /* create table */ +}; + +LOGSQL_REGISTER(pgsql) { + log_sql_register_driver(p,&pgsql_driver); + LOGSQL_REGISTER_RETURN; +} -- cgit