Debian Codegraph @

contrib/mod_sql_passwd.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_sql_passwd -- Various SQL password handlers
3 * Copyright (c) 2009-2025 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
22 * the source distribution.
23 *
24 * -----DO NOT EDIT BELOW THIS LINE-----
25 * $Libraries: -lcrypto$
26 */
27
28 #include "conf.h"
29 #include "privs.h"
30 #include "mod_sql.h"
31
32 #define MOD_SQL_PASSWD_VERSION "mod_sql_passwd/1.2"
33
34 #if defined(PR_USE_SODIUM)
35 # include <sodium.h>
36 /* Use/support Argon2, if libsodium is new enough. */
37 # if SODIUM_LIBRARY_VERSION_MAJOR > 9 || \
38 (SODIUM_LIBRARY_VERSION_MAJOR == 9 && \
39 SODIUM_LIBRARY_VERSION_MINOR >= 2)
40 # define USE_SODIUM_ARGON2
41 # endif
42 #endif /* PR_USE_SODIUM */
43
44 /* Make sure the version of proftpd is as necessary. */
45 #if PROFTPD_VERSION_NUMBER < 0x0001030703
46 # error "ProFTPD 1.3.7rc3 or later required"
47 #endif
48
49 #if !defined(HAVE_OPENSSL) && !defined(PR_USE_OPENSSL)
50 # error "OpenSSL support required (--enable-openssl)"
51 #else
52 # include <openssl/evp.h>
53 # include <openssl/err.h>
54 # include <openssl/objects.h>
55 #endif
56
57 /* Define if you have the LibreSSL library. */
58 #if defined(LIBRESSL_VERSION_NUMBER)
59 # define HAVE_LIBRESSL 1
60 #endif
61
62 module sql_passwd_module;
63
64 static int sql_passwd_engine = FALSE;
65
66 #define SQL_PASSWD_COST_INTERACTIVE 1
67 #define SQL_PASSWD_COST_SENSITIVE 2
68 static unsigned int sql_passwd_cost = SQL_PASSWD_COST_INTERACTIVE;
69
70 #define SQL_PASSWD_ENC_USE_BASE64 1
71 #define SQL_PASSWD_ENC_USE_HEX_LC 2
72 #define SQL_PASSWD_ENC_USE_HEX_UC 3
73 #define SQL_PASSWD_ENC_USE_NONE 4
74 static unsigned int sql_passwd_encoding = SQL_PASSWD_ENC_USE_HEX_LC;
75 static unsigned int sql_passwd_salt_encoding = SQL_PASSWD_ENC_USE_NONE;
76
77 static unsigned char *sql_passwd_file_salt = NULL;
78 static size_t sql_passwd_file_salt_len = 0;
79 static unsigned char *sql_passwd_user_salt = NULL;
80 static size_t sql_passwd_user_salt_len = 0;
81
82 #define SQL_PASSWD_SALT_FL_APPEND 0x0001
83 #define SQL_PASSWD_SALT_FL_PREPEND 0x0002
84 static unsigned long sql_passwd_file_salt_flags = SQL_PASSWD_SALT_FL_APPEND;
85 static unsigned long sql_passwd_user_salt_flags = SQL_PASSWD_SALT_FL_APPEND;
86
87 #define SQL_PASSWD_OPT_HASH_SALT 0x0001
88 #define SQL_PASSWD_OPT_ENCODE_SALT 0x0002
89 #define SQL_PASSWD_OPT_HASH_PASSWORD 0x0004
90 #define SQL_PASSWD_OPT_ENCODE_PASSWORD 0x0008
91
92 static unsigned long sql_passwd_opts = 0UL;
93
94 static unsigned long sql_passwd_nrounds = 1;
95
96 /* For PBKDF2 */
97 static const EVP_MD *sql_passwd_pbkdf2_digest = NULL;
98 static int sql_passwd_pbkdf2_iter = -1;
99 static int sql_passwd_pbkdf2_len = -1;
100 #define SQL_PASSWD_ERR_PBKDF2_UNKNOWN_DIGEST -1
101 #define SQL_PASSWD_ERR_PBKDF2_UNSUPPORTED_DIGEST -2
102 #define SQL_PASSWD_ERR_PBKDF2_BAD_ROUNDS -3
103 #define SQL_PASSWD_ERR_PBKDF2_BAD_LENGTH -4
104
105 #if defined(PR_USE_SODIUM)
106 /* For Scrypt */
107 # define SQL_PASSWD_SCRYPT_DEFAULT_HASH_SIZE 32U
108 # define SQL_PASSWD_SCRYPT_DEFAULT_SALT_SIZE 32U
109 static unsigned int sql_passwd_scrypt_hash_len = SQL_PASSWD_SCRYPT_DEFAULT_HASH_SIZE;
110
111 /* For Argon2 */
112 # if defined(USE_SODIUM_ARGON2)
113 # define SQL_PASSWD_ARGON2_DEFAULT_HASH_SIZE 32U
114 # define SQL_PASSWD_ARGON2_DEFAULT_SALT_SIZE 16U
115 static unsigned int sql_passwd_argon2_hash_len = SQL_PASSWD_ARGON2_DEFAULT_HASH_SIZE;
116 # endif /* USE_SODIUM_ARGON2 */
117 #endif /* PR_USE_SODIUM */
118
119 static const char *trace_channel = "sql.passwd";
120
121 /* Necessary prototypes */
122 static int sql_passwd_sess_init(void);
123
124 static cmd_rec *sql_passwd_cmd_create(pool *parent_pool,
125 unsigned int argc, ...) {
126 register unsigned int i = 0;
127 pool *cmd_pool = NULL;
128 cmd_rec *cmd = NULL;
129 va_list argp;
130
131 cmd_pool = make_sub_pool(parent_pool);
132 cmd = (cmd_rec *) pcalloc(cmd_pool, sizeof(cmd_rec));
133 cmd->pool = cmd_pool;
134
135 cmd->argc = argc;
136 cmd->argv = pcalloc(cmd->pool, argc * sizeof(void *));
137
138 /* Hmmm... */
139 cmd->tmp_pool = cmd->pool;
140
141 va_start(argp, argc);
142 for (i = 0; i < argc; i++) {
143 cmd->argv[i] = va_arg(argp, char *);
144 }
145 va_end(argp);
146
147 return cmd;
148 }
149
150 static const char *sql_passwd_get_str(pool *p, const char *str) {
151 cmdtable *cmdtab;
152 cmd_rec *cmd;
153 modret_t *res;
154
155 if (strlen(str) == 0) {
156 return str;
157 }
158
159 /* Find the cmdtable for the sql_escapestr command. */
160 cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_escapestr", NULL, NULL, NULL);
161 if (cmdtab == NULL) {
162 pr_log_debug(DEBUG2, MOD_SQL_PASSWD_VERSION
163 ": unable to find SQL hook symbol 'sql_escapestr'");
164 return str;
165 }
166
167 cmd = sql_passwd_cmd_create(p, 1, pr_str_strip(p, str));
168
169 /* Call the handler. */
170 res = pr_module_call(cmdtab->m, cmdtab->handler, cmd);
171
172 /* Check the results. */
173 if (MODRET_ISDECLINED(res) ||
174 MODRET_ISERROR(res)) {
175 pr_log_debug(DEBUG0, MOD_SQL_PASSWD_VERSION
176 ": error executing 'sql_escapestring'");
177 return str;
178 }
179
180 return res->data;
181 }
182
183 static const char *get_crypto_errors(void) {
184 unsigned int count = 0;
185 unsigned long error_code;
186 BIO *bio = NULL;
187 char *data = NULL;
188 long datalen;
189 const char *error_data = NULL, *str = "(unknown)";
190 int error_flags = 0;
191
192 /* Use ERR_print_errors() and a memory BIO to build up a string with
193 * all of the error messages from the error queue.
194 */
195
196 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
197 if (error_code) {
198 bio = BIO_new(BIO_s_mem());
199 }
200
201 while (error_code) {
202 pr_signals_handle();
203
204 if (error_flags & ERR_TXT_STRING) {
205 BIO_printf(bio, "\n (%u) %s [%s]", ++count,
206 ERR_error_string(error_code, NULL), error_data);
207
208 } else {
209 BIO_printf(bio, "\n (%u) %s", ++count,
210 ERR_error_string(error_code, NULL));
211 }
212
213 error_data = NULL;
214 error_flags = 0;
215 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
216 }
217
218 datalen = BIO_get_mem_data(bio, &data);
219 if (data) {
220 data[datalen] = '\0';
221 str = pstrdup(session.pool, data);
222 }
223
224 if (bio) {
225 BIO_free(bio);
226 }
227
228 return str;
229 }
230
231 static int get_pbkdf2_config(char *algo, const EVP_MD **md,
232 char *iter_str, int *iter, char *len_str, int *len) {
233
234 *md = EVP_get_digestbyname(algo);
235 if (*md == NULL) {
236 return SQL_PASSWD_ERR_PBKDF2_UNKNOWN_DIGEST;
237 }
238
239 #if OPENSSL_VERSION_NUMBER < 0x1000003f
240 /* The necessary OpenSSL support for non-SHA1 digests for PBKDF2 appeared in
241 * 1.0.0c.
242 */
243 if (EVP_MD_type(*md) != EVP_MD_type(EVP_sha1())) {
244 return SQL_PASSWD_ERR_PBKDF2_UNSUPPORTED_DIGEST;
245 }
246 #endif /* OpenSSL-1.0.0b and earlier */
247
248 *iter = atoi(iter_str);
249 if (*iter < 1) {
250 return SQL_PASSWD_ERR_PBKDF2_BAD_ROUNDS;
251 }
252
253 *len = atoi(len_str);
254 if (*len < 1) {
255 return SQL_PASSWD_ERR_PBKDF2_BAD_LENGTH;
256 }
257
258 return 0;
259 }
260
261 static unsigned char *sql_passwd_decode(pool *p, unsigned int encoding,
262 char *text, size_t text_len, size_t *data_len) {
263 unsigned char *data = NULL;
264
265 switch (encoding) {
266 case SQL_PASSWD_ENC_USE_NONE:
267 *data_len = text_len;
268 data = (unsigned char *) pstrndup(p, text, text_len);
269 break;
270
271 case SQL_PASSWD_ENC_USE_BASE64: {
272 int have_padding = FALSE, res;
273
274 /* Due to Base64's padding, we need to detect if the last block was
275 * padded with zeros; we do this by looking for '=' characters at the
276 * end of the text being decoded. If we see these characters, then we
277 * will "trim" off any trailing zero values in the decoded data, on the
278 * ASSUMPTION that they are the auto-added padding bytes.
279 */
280 if (text[text_len-1] == '=') {
281 have_padding = TRUE;
282 }
283
284 data = pcalloc(p, text_len);
285 res = EVP_DecodeBlock((unsigned char *) data, (unsigned char *) text,
286 (int) text_len);
287 if (res <= 0) {
288 /* Base64-decoding error. */
289 errno = EINVAL;
290 return NULL;
291 }
292
293 if (have_padding) {
294 /* Assume that only one or two zero bytes of padding were added. */
295 if (data[res-1] == '\0') {
296 res -= 1;
297
298 if (data[res-1] == '\0') {
299 res -= 1;
300 }
301 }
302 }
303
304 *data_len = (size_t) res;
305 break;
306 }
307
308 case SQL_PASSWD_ENC_USE_HEX_LC: {
309 register unsigned int i, j;
310 unsigned int len = 0;
311
312 data = pcalloc(p, text_len);
313 for (i = 0, j = 0; i < text_len; i += 2) {
314 int res;
315
316 res = sscanf(text + i, "%02hhx", &(data[j++]));
317 if (res == 0) {
318 /* hex decoding error. */
319 errno = EINVAL;
320 return NULL;
321 }
322
323 len += res;
324 }
325
326 *data_len = len;
327 break;
328 }
329
330 case SQL_PASSWD_ENC_USE_HEX_UC: {
331 register unsigned int i, j;
332 unsigned int len = 0;
333
334 data = pcalloc(p, text_len);
335 for (i = 0, j = 0; i < text_len; i += 2) {
336 int res;
337
338 res = sscanf(text + i, "%02hhX", &(data[j++]));
339 if (res == 0) {
340 /* hex decoding error. */
341 errno = EINVAL;
342 return NULL;
343 }
344
345 len += res;
346 }
347
348 *data_len = len;
349 break;
350 }
351
352 default:
353 errno = EPERM;
354 return NULL;
355 }
356
357 return data;
358 }
359
360 static char *sql_passwd_encode(pool *p, unsigned int encoding,
361 unsigned char *data, size_t data_len) {
362 char *buf = NULL;
363
364 switch (encoding) {
365 case SQL_PASSWD_ENC_USE_BASE64: {
366 /* According to RATS, the output buffer for EVP_EncodeBlock() needs to be
367 * 4/3 the size of the input buffer (which is usually EVP_MAX_MD_SIZE).
368 * Let's make it easy, and use an output buffer that's twice the size of
369 * the input buffer.
370 */
371 buf = pcalloc(p, (2 * data_len) + 1);
372 EVP_EncodeBlock((unsigned char *) buf, data, (int) data_len);
373 break;
374 }
375
376 case SQL_PASSWD_ENC_USE_HEX_LC: {
377 buf = pr_str_bin2hex(p, data, data_len, PR_STR_FL_HEX_USE_LC);
378 break;
379 }
380
381 case SQL_PASSWD_ENC_USE_HEX_UC: {
382 buf = pr_str_bin2hex(p, data, data_len, PR_STR_FL_HEX_USE_UC);
383 break;
384 }
385
386 default:
387 errno = EPERM;
388 return NULL;
389 }
390
391 return buf;
392 }
393
394 /* This may look a little weird, with the data, prefix, and suffix arguments.
395 * But they are used to handle the case where we are hashing data with
396 * a salt (either as a prefix or as a suffix), and where we are hashing
397 * already hashed data.
398 */
399 static unsigned char *sql_passwd_hash(pool *p, const EVP_MD *md,
400 unsigned char *data, size_t data_len,
401 unsigned char *prefix, size_t prefix_len,
402 unsigned char *suffix, size_t suffix_len,
403 unsigned int *hash_len) {
404
405 EVP_MD_CTX *md_ctx;
406 unsigned char *hash;
407
408 hash = palloc(p, EVP_MAX_MD_SIZE);
409
410 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
411 * int. Without these ugly OpenSSL version preprocessor checks, the
412 * compiler will error out with "void value not ignored as it ought to be".
413 */
414
415 md_ctx = EVP_MD_CTX_create();
416 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
417 if (EVP_DigestInit(md_ctx, md) != 1) {
418 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
419 ": error initializing '%s' digest: %s", OBJ_nid2ln(EVP_MD_type(md)),
420 get_crypto_errors());
421 EVP_MD_CTX_destroy(md_ctx);
422 errno = EPERM;
423 return NULL;
424 }
425 #else
426 EVP_DigestInit(md_ctx, md);
427 #endif
428
429 if (prefix != NULL) {
430 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
431 if (EVP_DigestUpdate(md_ctx, prefix, prefix_len) != 1) {
432 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
433 ": error updating '%s' digest: %s", OBJ_nid2ln(EVP_MD_type(md)),
434 get_crypto_errors());
435 EVP_MD_CTX_destroy(md_ctx);
436 errno = EPERM;
437 return NULL;
438 }
439 #else
440 EVP_DigestUpdate(md_ctx, prefix, prefix_len);
441 #endif
442 }
443
444 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
445 if (EVP_DigestUpdate(md_ctx, data, data_len) != 1) {
446 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
447 ": error updating '%s' digest: %s", OBJ_nid2ln(EVP_MD_type(md)),
448 get_crypto_errors());
449 EVP_MD_CTX_destroy(md_ctx);
450 errno = EPERM;
451 return NULL;
452 }
453 #else
454 EVP_DigestUpdate(md_ctx, data, data_len);
455 #endif
456
457 if (suffix != NULL) {
458 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
459 if (EVP_DigestUpdate(md_ctx, suffix, suffix_len) != 1) {
460 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
461 ": error updating '%s' digest: %s", OBJ_nid2ln(EVP_MD_type(md)),
462 get_crypto_errors());
463 EVP_MD_CTX_destroy(md_ctx);
464 errno = EPERM;
465 return NULL;
466 }
467 #else
468 EVP_DigestUpdate(md_ctx, suffix, suffix_len);
469 #endif
470 }
471
472 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
473 if (EVP_DigestFinal(md_ctx, hash, hash_len) != 1) {
474 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
475 ": error finishing '%s' digest: %s", OBJ_nid2ln(EVP_MD_type(md)),
476 get_crypto_errors());
477 EVP_MD_CTX_destroy(md_ctx);
478 errno = EPERM;
479 return NULL;
480 }
481 #else
482 EVP_DigestFinal(md_ctx, hash, hash_len);
483 #endif
484 EVP_MD_CTX_destroy(md_ctx);
485
486 return hash;
487 }
488
489 #if !defined(HAVE_TIMINGSAFE_BCMP)
490 static int timingsafe_bcmp(const void *b1, const void *b2, size_t n) {
491 const unsigned char *p1 = b1, *p2 = b2;
492 int ret = 0;
493
494 for (; n > 0; n--) {
495 ret |= *p1++ ^ *p2++;
496 }
497
498 return (ret != 0);
499 }
500 #endif /* HAVE_TIMINGSAFE_BCMP */
501
502 static modret_t *sql_passwd_auth(cmd_rec *cmd, const char *plaintext,
503 const char *ciphertext, const char *digest) {
504 const EVP_MD *md;
505 unsigned char *hash = NULL, *data = NULL, *prefix = NULL, *suffix = NULL;
506 size_t data_len = 0, prefix_len = 0, suffix_len = 0;
507 unsigned int cmp_len = 0, copytext_len = 0, hash_len = 0;
508
509 /* Temporary copy of the ciphertext string */
510 char *copytext;
511 const char *encodedtext;
512
513 if (sql_passwd_engine == FALSE) {
514 pr_log_pri(PR_LOG_INFO, MOD_SQL_PASSWD_VERSION
515 ": SQLPasswordEngine disabled; unable to handle %s SQLAuthType", digest);
516 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
517 ": SQLPasswordEngine disabled; unable to handle %s SQLAuthType", digest);
518 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
519 }
520
521 /* Reject stored empty password fields (Issue #2275). */
522 if (*ciphertext == '\0') {
523 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
524 ": ignoring empty password field");
525 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
526 }
527
528 md = EVP_get_digestbyname(digest);
529 if (md == NULL) {
530 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
531 ": no such digest '%s' supported", digest);
532 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
533 }
534
535 /* We need a copy of the ciphertext. */
536 copytext = pstrdup(cmd->tmp_pool, ciphertext);
537
538 /* If a salt is configured, do we prepend the salt as a prefix (i.e. throw
539 * it into the digest before the user-supplied password) or append it as a
540 * suffix?
541 */
542
543 if (sql_passwd_file_salt_len > 0 &&
544 (sql_passwd_file_salt_flags & SQL_PASSWD_SALT_FL_PREPEND)) {
545
546 /* If we have salt data, add it to the mix. */
547
548 if (!(sql_passwd_opts & SQL_PASSWD_OPT_HASH_SALT)) {
549 prefix = (unsigned char *) sql_passwd_file_salt;
550 prefix_len = sql_passwd_file_salt_len;
551
552 pr_trace_msg(trace_channel, 9,
553 "prepending %lu bytes of file salt data", (unsigned long) prefix_len);
554
555 } else {
556 unsigned int salt_hashlen = 0;
557
558 prefix = sql_passwd_hash(cmd->tmp_pool, md,
559 (unsigned char *) sql_passwd_file_salt, sql_passwd_file_salt_len,
560 NULL, 0, NULL, 0, &salt_hashlen);
561 prefix_len = salt_hashlen;
562
563 if (sql_passwd_opts & SQL_PASSWD_OPT_ENCODE_SALT) {
564 prefix = (unsigned char *) sql_passwd_encode(cmd->tmp_pool,
565 sql_passwd_encoding, (unsigned char *) prefix, prefix_len);
566 prefix_len = strlen((char *) prefix);
567 }
568
569 pr_trace_msg(trace_channel, 9,
570 "prepending %lu bytes of %s-hashed file salt data (%s)",
571 (unsigned long) prefix_len, digest, prefix);
572 }
573 }
574
575 if (sql_passwd_user_salt_len > 0 &&
576 (sql_passwd_user_salt_flags & SQL_PASSWD_SALT_FL_PREPEND)) {
577
578 /* If we have user salt data, add it to the mix. */
579
580 if (!(sql_passwd_opts & SQL_PASSWD_OPT_HASH_SALT)) {
581 prefix = (unsigned char *) sql_passwd_user_salt;
582 prefix_len = sql_passwd_user_salt_len;
583
584 pr_trace_msg(trace_channel, 9,
585 "prepending %lu bytes of user salt data", (unsigned long) prefix_len);
586
587 } else {
588 unsigned int salt_hashlen = 0;
589
590 prefix = sql_passwd_hash(cmd->tmp_pool, md,
591 (unsigned char *) sql_passwd_user_salt, sql_passwd_user_salt_len,
592 NULL, 0, NULL, 0, &salt_hashlen);
593 prefix_len = salt_hashlen;
594
595 if (sql_passwd_opts & SQL_PASSWD_OPT_ENCODE_SALT) {
596 prefix = (unsigned char *) sql_passwd_encode(cmd->tmp_pool,
597 sql_passwd_encoding, (unsigned char *) prefix, prefix_len);
598 prefix_len = strlen((char *) prefix);
599 }
600
601 pr_trace_msg(trace_channel, 9,
602 "prepending %lu bytes of %s-hashed user salt data (%s)",
603 (unsigned long) prefix_len, digest, prefix);
604 }
605 }
606
607 if (!(sql_passwd_opts & SQL_PASSWD_OPT_HASH_PASSWORD)) {
608 data = (unsigned char *) plaintext;
609 data_len = strlen(plaintext);
610
611 } else {
612 /* Note: We will only honor a HashEncodePassword option IFF there is
613 * also salt data present. Otherwise, it is equivalent to another
614 * round of processing, which defeats the principle of least surprise.
615 */
616 if ((sql_passwd_file_salt_len == 0 &&
617 sql_passwd_user_salt_len == 0) &&
618 (sql_passwd_opts & SQL_PASSWD_OPT_HASH_PASSWORD) &&
619 (sql_passwd_opts & SQL_PASSWD_OPT_ENCODE_PASSWORD)) {
620 pr_trace_msg(trace_channel, 4, "%s",
621 "no salt present, ignoring HashEncodePassword SQLPasswordOption");
622 data = (unsigned char *) plaintext;
623 data_len = strlen(plaintext);
624
625 } else {
626 unsigned int salt_hashlen = 0;
627
628 data = sql_passwd_hash(cmd->tmp_pool, md,
629 (unsigned char *) plaintext, strlen(plaintext),
630 NULL, 0, NULL, 0, &salt_hashlen);
631 data_len = salt_hashlen;
632
633 if (sql_passwd_opts & SQL_PASSWD_OPT_ENCODE_PASSWORD) {
634 data = (unsigned char *) sql_passwd_encode(cmd->tmp_pool,
635 sql_passwd_encoding, (unsigned char *) data, data_len);
636 data_len = strlen((char *) data);
637 }
638 }
639 }
640
641 if (sql_passwd_file_salt_len > 0 &&
642 (sql_passwd_file_salt_flags & SQL_PASSWD_SALT_FL_APPEND)) {
643 /* If we have file salt data, add it to the mix. */
644
645 if (!(sql_passwd_opts & SQL_PASSWD_OPT_HASH_SALT)) {
646 suffix = (unsigned char *) sql_passwd_file_salt;
647 suffix_len = sql_passwd_file_salt_len;
648
649 pr_trace_msg(trace_channel, 9,
650 "appending %lu bytes of file salt data", (unsigned long) suffix_len);
651
652 } else {
653 unsigned int salt_hashlen = 0;
654
655 suffix = sql_passwd_hash(cmd->tmp_pool, md,
656 (unsigned char *) sql_passwd_file_salt, sql_passwd_file_salt_len,
657 NULL, 0, NULL, 0, &salt_hashlen);
658 suffix_len = salt_hashlen;
659
660 if (sql_passwd_opts & SQL_PASSWD_OPT_ENCODE_SALT) {
661 suffix = (unsigned char *) sql_passwd_encode(cmd->tmp_pool,
662 sql_passwd_encoding, (unsigned char *) suffix, suffix_len);
663 suffix_len = strlen((char *) suffix);
664 }
665
666 pr_trace_msg(trace_channel, 9,
667 "appending %lu bytes of %s-hashed file salt data",
668 (unsigned long) suffix_len, digest);
669 }
670 }
671
672 if (sql_passwd_user_salt_len > 0 &&
673 (sql_passwd_user_salt_flags & SQL_PASSWD_SALT_FL_APPEND)) {
674 /* If we have user salt data, add it to the mix. */
675
676 if (!(sql_passwd_opts & SQL_PASSWD_OPT_HASH_SALT)) {
677 suffix = (unsigned char *) sql_passwd_user_salt;
678 suffix_len = sql_passwd_user_salt_len;
679
680 pr_trace_msg(trace_channel, 9,
681 "appending %lu bytes of user salt data", (unsigned long) suffix_len);
682
683 } else {
684 unsigned int salt_hashlen = 0;
685
686 suffix = sql_passwd_hash(cmd->tmp_pool, md,
687 (unsigned char *) sql_passwd_user_salt, sql_passwd_user_salt_len,
688 NULL, 0, NULL, 0, &salt_hashlen);
689 suffix_len = salt_hashlen;
690
691 if (sql_passwd_opts & SQL_PASSWD_OPT_ENCODE_SALT) {
692 suffix = (unsigned char *) sql_passwd_encode(cmd->tmp_pool,
693 sql_passwd_encoding, (unsigned char *) suffix, suffix_len);
694 suffix_len = strlen((char *) suffix);
695 }
696
697 pr_trace_msg(trace_channel, 9,
698 "appending %lu bytes of %s-hashed user salt data",
699 (unsigned long) suffix_len, digest);
700 }
701 }
702
703 hash = sql_passwd_hash(cmd->tmp_pool, md, data, data_len, prefix, prefix_len,
704 suffix, suffix_len, &hash_len);
705 if (hash == NULL) {
706 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
707 ": unable to obtain password hash: %s", strerror(errno));
708 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
709 }
710
711 encodedtext = sql_passwd_encode(cmd->tmp_pool, sql_passwd_encoding, hash,
712 hash_len);
713 if (encodedtext == NULL) {
714 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
715 ": unsupported SQLPasswordEncoding configured");
716 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
717 }
718
719 /* The case of nrounds == 1 is a special case, as that is when the salt
720 * data is processed. Any additional rounds are simply hashing and
721 * encoding the resulting data, over and over.
722 */
723 if (sql_passwd_nrounds > 1) {
724 register unsigned int i;
725 unsigned long nrounds = sql_passwd_nrounds - 1;
726
727 pr_trace_msg(trace_channel, 9,
728 "transforming the data for another %lu %s", nrounds,
729 nrounds != 1 ? "rounds" : "round");
730
731 for (i = 0; i < nrounds; i++) {
732 pr_signals_handle();
733
734 hash = sql_passwd_hash(cmd->tmp_pool, md, (unsigned char *) encodedtext,
735 strlen(encodedtext), NULL, 0, NULL, 0, &hash_len);
736 encodedtext = sql_passwd_encode(cmd->tmp_pool, sql_passwd_encoding,
737 hash, hash_len);
738
739 pr_trace_msg(trace_channel, 15, "data after round %u: '%s'", i + 1,
740 encodedtext);
741 }
742 }
743
744 /* Use the longer of the two texts for comparison length. */
745 cmp_len = strlen(encodedtext);
746 copytext_len = strlen(copytext);
747 if (copytext_len > cmp_len) {
748 cmp_len = copytext_len;
749 }
750
751 if (timingsafe_bcmp(encodedtext, copytext, cmp_len) == 0) {
752 return PR_HANDLED(cmd);
753 }
754
755 pr_trace_msg(trace_channel, 9, "expected '%s', got '%s'", copytext,
756 encodedtext);
757 pr_log_debug(DEBUG9, MOD_SQL_PASSWD_VERSION ": expected '%s', got '%s'",
758 copytext, encodedtext);
759
760 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
761 }
762
763 static modret_t *sql_passwd_bcrypt(cmd_rec *cmd, const char *plaintext,
764 const char *ciphertext) {
765 char *hashed;
766 size_t ciphertext_len = 0, cmp_len = 0, hashed_len = 0;
767
768 if (sql_passwd_engine == FALSE) {
769 pr_log_pri(PR_LOG_INFO, MOD_SQL_PASSWD_VERSION
770 ": SQLPasswordEngine disabled; unable to handle bcrypt SQLAuthType");
771 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
772 ": SQLPasswordEngine disabled; unable to handle bcrypt SQLAuthType");
773 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
774 }
775
776 /* Reject stored empty password fields (Issue #2275). */
777 if (*ciphertext == '\0') {
778 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
779 ": ignoring empty password field");
780 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
781 }
782
783 /* OpenSSL does not implement the bcrypt algorithm, so we handle it
784 * ourselves.
785 */
786
787 hashed = pr_auth_bcrypt(cmd->tmp_pool, plaintext, ciphertext, &hashed_len);
788 if (hashed == NULL) {
789 pr_trace_msg(trace_channel, 3, "error using 'bcrypt': %s", strerror(errno));
790 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
791 }
792
793 /* Use the longer of the two texts for comparison length. */
794 cmp_len = strlen(hashed);
795 ciphertext_len = strlen(ciphertext);
796 if (ciphertext_len > cmp_len) {
797 cmp_len = ciphertext_len;
798 }
799
800 if (timingsafe_bcmp(hashed, ciphertext, cmp_len) == 0) {
801 return PR_HANDLED(cmd);
802 }
803
804 pr_trace_msg(trace_channel, 9, "expected '%s', got '%s'", ciphertext, hashed);
805 pr_log_debug(DEBUG9, MOD_SQL_PASSWD_VERSION ": expected '%s', got '%s'",
806 ciphertext, hashed);
807
808 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
809 }
810
811 static modret_t *sql_passwd_md5(cmd_rec *cmd, const char *plaintext,
812 const char *ciphertext) {
813 return sql_passwd_auth(cmd, plaintext, ciphertext, "md5");
814 }
815
816 static modret_t *sql_passwd_sha1(cmd_rec *cmd, const char *plaintext,
817 const char *ciphertext) {
818 return sql_passwd_auth(cmd, plaintext, ciphertext, "sha1");
819 }
820
821 static modret_t *sql_passwd_sha256(cmd_rec *cmd, const char *plaintext,
822 const char *ciphertext) {
823 return sql_passwd_auth(cmd, plaintext, ciphertext, "sha256");
824 }
825
826 static modret_t *sql_passwd_sha512(cmd_rec *cmd, const char *plaintext,
827 const char *ciphertext) {
828 return sql_passwd_auth(cmd, plaintext, ciphertext, "sha512");
829 }
830
831 static modret_t *sql_passwd_pbkdf2(cmd_rec *cmd, const char *plaintext,
832 const char *ciphertext) {
833 unsigned char *derived_key;
834 const char *encodedtext;
835 char *pbkdf2_salt = NULL;
836 size_t ciphertext_len = 0, cmp_len = 0, pbkdf2_salt_len = 0;
837 int res;
838
839 if (sql_passwd_engine == FALSE) {
840 pr_log_pri(PR_LOG_INFO, MOD_SQL_PASSWD_VERSION
841 ": SQLPasswordEngine disabled; unable to handle PBKDF2 SQLAuthType");
842 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
843 ": SQLPasswordEngine disabled; unable to handle PBKDF2 SQLAuthType");
844 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
845 }
846
847 /* Reject stored empty password fields (Issue #2275). */
848 if (*ciphertext == '\0') {
849 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
850 ": ignoring empty password field");
851 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
852 }
853
854 if (sql_passwd_pbkdf2_digest == NULL) {
855 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
856 ": PBKDF2 not configured (see SQLPasswordPBKDF2 directive)");
857 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
858 }
859
860 /* PBKDF2 requires a salt; if no salt is configured, it is an error. */
861 if (sql_passwd_file_salt == NULL &&
862 sql_passwd_user_salt == NULL) {
863 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
864 ": no salt configured (PBKDF2 requires salt)");
865 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
866 }
867
868 derived_key = palloc(cmd->tmp_pool, sql_passwd_pbkdf2_len);
869
870 /* Prefer user salts over global salts. */
871 if (sql_passwd_user_salt_len > 0) {
872 pbkdf2_salt = (char *) sql_passwd_user_salt;
873 pbkdf2_salt_len = sql_passwd_user_salt_len;
874
875 } else {
876 pbkdf2_salt = (char *) sql_passwd_file_salt;
877 pbkdf2_salt_len = sql_passwd_file_salt_len;
878 }
879
880 #if OPENSSL_VERSION_NUMBER >= 0x1000003f
881 /* For digests other than SHA1, the necessary OpenSSL support
882 * (via PKCS5_PBKDF2_HMAC) appeared in 1.0.0c.
883 */
884 res = PKCS5_PBKDF2_HMAC(plaintext, -1,
885 (const unsigned char *) pbkdf2_salt, pbkdf2_salt_len,
886 sql_passwd_pbkdf2_iter, sql_passwd_pbkdf2_digest, sql_passwd_pbkdf2_len,
887 derived_key);
888 #else
889 res = PKCS5_PBKDF2_HMAC_SHA1(plaintext, -1,
890 (const unsigned char *) pbkdf2_salt, pbkdf2_salt_len,
891 sql_passwd_pbkdf2_iter, sql_passwd_pbkdf2_len, derived_key);
892 #endif /* OpenSSL-1.0.0b and earlier */
893
894 if (res != 1) {
895 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
896 ": error deriving PBKDF2 key: %s", get_crypto_errors());
897 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
898 }
899
900 encodedtext = sql_passwd_encode(cmd->tmp_pool, sql_passwd_encoding,
901 derived_key,
902 sql_passwd_pbkdf2_len);
903 if (encodedtext == NULL) {
904 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
905 ": unsupported SQLPasswordEncoding configured");
906 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
907 }
908
909 /* Use the longer of the two texts for comparison length. */
910 cmp_len = strlen(encodedtext);
911 ciphertext_len = strlen(ciphertext);
912 if (ciphertext_len > cmp_len) {
913 cmp_len = ciphertext_len;
914 }
915
916 if (timingsafe_bcmp(encodedtext, ciphertext, cmp_len) == 0) {
917 return PR_HANDLED(cmd);
918 }
919
920 pr_trace_msg(trace_channel, 9, "expected '%s', got '%s'", ciphertext,
921 encodedtext);
922 pr_log_debug(DEBUG9, MOD_SQL_PASSWD_VERSION ": expected '%s', got '%s'",
923 ciphertext, encodedtext);
924
925 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
926 }
927
928 #if defined(PR_USE_SODIUM)
929 static modret_t *sql_passwd_scrypt(cmd_rec *cmd, const char *plaintext,
930 const char *ciphertext) {
931 int res;
932 unsigned char *hash = NULL;
933 unsigned int hash_len = 0;
934 const char *encodedtext;
935 const unsigned char *scrypt_salt;
936 size_t ciphertext_len = 0, cmp_len = 0;
937 size_t ops_limit, mem_limit, plaintext_len, scrypt_salt_len;
938
939 if (sql_passwd_engine == FALSE) {
940 pr_log_pri(PR_LOG_INFO, MOD_SQL_PASSWD_VERSION
941 ": SQLPasswordEngine disabled; unable to handle scrypt SQLAuthType");
942 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
943 ": SQLPasswordEngine disabled; unable to handle scrypt SQLAuthType");
944 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
945 }
946
947 /* Reject stored empty password fields (Issue #2275). */
948 if (*ciphertext == '\0') {
949 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
950 ": ignoring empty password field");
951 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
952 }
953
954 /* scrypt requires a salt; if no salt is configured, it is an error. */
955 if (sql_passwd_file_salt == NULL &&
956 sql_passwd_user_salt == NULL) {
957 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
958 ": no salt configured (scrypt requires salt)");
959 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
960 }
961
962 /* Prefer user salts over global salts. */
963 if (sql_passwd_user_salt_len > 0) {
964 scrypt_salt = sql_passwd_user_salt;
965 scrypt_salt_len = sql_passwd_user_salt_len;
966
967 } else {
968 scrypt_salt = sql_passwd_file_salt;
969 scrypt_salt_len = sql_passwd_file_salt_len;
970 }
971
972 /* scrypt requires 32 bytes of salt */
973 if (scrypt_salt_len != SQL_PASSWD_SCRYPT_DEFAULT_SALT_SIZE) {
974 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
975 ": scrypt requires %u bytes of salt (%lu bytes of salt configured)",
976 SQL_PASSWD_SCRYPT_DEFAULT_SALT_SIZE, (unsigned long) scrypt_salt_len);
977 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
978 }
979
980 switch (sql_passwd_cost) {
981 case SQL_PASSWD_COST_INTERACTIVE:
982 ops_limit = crypto_pwhash_scryptsalsa208sha256_opslimit_interactive();
983 mem_limit = crypto_pwhash_scryptsalsa208sha256_memlimit_interactive();
984 break;
985
986 case SQL_PASSWD_COST_SENSITIVE:
987 ops_limit = crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive();
988 mem_limit = crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive();
989 break;
990
991 default:
992 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
993 ": unknown SQLPasswordCost value");
994 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
995 }
996
997 hash_len = sql_passwd_scrypt_hash_len;
998 hash = palloc(cmd->tmp_pool, hash_len);
999
1000 plaintext_len = strlen(plaintext);
1001 res = crypto_pwhash_scryptsalsa208sha256(hash, hash_len, plaintext,
1002 plaintext_len, scrypt_salt, ops_limit, mem_limit);
1003 if (res < 0) {
1004 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION ": scrypt error: %s",
1005 strerror(errno));
1006 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1007 }
1008
1009 encodedtext = sql_passwd_encode(cmd->tmp_pool, sql_passwd_encoding, hash,
1010 hash_len);
1011 if (encodedtext == NULL) {
1012 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1013 ": unsupported SQLPasswordEncoding configured");
1014 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1015 }
1016
1017 /* Use the longer of the two texts for comparison length. */
1018 cmp_len = strlen(encodedtext);
1019 ciphertext_len = strlen(ciphertext);
1020 if (ciphertext_len > cmp_len) {
1021 cmp_len = ciphertext_len;
1022 }
1023
1024 if (timingsafe_bcmp(encodedtext, ciphertext, cmp_len) == 0) {
1025 return PR_HANDLED(cmd);
1026 }
1027
1028 pr_trace_msg(trace_channel, 9, "expected '%s', got '%s'", ciphertext,
1029 encodedtext);
1030 pr_log_debug(DEBUG9, MOD_SQL_PASSWD_VERSION ": expected '%s', got '%s'",
1031 ciphertext, encodedtext);
1032
1033 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
1034 }
1035
1036 static modret_t *sql_passwd_argon2(cmd_rec *cmd, const char *plaintext,
1037 const char *ciphertext) {
1038 # if defined(USE_SODIUM_ARGON2)
1039 int argon2_algo, res;
1040 unsigned char *hash = NULL;
1041 unsigned int hash_len = 0;
1042 const char *encodedtext;
1043 const unsigned char *argon2_salt;
1044 size_t ciphertext_len = 0, cmp_len = 0;
1045 size_t ops_limit, mem_limit, plaintext_len, argon2_salt_len;
1046
1047 if (sql_passwd_engine == FALSE) {
1048 pr_log_pri(PR_LOG_INFO, MOD_SQL_PASSWD_VERSION
1049 ": SQLPasswordEngine disabled; unable to handle argon2 SQLAuthType");
1050 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1051 ": SQLPasswordEngine disabled; unable to handle argon2 SQLAuthType");
1052 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1053 }
1054
1055 /* Reject stored empty password fields (Issue #2275). */
1056 if (*ciphertext == '\0') {
1057 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1058 ": ignoring empty password field");
1059 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
1060 }
1061
1062 /* argon2 requires a salt; if no salt is configured, it is an error. */
1063 if (sql_passwd_file_salt == NULL &&
1064 sql_passwd_user_salt == NULL) {
1065 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1066 ": no salt configured (argon2 requires salt)");
1067 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1068 }
1069
1070 /* Prefer user salts over global salts. */
1071 if (sql_passwd_user_salt_len > 0) {
1072 argon2_salt = sql_passwd_user_salt;
1073 argon2_salt_len = sql_passwd_user_salt_len;
1074
1075 } else {
1076 argon2_salt = sql_passwd_file_salt;
1077 argon2_salt_len = sql_passwd_file_salt_len;
1078 }
1079
1080 /* argon2 requires 16 bytes of salt */
1081 if (argon2_salt_len != SQL_PASSWD_ARGON2_DEFAULT_SALT_SIZE) {
1082 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1083 ": argon2 requires %u bytes of salt (%lu bytes of salt configured)",
1084 SQL_PASSWD_ARGON2_DEFAULT_SALT_SIZE, (unsigned long) argon2_salt_len);
1085 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1086 }
1087
1088 argon2_algo = crypto_pwhash_argon2i_alg_argon2i13();
1089
1090 switch (sql_passwd_cost) {
1091 case SQL_PASSWD_COST_INTERACTIVE:
1092 ops_limit = crypto_pwhash_argon2i_opslimit_interactive();
1093 mem_limit = crypto_pwhash_argon2i_memlimit_interactive();
1094 break;
1095
1096 case SQL_PASSWD_COST_SENSITIVE:
1097 ops_limit = crypto_pwhash_argon2i_opslimit_sensitive();
1098 mem_limit = crypto_pwhash_argon2i_memlimit_sensitive();
1099 break;
1100
1101 default:
1102 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1103 ": unknown SQLPasswordCost value");
1104 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1105 }
1106
1107 hash_len = sql_passwd_argon2_hash_len;
1108 hash = palloc(cmd->tmp_pool, hash_len);
1109
1110 plaintext_len = strlen(plaintext);
1111 res = crypto_pwhash_argon2i(hash, hash_len, plaintext, plaintext_len,
1112 argon2_salt, ops_limit, mem_limit, argon2_algo);
1113 if (res < 0) {
1114 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION ": argon2 error: %s",
1115 strerror(errno));
1116 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1117 }
1118
1119 encodedtext = sql_passwd_encode(cmd->tmp_pool, sql_passwd_encoding, hash,
1120 hash_len);
1121 if (encodedtext == NULL) {
1122 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1123 ": unsupported SQLPasswordEncoding configured");
1124 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1125 }
1126
1127 /* Use the longer of the two texts for comparison length. */
1128 cmp_len = strlen(encodedtext);
1129 ciphertext_len = strlen(ciphertext);
1130 if (ciphertext_len > cmp_len) {
1131 cmp_len = ciphertext_len;
1132 }
1133
1134 if (timingsafe_bcmp(encodedtext, ciphertext, cmp_len) == 0) {
1135 return PR_HANDLED(cmd);
1136 }
1137
1138 pr_trace_msg(trace_channel, 9, "expected '%s', got '%s'", ciphertext,
1139 encodedtext);
1140 pr_log_debug(DEBUG9, MOD_SQL_PASSWD_VERSION ": expected '%s', got '%s'",
1141 ciphertext, encodedtext);
1142
1143 return PR_ERROR_INT(cmd, PR_AUTH_BADPWD);
1144 # else
1145 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1146 ": argon2 not supported on this system (requires libsodium-1.0.9 or "
1147 "later)");
1148 return PR_ERROR_INT(cmd, PR_AUTH_ERROR);
1149 # endif /* USE_SODIUM_ARGON2 */
1150 }
1151 #endif /* PR_USE_SODIUM */
1152
1153 /* Event handlers
1154 */
1155
1156 #if defined(PR_SHARED_MODULE)
1157 static void sql_passwd_mod_unload_ev(const void *event_data, void *user_data) {
1158 if (strcmp("mod_sql_passwd.c", (const char *) event_data) == 0) {
1159 sql_unregister_authtype("bcrypt");
1160 sql_unregister_authtype("md5");
1161 sql_unregister_authtype("sha1");
1162 sql_unregister_authtype("sha256");
1163 sql_unregister_authtype("sha512");
1164 sql_unregister_authtype("pbkdf2");
1165 # if defined(PR_USE_SODIUM)
1166 sql_unregister_authtype("argon2");
1167 sql_unregister_authtype("scrypt");
1168 # endif /* PR_USE_SODIUM */
1169
1170 pr_event_unregister(&sql_passwd_module, NULL, NULL);
1171 }
1172 }
1173 #endif /* PR_SHARED_MODULE */
1174
1175 /* Command handlers
1176 */
1177
1178 MODRET sql_passwd_pre_pass(cmd_rec *cmd) {
1179 config_rec *c;
1180
1181 if (sql_passwd_engine == FALSE) {
1182 return PR_DECLINED(cmd);
1183 }
1184
1185 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordRounds", FALSE);
1186 if (c != NULL) {
1187 sql_passwd_nrounds = *((unsigned long *) c->argv[0]);
1188 }
1189
1190 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordPBKDF2", FALSE);
1191 if (c != NULL) {
1192 if (c->argc == 3) {
1193 sql_passwd_pbkdf2_digest = c->argv[0];
1194 sql_passwd_pbkdf2_iter = *((int *) c->argv[1]);
1195 sql_passwd_pbkdf2_len = *((int *) c->argv[2]);
1196
1197 } else {
1198 const char *user;
1199 char *key, *named_query, *ptr;
1200 cmdtable *sql_cmdtab;
1201 cmd_rec *sql_cmd;
1202 modret_t *sql_res;
1203 array_header *sql_data;
1204
1205 key = c->argv[0];
1206
1207 ptr = key + 5;
1208 named_query = pstrcat(cmd->tmp_pool, "SQLNamedQuery_", ptr, NULL);
1209
1210 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
1211 if (c == NULL) {
1212 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1213 ": unable to resolve SQLNamedQuery '%s'", ptr);
1214 return PR_DECLINED(cmd);
1215 }
1216
1217 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
1218 NULL);
1219 if (sql_cmdtab == NULL) {
1220 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1221 ": unable to find SQL hook symbol 'sql_lookup'");
1222 return PR_DECLINED(cmd);
1223 }
1224
1225 user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
1226
1227 sql_cmd = sql_passwd_cmd_create(cmd->tmp_pool, 3, "sql_lookup", ptr,
1228 sql_passwd_get_str(cmd->tmp_pool, user));
1229
1230 /* Call the handler. */
1231 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
1232 if (sql_res == NULL ||
1233 MODRET_ISERROR(sql_res)) {
1234 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1235 ": error processing SQLNamedQuery '%s'", ptr);
1236 return PR_DECLINED(cmd);
1237 }
1238
1239 sql_data = (array_header *) sql_res->data;
1240
1241 if (sql_data->nelts != 3) {
1242 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1243 ": SQLNamedQuery '%s' returned wrong number of columns (%d)", ptr,
1244 sql_data->nelts);
1245
1246 } else {
1247 char **values;
1248 int iter, len, res;
1249 const EVP_MD *md;
1250
1251 values = sql_data->elts;
1252
1253 res = get_pbkdf2_config(values[0], &md, values[1], &iter,
1254 values[2], &len);
1255 switch (res) {
1256 case SQL_PASSWD_ERR_PBKDF2_UNKNOWN_DIGEST:
1257 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1258 ": SQLNamedQuery '%s' returned unknown PKBDF2 digest: %s",
1259 ptr, values[0]);
1260 break;
1261
1262 case SQL_PASSWD_ERR_PBKDF2_UNSUPPORTED_DIGEST:
1263 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1264 ": SQLNamedQuery '%s' returned unsupported PKBDF2 digest: %s",
1265 ptr, values[0]);
1266 break;
1267
1268 case SQL_PASSWD_ERR_PBKDF2_BAD_ROUNDS:
1269 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1270 ": SQLNamedQuery '%s' returned insufficient number of rounds: %s",
1271 ptr, values[1]);
1272 break;
1273
1274 case SQL_PASSWD_ERR_PBKDF2_BAD_LENGTH:
1275 sql_log(DEBUG_WARN, MOD_SQL_PASSWD_VERSION
1276 ": SQLNamedQuery '%s' returned insufficient length: %s", ptr,
1277 values[2]);
1278 break;
1279
1280 case 0:
1281 sql_passwd_pbkdf2_digest = md;
1282 sql_passwd_pbkdf2_iter = iter;
1283 sql_passwd_pbkdf2_len = len;
1284 break;
1285 }
1286 }
1287 }
1288 }
1289
1290 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordUserSalt", FALSE);
1291 if (c != NULL) {
1292 char *key;
1293 unsigned long salt_flags;
1294
1295 key = c->argv[0];
1296 salt_flags = *((unsigned long *) c->argv[1]);
1297
1298 if (strcasecmp(key, "name") == 0) {
1299 const char *user;
1300
1301 user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
1302 if (user == NULL) {
1303 pr_log_debug(DEBUG3, MOD_SQL_PASSWD_VERSION
1304 ": unable to determine original USER name");
1305 return PR_DECLINED(cmd);
1306 }
1307
1308 sql_passwd_user_salt = (unsigned char *) user;
1309 sql_passwd_user_salt_len = strlen(user);
1310
1311 } else if (strncasecmp(key, "sql:/", 5) == 0) {
1312 const char *user;
1313 char *named_query, *ptr, **values;
1314 cmdtable *sql_cmdtab;
1315 cmd_rec *sql_cmd;
1316 modret_t *sql_res;
1317 array_header *sql_data;
1318 size_t value_len;
1319
1320 ptr = key + 5;
1321 named_query = pstrcat(cmd->tmp_pool, "SQLNamedQuery_", ptr, NULL);
1322
1323 c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
1324 if (c == NULL) {
1325 pr_log_debug(DEBUG3, MOD_SQL_PASSWD_VERSION
1326 ": unable to resolve SQLNamedQuery '%s'", ptr);
1327 return PR_DECLINED(cmd);
1328 }
1329
1330 sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
1331 NULL);
1332 if (sql_cmdtab == NULL) {
1333 pr_log_debug(DEBUG3, MOD_SQL_PASSWD_VERSION
1334 ": unable to find SQL hook symbol 'sql_lookup'");
1335 return PR_DECLINED(cmd);
1336 }
1337
1338 user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
1339 if (user == NULL) {
1340 pr_log_debug(DEBUG3, MOD_SQL_PASSWD_VERSION
1341 ": unable to determine original USER name");
1342 return PR_DECLINED(cmd);
1343 }
1344
1345 sql_cmd = sql_passwd_cmd_create(cmd->tmp_pool, 3, "sql_lookup", ptr,
1346 sql_passwd_get_str(cmd->tmp_pool, user));
1347
1348 /* Call the handler. */
1349 sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
1350 if (sql_res == NULL ||
1351 MODRET_ISERROR(sql_res)) {
1352 pr_log_debug(DEBUG0, MOD_SQL_PASSWD_VERSION
1353 ": error processing SQLNamedQuery '%s'", ptr);
1354 return PR_DECLINED(cmd);
1355 }
1356
1357 sql_data = (array_header *) sql_res->data;
1358
1359 if (sql_data->nelts != 1) {
1360 pr_log_debug(DEBUG0, MOD_SQL_PASSWD_VERSION
1361 ": SQLNamedQuery '%s' returned wrong number of rows (%d)", ptr,
1362 sql_data->nelts);
1363 return PR_DECLINED(cmd);
1364 }
1365
1366 values = sql_data->elts;
1367
1368 /* Note: this ASSUMES that the value coming from the database is a
1369 * string.
1370 */
1371 value_len = strlen(values[0]);
1372
1373 sql_passwd_user_salt = sql_passwd_decode(session.pool,
1374 sql_passwd_salt_encoding, values[0], value_len,
1375 &sql_passwd_user_salt_len);
1376 if (sql_passwd_user_salt == NULL) {
1377 pr_log_debug(DEBUG0, MOD_SQL_PASSWD_VERSION
1378 ": error decoding salt from SQLNamedQuery '%s': %s", ptr,
1379 strerror(errno));
1380 return PR_DECLINED(cmd);
1381 }
1382
1383 } else {
1384 return PR_DECLINED(cmd);
1385 }
1386
1387 sql_passwd_user_salt_flags = salt_flags;
1388 }
1389
1390 return PR_DECLINED(cmd);
1391 }
1392
1393 /* Configuration handlers
1394 */
1395
1396 /* usage: SQLPasswordArgon2 len */
1397 MODRET set_sqlpasswdargon2(cmd_rec *cmd) {
1398 #if defined(USE_SODIUM_ARGON2)
1399 config_rec *c;
1400 int len;
1401
1402 CHECK_ARGS(cmd, 1);
1403 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1404
1405 len = atoi(cmd->argv[1]);
1406 if (len <= 0) {
1407 CONF_ERROR(cmd, "length must be greater than 0");
1408 }
1409
1410 c = add_config_param(cmd->argv[0], 1, NULL);
1411 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
1412 *((unsigned int *) c->argv[0]) = len;
1413
1414 return PR_HANDLED(cmd);
1415 #else
1416 CONF_ERROR(cmd, "requires libsodium Argon2 support");
1417 #endif /* No libsodium Argon2 support */
1418 }
1419
1420 /* usage: SQLPasswordCost "interactive"|"sensitive" */
1421 MODRET set_sqlpasswdcost(cmd_rec *cmd) {
1422 unsigned int cost;
1423 config_rec *c = NULL;
1424
1425 CHECK_ARGS(cmd, 1);
1426 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1427
1428 if (strcasecmp(cmd->argv[1], "interactive") == 0) {
1429 cost = SQL_PASSWD_COST_INTERACTIVE;
1430
1431 } else if (strcasecmp(cmd->argv[1], "sensitive") == 0) {
1432 cost = SQL_PASSWD_COST_SENSITIVE;
1433
1434 } else {
1435 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown/unsupported cost: '",
1436 cmd->argv[1], "'", NULL));
1437 }
1438
1439 c = add_config_param(cmd->argv[0], 1, NULL);
1440 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
1441 *((unsigned int *) c->argv[0]) = cost;
1442
1443 return PR_HANDLED(cmd);
1444 }
1445
1446 /* usage: SQLPasswordEncoding "base64"|"hex"|"HEX" */
1447 MODRET set_sqlpasswdencoding(cmd_rec *cmd) {
1448 unsigned int encoding;
1449 config_rec *c = NULL;
1450
1451 CHECK_ARGS(cmd, 1);
1452 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1453
1454 if (strcasecmp(cmd->argv[1], "none") == 0) {
1455 encoding = SQL_PASSWD_ENC_USE_NONE;
1456
1457 } else if (strcasecmp(cmd->argv[1], "base64") == 0) {
1458 encoding = SQL_PASSWD_ENC_USE_BASE64;
1459
1460 } else if (strcmp(cmd->argv[1], "hex") == 0) {
1461 encoding = SQL_PASSWD_ENC_USE_HEX_LC;
1462
1463 } else if (strcmp(cmd->argv[1], "HEX") == 0) {
1464 encoding = SQL_PASSWD_ENC_USE_HEX_UC;
1465
1466 } else {
1467 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported encoding '",
1468 cmd->argv[1], "' configured", NULL));
1469 }
1470
1471 c = add_config_param(cmd->argv[0], 1, NULL);
1472 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1473 *((unsigned int *) c->argv[0]) = encoding;
1474
1475 return PR_HANDLED(cmd);
1476 }
1477
1478 /* usage: SQLPasswordEngine on|off */
1479 MODRET set_sqlpasswdengine(cmd_rec *cmd) {
1480 int engine = -1;
1481 config_rec *c = NULL;
1482
1483 CHECK_ARGS(cmd, 1);
1484 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1485
1486 engine = get_boolean(cmd, 1);
1487 if (engine == -1) {
1488 CONF_ERROR(cmd, "expected Boolean parameter");
1489 }
1490
1491 c = add_config_param(cmd->argv[0], 1, NULL);
1492 c->argv[0] = pcalloc(c->pool, sizeof(int));
1493 *((int *) c->argv[0]) = engine;
1494
1495 return PR_HANDLED(cmd);
1496 }
1497
1498 /* usage: SQLPasswordOptions opt1 ... optN */
1499 MODRET set_sqlpasswdoptions(cmd_rec *cmd) {
1500 config_rec *c;
1501 unsigned long opts = 0UL;
1502 register unsigned int i;
1503
1504 if (cmd->argc < 2) {
1505 CONF_ERROR(cmd, "wrong number of parameters");
1506 }
1507
1508 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1509
1510 for (i = 1; i < cmd->argc; i++) {
1511 if (strcasecmp(cmd->argv[i], "HashPassword") == 0) {
1512 opts |= SQL_PASSWD_OPT_HASH_PASSWORD;
1513
1514 } else if (strcasecmp(cmd->argv[i], "HashSalt") == 0) {
1515 opts |= SQL_PASSWD_OPT_HASH_SALT;
1516
1517 } else if (strcasecmp(cmd->argv[i], "HashEncodePassword") == 0) {
1518 opts |= SQL_PASSWD_OPT_HASH_PASSWORD;
1519 opts |= SQL_PASSWD_OPT_ENCODE_PASSWORD;
1520
1521 } else if (strcasecmp(cmd->argv[i], "HashEncodeSalt") == 0) {
1522 opts |= SQL_PASSWD_OPT_HASH_SALT;
1523 opts |= SQL_PASSWD_OPT_ENCODE_SALT;
1524
1525 } else {
1526 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown SQLPasswordOption '",
1527 cmd->argv[i], "'", NULL));
1528 }
1529 }
1530
1531 c = add_config_param(cmd->argv[0], 1, NULL);
1532 c->argv[0] = palloc(c->pool, sizeof(unsigned long));
1533 *((unsigned long *) c->argv[0]) = opts;
1534
1535 if (pr_module_exists("mod_ifsession.c")) {
1536 /* These are needed in case this directive is used with mod_ifsession
1537 * configuration.
1538 */
1539 c->flags |= CF_MULTI;
1540 }
1541
1542 return PR_HANDLED(cmd);
1543 }
1544
1545 /* usage: SQLPasswordPBKDF2 "sql:/"named-query|algo iter len */
1546 MODRET set_sqlpasswdpbkdf2(cmd_rec *cmd) {
1547 config_rec *c;
1548
1549 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1550
1551 if (cmd->argc == 4) {
1552 int iter, len, res;
1553 const EVP_MD *md;
1554
1555 res = get_pbkdf2_config(cmd->argv[1], &md, cmd->argv[2], &iter,
1556 cmd->argv[3], &len);
1557 switch (res) {
1558 case SQL_PASSWD_ERR_PBKDF2_UNKNOWN_DIGEST:
1559 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported digest algorithm '",
1560 cmd->argv[1], "' configured", NULL));
1561 break;
1562
1563 case SQL_PASSWD_ERR_PBKDF2_UNSUPPORTED_DIGEST:
1564 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1565 "Use of non-SHA1 digests for PBKDF2, such as ", cmd->argv[1],
1566 ", requires OpenSSL-1.0.0c or later (currently using ",
1567 OPENSSL_VERSION_TEXT, ")", NULL));
1568 break;
1569
1570 case SQL_PASSWD_ERR_PBKDF2_BAD_ROUNDS:
1571 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1572 "insufficient number of rounds (", cmd->argv[2], ")", NULL));
1573 break;
1574
1575 case SQL_PASSWD_ERR_PBKDF2_BAD_LENGTH:
1576 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "insufficient length (",
1577 cmd->argv[3], ")", NULL));
1578 break;
1579
1580 case 0:
1581 break;
1582 }
1583
1584 c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL);
1585 c->argv[0] = (void *) md;
1586 c->argv[1] = palloc(c->pool, sizeof(int));
1587 *((int *) c->argv[1]) = iter;
1588 c->argv[2] = palloc(c->pool, sizeof(int));
1589 *((int *) c->argv[2]) = len;
1590
1591 } else if (cmd->argc == 2) {
1592 if (strncasecmp(cmd->argv[1], "sql:/", 5) != 0) {
1593 CONF_ERROR(cmd, "badly formatted parameter");
1594 }
1595
1596 c = add_config_param(cmd->argv[0], 1, NULL);
1597 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
1598
1599 } else {
1600 CONF_ERROR(cmd, "wrong number of parameters");
1601 }
1602
1603 return PR_HANDLED(cmd);
1604 }
1605
1606 /* usage: SQLPasswordRounds count */
1607 MODRET set_sqlpasswdrounds(cmd_rec *cmd) {
1608 config_rec *c;
1609 long nrounds;
1610
1611 CHECK_ARGS(cmd, 1);
1612 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1613
1614 nrounds = atol(cmd->argv[1]);
1615 if (nrounds < 1) {
1616 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "insufficient number of rounds (",
1617 cmd->argv[1], ")", NULL));
1618 }
1619
1620 c = add_config_param(cmd->argv[0], 1, NULL);
1621 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
1622 *((unsigned long *) c->argv[0]) = nrounds;
1623
1624 return PR_HANDLED(cmd);
1625 }
1626
1627 /* usage: SQLPasswordSaltEncoding "base64"|"hex"|"HEX"|"none" */
1628 MODRET set_sqlpasswdsaltencoding(cmd_rec *cmd) {
1629 /* Reuse the parsing code for the SQLPasswordEncoding directive. */
1630 return set_sqlpasswdencoding(cmd);
1631 }
1632
1633 /* usage: SQLPasswordSaltFile path|"none" [flags] */
1634 MODRET set_sqlpasswdsaltfile(cmd_rec *cmd) {
1635 config_rec *c;
1636 register unsigned int i;
1637 unsigned long flags = SQL_PASSWD_SALT_FL_APPEND;
1638
1639 if (cmd->argc < 2) {
1640 CONF_ERROR(cmd, "wrong number of parameters");
1641 }
1642
1643 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1644
1645 for (i = 2; i < cmd->argc; i++) {
1646 if (strcasecmp(cmd->argv[i], "Append") == 0) {
1647 flags &= ~SQL_PASSWD_SALT_FL_PREPEND;
1648 flags |= SQL_PASSWD_SALT_FL_APPEND;
1649
1650 } else if (strcasecmp(cmd->argv[i], "Prepend") == 0) {
1651 flags &= ~SQL_PASSWD_SALT_FL_APPEND;
1652 flags |= SQL_PASSWD_SALT_FL_PREPEND;
1653
1654 } else {
1655 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown salt flag '",
1656 cmd->argv[i], "'", NULL));
1657 }
1658 }
1659
1660 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
1661 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
1662 c->argv[1] = palloc(c->pool, sizeof(unsigned long));
1663 *((unsigned long *) c->argv[1]) = flags;
1664
1665 return PR_HANDLED(cmd);
1666 }
1667
1668 /* usage: SQLPasswordScrypt len */
1669 MODRET set_sqlpasswdscrypt(cmd_rec *cmd) {
1670 #if defined(PR_USE_SODIUM)
1671 config_rec *c;
1672 int len;
1673
1674 CHECK_ARGS(cmd, 1);
1675 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1676
1677 len = atoi(cmd->argv[1]);
1678 if (len <= 0) {
1679 CONF_ERROR(cmd, "length must be greater than 0");
1680 }
1681
1682 c = add_config_param(cmd->argv[0], 1, NULL);
1683 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
1684 *((unsigned int *) c->argv[0]) = len;
1685
1686 return PR_HANDLED(cmd);
1687 #else
1688 CONF_ERROR(cmd, "requires libsodium support");
1689 #endif /* No libsodium support */
1690 }
1691
1692 /* usage: SQLPasswordUserSalt "name"|"sql:/named-query" [flags] */
1693 MODRET set_sqlpasswdusersalt(cmd_rec *cmd) {
1694 config_rec *c;
1695 register unsigned int i;
1696 unsigned long flags = SQL_PASSWD_SALT_FL_APPEND;
1697
1698 if (cmd->argc < 2) {
1699 CONF_ERROR(cmd, "wrong number of parameters");
1700 }
1701
1702 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1703
1704 if (strcasecmp(cmd->argv[1], "name") != 0 &&
1705 strcasecmp(cmd->argv[1], "uid") != 0 &&
1706 strncasecmp(cmd->argv[1], "sql:/", 5) != 0) {
1707 CONF_ERROR(cmd, "badly formatted parameter");
1708 }
1709
1710 for (i = 2; i < cmd->argc; i++) {
1711 if (strcasecmp(cmd->argv[i], "Append") == 0) {
1712 flags &= ~SQL_PASSWD_SALT_FL_PREPEND;
1713 flags |= SQL_PASSWD_SALT_FL_APPEND;
1714
1715 } else if (strcasecmp(cmd->argv[i], "Prepend") == 0) {
1716 flags &= ~SQL_PASSWD_SALT_FL_APPEND;
1717 flags |= SQL_PASSWD_SALT_FL_PREPEND;
1718
1719 } else {
1720 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown salt flag '",
1721 cmd->argv[i], "'", NULL));
1722 }
1723 }
1724
1725 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
1726 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
1727 c->argv[1] = palloc(c->pool, sizeof(unsigned long));
1728 *((unsigned long *) c->argv[1]) = flags;
1729
1730 return PR_HANDLED(cmd);
1731 }
1732
1733 /* Event listeners
1734 */
1735
1736 static void sql_passwd_sess_reinit_ev(const void *event_data, void *user_data) {
1737 int res;
1738
1739 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
1740
1741 pr_event_unregister(&sql_passwd_module, "core.session-reinit",
1742 sql_passwd_sess_reinit_ev);
1743
1744 sql_passwd_engine = FALSE;
1745 sql_passwd_encoding = SQL_PASSWD_ENC_USE_HEX_LC;
1746 sql_passwd_salt_encoding = SQL_PASSWD_ENC_USE_NONE;
1747 sql_passwd_file_salt = NULL;
1748 sql_passwd_file_salt_len = 0;
1749 sql_passwd_user_salt = NULL;
1750 sql_passwd_user_salt_len = 0;
1751 sql_passwd_file_salt_flags = SQL_PASSWD_SALT_FL_APPEND;
1752 sql_passwd_user_salt_flags = SQL_PASSWD_SALT_FL_APPEND;
1753 sql_passwd_opts = 0UL;
1754 sql_passwd_nrounds = 1;
1755
1756 #if defined(PR_USE_SODIUM)
1757 sql_passwd_scrypt_hash_len = SQL_PASSWD_SCRYPT_DEFAULT_HASH_SIZE;
1758 # if defined(USE_SODIUM_ARGON2)
1759 sql_passwd_argon2_hash_len = SQL_PASSWD_ARGON2_DEFAULT_HASH_SIZE;
1760 # endif /* USE_SODIUM_ARGON2 */
1761 #endif /* PR_USE_SODIUM */
1762
1763 res = sql_passwd_sess_init();
1764 if (res < 0) {
1765 pr_session_disconnect(&sql_passwd_module,
1766 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
1767 }
1768 }
1769
1770 /* Initialization routines
1771 */
1772
1773 static int sql_passwd_init(void) {
1774 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1775 defined(HAVE_LIBRESSL)
1776 OpenSSL_add_all_digests();
1777 #endif /* prior to OpenSSL-1.1.0 */
1778
1779 #if defined(PR_SHARED_MODULE)
1780 pr_event_register(&sql_passwd_module, "core.module-unload",
1781 sql_passwd_mod_unload_ev, NULL);
1782 #endif /* PR_SHARED_MODULE */
1783
1784 #if defined(PR_USE_SODIUM)
1785 if (sodium_init() < 0) {
1786 pr_log_pri(PR_LOG_NOTICE, MOD_SQL_PASSWD_VERSION
1787 ": error initializing libsodium");
1788
1789 } else {
1790 const char *sodium_version;
1791
1792 sodium_version = sodium_version_string();
1793 pr_log_debug(DEBUG2, MOD_SQL_PASSWD_VERSION ": using libsodium-%s",
1794 sodium_version);
1795 }
1796 #endif /* PR_USE_SODIUM */
1797
1798 if (sql_register_authtype("bcrypt", sql_passwd_bcrypt) < 0) {
1799 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1800 ": unable to register 'bcrypt' SQLAuthType handler: %s", strerror(errno));
1801
1802 } else {
1803 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1804 ": registered 'bcrypt' SQLAuthType handler");
1805 }
1806
1807 if (sql_register_authtype("md5", sql_passwd_md5) < 0) {
1808 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1809 ": unable to register 'md5' SQLAuthType handler: %s", strerror(errno));
1810
1811 } else {
1812 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1813 ": registered 'md5' SQLAuthType handler");
1814 }
1815
1816 if (sql_register_authtype("sha1", sql_passwd_sha1) < 0) {
1817 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1818 ": unable to register 'sha1' SQLAuthType handler: %s", strerror(errno));
1819
1820 } else {
1821 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1822 ": registered 'sha1' SQLAuthType handler");
1823 }
1824
1825 if (sql_register_authtype("sha256", sql_passwd_sha256) < 0) {
1826 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1827 ": unable to register 'sha256' SQLAuthType handler: %s", strerror(errno));
1828
1829 } else {
1830 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1831 ": registered 'sha256' SQLAuthType handler");
1832 }
1833
1834 if (sql_register_authtype("sha512", sql_passwd_sha512) < 0) {
1835 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1836 ": unable to register 'sha512' SQLAuthType handler: %s", strerror(errno));
1837
1838 } else {
1839 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1840 ": registered 'sha512' SQLAuthType handler");
1841 }
1842
1843 if (sql_register_authtype("pbkdf2", sql_passwd_pbkdf2) < 0) {
1844 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1845 ": unable to register 'pbkdf2' SQLAuthType handler: %s", strerror(errno));
1846
1847 } else {
1848 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1849 ": registered 'pbkdf2' SQLAuthType handler");
1850 }
1851
1852 #if defined(PR_USE_SODIUM)
1853 if (sql_register_authtype("scrypt", sql_passwd_scrypt) < 0) {
1854 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1855 ": unable to register 'scrypt' SQLAuthType handler: %s", strerror(errno));
1856
1857 } else {
1858 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1859 ": registered 'scrypt' SQLAuthType handler");
1860 }
1861
1862 if (sql_register_authtype("argon2", sql_passwd_argon2) < 0) {
1863 pr_log_pri(PR_LOG_WARNING, MOD_SQL_PASSWD_VERSION
1864 ": unable to register 'argon2' SQLAuthType handler: %s", strerror(errno));
1865
1866 } else {
1867 pr_log_debug(DEBUG6, MOD_SQL_PASSWD_VERSION
1868 ": registered 'argon2' SQLAuthType handler");
1869 }
1870 #endif /* PR_USE_SODIUM */
1871
1872 return 0;
1873 }
1874
1875 static int sql_passwd_sess_init(void) {
1876 config_rec *c;
1877
1878 pr_event_register(&sql_passwd_module, "core.session-reinit",
1879 sql_passwd_sess_reinit_ev, NULL);
1880
1881 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordEngine", FALSE);
1882 if (c != NULL) {
1883 sql_passwd_engine = *((int *) c->argv[0]);
1884 }
1885
1886 if (sql_passwd_engine == FALSE) {
1887 return 0;
1888 }
1889
1890 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordCost", FALSE);
1891 if (c != NULL) {
1892 sql_passwd_cost = *((unsigned int *) c->argv[0]);
1893 }
1894
1895 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordEncoding", FALSE);
1896 if (c != NULL) {
1897 sql_passwd_encoding = *((unsigned int *) c->argv[0]);
1898 }
1899
1900 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordOptions", FALSE);
1901 while (c != NULL) {
1902 unsigned long opts;
1903
1904 pr_signals_handle();
1905
1906 opts = *((unsigned long *) c->argv[0]);
1907 sql_passwd_opts |= opts;
1908
1909 c = find_config_next(c, c->next, CONF_PARAM, "SQLPasswordOptions", FALSE);
1910 }
1911
1912 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordSaltEncoding",
1913 FALSE);
1914 if (c != NULL) {
1915 sql_passwd_salt_encoding = *((unsigned int *) c->argv[0]);
1916 }
1917
1918 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordSaltFile", FALSE);
1919 if (c != NULL) {
1920 char *path;
1921 unsigned long salt_flags;
1922
1923 path = c->argv[0];
1924 salt_flags = *((unsigned long *) c->argv[1]);
1925
1926 if (strcasecmp(path, "none") != 0) {
1927 int fd, xerrno = 0;
1928
1929 PRIVS_ROOT
1930 fd = open(path, O_RDONLY|O_NONBLOCK);
1931 if (fd < 0) {
1932 xerrno = errno;
1933 }
1934 PRIVS_RELINQUISH
1935
1936 if (fd >= 0) {
1937 char *file_salt = NULL;
1938 size_t file_salt_len = 0;
1939 int flags;
1940
1941 /* XXX Rather than using a fixed size, this should be a dynamically
1942 * allocated buffer, of st.st_blksize bytes, for optimal disk IO.
1943 */
1944 char buf[512];
1945 ssize_t nread;
1946
1947 /* Set this descriptor for blocking. */
1948 flags = fcntl(fd, F_GETFL);
1949 if (fcntl(fd, F_SETFL, flags & (U32BITS^O_NONBLOCK)) < 0) {
1950 pr_log_debug(DEBUG3, MOD_SQL_PASSWD_VERSION
1951 ": error setting blocking mode on SQLPasswordSaltFile '%s': %s",
1952 path, strerror(errno));
1953 }
1954
1955 nread = read(fd, buf, sizeof(buf));
1956 while (nread > 0) {
1957 pr_signals_handle();
1958
1959 if (file_salt == NULL) {
1960 /* If the very last byte in the buffer is a newline, trim it. */
1961 if (buf[nread-1] == '\n') {
1962 buf[nread-1] = '\0';
1963 nread--;
1964 }
1965
1966 file_salt_len = nread;
1967 file_salt = palloc(session.pool, file_salt_len);
1968 memcpy(file_salt, buf, nread);
1969
1970 } else {
1971 char *ptr, *tmp;
1972
1973 /* Allocate a larger buffer for the salt. */
1974 ptr = tmp = palloc(session.pool, file_salt_len + nread);
1975 memcpy(tmp, file_salt, file_salt_len);
1976 tmp += file_salt_len;
1977
1978 memcpy(tmp, buf, nread);
1979 file_salt_len += nread;
1980 file_salt = ptr;
1981 }
1982
1983 nread = read(fd, buf, sizeof(buf));
1984 }
1985
1986 if (nread < 0) {
1987 pr_log_debug(DEBUG1, MOD_SQL_PASSWD_VERSION
1988 ": error reading salt data from SQLPasswordSaltFile '%s': %s",
1989 path, strerror(errno));
1990 file_salt = NULL;
1991 }
1992
1993 (void) close(fd);
1994
1995 if (file_salt != NULL) {
1996 /* If the very last byte in the buffer is a newline, trim it. This
1997 * is to deal with cases where the SaltFile may have been written
1998 * with an editor (e.g. vi) which automatically adds a trailing
1999 * newline.
2000 */
2001 if (file_salt[file_salt_len-1] == '\n') {
2002 file_salt[file_salt_len-1] = '\0';
2003 file_salt_len--;
2004 }
2005
2006 sql_passwd_file_salt = sql_passwd_decode(session.pool,
2007 sql_passwd_salt_encoding, file_salt, file_salt_len,
2008 &sql_passwd_file_salt_len);
2009 if (sql_passwd_file_salt == NULL) {
2010 pr_log_debug(DEBUG0, MOD_SQL_PASSWD_VERSION
2011 ": error decoding salt from SQLPasswordSaltFile '%s': %s", path,
2012 strerror(errno));
2013
2014 } else {
2015 sql_passwd_file_salt_flags = salt_flags;
2016 }
2017 }
2018
2019 } else {
2020 pr_log_debug(DEBUG1, MOD_SQL_PASSWD_VERSION
2021 ": unable to read SQLPasswordSaltFile '%s': %s", path,
2022 strerror(xerrno));
2023 }
2024 }
2025 }
2026
2027 #if defined(PR_USE_SODIUM)
2028 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordScrypt", FALSE);
2029 if (c != NULL) {
2030 sql_passwd_scrypt_hash_len = *((unsigned int *) c->argv[0]);
2031 }
2032
2033 # if defined(USE_SODIUM_ARGON2)
2034 c = find_config(main_server->conf, CONF_PARAM, "SQLPasswordArgon2", FALSE);
2035 if (c != NULL) {
2036 sql_passwd_argon2_hash_len = *((unsigned int *) c->argv[0]);
2037 }
2038 # endif /* USE_SODIUM_ARGON2 */
2039 #endif /* PR_USE_SODIUM */
2040
2041 return 0;
2042 }
2043
2044 /* Module API tables
2045 */
2046
2047 static conftable sql_passwd_conftab[] = {
2048 { "SQLPasswordArgon2", set_sqlpasswdargon2, NULL },
2049 { "SQLPasswordCost", set_sqlpasswdcost, NULL },
2050 { "SQLPasswordEncoding", set_sqlpasswdencoding, NULL },
2051 { "SQLPasswordEngine", set_sqlpasswdengine, NULL },
2052 { "SQLPasswordOptions", set_sqlpasswdoptions, NULL },
2053 { "SQLPasswordPBKDF2", set_sqlpasswdpbkdf2, NULL },
2054 { "SQLPasswordRounds", set_sqlpasswdrounds, NULL },
2055 { "SQLPasswordSaltEncoding", set_sqlpasswdsaltencoding, NULL },
2056 { "SQLPasswordSaltFile", set_sqlpasswdsaltfile, NULL },
2057 { "SQLPasswordScrypt", set_sqlpasswdscrypt, NULL },
2058 { "SQLPasswordUserSalt", set_sqlpasswdusersalt, NULL },
2059
2060 { NULL, NULL, NULL }
2061 };
2062
2063 static cmdtable sql_passwd_cmdtab[] = {
2064 { PRE_CMD, C_PASS, G_NONE, sql_passwd_pre_pass, FALSE, FALSE },
2065
2066 { 0, NULL }
2067 };
2068
2069 module sql_passwd_module = {
2070
2071 /* Always NULL */
2072 NULL, NULL,
2073
2074 /* Module API version */
2075 0x20,
2076
2077 /* Module name */
2078 "sql_passwd",
2079
2080 /* Module configuration directive table */
2081 sql_passwd_conftab,
2082
2083 /* Module command handler table */
2084 sql_passwd_cmdtab,
2085
2086 /* Module auth handler table */
2087 NULL,
2088
2089 /* Module initialization */
2090 sql_passwd_init,
2091
2092 /* Session initialization */
2093 sql_passwd_sess_init,
2094
2095 /* Module version */
2096 MOD_SQL_PASSWD_VERSION
2097 };