Debian Codegraph @

contrib/mod_auth_otp/mod_auth_otp.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_auth_otp
3 * Copyright (c) 2015-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 the
22 * source distribution.
23 *
24 * -----DO NOT EDIT BELOW THIS LINE-----
25 * $Archive: mod_auth_otp.a $
26 * $Libraries: -lcrypto$
27 */
28
29 #include "mod_auth_otp.h"
30 #include "db.h"
31 #include "otp.h"
32
33 #if defined(HAVE_SFTP)
34 # include "mod_sftp.h"
35 # include "contrib/mod_sftp/auth.h"
36 #endif /* HAVE_SFTP */
37
38 /* mod_auth_otp option flags */
39 #define AUTH_OTP_OPT_FTP_STANDARD_RESPONSE 0x001
40 #define AUTH_OTP_OPT_OPTIONAL_TABLE_ENTRY 0x002
41 #define AUTH_OTP_OPT_DISPLAY_VERIFICATION_CODE 0x004
42
43 #define AUTH_OTP_VERIFICATION_CODE_PROMPT "Verification code: "
44
45 /* From src/response.c */
46 extern pr_response_t *resp_list;
47
48 pool *auth_otp_pool = NULL;
49 int auth_otp_logfd = -1;
50 unsigned long auth_otp_opts = 0UL;
51 module auth_otp_module;
52
53 static authtable auth_otp_authtab[3];
54 static int auth_otp_engine = FALSE;
55 static unsigned int auth_otp_algo = AUTH_OTP_ALGO_TOTP_SHA1;
56 static struct auth_otp_db *dbh = NULL;
57 static config_rec *auth_otp_db_config = NULL;
58 static int auth_otp_auth_code = PR_AUTH_NOPWD;
59
60 /* Necessary prototypes */
61 static int auth_otp_sess_init(void);
62 static int handle_user_otp(pool *p, const char *user, const char *user_otp,
63 int authoritative);
64
65 #if defined(HAVE_SFTP)
66 /* mod_sftp support */
67 static int auth_otp_using_sftp = FALSE;
68 static sftp_kbdint_driver_t auth_otp_kbdint_driver;
69 #endif /* HAVE_SFTP */
70
71 static const char *trace_channel = "auth_otp";
72
73 #if defined(HAVE_SFTP)
74 static int auth_otp_sftp_allow_basic_kbdint(pool *p) {
75 register unsigned int i;
76 config_rec *c;
77 array_header *auth_chains;
78
79 c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthMethods", FALSE);
80 if (c == NULL) {
81 /* Basic "keyboard-interactive" authentication is allowed by default. */
82 return TRUE;
83 }
84
85 auth_chains = c->argv[0];
86
87 for (i = 0; i < auth_chains->nelts; i++) {
88 struct sftp_auth_chain *auth_chain;
89 struct sftp_auth_method *meth;
90
91 auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i];
92 if (auth_chain->methods->nelts > 1) {
93 continue;
94 }
95
96 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[0];
97 if (meth->method_id == SFTP_AUTH_FL_METH_KBDINT) {
98 return TRUE;
99 }
100 }
101
102 return FALSE;
103 }
104
105 static int auth_otp_kbdint_open(sftp_kbdint_driver_t *driver,
106 const char *user) {
107 const char *tabinfo;
108 int xerrno;
109
110 tabinfo = auth_otp_db_config->argv[0];
111
112 PRIVS_ROOT
113 dbh = auth_otp_db_open(driver->driver_pool, tabinfo);
114 xerrno = errno;
115 PRIVS_RELINQUISH
116
117 if (dbh == NULL) {
118 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
119 "unable to open AuthOTPTable: %s", strerror(xerrno));
120 errno = xerrno;
121 return -1;
122 }
123
124 driver->driver_pool = make_sub_pool(auth_otp_pool);
125 pr_pool_tag(driver->driver_pool, "AuthOTP keyboard-interactive driver pool");
126
127 return 0;
128 }
129
130 static int auth_otp_kbdint_authenticate(sftp_kbdint_driver_t *driver,
131 const char *user) {
132 int authoritative = FALSE, res, xerrno;
133 sftp_kbdint_challenge_t *challenge;
134 unsigned int recvd_count = 0;
135 const char **recvd_responses = NULL, *user_otp = NULL;
136
137 if (auth_otp_authtab[0].auth_flags & PR_AUTH_FL_REQUIRED) {
138 authoritative = TRUE;
139 }
140
141 /* Check first to see if we even have information for this user, for to
142 * use when verifying them. If not, then don't prompt the user for info
143 * that we know, a priori, we cannot verify.
144 */
145 res = auth_otp_db_rlock(dbh);
146 if (res < 0) {
147 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
148 "failed to read-lock AuthOTPTable: %s", strerror(errno));
149 }
150
151 res = auth_otp_db_have_user_info(driver->driver_pool, dbh, user);
152 xerrno = errno;
153
154 if (auth_otp_db_unlock(dbh) < 0) {
155 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
156 "failed to unlock AuthOTPTable: %s", strerror(errno));
157 }
158
159 if (res < 0) {
160 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
161 "no info for user '%s' found in AuthOTPTable, skipping "
162 "SSH2 keyboard-interactive challenge", user);
163
164 if (!(auth_otp_opts & AUTH_OTP_OPT_OPTIONAL_TABLE_ENTRY)) {
165 errno = xerrno;
166 return -1;
167 }
168
169 /* Note that we do not want to return zero here for missing entries
170 * blindly. Why not?
171 *
172 * Consider the case where kbdint authentication by itself is selected
173 * by the client. No matter what the client would send, it would "succeed"
174 * the authentication here, as there is no data for comparison.
175 *
176 * Issue #1319 concerns the use case where we want to support the opt-in
177 * nature of the module, and honor the REQUIRE_TABLE_ENTRY option for
178 * SSH logins.
179 *
180 * To support both cases, then, we double-check here the actual
181 * SFTPAuthMethods configuration. If it allows kbdint authentication
182 * by itself, then we return an error here, lest we allow clients in
183 * without proper authentication.
184 */
185 if (auth_otp_sftp_allow_basic_kbdint(driver->driver_pool) == TRUE) {
186 errno = ENOENT;
187 return -1;
188 }
189
190 return 0;
191 }
192
193 challenge = pcalloc(driver->driver_pool, sizeof(sftp_kbdint_challenge_t));
194 challenge->challenge = pstrdup(driver->driver_pool,
195 AUTH_OTP_VERIFICATION_CODE_PROMPT);
196 challenge->display_response = FALSE;
197
198 if (auth_otp_opts & AUTH_OTP_OPT_DISPLAY_VERIFICATION_CODE) {
199 challenge->display_response = TRUE;
200 }
201
202 if (sftp_kbdint_send_challenge(NULL, NULL, 1, challenge) < 0) {
203 xerrno = errno;
204
205 pr_trace_msg(trace_channel, 3,
206 "error sending keyboard-interactive challenges: %s", strerror(xerrno));
207
208 errno = xerrno;
209 return -1;
210 }
211
212 if (sftp_kbdint_recv_response(driver->driver_pool, 1,
213 &recvd_count, &recvd_responses) < 0) {
214 xerrno = errno;
215
216 pr_trace_msg(trace_channel, 3,
217 "error receiving keyboard-interactive responses: %s", strerror(xerrno));
218
219 errno = xerrno;
220 return -1;
221 }
222
223 user_otp = recvd_responses[0];
224 res = handle_user_otp(driver->driver_pool, user, user_otp, authoritative);
225 if (res == 1) {
226 return 0;
227 }
228
229 errno = EPERM;
230 return -1;
231 }
232
233 static int auth_otp_kbdint_close(sftp_kbdint_driver_t *driver) {
234 if (dbh != NULL) {
235 if (auth_otp_db_close(dbh) < 0) {
236 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
237 "error closing AuthOTPTable: %s", strerror(errno));
238 }
239
240 dbh = NULL;
241 }
242
243 if (driver->driver_pool != NULL) {
244 destroy_pool(driver->driver_pool);
245 driver->driver_pool = NULL;
246 }
247
248 auth_otp_auth_code = PR_AUTH_BADPWD;
249 return 0;
250 }
251 #endif /* HAVE_SFTP */
252
253 static int check_otp_code(pool *p, const char *user, const char *user_otp,
254 const unsigned char *secret, size_t secret_len, unsigned long counter) {
255 int res;
256 char code_str[9];
257 unsigned int code;
258
259 switch (auth_otp_algo) {
260 case AUTH_OTP_ALGO_TOTP_SHA1:
261 case AUTH_OTP_ALGO_TOTP_SHA256:
262 case AUTH_OTP_ALGO_TOTP_SHA512:
263 res = auth_otp_totp(p, secret, secret_len, counter, auth_otp_algo, &code);
264 if (res < 0) {
265 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
266 "error generating TOTP code for user '%s': %s", user,
267 strerror(errno));
268 }
269 break;
270
271 case AUTH_OTP_ALGO_HOTP:
272 res = auth_otp_hotp(p, secret, secret_len, counter, &code);
273 if (res < 0) {
274 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
275 "error generating HOTP code for user '%s': %s", user,
276 strerror(errno));
277 }
278 break;
279
280 default:
281 errno = EINVAL;
282 res = -1;
283 break;
284 }
285
286 if (res < 0) {
287 return -1;
288 }
289
290 memset(code_str, '\0', sizeof(code_str));
291
292 /* Note: If/when more than 6 digits are needed, the following format string
293 * would need to change to match.
294 */
295 pr_snprintf(code_str, sizeof(code_str)-1, "%06u", code);
296
297 pr_trace_msg(trace_channel, 13,
298 "computed code '%s', client sent code '%s'", code_str, user_otp);
299
300 res = pr_auth_check(p, code_str, user, user_otp);
301 if (res == PR_AUTH_OK ||
302 res == PR_AUTH_RFC2228_OK) {
303 return 0;
304 }
305
306 pr_trace_msg(trace_channel, 1, "error checking auth code: %d", res);
307 return -1;
308 }
309
310 static int update_otp_counter(pool *p, const char *user,
311 unsigned long next_counter) {
312 int res = 0;
313
314 if (auth_otp_algo == AUTH_OTP_ALGO_HOTP) {
315 int lock;
316
317 lock = auth_otp_db_wlock(dbh);
318 if (lock < 0) {
319 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
320 "failed to write-lock AuthOTPTable: %s", strerror(errno));
321 }
322
323 res = auth_otp_db_update_counter(dbh, user, next_counter);
324 if (res < 0) {
325 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
326 "error updating AuthOTPTable for HOTP counter for user '%s': %s",
327 user, strerror(errno));
328 }
329
330 lock = auth_otp_db_unlock(dbh);
331 if (lock < 0) {
332 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
333 "failed to unlock AuthOTPTable: %s", strerror(errno));
334 }
335 }
336
337 return res;
338 }
339
340 /* Sets the auth_otp_auth_code variable upon failure. */
341 static int handle_user_otp(pool *p, const char *user, const char *user_otp,
342 int authoritative) {
343 int res = 0, xerrno = 0;
344 const unsigned char *secret = NULL;
345 size_t secret_len = 0;
346 unsigned long counter = 0, *counter_ptr = NULL, next_counter = 0;
347
348 if (user_otp == NULL ||
349 (strlen(user_otp) == 0)) {
350 pr_trace_msg(trace_channel, 1,
351 "no OTP code provided by user, rejecting");
352 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
353 "FAILED: user '%s' provided invalid OTP code", user);
354 auth_otp_auth_code = PR_AUTH_BADPWD;
355 return -1;
356 }
357
358 switch (auth_otp_algo) {
359 case AUTH_OTP_ALGO_TOTP_SHA1:
360 case AUTH_OTP_ALGO_TOTP_SHA256:
361 case AUTH_OTP_ALGO_TOTP_SHA512: {
362 counter = (unsigned long) time(NULL);
363 break;
364 }
365
366 case AUTH_OTP_ALGO_HOTP:
367 counter_ptr = &counter;
368 break;
369
370 default:
371 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
372 "unsupported AuthOTPAlgorithm configured");
373 return 0;
374 }
375
376 res = auth_otp_db_rlock(dbh);
377 if (res < 0) {
378 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
379 "failed to read-lock AuthOTPTable: %s", strerror(errno));
380 }
381
382 res = auth_otp_db_get_user_info(p, dbh, user, &secret, &secret_len,
383 counter_ptr);
384 xerrno = errno;
385
386 if (auth_otp_db_unlock(dbh) < 0) {
387 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
388 "failed to unlock AuthOTPTable: %s", strerror(errno));
389 }
390
391 if (res < 0) {
392 if (xerrno == ENOENT) {
393 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
394 "user '%s' has no OTP info in AuthOTPTable", user);
395
396 } else {
397 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
398 "unable to retrieve OTP info for user '%s': %s", user,
399 strerror(xerrno));
400 }
401
402 /* If there was no entry found in the table (errno = ENOENT), should we
403 * returned ERROR or DECLINED?
404 *
405 * If we are not authoritative, then we returned DECLINED, regardless of
406 * the errno value, in order to allow other modules a chance at handling
407 * the authentication. This module can only be "authoritative" about
408 * OTP codes it can generate, and if there is no entry in the table,
409 * REQUIRE_TABLE_ENTRY option or not, we cannot generate a code for
410 * comparisons.
411 *
412 * If we ARE authoritative, and the REQUIRE_TABLE_ENTRY option is in
413 * effect, then we return ERROR -- this is how we require OTP codes for
414 * ALL users. Otherwise we return DECLINED, despite being authoritative,
415 * because again, we don't have the necessary data for computing the code.
416 */
417
418 if (authoritative == TRUE) {
419 if (!(auth_otp_opts & AUTH_OTP_OPT_OPTIONAL_TABLE_ENTRY)) {
420 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
421 "FAILED: user '%s' does not have entry in OTP tables", user);
422 auth_otp_auth_code = PR_AUTH_NOPWD;
423 return -1;
424 }
425 }
426
427 return 0;
428 }
429
430 res = check_otp_code(p, user, user_otp, secret, secret_len, counter);
431 if (res == 0) {
432 pr_memscrub((char *) secret, secret_len);
433
434 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
435 "SUCCESS: user '%s' provided valid OTP code", user);
436
437 /* XXX Update state with details about the expected OTP found,
438 * e.g. for clock drift.
439 */
440 update_otp_counter(p, user, counter + 1);
441
442 auth_otp_auth_code = PR_AUTH_OK;
443 return 1;
444 }
445
446 /* We SHOULD be allowing for clock skew/counter drift here. We
447 * currently check one window ahead AND behind; is this policy too lenient?
448 * RFC 6238, Section 5.2 recommends one window for network transmission
449 * delay (which assumes that the client's OTP is always "behind" the
450 * server's OTP).
451 *
452 * By checking one window ahead/behind, we allow for clock skew in either
453 * direction: server ahead of client (most likely), client ahead of server.
454 */
455 pr_trace_msg(trace_channel, 3,
456 "current counter check failed, checking one window behind");
457
458 switch (auth_otp_algo) {
459 case AUTH_OTP_ALGO_TOTP_SHA1:
460 case AUTH_OTP_ALGO_TOTP_SHA256:
461 case AUTH_OTP_ALGO_TOTP_SHA512:
462 next_counter = counter - AUTH_OTP_TOTP_TIMESTEP_SECS;
463 break;
464
465 case AUTH_OTP_ALGO_HOTP:
466 next_counter = counter - 1;
467 break;
468 }
469
470 res = check_otp_code(p, user, user_otp, secret, secret_len, next_counter);
471 if (res == 0) {
472 pr_memscrub((char *) secret, secret_len);
473
474 pr_trace_msg(trace_channel, 3,
475 "counter check SUCCEEDED for one counter window behind; client is "
476 "out-of-sync");
477
478 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
479 "SUCCESS: user '%s' provided valid OTP code", user);
480
481 /* XXX Update state with details about the expected OTP found,
482 * e.g. for clock drift, event counter increment, etc.
483 *
484 * Note that here, the client is "behind". Expected for TOTP, but not
485 * for HOTP. Hmm.
486 */
487 update_otp_counter(p, user, counter + 1);
488
489 auth_otp_auth_code = PR_AUTH_OK;
490 return 1;
491 }
492
493 pr_trace_msg(trace_channel, 3,
494 "counter one window behind check failed, checking one window ahead");
495
496 switch (auth_otp_algo) {
497 case AUTH_OTP_ALGO_TOTP_SHA1:
498 case AUTH_OTP_ALGO_TOTP_SHA256:
499 case AUTH_OTP_ALGO_TOTP_SHA512:
500 next_counter = counter + AUTH_OTP_TOTP_TIMESTEP_SECS;
501 break;
502
503 case AUTH_OTP_ALGO_HOTP:
504 next_counter = counter + 1;
505 break;
506 }
507
508 res = check_otp_code(p, user, user_otp, secret, secret_len, next_counter);
509 if (res == 0) {
510 pr_memscrub((char *) secret, secret_len);
511
512 pr_trace_msg(trace_channel, 3,
513 "counter check SUCCEEDED for one counter window ahead; client is "
514 "out-of-sync");
515
516 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
517 "SUCCESS: user '%s' provided valid OTP code", user);
518
519 /* XXX Update state with details about the expected OTP found,
520 * e.g. for clock drift, event counter increment, etc.
521 *
522 * Note that here, the client is "ahead". NOT expected for TOTP, but is
523 * for HOTP. Hmm.
524 */
525 update_otp_counter(p, user, counter + 1);
526
527 auth_otp_auth_code = PR_AUTH_OK;
528 return 1;
529 }
530
531 pr_memscrub((char *) secret, secret_len);
532
533 if (authoritative == TRUE) {
534 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
535 "FAILED: user '%s' provided invalid OTP code", user);
536 auth_otp_auth_code = PR_AUTH_BADPWD;
537 return -1;
538 }
539
540 return 0;
541 }
542
543 /* Authentication handlers
544 */
545
546 MODRET auth_otp_auth(cmd_rec *cmd) {
547 int authoritative = FALSE, res = 0;
548 char *user = NULL, *user_otp = NULL;
549
550 if (auth_otp_engine == FALSE ||
551 dbh == NULL) {
552 return PR_DECLINED(cmd);
553 }
554
555 user = cmd->argv[0];
556 user_otp = cmd->argv[1];
557
558 /* Figure out our default return style: whether or not we should allow
559 * other auth modules a shot at this user or not is controlled by adding
560 * '*' to a module name in the AuthOrder directive. By default, auth
561 * modules are not authoritative, and allow other auth modules a chance at
562 * authenticating the user. This is not the most secure configuration, but
563 * it allows things like AuthUserFile to work "out of the box".
564 */
565 if (auth_otp_authtab[0].auth_flags & PR_AUTH_FL_REQUIRED) {
566 authoritative = TRUE;
567 }
568
569 #if defined(HAVE_SFTP)
570 if (auth_otp_using_sftp == TRUE) {
571 const char *proto = NULL;
572
573 proto = pr_session_get_protocol(0);
574 if (strcmp(proto, "ssh2") == 0) {
575 if (auth_otp_auth_code == PR_AUTH_OK ||
576 auth_otp_auth_code == PR_AUTH_RFC2228_OK) {
577 /* Indicate HANDLED. */
578 res = 1;
579
580 } else if (auth_otp_auth_code == PR_AUTH_BADPWD) {
581 /* We've already tried, and the OTP was wrong. */
582 if (authoritative == TRUE) {
583 /* Indicate ERROR. */
584 res = -1;
585
586 } else {
587 /* Indicate DECLINED. */
588 res = 0;
589 }
590
591 } else {
592 res = handle_user_otp(cmd->tmp_pool, user, user_otp, authoritative);
593 }
594
595 } else {
596 res = handle_user_otp(cmd->tmp_pool, user, user_otp, authoritative);
597 }
598
599 } else {
600 res = handle_user_otp(cmd->tmp_pool, user, user_otp, authoritative);
601 }
602 #else
603 res = handle_user_otp(cmd->tmp_pool, user, user_otp, authoritative);
604 #endif /* HAVE_SFTP */
605
606 if (res == 1) {
607 session.auth_mech = "mod_auth_otp.c";
608 return PR_HANDLED(cmd);
609
610 } else if (res < 0) {
611 return PR_ERROR_INT(cmd, auth_otp_auth_code);
612 }
613
614 return PR_DECLINED(cmd);
615 }
616
617 MODRET auth_otp_chkpass(cmd_rec *cmd) {
618 const char *real_otp, *user, *user_otp;
619
620 if (auth_otp_engine == FALSE) {
621 return PR_DECLINED(cmd);
622 }
623
624 real_otp = cmd->argv[0];
625 user = cmd->argv[1];
626 user_otp = cmd->argv[2];
627
628 if (strcmp(real_otp, user_otp) == 0) {
629 return PR_HANDLED(cmd);
630 }
631
632 switch (auth_otp_algo) {
633 case AUTH_OTP_ALGO_TOTP_SHA1:
634 pr_trace_msg(trace_channel, 9,
635 "expected TOTP-SHA1 '%s', got '%s' for user '%s'", real_otp, user_otp,
636 user);
637 break;
638
639 case AUTH_OTP_ALGO_TOTP_SHA256:
640 pr_trace_msg(trace_channel, 9,
641 "expected TOTP-SHA256 '%s', got '%s' for user '%s'", real_otp, user_otp,
642 user);
643 break;
644
645 case AUTH_OTP_ALGO_TOTP_SHA512:
646 pr_trace_msg(trace_channel, 9,
647 "expected TOTP-SHA512 '%s', got '%s' for user '%s'", real_otp, user_otp,
648 user);
649 break;
650
651 case AUTH_OTP_ALGO_HOTP:
652 pr_trace_msg(trace_channel, 9,
653 "expected HOTP '%s', got '%s' for user '%s'", real_otp, user_otp, user);
654 break;
655 }
656
657 return PR_DECLINED(cmd);
658 }
659
660 /* Configuration handlers
661 */
662
663 /* usage: AuthOTPAlgorithm algo */
664 MODRET set_authotpalgo(cmd_rec *cmd) {
665 int algo = -1;
666 config_rec *c = NULL;
667
668 CHECK_ARGS(cmd, 1);
669 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
670
671 if (strcasecmp(cmd->argv[1], "hotp") == 0) {
672 algo = AUTH_OTP_ALGO_HOTP;
673
674 } else if (strcasecmp(cmd->argv[1], "totp") == 0 ||
675 strcasecmp(cmd->argv[1], "totp-sha1") == 0) {
676 algo = AUTH_OTP_ALGO_TOTP_SHA1;
677
678 #if defined(HAVE_SHA256_OPENSSL)
679 } else if (strcasecmp(cmd->argv[1], "totp-sha256") == 0) {
680 algo = AUTH_OTP_ALGO_TOTP_SHA256;
681 #endif /* SHA256 OpenSSL support */
682
683 #if defined(HAVE_SHA512_OPENSSL)
684 } else if (strcasecmp(cmd->argv[1], "totp-sha512") == 0) {
685 algo = AUTH_OTP_ALGO_TOTP_SHA512;
686 #endif /* SHA512 OpenSSL support */
687
688 } else {
689 CONF_ERROR(cmd, "expected supported OTP algorithm");
690 }
691
692 c = add_config_param(cmd->argv[0], 1, NULL);
693 c->argv[0] = pcalloc(c->pool, sizeof(int));
694 *((int *) c->argv[0]) = algo;
695
696 return PR_HANDLED(cmd);
697 }
698
699 /* usage: AuthOTPEngine on|off */
700 MODRET set_authotpengine(cmd_rec *cmd) {
701 int engine = -1;
702 config_rec *c = NULL;
703
704 CHECK_ARGS(cmd, 1);
705 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
706
707 engine = get_boolean(cmd, 1);
708 if (engine == -1) {
709 CONF_ERROR(cmd, "expected Boolean parameter");
710 }
711
712 c = add_config_param(cmd->argv[0], 1, NULL);
713 c->argv[0] = pcalloc(c->pool, sizeof(int));
714 *((int *) c->argv[0]) = engine;
715
716 return PR_HANDLED(cmd);
717 }
718
719 /* usage: AuthOTPLog path|"none" */
720 MODRET set_authotplog(cmd_rec *cmd) {
721 CHECK_ARGS(cmd, 1);
722 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
723
724 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
725 return PR_HANDLED(cmd);
726 }
727
728 /* usage: AuthOTPOptions opt1 opt2 ... */
729 MODRET set_authotpoptions(cmd_rec *cmd) {
730 config_rec *c = NULL;
731 register unsigned int i = 0;
732 unsigned long opts = 0UL;
733
734 if (cmd->argc-1 == 0) {
735 CONF_ERROR(cmd, "wrong number of parameters");
736 }
737
738 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
739
740 c = add_config_param(cmd->argv[0], 1, NULL);
741
742 for (i = 1; i < cmd->argc; i++) {
743 if (strcmp(cmd->argv[i], "FTPStandardResponse") == 0 ||
744 strcmp(cmd->argv[i], "StandardResponse") == 0) {
745 opts |= AUTH_OTP_OPT_FTP_STANDARD_RESPONSE;
746
747 } else if (strcmp(cmd->argv[i], "RequireTableEntry") == 0) {
748 /* This is the default; we keep this here for backward compatibility;
749 * see Issue #1562.
750 */
751
752 } else if (strcmp(cmd->argv[i], "DisplayVerificationCode") == 0) {
753 opts |= AUTH_OTP_OPT_DISPLAY_VERIFICATION_CODE;
754
755 } else if (strcmp(cmd->argv[i], "OptionalTableEntry") == 0) {
756 opts |= AUTH_OTP_OPT_OPTIONAL_TABLE_ENTRY;
757
758 } else {
759 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown AuthOTPOption: '",
760 cmd->argv[i], "'", NULL));
761 }
762 }
763
764 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
765 *((unsigned long *) c->argv[0]) = opts;
766
767 if (pr_module_exists("mod_ifsession.c")) {
768 /* These are needed in case this directive is used with mod_ifsession
769 * configuration.
770 */
771 c->flags |= CF_MULTI;
772 }
773
774 return PR_HANDLED(cmd);
775 }
776
777 /* usage: AuthOTPTable sql:/... */
778 MODRET set_authotptable(cmd_rec *cmd) {
779 char *ptr = NULL;
780
781 CHECK_ARGS(cmd, 1);
782 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
783
784 /* Separate the parameter into the separate pieces. The parameter is
785 * given as one string to enhance its similarity to URL syntax.
786 */
787 ptr = strchr(cmd->argv[1], ':');
788 if (ptr == NULL) {
789 CONF_ERROR(cmd, "badly formatted parameter");
790 }
791
792 if (strncasecmp(cmd->argv[1], "sql:/", 5) != 0) {
793 CONF_ERROR(cmd, "badly formatted parameter");
794 }
795
796 *ptr++ = '\0';
797
798 add_config_param_str(cmd->argv[0], 1, ptr);
799 return PR_HANDLED(cmd);
800 }
801
802 /* usage: AuthOTPTableLock path */
803 MODRET set_authotptablelock(cmd_rec *cmd) {
804 CHECK_ARGS(cmd, 1);
805 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
806
807 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
808 return PR_HANDLED(cmd);
809 }
810
811 /* Command handlers
812 */
813
814 MODRET auth_otp_post_pass(cmd_rec *cmd) {
815 if (auth_otp_engine == FALSE) {
816 return PR_DECLINED(cmd);
817 }
818
819 if (dbh != NULL) {
820 if (auth_otp_db_close(dbh) < 0) {
821 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
822 "error closing AuthOTPTable: %s", strerror(errno));
823 }
824
825 dbh = NULL;
826 }
827
828 return PR_DECLINED(cmd);
829 }
830
831 MODRET auth_otp_pre_user(cmd_rec *cmd) {
832 const char *tabinfo;
833 int xerrno;
834
835 if (auth_otp_engine == FALSE) {
836 return PR_DECLINED(cmd);
837 }
838
839 tabinfo = auth_otp_db_config->argv[0];
840
841 PRIVS_ROOT
842 dbh = auth_otp_db_open(auth_otp_pool, tabinfo);
843 xerrno = errno;
844 PRIVS_RELINQUISH
845
846 if (dbh == NULL) {
847 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
848 "unable to open AuthOTPTable: %s", strerror(xerrno));
849 }
850
851 return PR_DECLINED(cmd);
852 }
853
854 MODRET auth_otp_post_user(cmd_rec *cmd) {
855 const char *user;
856
857 if (auth_otp_engine == FALSE ||
858 dbh == NULL) {
859 return PR_DECLINED(cmd);
860 }
861
862 user = cmd->argv[1];
863
864 /* We want to respond using the normal 331 response code, BUT we'd like
865 * to change the response message. Note that this might be considered
866 * an information leak, i.e. we're leaking the server's expectation of
867 * OTPs from the connecting client.
868 */
869
870 if (!(auth_otp_opts & AUTH_OTP_OPT_FTP_STANDARD_RESPONSE)) {
871 pr_response_clear(&resp_list);
872 #if defined(HAVE_SFTP)
873 /* Note: for some reason, when building with mod_sftp, the '_' function
874 * used for localization is not resolvable by the linker, thus we
875 * work around the problem. For now.
876 */
877 pr_response_add(R_331, "One-time password required for %s", user);
878 #else
879 pr_response_add(R_331, _("One-time password required for %s"), user);
880 #endif /* HAVE_SFTP */
881 }
882
883 return PR_DECLINED(cmd);
884 }
885
886 /* Event listeners
887 */
888
889 static void auth_otp_exit_ev(const void *event_data, void *user_data) {
890 if (dbh != NULL) {
891 (void) auth_otp_db_close(dbh);
892 dbh = NULL;
893 }
894 }
895
896 #if defined(PR_SHARED_MODULE)
897 static void auth_otp_mod_unload_ev(const void *event_data, void *user_data) {
898 if (strcmp("mod_auth_otp.c", (const char *) event_data) == 0) {
899 # if defined(HAVE_SFTP)
900 if (pr_module_exists("mod_sftp.c") == TRUE) {
901 sftp_kbdint_unregister_driver("auth_otp");
902 }
903 # endif /* HAVE_SFTP */
904 pr_event_unregister(&auth_otp_module, NULL, NULL);
905 }
906 }
907 #endif /* PR_SHARED_MODULE */
908
909 static void auth_otp_sess_reinit_ev(const void *event_data, void *user_data) {
910 int res;
911
912 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
913
914 pr_event_unregister(&auth_otp_module, "core.exit", auth_otp_exit_ev);
915 pr_event_unregister(&auth_otp_module, "core.session-reinit",
916 auth_otp_sess_reinit_ev);
917
918 auth_otp_engine = FALSE;
919 auth_otp_opts = 0UL;
920 auth_otp_algo = AUTH_OTP_ALGO_TOTP_SHA1;
921 auth_otp_db_config = NULL;
922
923 if (auth_otp_logfd >= 0) {
924 (void) close(auth_otp_logfd);
925 auth_otp_logfd = -1;
926 }
927
928 #if defined(HAVE_SFTP)
929 auth_otp_using_sftp = FALSE;
930 (void) sftp_kbdint_register_driver("auth_otp", &auth_otp_kbdint_driver);
931 #endif /* HAVE_SFTP */
932
933 if (auth_otp_pool != NULL) {
934 destroy_pool(auth_otp_pool);
935 }
936
937 res = auth_otp_sess_init();
938 if (res < 0) {
939 pr_session_disconnect(&auth_otp_module,
940 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
941 }
942 }
943
944 /* Initialization routines
945 */
946
947 static int auth_otp_init(void) {
948
949 #if defined(PR_SHARED_MODULE)
950 pr_event_register(&auth_otp_module, "core.module-unload",
951 auth_otp_mod_unload_ev, NULL);
952 #endif /* PR_SHARED_MODULE */
953
954 if (pr_module_exists("mod_sql.c") == FALSE) {
955 pr_log_pri(PR_LOG_NOTICE, MOD_AUTH_OTP_VERSION
956 ": Missing required 'mod_sql.c'; HOTP/TOTP logins will FAIL");
957 }
958
959 #if defined(HAVE_SFTP)
960 auth_otp_using_sftp = pr_module_exists("mod_sftp.c");
961 if (auth_otp_using_sftp) {
962 /* Prepare our keyboard-interactive driver. */
963 memset(&auth_otp_kbdint_driver, 0, sizeof(auth_otp_kbdint_driver));
964 auth_otp_kbdint_driver.open = auth_otp_kbdint_open;
965 auth_otp_kbdint_driver.authenticate = auth_otp_kbdint_authenticate;
966 auth_otp_kbdint_driver.close = auth_otp_kbdint_close;
967
968 if (sftp_kbdint_register_driver("auth_otp", &auth_otp_kbdint_driver) < 0) {
969 int xerrno = errno;
970
971 pr_log_pri(PR_LOG_NOTICE, MOD_AUTH_OTP_VERSION
972 ": notice: error registering 'keyboard-interactive' driver: %s",
973 strerror(xerrno));
974
975 errno = xerrno;
976 return -1;
977 }
978
979 } else {
980 pr_log_debug(DEBUG1, MOD_AUTH_OTP_VERSION
981 ": mod_sftp not loaded, skipping keyboard-interactive support");
982 }
983 #endif /* HAVE_SFTP */
984
985 return 0;
986 }
987
988 static int auth_otp_sess_init(void) {
989 config_rec *c;
990
991 pr_event_register(&auth_otp_module, "core.session-reinit",
992 auth_otp_sess_reinit_ev, NULL);
993
994 if (pr_auth_add_auth_only_module("mod_auth_otp.c") < 0 &&
995 errno != EEXIST) {
996 pr_log_pri(PR_LOG_NOTICE, MOD_AUTH_OTP_VERSION
997 ": unable to add 'mod_auth_otp.c' as an auth-only module: %s",
998 strerror(errno));
999
1000 errno = EPERM;
1001 return -1;
1002 }
1003
1004 /* XXX Can we handle both FTP and SSH2 connections in the same module? */
1005
1006 c = find_config(main_server->conf, CONF_PARAM, "AuthOTPEngine", FALSE);
1007 if (c != NULL) {
1008 auth_otp_engine = *((int *) c->argv[0]);
1009 }
1010
1011 if (auth_otp_engine == FALSE) {
1012 #if defined(HAVE_SFTP)
1013 if (auth_otp_using_sftp == TRUE) {
1014 sftp_kbdint_unregister_driver("auth_otp");
1015 }
1016 #endif /* HAVE_SFTP */
1017 return 0;
1018 }
1019
1020 c = find_config(main_server->conf, CONF_PARAM, "AuthOTPLog", FALSE);
1021 if (c != NULL) {
1022 char *path;
1023
1024 path = c->argv[0];
1025 if (strncasecmp(path, "none", 5) != 0) {
1026 int res, xerrno;
1027
1028 pr_signals_block();
1029 PRIVS_ROOT
1030 res = pr_log_openfile(path, &auth_otp_logfd, 0600);
1031 xerrno = errno;
1032 PRIVS_RELINQUISH
1033 pr_signals_unblock();
1034
1035 if (res < 0) {
1036 if (res == -1) {
1037 pr_log_pri(PR_LOG_NOTICE, MOD_AUTH_OTP_VERSION
1038 ": notice: unable to open AuthOTPLog '%s': %s", path,
1039 strerror(xerrno));
1040
1041 } else if (res == PR_LOG_WRITABLE_DIR) {
1042 pr_log_pri(PR_LOG_WARNING, MOD_AUTH_OTP_VERSION
1043 ": notice: unable to open AuthOTPLog '%s': parent directory is "
1044 "world-writable", path);
1045
1046 } else if (res == PR_LOG_SYMLINK) {
1047 pr_log_pri(PR_LOG_WARNING, MOD_AUTH_OTP_VERSION
1048 ": notice: unable to open AuthOTPLog '%s': cannot log to a symlink",
1049 path);
1050 }
1051 }
1052 }
1053 }
1054
1055 c = find_config(main_server->conf, CONF_PARAM, "AuthOTPTable", FALSE);
1056 if (c == NULL) {
1057 pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
1058 "missing required AuthOTPTable directive, disabling module");
1059 pr_log_pri(PR_LOG_NOTICE, MOD_AUTH_OTP_VERSION
1060 ": missing required AuthOTPTable directive, disabling module");
1061 auth_otp_engine = FALSE;
1062 (void) close(auth_otp_logfd);
1063 auth_otp_logfd = -1;
1064
1065 #if defined(HAVE_SFTP)
1066 if (auth_otp_using_sftp == TRUE) {
1067 sftp_kbdint_unregister_driver("auth_otp");
1068 }
1069 #endif /* HAVE_SFTP */
1070
1071 return 0;
1072 }
1073 auth_otp_db_config = c;
1074
1075 auth_otp_pool = make_sub_pool(session.pool);
1076 pr_pool_tag(auth_otp_pool, MOD_AUTH_OTP_VERSION);
1077
1078 c = find_config(main_server->conf, CONF_PARAM, "AuthOTPAlgorithm", FALSE);
1079 if (c != NULL) {
1080 auth_otp_algo = *((int *) c->argv[0]);
1081 }
1082
1083 c = find_config(main_server->conf, CONF_PARAM, "AuthOTPOptions", FALSE);
1084 while (c != NULL) {
1085 unsigned long opts = 0;
1086
1087 pr_signals_handle();
1088
1089 opts = *((unsigned long *) c->argv[0]);
1090 auth_otp_opts |= opts;
1091
1092 c = find_config_next(c, c->next, CONF_PARAM, "AuthOTPOptions", FALSE);
1093 }
1094
1095 pr_event_register(&auth_otp_module, "core.exit", auth_otp_exit_ev, NULL);
1096 return 0;
1097 }
1098
1099 static cmdtable auth_otp_cmdtab[] = {
1100 { PRE_CMD, C_USER, G_NONE, auth_otp_pre_user, FALSE, FALSE },
1101 { POST_CMD, C_USER, G_NONE, auth_otp_post_user, FALSE, FALSE },
1102 { POST_CMD, C_PASS, G_NONE, auth_otp_post_pass, FALSE, FALSE },
1103 { POST_CMD_ERR, C_PASS, G_NONE, auth_otp_post_pass, FALSE, FALSE },
1104 { 0, NULL },
1105 };
1106
1107 static authtable auth_otp_authtab[] = {
1108 { 0, "auth", auth_otp_auth },
1109 { 0, "check", auth_otp_chkpass },
1110 { 0, NULL, NULL }
1111 };
1112
1113 static conftable auth_otp_conftab[] = {
1114 { "AuthOTPAlgorithm", set_authotpalgo, NULL },
1115 { "AuthOTPEngine", set_authotpengine, NULL },
1116 { "AuthOTPLog", set_authotplog, NULL },
1117 { "AuthOTPOptions", set_authotpoptions, NULL },
1118 { "AuthOTPTable", set_authotptable, NULL },
1119 { "AuthOTPTableLock", set_authotptablelock, NULL },
1120 { NULL, NULL, NULL }
1121 };
1122
1123 module auth_otp_module = {
1124 NULL, NULL,
1125
1126 /* Module API version */
1127 0x20,
1128
1129 /* Module name */
1130 "auth_otp",
1131
1132 /* Module configuration handler table */
1133 auth_otp_conftab,
1134
1135 /* Module command handler table */
1136 auth_otp_cmdtab,
1137
1138 /* Module authentication handler table */
1139 auth_otp_authtab,
1140
1141 /* Module initialization */
1142 auth_otp_init,
1143
1144 /* Session initialization */
1145 auth_otp_sess_init,
1146
1147 /* Module version */
1148 MOD_AUTH_OTP_VERSION
1149 };