Debian Codegraph @

contrib/mod_sftp_sql.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_sftp_sql -- SQL backend module for retrieving authorized keys
3 * Copyright (c) 2008-2024 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 and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 *
24 * This is mod_sftp_sql, contrib software for proftpd 1.3.x and above.
25 * For more information contact TJ Saunders <[email protected]>.
26 */
27
28 #include "conf.h"
29 #include "privs.h"
30 #include "mod_sftp.h"
31 #include "mod_sql.h"
32
33 #define MOD_SFTP_SQL_VERSION "mod_sftp_sql/0.4"
34
35 module sftp_sql_module;
36
37 #define SFTP_SQL_BUFSZ 1024
38
39 struct sqlstore_key {
40 const char *subject;
41
42 /* Optional headers */
43 pr_table_t *headers;
44
45 /* Key data */
46 unsigned char *key_data;
47 uint32_t key_datalen;
48 };
49
50 struct sqlstore_data {
51 const char *select_query;
52 };
53
54 static const char *sqlstore_user = NULL;
55
56 static const char *trace_channel = "sftp.sql";
57
58 static cmd_rec *sqlstore_cmd_create(pool *parent_pool, unsigned int argc, ...) {
59 register unsigned int i = 0;
60 pool *cmd_pool = NULL;
61 cmd_rec *cmd = NULL;
62 va_list argp;
63
64 cmd_pool = make_sub_pool(parent_pool);
65 cmd = (cmd_rec *) pcalloc(cmd_pool, sizeof(cmd_rec));
66 cmd->pool = cmd_pool;
67
68 cmd->argc = argc;
69 cmd->argv = pcalloc(cmd->pool, argc * sizeof(void *));
70
71 /* Hmmm... */
72 cmd->tmp_pool = cmd->pool;
73
74 va_start(argp, argc);
75 for (i = 0; i < argc; i++) {
76 cmd->argv[i] = va_arg(argp, char *);
77 }
78 va_end(argp);
79
80 return cmd;
81 }
82
83 /* Given a blob of bytes retrieved from a single row, read that blob as if
84 * it were text, line by line.
85 */
86 static char *sqlstore_getline(pool *p, char **blob, size_t *bloblen) {
87 char linebuf[SFTP_SQL_BUFSZ], *line = "", *data;
88 size_t datalen;
89
90 data = *blob;
91 datalen = *bloblen;
92
93 if (data == NULL ||
94 datalen == 0) {
95 errno = EOF;
96 return NULL;
97 }
98
99 while (data != NULL &&
100 datalen > 0) {
101 char *ptr;
102 size_t delimlen, linelen;
103 int have_line_continuation = FALSE;
104
105 pr_signals_handle();
106
107 if (datalen <= 2) {
108 line = pstrcat(p, line, data, NULL);
109
110 *blob = NULL;
111 *bloblen = 0;
112
113 return line;
114 }
115
116 /* Find the CRLF markers in the data. */
117 ptr = strstr(data, "\r\n");
118 if (ptr != NULL) {
119 delimlen = 1;
120
121 } else {
122 ptr = strstr(data, "\n");
123 if (ptr != NULL) {
124 delimlen = 0;
125 }
126 }
127
128 if (ptr == NULL) {
129 /* Just return the rest of the data. */
130 line = pstrcat(p, line, data, NULL);
131
132 *blob = NULL;
133 *bloblen = 0;
134
135 return line;
136 }
137
138 linelen = (ptr - data + 1);
139
140 if (linelen == 1) {
141 data += (delimlen + 1);
142 datalen -= (delimlen + 1);
143
144 continue;
145 }
146
147 /* Watch out for lines larger than our buffer. */
148 if (linelen > sizeof(linebuf)) {
149 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
150 "line of key data (%lu bytes) for '%s' exceeds buffer size, "
151 "truncating; this WILL cause authentication failures",
152 (unsigned long) linelen, sqlstore_user);
153 linelen = sizeof(linebuf);
154 }
155
156 memcpy(linebuf, data, linelen);
157 linebuf[linelen-1] = '\0';
158
159 data += (linelen + delimlen);
160 datalen -= (linelen + delimlen);
161
162 /* Check for continued lines. */
163 if (linelen >= 2 &&
164 linebuf[linelen-2] == '\\') {
165 linebuf[linelen-2] = '\0';
166 have_line_continuation = TRUE;
167 }
168
169 line = pstrcat(p, line, linebuf, NULL);
170 linelen = strlen(line);
171
172 if (have_line_continuation == TRUE) {
173 continue;
174 }
175
176 ptr = strchr(line, ':');
177 if (ptr != NULL) {
178 unsigned int header_taglen, header_valuelen;
179
180 /* We have a header. Make sure the header tag is not longer than
181 * the specified length of 64 bytes, and that the header value is
182 * not longer than 1024 bytes.
183 */
184 header_taglen = ptr - line;
185 if (header_taglen > 64) {
186 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
187 "header tag too long (%u) in retrieved SQL data for '%s'",
188 header_taglen, sqlstore_user);
189 errno = EINVAL;
190 return NULL;
191 }
192
193 /* Header value starts at 2 after the ':' (one for the mandatory
194 * space character. Make sure to check that we actually have text
195 * after the ':' character (see Issue #1529).
196 */
197 if (header_taglen + 2 < linelen) {
198 header_valuelen = linelen - (header_taglen + 2);
199 if (header_valuelen > 1024) {
200 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
201 "header value too long (%u) in retrieved SQL data for '%s'",
202 header_valuelen, sqlstore_user);
203 errno = EINVAL;
204 return NULL;
205 }
206
207 } else {
208 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
209 "empty/missing '%.*s' header value, ignoring", (int) header_taglen,
210 line);
211
212 /* Make sure we advance past this line. */
213 *blob = data;
214 *bloblen = datalen;
215
216 errno = EINVAL;
217 return NULL;
218 }
219 }
220
221 *blob = data;
222 *bloblen = datalen;
223
224 return line;
225 }
226
227 return NULL;
228 }
229
230 static struct sqlstore_key *sqlstore_alloc_key(pool *p) {
231 struct sqlstore_key *key = NULL;
232
233 key = pcalloc(p, sizeof(struct sqlstore_key));
234 key->headers = pr_table_nalloc(p, 0, 1);
235
236 return key;
237 }
238
239 static struct sqlstore_key *sqlstore_get_key_raw(pool *p, char **blob,
240 size_t *bloblen) {
241 char chunk[SFTP_SQL_BUFSZ], *data = NULL;
242 BIO *bio = NULL, *b64 = NULL, *bmem = NULL;
243 int chunklen;
244 long datalen = 0;
245 struct sqlstore_key *key = NULL;
246
247 bio = BIO_new(BIO_s_mem());
248
249 if (BIO_write(bio, (void *) *blob, *bloblen) < 0) {
250 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
251 "error buffering base64 data");
252 }
253
254 /* Add a base64 filter BIO, and read the data out, thus base64-decoding
255 * the key. Write the decoded data into another memory BIO.
256 */
257 b64 = BIO_new(BIO_f_base64());
258 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
259 bio = BIO_push(b64, bio);
260
261 bmem = BIO_new(BIO_s_mem());
262
263 memset(chunk, '\0', sizeof(chunk));
264 chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));
265
266 if (chunklen < 0 &&
267 !BIO_should_retry(bio)) {
268 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
269 "unable to base64-decode raw key data for '%s' from database: %s",
270 sqlstore_user, sftp_crypto_get_errors());
271 BIO_free_all(bio);
272 BIO_free_all(bmem);
273
274 errno = EPERM;
275 return NULL;
276 }
277
278 while (chunklen > 0) {
279 pr_signals_handle();
280
281 if (BIO_write(bmem, (void *) chunk, chunklen) < 0) {
282 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
283 "error writing to memory BIO: %s", sftp_crypto_get_errors());
284 BIO_free_all(bio);
285 BIO_free_all(bmem);
286
287 errno = EPERM;
288 return NULL;
289 }
290
291 memset(chunk, '\0', sizeof(chunk));
292 chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));
293 }
294
295 datalen = BIO_get_mem_data(bmem, &data);
296
297 if (data != NULL &&
298 datalen > 0) {
299 key = sqlstore_alloc_key(p);
300 key->key_data = pcalloc(p, datalen + 1);
301 key->key_datalen = datalen;
302 memcpy(key->key_data, data, datalen);
303
304 } else {
305 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
306 "error base64-decoding raw key data from database");
307 }
308
309 BIO_free_all(bio);
310 bio = NULL;
311
312 BIO_free_all(bmem);
313 return key;
314 }
315
316 static struct sqlstore_key *sqlstore_get_key_rfc4716(pool *p, char **blob,
317 size_t *bloblen) {
318 char *line;
319 BIO *bio = NULL;
320 struct sqlstore_key *key = NULL;
321 size_t begin_markerlen = 0, end_markerlen = 0;
322
323 line = sqlstore_getline(p, blob, bloblen);
324 while (line == NULL &&
325 errno == EINVAL) {
326 pr_signals_handle();
327 line = sqlstore_getline(p, blob, bloblen);
328 }
329
330 if (line == NULL) {
331 return NULL;
332 }
333
334 begin_markerlen = strlen(SFTP_SSH2_PUBKEY_BEGIN_MARKER);
335 end_markerlen = strlen(SFTP_SSH2_PUBKEY_END_MARKER);
336
337 while (line != NULL) {
338 pr_signals_handle();
339
340 if (key == NULL &&
341 strncmp(line, SFTP_SSH2_PUBKEY_BEGIN_MARKER, begin_markerlen) == 0) {
342 key = pcalloc(p, sizeof(struct sqlstore_key));
343 bio = BIO_new(BIO_s_mem());
344
345 } else if (key != NULL &&
346 strncmp(line, SFTP_SSH2_PUBKEY_END_MARKER, end_markerlen) == 0) {
347 if (bio != NULL) {
348 char chunk[SFTP_SQL_BUFSZ], *data = NULL;
349 BIO *b64 = NULL, *bmem = NULL;
350 int chunklen;
351 long datalen = 0;
352
353 /* Add a base64 filter BIO, and read the data out, thus base64-decoding
354 * the key. Write the decoded data into another memory BIO.
355 */
356 b64 = BIO_new(BIO_f_base64());
357 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
358 bio = BIO_push(b64, bio);
359
360 bmem = BIO_new(BIO_s_mem());
361
362 memset(chunk, '\0', sizeof(chunk));
363 chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));
364
365 if (chunklen < 0 &&
366 !BIO_should_retry(bio)) {
367 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
368 "unable to base64-decode RFC4716 key data for '%s' from "
369 "database: %s", sqlstore_user, sftp_crypto_get_errors());
370 BIO_free_all(bio);
371 BIO_free_all(bmem);
372
373 errno = EPERM;
374 return NULL;
375 }
376
377 while (chunklen > 0) {
378 pr_signals_handle();
379
380 if (BIO_write(bmem, (void *) chunk, chunklen) < 0) {
381 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
382 "error writing to memory BIO: %s", sftp_crypto_get_errors());
383 BIO_free_all(bio);
384 BIO_free_all(bmem);
385
386 errno = EPERM;
387 return NULL;
388 }
389
390 memset(chunk, '\0', sizeof(chunk));
391 chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));
392 }
393
394 datalen = BIO_get_mem_data(bmem, &data);
395
396 if (data != NULL &&
397 datalen > 0) {
398 key = pcalloc(p, sizeof(struct sqlstore_key));
399 key->key_data = pcalloc(p, datalen + 1);
400 key->key_datalen = datalen;
401 memcpy(key->key_data, data, datalen);
402
403 } else {
404 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
405 "error base64-decoding RFC4716 key data from database");
406 }
407
408 BIO_free_all(bio);
409 bio = NULL;
410
411 BIO_free_all(bmem);
412 }
413
414 break;
415
416 } else {
417 if (key != NULL) {
418 char *ptr;
419
420 ptr = strstr(line, ": ");
421 if (ptr != NULL) {
422 /* We have a header. */
423 char *tag, *text;
424
425 tag = pstrndup(p, line, ptr - line);
426 text = pstrdup(p, ptr + 2);
427 if (pr_table_add(key->headers, tag, text, 0) < 0) {
428 pr_trace_msg(trace_channel, 4,
429 "failed to add header '%s' to notes: %s", tag, strerror(errno));
430
431 } else {
432 pr_trace_msg(trace_channel, 22, "added header '%s: %s' to notes",
433 tag, text);
434 }
435
436 if (strcasecmp(tag, SFTP_KEYSTORE_HEADER_SUBJECT) == 0) {
437 key->subject = text;
438 }
439
440 } else {
441 if (BIO_write(bio, line, strlen(line)) < 0) {
442 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
443 "error buffering base64 data");
444 }
445 }
446 }
447 }
448
449 line = sqlstore_getline(p, blob, bloblen);
450 while (line == NULL &&
451 errno == EINVAL) {
452 pr_signals_handle();
453 line = sqlstore_getline(p, blob, bloblen);
454 }
455 }
456
457 return key;
458 }
459
460 static char *sqlstore_get_str(pool *p, char *str) {
461 cmdtable *cmdtab;
462 cmd_rec *cmd;
463 modret_t *res;
464
465 if (strlen(str) == 0)
466 return str;
467
468 /* Find the cmdtable for the sql_escapestr command. */
469 cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_escapestr", NULL, NULL, NULL);
470 if (cmdtab == NULL) {
471 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
472 "unable to find SQL hook symbol 'sql_escapestr'");
473 return str;
474 }
475
476 cmd = sqlstore_cmd_create(p, 1, pr_str_strip(p, str));
477
478 /* Call the handler. */
479 res = pr_module_call(cmdtab->m, cmdtab->handler, cmd);
480
481 /* Check the results. */
482 if (MODRET_ISERROR(res)) {
483 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
484 "error executing 'sql_escapestring'");
485 return str;
486 }
487
488 return res->data;
489 }
490
491 static int sqlstore_verify_key_raw(pool *p, struct sqlstore_data *store_data,
492 int nrow, char *col_data, size_t col_datalen, unsigned char *key_data,
493 uint32_t key_datalen) {
494 struct sqlstore_key *key;
495 int res, xerrno = EINVAL;
496
497 key = sqlstore_get_key_raw(p, &col_data, &col_datalen);
498 if (key == NULL) {
499 pr_trace_msg(trace_channel, 10,
500 "unable to parse data (row %u) as raw data", nrow+1);
501 errno = xerrno;
502 return -1;
503 }
504
505 res = sftp_keys_compare_keys(p, key_data, key_datalen, key->key_data,
506 key->key_datalen);
507 if (res < 0) {
508 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
509 "error comparing client-sent host key for '%s' with SQL data (row %u) "
510 "from SQLNamedQuery '%s': %s", sqlstore_user, nrow+1,
511 store_data->select_query, strerror(errno));
512
513 } else if (res == FALSE) {
514 pr_trace_msg(trace_channel, 3,
515 "client-sent host key for '%s' does not match SQL data (row %u) from "
516 "SQLNamedQuery '%s'", sqlstore_user, nrow+1, store_data->select_query);
517 res = -1;
518
519 /* Use EPERM here to indicate that we have a well-formatted key that
520 * simply did not match the key presented by the client.
521 */
522 xerrno = EPERM;
523
524 } else {
525 res = 0;
526 }
527
528 errno = xerrno;
529 return res;
530 }
531
532 static int sqlstore_verify_key_rfc4716(pool *p,
533 struct sqlstore_data *store_data, int nrow, char *col_data,
534 size_t col_datalen, unsigned char *key_data, uint32_t key_datalen,
535 pr_table_t *headers) {
536 struct sqlstore_key *key;
537 int xerrno = EINVAL;
538
539 key = sqlstore_get_key_rfc4716(p, &col_data, &col_datalen);
540 while (key != NULL) {
541 int res;
542
543 pr_signals_handle();
544
545 res = sftp_keys_compare_keys(p, key_data, key_datalen, key->key_data,
546 key->key_datalen);
547 if (res < 0) {
548 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
549 "error comparing client-sent key for '%s' with SQL data (row %u) from "
550 "SQLNamedQuery '%s': %s", sqlstore_user, nrow+1,
551 store_data->select_query, strerror(errno));
552 key = sqlstore_get_key_rfc4716(p, &col_data, &col_datalen);
553 continue;
554
555 } else if (res == FALSE) {
556 pr_trace_msg(trace_channel, 3,
557 "client-sent key for '%s' does not match SQL data (row %u) from "
558 "SQLNamedQuery '%s'", sqlstore_user, nrow+1, store_data->select_query);
559
560 /* Use EPERM here to indicate that we have a well-formatted key that
561 * simply did not match the key presented by the client.
562 */
563 xerrno = EPERM;
564
565 key = sqlstore_get_key_rfc4716(p, &col_data, &col_datalen);
566 continue;
567 }
568
569 if (pr_table_copy(headers, key->headers, 0) < 0) {
570 pr_trace_msg(trace_channel, 19, "error copying verify notes: %s",
571 strerror(errno));
572 }
573
574 return 0;
575 }
576
577 errno = xerrno;
578 return -1;
579 }
580
581 static int sqlstore_verify_host_key(sftp_keystore_t *store, pool *p,
582 const char *user, const char *host_fqdn, const char *host_user,
583 unsigned char *key_data, uint32_t key_datalen, pr_table_t *headers) {
584 register unsigned int i;
585 struct sqlstore_data *store_data;
586 pool *tmp_pool;
587 cmdtable *sql_cmdtab;
588 cmd_rec *sql_cmd;
589 modret_t *sql_res;
590 array_header *sql_data;
591 char **values;
592 int res;
593
594 store_data = store->keystore_data;
595
596 /* Find the cmdtable for the sql_lookup command. */
597 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
598 NULL);
599 if (sql_cmdtab == NULL) {
600 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
601 "unable to find SQL hook symbol 'sql_lookup'");
602 errno = EPERM;
603 return -1;
604 }
605
606 tmp_pool = make_sub_pool(store->keystore_pool);
607
608 /* Prepare the SELECT query. */
609 sql_cmd = sqlstore_cmd_create(tmp_pool, 3, "sql_lookup",
610 store_data->select_query, sqlstore_get_str(tmp_pool, (char *) host_fqdn));
611
612 /* Call the handler. */
613 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
614 if (sql_res == NULL ||
615 MODRET_ISERROR(sql_res)) {
616 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
617 "error processing SQLNamedQuery '%s'", store_data->select_query);
618 destroy_pool(tmp_pool);
619
620 errno = EPERM;
621 return -1;
622 }
623
624 sql_data = (array_header *) sql_res->data;
625
626 if (sql_data->nelts == 0) {
627 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
628 "SQLNamedQuery '%s' returned zero results", store_data->select_query);
629 destroy_pool(tmp_pool);
630 errno = ENOENT;
631 return -1;
632 }
633
634 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
635 "SQLNamedQuery '%s' returned %d %s", store_data->select_query,
636 sql_data->nelts, sql_data->nelts != 1 ? "rows" : "row");
637
638 values = (char **) sql_data->elts;
639 for (i = 0; i < sql_data->nelts; i++) {
640 char *col_data;
641 size_t col_datalen;
642
643 pr_signals_handle();
644
645 col_data = values[i];
646 if (col_data == NULL) {
647 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
648 "SQLNamedQuery '%s' returned NULL data", store_data->select_query);
649 destroy_pool(tmp_pool);
650 errno = EINVAL;
651 return -1;
652 }
653
654 col_datalen = strlen(values[i]);
655
656 res = sqlstore_verify_key_rfc4716(p, store_data, i, col_data, col_datalen,
657 key_data, key_datalen, headers);
658 if (res == 0) {
659 pr_trace_msg(trace_channel, 10, "found matching RFC4716 public key "
660 "(row %u) for host '%s' using SQLNamedQuery '%s'", i+1, host_fqdn,
661 store_data->select_query);
662 destroy_pool(tmp_pool);
663 return 0;
664 }
665
666 /* If verification as an RFC 4716 key failed with EINVAL, assume it is
667 * because the key did not actually use the RFC 4716 format. Otherwise,
668 * verification might have failed because of simple mismatching keys
669 * (wrong RSA key, or comparing RSA and EC keys).
670 */
671 if (errno == EINVAL) {
672 res = sqlstore_verify_key_raw(p, store_data, i, col_data, col_datalen,
673 key_data, key_datalen);
674 if (res == 0) {
675 pr_trace_msg(trace_channel, 10, "found matching public key (row %u) "
676 "for host '%s' using SQLNamedQuery '%s'", i+1, host_fqdn,
677 store_data->select_query);
678 destroy_pool(tmp_pool);
679 return 0;
680 }
681 }
682 }
683
684 destroy_pool(tmp_pool);
685 errno = ENOENT;
686 return -1;
687 }
688
689 static int sqlstore_verify_user_key(sftp_keystore_t *store, pool *p,
690 const char *user, unsigned char *key_data, uint32_t key_datalen,
691 pr_table_t *headers) {
692 register unsigned int i;
693 struct sqlstore_data *store_data;
694 pool *tmp_pool;
695 cmdtable *sql_cmdtab;
696 cmd_rec *sql_cmd;
697 modret_t *sql_res;
698 array_header *sql_data;
699 char **values;
700
701 store_data = store->keystore_data;
702
703 /* Find the cmdtable for the sql_lookup command. */
704 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
705 NULL);
706 if (sql_cmdtab == NULL) {
707 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
708 "unable to find SQL hook symbol 'sql_lookup'");
709 errno = EPERM;
710 return -1;
711 }
712
713 tmp_pool = make_sub_pool(store->keystore_pool);
714
715 /* Prepare the SELECT query. */
716 sql_cmd = sqlstore_cmd_create(tmp_pool, 3, "sql_lookup",
717 store_data->select_query, sqlstore_get_str(tmp_pool, (char *) user));
718
719 /* Call the handler. */
720 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
721 if (sql_res == NULL ||
722 MODRET_ISERROR(sql_res)) {
723 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
724 "error processing SQLNamedQuery '%s'", store_data->select_query);
725 destroy_pool(tmp_pool);
726
727 errno = EPERM;
728 return -1;
729 }
730
731 sql_data = (array_header *) sql_res->data;
732
733 if (sql_data->nelts == 0) {
734 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
735 "SQLNamedQuery '%s' returned zero results", store_data->select_query);
736 destroy_pool(tmp_pool);
737 errno = ENOENT;
738 return -1;
739 }
740
741 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
742 "SQLNamedQuery '%s' returned %d %s", store_data->select_query,
743 sql_data->nelts, sql_data->nelts != 1 ? "rows" : "row");
744
745 values = (char **) sql_data->elts;
746 for (i = 0; i < sql_data->nelts; i++) {
747 int res;
748 char *col_data;
749 size_t col_datalen;
750
751 pr_signals_handle();
752
753 col_data = values[i];
754 if (col_data == NULL) {
755 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
756 "SQLNamedQuery '%s' returned NULL data", store_data->select_query);
757 destroy_pool(tmp_pool);
758 errno = EINVAL;
759 return -1;
760 }
761
762 col_datalen = strlen(values[i]);
763
764 res = sqlstore_verify_key_rfc4716(p, store_data, i, col_data, col_datalen,
765 key_data, key_datalen, headers);
766 if (res == 0) {
767 pr_trace_msg(trace_channel, 10, "found matching RFC4716 public key "
768 "(row %u) for user '%s' using SQLNamedQuery '%s'", i+1, user,
769 store_data->select_query);
770 destroy_pool(tmp_pool);
771 return 0;
772 }
773
774 /* If verification as an RFC 4716 key failed with EINVAL, assume it is
775 * because the key did not actually use the RFC 4716 format. Otherwise,
776 * verification might have failed because of simple mismatching keys
777 * (wrong RSA key, or comparing RSA and EC keys).
778 */
779 if (errno == EINVAL) {
780 res = sqlstore_verify_key_raw(p, store_data, i, col_data, col_datalen,
781 key_data, key_datalen);
782 if (res == 0) {
783 pr_trace_msg(trace_channel, 10, "found matching public key (row %u) "
784 "for user '%s' using SQLNamedQuery '%s'", i+1, user,
785 store_data->select_query);
786 destroy_pool(tmp_pool);
787 return 0;
788 }
789 }
790 }
791
792 destroy_pool(tmp_pool);
793 errno = ENOENT;
794 return -1;
795 }
796
797 static int sqlstore_close(sftp_keystore_t *store) {
798 sqlstore_user = NULL;
799 return 0;
800 }
801
802 static sftp_keystore_t *sqlstore_open(pool *parent_pool,
803 int requested_key_type, const char *store_info, const char *user) {
804 sftp_keystore_t *store;
805 pool *sqlstore_pool, *tmp_pool;
806 struct sqlstore_data *store_data;
807 char *named_query, *select_query, *ptr;
808 config_rec *c;
809
810 tmp_pool = make_sub_pool(parent_pool);
811
812 sqlstore_pool = make_sub_pool(parent_pool);
813 pr_pool_tag(sqlstore_pool, "SFTP SQL-based Keystore Pool");
814
815 sqlstore_user = pstrdup(sqlstore_pool, user);
816
817 store = pcalloc(sqlstore_pool, sizeof(sftp_keystore_t));
818 store->keystore_pool = sqlstore_pool;
819 store->store_ktypes = requested_key_type;
820
821 switch (requested_key_type) {
822 case SFTP_SSH2_HOST_KEY_STORE:
823 store->verify_host_key = sqlstore_verify_host_key;
824 break;
825
826 case SFTP_SSH2_USER_KEY_STORE:
827 store->verify_user_key = sqlstore_verify_user_key;
828 break;
829 }
830
831 store->store_close = sqlstore_close;
832
833 /* Parse the SELECT query name out of the store_info string:
834 *
835 * "/<select-named-query"
836 */
837 ptr = strchr(store_info, '/');
838 if (ptr == NULL) {
839 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_SQL_VERSION,
840 "badly formatted store info '%s'", store_info);
841 destroy_pool(tmp_pool);
842
843 errno = EINVAL;
844 return NULL;
845 }
846
847 ptr++;
848 select_query = pstrdup(sqlstore_pool, ptr);
849
850 /* Verify that the named query has indeed been configured. This is based
851 * on how mod_sql creates its config_rec names.
852 */
853 named_query = pstrcat(tmp_pool, "SQLNamedQuery_", select_query, NULL);
854
855 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
856 if (c == NULL) {
857 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
858 "unable to resolve SQLNamedQuery '%s'", select_query);
859 destroy_pool(tmp_pool);
860
861 errno = EINVAL;
862 return NULL;
863 }
864
865 store_data = pcalloc(sqlstore_pool, sizeof(struct sqlstore_data));
866 store->keystore_data = store_data;
867 store_data->select_query = pstrdup(sqlstore_pool, select_query);
868
869 destroy_pool(tmp_pool);
870 return store;
871 }
872
873 /* Event Handlers
874 */
875
876 #if defined(PR_SHARED_MODULE)
877 static void sftpsql_mod_unload_ev(const void *event_data, void *user_data) {
878 if (strcmp("mod_sftp_sql.c", (const char *) event_data) != 0) {
879 return;
880 }
881
882 sqlstore_user = NULL;
883 sftp_keystore_unregister_store("sql",
884 SFTP_SSH2_HOST_KEY_STORE|SFTP_SSH2_USER_KEY_STORE);
885 pr_event_unregister(&sftp_sql_module, NULL, NULL);
886 }
887 #endif /* !PR_SHARED_MODULE */
888
889 /* Initialization functions
890 */
891
892 static int sftpsql_init(void) {
893 sftp_keystore_register_store("sql", sqlstore_open,
894 SFTP_SSH2_HOST_KEY_STORE|SFTP_SSH2_USER_KEY_STORE);
895
896 #if defined(PR_SHARED_MODULE)
897 pr_event_register(&sftp_sql_module, "core.module-unload",
898 sftpsql_mod_unload_ev, NULL);
899 #endif /* !PR_SHARED_MODULE */
900
901 return 0;
902 }
903
904 module sftp_sql_module = {
905 NULL, NULL,
906
907 /* Module API version 2.0 */
908 0x20,
909
910 /* Module name */
911 "sftp_sql",
912
913 /* Module configuration handler table */
914 NULL,
915
916 /* Module command handler table */
917 NULL,
918
919 /* Module authentication handler table */
920 NULL,
921
922 /* Module initialization function */
923 sftpsql_init,
924
925 /* Session initialization function */
926 NULL,
927
928 /* Module version */
929 MOD_SFTP_SQL_VERSION
930 };