summaryrefslogtreecommitdiffstats
path: root/mod_log_sql_pgsql.c
diff options
context:
space:
mode:
Diffstat (limited to 'mod_log_sql_pgsql.c')
-rw-r--r--mod_log_sql_pgsql.c251
1 files changed, 0 insertions, 251 deletions
diff --git a/mod_log_sql_pgsql.c b/mod_log_sql_pgsql.c
deleted file mode 100644
index 7ac4053..0000000
--- a/mod_log_sql_pgsql.c
+++ /dev/null
@@ -1,251 +0,0 @@
1/* $Id$ */
2
3#if defined(WITH_APACHE20)
4# include "apache20.h"
5#elif defined(WITH_APACHE13)
6# include "apache13.h"
7#else
8# error Unsupported Apache version
9#endif
10
11
12#ifdef HAVE_CONFIG_H
13/* Undefine these to prevent conflicts between Apache ap_config_auto.h and
14 * my config.h. Only really needed for Apache < 2.0.48, but it can't hurt.
15 */
16#undef PACKAGE_BUGREPORT
17#undef PACKAGE_NAME
18#undef PACKAGE_STRING
19#undef PACKAGE_TARNAME
20#undef PACKAGE_VERSION
21
22#include "config.h"
23#endif
24
25#include "mod_log_sql.h"
26
27#include "libpq-fe.h"
28
29typedef struct {
30 PGconn *PG;
31 char *connectioninfo;
32} pg_conn_rec;
33
34/* Connect to the MYSQL database */
35static logsql_opendb_ret log_sql_pgsql_connect(server_rec *s, logsql_dbconnection *db)
36{
37 const char *host = apr_table_get(db->parms,"hostname");
38 const char *user = apr_table_get(db->parms,"username");
39 const char *passwd = apr_table_get(db->parms,"password");
40 const char *database = apr_table_get(db->parms,"database");
41 const char *s_tcpport = apr_table_get(db->parms,"port");
42 unsigned int tcpport = (s_tcpport)?atoi(s_tcpport):3306;
43 const char *socketfile = apr_table_get(db->parms,"socketfile");
44 pg_conn_rec *dblink = db->handle;
45
46 dblink = mysql_init(dblink);
47 db->handle = (void *)dblink;
48
49
50 if (!socketfile) {
51 socketfile = "/var/lib/mysql/mysql.sock";
52 }
53
54 if (PQconnectdb(dblink, host, user, passwd, database, tcpport,
55 socketfile, 0)) {
56 log_error(APLOG_MARK,APLOG_DEBUG,0, s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'",
57 host, tcpport, database, user, socketfile);
58 return LOGSQL_OPENDB_SUCCESS;
59 } else {
60 log_error(APLOG_MARK,APLOG_DEBUG,0, s,"mod_log_sql: database connection error: %s",
61 MYSQL_ERROR(dblink));
62 log_error(APLOG_MARK,APLOG_DEBUG, 0, s,"HOST: '%s' PORT: '%d' DB: '%s' USER: '%s' SOCKET: '%s'",
63 host, tcpport, database, user, socketfile);
64 return LOGSQL_OPENDB_FAIL;
65 }
66}
67
68/* Close the DB link */
69static void log_sql_pgsql_close(logsql_dbconnection *db)
70{
71 PQfinish(((pg_conn_rec *)db->handle)->PG);
72}
73
74/* Routine to escape the 'dangerous' characters that would otherwise
75 * corrupt the INSERT string: ', \, and "
76 */
77static const char *log_sql_pgsql_escape(const char *from_str, apr_pool_t *p,
78 logsql_dbconnection *db)
79{
80 if (!from_str)
81 return NULL;
82 else {
83 char *to_str;
84 unsigned long length = strlen(from_str);
85 unsigned long retval;
86
87 /* Pre-allocate a new string that could hold twice the original, which would only
88 * happen if the whole original string was 'dangerous' characters.
89 */
90 to_str = (char *) apr_palloc(p, length * 2 + 1);
91 if (!to_str) {
92 return from_str;
93 }
94
95 if (!db->connected) {
96 /* Well, I would have liked to use the current database charset. mysql is
97 * unavailable, however, so I fall back to the slightly less respectful
98 * mysql_escape_string() function that uses the default charset.
99 */
100 retval = mysql_escape_string(to_str, from_str, length);
101 } else {
102 /* MySQL is available, so I'll go ahead and respect the current charset when
103 * I perform the escape.
104 */
105 retval = mysql_real_escape_string((MYSQL *)db->handle, to_str, from_str, length);
106 }
107
108 if (retval)
109 return to_str;
110 else
111 return from_str;
112 }
113}
114
115/* Run a mysql insert query and return a categorized error or success */
116static logsql_query_ret log_sql_pgsql_query(request_rec *r,logsql_dbconnection *db,
117 const char *query)
118{
119 int retval;
120 void (*handler) (int);
121 unsigned int real_error = 0;
122 /*const char *real_error_str = NULL;*/
123
124 pg_conn_rec *dblink = db->handle;
125
126 if (!dblink) {
127 return LOGSQL_QUERY_NOLINK;
128 }
129 /* A failed mysql_query() may send a SIGPIPE, so we ignore that signal momentarily. */
130 handler = signal(SIGPIPE, SIG_IGN);
131
132 /* Run the query */
133 if (!(retval = mysql_query(dblink, query))) {
134 signal(SIGPIPE, handler);
135 return LOGSQL_QUERY_SUCCESS;
136 }
137 /* Check to see if the error is "nonexistent table" */
138 real_error = mysql_errno(dblink);
139
140 if (real_error == ER_NO_SUCH_TABLE) {
141 log_error(APLOG_MARK,APLOG_ERR,0, r->server,"table does not exist, preserving query");
142 /* Restore SIGPIPE to its original handler function */
143 signal(SIGPIPE, handler);
144 return LOGSQL_QUERY_NOTABLE;
145 }
146
147 /* Restore SIGPIPE to its original handler function */
148 signal(SIGPIPE, handler);
149 return LOGSQL_QUERY_FAIL;
150}
151
152/* Create table table_name of type table_type. */
153static logsql_table_ret log_sql_pgsql_create(request_rec *r, logsql_dbconnection *db,
154 logsql_tabletype table_type, const char *table_name)
155{
156 int retval;
157 const char *tabletype = apr_table_get(db->parms,"tabletype");
158 void (*handler) (int);
159 char *type_suffix = NULL;
160
161 char *create_prefix = "create table if not exists `";
162 char *create_suffix = NULL;
163 char *create_sql;
164
165 pg_conn_rec *dblink = db->handle;
166
167/* if (!global_config.createtables) {
168 return APR_SUCCESS;
169 }*/
170
171 switch (table_type) {
172 case LOGSQL_TABLE_ACCESS:
173 create_suffix =
174 "` (id char(19),\
175 agent varchar(255),\
176 bytes_sent int unsigned,\
177 child_pid smallint unsigned,\
178 cookie varchar(255),\
179 machine_id varchar(25),\
180 request_file varchar(255),\
181 referer varchar(255),\
182 remote_host varchar(50),\
183 remote_logname varchar(50),\
184 remote_user varchar(50),\
185 request_duration smallint unsigned,\
186 request_line varchar(255),\
187 request_method varchar(10),\
188 request_protocol varchar(10),\
189 request_time char(28),\
190 request_uri varchar(255),\
191 request_args varchar(255),\
192 server_port smallint unsigned,\
193 ssl_cipher varchar(25),\
194 ssl_keysize smallint unsigned,\
195 ssl_maxkeysize smallint unsigned,\
196 status smallint unsigned,\
197 time_stamp int unsigned,\
198 virtual_host varchar(255))";
199 break;
200 case LOGSQL_TABLE_COOKIES:
201 case LOGSQL_TABLE_HEADERSIN:
202 case LOGSQL_TABLE_HEADERSOUT:
203 case LOGSQL_TABLE_NOTES:
204 create_suffix =
205 "` (id char(19),\
206 item varchar(80),\
207 val varchar(80))";
208 break;
209 }
210
211 if (tabletype) {
212 type_suffix = apr_pstrcat(r->pool, " TYPE=",
213 tabletype, NULL);
214 }
215 /* Find memory long enough to hold the whole CREATE string + \0 */
216 create_sql = apr_pstrcat(r->pool, create_prefix, table_name, create_suffix,
217 type_suffix, NULL);
218
219 log_error(APLOG_MARK,APLOG_DEBUG,0, r->server,"create string: %s", create_sql);
220
221 if (!dblink) {
222 return LOGSQL_QUERY_NOLINK;
223 }
224 /* A failed mysql_query() may send a SIGPIPE, so we ignore that signal momentarily. */
225 handler = signal(SIGPIPE, SIG_IGN);
226
227 /* Run the create query */
228 if ((retval = mysql_query(dblink, create_sql))) {
229 log_error(APLOG_MARK,APLOG_ERR,0, r->server,"failed to create table: %s",
230 table_name);
231 signal(SIGPIPE, handler);
232 return LOGSQL_TABLE_FAIL;
233 }
234 signal(SIGPIPE, handler);
235 return LOGSQL_TABLE_SUCCESS;
236}
237
238static char *supported_drivers[] = {"pgsql",NULL};
239static logsql_dbdriver pgsql_driver = {
240 supported_drivers,
241 log_sql_pgsql_connect, /* open DB connection */
242 log_sql_pgsql_close, /* close DB connection */
243 log_sql_pgsql_escape, /* escape query */
244 log_sql_pgsql_query, /* insert query */
245 log_sql_pgsql_create /* create table */
246};
247
248LOGSQL_REGISTER(pgsql) {
249 log_sql_register_driver(p,&pgsql_driver);
250 LOGSQL_REGISTER_RETURN;
251}