Debian Codegraph @

contrib/mod_sql_odbc.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * ProFTPD: mod_sql_odbc -- Support for connecting to databases via ODBC
3 * Copyright (c) 2003-2020 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders gives permission to link this program
20 * with OpenSSL, and distribute the resulting executable, without including
21 * the source code for OpenSSL in the source distribution.
•22 * $Libraries: -lodbc $
23 */
24
25 #include "conf.h"
26 #include "mod_sql.h"
27
28 #define MOD_SQL_ODBC_VERSION "mod_sql_odbc/0.3.4"
29
30 /* Make sure the version of proftpd is as necessary. */
31 #if PROFTPD_VERSION_NUMBER < 0x0001030602
32 # error "ProFTPD 1.3.6rc2 or later required"
33 #endif
34
35 #include "sql.h"
36 #include "sqlext.h"
37
38 module sql_odbc_module;
39
40 typedef struct db_conn_struct {
41 char *dsn;
42 char *user;
43 char *pass;
44
45 HENV envh;
46 HDBC dbh;
47 HSTMT sth;
48
49 unsigned int state;
50
51 } db_conn_t;
52
53 #define SQLODBC_HAVE_ENV_HANDLE 0x0001
54 #define SQLODBC_HAVE_DBC_HANDLE 0x0002
55 #define SQLODBC_HAVE_STMT_HANDLE 0x0004
56 #define SQLODBC_HAVE_INFO 0x0010
57
58 typedef struct conn_entry_struct {
59 char *name;
60 void *data;
61
62 /* Timer handling */
63 int timer;
64 int ttl;
65
66 /* Connection handling */
67 unsigned int nconn;
68
69 } conn_entry_t;
70
71 #define DEF_CONN_POOL_SIZE 10
72
73 static pool *conn_pool = NULL;
74 static array_header *conn_cache = NULL;
75 static int odbc_version = SQL_OV_ODBC3;
76 static const char *odbc_version_str = "ODBCv3";
77
78 /* Default to using the LIMIT clause. Some database drivers prefer
79 * ROWNUM (e.g. Oracle), and others prefer TOP (e.g. TDS).
80 */
81 static int use_limit_clause = TRUE;
82 static int use_rownum_clause = FALSE;
83 static int use_top_clause = FALSE;
84
85 MODRET sqlodbc_close(cmd_rec *);
86
87 static conn_entry_t *sqlodbc_get_conn(char *name) {
88 register unsigned int i = 0;
89
90 if (!name)
91 return NULL;
92
93 for (i = 0; i < conn_cache->nelts; i++) {
94 conn_entry_t *entry = ((conn_entry_t **) conn_cache->elts)[i];
95
96 if (strcmp(name, entry->name) == 0)
97 return entry;
98 }
99
100 return NULL;
101 }
102
103 static void *sqlodbc_add_conn(pool *p, char *name, db_conn_t *conn) {
104 conn_entry_t *entry = NULL;
105
106 if (p == NULL ||
107 name == NULL ||
108 conn == NULL) {
109 errno = EINVAL;
110 return NULL;
111 }
112
113 if (sqlodbc_get_conn(name) != NULL) {
114 errno = EEXIST;
115 return NULL;
116 }
117
118 entry = (conn_entry_t *) pcalloc(p, sizeof(conn_entry_t));
119 entry->name = name;
120 entry->data = conn;
121
122 *((conn_entry_t **) push_array(conn_cache)) = entry;
123 return entry;
124 }
125
126 static int sqlodbc_timer_cb(CALLBACK_FRAME) {
127 register unsigned int i = 0;
128
129 for (i = 0; i < conn_cache->nelts; i++) {
130 conn_entry_t *entry = ((conn_entry_t **) conn_cache->elts)[i];
131
132 if (entry->timer == p2) {
133 cmd_rec *cmd = NULL;
134
135 sql_log(DEBUG_INFO, "timer expired for connection '%s'", entry->name);
136
137 cmd = pr_cmd_alloc(conn_pool, 2, entry->name, "1");
138 sqlodbc_close(cmd);
139 destroy_pool(cmd->pool);
140
141 entry->timer = 0;
142 }
143 }
144
145 return 0;
146 }
147
148 static SQLCHAR odbc_state[6];
149 static SQLCHAR odbc_errstr[SQL_MAX_MESSAGE_LENGTH];
150
151 static const char *sqlodbc_errstr(SQLSMALLINT handle_type, SQLHANDLE handle,
152 SQLCHAR **statep) {
153 SQLSMALLINT odbc_errlen = 0;
154 SQLINTEGER odbc_errno;
155 SQLRETURN res;
156
157 memset(odbc_state, '\0', sizeof(odbc_state));
158 memset(odbc_errstr, '\0', sizeof(odbc_errstr));
159
160 /* Ideally, we'd keep calling SQLGetDiagRec() until it returned SQL_NO_DATA,
161 * in order to capture the entire error message stack.
162 */
163
164 res = SQLGetDiagRec(handle_type, handle, 1, odbc_state, &odbc_errno,
165 odbc_errstr, sizeof(odbc_errstr), &odbc_errlen);
166
167 if (res != SQL_NO_DATA) {
168 if (statep)
169 *statep = odbc_state;
170
171 return (const char *) odbc_errstr;
172 }
173
174 return "(no data)";
175 }
176
177 /* Liberally borrowed from MySQL-3.23.55's libmysql.c file,
178 * mysql_odbc_escape_string() function.
179 */
180 static void sqlodbc_escape_string(char *to, const char *from, size_t fromlen) {
181 const char *end;
182
183 for (end = from + fromlen; from != end; from++) {
184 switch (*from) {
185 case 0:
186 *to++ = '\\';
187 *to++ = '0';
188 break;
189
190 case '\n':
191 *to++ = '\\';
192 *to++ = 'n';
193 break;
194
195 case '\r':
196 *to++ = '\\';
197 *to++ = 'r';
198 break;
199
200 case '\\':
201 *to++ = '\\';
202 *to++ = '\\';
203 break;
204
205 case '\'':
206 *to++ = '\'';
207 *to++ = '\'';
208 break;
209
210 case '"':
211 *to++ = '\\';
212 *to++ = '"';
213 break;
214
215 case '\032':
216 *to++ = '\\';
217 *to++ = 'Z';
218 break;
219
220 default:
221 *to++ = *from;
222 }
223 }
224 }
225
226 static const char *sqlodbc_typestr(SQLSMALLINT type) {
227 switch (type) {
228 case SQL_CHAR:
229 return "SQL_CHAR";
230
231 case SQL_VARCHAR:
232 return "SQL_VARCHAR";
233
234 case SQL_LONGVARCHAR:
235 return "SQL_LONGVARCHAR";
236
237 #ifdef SQL_WCHAR
238 case SQL_WCHAR:
239 return "SQL_WCHAR";
240 #endif
241
242 #ifdef SQL_WVARCHAR
243 case SQL_WVARCHAR:
244 return "SQL_WVARCHAR";
245 #endif
246
247 #ifdef SQL_WLONGVARCHAR
248 case SQL_WLONGVARCHAR:
249 return "SQL_WLONGVARCHAR";
250 #endif
251
252 case SQL_DECIMAL:
253 return "SQL_DECIMAL";
254
255 case SQL_NUMERIC:
256 return "SQL_NUMERIC";
257
258 case SQL_BIT:
259 return "SQL_BIT";
260
261 case SQL_TINYINT:
262 return "SQL_TINYINT";
263
264 case SQL_SMALLINT:
265 return "SQL_SMALLINT";
266
267 case SQL_INTEGER:
268 return "SQL_INTEGER";
269
270 case SQL_BIGINT:
271 return "SQL_BIGINT";
272
273 case SQL_REAL:
274 return "SQL_REAL";
275
276 case SQL_FLOAT:
277 return "SQL_FLOAT";
278
279 case SQL_DOUBLE:
280 return "SQL_DOUBLE";
281
282 case SQL_BINARY:
283 return "SQL_BINARY";
284
285 case SQL_VARBINARY:
286 return "SQL_VARBINARY";
287
288 case SQL_LONGVARBINARY:
289 return "SQL_LONGVARBINARY";
290
291 case SQL_TYPE_DATE:
292 return "SQL_TYPE_DATE";
293
294 case SQL_TYPE_TIME:
295 return "SQL_TYPE_TIME";
296
297 case SQL_TYPE_TIMESTAMP:
298 return "SQL_TYPE_TIMESTAMP";
299
300 case SQL_GUID:
301 return "SQL_GUID";
302 }
303
304 return "[unknown]";
305 }
306
307 static const char *sqlodbc_strerror(SQLSMALLINT odbc_error) {
308 switch (odbc_error) {
309 case SQL_SUCCESS:
310 return "Success";
311
312 case SQL_SUCCESS_WITH_INFO:
313 return "Success with info";
314
315 #ifdef SQL_NO_DATA
316 case SQL_NO_DATA:
317 return "No data";
318 #endif
319
320 case SQL_ERROR:
321 return "Error";
322
323 case SQL_INVALID_HANDLE:
324 return "Invalid handle";
325
326 case SQL_STILL_EXECUTING:
327 return "Still executing";
328
329 case SQL_NEED_DATA:
330 return "Need data";
331 }
332
333 return "(unknown)";
334 }
335
336 static modret_t *sqlodbc_get_error(cmd_rec *cmd, SQLSMALLINT handle_type,
337 SQLHANDLE handle) {
338 SQLCHAR state[6], errstr[SQL_MAX_MESSAGE_LENGTH];
339 SQLSMALLINT errlen;
340 SQLINTEGER odbc_errno = 0;
341 SQLRETURN res;
342 unsigned int recno = 1;
343 char numstr[20];
344
345 memset(errstr, '\0', sizeof(errstr));
346 pr_snprintf((char *) errstr, sizeof(errstr)-1, "%s", "(no data)");
347
348 res = SQLGetDiagRec(handle_type, handle, recno++, state, &odbc_errno,
349 errstr, sizeof(errstr), &errlen);
350 while (res != SQL_NO_DATA
351 #ifdef SQL_ERROR
352 && res != SQL_ERROR
353 #endif /* SQL_ERROR */
354 #ifdef SQL_INVALID_HANDLE
355 && res != SQL_INVALID_HANDLE
356 #endif /* SQL_INVALID_HANDLE */
357 ) {
358 pr_signals_handle();
359
360 sql_log(DEBUG_FUNC, "odbc error: [%d] %s", odbc_errno, errstr);
361
362 res = SQLGetDiagRec(handle_type, handle, recno++, state, &odbc_errno,
363 errstr, sizeof(errstr), &errlen);
364 }
365
366 /* This will return the last error retrieved. This is OK, since we
367 * have logged all the previous errors.
368 */
369 memset(numstr, '\0', sizeof(numstr));
370 pr_snprintf(numstr, 20, "%d", (int) odbc_errno);
371
372 return PR_ERROR_MSG(cmd, numstr, (char *) errstr);
373 }
374
375 static modret_t *sqlodbc_get_data(cmd_rec *cmd, db_conn_t *conn) {
376 sql_data_t *sd = NULL;
377 array_header *dh = NULL;
378 SQLSMALLINT ncols;
379 SQLRETURN res;
380
381 if (!conn) {
382 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
383 }
384
385 sd = (sql_data_t *) pcalloc(cmd->tmp_pool, sizeof(sql_data_t));
386
387 res = SQLNumResultCols(conn->sth, &ncols);
388 if (res != SQL_SUCCESS &&
389 res != SQL_SUCCESS_WITH_INFO) {
390 char *err;
391 SQLCHAR *state = NULL;
392
393 err = (char *) sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth, &state);
394
395 sql_log(DEBUG_WARN, "error getting column count: %s", err);
396 return PR_ERROR_MSG(cmd, state ? (char *) state : "0", err);
397 }
398 sd->fnum = (unsigned long) ncols;
399 sd->rnum = 0;
400
401 dh = (array_header *) make_array(cmd->tmp_pool, sd->fnum, sizeof(char *));
402
403 /* NOTE: this could be optimised, caching the SQLDescribeCol() results
404 * (used mainly to get the column datatype) so that that call is not
405 * needed for subsequent rows, just the first row.
406 */
407 while (TRUE) {
408 int done_fetching = FALSE;
409 register unsigned int i;
410
411 pr_signals_handle();
412
413 res = SQLFetch(conn->sth);
414
415 switch (res) {
416 case SQL_ERROR:
417 sql_log(DEBUG_WARN, "error fetching row %lu: %s", sd->rnum + 1,
418 sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth, NULL));
419 return PR_ERROR(cmd);
420
421 case SQL_NO_DATA:
422 done_fetching = TRUE;
423 break;
424
425 case SQL_SUCCESS_WITH_INFO:
426 /* Note: this deliberately falls through to the SQL_SUCCESS case. */
427 sql_log(DEBUG_WARN, "fetching row %lu: %s", sd->rnum + 1,
428 sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth, NULL));
429
430 case SQL_SUCCESS:
431 sd->rnum++;
432
433 for (i = 1; i <= sd->fnum; i++) {
434 SQLCHAR col_name[80];
435 SQLSMALLINT col_namelen, col_type, col_digits, col_nullable;
436 SQLULEN col_size;
437
438 if (SQLDescribeCol(conn->sth, i, col_name, sizeof(col_name),
439 &col_namelen, &col_type, &col_size, &col_digits,
440 &col_nullable) == SQL_SUCCESS) {
441 SQLSMALLINT col_ctype;
442
443 /* mod_sql expects to handle all returned data elements as strings
444 * (even though it converts some to numbers), so we need to
445 * stringify any numeric datatypes returned.
446 */
447 switch (col_type) {
448 case SQL_CHAR:
449 case SQL_LONGVARCHAR:
450 case SQL_VARCHAR:
451 #ifdef SQL_WVARCHAR
452 case SQL_WVARCHAR:
453 #endif
454 col_ctype = SQL_C_CHAR;
455
456 if (col_size) {
457 SQLLEN buflen;
458 SQLCHAR *buf = pcalloc(cmd->tmp_pool, ++col_size);
459
460 if (SQLGetData(conn->sth, i, col_ctype, buf, col_size,
461 &buflen) != SQL_SUCCESS) {
462 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
463 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
464 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
465 NULL));
466 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
467 continue;
468 }
469
470 if (buflen == SQL_NO_TOTAL) {
471 sql_log(DEBUG_WARN, "notice: unable to determine total "
472 "number of bytes remaining for %s column %u, row %lu",
473 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
474 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
475 break;
476
477 } else if (buflen == SQL_NULL_DATA) {
478 sql_log(DEBUG_WARN, "notice: data is NULL for %s column "
479 "%u, row %lu", sqlodbc_typestr(col_ctype), i,
480 sd->rnum + 1);
481 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
482 break;
483 }
484
485 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool,
486 (char *) buf);
487
488 } else {
489 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
490 }
491
492 break;
493
494 /* Note: ODBC doesn't seem to provide a way of knowing whether
495 * the value in the column is supposed to be signed or unsigned.
496 * Could cause problems later on.
497 */
498
499 case SQL_TINYINT: {
500 char buf[64];
501 short col_cval;
502 SQLLEN ind;
503
504 col_ctype = SQL_C_TINYINT;
505
506 if (SQLGetData(conn->sth, i, col_ctype,
507 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
508 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
509 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
510 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
511 NULL));
512 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
513 break;
514 }
515
516 if (ind == SQL_NO_TOTAL) {
517 sql_log(DEBUG_WARN, "notice: unable to determine total "
518 "number of bytes remaining for %s column %u, row %lu",
519 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
520 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
521 break;
522
523 } else if (ind == SQL_NULL_DATA) {
524 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
525 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
526 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
527 break;
528 }
529
530 pr_snprintf(buf, sizeof(buf), "%hd", col_cval);
531 buf[sizeof(buf)-1] = '\0';
532
533 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
534 break;
535 }
536
537 case SQL_SMALLINT: {
538 char buf[64];
539 short col_cval;
540 SQLLEN ind;
541
542 col_ctype = SQL_C_SHORT;
543
544 if (SQLGetData(conn->sth, i, col_ctype,
545 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
546 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
547 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
548 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
549 NULL));
550 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
551 break;
552 }
553
554 if (ind == SQL_NO_TOTAL) {
555 sql_log(DEBUG_WARN, "notice: unable to determine total "
556 "number of bytes remaining for %s column %u, row %lu",
557 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
558 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
559 break;
560
561 } else if (ind == SQL_NULL_DATA) {
562 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
563 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
564 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
565 break;
566 }
567
568 pr_snprintf(buf, sizeof(buf), "%hd", col_cval);
569 buf[sizeof(buf)-1] = '\0';
570
571 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
572 break;
573 }
574
575 case SQL_INTEGER: {
576 char buf[64];
577 int col_cval;
578 SQLLEN ind;
579
580 col_ctype = SQL_INTEGER;
581
582 if (SQLGetData(conn->sth, i, col_ctype,
583 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
584 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
585 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
586 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
587 NULL));
588 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
589 break;
590 }
591
592 if (ind == SQL_NO_TOTAL) {
593 sql_log(DEBUG_WARN, "notice: unable to determine total "
594 "number of bytes remaining for %s column %u, row %lu",
595 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
596 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
597 break;
598
599 } else if (ind == SQL_NULL_DATA) {
600 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
601 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
602 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
603 break;
604 }
605
606 pr_snprintf(buf, sizeof(buf), "%d", col_cval);
607 buf[sizeof(buf)-1] = '\0';
608
609 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
610 break;
611 }
612
613 case SQL_BIGINT: {
614 char buf[64];
615 long col_cval;
616 SQLLEN ind;
617
618 col_ctype = SQL_C_LONG;
619
620 if (SQLGetData(conn->sth, i, col_ctype,
621 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
622 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
623 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
624 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
625 NULL));
626 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
627 break;
628 }
629
630 if (ind == SQL_NO_TOTAL) {
631 sql_log(DEBUG_WARN, "notice: unable to determine total "
632 "number of bytes remaining for %s column %u, row %lu",
633 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
634 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
635 break;
636
637 } else if (ind == SQL_NULL_DATA) {
638 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
639 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
640 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
641 break;
642 }
643
644 pr_snprintf(buf, sizeof(buf), "%ld", col_cval);
645 buf[sizeof(buf)-1] = '\0';
646
647 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
648 break;
649 }
650
651 case SQL_REAL: {
652 char buf[64];
653 long col_cval;
654 SQLLEN ind;
655
656 col_ctype = SQL_C_LONG;
657
658 if (SQLGetData(conn->sth, i, col_ctype,
659 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
660 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
661 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
662 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
663 NULL));
664 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
665 break;
666 }
667
668 if (ind == SQL_NO_TOTAL) {
669 sql_log(DEBUG_WARN, "notice: unable to determine total "
670 "number of bytes remaining for %s column %u, row %lu",
671 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
672 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
673 break;
674
675 } else if (ind == SQL_NULL_DATA) {
676 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
677 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
678 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
679 break;
680 }
681
682 pr_snprintf(buf, sizeof(buf), "%ld", col_cval);
683 buf[sizeof(buf)-1] = '\0';
684
685 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
686 break;
687 }
688
689 case SQL_FLOAT: {
690 char buf[64];
691 float col_cval;
692 SQLLEN ind;
693
694 col_ctype = SQL_C_FLOAT;
695
696 if (SQLGetData(conn->sth, i, col_ctype,
697 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
698 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
699 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
700 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
701 NULL));
702 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
703 break;
704 }
705
706 if (ind == SQL_NO_TOTAL) {
707 sql_log(DEBUG_WARN, "notice: unable to determine total "
708 "number of bytes remaining for %s column %u, row %lu",
709 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
710 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
711 break;
712
713 } else if (ind == SQL_NULL_DATA) {
714 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
715 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
716 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
717 break;
718 }
719
720 pr_snprintf(buf, sizeof(buf), "%f", col_cval);
721 buf[sizeof(buf)-1] = '\0';
722
723 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
724 break;
725 }
726
727 case SQL_DOUBLE: {
728 char buf[64];
729 double col_cval;
730 SQLLEN ind;
731
732 col_ctype = SQL_C_DOUBLE;
733
734 if (SQLGetData(conn->sth, i, col_ctype,
735 (SQLPOINTER) &col_cval, 0, &ind) != SQL_SUCCESS) {
736 sql_log(DEBUG_WARN, "error getting %s data for column %u, "
737 "row %lu: %s", sqlodbc_typestr(col_ctype), i,
738 sd->rnum + 1, sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth,
739 NULL));
740 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
741 break;
742 }
743
744 if (ind == SQL_NO_TOTAL) {
745 sql_log(DEBUG_WARN, "notice: unable to determine total "
746 "number of bytes remaining for %s column %u, row %lu",
747 sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
748 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
749 break;
750
751 } else if (ind == SQL_NULL_DATA) {
752 sql_log(DEBUG_WARN, "notice: data is NULL for %s column %u, "
753 "row %lu", sqlodbc_typestr(col_ctype), i, sd->rnum + 1);
754 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, "");
755 break;
756 }
757
758 pr_snprintf(buf, sizeof(buf), "%f", col_cval);
759 buf[sizeof(buf)-1] = '\0';
760
761 *((char **) push_array(dh)) = pstrdup(cmd->tmp_pool, buf);
762 break;
763 }
764
765 default:
766 sql_log(DEBUG_WARN, "data type (%s) of column %u not handled",
767 sqlodbc_typestr(col_type), i);
768 }
769
770 } else {
771 sql_log(DEBUG_WARN, "error describing column %u: %s", i,
772 sqlodbc_errstr(SQL_HANDLE_STMT, conn->sth, NULL));
773 }
774 }
775 break;
776
777 default:
778 sql_log(DEBUG_WARN, "SQLFetch returned unhandled result: %d", res);
779 break;
780 }
781
782 if (done_fetching)
783 break;
784 }
785
786 SQLFreeHandle(SQL_HANDLE_STMT, conn->sth);
787 conn->state &= ~SQLODBC_HAVE_STMT_HANDLE;
788
789 /* We allocate an extra char *, for a terminating NULL. */
790 *((char **) push_array(dh)) = NULL;
791 sd->data = (char **) dh->elts;
792
793 return mod_create_data(cmd, (void *) sd);
794 }
795
796 MODRET sqlodbc_open(cmd_rec *cmd) {
797 conn_entry_t *entry = NULL;
798 db_conn_t *conn = NULL;
799 SQLSMALLINT res;
800
801 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_open");
802
803 if (cmd->argc < 1) {
804 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
805 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
806 }
807
808 /* Get the named connection. */
809 entry = sqlodbc_get_conn(cmd->argv[0]);
810 if (entry == NULL) {
811 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
812 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
813 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
814 }
815
816 conn = (db_conn_t *) entry->data;
817
818 /* If we're already open (nconn > 0), increment the number of connections.
819 * Reset our timer if we have one, and return HANDLED.
820 */
821 if (entry->nconn > 0) {
822 entry->nconn++;
823
824 if (entry->timer)
825 pr_timer_reset(entry->timer, &sql_odbc_module);
826
827 sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name,
828 entry->nconn);
829 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
830 return PR_HANDLED(cmd);
831 }
832
833 if (!(conn->state & SQLODBC_HAVE_ENV_HANDLE)) {
834 res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &conn->envh);
835 if (res != SQL_SUCCESS) {
836 sql_log(DEBUG_WARN, "error allocating environment handle: %s",
837 sqlodbc_strerror(res));
838 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
839 return sqlodbc_get_error(cmd, SQL_HANDLE_ENV, conn->envh);
840 }
841
842 res = SQLSetEnvAttr(conn->envh, SQL_ATTR_ODBC_VERSION,
843 (SQLPOINTER) odbc_version, 0);
844 if (res != SQL_SUCCESS) {
845 sql_log(DEBUG_WARN, "error setting SQL_ATTR_ODBC_VERSION %s: %s",
846 odbc_version_str, sqlodbc_strerror(res));
847 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
848 return sqlodbc_get_error(cmd, SQL_HANDLE_ENV, conn->envh);
849 }
850
851 conn->state |= SQLODBC_HAVE_ENV_HANDLE;
852 }
853
854 if (!(conn->state & SQLODBC_HAVE_DBC_HANDLE)) {
855 res = SQLAllocHandle(SQL_HANDLE_DBC, conn->envh, &conn->dbh);
856 if (res != SQL_SUCCESS) {
857 sql_log(DEBUG_WARN, "error allocating database handle: %s",
858 sqlodbc_strerror(res));
859 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
860 return sqlodbc_get_error(cmd, SQL_HANDLE_DBC, conn->dbh);
861 }
862
863 conn->state |= SQLODBC_HAVE_DBC_HANDLE;
864 }
865
866 res = SQLConnect(conn->dbh, (SQLCHAR *) conn->dsn, SQL_NTS,
867 (SQLCHAR *) conn->user, SQL_NTS, (SQLCHAR *) conn->pass, SQL_NTS);
868 if (res != SQL_SUCCESS &&
869 res != SQL_SUCCESS_WITH_INFO) {
870 sql_log(DEBUG_FUNC, "error connecting to dsn '%s': %s", conn->dsn,
871 sqlodbc_strerror(res));
872 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
873 return sqlodbc_get_error(cmd, SQL_HANDLE_DBC, conn->dbh);
874 }
875
876 /* Add some DriverManager and Driver information to the logs via
877 * SQLGetInfo().
878 */
879 if (!(conn->state & SQLODBC_HAVE_INFO)) {
880 SQLCHAR info[SQL_MAX_MESSAGE_LENGTH];
881 SQLSMALLINT infolen;
882
883 res = SQLGetInfo(conn->dbh, SQL_DBMS_NAME, info, sizeof(info), &infolen);
884 if (res == SQL_SUCCESS) {
885 info[infolen] = '\0';
886 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Product name: %s", info);
887
888 /* Oracle does not like use of the LIMIT, and prefers ROWNUM instead.
889 * Thus we need to check to see if this driver is for Oracle.
890 */
891 if (strcasecmp((char *) info, "Oracle") == 0) {
892 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
893 ": %s does not support LIMIT, using ROWNUM instead", info);
894 use_limit_clause = FALSE;
895 use_rownum_clause = TRUE;
896 use_top_clause = FALSE;
897 }
898
899 /* FreeTDS (and SQL Server) does not like either LIMIT or ROWNUM, and
900 * prefers TOP. So we need to check for this as well.
901 */
902 if (strcasecmp((char *) info, "FreeTDS") == 0 ||
903 strstr((char *) info, "SQL Server") != NULL) {
904 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
905 ": %s does not support LIMIT, using TOP instead", info);
906 use_limit_clause = FALSE;
907 use_rownum_clause = FALSE;
908 use_top_clause = TRUE;
909 }
910
911 } else {
912 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
913 ": Product name: (unavailable)");
914 }
915
916 res = SQLGetInfo(conn->dbh, SQL_DBMS_VER, info, sizeof(info), &infolen);
917 if (res == SQL_SUCCESS) {
918 info[infolen] = '\0';
919 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Product version: %s", info);
920
921 } else {
922 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
923 ": Product version: (unavailable)");
924 }
925
926 res = SQLGetInfo(conn->dbh, SQL_DM_VER, info, sizeof(info), &infolen);
927 if (res == SQL_SUCCESS) {
928 info[infolen] = '\0';
929 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Driver Manager version: %s",
930 info);
931
932 } else {
933 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
934 ": Driver Manager version: (unavailable)");
935 }
936
937 res = SQLGetInfo(conn->dbh, SQL_DRIVER_NAME, info, sizeof(info), &infolen);
938 if (res == SQL_SUCCESS) {
939 info[infolen] = '\0';
940 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Driver name: %s", info);
941
942 } else {
943 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Driver name: (unavailable)");
944 }
945
946 res = SQLGetInfo(conn->dbh, SQL_DRIVER_VER, info, sizeof(info), &infolen);
947 if (res == SQL_SUCCESS) {
948 info[infolen] = '\0';
949 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Driver version: %s", info);
950
951 } else {
952 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
953 ": Driver version: (unavailable)");
954 }
955
956 res = SQLGetInfo(conn->dbh, SQL_DRIVER_ODBC_VER, info, sizeof(info),
957 &infolen);
958 if (res == SQL_SUCCESS) {
959 info[infolen] = '\0';
960 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION ": Driver ODBC version: %s",
961 info);
962
963 } else {
964 sql_log(DEBUG_INFO, MOD_SQL_ODBC_VERSION
965 ": Driver ODBC version: (unavailable)");
966 }
967
968 conn->state |= SQLODBC_HAVE_INFO;
969 }
970
971 entry->nconn++;
972
973 if (pr_sql_conn_policy == SQL_CONN_POLICY_PERSESSION) {
974 /* If the connection policy is PERSESSION... */
975 if (entry->nconn == 1) {
976 /* ...and we are actually opening the first connection to the database;
977 * we want to make sure this connection stays open, after this first use
978 * (as per Bug#3290). To do this, we re-bump the connection count.
979 */
980 entry->nconn++;
981 }
982
983 } else if (entry->ttl > 0) {
984 /* Set up our timer, if necessary. */
985
986 entry->timer = pr_timer_add(entry->ttl, -1, &sql_odbc_module,
987 sqlodbc_timer_cb, "odbc connection ttl");
988
989 sql_log(DEBUG_INFO, "'%s' connection: %d second timer started",
990 entry->name, entry->ttl);
991
992 /* Timed connections get re-bumped so they don't go away when
993 * sqlodbc_close() is called.
994 */
995 entry->nconn++;
996 }
997
998 sql_log(DEBUG_INFO, "'%s' connection opened", entry->name);
999 sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name,
1000 entry->nconn);
1001 pr_event_generate("mod_sql.db.connection-opened", &sql_odbc_module);
1002
1003 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_open");
1004 return PR_HANDLED(cmd);
1005 }
1006
1007 MODRET sqlodbc_close(cmd_rec *cmd) {
1008 conn_entry_t *entry = NULL;
1009 db_conn_t *conn = NULL;
1010
1011 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_close");
1012
1013 if (cmd->argc < 1 || cmd->argc > 2) {
1014 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_close");
1015 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1016 }
1017
1018 /* Get the named connection. */
1019 entry = sqlodbc_get_conn(cmd->argv[0]);
1020 if (entry == NULL) {
1021 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_close");
1022 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1023 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
1024 }
1025
1026 conn = (db_conn_t *) entry->data;
1027
1028 /* If we're closed already (nconn == 0), return HANDLED. */
1029 if (entry->nconn == 0) {
1030 sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name,
1031 entry->nconn);
1032 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_close");
1033 return PR_HANDLED(cmd);
1034 }
1035
1036 /* Decrement nconn. If our count is 0 or we received a second arg, close
1037 * the connection, explicitly set the counter to 0, and remove any timers.
1038 */
1039 if ((--entry->nconn) == 0 ||
1040 (cmd->argc == 2 && cmd->argv[1])) {
1041
1042 if (conn->state & SQLODBC_HAVE_STMT_HANDLE) {
1043 SQLFreeHandle(SQL_HANDLE_STMT, conn->sth);
1044 conn->sth = NULL;
1045
1046 conn->state &= ~SQLODBC_HAVE_STMT_HANDLE;
1047 }
1048
1049 if (conn->state & SQLODBC_HAVE_DBC_HANDLE) {
1050 SQLDisconnect(conn->dbh);
1051 SQLFreeHandle(SQL_HANDLE_DBC, conn->dbh);
1052 conn->dbh = NULL;
1053
1054 conn->state &= ~SQLODBC_HAVE_DBC_HANDLE;
1055 }
1056
1057 if (conn->state & SQLODBC_HAVE_ENV_HANDLE) {
1058 SQLFreeHandle(SQL_HANDLE_ENV, conn->envh);
1059 conn->envh = NULL;
1060
1061 conn->state &= ~SQLODBC_HAVE_ENV_HANDLE;
1062 }
1063
1064 entry->nconn = 0;
1065
1066 if (entry->timer) {
1067 pr_timer_remove(entry->timer, &sql_odbc_module);
1068 entry->timer = 0;
1069 sql_log(DEBUG_INFO, "'%s' connection timer stopped", entry->name);
1070 }
1071
1072 sql_log(DEBUG_INFO, "'%s' connection closed", entry->name);
1073 pr_event_generate("mod_sql.db.connection-closed", &sql_odbc_module);
1074 }
1075
1076 sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name,
1077 entry->nconn);
1078 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_close");
1079
1080 return PR_HANDLED(cmd);
1081 }
1082
1083 MODRET sqlodbc_def_conn(cmd_rec *cmd) {
1084 char *name = NULL;
1085 conn_entry_t *entry = NULL;
1086 db_conn_t *conn = NULL;
1087
1088 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_defineconnection");
1089
1090 if (cmd->argc < 4 ||
1091 cmd->argc > 10 ||
1092 !cmd->argv[0]) {
1093 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_defineconnection");
1094 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1095 }
1096
1097 conn = (db_conn_t *) pcalloc(conn_pool, sizeof(db_conn_t));
1098
1099 name = pstrdup(conn_pool, cmd->argv[0]);
1100 conn->user = pstrdup(conn_pool, cmd->argv[1]);
1101 conn->pass = pstrdup(conn_pool, cmd->argv[2]);
1102 conn->dsn = pstrdup(conn_pool, cmd->argv[3]);
1103
1104 /* Insert the new conn_info into the connection hash */
1105 entry = sqlodbc_add_conn(conn_pool, name, (void *) conn);
1106 if (entry == NULL &&
1107 errno == EEXIST) {
1108 /* Log only connections named other than "default", for debugging
1109 * misconfigurations with multiple different SQLNamedConnectInfo
1110 * directives using the same name.
1111 */
1112 if (strcmp(name, "default") != 0) {
1113 sql_log(DEBUG_FUNC, "named connection '%s' already exists", name);
1114 }
1115
1116 entry = sqlodbc_get_conn(name);
1117 }
1118
1119 if (entry == NULL) {
1120 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_defineconnection");
1121 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1122 "error adding named connection");
1123 }
1124
1125 if (cmd->argc >= 5) {
1126 entry->ttl = (int) strtol(cmd->argv[4], (char **) NULL, 10);
1127 if (entry->ttl >= 1) {
1128 pr_sql_conn_policy = SQL_CONN_POLICY_TIMER;
1129
1130 } else {
1131 entry->ttl = 0;
1132 }
1133 }
1134
1135 entry->timer = 0;
1136 entry->nconn = 0;
1137
1138 sql_log(DEBUG_INFO, " name: '%s'", entry->name);
1139 sql_log(DEBUG_INFO, " user: '%s'", conn->user);
1140 sql_log(DEBUG_INFO, " dsn: '%s'", conn->dsn);
1141
1142 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_defineconnection");
1143 return PR_HANDLED(cmd);
1144 }
1145
1146 MODRET sqlodbc_select(cmd_rec *cmd) {
1147 conn_entry_t *entry = NULL;
1148 db_conn_t *conn = NULL;
1149 modret_t *mr = NULL;
1150 char *query = NULL;
1151 cmd_rec *close_cmd;
1152
1153 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_select");
1154
1155 if (cmd->argc < 2) {
1156 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1157 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1158 }
1159
1160 /* Get the named connection. */
1161 entry = sqlodbc_get_conn(cmd->argv[0]);
1162 if (entry == NULL) {
1163 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1164 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1165 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
1166 }
1167
1168 conn = (db_conn_t *) entry->data;
1169
1170 mr = sqlodbc_open(cmd);
1171 if (MODRET_ERROR(mr)) {
1172 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1173 return mr;
1174 }
1175
1176 /* Construct the query string. */
1177 if (cmd->argc == 2) {
1178 query = pstrcat(cmd->tmp_pool, "SELECT ", cmd->argv[1], NULL);
1179
1180 } else {
1181 query = pstrcat(cmd->tmp_pool, cmd->argv[2], " FROM ", cmd->argv[1], NULL);
1182
1183 if (cmd->argc > 3 &&
1184 cmd->argv[3])
1185 query = pstrcat(cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL);
1186
1187 if (cmd->argc > 4 &&
1188 cmd->argv[4]) {
1189
1190 if (use_limit_clause) {
1191 query = pstrcat(cmd->tmp_pool, query, " LIMIT ", cmd->argv[4], NULL);
1192
1193 } else if (use_rownum_clause) {
1194 query = pstrcat(cmd->tmp_pool, query, " AND ROWNUM = ", cmd->argv[4],
1195 NULL);
1196
1197 } else if (use_top_clause) {
1198 /* Note that the TOP clause must be at the start of the query, not
1199 * the end.
1200 */
1201 query = pstrcat(cmd->tmp_pool, "TOP ", cmd->argv[4], " ", query, NULL);
1202 }
1203 }
1204
1205 if (cmd->argc > 5) {
1206 register unsigned int i = 0;
1207
1208 /* Handle the optional arguments -- they're rare, so in this case
1209 * we'll play with the already constructed query string, but in
1210 * general we should probably take optional arguments into account
1211 * and put the query string together later once we know what they are.
1212 */
1213
1214 for (i = 5; i < cmd->argc; i++) {
1215 if (cmd->argv[i] &&
1216 strcasecmp("DISTINCT", cmd->argv[i]) == 0) {
1217 query = pstrcat(cmd->tmp_pool, "DISTINCT ", query, NULL);
1218 }
1219 }
1220 }
1221
1222 query = pstrcat(cmd->tmp_pool, "SELECT ", query, NULL);
1223 }
1224
1225 /* Log the query string */
1226 sql_log(DEBUG_INFO, "query \"%s\"", query);
1227
1228 if (!(conn->state & SQLODBC_HAVE_STMT_HANDLE)) {
1229 if (SQLAllocHandle(SQL_HANDLE_STMT, conn->dbh, &conn->sth) !=
1230 SQL_SUCCESS) {
1231 sql_log(DEBUG_WARN, "%s", "error allocating statement handle");
1232 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1233 return sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1234 }
1235
1236 conn->state |= SQLODBC_HAVE_STMT_HANDLE;
1237 }
1238
1239 /* Perform the query. If it doesn't work, log the error, close the
1240 * connection, then return the error from the query processing.
1241 */
1242 switch (SQLPrepare(conn->sth, (SQLCHAR *) query, strlen(query))) {
1243 case SQL_SUCCESS:
1244 case SQL_SUCCESS_WITH_INFO:
1245 break;
1246
1247 default:
1248 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1249
1250 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1251 sqlodbc_close(close_cmd);
1252 destroy_pool(close_cmd->pool);
1253
1254 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1255 return mr;
1256 }
1257
1258 switch (SQLExecute(conn->sth)) {
1259 case SQL_SUCCESS:
1260 case SQL_SUCCESS_WITH_INFO:
1261 break;
1262
1263 default:
1264 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1265
1266 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1267 sqlodbc_close(close_cmd);
1268 destroy_pool(close_cmd->pool);
1269
1270 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1271 return mr;
1272 }
1273
1274 /* Get the data. If it doesn't work, log the error, close the connection,
1275 * then return the error from the data processing.
1276 */
1277
1278 mr = sqlodbc_get_data(cmd, conn);
1279 if (MODRET_ERROR(mr)) {
1280 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1281 sqlodbc_close(close_cmd);
1282 destroy_pool(close_cmd->pool);
1283
1284 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1285 return mr;
1286 }
1287
1288 /* Close the connection, return the data. */
1289 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1290 sqlodbc_close(close_cmd);
1291 destroy_pool(close_cmd->pool);
1292
1293 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_select");
1294 return mr;
1295 }
1296
1297 MODRET sqlodbc_insert(cmd_rec *cmd) {
1298 conn_entry_t *entry = NULL;
1299 db_conn_t *conn = NULL;
1300 modret_t *mr = NULL;
1301 char *query = NULL;
1302 cmd_rec *close_cmd;
1303
1304 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_insert");
1305
1306 if (cmd->argc != 2 && cmd->argc != 4) {
1307 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1308 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1309 }
1310
1311 /* Get the named connection. */
1312 entry = sqlodbc_get_conn(cmd->argv[0]);
1313 if (entry == NULL) {
1314 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1315 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1316 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
1317 }
1318
1319 conn = (db_conn_t *) entry->data;
1320
1321 mr = sqlodbc_open(cmd);
1322 if (MODRET_ERROR(mr)) {
1323 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1324 return mr;
1325 }
1326
1327 /* Construct the query string */
1328 if (cmd->argc == 2)
1329 query = pstrcat(cmd->tmp_pool, "INSERT ", cmd->argv[1], NULL);
1330
1331 else
1332 query = pstrcat(cmd->tmp_pool, "INSERT INTO ", cmd->argv[1], " (",
1333 cmd->argv[2], ") VALUES (", cmd->argv[3], ")", NULL);
1334
1335 /* Log the query string */
1336 sql_log(DEBUG_INFO, "query \"%s\"", query);
1337
1338 if (!(conn->state & SQLODBC_HAVE_STMT_HANDLE)) {
1339 if (SQLAllocHandle(SQL_HANDLE_STMT, conn->dbh, &conn->sth) !=
1340 SQL_SUCCESS) {
1341 sql_log(DEBUG_WARN, "%s", "error allocating statement handle");
1342 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1343 return sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1344 }
1345
1346 conn->state |= SQLODBC_HAVE_STMT_HANDLE;
1347 }
1348
1349 /* Perform the query. If it doesn't work, log the error, close the
1350 * connection (and log any errors there, too) then return the error
1351 * from the query processing.
1352 */
1353 if (SQLPrepare(conn->sth, (SQLCHAR *) query, strlen(query)) != SQL_SUCCESS) {
1354 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1355
1356 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1357 sqlodbc_close(close_cmd);
1358 destroy_pool(close_cmd->pool);
1359
1360 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1361 return mr;
1362 }
1363
1364 switch (SQLExecute(conn->sth)) {
1365 case SQL_SUCCESS:
1366 case SQL_SUCCESS_WITH_INFO:
1367 break;
1368
1369 default:
1370 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1371
1372 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1373 sqlodbc_close(close_cmd);
1374 destroy_pool(close_cmd->pool);
1375
1376 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1377 return mr;
1378 }
1379
1380 /* Close the connection and return HANDLED. */
1381 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1382 sqlodbc_close(close_cmd);
1383 destroy_pool(close_cmd->pool);
1384
1385 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_insert");
1386 return PR_HANDLED(cmd);
1387 }
1388
1389 MODRET sqlodbc_update(cmd_rec *cmd) {
1390 conn_entry_t *entry = NULL;
1391 db_conn_t *conn = NULL;
1392 modret_t *mr = NULL;
1393 char *query = NULL;
1394 cmd_rec *close_cmd;
1395
1396 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_update");
1397
1398 if (cmd->argc < 2 || cmd->argc > 4) {
1399 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1400 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1401 }
1402
1403 /* Get the named connection. */
1404 entry = sqlodbc_get_conn(cmd->argv[0]);
1405 if (entry == NULL) {
1406 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1407 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1408 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
1409 }
1410
1411 conn = (db_conn_t *) entry->data;
1412
1413 mr = sqlodbc_open(cmd);
1414 if (MODRET_ERROR(mr)) {
1415 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1416 return mr;
1417 }
1418
1419 if (cmd->argc == 2) {
1420 query = pstrcat(cmd->tmp_pool, "UPDATE ", cmd->argv[1], NULL);
1421
1422 } else {
1423 /* Construct the query string */
1424 query = pstrcat(cmd->tmp_pool, "UPDATE ", cmd->argv[1], " SET ",
1425 cmd->argv[2], NULL);
1426
1427 if (cmd->argc > 3 && cmd->argv[3])
1428 query = pstrcat(cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL);
1429 }
1430
1431 /* Log the query string. */
1432 sql_log(DEBUG_INFO, "query \"%s\"", query);
1433
1434 if (!(conn->state & SQLODBC_HAVE_STMT_HANDLE)) {
1435 if (SQLAllocHandle(SQL_HANDLE_STMT, conn->dbh, &conn->sth) !=
1436 SQL_SUCCESS) {
1437 sql_log(DEBUG_WARN, "%s", "error allocating statement handle");
1438 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1439 return sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1440 }
1441
1442 conn->state |= SQLODBC_HAVE_STMT_HANDLE;
1443 }
1444
1445 /* Perform the query. If it doesn't work close the connection, then
1446 * return the error from the query processing.
1447 */
1448 if (SQLPrepare(conn->sth, (SQLCHAR *) query, strlen(query)) != SQL_SUCCESS) {
1449 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1450
1451 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1452 sqlodbc_close(close_cmd);
1453 destroy_pool(close_cmd->pool);
1454
1455 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1456 return mr;
1457 }
1458
1459 switch (SQLExecute(conn->sth)) {
1460 case SQL_SUCCESS:
1461 case SQL_SUCCESS_WITH_INFO:
1462 break;
1463
1464 default:
1465 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1466
1467 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1468 sqlodbc_close(close_cmd);
1469 destroy_pool(close_cmd->pool);
1470
1471 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1472 return mr;
1473 }
1474
1475 /* Close the connection, return HANDLED. */
1476 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1477 sqlodbc_close(close_cmd);
1478 destroy_pool(close_cmd->pool);
1479
1480 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_update");
1481 return PR_HANDLED(cmd);
1482 }
1483
1484 MODRET sqlodbc_procedure(cmd_rec *cmd) {
1485 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_procedure");
1486
1487 if (cmd->argc != 3) {
1488 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_procedure");
1489 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1490 }
1491
1492 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_procedure");
1493 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1494 "backend does not support procedures");
1495 }
1496
1497 MODRET sqlodbc_query(cmd_rec *cmd) {
1498 conn_entry_t *entry = NULL;
1499 db_conn_t *conn = NULL;
1500 modret_t *mr = NULL;
1501 char *query = NULL;
1502 cmd_rec *close_cmd;
1503
1504 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_query");
1505
1506 if (cmd->argc != 2) {
1507 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1508 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1509 }
1510
1511 /* Get the named connection. */
1512 entry = sqlodbc_get_conn(cmd->argv[0]);
1513 if (entry == NULL) {
1514 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1515 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1516 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
1517 }
1518
1519 conn = (db_conn_t *) entry->data;
1520
1521 mr = sqlodbc_open(cmd);
1522 if (MODRET_ERROR(mr)) {
1523 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1524 return mr;
1525 }
1526
1527 query = pstrcat(cmd->tmp_pool, cmd->argv[1], NULL);
1528
1529 /* Log the query string */
1530 sql_log(DEBUG_INFO, "query \"%s\"", query);
1531
1532 if (!(conn->state & SQLODBC_HAVE_STMT_HANDLE)) {
1533 if (SQLAllocHandle(SQL_HANDLE_STMT, conn->dbh, &conn->sth) !=
1534 SQL_SUCCESS) {
1535 sql_log(DEBUG_WARN, "%s", "error allocating statement handle");
1536 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1537 return sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1538 }
1539
1540 conn->state |= SQLODBC_HAVE_STMT_HANDLE;
1541 }
1542
1543 /* Perform the query. If it doesn't work close the connection, then
1544 * return the error from the query processing.
1545 */
1546 if (SQLPrepare(conn->sth, (SQLCHAR *) query, strlen(query)) != SQL_SUCCESS) {
1547 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1548
1549 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1550 sqlodbc_close(close_cmd);
1551 destroy_pool(close_cmd->pool);
1552
1553 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1554 return mr;
1555 }
1556
1557 switch (SQLExecute(conn->sth)) {
1558 case SQL_SUCCESS:
1559 case SQL_SUCCESS_WITH_INFO:
1560 break;
1561
1562 default:
1563 mr = sqlodbc_get_error(cmd, SQL_HANDLE_STMT, conn->sth);
1564
1565 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1566 sqlodbc_close(close_cmd);
1567 destroy_pool(close_cmd->pool);
1568
1569 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1570 return mr;
1571 }
1572
1573 /* Get data if necessary. If it doesn't work, log the error, close the
1574 * connection then return the error from the data processing.
1575 */
1576
1577 mr = sqlodbc_get_data(cmd, conn);
1578 if (MODRET_ERROR(mr)) {
1579 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1580
1581 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1582 sqlodbc_close(close_cmd);
1583 destroy_pool(close_cmd->pool);
1584
1585 return mr;
1586 }
1587
1588 /* Close the connection, return the data. */
1589 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1590 sqlodbc_close(close_cmd);
1591 destroy_pool(close_cmd->pool);
1592
1593 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_query");
1594 return mr;
1595 }
1596
1597 /* XXX need to provide an escapestring implementation, probably borrowed
1598 * from MySQL. Make sure it's standards-compliant.
1599 */
1600
1601 MODRET sqlodbc_quote(cmd_rec *cmd) {
1602 conn_entry_t *entry = NULL;
1603 modret_t *mr = NULL;
1604 char *unescaped = NULL;
1605 char *escaped = NULL;
1606 cmd_rec *close_cmd;
1607
1608 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_escapestring");
1609
1610 if (cmd->argc != 2) {
1611 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_escapestring");
1612 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION, "badly formed request");
1613 }
1614
1615 /* Get the named connection. */
1616 entry = sqlodbc_get_conn(cmd->argv[0]);
1617 if (entry == NULL) {
1618 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_escapestring");
1619 return PR_ERROR_MSG(cmd, MOD_SQL_ODBC_VERSION,
1620 pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL));
1621 }
1622
1623 /* Make sure the connection is open. */
1624 mr = sqlodbc_open(cmd);
1625 if (MODRET_ERROR(mr)) {
1626 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_escapestring");
1627 return mr;
1628 }
1629
1630 unescaped = cmd->argv[1];
1631 escaped = (char *) pcalloc(cmd->tmp_pool, sizeof(char) *
1632 (strlen(unescaped) * 2) + 1);
1633
1634 sqlodbc_escape_string(escaped, unescaped, strlen(unescaped));
1635
1636 close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name);
1637 sqlodbc_close(close_cmd);
1638 destroy_pool(close_cmd->pool);
1639
1640 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_escapestring");
1641 return mod_create_data(cmd, (void *) escaped);
1642 }
1643
1644 MODRET sqlodbc_exit(cmd_rec *cmd) {
1645 register unsigned int i = 0;
1646
1647 sql_log(DEBUG_FUNC, "%s", "entering \todbc cmd_exit");
1648
1649 for (i = 0; i < conn_cache->nelts; i++) {
1650 conn_entry_t *entry = ((conn_entry_t **) conn_cache->elts)[i];
1651
1652 if (entry->nconn > 0) {
1653 cmd_rec *close_cmd = pr_cmd_alloc(conn_pool, 2, entry->name, "1");
1654 sqlodbc_close(close_cmd);
1655 destroy_pool(close_cmd->pool);
1656 }
1657 }
1658
1659 sql_log(DEBUG_FUNC, "%s", "exiting \todbc cmd_exit");
1660 return PR_HANDLED(cmd);
1661 }
1662
1663 MODRET sqlodbc_identify(cmd_rec *cmd) {
1664 sql_data_t *sd = NULL;
1665
1666 sd = (sql_data_t *) pcalloc(cmd->tmp_pool, sizeof(sql_data_t));
1667 sd->data = (char **) pcalloc(cmd->tmp_pool, sizeof(char *) * 2);
1668
1669 sd->rnum = 1;
1670 sd->fnum = 2;
1671
1672 sd->data[0] = MOD_SQL_ODBC_VERSION;
1673 sd->data[1] = MOD_SQL_API_V1;
1674
1675 return mod_create_data(cmd, (void *) sd);
1676 }
1677
1678 /* mod_sql-specific command dispatch/handler table */
1679 static cmdtable sqlodbc_cmdtable[] = {
1680 { CMD, "sql_open", G_NONE, sqlodbc_open, FALSE, FALSE },
1681 { CMD, "sql_close", G_NONE, sqlodbc_close, FALSE, FALSE },
1682 { CMD, "sql_defineconnection",G_NONE, sqlodbc_def_conn, FALSE, FALSE },
1683 { CMD, "sql_select", G_NONE, sqlodbc_select, FALSE, FALSE },
1684 { CMD, "sql_insert", G_NONE, sqlodbc_insert, FALSE, FALSE },
1685 { CMD, "sql_update", G_NONE, sqlodbc_update, FALSE, FALSE },
1686 { CMD, "sql_procedure", G_NONE, sqlodbc_procedure, FALSE, FALSE },
1687 { CMD, "sql_query", G_NONE, sqlodbc_query, FALSE, FALSE },
1688 { CMD, "sql_escapestring", G_NONE, sqlodbc_quote, FALSE, FALSE },
1689 { CMD, "sql_exit", G_NONE, sqlodbc_exit, FALSE, FALSE },
1690 { CMD, "sql_identify", G_NONE, sqlodbc_identify, FALSE, FALSE },
1691
1692 { 0, NULL }
1693 };
1694
1695 /* Configuration handlers
1696 */
1697
1698 /* usage: SQLODBCVersion version */
1699 MODRET set_sqlodbcversion(cmd_rec *cmd) {
1700 int version = -1;
1701 const char *version_str;
1702 config_rec *c;
1703
1704 CHECK_ARGS(cmd, 1);
1705 CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL);
1706
1707 if (strcasecmp(cmd->argv[1], "2") == 0 ||
1708 strcasecmp(cmd->argv[1], "odbcv2") == 0) {
1709 #if defined(SQL_OV_ODBC2)
1710 version = SQL_OV_ODBC2;
1711 version_str = "ODBCv2";
1712 #endif /* ODBCv2 */
1713
1714 } else if (strcasecmp(cmd->argv[1], "3") == 0 ||
1715 strcasecmp(cmd->argv[1], "odbcv3") == 0) {
1716 #if defined(SQL_OV_ODBC3)
1717 version = SQL_OV_ODBC3;
1718 version_str = "ODBCv3";
1719 #endif /* ODBCv3 */
1720
1721 } else if (strcasecmp(cmd->argv[1], "3.80") == 0 ||
1722 strcasecmp(cmd->argv[1], "odbcv3.80") == 0) {
1723 #if defined(SQL_OV_ODBC3_80)
1724 version = SQL_OV_ODBC3_80;
1725 version_str = "ODBCv3.80";
1726 #endif /* ODBCv3.80 */
1727 }
1728
1729 if (version < 0) {
1730 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1731 "unknown/supported ODBC API version: ", cmd->argv[1], NULL));
1732 }
1733
1734 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
1735 c->argv[0] = palloc(c->pool, sizeof(int));
1736 *((int *) c->argv[0]) = version;
1737 c->argv[1] = pstrdup(c->pool, version_str);
1738
1739 return PR_HANDLED(cmd);
1740 }
1741
1742 /* Event handlers
1743 */
1744
1745 static void sqlodbc_mod_unload_ev(const void *event_data, void *user_data) {
1746
1747 if (strcmp("mod_sql_odbc.c", (const char *) event_data) == 0) {
1748 /* Unegister ourselves with mod_sql. */
1749 if (sql_unregister_backend("odbc") < 0) {
1750 pr_log_pri(PR_LOG_NOTICE, MOD_SQL_ODBC_VERSION
1751 ": notice: error unregistering backend: %s", strerror(errno));
1752 pr_session_end(0);
1753 }
1754
1755 /* Unregister ourselves from all events. */
1756 pr_event_unregister(&sql_odbc_module, NULL, NULL);
1757 }
1758 }
1759
1760 /* Module initialization routines
1761 */
1762
1763 static int sqlodbc_init(void) {
1764
1765 /* Register ourselves with mod_sql. */
1766 if (sql_register_backend("odbc", sqlodbc_cmdtable) < 0) {
1767 pr_log_pri(PR_LOG_NOTICE, MOD_SQL_ODBC_VERSION
1768 ": notice: error registering backend: %s", strerror(errno));
1769 errno = EPERM;
1770 return -1;
1771 }
1772
1773 /* Register listeners for the unload event. */
1774 pr_event_register(&sql_odbc_module, "core.module-unload",
1775 sqlodbc_mod_unload_ev, NULL);
1776
1777 return 0;
1778 }
1779
1780 static int sqlodbc_sess_init(void) {
1781 config_rec *c;
1782
1783 if (conn_pool != NULL) {
1784 destroy_pool(conn_pool);
1785 conn_cache = NULL;
1786 }
1787
1788 conn_pool = make_sub_pool(session.pool);
1789 pr_pool_tag(conn_pool, "ODBC connection pool");
1790
1791 if (conn_cache == NULL) {
1792 conn_cache = make_array(conn_pool, DEF_CONN_POOL_SIZE,
1793 sizeof(conn_entry_t *));
1794 }
1795
1796 /* There is a very specific reason for using square brackets, rather than
1797 * parentheses, here.
1798 *
1799 * Users of the mod_sql_odbc module for talking to an Oracle database
1800 * via ODBC encountered a bug in the Oracle client library, where the Oracle
1801 * client library tries to find the name of the process calling the client,
1802 * and adds that name to the connection string used. However, that process
1803 * name parsing will fail if the process name uses parentheses. The
1804 * workaround, then, is to use square brackets.
1805 *
1806 * For those curious, this is Oracle Bug 3807408. More discussions on
1807 * problem can be found at:
1808 *
1809 * http://forums.oracle.com/forums/thread.jspa?threadID=296725
1810 *
1811 * The following comment indicates that more recent versions of Oracle
1812 * have this issue fixed; there is also a patch available:
1813 *
1814 * "This issue is Oracle bug 3807408 and can be fixed by applying 10.2.0.1
1815 * Patch 6. This can be downloaded from MetaLink if you have an account:
1816 *
1817 * http://updates.oracle.com/ARULink/PatchDetails/process_form?patch_num=5059238"
1818 * This comment was found at:
1819 *
1820 * http://www.topxml.com/forum/Database_Adapter_for_Oracle_on_64bit_Windows/m_3448/tm.htm
1821 */
1822
1823 pr_proctitle_set("[accepting connections]");
1824
1825 c = find_config(main_server->conf, CONF_PARAM, "SQLODBCVersion", FALSE);
1826 if (c != NULL) {
1827 odbc_version = *((int *) c->argv[0]);
1828 odbc_version_str = c->argv[1];
1829 }
1830
1831 return 0;
1832 }
1833
1834 /* Module API tables
1835 */
1836
1837 static conftable sqlodbc_conftab[] = {
1838 { "SQLODBCVersion", set_sqlodbcversion, NULL },
1839 { NULL, NULL, NULL }
1840 };
1841
1842 module sql_odbc_module = {
1843 NULL, NULL,
1844
1845 /* Module API version */
1846 0x20,
1847
1848 /* Module name */
1849 "sql_odbc",
1850
1851 /* Module configuration directive table */
1852 sqlodbc_conftab,
1853
1854 /* Module command handler table */
1855 NULL,
1856
1857 /* Module authentication handler table */
1858 NULL,
1859
1860 /* Module initialization */
1861 sqlodbc_init,
1862
1863 /* Session initialization */
1864 sqlodbc_sess_init,
1865
1866 /* Module version */
1867 MOD_SQL_ODBC_VERSION
1868 };