Debian Codegraph @

contrib/mod_quotatab_sql.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_quotatab_sql -- a mod_quotatab sub-module for managing quota
3 * data via SQL-based tables
4 * Copyright (c) 2002-2021 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders gives permission to link this program
21 * with OpenSSL, and distribute the resulting executable, without including
22 * the source code for OpenSSL in the source distribution.
23 */
24
25 #include "mod_quotatab.h"
26 #include "mod_sql.h"
27
28 #define QUOTATAB_SQL_VALUE_BUFSZ 20
29
30 module quotatab_sql_module;
31
32 static cmd_rec *sqltab_cmd_create(pool *parent_pool, unsigned int argc, ...) {
33 register unsigned int i = 0;
34 pool *cmd_pool = NULL;
35 cmd_rec *cmd = NULL;
36 va_list argp;
37
38 cmd_pool = make_sub_pool(parent_pool);
39 cmd = (cmd_rec *) pcalloc(cmd_pool, sizeof(cmd_rec));
40 cmd->pool = cmd_pool;
41
42 cmd->argc = argc;
43 cmd->argv = pcalloc(cmd->pool, argc * sizeof(void *));
44
45 /* Hmmm... */
46 cmd->tmp_pool = cmd->pool;
47
48 va_start(argp, argc);
49 for (i = 0; i < argc; i++) {
50 cmd->argv[i] = va_arg(argp, char *);
51 }
52 va_end(argp);
53
54 return cmd;
55 }
56
57 static char *sqltab_get_name(pool *p, char *name) {
58 cmdtable *cmdtab;
59 cmd_rec *cmd;
60 modret_t *res;
61
62 /* Find the cmdtable for the sql_escapestr command. */
63 cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_escapestr", NULL, NULL, NULL);
64 if (cmdtab == NULL) {
65 quotatab_log("error: unable to find SQL hook symbol 'sql_escapestr'");
66 return name;
67 }
68
69 if (strlen(name) == 0) {
70 return name;
71 }
72
73 cmd = sqltab_cmd_create(p, 1, pr_str_strip(p, name));
74
75 /* Call the handler. */
76 res = pr_module_call(cmdtab->m, cmdtab->handler, cmd);
77
78 /* Check the results. */
79 if (MODRET_ISDECLINED(res) ||
80 MODRET_ISERROR(res)) {
81 quotatab_log("error executing 'sql_escapestring'");
82 return name;
83 }
84
85 return res->data;
86 }
87
88 static int sqltab_close(quota_table_t *sqltab) {
89
90 /* Is there really anything that needs to be done here? */
91 return 0;
92 }
93
94 static int sqltab_create(quota_table_t *sqltab, void *ptr) {
95 pool *tmp_pool = NULL;
96 cmdtable *sql_cmdtab = NULL;
97 cmd_rec *sql_cmd = NULL;
98 modret_t *sql_res = NULL;
99 char *insert_query = NULL, *tally_quota_name = NULL, *tally_quota_type = NULL,
100 *tally_bytes_in = NULL, *tally_bytes_out = NULL, *tally_bytes_xfer = NULL,
101 *tally_files_in = NULL, *tally_files_out = NULL, *tally_files_xfer = NULL;
102 quota_tally_t *tally = ptr;
103
104 /* Allocate a sub pool for use by this function. */
105 tmp_pool = make_sub_pool(sqltab->tab_pool);
106
107 /* Allocate small buffers for stringifying most of the tally struct
108 * members: quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used,
109 * files_in_used, files_out_used, files_xfer_used.
110 */
111 tally_quota_name = pcalloc(tmp_pool, 83 * sizeof(char));
112 tally_quota_type = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
113 tally_bytes_in = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
114 tally_bytes_out = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
115 tally_bytes_xfer = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
116 tally_files_in = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
117 tally_files_out = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
118 tally_files_xfer = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
119
120 /* Execute the INSERT NamedQuery */
121 insert_query = ((char **) sqltab->tab_data)[2];
122
123 /* Populate the INSERT query with the necessary data from the current
124 * limit record. Need to stringify most of the tally struct members:
125 * quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used,
126 * files_in_used, files_out_used, files_xfer_used.
127 */
128
129 /* NOTE: Per Issue #1149, we should NOT be adding the quotes to the
130 * text ourselves here. It also makes the mod_quotatab_sql configuration
131 * inconsistent; the admin must quote these texts in the config for
132 * SELECTs, but not for INSERTs.
133 */
134 pr_snprintf(tally_quota_name, 83, "'%s'",
135 sqltab_get_name(tmp_pool, tally->name));
136 tally_quota_name[82] = '\0';
137
138 if (tally->quota_type == USER_QUOTA) {
139 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "'%s'", "user");
140
141 } else if (tally->quota_type == GROUP_QUOTA) {
142 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "'%s'", "group");
143
144 } else if (tally->quota_type == CLASS_QUOTA) {
145 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "'%s'", "class");
146
147 } else if (tally->quota_type == ALL_QUOTA) {
148 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "'%s'", "all");
149 }
150
151 tally_quota_type[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
152
153 pr_snprintf(tally_bytes_in, QUOTATAB_SQL_VALUE_BUFSZ, "%f",
154 tally->bytes_in_used);
155 tally_bytes_in[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
156
157 pr_snprintf(tally_bytes_out, QUOTATAB_SQL_VALUE_BUFSZ, "%f",
158 tally->bytes_out_used);
159 tally_bytes_out[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
160
161 pr_snprintf(tally_bytes_xfer, QUOTATAB_SQL_VALUE_BUFSZ, "%f",
162 tally->bytes_xfer_used);
163 tally_bytes_xfer[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
164
165 pr_snprintf(tally_files_in, QUOTATAB_SQL_VALUE_BUFSZ, "%u",
166 tally->files_in_used);
167 tally_files_in[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
168
169 pr_snprintf(tally_files_out, QUOTATAB_SQL_VALUE_BUFSZ, "%u",
170 tally->files_out_used);
171 tally_files_out[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
172
173 pr_snprintf(tally_files_xfer, QUOTATAB_SQL_VALUE_BUFSZ, "%u",
174 tally->files_xfer_used);
175 tally_files_xfer[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
176
177 sql_cmd = sqltab_cmd_create(tmp_pool, 10, "sql_change", insert_query,
178 tally_quota_name, tally_quota_type,
179 tally_bytes_in, tally_bytes_out, tally_bytes_xfer,
180 tally_files_in, tally_files_out, tally_files_xfer);
181
182 /* Find the cmdtable for the sql_change command. */
183 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_change", NULL, NULL,
184 NULL);
185 if (sql_cmdtab == NULL) {
186 quotatab_log("error: unable to find SQL hook symbol 'sql_change'");
187 destroy_pool(tmp_pool);
188 return -1;
189 }
190
191 /* Call the handler. */
192 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
193
194 /* Check the results. */
195 if (MODRET_ISERROR(sql_res)) {
196 quotatab_log("error executing NamedQuery '%s': %s", insert_query,
197 strerror(errno));
198 destroy_pool(tmp_pool);
199 return -1;
200 }
201
202 destroy_pool(tmp_pool);
203 return 0;
204 }
205
206 static unsigned char sqltab_lookup(quota_table_t *sqltab, void *ptr,
207 const char *name, quota_type_t quota_type) {
208 pool *tmp_pool = NULL;
209 cmdtable *sql_cmdtab = NULL;
210 cmd_rec *sql_cmd = NULL;
211 modret_t *sql_res = NULL;
212 array_header *sql_data = NULL;
213 char *select_query = NULL;
214
215 /* Allocate a temporary pool for the duration of this lookup. */
216 tmp_pool = make_sub_pool(sqltab->tab_pool);
217
218 /* Handle tally and limit tables differently... */
219 if (sqltab->tab_type == TYPE_TALLY) {
220 select_query = ((char **) sqltab->tab_data)[0];
221
222 } else if (sqltab->tab_type == TYPE_LIMIT) {
223 select_query = (char *) sqltab->tab_data;
224 }
225
226 /* Find the cmdtable for the sql_lookup command. */
227 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
228 NULL);
229 if (sql_cmdtab == NULL) {
230 quotatab_log("error: unable to find SQL hook symbol 'sql_lookup'");
231 destroy_pool(tmp_pool);
232 return FALSE;
233 }
234
235 /* Prepare the SELECT query. */
236 sql_cmd = sqltab_cmd_create(tmp_pool, 4, "sql_lookup", select_query,
237 name ? sqltab_get_name(tmp_pool, (char *) name) : "",
238 quota_type == USER_QUOTA ? "user" : quota_type == GROUP_QUOTA ? "group" :
239 quota_type == CLASS_QUOTA ? "class" : "all");
240
241 /* Call the handler. */
242 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
243
244 /* Check the results. */
245 if (sql_res == NULL ||
246 MODRET_ISERROR(sql_res)) {
247 quotatab_log("error processing NamedQuery '%s'", select_query);
248 destroy_pool(tmp_pool);
249 return FALSE;
250 }
251
252 sql_data = (array_header *) sql_res->data;
253
254 if (sqltab->tab_type == TYPE_TALLY) {
255 quota_tally_t *tally = ptr;
256 char **values = (char **) sql_data->elts;
257
258 /* Update the tally record with the 8 values:
259 * name
260 * quota_type
261 * bytes_{in,out,xfer}_used
262 * files_{in,out,xfer}_used
263 */
264
265 if (sql_data->nelts < 8) {
266 if (sql_data->nelts > 0) {
267 quotatab_log("error: SQLNamedQuery '%s' returned incorrect number of "
268 "values (%d)", select_query, sql_data->nelts);
269 }
270
271 destroy_pool(tmp_pool);
272 return FALSE;
273 }
274
275 if (sql_data->nelts > 8) {
276 quotatab_log("warning: SQLNamedQuery '%s' returned more values than "
277 "expected (%d)", select_query, sql_data->nelts);
278 }
279
280 /* Process each element returned. */
281 sstrncpy(tally->name, values[0], sizeof(tally->name)-1);
282
283 if (strcasecmp(values[1], "user") == 0) {
284 tally->quota_type = USER_QUOTA;
285
286 } else if (strcasecmp(values[1], "group") == 0) {
287 tally->quota_type = GROUP_QUOTA;
288
289 } else if (strcasecmp(values[1], "class") == 0) {
290 tally->quota_type = CLASS_QUOTA;
291
292 } else if (strcasecmp(values[1], "all") == 0) {
293 tally->quota_type = ALL_QUOTA;
294
295 } else {
296 quotatab_log("warning: SQLNamedQuery '%s' returned unknown/unsupported "
297 "quota type ('%s')", select_query, values[1]);
298 }
299
300 /* Check if this is the requested record, now that enough information
301 * is in place.
302 */
303 if (quota_type != tally->quota_type) {
304 destroy_pool(tmp_pool);
305 return FALSE;
306 }
307
308 /* Match names if need be */
309 if (quota_type != ALL_QUOTA &&
310 values[0] &&
311 strlen(values[0]) > 0 &&
312 name != NULL &&
313 strcmp(name, tally->name) != 0) {
314 destroy_pool(tmp_pool);
315 return FALSE;
316 }
317
318 tally->bytes_in_used = -1.0;
319 if (values[2]) {
320 tally->bytes_in_used = atof(values[2]);
321 }
322
323 tally->bytes_out_used = -1.0;
324 if (values[3]) {
325 tally->bytes_out_used = atof(values[3]);
326 }
327
328 tally->bytes_xfer_used = -1.0;
329 if (values[4]) {
330 tally->bytes_xfer_used = atof(values[4]);
331 }
332
333 tally->files_in_used = 0;
334 if (values[5]) {
335 tally->files_in_used = atol(values[5]);
336 }
337
338 tally->files_out_used = 0;
339 if (values[6]) {
340 tally->files_out_used = atol(values[6]);
341 }
342
343 tally->files_xfer_used = 0;
344 if (values[7]) {
345 tally->files_xfer_used = atol(values[7]);
346 }
347
348 destroy_pool(tmp_pool);
349 return TRUE;
350 }
351
352 if (sqltab->tab_type == TYPE_LIMIT) {
353 quota_limit_t *limit = ptr;
354 char **values = (char **) sql_data->elts;
355
356 /* Update the limit record with the 10 values:
357 * name
358 * quota_type
359 * per_session
360 * limit_type
361 * bytes_{in,out,xfer}_avail
362 * files_{in,out,xfer}_avail
363 */
364
365 if (sql_data->nelts < 10) {
366 if (sql_data->nelts > 0) {
367 quotatab_log("error: SQLNamedQuery '%s' returned incorrect number of "
368 "values (%d)", select_query, sql_data->nelts);
369 }
370
371 destroy_pool(tmp_pool);
372 return FALSE;
373 }
374
375 if (sql_data->nelts > 10) {
376 quotatab_log("warning: SQLNamedQuery '%s' returned more values than "
377 "expected (%d)", select_query, sql_data->nelts);
378 }
379
380 /* Process each element returned. */
381 sstrncpy(limit->name, values[0], sizeof(limit->name)-1);
382
383 if (strcasecmp(values[1], "user") == 0) {
384 limit->quota_type = USER_QUOTA;
385
386 } else if (strcasecmp(values[1], "group") == 0) {
387 limit->quota_type = GROUP_QUOTA;
388
389 } else if (strcasecmp(values[1], "class") == 0) {
390 limit->quota_type = CLASS_QUOTA;
391
392 } else if (strcasecmp(values[1], "all") == 0) {
393 limit->quota_type = ALL_QUOTA;
394
395 } else {
396 quotatab_log("warning: SQLNamedQuery '%s' returned unknown/unsupported "
397 "quota type ('%s')", select_query, values[1]);
398 }
399
400 /* Check if this is the requested record, now that enough information
401 * is in place.
402 */
403 if (quota_type != limit->quota_type) {
404 destroy_pool(tmp_pool);
405 return FALSE;
406 }
407
408 /* Match names if need be */
409 if (quota_type != ALL_QUOTA &&
410 values[0] &&
411 strlen(values[0]) > 0 &&
412 name != NULL &&
413 strcmp(name, limit->name) != 0) {
414 destroy_pool(tmp_pool);
415 return FALSE;
416 }
417
418 if (strcasecmp(values[2], "false") == 0) {
419 limit->quota_per_session = FALSE;
420
421 } else if (strcasecmp(values[2], "true") == 0) {
422 limit->quota_per_session = TRUE;
423
424 } else {
425 quotatab_log("warning: SQLNamedQuery '%s' returned unknown/unsupported "
426 "quota per-session value ('%s')", select_query, values[2]);
427 }
428
429 if (strcasecmp(values[3], "soft") == 0) {
430 limit->quota_limit_type = SOFT_LIMIT;
431
432 } else if (strcasecmp(values[3], "hard") == 0) {
433 limit->quota_limit_type = HARD_LIMIT;
434
435 } else {
436 quotatab_log("warning: SQLNamedQuery '%s' returned unknown/unsupported "
437 "quota limit type ('%s')", select_query, values[3]);
438 }
439
440 limit->bytes_in_avail = -1.0;
441 if (values[4]) {
442 limit->bytes_in_avail = atof(values[4]);
443 }
444
445 limit->bytes_out_avail = -1.0;
446 if (values[5]) {
447 limit->bytes_out_avail = atof(values[5]);
448 }
449
450 limit->bytes_xfer_avail = -1.0;
451 if (values[6]) {
452 limit->bytes_xfer_avail = atof(values[6]);
453 }
454
455 limit->files_in_avail = 0;
456 if (values[7]) {
457 limit->files_in_avail = atol(values[7]);
458 }
459
460 limit->files_out_avail = 0;
461 if (values[8]) {
462 limit->files_out_avail = atol(values[8]);
463 }
464
465 limit->files_xfer_avail = 0;
466 if (values[9]) {
467 limit->files_xfer_avail = atol(values[9]);
468 }
469
470 destroy_pool(tmp_pool);
471 return TRUE;
472 }
473
474 destroy_pool(tmp_pool);
475
476 /* default */
477 return FALSE;
478 }
479
480 static int sqltab_read(quota_table_t *sqltab, void *ptr) {
481 quota_tally_t *tally = ptr;
482
483 return sqltab_lookup(sqltab, ptr, tally->name, tally->quota_type);
484 }
485
486 static unsigned char sqltab_verify(quota_table_t *sqltab) {
487
488 /* Always TRUE. */
489 return TRUE;
490 }
491
492 static int sqltab_write(quota_table_t *sqltab, void *ptr) {
493 pool *tmp_pool = NULL;
494 cmdtable *sql_cmdtab = NULL;
495 cmd_rec *sql_cmd = NULL;
496 modret_t *sql_res = NULL;
497 char *update_query = NULL, *tally_quota_type = NULL,
498 *tally_bytes_in = NULL, *tally_bytes_out = NULL, *tally_bytes_xfer = NULL,
499 *tally_files_in = NULL, *tally_files_out = NULL, *tally_files_xfer = NULL;
500 quota_tally_t *tally = ptr;
501
502 /* Allocate a sub pool for use by this function. */
503 tmp_pool = make_sub_pool(sqltab->tab_pool);
504
505 /* Allocate small buffers for stringifying most of the tally struct
506 * members: quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used,
507 * files_in_used, files_out_used, files_xfer_used.
508 */
509 tally_quota_type = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
510 tally_bytes_in = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
511 tally_bytes_out = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
512 tally_bytes_xfer = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
513 tally_files_in = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
514 tally_files_out = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
515 tally_files_xfer = pcalloc(tmp_pool, QUOTATAB_SQL_VALUE_BUFSZ * sizeof(char));
516
517 /* Retrieve the UPDATE NamedQuery */
518 update_query = ((char **) sqltab->tab_data)[1];
519
520 /* Populate the UPDATE query with the necessary data from the current
521 * limit record. Need to stringify most of the tally struct members:
522 * quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used,
523 * files_in_used, files_out_used, files_xfer_used.
524 */
525
526 if (tally->quota_type == USER_QUOTA) {
527 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "%s", "user");
528
529 } else if (tally->quota_type == GROUP_QUOTA) {
530 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "%s", "group");
531
532 } else if (tally->quota_type == CLASS_QUOTA) {
533 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "%s", "class");
534
535 } else if (tally->quota_type == ALL_QUOTA) {
536 pr_snprintf(tally_quota_type, QUOTATAB_SQL_VALUE_BUFSZ, "%s", "all");
537 }
538
539 tally_quota_type[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
540
541 /* Note: use the deltas data, not the tally members, so that the
542 * UPDATE can do an "atomic" read+update all in one shot.
543 */
544 pr_snprintf(tally_bytes_in, QUOTATAB_SQL_VALUE_BUFSZ, "%f",
545 quotatab_deltas.bytes_in_delta);
546 tally_bytes_in[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
547
548 pr_snprintf(tally_bytes_out, QUOTATAB_SQL_VALUE_BUFSZ, "%f",
549 quotatab_deltas.bytes_out_delta);
550 tally_bytes_out[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
551
552 pr_snprintf(tally_bytes_xfer, QUOTATAB_SQL_VALUE_BUFSZ, "%f",
553 quotatab_deltas.bytes_xfer_delta);
554 tally_bytes_xfer[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
555
556 /* Don't try to prevent underflows here; mod_quotatab already makes
557 * these checks.
558 */
559 pr_snprintf(tally_files_in, QUOTATAB_SQL_VALUE_BUFSZ, "%d",
560 quotatab_deltas.files_in_delta);
561 tally_files_in[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
562
563 pr_snprintf(tally_files_out, QUOTATAB_SQL_VALUE_BUFSZ, "%d",
564 quotatab_deltas.files_out_delta);
565 tally_files_out[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
566
567 pr_snprintf(tally_files_xfer, QUOTATAB_SQL_VALUE_BUFSZ, "%d",
568 quotatab_deltas.files_xfer_delta);
569 tally_files_xfer[QUOTATAB_SQL_VALUE_BUFSZ-1] = '\0';
570
571 sql_cmd = sqltab_cmd_create(tmp_pool, 10, "sql_change", update_query,
572 tally_bytes_in, tally_bytes_out, tally_bytes_xfer,
573 tally_files_in, tally_files_out, tally_files_xfer,
574 sqltab_get_name(tmp_pool, tally->name), tally_quota_type);
575
576 /* Find the cmdtable for the sql_change command. */
577 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_change", NULL, NULL,
578 NULL);
579 if (sql_cmdtab == NULL) {
580 quotatab_log("error: unable to find SQL hook symbol 'sql_change'");
581 destroy_pool(tmp_pool);
582 return -1;
583 }
584
585 /* Call the handler. */
586 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
587
588 /* Check the results. */
589 if (MODRET_ISERROR(sql_res)) {
590 quotatab_log("error executing NamedQuery '%s': %s", update_query,
591 strerror(errno));
592 destroy_pool(tmp_pool);
593 return -1;
594 }
595
596 destroy_pool(tmp_pool);
597 return 0;
598 }
599
600 static int sqltab_rlock(quota_table_t *sqltab) {
601
602 /* Check for a configured lock file. */
603 if (sqltab->tab_lockfd > 0) {
604 sqltab->tab_lock.l_type = F_RDLCK;
605 return fcntl(sqltab->tab_lockfd, F_SETLK, &sqltab->tab_lock);
606 }
607
608 return 0;
609 }
610
611 static int sqltab_unlock(quota_table_t *sqltab) {
612
613 /* Check for a configured lock file. */
614 if (sqltab->tab_lockfd > 0) {
615 sqltab->tab_lock.l_type = F_UNLCK;
616 return fcntl(sqltab->tab_lockfd, F_SETLK, &sqltab->tab_lock);
617 }
618
619 return 0;
620 }
621
622 static int sqltab_wlock(quota_table_t *sqltab) {
623
624 /* Check for a configured lock file. */
625 if (sqltab->tab_lockfd > 0) {
626 sqltab->tab_lock.l_type = F_WRLCK;
627 return fcntl(sqltab->tab_lockfd, F_SETLK, &sqltab->tab_lock);
628 }
629
630 return 0;
631 }
632
633 static quota_table_t *sqltab_open(pool *parent_pool, quota_tabtype_t tab_type,
634 const char *srcinfo) {
635
636 quota_table_t *tab = NULL;
637 pool *tab_pool = make_sub_pool(parent_pool),
638 *tmp_pool = make_sub_pool(parent_pool);
639 config_rec *c = NULL;
640 char *named_query = NULL;
641
642 tab = (quota_table_t *) pcalloc(tab_pool, sizeof(quota_table_t));
643 tab->tab_pool = tab_pool;
644 tab->tab_type = tab_type;
645
646 if (tab->tab_type == TYPE_TALLY) {
647 char *start = NULL, *finish = NULL;
648 char *select_query = NULL, *update_query = NULL, *insert_query = NULL;
649
650 /* Parse the SELECT, UPDATE, and INSERT query names out of the srcinfo
651 * string. Lookup and store the queries in the tab_data area, so that
652 * they need not be looked up later.
653 *
654 * The srcinfo string for this case should look like:
655 * "/<select-named-query>/<update-named-query>/<insert-named-query>/"
656 */
657
658 start = strchr(srcinfo, '/');
659 if (start == NULL) {
660 quotatab_log("error: badly formatted source info '%s'", srcinfo);
661 destroy_pool(tmp_pool);
662 errno = EINVAL;
663 return NULL;
664 }
665
666 /* Find the next slash. */
667 finish = strchr(++start, '/');
668 if (finish == NULL) {
669 quotatab_log("error: badly formatted source info '%s'", srcinfo);
670 destroy_pool(tmp_pool);
671 errno = EINVAL;
672 return NULL;
673 }
674
675 *finish = '\0';
676 select_query = pstrdup(tab->tab_pool, start);
677
678 /* Verify that the named query has indeed been defined. This is
679 * based on how mod_sql creates its config_rec names.
680 */
681 named_query = pstrcat(tmp_pool, "SQLNamedQuery_", select_query, NULL);
682
683 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
684 if (c == NULL) {
685 quotatab_log("error: unable to resolve SQLNamedQuery name '%s'",
686 select_query);
687 destroy_pool(tmp_pool);
688 errno = EINVAL;
689 return NULL;
690 }
691
692 /* Now, find the next slash. */
693 start = finish;
694 finish = strchr(++start, '/');
695 if (finish == NULL) {
696 quotatab_log("error: badly formatted source info '%s'", srcinfo);
697 destroy_pool(tmp_pool);
698 errno = EINVAL;
699 return NULL;
700 }
701
702 *finish = '\0';
703 update_query = pstrdup(tab->tab_pool, start);
704
705 /* Verify that the named query has indeed been defined. This is
706 * based on how mod_sql creates its config_rec names.
707 */
708 named_query = pstrcat(tmp_pool, "SQLNamedQuery_", update_query, NULL);
709
710 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
711 if (c == NULL) {
712 quotatab_log("error: unable to resolve SQLNamedQuery name '%s'",
713 update_query);
714 destroy_pool(tmp_pool);
715 errno = EINVAL;
716 return NULL;
717 }
718
719 /* Assume the rest of the srcinfo string is the INSERT query (assuming
720 * there *is* a "rest of the string").
721 */
722 if (*(++finish) != '\0') {
723 insert_query = pstrdup(tab->tab_pool, finish);
724
725 } else {
726 quotatab_log("error: badly formatted source info '%s'", srcinfo);
727 destroy_pool(tmp_pool);
728 errno = EINVAL;
729 return NULL;
730 }
731
732 /* Verify that the named query has indeed been defined. This is
733 * based on how mod_sql creates its config_rec names.
734 */
735 named_query = pstrcat(tmp_pool, "SQLNamedQuery_", insert_query, NULL);
736
737 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
738 if (c == NULL) {
739 quotatab_log("error: unable to resolve SQLNamedQuery name '%s'",
740 insert_query);
741 destroy_pool(tmp_pool);
742 errno = EINVAL;
743 return NULL;
744 }
745
746 tab->tab_data = (char **) pcalloc(tab->tab_pool, 3 * sizeof(char *));
747 ((char **) tab->tab_data)[0] = pstrdup(tab->tab_pool, select_query);
748 ((char **) tab->tab_data)[1] = pstrdup(tab->tab_pool, update_query);
749 ((char **) tab->tab_data)[2] = pstrdup(tab->tab_pool, insert_query);
750
751 } else if (tab->tab_type == TYPE_LIMIT) {
752 char *start = NULL, *select_query = NULL;
753
754 /* Parse the SELECT query name out of the srcinfo string. Lookup and
755 * store the queries in the tab_data area, so that it need not be looked
756 * up later.
757 *
758 * The srcinfo string for this case should look like:
759 * "/<select-named-query>"
760 */
761
762 start = strchr(srcinfo, '/');
763 if (start == NULL) {
764 quotatab_log("error: badly formatted source info '%s'", srcinfo);
765 destroy_pool(tmp_pool);
766 errno = EINVAL;
767 return NULL;
768 }
769 select_query = ++start;
770
771 /* Verify that the named query has indeed been defined. This is
772 * based on how mod_sql creates its config_rec names.
773 */
774 named_query = pstrcat(tmp_pool, "SQLNamedQuery_", select_query, NULL);
775
776 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
777 if (c == NULL) {
778 quotatab_log("error: unable to resolve SQLNamedQuery name '%s'",
779 select_query);
780 destroy_pool(tmp_pool);
781 errno = EINVAL;
782 return NULL;
783 }
784
785 tab->tab_data = (void *) pstrdup(tab->tab_pool, select_query);
786 }
787
788 /* Set all the necessary function pointers. */
789 tab->tab_close = sqltab_close;
790 tab->tab_create = sqltab_create;
791 tab->tab_lookup = sqltab_lookup;
792 tab->tab_read = sqltab_read;
793 tab->tab_verify = sqltab_verify;
794 tab->tab_write = sqltab_write;
795
796 tab->tab_rlock = sqltab_rlock;
797 tab->tab_unlock = sqltab_unlock;
798 tab->tab_wlock = sqltab_wlock;
799
800 /* Prepare the lock structure. */
801 tab->tab_lock.l_whence = SEEK_CUR;
802 tab->tab_lock.l_start = 0;
803 tab->tab_lock.l_len = 0;
804
805 destroy_pool(tmp_pool);
806 return tab;
807 }
808
809 /* Event handlers
810 */
811
812 #if defined(PR_SHARED_MODULE)
813 static void sqltab_mod_unload_ev(const void *event_data, void *user_data) {
814 if (strcmp("mod_quotatab_sql.c", (const char *) event_data) == 0) {
815 pr_event_unregister(&quotatab_sql_module, NULL, NULL);
816 quotatab_unregister_backend("sql", QUOTATAB_LIMIT_SRC|QUOTATAB_TALLY_SRC);
817 }
818 }
819 #endif /* PR_SHARED_MODULE */
820
821 /* Initialization routines
822 */
823
824 static int sqltab_init(void) {
825
826 /* Initialize the quota source objects for type "sql". */
827 quotatab_register_backend("sql", sqltab_open,
828 QUOTATAB_LIMIT_SRC|QUOTATAB_TALLY_SRC);
829
830 #if defined(PR_SHARED_MODULE)
831 pr_event_register(&quotatab_sql_module, "core.module-unload",
832 sqltab_mod_unload_ev, NULL);
833 #endif /* PR_SHARED_MODULE */
834
835 return 0;
836 }
837
838 module quotatab_sql_module = {
839 NULL, NULL,
840
841 /* Module API version 2.0 */
842 0x20,
843
844 /* Module name */
845 "quotatab_sql",
846
847 /* Module configuration handler table */
848 NULL,
849
850 /* Module command handler table */
851 NULL,
852
853 /* Module authentication handler table */
854 NULL,
855
856 /* Module initialization */
857 sqltab_init,
858
859 /* Session initialization */
860 NULL
861 };