Debian Codegraph @

contrib/mod_wrap2_sql.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_wrap2_sql -- a mod_wrap2 sub-module for supplying IP-based
3 * access control data via SQL tables
4 * Copyright (c) 2002-2026 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_wrap2.h"
26 #include "mod_sql.h"
27
28 #define MOD_WRAP2_SQL_VERSION "mod_wrap2_sql/1.1"
29
30 #define WRAP2_SQL_NSLOTS 2
31 #define WRAP2_SQL_CLIENT_QUERY_IDX 0
32 #define WRAP2_SQL_OPTION_QUERY_IDX 1
33
34 module wrap2_sql_module;
35
36 static cmd_rec *sql_cmd_create(pool *parent_pool, unsigned int argc, ...) {
37 register unsigned int i = 0;
38 pool *cmd_pool = NULL;
39 cmd_rec *cmd = NULL;
40 va_list argp;
41
42 cmd_pool = make_sub_pool(parent_pool);
43 cmd = (cmd_rec *) pcalloc(cmd_pool, sizeof(cmd_rec));
44 cmd->pool = cmd_pool;
45
46 cmd->argc = argc;
47 cmd->argv = pcalloc(cmd->pool, argc * sizeof(void *));
48
49 /* Hmmm... */
50 cmd->tmp_pool = cmd->pool;
51
52 va_start(argp, argc);
53 for (i = 0; i < argc; i++) {
54 cmd->argv[i] = va_arg(argp, char *);
55 }
56 va_end(argp);
57
58 return cmd;
59 }
60
61 static int sqltab_close_cb(wrap2_table_t *sqltab) {
62 return 0;
63 }
64
65 static char *sqltab_get_escaped_text(pool *p, wrap2_table_t *sqltab,
66 const char *text) {
67 pool *tmp_pool = NULL;
68 cmdtable *sql_cmdtab = NULL;
69 cmd_rec *sql_cmd = NULL;
70 modret_t *sql_res = NULL;
71
72 /* Find the cmdtable for the sql_escapestr command, as the provided
73 * name needs to be properly escaped for SQL syntax; see Issue #2057.
74 */
75 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_escapestr", NULL, NULL,
76 NULL);
77 if (sql_cmdtab == NULL) {
78 wrap2_log("error: unable to find SQL hook symbol 'sql_escapestr': "
79 "perhaps your proftpd.conf needs 'LoadModule mod_sql.c'?");
80 return NULL;
81 }
82
83 sql_cmd = sql_cmd_create(tmp_pool, 1, text);
84 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
85 if (sql_res == NULL) {
86 wrap2_log("sql_escapestr '%s' returned no data; "
87 "see the mod_sql.c SQLLogFile for more details", text);
88 return NULL;
89 }
90
91 if (MODRET_ISERROR(sql_res)) {
92 wrap2_log("error processing sql_escapestr '%s': "
93 "check the mod_sql.c SQLLogFile for more details", text);
94 return NULL;
95 }
96
97 return sql_res->data;
98 }
99
100 static array_header *sqltab_fetch_clients_cb(wrap2_table_t *sqltab,
101 const char *name) {
102 register unsigned int i;
103 pool *tmp_pool = NULL;
104 cmdtable *sql_cmdtab = NULL;
105 cmd_rec *sql_cmd = NULL;
106 modret_t *sql_res = NULL;
107 array_header *sql_data = NULL;
108 char *escaped_name = NULL, *query = NULL, **vals = NULL;
109 array_header *clients_list = NULL;
110
111 /* Allocate a temporary pool for the duration of this read. */
112 tmp_pool = make_sub_pool(sqltab->tab_pool);
113
114 escaped_name = sqltab_get_escaped_text(tmp_pool, sqltab, name);
115 if (escaped_name == NULL) {
116 destroy_pool(tmp_pool);
117 return NULL;
118 }
119
120 query = ((char **) sqltab->tab_data)[WRAP2_SQL_CLIENT_QUERY_IDX];
121
122 /* Find the cmdtable for the sql_lookup command. */
123 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
124 NULL);
125 if (sql_cmdtab == NULL) {
126 wrap2_log("error: unable to find SQL hook symbol 'sql_lookup': "
127 "perhaps your proftpd.conf needs 'LoadModule mod_sql.c'?");
128 destroy_pool(tmp_pool);
129 return NULL;
130 }
131
132 /* Prepare the SELECT query. */
133 sql_cmd = sql_cmd_create(tmp_pool, 3, "sql_lookup", query, escaped_name);
134
135 /* Call the handler. */
136 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
137
138 /* Check the results. */
139 if (sql_res == NULL) {
140 wrap2_log("SQLNamedQuery '%s' returned no data; "
141 "see the mod_sql.c SQLLogFile for more details", query);
142 destroy_pool(tmp_pool);
143 return NULL;
144 }
145
146 if (MODRET_ISERROR(sql_res)) {
147 wrap2_log("error processing SQLNamedQuery '%s': "
148 "check the mod_sql.c SQLLogFile for more details", query);
149 destroy_pool(tmp_pool);
150 return NULL;
151 }
152
153 sql_data = (array_header *) sql_res->data;
154 vals = (char **) sql_data->elts;
155
156 if (sql_data->nelts < 1) {
157 wrap2_log("SQLNamedQuery '%s' returned no data; "
158 "see the mod_sql.c SQLLogFile for more details", query);
159 destroy_pool(tmp_pool);
160 return NULL;
161 }
162
163 clients_list = make_array(sqltab->tab_pool, sql_data->nelts, sizeof(char *));
164
165 /* Iterate through each returned row. If there are commas or whitespace
166 * in the row, parse them as separate client names. Otherwise, a comma-
167 * or space-delimited list of names will be treated as a single name, and
168 * violate the principle of least surprise for the site admin.
169 */
170
171 for (i = 0; i < sql_data->nelts; i++) {
172 char *ptr;
173
174 if (vals[i] == NULL) {
175 continue;
176 }
177
178 ptr = strpbrk(vals[i], ", \t");
179 if (ptr != NULL) {
180 char *dup_opts, *word;
181
182 dup_opts = pstrdup(sqltab->tab_pool, vals[i]);
183 while ((word = pr_str_get_token(&dup_opts, ", \t")) != NULL) {
184 size_t wordlen;
185
186 pr_signals_handle();
187
188 wordlen = strlen(word);
189 if (wordlen == 0) {
190 continue;
191 }
192
193 /* Remove any trailing comma */
194 if (word[wordlen-1] == ',') {
195 word[wordlen-1] = '\0';
196 wordlen--;
197 }
198
199 *((char **) push_array(clients_list)) = word;
200
201 /* Skip redundant whitespaces */
202 while (*dup_opts == ' ' ||
203 *dup_opts == '\t') {
204 pr_signals_handle();
205 dup_opts++;
206 }
207 }
208
209 } else {
210 *((char **) push_array(clients_list)) = pstrdup(sqltab->tab_pool,
211 vals[i]);
212 }
213 }
214
215 destroy_pool(tmp_pool);
216 return clients_list;
217 }
218
219 static array_header *sqltab_fetch_daemons_cb(wrap2_table_t *sqltab,
220 const char *name) {
221 array_header *daemons_list = make_array(sqltab->tab_pool, 1, sizeof(char *));
222
223 /* Simply return the service name we're given. */
224 *((char **) push_array(daemons_list)) = pstrdup(sqltab->tab_pool, name);
225
226 return daemons_list;
227 }
228
229 static array_header *sqltab_fetch_options_cb(wrap2_table_t *sqltab,
230 const char *name) {
231 pool *tmp_pool = NULL;
232 cmdtable *sql_cmdtab = NULL;
233 cmd_rec *sql_cmd = NULL;
234 modret_t *sql_res = NULL;
235 array_header *sql_data = NULL;
236 char *escaped_name = NULL, *query = NULL, **vals = NULL;
237 array_header *options_list = NULL;
238
239 /* Allocate a temporary pool for the duration of this read. */
240 tmp_pool = make_sub_pool(sqltab->tab_pool);
241
242 escaped_name = sqltab_get_escaped_text(tmp_pool, sqltab, name);
243 if (escaped_name == NULL) {
244 destroy_pool(tmp_pool);
245 return NULL;
246 }
247
248 query = ((char **) sqltab->tab_data)[WRAP2_SQL_OPTION_QUERY_IDX];
249
250 /* The options-query is not necessary. Skip if not present. */
251 if (query == NULL) {
252 destroy_pool(tmp_pool);
253 return NULL;
254 }
255
256 /* Find the cmdtable for the sql_lookup command. */
257 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
258 NULL);
259 if (sql_cmdtab == NULL) {
260 wrap2_log("error: unable to find SQL hook symbol 'sql_lookup': "
261 "perhaps your proftpd.conf needs 'LoadModule mod_sql.c'?");
262 destroy_pool(tmp_pool);
263 return NULL;
264 }
265
266 /* Prepare the SELECT query. */
267 sql_cmd = sql_cmd_create(tmp_pool, 3, "sql_lookup", query, escaped_name);
268
269 /* Call the handler. */
270 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
271
272 /* Check the results. */
273 if (!sql_res) {
274 wrap2_log("SQLNamedQuery '%s' returned no data; "
275 "see the mod_sql.c SQLLogFile for more details", query);
276 destroy_pool(tmp_pool);
277 return NULL;
278 }
279
280 if (MODRET_ISERROR(sql_res)) {
281 wrap2_log("error processing SQLNamedQuery '%s': "
282 "check the mod_sql.c SQLLogFile for more details", query);
283 destroy_pool(tmp_pool);
284 return NULL;
285 }
286
287 /* Construct a single string, concatenating the returned client tokens
288 * together.
289 */
290 sql_data = (array_header *) sql_res->data;
291 vals = (char **) sql_data->elts;
292
293 if (sql_data->nelts < 1) {
294 wrap2_log("SQLNamedQuery '%s' returned no data; "
295 "see the mod_sql.c SQLLogFile for more details", query);
296 destroy_pool(tmp_pool);
297 return NULL;
298 }
299
300 options_list = make_array(sqltab->tab_pool, sql_data->nelts, sizeof(char *));
301 *((char **) push_array(options_list)) = pstrdup(sqltab->tab_pool, vals[0]);
302
303 if (sql_data->nelts > 1) {
304 register unsigned int i = 0;
305
306 for (i = 1; i < sql_data->nelts; i++) {
307 if (vals[i] == NULL) {
308 continue;
309 }
310
311 *((char **) push_array(options_list)) = pstrdup(sqltab->tab_pool,
312 vals[i]);
313 }
314 }
315
316 destroy_pool(tmp_pool);
317 return options_list;
318 }
319
320 static wrap2_table_t *sqltab_open_cb(pool *parent_pool, const char *srcinfo) {
321 wrap2_table_t *tab = NULL;
322 pool *tab_pool = make_sub_pool(parent_pool),
323 *tmp_pool = make_sub_pool(parent_pool);
324 config_rec *c = NULL;
325 char *start = NULL, *finish = NULL, *query = NULL, *clients_query = NULL,
326 *options_query = NULL, *info;
327
328 tab = (wrap2_table_t *) pcalloc(tab_pool, sizeof(wrap2_table_t));
329 tab->tab_pool = tab_pool;
330
331 /* Parse the SELECT query name out of the srcinfo string. Lookup and
332 * store the query in the tab_data area, so that it need not be looked
333 * up later.
334 *
335 * The srcinfo string for this case should look like:
336 * "/<clients-named-query>[/<options-named-query>]"
337 */
338
339 info = pstrdup(tmp_pool, srcinfo);
340 start = strchr(info, '/');
341 if (start == NULL) {
342 wrap2_log("error: badly formatted source info '%s'", srcinfo);
343 destroy_pool(tab_pool);
344 destroy_pool(tmp_pool);
345 errno = EINVAL;
346 return NULL;
347 }
348
349 /* Find the next slash. */
350 finish = strchr(++start, '/');
351 if (finish != NULL) {
352 *finish = '\0';
353 }
354
355 clients_query = pstrdup(tab->tab_pool, start);
356
357 /* Verify that the named query has indeed been defined. This is
358 * base on how mod_sql creates its config_rec names.
359 */
360 query = pstrcat(tmp_pool, "SQLNamedQuery_", clients_query, NULL);
361
362 c = find_config(main_server->conf, CONF_PARAM, query, FALSE);
363 if (c == NULL) {
364 wrap2_log("error: unable to resolve SQLNamedQuery name '%s'",
365 clients_query);
366 pr_log_pri(PR_LOG_WARNING, MOD_WRAP2_SQL_VERSION
367 ": no such SQLNamedQuery '%s' found, allowing connection", clients_query);
368
369 destroy_pool(tab_pool);
370 destroy_pool(tmp_pool);
371 errno = EINVAL;
372 return NULL;
373 }
374
375 /* Handle the options-query, if present. */
376 if (finish != NULL) {
377 options_query = pstrdup(tab->tab_pool, ++finish);
378
379 query = pstrcat(tmp_pool, "SQLNamedQuery_", options_query, NULL);
380
381 c = find_config(main_server->conf, CONF_PARAM, query, FALSE);
382 if (c == NULL) {
383 wrap2_log("error: unable to resolve SQLNamedQuery name '%s'",
384 options_query);
385 destroy_pool(tab_pool);
386 destroy_pool(tmp_pool);
387 errno = EINVAL;
388 return NULL;
389 }
390 }
391
392 tab->tab_name = pstrcat(tab->tab_pool, "SQL(", info, ")", NULL);
393
394 tab->tab_data = pcalloc(tab->tab_pool, WRAP2_SQL_NSLOTS * sizeof(char *));
395 ((char **) tab->tab_data)[WRAP2_SQL_CLIENT_QUERY_IDX] =
396 pstrdup(tab->tab_pool, clients_query);
397
398 ((char **) tab->tab_data)[WRAP2_SQL_OPTION_QUERY_IDX] =
399 (options_query ? pstrdup(tab->tab_pool, options_query) : NULL);
400
401 /* Set the necessary callbacks. */
402 tab->tab_close = sqltab_close_cb;
403 tab->tab_fetch_clients = sqltab_fetch_clients_cb;
404 tab->tab_fetch_daemons = sqltab_fetch_daemons_cb;
405 tab->tab_fetch_options = sqltab_fetch_options_cb;
406
407 destroy_pool(tmp_pool);
408 return tab;
409 }
410
411 /* Event handlers
412 */
413
414 #if defined(PR_SHARED_MODULE)
415 static void sqltab_mod_unload_ev(const void *event_data, void *user_data) {
416 if (strcmp("mod_wrap2_sql.c", (const char *) event_data) == 0) {
417 pr_event_unregister(&wrap2_sql_module, NULL, NULL);
418 wrap2_unregister("sql");
419 }
420 }
421 #endif /* PR_SHARED_MODULE */
422
423 /* Initialization routines
424 */
425
426 static int sqltab_init(void) {
427
428 /* Initialize the wrap source objects for type "sql". */
429 wrap2_register("sql", sqltab_open_cb);
430
431 #if defined(PR_SHARED_MODULE)
432 pr_event_register(&wrap2_sql_module, "core.module-unload",
433 sqltab_mod_unload_ev, NULL);
434 #endif /* PR_SHARED_MODULE */
435
436 return 0;
437 }
438
439 module wrap2_sql_module = {
440 NULL, NULL,
441
442 /* Module API version 2.0 */
443 0x20,
444
445 /* Module name */
446 "wrap2_sql",
447
448 /* Module configuration handler table */
449 NULL,
450
451 /* Module command handler table */
452 NULL,
453
454 /* Module authentication handler table */
455 NULL,
456
457 /* Module initialization function */
458 sqltab_init,
459
460 /* Session initialization function */
461 NULL,
462
463 /* Module version */
464 MOD_WRAP2_SQL_VERSION
465 };