-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_sql_tds.c
1377 lines (1158 loc) · 41.1 KB
/
mod_sql_tds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ProFTPD: mod_tds -- Support for connecting to TDS databases.
* Microsoft SQL Server / Sybase ASE
*
* Copyright (c) 2001-2010 Patrick Muldoon
* From code borrowed from mod_sql_mysql
* Copyright (c) 2001 Andrew Houghton
* Copyright (c) 2004-2005 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
* and other respective copyright holders give permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
/*
* $Libraries: -lsybdb $
*/
/* INTRO:
*
* mod_sql_tds is a mod_sql module for ProFTPD that allow data to be
* stored in database's that speak the TDS protocol (MS SQLServer, Sybase)
*
* Updated versions can always be found at http://labratsoftware.com/mod_sql_tds/
*
* COMMENTS / QUESTIONS / BUGS(not like there will be any bugs right?):
*
* Can be sent to me Patrick Muldoon([email protected]).
*
* Thanks to Noah Roberts From Reachone for his patch correcting an error in cmd_select
*
*/
/*
* Internal define used for debug and logging.
*/
#define MOD_SQL_TDS_VERSION "mod_sql_tds/4.14"
#include <sybfront.h>
#include <sybdb.h>
#include <syberror.h>
#include "conf.h"
#include "../contrib/mod_sql.h"
/*
* timer-handling code adds the need for a couple of forward declarations
*/
MODRET cmd_close( cmd_rec *cmd );
module sql_tds_module;
#define ARBITRARY_MAX 256;
/*
* db_conn_struct:
*/
struct db_conn_struct {
char *server; /* server name from INTERFACE file */
char *user; /* User to access the server */
char *pass; /* Password */
char *db; /* What Database Are we using */
DBPROCESS *dbproc; /* Our connection to the DB */
};
typedef struct db_conn_struct db_conn_t;
struct rowdata{
char *value;
char *name;
};
typedef struct rowdata rowdata_t;
struct tempdata{
char **data;
struct tempdata *next;
};
typedef struct tempdata tempdata_t;
struct conn_entry_struct {
char *name;
void *data;
/* timer handling */
int timer;
int ttl;
/* connection handling */
unsigned int connections;
};
typedef struct conn_entry_struct conn_entry_t;
#define DEF_CONN_POOL_SIZE 10
static array_header *conn_cache;
static pool *conn_pool;
/*
* _sql_get_connection: walks the connection cache looking for the named
* connection. Returns NULL if unsuccessful, a pointer to the conn_entry_t
* if successful.
*/
static conn_entry_t *_sql_get_connection(char *name){
conn_entry_t *entry = NULL;
int cnt;
if (name == NULL) return NULL;
/* walk the array looking for our entry */
for (cnt=0; cnt < conn_cache->nelts; cnt++) {
entry = ((conn_entry_t **) conn_cache->elts)[cnt];
if (!strcmp(name, entry->name)) {
return entry;
}
}
return NULL;
}
/*
* _sql_add_connection: internal helper function to maintain a cache of
* connections. Since we expect the number of named connections to
* be small, simply use an array header to hold them. We don't allow
* duplicate connection names.
*
* Returns: NULL if the insertion was unsuccessful, a pointer to the
* conn_entry_t that was created if successful.
*/
static void *_sql_add_connection(pool *p, char *name, db_conn_t *conn){
conn_entry_t *entry = NULL;
if ((!name) || (!conn) || (!p)) return NULL;
if (_sql_get_connection(name)) {
/* duplicated name */
return NULL;
}
entry = (conn_entry_t *) pcalloc( p, sizeof( conn_entry_t ));
entry->name = name;
entry->data = conn;
*((conn_entry_t **) push_array(conn_cache)) = entry;
return entry;
}
/* _sql_check_cmd: tests to make sure the cmd_rec is valid and is
* properly filled in. If not, it's grounds for the daemon to
* shutdown.
*/
static void _sql_check_cmd(cmd_rec *cmd, char *msg){
if ((!cmd) || (!cmd->tmp_pool)) {
pr_log_pri(PR_LOG_ERR, MOD_SQL_TDS_VERSION ": '%s' was passed an invalid cmd_rec. Shutting down.", msg);
sql_log(DEBUG_WARN, "'%s' was passed an invalid cmd_rec. Shutting down.", msg);
end_login(1);
}
return;
}
/*
* _sql_timer_callback: when a timer goes off, this is the function
* that gets called. This function makes assumptions about the
* db_conn_t members.
*/
static int _sql_timer_callback(CALLBACK_FRAME){
conn_entry_t *entry = NULL;
int cnt = 0;
cmd_rec *cmd = NULL;
for (cnt=0; cnt < conn_cache->nelts; cnt++) {
entry = ((conn_entry_t **) conn_cache->elts)[cnt];
if (entry->timer == p2) {
sql_log(DEBUG_INFO, "%s", " timer expired for connection '%s'",
entry->name);
cmd = _sql_make_cmd( conn_pool, 2, entry->name, "1" );
cmd_close( cmd );
SQL_FREE_CMD( cmd );
entry->timer = 0;
}
}
return 0;
}
/*
* _build_error: constructs a modret_t filled with error information;
*/
static modret_t *_build_error( cmd_rec *cmd, db_conn_t *conn ){
char num[20] = {'\0'};
snprintf(num, 20, "%u", 1234);
if (!conn){
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
return PR_ERROR_MSG(cmd, num, "An Internal Error Occured");
}
/*
* _build_data: both cmd_select and cmd_procedure potentially
* return data to mod_sql; this function builds a modret to return
* that data.
* Once we get here, we have rows to return, do it here
*/
static modret_t *_build_data( cmd_rec *cmd, db_conn_t *conn ){
sql_data_t *sd = NULL;
char **data = NULL;
unsigned long cnt = 0;
unsigned long index = 0;
BYTE **row = NULL;
tempdata_t *td, *ptr;
int x, rcount = 0;
sql_log(DEBUG_FUNC, "%s", " >>> tds _build_data");
if (!conn){
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
/* create a sql_data structure to eventually hold results */
sd = (sql_data_t *) pcalloc(cmd->tmp_pool, sizeof(sql_data_t));
sd->fnum = (unsigned long) dbnumcols(conn->dbproc); /* Number of columns in the result */
sql_log(DEBUG_INFO, "%d columns in the result ", sd->fnum);
/*create datastructure to hold the results */
row = (BYTE **) pcalloc(cmd->tmp_pool, sizeof(BYTE *) * sd->fnum);
/* setup the temp storage to hold all of our data here */
td = (tempdata_t *)pcalloc(cmd->tmp_pool, sizeof(tempdata_t));
/* make sure td points to the top of the list */
ptr = td;
ptr->data = pcalloc(cmd->tmp_pool,(sizeof(char *) * sd->fnum));
ptr->next = NULL;
/* need to bind the columns for our results */
for (x=0;x<sd->fnum;x++){
row[x] = (BYTE *)pcalloc(cmd->tmp_pool, 256);
dbbind(conn->dbproc, x+1, STRINGBIND, (DBINT) 0, row[x]);
}
/* return the rows from the query and populate temp list */
while(dbnextrow(conn->dbproc) != NO_MORE_ROWS){
if(rcount > 0){
ptr->next = (tempdata_t *) pcalloc(cmd->tmp_pool, sizeof(tempdata_t));
ptr = ptr->next;
ptr->data = pcalloc(cmd->tmp_pool,(sizeof(char *) * sd->fnum));
ptr->next = NULL;
sql_log(DEBUG_INFO, "%s", " Created a new temp record");
}
/* add onto our temp record */
for(x=0;x<sd->fnum;x++){
ptr->data[x] = pstrdup(cmd->tmp_pool, (char*)row[x]);
}
rcount++; /* done with this row -- inc to the next */
}
sd->rnum = rcount;
cnt = sd->rnum * sd->fnum;
data = (char **) pcalloc( cmd->tmp_pool, sizeof(char *) * (cnt + 1) );
/* reset list ptr */
ptr = td;
while (ptr != NULL){
for(x=0;x<sd->fnum;x++){
data[index++] = pstrdup(cmd->tmp_pool, ptr->data[x]);
sql_log(DEBUG_INFO, "copied %s to data[%d]",ptr->data[x], (index-1));
}
ptr = ptr->next;
}
data[index]=NULL;
sd->data = data;
return mod_create_data( cmd, (void *) sd );
}
/*
* cmd_open: attempts to open a named connection to the database.
*
* Inputs:
* cmd->argv[0]: connection name
*
* Returns:
* either a properly filled error modret_t if a connection could not be
* opened, or a simple non-error modret_t.
*
*/
MODRET cmd_open(cmd_rec *cmd){
conn_entry_t *entry = NULL;
db_conn_t *conn = NULL;
LOGINREC *login;
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_open");
_sql_check_cmd(cmd, "cmd_open" );
if (cmd->argc < 1) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_open with argc < 1");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
/* get the named connection */
if (!(entry = _sql_get_connection( cmd->argv[0]))) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_open");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "Unknown Named Connection");
}
conn = (db_conn_t *) entry->data;
/* if we're already open (connections > 0) increment connections
* reset our timer if we have one, and return HANDLED
*/
if (entry->connections > 0){
/*FIXME: add check to see if connection still around*/
entry->connections++;
if (entry->timer) {
pr_timer_reset( entry->timer, &sql_tds_module );
}
sql_log(DEBUG_INFO, "connection '%s' count is now %d", entry->name, entry->connections);
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_open");
return PR_HANDLED(cmd);
}
if(dbinit() == FAIL){
pr_log_pri(PR_LOG_ERR, MOD_SQL_TDS_VERSION ": failed to init database. Shutting down.");
sql_log(DEBUG_WARN, "%s", " failed to init tds database! Shutting down.");
end_login(1);
}
sql_log(DEBUG_FUNC, "%s", "Attempting to call dblogin ");
login = dblogin();
DBSETLPWD(login,conn->pass);
DBSETLAPP(login,"proftpd");
DBSETLUSER(login,conn->user);
sql_log(DEBUG_FUNC, "Adding user %s and password %s to login", conn->user,conn->pass);
sql_log(DEBUG_FUNC, "%s", "calling dbopen");
#ifdef PR_USE_NLS
/* We actually need to set the Char encoding before we open the connection
* according to
* http://manuals.sybase.com:80/onlinebooks/group-cnarc/cng1110e/dblib/@Generic__BookTextView/37135;pt=37135#X
* The Client picks a default char and the server does conversions between the local and server sets.
* This should override any char set, that was set in your interfaces file
*/
if (pr_encode_get_encoding() != NULL){
DBSETLCHARSET(login,pr_encode_get_charset());
sql_log(DEBUG_FUNC,"Setting Client Character Set to '%s'",pr_encode_get_charset());
}
#endif /* !PR_USE_NLS */
conn->dbproc = dbopen(login,conn->server);
//free the login rec.
dbloginfree(login);
sql_log(DEBUG_FUNC, "%s", "freeing our loginrec");
if(!conn->dbproc){
pr_log_pri(PR_LOG_ERR, MOD_SQL_TDS_VERSION ": failed to Login to DB server! Shutting down.");
sql_log(DEBUG_WARN, "%s", " failed to Login to DB server. Shutting down.");
end_login(1);
}
sql_log(DEBUG_FUNC, "attempting to switch to database: %s", conn->db);
if(dbuse(conn->dbproc, conn->db) == FAIL){
pr_log_pri(PR_LOG_ERR, MOD_SQL_TDS_VERSION ": failed to use database Shutting down.");
sql_log(DEBUG_WARN, "%s", " failed to use database Shutting down.");
end_login(1);
}
/* bump connections */
entry->connections++;
if(pr_sql_conn_policy == SQL_CONN_POLICY_PERSESSION){
if (entry->connections == 1){
/*if this is our first connection, bump again to make sure the connection
* stays open after first use, see bug #3290)
*/
entry->connections++;
}
}else if (entry->ttl > 0) {
/* set up our timer if necessary */
entry->timer = pr_timer_add(entry->ttl, -1,
&sql_tds_module,
_sql_timer_callback,
"TDS Connection ttl");
sql_log(DEBUG_INFO, "connection '%s' - %d second timer started",
entry->name, entry->ttl);
/* timed connections get re-bumped so they don't go away when cmd_close
* is called.
*/
entry->connections++;
}
/* return HANDLED */
sql_log(DEBUG_INFO, "connection '%s' opened count is now %d", entry->name, entry->connections);
sql_log(DEBUG_FUNC, "%s", " <<< tds cmd_open");
return PR_HANDLED(cmd);
}
/*
* cmd_close: attempts to close the named connection.
*
* Inputs:
* cmd->argv[0]: connection name
* Optional:
* cmd->argv[1]: close immediately
*
* Returns:
* either a properly filled error modret_t if a connection could not be
* closed, or a simple non-error modret_t. For the case of mod_sql_mysql,
* there are no error codes returned by the close call; other backends
* may be able to return a useful error message.
*
* If argv[1] exists and is not NULL, the connection should be immediately
* closed and the connection count should be reset to 0.
*/
MODRET cmd_close(cmd_rec *cmd){
conn_entry_t *entry = NULL;
db_conn_t *conn = NULL;
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_close");
_sql_check_cmd(cmd, "cmd_close");
if ((cmd->argc < 1) || (cmd->argc > 2)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_close --badly formed request");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
/* get the named connection */
if (!(entry = _sql_get_connection( cmd->argv[0] ))) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_close -- unknown named connection");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "Unknown Named Connection");
}
conn = (db_conn_t *) entry->data;
/* if we're closed already (connections == 0) return HANDLED */
if (entry->connections == 0) {
sql_log(DEBUG_INFO, "connection '%s' count is now %d", entry->name, entry->connections);
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_close - connections = 0");
return PR_HANDLED(cmd);
}
/* decrement connections. If our count is 0 or we received a second arg
* close the connection, explicitly set the counter to 0, and remove any
* timers.
*/
if (((--entry->connections) == 0 ) || ((cmd->argc == 2) && (cmd->argv[1]))) {
/* need to close connection here */
dbclose(conn->dbproc);
conn->dbproc = NULL;
entry->connections = 0;
if (entry->timer) {
pr_timer_remove( entry->timer, &sql_tds_module );
entry->timer = 0;
sql_log(DEBUG_INFO, "connection '%s' - timer stopped",
entry->name );
}
sql_log(DEBUG_INFO, "connection '%s' closed", entry->name);
}
sql_log(DEBUG_INFO, "connection '%s' count is now %d", entry->name, entry->connections);
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_close");
return PR_HANDLED(cmd);
}
/*
* cmd_defineconnection: takes all information about a database
* connection and stores it for later use.
*
* Inputs:
* cmd->argv[0]: connection name
* cmd->argv[1]: username portion of the SQLConnectInfo directive
* cmd->argv[2]: password portion of the SQLConnectInfo directive
* cmd->argv[3]: info portion of the SQLConnectInfo directive
* Optional:
* cmd->argv[4]: time-to-live in seconds
*
* Returns:
* either a properly filled error modret_t if the connection could not
* defined, or a simple non-error modret_t.
*
* Notes:
* time-to-live is the length of time to allow a connection to remain unused;
* once that amount of time has passed, a connection should be closed and
* it's connection count should be reduced to 0. If ttl is 0, or ttl is not
* a number or ttl is negative, the connection will be assumed to have no
* associated timer.
*/
MODRET cmd_defineconnection(cmd_rec *cmd){
char *info = NULL;
char *name = NULL;
char *db = NULL;
char *server = NULL;
char *haveserver = NULL;
conn_entry_t *entry = NULL;
db_conn_t *conn = NULL;
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_defineconnection");
_sql_check_cmd(cmd, "cmd_defineconnection");
if ((cmd->argc < 4) || (cmd->argc > 5) || (!cmd->argv[0])) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_defineconnection - invalid argv count");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
if (!conn_pool) {
pr_log_pri(PR_LOG_WARNING, "warning: the mod_sql_tds module has not been "
"properly initialized. Please make sure your --with-modules configure "
"option lists mod_sql *before* mod_sql_tds, and recompile.");
sql_log(DEBUG_FUNC, "%s", "The mod_sql_tds module has not been properly "
"initialized. Please make sure your --with-modules configure option "
"lists mod_sql *before* mod_sql_tds, and recompile.");
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_defineconnection");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "uninitialized module");
}
conn = (db_conn_t *) palloc(conn_pool, sizeof(db_conn_t));
name = pstrdup(conn_pool, cmd->argv[0]);
conn->user = pstrdup(conn_pool, cmd->argv[1]);
conn->pass = pstrdup(conn_pool, cmd->argv[2]);
info = cmd->argv[3];
db = pstrdup(cmd->tmp_pool, info);
haveserver = strchr(db, '@');
if (haveserver) {
server = haveserver + 1;
*haveserver = '\0';
} else {
/* Didn't specify a server, they could have it set in DSQUERY, so lets check there */
sql_log(DEBUG_WARN, "%s", "No Host Specified! \t Checking Enviroment Variable");
server = getenv("DSQUERY");
if(server == NULL){
pr_log_pri(PR_LOG_ERR, "%s", "NO Host Specified and DSQUERY Enviroment Variable NOT Found! "
"-- Shutting down!.");
sql_log(DEBUG_WARN, "%s", "NO Host Specified and DSQUERY Enviroment Variable NOT Found! "
"-- Shutting down!.");
end_login(1); /* is this the right command to use here? */
}
}
conn->server = pstrdup(conn_pool, server);
conn->db = pstrdup(conn_pool, db);
/* insert the new conn_info into the connection hash */
if (!(entry = _sql_add_connection(conn_pool, name, (void *) conn))) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_defineconnection");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "Named Connection Already Exists");
}
entry->ttl = (cmd->argc == 5) ?
(int) strtol(cmd->argv[4], (char **)NULL, 10) : 0;
if (entry->ttl < 0)
entry->ttl = 0;
entry->timer = 0;
entry->connections = 0;
sql_log(DEBUG_INFO, " name: '%s'", entry->name);
sql_log(DEBUG_INFO, " user: '%s'", conn->user);
sql_log(DEBUG_INFO, " server: '%s'", conn->server);
sql_log(DEBUG_INFO, " db: '%s'", conn->db);
sql_log(DEBUG_INFO, " ttl: '%d'", entry->ttl);
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_defineconnection");
return PR_HANDLED(cmd);
}
/*
* cmd_exit: walks the connection cache and closes every
* open connection, resetting their connection counts to 0.
*/
static modret_t *cmd_exit(cmd_rec *cmd) {
register unsigned int cnt = 0;
sql_log(DEBUG_FUNC,"%s",">>> tds cmd_exit");
conn_entry_t *entry = NULL;
for (cnt=0; cnt < conn_cache->nelts; cnt++) {
entry = ((conn_entry_t **) conn_cache->elts)[cnt];
if (entry->connections > 0) {
cmd = _sql_make_cmd( conn_pool, 2, entry->name, "1" );
cmd_close( cmd );
SQL_FREE_CMD( cmd );
}
}
dbexit(); /* magic cleanup routine will clean up any remaining dbprocess that we might have missed */
sql_log(DEBUG_FUNC,"%s","<<< tds cmd_exit");
return PR_HANDLED(cmd);
}
/*
* cmd_select: executes a SELECT query. properly constructing the query
* based on the inputs. See mod_sql.h for the definition of the _sql_data
* structure which is used to return the result data.
*
* cmd_select takes either exactly two inputs, or more than two. If only
* two inputs are given, the second is a monolithic query string. See
* the examples below.
*
* Inputs:
* cmd->argv[0]: connection name
* cmd->argv[1]: table
* cmd->argv[2]: select string
* Optional:
* cmd->argv[3]: where clause
* cmd->argv[4]: requested number of return rows (LIMIT)
*
* etc. : other options, such as "GROUP BY", "ORDER BY",
* and "DISTINCT" will start at cmd->arg[5]. All
* backends MUST support 'DISTINCT', the other
* arguments are optional (but encouraged).
*
* Returns:
* either a properly filled error modret_t if the select failed, or a
* modret_t with the result data filled in.
*
*
* argv[] = "default","user","userid, count", "userid='aah'","2"
* query = "SELECT TOP 2 userid, count FROM user WHERE userid='aah'"
*
* argv[] = "default","usr1, usr2","usr1.foo, usr2.bar"
* query = "SELECT usr1.foo, usr2.bar FROM usr1, usr2"
*
* argv[] = "default","usr1","foo",,,"DISTINCT"
* query = "SELECT DISTINCT foo FROM usr1"
*
* argv[] = "default","bar FROM usr1 WHERE tmp=1 ORDER BY bar"
* query = "SELECT bar FROM usr1 WHERE tmp=1 ORDER BY bar"
*
*/
MODRET cmd_select(cmd_rec *cmd){
conn_entry_t *entry = NULL;
db_conn_t *conn = NULL;
modret_t *cmr = NULL;
modret_t *dmr = NULL;
char *query = NULL;
int cnt = 0;
cmd_rec *close_cmd;
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_select");
_sql_check_cmd(cmd, "cmd_select");
if (cmd->argc < 2) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select - Argc < 2");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
/* get the named connection */
entry = _sql_get_connection( cmd->argv[0] );
if (!entry) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select - Failed to get Entry");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "Unknown Named Connection");
}
conn = (db_conn_t *) entry->data;
cmr = cmd_open(cmd);
if (MODRET_ERROR(cmr)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select - error in cmd_open");
return cmr;
}
/* construct the query string */
if (cmd->argc == 2) {
query = pstrcat(cmd->tmp_pool, "SELECT ", cmd->argv[1], NULL);
} else {
query = pstrcat( cmd->tmp_pool, cmd->argv[2], " FROM ",
cmd->argv[1], NULL );
if ((cmd->argc > 3) && (cmd->argv[3]))
query = pstrcat( cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL );
if ((cmd->argc > 4) && (cmd->argv[4]))
query = pstrcat( cmd->tmp_pool, "TOP ", cmd->argv[4], " ", query, NULL );
if (cmd->argc > 5) {
/* handle the optional arguments -- they're rare, so in this case
* we'll play with the already constructed query string, but in
* general we should probably take optional arguments into account
* and put the query string together later once we know what they are.
*/
for (cnt=5; cnt < cmd->argc; cnt++) {
if ((cmd->argv[cnt]) && !strcasecmp("DISTINCT",cmd->argv[cnt])) {
query = pstrcat( cmd->tmp_pool, "DISTINCT ", query, NULL);
}
}
}
query = pstrcat( cmd->tmp_pool, "SELECT ", query, NULL);
}
/* log the query string */
sql_log( DEBUG_INFO, "query \"%s\"", query);
/* perform the query. if it doesn't work, log the error, close the
* connection then return the error from the query processing.
*/
dbcmd(conn->dbproc, query);
if(dbsqlexec(conn->dbproc) != SUCCEED){
dmr = _build_error( cmd, conn );
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select DBSQLEXEC != SUCCEED");
return dmr;
}
if(dbresults(conn->dbproc) == FAIL){
dmr = _build_error( cmd, conn );
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select DBRESULTS == FAIL");
return dmr;
}
/* get the data. if it doesn't work, log the error, close the
* connection then return the error from the data processing.
*/
dmr = _build_data( cmd, conn );
if (MODRET_ERROR(dmr)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select");
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
return dmr;
}
/* close the connection, return the data. */
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_select (normal)");
return dmr;
}
/*
* cmd_insert: executes an INSERT query, properly constructing the query
* based on the inputs.
*
* cmd_insert takes either exactly two inputs, or exactly four. If only
* two inputs are given, the second is a monolithic query string. See
* the examples below.
*
* Inputs:
* cmd->argv[0]: connection name
* cmd->argv[1]: table
* cmd->argv[2]: field string
* cmd->argv[3]: value string
*
* Returns:
* either a properly filled error modret_t if the insert failed, or a
* simple non-error modret_t.
*
* Example:
* These are example queries that would be executed for MySQL; other
* backends will have different SQL syntax.
*
* argv[] = "default","log","userid, date, count", "'aah', now(), 2"
* query = "INSERT INTO log (userid, date, count) VALUES ('aah', now(), 2)"
*
* argv[] = "default"," INTO foo VALUES ('do','re','mi','fa')"
* query = "INSERT INTO foo VALUES ('do','re','mi','fa')"
*
* Notes:
* none
*/
MODRET cmd_insert(cmd_rec *cmd){
conn_entry_t *entry = NULL;
db_conn_t *conn = NULL;
modret_t *cmr = NULL;
modret_t *dmr = NULL;
char *query = NULL;
cmd_rec *close_cmd;
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_insert");
_sql_check_cmd(cmd, "cmd_insert");
if ((cmd->argc != 2) && (cmd->argc != 4)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_insert");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request");
}
/* get the named connection */
entry = _sql_get_connection( cmd->argv[0] );
if (!entry) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_insert");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "Unknown named connection");
}
conn = (db_conn_t *) entry->data;
cmr = cmd_open(cmd);
if (MODRET_ERROR(cmr)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_insert");
return cmr;
}
/* construct the query string */
if (cmd->argc == 2) {
query = pstrcat(cmd->tmp_pool, "INSERT ", cmd->argv[1], NULL);
} else {
query = pstrcat( cmd->tmp_pool, "INSERT INTO ", cmd->argv[1], " (",
cmd->argv[2], ") VALUES (", cmd->argv[3], ")",
NULL );
}
/* log the query string */
sql_log( DEBUG_INFO, "query \"%s\"", query);
/* perform the query. if it doesn't work, log the error, close the
* connection (and log any errors there, too) then return the error
* from the query processing.
*/
dbcmd(conn->dbproc, query);
dbsqlexec(conn->dbproc);
if(dbresults(conn->dbproc) != SUCCEED){
dmr = _build_error( cmd, conn );
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_insert");
return dmr;
}
/* close the connection and return HANDLED. */
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_insert");
return PR_HANDLED(cmd);
}
/*
* cmd_update: executes an UPDATE query, properly constructing the query
* based on the inputs.
*
* cmd_update takes either exactly two, three, or four inputs. If only
* two inputs are given, the second is a monolithic query string. See
* the examples below.
*
* Inputs:
* cmd->argv[0]: connection name
* cmd->argv[1]: table
* cmd->argv[2]: update string
* Optional:
* cmd->argv[3]: where string
*
* Returns:
* either a properly filled error modret_t if the update failed, or a
* simple non-error modret_t. *
*
*/
MODRET cmd_update(cmd_rec *cmd){
conn_entry_t *entry = NULL;
db_conn_t *conn = NULL;
modret_t *cmr = NULL;
modret_t *dmr = NULL;
char *query = NULL;
cmd_rec *close_cmd;
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_update");
_sql_check_cmd(cmd, "cmd_update");
if ((cmd->argc < 2) || (cmd->argc > 4)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_update");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "badly formed request"); }
/* get the named connection */
entry = _sql_get_connection( cmd->argv[0] );
if (!entry) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_update");
return PR_ERROR_MSG(cmd, MOD_SQL_TDS_VERSION, "Unknown Named Connection");
}
conn = (db_conn_t *) entry->data;
cmr = cmd_open(cmd);
if (MODRET_ERROR(cmr)) {
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_update");
return cmr;
}
if (cmd->argc == 2) {
query = pstrcat(cmd->tmp_pool, "UPDATE ", cmd->argv[1], NULL);
} else {
/* construct the query string */
query = pstrcat( cmd->tmp_pool, "UPDATE ", cmd->argv[1], " SET ",
cmd->argv[2], NULL );
if ((cmd->argc > 3) && (cmd->argv[3])) {
query = pstrcat( cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL );
}
}
/* log the query string */
sql_log( DEBUG_INFO, "query \"%s\"", query);
/* perform the query. if it doesn't work close the connection, then
* return the error from the query processing.
*/
dbcmd(conn->dbproc, query);
dbsqlexec(conn->dbproc);
if(dbresults(conn->dbproc) != SUCCEED){
dmr = _build_error( cmd, conn );
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_update");
return dmr;
}
/* close the connection, return HANDLED. */
close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
cmd_close(close_cmd);
SQL_FREE_CMD( close_cmd );
sql_log(DEBUG_FUNC, "%s", "<<< tds cmd_update");
return PR_HANDLED(cmd);
}
/*
* cmd_procedure: executes a stored procedure.
*
* Inputs:
* cmd->argv[0]: connection name
* cmd->argv[1]: procedure name
* cmd->argv[2]: procedure string
*
* Returns:
* either a properly filled error modret_t if the procedure failed in
* some way, or a modret_t with the result data. If a procedure
* returns data, it should be returned in the same way as cmd_select.
*
* Notes:
* not every backend will support stored procedures. Backends which do
* not support stored procedures should return an error with a descriptive
* error message (something like 'backend does not support procedures').
*/
MODRET cmd_procedure(cmd_rec *cmd)
{
sql_log(DEBUG_FUNC, "%s", ">>> tds cmd_procedure");
_sql_check_cmd(cmd, "cmd_procedure");