| 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 |
#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 |
|
| 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 |
|
| 73 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 133 |
sql_cmd = sql_cmd_create(tmp_pool, 3, "sql_lookup", query, escaped_name); |
| 134 |
|
| 135 |
|
| 136 |
sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd); |
| 137 |
|
| 138 |
|
| 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 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 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 |
|
| 194 |
if (word[wordlen-1] == ',') { |
| 195 |
word[wordlen-1] = '\0'; |
| 196 |
wordlen--; |
| 197 |
} |
| 198 |
|
| 199 |
*((char **) push_array(clients_list)) = word; |
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 251 |
if (query == NULL) { |
| 252 |
destroy_pool(tmp_pool); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 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 |
|
| 267 |
sql_cmd = sql_cmd_create(tmp_pool, 3, "sql_lookup", query, escaped_name); |
| 268 |
|
| 269 |
|
| 270 |
sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd); |
| 271 |
|
| 272 |
|
| 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 |
|
| 288 |
|
| 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 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 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 |
|
| 350 |
finish = strchr(++start, '/'); |
| 351 |
if (finish != NULL) { |
| 352 |
*finish = '\0'; |
| 353 |
} |
| 354 |
|
| 355 |
clients_query = pstrdup(tab->tab_pool, start); |
| 356 |
|
| 357 |
|
| 358 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static int sqltab_init(void) { |
| 427 |
|
| 428 |
|
| 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 |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
module wrap2_sql_module = { |
| 440 |
NULL, NULL, |
| 441 |
|
| 442 |
|
| 443 |
0x20, |
| 444 |
|
| 445 |
|
| 446 |
"wrap2_sql", |
| 447 |
|
| 448 |
|
| 449 |
NULL, |
| 450 |
|
| 451 |
|
| 452 |
NULL, |
| 453 |
|
| 454 |
|
| 455 |
NULL, |
| 456 |
|
| 457 |
|
| 458 |
sqltab_init, |
| 459 |
|
| 460 |
|
| 461 |
NULL, |
| 462 |
|
| 463 |
|
| 464 |
MOD_WRAP2_SQL_VERSION |
| 465 |
}; |