Debian Codegraph @

contrib/mod_sql_odbc.c

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