Debian Codegraph @

contrib/mod_sftp/auth.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp user authentication
3 * Copyright (c) 2008-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
25 #include "mod_sftp.h"
26 #include "ssh2.h"
27 #include "packet.h"
28 #include "msg.h"
29 #include "disconnect.h"
30 #include "interop.h"
31 #include "auth.h"
32 #include "crypto.h"
33 #include "cipher.h"
34 #include "mac.h"
35 #include "compress.h"
36 #include "session.h"
37 #include "keys.h"
38 #include "keystore.h"
39 #include "kbdint.h"
40 #include "utf8.h"
41 #include "display.h"
42
43 /* From response.c */
44 extern pr_response_t *resp_list, *resp_err_list;
45
46 /* This value of 6 is the same default as OpenSSH's MaxAuthTries. */
47 static unsigned int auth_attempts_max = 6;
48 static unsigned int auth_attempts = 0;
49
50 static pool *auth_pool = NULL;
51 static char *auth_default_dir = NULL;
52
53 static const char *auth_avail_meths = NULL;
54
55 /* This is a bitset of flags, used to check on the authentication method
56 * used by the client, to see if the requested method is even enabled.
57 */
58 static unsigned int auth_meths_enabled_flags = 0;
59
60 static array_header *auth_chains = NULL;
61
62 static int auth_sent_userauth_banner_file = FALSE;
63 static int auth_sent_userauth_success = FALSE;
64
65 static const char *auth_user = NULL;
66 static const char *auth_service = NULL;
67
68 struct sftp_auth_publickey {
69 const char *algo;
70 enum sftp_key_type_e key_type;
71 };
72
73 static struct sftp_auth_publickey publickeys[] = {
74 { "ssh-dss", SFTP_KEY_DSA },
75 { "ssh-rsa", SFTP_KEY_RSA },
76 #if defined(HAVE_SHA256_OPENSSL)
77 { "rsa-sha2-256", SFTP_KEY_RSA_SHA256 },
78 #endif /* HAVE_SHA256_OPENSSL */
79 #if defined(HAVE_SHA512_OPENSSL)
80 { "rsa-sha2-512", SFTP_KEY_RSA_SHA512 },
81 #endif /* HAVE_SHA512_OPENSSL */
82 #if defined(PR_USE_OPENSSL_ECC)
83 { "ecdsa-sha2-nistp256", SFTP_KEY_ECDSA_256 },
84 { "ecdsa-sha2-nistp384", SFTP_KEY_ECDSA_384 },
85 { "ecdsa-sha2-nistp521", SFTP_KEY_ECDSA_521 },
86 { "[email protected]", SFTP_KEY_ECDSA_256_SK },
87 #endif /* PR_USE_OPENSSL_ECC */
88 #if defined(PR_USE_SODIUM)
89 { "ssh-ed25519", SFTP_KEY_ED25519 },
90 { "[email protected]", SFTP_KEY_ED25519_SK },
91 #endif /* PR_USE_SODIUM */
92 #if defined(HAVE_X448_OPENSSL)
93 { "ssh-ed448", SFTP_KEY_ED448 },
94 #endif /* HAVE_X448_OPENSSL */
95 { NULL, 0 }
96 };
97
98 /* Customizable callback for handling successful SSH authentication. */
99 static int (*success_handler)(pool *, const char *) = NULL;
100
101 static const char *trace_channel = "ssh2";
102
103 static struct passwd *dup_passwd(pool *p, struct passwd *pw) {
104 struct passwd *res = NULL;
105
106 res = pcalloc(p, sizeof(struct passwd));
107 res->pw_name = pstrdup(p, pw->pw_name);
108 res->pw_uid = pw->pw_uid;
109 res->pw_gid = pw->pw_gid;
110 res->pw_gecos = pstrdup(p, pw->pw_gecos);
111 res->pw_dir = pstrdup(p, pw->pw_dir);
112 res->pw_shell = pstrdup(p, pw->pw_shell);
113
114 return res;
115 }
116
117 static void ensure_open_passwd(pool *p) {
118 pr_auth_setpwent(p);
119 pr_auth_setgrent(p);
120
121 pr_auth_getpwent(p);
122 pr_auth_getgrent(p);
123 }
124
125 static const char *get_default_chdir(pool *p) {
126 config_rec *c;
127 const char *path = NULL;
128
129 c = find_config(main_server->conf, CONF_PARAM, "DefaultChdir", FALSE);
130 while (c != NULL) {
131 int res;
132
133 pr_signals_handle();
134
135 if (c->argc < 2) {
136 path = c->argv[0];
137 break;
138 }
139
140 res = pr_expr_eval_group_and(((char **) c->argv) + 1);
141 if (res) {
142 path = c->argv[0];
143 break;
144 }
145
146 c = find_config_next(c, c->next, CONF_PARAM, "DefaultChdir", FALSE);
147 }
148
149 if (path &&
150 *path != '/' &&
151 *path != '~') {
152 path = pdircat(p, session.cwd, path, NULL);
153 }
154
155 if (path != NULL) {
156 path = path_subst_uservar(p, &path);
157 }
158
159 return path;
160 }
161
162 static const char *get_default_root(pool *p) {
163 config_rec *c;
164 const char *path = NULL;
165
166 c = find_config(main_server->conf, CONF_PARAM, "DefaultRoot", FALSE);
167 while (c) {
168 int res;
169
170 pr_signals_handle();
171
172 if (c->argc < 2) {
173 path = c->argv[0];
174 break;
175 }
176
177 res = pr_expr_eval_group_and(((char **) c->argv) + 1);
178 if (res) {
179 path = c->argv[0];
180 break;
181 }
182
183 c = find_config_next(c, c->next, CONF_PARAM, "DefaultRoot", FALSE);
184 }
185
186 if (path) {
187 path = path_subst_uservar(p, &path);
188
189 if (strncmp(path, "/", 2) == 0) {
190 path = NULL;
191
192 } else {
193 char *real_path;
194 int xerrno = 0;
195
196 PRIVS_USER
197 real_path = dir_realpath(p, path);
198 if (real_path == NULL) {
199 xerrno = errno;
200 }
201 PRIVS_RELINQUISH
202
203 if (real_path) {
204 path = real_path;
205
206 } else {
207 char interp_path[PR_TUNABLE_PATH_MAX + 1];
208
209 memset(interp_path, '\0', sizeof(interp_path));
210 (void) pr_fs_interpolate(path, interp_path, sizeof(interp_path) - 1);
211
212 pr_log_pri(PR_LOG_NOTICE,
213 "notice: unable to use DefaultRoot %s (resolved to '%s'): %s",
214 path, interp_path,
215 strerror(xerrno));
216 }
217 }
218 }
219
220 return path;
221 }
222
223 static void set_userauth_methods(void) {
224 config_rec *c;
225 register unsigned int i;
226
227 if (auth_chains != NULL) {
228 return;
229 }
230
231 auth_avail_meths = NULL;
232 auth_meths_enabled_flags = 0;
233
234 c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthMethods", FALSE);
235 if (c != NULL) {
236 /* Sanity checking of the configured methods is done in the postparse
237 * event listener; we can use this as-is without fear.
238 */
239 auth_chains = c->argv[0];
240
241 } else {
242 struct sftp_auth_chain *auth_chain;
243
244 auth_chains = make_array(auth_pool, 0, sizeof(struct sftp_auth_chain *));
245
246 c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthorizedUserKeys",
247 FALSE);
248 if (c != NULL) {
249 auth_chain = sftp_auth_chain_alloc(auth_pool);
250 sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_PUBLICKEY,
251 "publickey", NULL);
252 *((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain;
253
254 } else {
255 pr_trace_msg(trace_channel, 9, "no SFTPAuthorizedUserKeys configured, "
256 "not offering 'publickey' authentication");
257 }
258
259 c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthorizedHostKeys",
260 FALSE);
261 if (c != NULL) {
262 auth_chain = sftp_auth_chain_alloc(auth_pool);
263 sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_HOSTBASED,
264 "hostbased", NULL);
265 *((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain;
266
267 } else {
268 pr_trace_msg(trace_channel, 9, "no SFTPAuthorizedHostKeys configured, "
269 "not offering 'hostbased' authentication");
270 }
271
272 if (sftp_kbdint_have_drivers() > 0) {
273 auth_chain = sftp_auth_chain_alloc(auth_pool);
274 sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_KBDINT,
275 "keyboard-interactive", NULL);
276 *((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain;
277
278 } else {
279 pr_trace_msg(trace_channel, 9, "no kbdint drivers present, not "
280 "offering 'keyboard-interactive' authentication");
281 }
282
283 /* The 'password' method is always available. */
284 auth_chain = sftp_auth_chain_alloc(auth_pool);
285 sftp_auth_chain_add_method(auth_chain, SFTP_AUTH_FL_METH_PASSWORD,
286 "password", NULL);
287 *((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain;
288 }
289
290 for (i = 0 ; i < auth_chains->nelts; i++) {
291 struct sftp_auth_chain *auth_chain;
292 struct sftp_auth_method *meth;
293
294 auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i];
295 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[0];
296
297 if (!(auth_meths_enabled_flags & meth->method_id)) {
298 auth_meths_enabled_flags |= meth->method_id;
299
300 if (auth_avail_meths != NULL) {
301 auth_avail_meths = pstrcat(auth_pool, auth_avail_meths, ",",
302 meth->method_name, NULL);
303 } else {
304 auth_avail_meths = meth->method_name;
305 }
306 }
307 }
308
309 pr_trace_msg(trace_channel, 9, "offering authentication methods: %s",
310 auth_avail_meths);
311
312 /* Prepare the method-specific APIs, too. */
313 if (sftp_auth_hostbased_init(auth_pool) < 0) {
314 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
315 "error preparing for 'hostbased' authentication: %s", strerror(errno));
316 }
317
318 if (sftp_auth_kbdint_init(auth_pool) < 0) {
319 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
320 "error preparing for 'keyboard-interactive' authentication: %s",
321 strerror(errno));
322 }
323
324 if (sftp_auth_password_init(auth_pool) < 0) {
325 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
326 "error preparing for 'password' authentication: %s", strerror(errno));
327 }
328
329 if (sftp_auth_publickey_init(auth_pool) < 0) {
330 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
331 "error preparing for 'publickey' authentication: %s", strerror(errno));
332 }
333 }
334
335 static int setup_env(pool *p, const char *user) {
336 struct passwd *pw;
337 config_rec *c;
338 int login_acl, i, res, root_revoke = TRUE, show_symlinks = FALSE, xerrno;
339 int auth_code = PR_AUTH_OK;
340 struct stat st;
341 const char *default_chdir, *default_root, *home_dir;
342 const char *sess_ttyname = NULL, *xferlog = NULL;
343 cmd_rec *cmd;
344
345 session.hide_password = TRUE;
346
347 pw = pr_auth_getpwnam(p, user);
348 if (pw == NULL) {
349 xerrno = errno;
350 auth_code = PR_AUTH_NOPWD;
351
352 /* This is highly unlikely to happen...*/
353 pr_log_auth(PR_LOG_NOTICE,
354 "USER %s (Login failed): Unable to retrieve user information: %s", user,
355 strerror(xerrno));
356 pr_event_generate("mod_auth.authentication-code", &auth_code);
357
358 errno = xerrno;
359 return -1;
360 }
361
362 pw = dup_passwd(p, pw);
363
364 if (pw->pw_uid == PR_ROOT_UID) {
365 pr_event_generate("mod_auth.root-login", NULL);
366
367 c = find_config(main_server->conf, CONF_PARAM, "RootLogin", FALSE);
368 if (c) {
369 if (*((int *) c->argv[0]) == FALSE) {
370 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
371 "root login attempted, denied by RootLogin configuration");
372 pr_log_auth(PR_LOG_NOTICE, "SECURITY VIOLATION: Root login attempted");
373 return -1;
374 }
375
376 } else {
377 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
378 "root login attempted, denied by RootLogin configuration");
379 pr_log_auth(PR_LOG_NOTICE, "SECURITY VIOLATION: Root login attempted");
380 return -1;
381 }
382 }
383
384 res = pr_auth_is_valid_shell(main_server->conf, pw->pw_shell);
385 if (res == FALSE) {
386 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
387 "authentication for user '%s' failed: Invalid shell", user);
388 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Invalid shell: '%s'",
389 user, pw->pw_shell);
390 return -1;
391 }
392
393 res = pr_auth_banned_by_ftpusers(main_server->conf, pw->pw_name);
394 if (res == TRUE) {
395 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
396 "authentication for user '%s' failed: User in " PR_FTPUSERS_PATH, user);
397 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): User in "
398 PR_FTPUSERS_PATH, pw->pw_name);
399 return -1;
400 }
401
402 session.user = pstrdup(p, pw->pw_name);
403 session.user_homedir = pstrdup(p, pw->pw_dir);
404 session.group = pstrdup(p, pr_auth_gid2name(p, pw->pw_gid));
405
406 session.login_uid = pw->pw_uid;
407 session.login_gid = pw->pw_gid;
408
409 /* Note that we do NOT need to explicitly call pr_auth_get_home() here;
410 * that call already happens in the pr_auth_getpwnam() call above. To
411 * call it again here would cause the home directory to be rewritten twice;
412 * depending on the configured rewrite rules, that would lead to an
413 * incorrect value (Bug#3421).
414 */
415
416 pw->pw_dir = (char *) path_subst_uservar(p, (const char **) &pw->pw_dir);
417
418 if (session.gids == NULL &&
419 session.groups == NULL) {
420 res = pr_auth_getgroups(p, pw->pw_name, &session.gids, &session.groups);
421 if (res < 1) {
422 /* If no supplemental groups are provided, default to using the process
423 * primary GID as the supplemental group. This prevents access
424 * regressions as seen in Issue #1830.
425 */
426 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
427 "no supplemental groups found for user '%s', "
428 "using primary group %s (GID %lu)", pw->pw_name, session.group,
429 (unsigned long) session.login_gid);
430
431 session.gids = make_array(p, 2, sizeof(gid_t));
432 session.groups = make_array(p, 2, sizeof(char *));
433
434 *((gid_t *) push_array(session.gids)) = session.login_gid;
435 *((char **) push_array(session.groups)) = pstrdup(p, session.group);
436 }
437 }
438
439 login_acl = login_check_limits(main_server->conf, FALSE, TRUE, &i);
440 if (!login_acl) {
441 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Limit configuration "
442 "denies login", user);
443 return -1;
444 }
445
446 PRIVS_USER
447 home_dir = dir_realpath(p, pw->pw_dir);
448 PRIVS_RELINQUISH
449
450 if (home_dir != NULL) {
451 sstrncpy(session.cwd, home_dir, sizeof(session.cwd));
452
453 } else {
454 sstrncpy(session.cwd, pw->pw_dir, sizeof(session.cwd));
455 }
456
457 c = find_config(main_server->conf, CONF_PARAM, "CreateHome", FALSE);
458 if (c != NULL) {
459 if (*((unsigned char *) c->argv[0]) == TRUE) {
460 if (create_home(p, session.cwd, user, pw->pw_uid, pw->pw_gid) < 0) {
461 return -1;
462 }
463 }
464 }
465
466 default_chdir = get_default_chdir(p);
467 if (default_chdir != NULL) {
468 default_chdir = dir_abs_path(p, default_chdir, TRUE);
469 sstrncpy(session.cwd, default_chdir, sizeof(session.cwd));
470 }
471
472 /* Make sure any <Limit LOGIN> sections still allow access. */
473 login_acl = login_check_limits(main_server->conf, FALSE, TRUE, &i);
474 if (!login_acl) {
475 pr_log_auth(PR_LOG_NOTICE, "USER %s: Limit configuration denies login",
476 user);
477 return -1;
478 }
479
480 resolve_deferred_dirs(main_server);
481 fixup_dirs(main_server, CF_DEFER);
482
483 session.wtmp_log = TRUE;
484
485 c = find_config(main_server->conf, CONF_PARAM, "WtmpLog", FALSE);
486 if (c != NULL &&
487 *((unsigned char *) c->argv[0]) == FALSE) {
488 session.wtmp_log = FALSE;
489 }
490
491 /* As per Bug#3482, we need to disable WtmpLog for FreeBSD 9.0, as
492 * an interim measure.
493 *
494 * The issue is that some platforms update multiple files for a single
495 * pututxline(3) call; proftpd tries to update those files manually,
496 * do to chroots (after which a pututxline(3) call will fail). A proper
497 * solution requires a separate process, running with the correct
498 * privileges, which would handle wtmp logging. The proftpd session
499 * processes would send messages to this logging daemon (via Unix domain
500 * socket, or FIFO, or TCP socket).
501 *
502 * Also note that this hack to disable WtmpLog may need to be extended
503 * to other platforms in the future.
504 */
505 #if defined(HAVE_UTMPX_H) && \
506 defined(__FreeBSD_version) && __FreeBSD_version >= 900007
507 if (session.wtmp_log == TRUE) {
508 session.wtmp_log = FALSE;
509
510 pr_log_debug(DEBUG5,
511 "WtpmLog automatically disabled; see Bug#3482 for details");
512 }
513 #endif
514
515 PRIVS_ROOT
516
517 if (session.wtmp_log) {
518 sess_ttyname = pr_session_get_ttyname(p);
519
520 log_wtmp(sess_ttyname, session.user, session.c->remote_name,
521 session.c->remote_addr);
522 }
523
524 #if defined(PR_USE_LASTLOG)
525 c = find_config(main_server->conf, CONF_PARAM, "UseLastlog", FALSE);
526 if (c != NULL &&
527 *((unsigned char *) c->argv[0]) == TRUE) {
528 if (sess_ttyname == NULL) {
529 sess_ttyname = pr_session_get_ttyname(p);
530 }
531
532 log_lastlog(pw->pw_uid, session.user, sess_ttyname,
533 session.c->remote_addr);
534 }
535 #endif /* PR_USE_LASTLOG */
536
537 c = find_config(main_server->conf, CONF_PARAM, "TransferLog", FALSE);
538 if (c == NULL) {
539 xferlog = PR_XFERLOG_PATH;
540
541 } else {
542 xferlog = c->argv[0];
543 }
544
545 if (strcasecmp(xferlog, "none") == 0) {
546 xferlog_open(NULL);
547
548 } else {
549 xferlog_open(xferlog);
550 }
551
552 pr_event_generate("mod_auth.authentication-code", &auth_code);
553
554 res = set_groups(p, pw->pw_gid, session.gids);
555 xerrno = errno;
556 PRIVS_RELINQUISH
557
558 if (res < 0) {
559 if (xerrno != ENOSYS) {
560 pr_log_pri(PR_LOG_WARNING, "unable to set process groups: %s",
561 strerror(xerrno));
562 }
563 }
564
565 default_root = get_default_root(session.pool);
566 if (default_root != NULL) {
567 ensure_open_passwd(p);
568
569 if (pr_auth_chroot(default_root) < 0) {
570 pr_log_pri(PR_LOG_WARNING, "unable to set DefaultRoot directory '%s'",
571 default_root);
572 return -1;
573 }
574
575 if (strncmp(session.cwd, default_root, strlen(default_root)) == 0) {
576 char *new_cwd;
577
578 new_cwd = &session.cwd[strlen(default_root)];
579
580 if (*new_cwd == '/') {
581 new_cwd++;
582 }
583 session.cwd[0] = '/';
584
585 sstrncpy(&session.cwd[1], new_cwd, sizeof(session.cwd));
586 }
587 }
588
589 pr_signals_block();
590 PRIVS_ROOT
591 PRIVS_SETUP(pw->pw_uid, pw->pw_gid)
592 pr_signals_unblock();
593
594 /* Should we give up root privs completely here? */
595 c = find_config(main_server->conf, CONF_PARAM, "RootRevoke", FALSE);
596 if (c != NULL) {
597 root_revoke = *((int *) c->argv[0]);
598
599 if (root_revoke == FALSE) {
600 pr_log_debug(DEBUG8, MOD_SFTP_VERSION
601 ": retaining root privileges per RootRevoke setting");
602 }
603
604 } else {
605 /* Do a recursive look for any UserOwner directives; honoring that
606 * configuration also requires root privs.
607 */
608 c = find_config(main_server->conf, CONF_PARAM, "UserOwner", TRUE);
609 if (c != NULL) {
610 pr_log_debug(DEBUG9, MOD_SFTP_VERSION
611 ": retaining root privileges per UserOwner setting");
612 root_revoke = FALSE;
613 }
614 }
615
616 if (root_revoke) {
617 PRIVS_ROOT
618 PRIVS_REVOKE
619 session.disable_id_switching = TRUE;
620 }
621
622 #ifdef HAVE_GETEUID
623 if (getegid() != pw->pw_gid ||
624 geteuid() != pw->pw_uid) {
625 pr_log_pri(PR_LOG_WARNING,
626 "process effective IDs do not match expected IDs");
627 return -1;
628 }
629 #endif
630
631 if (pw->pw_dir == NULL ||
632 strncmp(pw->pw_dir, "", 1) == 0) {
633 pr_log_pri(PR_LOG_WARNING, "Home directory for user '%s' is NULL/empty",
634 session.user);
635 return -1;
636 }
637
638 c = find_config(main_server->conf, CONF_PARAM, "ShowSymlinks", FALSE);
639 if (c != NULL) {
640 if (*((unsigned char *) c->argv[0]) == TRUE) {
641 show_symlinks = TRUE;
642 }
643 }
644
645 if (pr_fsio_chdir_canon(session.cwd, !show_symlinks) < 0) {
646 xerrno = errno;
647
648 if (session.chroot_path != NULL ||
649 default_root != NULL) {
650
651 pr_log_debug(DEBUG2, "unable to chdir to %s (%s), defaulting to chroot "
652 "directory %s", session.cwd, strerror(xerrno),
653 (session.chroot_path ? session.chroot_path : default_root));
654
655 if (pr_fsio_chdir_canon("/", !show_symlinks) == -1) {
656 xerrno = errno;
657
658 pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"/\") failed: %s", session.user,
659 strerror(xerrno));
660 errno = xerrno;
661 return -1;
662 }
663
664 } else if (default_chdir) {
665 pr_log_debug(DEBUG2, "unable to chdir to %s (%s), defaulting to home "
666 "directory %s", session.cwd, strerror(errno), pw->pw_dir);
667
668 if (pr_fsio_chdir_canon(pw->pw_dir, !show_symlinks) == -1) {
669 xerrno = errno;
670
671 pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"%s\") failed: %s", session.user,
672 session.cwd, strerror(xerrno));
673 errno = xerrno;
674 return -1;
675 }
676
677 } else {
678 pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"%s\") failed: %s", session.user,
679 session.cwd, strerror(xerrno));
680 errno = xerrno;
681 return -1;
682 }
683
684 } else {
685 pr_log_debug(DEBUG10, "changed directory to '%s'", session.cwd);
686 }
687
688 sstrncpy(session.cwd, pr_fs_getcwd(), sizeof(session.cwd));
689 sstrncpy(session.vwd, pr_fs_getvwd(), sizeof(session.vwd));
690
691 /* Make sure directory config pointers are set correctly */
692 cmd = pr_cmd_alloc(p, 1, C_PASS);
693 cmd->arg = "";
694 dir_check_full(p, cmd, G_NONE, session.cwd, NULL);
695
696 session.proc_prefix = pstrdup(session.pool, session.c->remote_name);
697 session.sf_flags = 0;
698
699 pr_log_auth(PR_LOG_INFO, "USER %s: Login successful", user);
700
701 if (pw->pw_uid == PR_ROOT_UID) {
702 pr_log_auth(PR_LOG_WARNING, "ROOT SFTP login successful");
703 }
704
705 if (pr_fsio_stat(session.cwd, &st) != -1) {
706 build_dyn_config2(p, session.cwd, &st);
707 }
708
709 pr_scoreboard_entry_update(session.pid,
710 PR_SCORE_USER, session.user,
711 PR_SCORE_CWD, session.cwd,
712 NULL);
713
714 pr_timer_remove(PR_TIMER_LOGIN, ANY_MODULE);
715
716 auth_default_dir = pstrdup(session.pool, pr_fs_getvwd());
717
718 session.user = pstrdup(session.pool, session.user);
719
720 if (session.user_homedir != NULL) {
721 session.user_homedir = pstrdup(session.pool, session.user_homedir);
722 }
723
724 if (session.group != NULL) {
725 session.group = pstrdup(session.pool, session.group);
726 }
727
728 if (session.gids != NULL) {
729 session.gids = copy_array(session.pool, session.gids);
730 }
731
732 session.groups = copy_array_str(session.pool, session.groups);
733
734 pr_resolve_fs_map();
735 return 0;
736 }
737
738 static int send_userauth_banner_file(void) {
739 struct ssh2_packet *pkt;
740 char *path;
741 unsigned char *buf, *ptr;
742 const char *msg;
743 int res;
744 uint32_t buflen, bufsz;
745 config_rec *c;
746 pr_fh_t *fh;
747 pool *sub_pool;
748 struct stat st;
749
750 if (auth_sent_userauth_banner_file) {
751 /* Already sent the banner; no need to do it again. */
752 return 0;
753 }
754
755 c = find_config(main_server->conf, CONF_PARAM, "SFTPDisplayBanner", FALSE);
756 if (c == NULL) {
757 return 0;
758 }
759 path = c->argv[0];
760
761 if (!sftp_interop_supports_feature(SFTP_SSH2_FEAT_USERAUTH_BANNER)) {
762 pr_trace_msg(trace_channel, 3, "unable to send SFTPDisplayBanner '%s': "
763 "USERAUTH_BANNER supported by client", path);
764 return 0;
765 }
766
767 fh = pr_fsio_open_canon(path, O_RDONLY);
768 if (fh == NULL) {
769 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
770 "error opening SFTPDisplayBanner '%s': %s", path, strerror(errno));
771 return 0;
772 }
773
774 res = pr_fsio_fstat(fh, &st);
775 if (res < 0) {
776 int xerrno = errno;
777
778 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
779 "unable to stat SFTPDisplayBanner '%s': %s", path, strerror(xerrno));
780
781 pr_fsio_close(fh);
782 return 0;
783 }
784
785 if (S_ISDIR(st.st_mode)) {
786 int xerrno = EISDIR;
787
788 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
789 "unable to use SFTPDisplayBanner '%s': %s", path, strerror(xerrno));
790
791 pr_fsio_close(fh);
792 return 0;
793 }
794
795 sub_pool = make_sub_pool(auth_pool);
796 pr_pool_tag(sub_pool, "SSH2 auth banner pool");
797
798 msg = sftp_display_fh_get_msg(sub_pool, fh);
799 pr_fsio_close(fh);
800
801 if (msg == NULL) {
802 destroy_pool(sub_pool);
803 return -1;
804 }
805
806 pr_trace_msg(trace_channel, 3,
807 "sending userauth banner from SFTPDisplayBanner file '%s'", path);
808
809 pkt = sftp_ssh2_packet_create(sub_pool);
810
811 buflen = bufsz = strlen(msg) + 32;
812 ptr = buf = palloc(pkt->pool, bufsz);
813
814 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_BANNER);
815 sftp_msg_write_string(&buf, &buflen, msg);
816
817 /* XXX locale of banner */
818 sftp_msg_write_string(&buf, &buflen, "");
819
820 pkt->payload = ptr;
821 pkt->payload_len = (bufsz - buflen);
822
823 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
824 destroy_pool(pkt->pool);
825
826 if (res < 0) {
827 destroy_pool(sub_pool);
828 return -1;
829 }
830
831 auth_sent_userauth_banner_file = TRUE;
832 destroy_pool(sub_pool);
833 return 0;
834 }
835
836 static int send_userauth_failure(char *failed_meth) {
837 struct ssh2_packet *pkt;
838 unsigned char *buf, *ptr;
839 char *meths;
840 uint32_t buflen, bufsz = 1024;
841 int res;
842
843 pkt = sftp_ssh2_packet_create(auth_pool);
844
845 if (failed_meth) {
846 register unsigned int i;
847
848 auth_avail_meths = NULL;
849 auth_meths_enabled_flags = 0;
850
851 for (i = 0 ; i < auth_chains->nelts; i++) {
852 register unsigned int j;
853 struct sftp_auth_chain *auth_chain;
854 struct sftp_auth_method *meth = NULL;
855
856 pr_signals_handle();
857 auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i];
858
859 for (j = 0; j < auth_chain->methods->nelts; j++) {
860 struct sftp_auth_method *m;
861
862 m = ((struct sftp_auth_method **) auth_chain->methods->elts)[j];
863 if (m->succeeded != TRUE &&
864 m->failed != TRUE) {
865 meth = m;
866 break;
867 }
868 }
869
870 if (meth == NULL) {
871 /* All of the methods in this list have failed; check the next
872 * list.
873 */
874 continue;
875 }
876
877 if (strcmp(meth->method_name, failed_meth) != 0) {
878 if (!(auth_meths_enabled_flags & meth->method_id)) {
879 auth_meths_enabled_flags |= meth->method_id;
880
881 if (auth_avail_meths != NULL) {
882 auth_avail_meths = pstrcat(auth_pool, auth_avail_meths, ",",
883 meth->method_name, NULL);
884 } else {
885 auth_avail_meths = meth->method_name;
886 }
887 }
888 } else {
889 meth->failed = TRUE;
890 }
891 }
892
893 if (auth_avail_meths == NULL) {
894 /* If there are no more auth methods available, we have to disconnect. */
895 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
896 "no more auth methods available, disconnecting");
897 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,
898 NULL);
899 }
900
901 } else {
902 if (auth_avail_meths == NULL) {
903 /* This situation can happen when the authentication method succeeded,
904 * BUT the subsequent login actions failed (such as failing to chroot,
905 * no such home directory, etc etc). But we still need a list
906 * of authentication methods to send back to the client in this message;
907 * we thus use the empty string.
908 */
909 auth_avail_meths = "";
910 }
911 }
912
913 meths = pstrdup(pkt->pool, auth_avail_meths);
914
915 buflen = bufsz;
916 ptr = buf = palloc(pkt->pool, bufsz);
917
918 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_FAILURE);
919 sftp_msg_write_string(&buf, &buflen, meths);
920 sftp_msg_write_bool(&buf, &buflen, FALSE);
921
922 pkt->payload = ptr;
923 pkt->payload_len = (bufsz - buflen);
924
925 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
926 "sending userauth failure; remaining userauth methods: %s", meths);
927
928 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
929 if (res < 0) {
930 destroy_pool(pkt->pool);
931 return -1;
932 }
933
934 return 0;
935 }
936
937 static int send_userauth_success(void) {
938 struct ssh2_packet *pkt;
939 unsigned char *buf, *ptr;
940 uint32_t buflen, bufsz = 1024;
941 int res;
942
943 if (auth_sent_userauth_success) {
944 return 0;
945 }
946
947 pkt = sftp_ssh2_packet_create(auth_pool);
948
949 buflen = bufsz;
950 ptr = buf = palloc(pkt->pool, bufsz);
951
952 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_SUCCESS);
953
954 pkt->payload = ptr;
955 pkt->payload_len = (bufsz - buflen);
956
957 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
958 "sending userauth success");
959
960 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
961 if (res < 0) {
962 destroy_pool(pkt->pool);
963 return -1;
964 }
965
966 auth_sent_userauth_success = TRUE;
967
968 /* We call the compression init routines here as well, in case the
969 * client selected "delayed" compression.
970 */
971 sftp_compress_init_read(SFTP_COMPRESS_FL_AUTHENTICATED);
972 sftp_compress_init_write(SFTP_COMPRESS_FL_AUTHENTICATED);
973
974 return 0;
975 }
976
977 static int set_userauth_success(const char *succeeded_meth) {
978 register unsigned int i;
979 int completed = FALSE;
980
981 auth_avail_meths = NULL;
982 auth_meths_enabled_flags = 0;
983
984 for (i = 0 ; i < auth_chains->nelts; i++) {
985 register unsigned int j;
986 struct sftp_auth_chain *auth_chain;
987
988 pr_signals_handle();
989 auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i];
990
991 for (j = 0; j < auth_chain->methods->nelts; j++) {
992 struct sftp_auth_method *meth = NULL;
993
994 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j];
995 if (meth->succeeded != TRUE &&
996 meth->failed != TRUE) {
997
998 if (strcmp(meth->method_name, succeeded_meth) == 0) {
999 /* TODO: What about submethods, for kbdint drivers? */
1000 meth->succeeded = TRUE;
1001 }
1002
1003 /* Add the next method in the list (if any) to the available methods. */
1004 j++;
1005 if (j < auth_chain->methods->nelts) {
1006 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j];
1007
1008 if (!(auth_meths_enabled_flags & meth->method_id)) {
1009 auth_meths_enabled_flags |= meth->method_id;
1010
1011 if (auth_avail_meths != NULL) {
1012 auth_avail_meths = pstrcat(auth_pool, auth_avail_meths, ",",
1013 meth->method_name, NULL);
1014 } else {
1015 auth_avail_meths = meth->method_name;
1016 }
1017 }
1018 }
1019
1020 break;
1021 }
1022 }
1023 }
1024
1025 /* Now, having marked the list items that have succeeded, check each list
1026 * to see if any has been completed.
1027 */
1028
1029 for (i = 0 ; i < auth_chains->nelts; i++) {
1030 register unsigned int j;
1031 struct sftp_auth_chain *auth_chain;
1032 int ok = TRUE;
1033
1034 pr_signals_handle();
1035 auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i];
1036
1037 for (j = 0; j < auth_chain->methods->nelts; j++) {
1038 struct sftp_auth_method *meth = NULL;
1039
1040 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j];
1041 if (meth->succeeded != TRUE) {
1042 ok = FALSE;
1043 break;
1044 }
1045 }
1046
1047 if (ok == TRUE) {
1048 /* We have a successfully completed list! */
1049 auth_chain->completed = completed = TRUE;
1050 break;
1051 }
1052 }
1053
1054 if (completed == TRUE) {
1055 return 1;
1056 }
1057
1058 if (auth_avail_meths == NULL) {
1059 /* If there are no more auth methods available, we have to disconnect. */
1060 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1061 "no more auth methods available, disconnecting");
1062 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,
1063 NULL);
1064 }
1065
1066 return 0;
1067 }
1068
1069 static int send_userauth_methods(char partial_success) {
1070 struct ssh2_packet *pkt;
1071 unsigned char *buf, *ptr;
1072 uint32_t buflen, bufsz = 1024;
1073 int res;
1074
1075 pkt = sftp_ssh2_packet_create(auth_pool);
1076
1077 buflen = bufsz;
1078 ptr = buf = palloc(pkt->pool, bufsz);
1079
1080 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1081 "sending acceptable userauth methods: %s", auth_avail_meths);
1082
1083 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_FAILURE);
1084 sftp_msg_write_string(&buf, &buflen, auth_avail_meths);
1085 sftp_msg_write_bool(&buf, &buflen, partial_success);
1086
1087 pkt->payload = ptr;
1088 pkt->payload_len = (bufsz - buflen);
1089
1090 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
1091 if (res < 0) {
1092 destroy_pool(pkt->pool);
1093 return -1;
1094 }
1095
1096 return 0;
1097 }
1098
1099 static void add_cmd_note(cmd_rec *cmd, const char *key, const char *val) {
1100 int res;
1101
1102 res = pr_table_add(cmd->notes, key, val, 0);
1103 if (res < 0 &&
1104 errno != EEXIST) {
1105 pr_trace_msg(trace_channel, 19, "error adding '%s' command note: %s",
1106 key, strerror(errno));
1107 }
1108 }
1109
1110 static void remove_cmd_note(cmd_rec *cmd, const char *key) {
1111 (void) pr_table_remove(cmd->notes, key, NULL);
1112 }
1113
1114 static void dispatch_cmd_err(cmd_rec *cmd) {
1115 pr_response_add_err(R_530, "Login incorrect.");
1116 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1117 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1118 pr_response_clear(&resp_err_list);
1119 }
1120
1121 static void dispatch_nonfatal_pass(cmd_rec *cmd, const char *method) {
1122 const char *note = "mod_sftp.nonfatal-attempt";
1123
1124 add_cmd_note(cmd, note, method);
1125 dispatch_cmd_err(cmd);
1126 remove_cmd_note(cmd, note);
1127 }
1128
1129 static void incr_auth_attempts(const char *user, cmd_rec *pass_cmd) {
1130 auth_attempts++;
1131
1132 if (auth_attempts_max > 0 &&
1133 auth_attempts >= auth_attempts_max) {
1134 pr_log_auth(PR_LOG_NOTICE,
1135 "Maximum login attempts (%u) exceeded, connection refused",
1136 auth_attempts_max);
1137 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1138 "Maximum login attempts (%u) exceeded, refusing connection for user '%s'",
1139 auth_attempts_max, user);
1140
1141 /* Ensure that the "mod_auth.orig_user" session note exists before
1142 * dispatching to the PASS error handlers. Some of them look for this
1143 * session note (Issue #693).
1144 */
1145 if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0 &&
1146 errno != EEXIST) {
1147 pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in "
1148 "session.notes: %s", strerror(errno));
1149 }
1150
1151 dispatch_cmd_err(pass_cmd);
1152
1153 /* Note that the mod_auth LOG_CMD_ERR handler for PASS commands will remove
1154 * the "mod_auth.orig-user" note as cleanup for future PASS commands, which
1155 * may use different user names. Unfortunately, some event listeners
1156 * in mod_ban may expect/require the presence of that note; see
1157 * Issue #2009.
1158 *
1159 * Thus we add the note back here, if necessary, and remove it again
1160 * afterward.
1161 */
1162 if (pr_table_get(session.notes, "mod_auth.orig-user", NULL) == NULL) {
1163 (void) pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0);
1164 }
1165
1166 pr_event_generate("mod_auth.max-login-attempts", session.c);
1167 pr_table_remove(session.notes, "mod_auth.orig-user", NULL);
1168
1169 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
1170 }
1171 }
1172
1173 static int handle_auth_success(pool *p, const char *user) {
1174 int res = 0;
1175
1176 if (success_handler != NULL) {
1177 res = (success_handler)(p, user);
1178 }
1179
1180 return res;
1181 }
1182
1183 /* Return -1 on error, 0 to continue, and 1 if the authentication succeeded. */
1184 static int handle_userauth_req(struct ssh2_packet *pkt, char **service) {
1185 unsigned char *buf;
1186 const char *orig_user, *user;
1187 char *method;
1188 uint32_t buflen;
1189 cmd_rec *cmd, *user_cmd, *pass_cmd;
1190 int res, send_userauth_fail = FALSE;
1191 config_rec *c;
1192
1193 buf = pkt->payload;
1194 buflen = pkt->payload_len;
1195
1196 orig_user = sftp_msg_read_string(pkt->pool, &buf, &buflen);
1197
1198 user_cmd = pr_cmd_alloc(pkt->pool, 2, pstrdup(pkt->pool, C_USER), orig_user);
1199 user_cmd->arg = (char *) orig_user;
1200
1201 pass_cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, C_PASS));
1202 pass_cmd->arg = pstrdup(pkt->pool, "(hidden)");
1203
1204 /* Dispatch these as PRE_CMDs, so that mod_delay's tactics can be used
1205 * to ameliorate any timing-based attacks.
1206 */
1207 if (pr_cmd_dispatch_phase(user_cmd, PRE_CMD, 0) < 0) {
1208 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1209 "authentication request for user '%s' blocked by '%s' handler",
1210 orig_user, (char *) user_cmd->argv[0]);
1211
1212 dispatch_cmd_err(user_cmd);
1213 return -1;
1214 }
1215
1216 if (strcmp(orig_user, user_cmd->arg) == 0) {
1217 user = orig_user;
1218
1219 } else {
1220 user = user_cmd->arg;
1221 }
1222
1223 if (auth_user != NULL) {
1224 /* Check to see if the client has requested a different user name in
1225 * this USERAUTH_REQUEST. As per Section 5 of RFC4252, if the user
1226 * name changes, we can disconnect the client.
1227 */
1228 if (strcmp(orig_user, auth_user) != 0) {
1229 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1230 "client used different user name '%s' in USERAUTH_REQUEST (was '%s'), "
1231 "disconnecting", orig_user, auth_user);
1232
1233 dispatch_cmd_err(user_cmd);
1234 return -1;
1235 }
1236
1237 } else {
1238 auth_user = pstrdup(auth_pool, orig_user);
1239 }
1240
1241 *service = sftp_msg_read_string(pkt->pool, &buf, &buflen);
1242 if (auth_service != NULL) {
1243 /* Check to see if the client has requested a different service name in
1244 * this USERAUTH_REQUEST. As per Section 5 of RFC4252, if the service
1245 * name changes, we can disconnect the client.
1246 */
1247 if (strcmp(*service, auth_service) != 0) {
1248 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1249 "client used different service name '%s' in USERAUTH_REQUEST (was "
1250 "'%s'), disconnecting", *service, auth_service);
1251
1252 dispatch_cmd_err(user_cmd);
1253 return -1;
1254 }
1255
1256 } else {
1257
1258 /* Check to see if the requested service is one that we support.
1259 *
1260 * As far as I can tell, the only defined 'service names' are:
1261 *
1262 * ssh-userauth (RFC4252)
1263 * ssh-connection (RFC4254)
1264 *
1265 * If the requested service name is NOT one of the above,
1266 * we should disconnect, as recommended by RFC4252.
1267 */
1268
1269 if (strncmp(*service, "ssh-userauth", 13) == 0 ||
1270 strncmp(*service, "ssh-connection", 15) == 0) {
1271 auth_service = pstrdup(auth_pool, *service);
1272
1273 } else {
1274 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1275 "client requested unknown/unsupported service name '%s' in "
1276 "USERAUTH_REQUEST, disconnecting", *service);
1277
1278 dispatch_cmd_err(user_cmd);
1279 return -1;
1280 }
1281 }
1282
1283 pr_response_add(R_331, "Password required for %s", user);
1284 pr_cmd_dispatch_phase(user_cmd, POST_CMD, 0);
1285 pr_cmd_dispatch_phase(user_cmd, LOG_CMD, 0);
1286 pr_response_clear(&resp_list);
1287
1288 method = sftp_msg_read_string(pkt->pool, &buf, &buflen);
1289
1290 pr_trace_msg(trace_channel, 10, "auth requested for user '%s', service '%s', "
1291 "using method '%s'", user, *service, method);
1292
1293 (void) pr_table_remove(session.notes, "mod_auth.orig-user", NULL);
1294 if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0 &&
1295 errno != EEXIST) {
1296 pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in "
1297 "session.notes: %s", strerror(errno));
1298 }
1299
1300 cmd = pr_cmd_alloc(pkt->pool, 3, pstrdup(pkt->pool, "USERAUTH_REQUEST"),
1301 pstrdup(pkt->pool, user), pstrdup(pkt->pool, method));
1302 cmd->arg = pstrcat(pkt->pool, user, " ", method, NULL);
1303 cmd->cmd_class = CL_AUTH|CL_SSH;
1304 cmd->cmd_id = SFTP_CMD_ID;
1305
1306 if (auth_attempts_max > 0 &&
1307 auth_attempts > auth_attempts_max) {
1308 pr_log_auth(PR_LOG_NOTICE,
1309 "Maximum login attempts (%u) exceeded, connection refused",
1310 auth_attempts_max);
1311 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1312 "Maximum login attempts (%u) exceeded, refusing connection for user '%s'",
1313 auth_attempts_max, user);
1314
1315 /* Ensure that the "mod_auth.orig_user" session note exists before
1316 * dispatching to the PASS error handlers. Some of them look for this
1317 * session note (Issue #693).
1318 */
1319 if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0 &&
1320 errno != EEXIST) {
1321 pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in "
1322 "session.notes: %s", strerror(errno));
1323 }
1324
1325 dispatch_cmd_err(pass_cmd);
1326 pr_event_generate("mod_auth.max-login-attempts", session.c);
1327 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
1328 }
1329
1330 set_userauth_methods();
1331
1332 /* Populate the environment for the requested authentication method,
1333 * so that it can be recorded regardless of success/failure. Note that
1334 * the "none" method is not really expected by users, so we deliberately
1335 * skip/omit that value.
1336 */
1337 if (strcmp(method, "none") != 0) {
1338 pr_env_unset(cmd->pool, "SFTP_USER_AUTH_METHOD");
1339 pr_env_set(cmd->pool, "SFTP_USER_AUTH_METHOD", method);
1340 (void) pr_table_add_dup(session.notes, "SFTP_USER_AUTH_METHOD", method, 0);
1341 }
1342
1343 /* In order for the actual user password (if any) to be populated properly
1344 * in the PRE_CMD PASS dispatch (e.g. this is needed for modules like
1345 * mod_radius; see Bug#3676), the PRE_CMD PASS dispatch needs to happen
1346 * in the method-specific auth functions, not here.
1347 *
1348 * Thus this code will look a little strange; the PRE_CMD PASS dispatching
1349 * happens in the auth-specific functions, but the POST/LOG_CMD PASS
1350 * dispatching will happen here.
1351 */
1352
1353 if (strncmp(method, "none", 5) == 0) {
1354 /* If the client requested the "none" auth method at this point, then
1355 * the list of authentication methods supported by the server is being
1356 * queried.
1357 */
1358 if (send_userauth_methods(FALSE) < 0) {
1359 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1360 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1361 return -1;
1362 }
1363
1364 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1365 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1366 pr_response_clear(&resp_list);
1367
1368 return 0;
1369
1370 } else if (strncmp(method, "publickey", 10) == 0) {
1371 if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_PUBLICKEY) {
1372 int xerrno;
1373
1374 res = sftp_auth_publickey(pkt, pass_cmd, orig_user, user, *service,
1375 &buf, &buflen, &send_userauth_fail);
1376 xerrno = errno;
1377
1378 if (res < 0) {
1379 pr_event_generate("mod_sftp.ssh2.auth-publickey.failed", NULL);
1380
1381 } else if (res == 1) {
1382 pr_event_generate("mod_sftp.ssh2.auth-publickey", NULL);
1383 }
1384
1385 errno = xerrno;
1386
1387 } else {
1388 pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method);
1389
1390 if (send_userauth_methods(FALSE) < 0) {
1391 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1392 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1393 return -1;
1394 }
1395
1396 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1397 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1398
1399 incr_auth_attempts(user, pass_cmd);
1400 return 0;
1401 }
1402
1403 } else if (strncmp(method, "keyboard-interactive", 21) == 0) {
1404 if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_KBDINT) {
1405 int xerrno = errno;
1406
1407 res = sftp_auth_kbdint(pkt, pass_cmd, orig_user, user, *service,
1408 &buf, &buflen, &send_userauth_fail);
1409 xerrno = errno;
1410
1411 if (res < 0) {
1412 pr_event_generate("mod_sftp.ssh2.auth-kbdint.failed", NULL);
1413
1414 } else if (res == 1) {
1415 pr_event_generate("mod_sftp.ssh2.auth-kbdint", NULL);
1416 }
1417
1418 errno = xerrno;
1419
1420 } else {
1421 pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method);
1422
1423 if (send_userauth_methods(FALSE) < 0) {
1424 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1425 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1426 return -1;
1427 }
1428
1429 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1430 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1431
1432 incr_auth_attempts(user, pass_cmd);
1433 return 0;
1434 }
1435
1436 } else if (strncmp(method, "password", 9) == 0) {
1437 if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_PASSWORD) {
1438 int xerrno;
1439
1440 res = sftp_auth_password(pkt, pass_cmd, orig_user, user, *service,
1441 &buf, &buflen, &send_userauth_fail);
1442 xerrno = errno;
1443
1444 if (res < 0) {
1445 pr_event_generate("mod_sftp.ssh2.auth-password.failed", NULL);
1446
1447 } else if (res == 1) {
1448 pr_event_generate("mod_sftp.ssh2.auth-password", NULL);
1449 }
1450
1451 errno = xerrno;
1452
1453 } else {
1454 pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method);
1455
1456 if (send_userauth_methods(FALSE) < 0) {
1457 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1458 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1459 return -1;
1460 }
1461
1462 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1463 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1464
1465 incr_auth_attempts(user, pass_cmd);
1466 return 0;
1467 }
1468
1469 } else if (strncmp(method, "hostbased", 10) == 0) {
1470 if (auth_meths_enabled_flags & SFTP_AUTH_FL_METH_HOSTBASED) {
1471 int xerrno;
1472
1473 res = sftp_auth_hostbased(pkt, pass_cmd, orig_user, user, *service,
1474 &buf, &buflen, &send_userauth_fail);
1475 xerrno = errno;
1476
1477 if (res < 0) {
1478 pr_event_generate("mod_sftp.ssh2.auth-hostbased.failed", NULL);
1479
1480 } else if (res == 1) {
1481 pr_event_generate("mod_sftp.ssh2.auth-hostbased", NULL);
1482 }
1483
1484 errno = xerrno;
1485
1486 } else {
1487 pr_trace_msg(trace_channel, 10, "auth method '%s' not enabled", method);
1488
1489 if (send_userauth_methods(FALSE) < 0) {
1490 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1491 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1492 return -1;
1493 }
1494
1495 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1496 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1497
1498 incr_auth_attempts(user, pass_cmd);
1499 return 0;
1500 }
1501
1502 } else {
1503 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1504 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1505
1506 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1507 "unsupported authentication method '%s' requested", method);
1508
1509 incr_auth_attempts(user, pass_cmd);
1510 return 0;
1511 }
1512
1513 /* Make sure that the password cmd_rec arg points back to the static
1514 * string for passwords.
1515 */
1516 pass_cmd->arg = pstrdup(pkt->pool, "(hidden)");
1517
1518 if (res <= 0) {
1519 int xerrno = errno;
1520
1521 /* For a failed authentication _attempt_ (but not yet a real error), we
1522 * attach a note to this fake PASS "error" command dispatch, so that
1523 * interested handlers can use it to determine if the command they receive
1524 * is a real error or not, for e.g. logging/accounting purposes
1525 * (Issue #693).
1526 */
1527 if (res < 0) {
1528 dispatch_cmd_err(pass_cmd);
1529
1530 } else {
1531 dispatch_nonfatal_pass(pass_cmd, method);
1532 }
1533
1534 pr_cmd_dispatch_phase(cmd, res == 0 ? POST_CMD : POST_CMD_ERR, 0);
1535 pr_cmd_dispatch_phase(cmd, res == 0 ? LOG_CMD : LOG_CMD_ERR, 0);
1536
1537 if (send_userauth_fail) {
1538 errno = xerrno;
1539
1540 if (send_userauth_failure(errno != EPERM ? NULL : method) < 0) {
1541 return -1;
1542 }
1543
1544 incr_auth_attempts(user, pass_cmd);
1545 }
1546
1547 return res;
1548 }
1549
1550 res = set_userauth_success(method);
1551 if (res == 0) {
1552 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1553 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1554
1555 if (send_userauth_methods(TRUE) < 0) {
1556 return -1;
1557 }
1558
1559 return res;
1560 }
1561
1562 /* Past this point we will not call incr_auth_attempts(); the client has
1563 * successfully authenticated at this point, and should not be penalized
1564 * if an internal error causes the rest of the login process to fail.
1565 * Right?
1566 */
1567
1568 res = handle_auth_success(pkt->pool, user);
1569 if (res < 0) {
1570 dispatch_cmd_err(pass_cmd);
1571
1572 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1573 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1574
1575 if (send_userauth_failure(NULL) < 0) {
1576 return -1;
1577 }
1578
1579 return 0;
1580 }
1581
1582 if (send_userauth_success() < 0) {
1583 dispatch_cmd_err(pass_cmd);
1584
1585 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1586 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1587
1588 return -1;
1589 }
1590
1591 if (session.auth_mech != NULL) {
1592 pr_log_debug(DEBUG2, "user '%s' authenticated by %s", user,
1593 session.auth_mech);
1594 }
1595
1596 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1597 "user '%s' authenticated via '%s' method", user, method);
1598
1599 /* This allows for the %s response code LogFormat variable to be populated
1600 * in an AUTH ExtendedLog.
1601 */
1602 pr_response_add(R_230, "User %s logged in", user);
1603 pr_cmd_dispatch_phase(pass_cmd, POST_CMD, 0);
1604 pr_cmd_dispatch_phase(pass_cmd, LOG_CMD, 0);
1605 pr_response_clear(&resp_list);
1606
1607 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1608 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1609
1610 /* At this point, we can look up the Protocols config, which may have
1611 * been tweaked via mod_ifsession's user/group/class-specific sections.
1612 */
1613 c = find_config(main_server->conf, CONF_PARAM, "Protocols", FALSE);
1614 if (c != NULL) {
1615 register unsigned int i;
1616 unsigned int services = 0UL;
1617 array_header *protocols;
1618 char **elts;
1619
1620 protocols = c->argv[0];
1621 elts = protocols->elts;
1622
1623 for (i = 0; i < protocols->nelts; i++) {
1624 char *protocol;
1625
1626 protocol = elts[i];
1627 if (protocol != NULL) {
1628 if (strncasecmp(protocol, "sftp", 5) == 0) {
1629 services |= SFTP_SERVICE_FL_SFTP;
1630
1631 } else if (strncasecmp(protocol, "scp", 4) == 0) {
1632 services |= SFTP_SERVICE_FL_SCP;
1633
1634 } else if (strncasecmp(protocol, "date", 5) == 0) {
1635 services |= SFTP_SERVICE_FL_DATE;
1636 }
1637 }
1638 }
1639
1640 sftp_services = services;
1641 }
1642
1643 sftp_keys_send_hostkeys(pkt->pool);
1644 return 1;
1645 }
1646
1647 /* Auth Lists API */
1648 struct sftp_auth_chain *sftp_auth_chain_alloc(pool *p) {
1649 pool *sub_pool;
1650 struct sftp_auth_chain *auth_chain;
1651
1652 if (p == NULL) {
1653 errno = EINVAL;
1654 return NULL;
1655 }
1656
1657 sub_pool = pr_pool_create_sz(p, 256);
1658 pr_pool_tag(sub_pool, "SSH2 Auth Chain Pool");
1659
1660 auth_chain = pcalloc(sub_pool, sizeof(struct sftp_auth_chain));
1661 auth_chain->pool = sub_pool;
1662 auth_chain->methods = make_array(sub_pool, 1,
1663 sizeof(struct sftp_auth_method *));
1664 auth_chain->completed = FALSE;
1665
1666 return auth_chain;
1667 }
1668
1669 /* Check if 'password' or 'hostbased' methods appear multiple times in
1670 * this chain; if so, it's an invalid chain. Multiple 'publickey' or
1671 * 'keyboard-interactive' methods are allowed, though.
1672 */
1673 int sftp_auth_chain_isvalid(struct sftp_auth_chain *auth_chain) {
1674 register unsigned int i;
1675 int has_password = FALSE, has_hostbased = FALSE;
1676
1677 for (i = 0; i < auth_chain->methods->nelts; i++) {
1678 struct sftp_auth_method *meth;
1679
1680 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[i];
1681
1682 switch (meth->method_id) {
1683 case SFTP_AUTH_FL_METH_PASSWORD:
1684 if (has_password == TRUE) {
1685 errno = EPERM;
1686 return -1;
1687 }
1688
1689 has_password = TRUE;
1690 break;
1691
1692 case SFTP_AUTH_FL_METH_HOSTBASED:
1693 if (has_hostbased == TRUE) {
1694 errno = EPERM;
1695 return -1;
1696 }
1697
1698 has_hostbased = TRUE;
1699 break;
1700
1701 case SFTP_AUTH_FL_METH_PUBLICKEY:
1702 case SFTP_AUTH_FL_METH_KBDINT:
1703 default:
1704 break;
1705 }
1706 }
1707
1708 return 0;
1709 }
1710
1711 int sftp_auth_chain_add_method(struct sftp_auth_chain *auth_chain,
1712 unsigned int method_id, const char *method_name,
1713 const char *submethod_name) {
1714 struct sftp_auth_method *meth;
1715
1716 if (auth_chain == NULL ||
1717 method_name == NULL) {
1718 errno = EINVAL;
1719 return -1;
1720 }
1721
1722 /* We currently only allow submethod names for kbdint methods. */
1723 if (submethod_name != NULL &&
1724 method_id != SFTP_AUTH_FL_METH_KBDINT) {
1725 errno = EPERM;
1726 return -1;
1727 }
1728
1729 meth = pcalloc(auth_chain->pool, sizeof(struct sftp_auth_method));
1730 meth->method_id = method_id;
1731 meth->method_name = pstrdup(auth_chain->pool, method_name);
1732 if (submethod_name != NULL) {
1733 meth->submethod_name = pstrdup(auth_chain->pool, submethod_name);
1734 }
1735 meth->succeeded = FALSE;
1736 meth->failed = FALSE;
1737
1738 *((struct sftp_auth_method **) push_array(auth_chain->methods)) = meth;
1739 return 0;
1740 }
1741
1742 int sftp_auth_chain_parse_method(pool *p, const char *name,
1743 unsigned int *method_id, const char **method_name,
1744 const char **submethod_name) {
1745 char *ptr;
1746 size_t method_namelen;
1747
1748 if (name == NULL ||
1749 method_id == NULL ||
1750 method_name == NULL) {
1751 errno = EINVAL;
1752 return -1;
1753 }
1754
1755 /* Look for the syntax indicating a submethod name. */
1756 ptr = strchr(name, ':');
1757 if (ptr == NULL) {
1758 method_namelen = strlen(name);
1759 } else {
1760 method_namelen = ptr - name - 1;
1761 }
1762
1763 if (strncmp(name, "publickey", method_namelen) == 0) {
1764 *method_id = SFTP_AUTH_FL_METH_PUBLICKEY;
1765 *method_name = name;
1766
1767 } else if (strncmp(name, "hostbased", method_namelen) == 0) {
1768 *method_id = SFTP_AUTH_FL_METH_HOSTBASED;
1769 *method_name = name;
1770
1771 } else if (strncmp(name, "password", method_namelen) == 0) {
1772 *method_id = SFTP_AUTH_FL_METH_PASSWORD;
1773 *method_name = name;
1774
1775 } else if (strncmp(name, "keyboard-interactive", method_namelen) == 0) {
1776 *method_id = SFTP_AUTH_FL_METH_KBDINT;
1777
1778 if (sftp_kbdint_have_drivers() == 0) {
1779 errno = EPERM;
1780 return -1;
1781 }
1782
1783 /* If we have a submethod name, check whether it matches one of our
1784 * loaded kbdint drivers.
1785 */
1786 if (ptr != NULL) {
1787 if (sftp_kbdint_get_driver(ptr) == NULL) {
1788 errno = EPERM;
1789 return -1;
1790 }
1791
1792 *method_name = pstrndup(p, name, method_namelen);
1793 if (submethod_name != NULL) {
1794 *submethod_name = ptr;
1795 }
1796
1797 } else {
1798 *method_name = name;
1799 }
1800
1801 } else {
1802 /* Unknown/unsupported method name. */
1803 errno = EINVAL;
1804 return -1;
1805 }
1806
1807 return 0;
1808 }
1809
1810 array_header *sftp_auth_chain_parse_method_chain(pool *p,
1811 const char *method_list) {
1812 char *ptr;
1813 size_t method_listlen;
1814 array_header *method_names;
1815
1816 if (p == NULL ||
1817 method_list == NULL) {
1818 errno = EINVAL;
1819 return NULL;
1820 }
1821
1822 ptr = strchr(method_list, '+');
1823 if (ptr == NULL) {
1824 method_names = make_array(p, 0, sizeof(char *));
1825 *((char **) push_array(method_names)) = pstrdup(p, method_list);
1826 return method_names;
1827 }
1828
1829 if (ptr == method_list) {
1830 /* Leading '+'. */
1831 errno = EPERM;
1832 return NULL;
1833 }
1834
1835 method_listlen = strlen(method_list);
1836 if (method_list[method_listlen-1] == '+') {
1837 /* Trailing '+'. */
1838 errno = EPERM;
1839 return NULL;
1840 }
1841
1842 method_names = make_array(p, 0, sizeof(char *));
1843
1844 while (ptr != NULL) {
1845 size_t namelen;
1846
1847 pr_signals_handle();
1848
1849 namelen = (ptr - method_list);
1850 if (namelen == 0) {
1851 /* Double '+' characters. */
1852 errno = EPERM;
1853 return NULL;
1854 }
1855
1856 *((char **) push_array(method_names)) = pstrndup(p, method_list, namelen);
1857
1858 method_list = ptr + 1;
1859 ptr = strchr(method_list, '+');
1860
1861 /* Don't forget the last name in the list. */
1862 if (ptr == NULL) {
1863 *((char **) push_array(method_names)) = pstrdup(p, method_list);
1864 }
1865 }
1866
1867 return method_names;
1868 }
1869
1870 char *sftp_auth_get_default_dir(void) {
1871 return auth_default_dir;
1872 }
1873
1874 int sftp_auth_publickey_isvalid(const char *algo,
1875 enum sftp_key_type_e *pubkey_type) {
1876 register unsigned int i;
1877
1878 if (algo == NULL) {
1879 errno = EINVAL;
1880 return -1;
1881 }
1882
1883 for (i = 0; publickeys[i].algo != NULL; i++) {
1884 if (strcmp(publickeys[i].algo, algo) == 0) {
1885 if (pubkey_type != NULL) {
1886 *pubkey_type = publickeys[i].key_type;
1887 }
1888
1889 return TRUE;
1890 }
1891 }
1892
1893 return FALSE;
1894 }
1895
1896 int sftp_auth_send_banner(const char *banner) {
1897 struct ssh2_packet *pkt;
1898 unsigned char *buf, *ptr;
1899 uint32_t buflen, bufsz;
1900 size_t banner_len;
1901 int res;
1902
1903 if (banner == NULL) {
1904 errno = EINVAL;
1905 return -1;
1906 }
1907
1908 /* If the client has finished authenticating, then we can no longer
1909 * send USERAUTH_BANNER messages.
1910 */
1911 if (sftp_sess_state & SFTP_SESS_STATE_HAVE_AUTH) {
1912 pr_trace_msg(trace_channel, 1,
1913 "unable to send banner: client has authenticated");
1914 return 0;
1915 }
1916
1917 /* Make sure that the given banner string ends in CRLF, as required by
1918 * RFC4252 Section 5.4.
1919 */
1920 banner_len = strlen(banner);
1921 if (banner[banner_len-2] != '\r' ||
1922 banner[banner_len-1] != '\n') {
1923 banner = pstrcat(auth_pool, banner, "\r\n", NULL);
1924 banner_len = strlen(banner);
1925 }
1926
1927 pkt = sftp_ssh2_packet_create(auth_pool);
1928
1929 buflen = bufsz = banner_len + 32;
1930 ptr = buf = palloc(pkt->pool, bufsz);
1931
1932 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_BANNER);
1933 sftp_msg_write_string(&buf, &buflen, banner);
1934
1935 /* XXX locale of banner */
1936 sftp_msg_write_string(&buf, &buflen, "");
1937
1938 pkt->payload = ptr;
1939 pkt->payload_len = (bufsz - buflen);
1940
1941 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1942 "sending userauth banner");
1943
1944 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
1945 if (res < 0) {
1946 destroy_pool(pkt->pool);
1947 return -1;
1948 }
1949
1950 destroy_pool(pkt->pool);
1951 return 0;
1952 }
1953
1954 int sftp_auth_handle(struct ssh2_packet *pkt) {
1955 char *service = NULL;
1956 int res;
1957
1958 /* The send_userauth_banner_file() function makes sure that the
1959 * SFTPDisplayBanner file is not sent multiple times.
1960 */
1961 if (send_userauth_banner_file() < 0) {
1962 return -1;
1963 }
1964
1965 if (pr_response_get_pool() == NULL) {
1966 /* We set this pool for use by the Response API, for logging response codes/
1967 * messages during login.
1968 */
1969 pr_response_set_pool(session.pool);
1970 }
1971
1972 res = handle_userauth_req(pkt, &service);
1973 if (res < 0) {
1974 destroy_pool(pkt->pool);
1975 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_ERROR, NULL);
1976 }
1977
1978 destroy_pool(pkt->pool);
1979 return res;
1980 }
1981
1982 int sftp_auth_init(void) {
1983
1984 /* There's no point in trying to handle the case where a client will
1985 * want/attempt to authenticate again, as a different user.
1986 *
1987 * The issue is that if a client successfully authenticates, and the
1988 * authenticated session is chrooted, a subsequent attempt to authenticate
1989 * will occur in a chrooted process, and that will likely lead to all
1990 * sorts of brokenness.
1991 */
1992
1993 if (auth_pool == NULL) {
1994 unsigned int *max_logins;
1995
1996 auth_pool = make_sub_pool(sftp_pool);
1997 pr_pool_tag(auth_pool, "SSH2 Auth Pool");
1998
1999 max_logins = get_param_ptr(main_server->conf, "MaxLoginAttempts", FALSE);
2000 if (max_logins != NULL) {
2001 auth_attempts_max = *max_logins;
2002 }
2003 }
2004
2005 sftp_auth_set_success_handler(setup_env);
2006 return 0;
2007 }
2008
2009 void sftp_auth_set_success_handler(int (*handler)(pool *, const char *)) {
2010 success_handler = handler;
2011 }