10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved.
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate * are met:
70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate /*
25*12597SJan.Pechanec@Sun.COM * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include "includes.h"
290Sstevel@tonic-gate RCSID("$OpenBSD: auth2.c,v 1.95 2002/08/22 21:33:58 markus Exp $");
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include "ssh2.h"
320Sstevel@tonic-gate #include "xmalloc.h"
330Sstevel@tonic-gate #include "packet.h"
340Sstevel@tonic-gate #include "log.h"
350Sstevel@tonic-gate #include "servconf.h"
360Sstevel@tonic-gate #include "compat.h"
370Sstevel@tonic-gate #include "misc.h"
380Sstevel@tonic-gate #include "auth.h"
390Sstevel@tonic-gate #include "dispatch.h"
400Sstevel@tonic-gate #include "sshlogin.h"
410Sstevel@tonic-gate #include "pathnames.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate #ifdef HAVE_BSM
440Sstevel@tonic-gate #include "bsmaudit.h"
450Sstevel@tonic-gate extern adt_session_data_t *ah;
460Sstevel@tonic-gate #endif /* HAVE_BSM */
470Sstevel@tonic-gate
480Sstevel@tonic-gate #ifdef GSSAPI
490Sstevel@tonic-gate #include "ssh-gss.h"
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* import */
530Sstevel@tonic-gate extern ServerOptions options;
540Sstevel@tonic-gate extern u_char *session_id2;
550Sstevel@tonic-gate extern int session_id2_len;
560Sstevel@tonic-gate
570Sstevel@tonic-gate Authctxt *x_authctxt = NULL;
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* methods */
600Sstevel@tonic-gate
610Sstevel@tonic-gate extern Authmethod method_none;
620Sstevel@tonic-gate extern Authmethod method_pubkey;
630Sstevel@tonic-gate extern Authmethod method_passwd;
640Sstevel@tonic-gate extern Authmethod method_kbdint;
650Sstevel@tonic-gate extern Authmethod method_hostbased;
660Sstevel@tonic-gate extern Authmethod method_external;
670Sstevel@tonic-gate extern Authmethod method_gssapi;
680Sstevel@tonic-gate
690Sstevel@tonic-gate static Authmethod *authmethods[] = {
700Sstevel@tonic-gate &method_none,
710Sstevel@tonic-gate #ifdef GSSAPI
720Sstevel@tonic-gate &method_external,
730Sstevel@tonic-gate &method_gssapi,
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate &method_pubkey,
760Sstevel@tonic-gate &method_passwd,
770Sstevel@tonic-gate &method_kbdint,
780Sstevel@tonic-gate &method_hostbased,
790Sstevel@tonic-gate NULL
800Sstevel@tonic-gate };
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* protocol */
830Sstevel@tonic-gate
840Sstevel@tonic-gate static void input_service_request(int, u_int32_t, void *);
850Sstevel@tonic-gate static void input_userauth_request(int, u_int32_t, void *);
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* helper */
880Sstevel@tonic-gate static Authmethod *authmethod_lookup(const char *);
890Sstevel@tonic-gate static char *authmethods_get(void);
900Sstevel@tonic-gate static char *authmethods_check_abandonment(Authctxt *authctxt,
910Sstevel@tonic-gate Authmethod *method);
920Sstevel@tonic-gate static void authmethod_count_attempt(Authmethod *method);
930Sstevel@tonic-gate /*static char *authmethods_get_kbdint(void);*/
940Sstevel@tonic-gate int user_key_allowed(struct passwd *, Key *);
950Sstevel@tonic-gate int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
960Sstevel@tonic-gate static int userauth_method_can_run(Authmethod *method);
970Sstevel@tonic-gate static void userauth_reset_methods(void);
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * loop until authctxt->success == TRUE
1010Sstevel@tonic-gate */
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate Authctxt *
do_authentication2(void)1040Sstevel@tonic-gate do_authentication2(void)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate Authctxt *authctxt = authctxt_new();
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate x_authctxt = authctxt; /*XXX*/
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate #ifdef HAVE_BSM
1110Sstevel@tonic-gate fatal_add_cleanup(audit_failed_login_cleanup, authctxt);
1120Sstevel@tonic-gate #endif /* HAVE_BSM */
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate dispatch_init(&dispatch_protocol_error);
1150Sstevel@tonic-gate dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1160Sstevel@tonic-gate dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate return (authctxt);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate static void
input_service_request(int type,u_int32_t seq,void * ctxt)1220Sstevel@tonic-gate input_service_request(int type, u_int32_t seq, void *ctxt)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate Authctxt *authctxt = ctxt;
1250Sstevel@tonic-gate u_int len;
1260Sstevel@tonic-gate int acceptit = 0;
1270Sstevel@tonic-gate char *service = packet_get_string(&len);
1280Sstevel@tonic-gate packet_check_eom();
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (authctxt == NULL)
1310Sstevel@tonic-gate fatal("input_service_request: no authctxt");
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate if (strcmp(service, "ssh-userauth") == 0) {
1340Sstevel@tonic-gate if (!authctxt->success) {
1350Sstevel@tonic-gate acceptit = 1;
1360Sstevel@tonic-gate /* now we can handle user-auth requests */
1370Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate /* XXX all other service requests are denied */
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate if (acceptit) {
1430Sstevel@tonic-gate packet_start(SSH2_MSG_SERVICE_ACCEPT);
1440Sstevel@tonic-gate packet_put_cstring(service);
1450Sstevel@tonic-gate packet_send();
1460Sstevel@tonic-gate packet_write_wait();
1470Sstevel@tonic-gate } else {
1480Sstevel@tonic-gate debug("bad service request %s", service);
1490Sstevel@tonic-gate packet_disconnect("bad service request %s", service);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate xfree(service);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static void
input_userauth_request(int type,u_int32_t seq,void * ctxt)1550Sstevel@tonic-gate input_userauth_request(int type, u_int32_t seq, void *ctxt)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate Authctxt *authctxt = ctxt;
1580Sstevel@tonic-gate Authmethod *m = NULL;
1590Sstevel@tonic-gate char *user, *service, *method, *style = NULL;
16011251SErik.Trauschke@Sun.COM int valid_attempt;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (authctxt == NULL)
1630Sstevel@tonic-gate fatal("input_userauth_request: no authctxt");
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate user = packet_get_string(NULL);
1660Sstevel@tonic-gate service = packet_get_string(NULL);
1670Sstevel@tonic-gate method = packet_get_string(NULL);
1680Sstevel@tonic-gate debug("userauth-request for user %s service %s method %s", user,
1690Sstevel@tonic-gate service, method);
1700Sstevel@tonic-gate debug("attempt %d initial attempt %d failures %d initial failures %d",
1710Sstevel@tonic-gate authctxt->attempt, authctxt->init_attempt,
1720Sstevel@tonic-gate authctxt->failures, authctxt->init_failures);
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate m = authmethod_lookup(method);
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if ((style = strchr(user, ':')) != NULL)
1770Sstevel@tonic-gate *style++ = 0;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate authctxt->attempt++;
1800Sstevel@tonic-gate if (m != NULL && m->is_initial)
1810Sstevel@tonic-gate authctxt->init_attempt++;
1820Sstevel@tonic-gate
18311251SErik.Trauschke@Sun.COM if (options.pre_userauth_hook != NULL &&
18411251SErik.Trauschke@Sun.COM run_auth_hook(options.pre_userauth_hook, user, m->name) != 0) {
18511251SErik.Trauschke@Sun.COM valid_attempt = 0;
18611251SErik.Trauschke@Sun.COM } else {
18711251SErik.Trauschke@Sun.COM valid_attempt = 1;
18811251SErik.Trauschke@Sun.COM }
18911251SErik.Trauschke@Sun.COM
1900Sstevel@tonic-gate if (authctxt->attempt == 1) {
1910Sstevel@tonic-gate /* setup auth context */
1925562Sjp161948 authctxt->pw = getpwnamallow(user);
1930Sstevel@tonic-gate /* May want to abstract SSHv2 services someday */
1940Sstevel@tonic-gate if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1950Sstevel@tonic-gate /* enforced in userauth_finish() below */
19611251SErik.Trauschke@Sun.COM if (valid_attempt) {
19711251SErik.Trauschke@Sun.COM authctxt->valid = 1;
19811251SErik.Trauschke@Sun.COM }
1990Sstevel@tonic-gate debug2("input_userauth_request: setting up authctxt for %s", user);
2000Sstevel@tonic-gate } else {
2010Sstevel@tonic-gate log("input_userauth_request: illegal user %s", user);
2020Sstevel@tonic-gate }
2035562Sjp161948 setproctitle("%s", authctxt->pw ? user : "unknown");
2040Sstevel@tonic-gate authctxt->user = xstrdup(user);
2050Sstevel@tonic-gate authctxt->service = xstrdup(service);
2060Sstevel@tonic-gate authctxt->style = style ? xstrdup(style) : NULL;
2070Sstevel@tonic-gate userauth_reset_methods();
2080Sstevel@tonic-gate } else {
2090Sstevel@tonic-gate char *abandoned;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * Check for abandoned [multi-round-trip] userauths
2130Sstevel@tonic-gate * methods (e.g., kbdint). Userauth method abandonment
2140Sstevel@tonic-gate * should be treated as userauth method failure and
2150Sstevel@tonic-gate * counted against max_auth_tries.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate abandoned = authmethods_check_abandonment(authctxt, m);
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (abandoned != NULL &&
2200Sstevel@tonic-gate authctxt->failures > options.max_auth_tries) {
2210Sstevel@tonic-gate /* userauth_finish() will now packet_disconnect() */
2220Sstevel@tonic-gate userauth_finish(authctxt, abandoned);
2230Sstevel@tonic-gate /* NOTREACHED */
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /* Handle user|service changes, possibly packet_disconnect() */
2270Sstevel@tonic-gate userauth_user_svc_change(authctxt, user, service);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate authctxt->method = m;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /* run userauth method, try to authenticate user */
2330Sstevel@tonic-gate if (m != NULL && userauth_method_can_run(m)) {
2340Sstevel@tonic-gate debug2("input_userauth_request: try method %s", method);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate m->postponed = 0;
2370Sstevel@tonic-gate m->abandoned = 0;
2380Sstevel@tonic-gate m->authenticated = 0;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if (!m->is_initial ||
2410Sstevel@tonic-gate authctxt->init_failures < options.max_init_auth_tries)
2420Sstevel@tonic-gate m->userauth(authctxt);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate authmethod_count_attempt(m);
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate if (authctxt->unwind_dispatch_loop) {
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate * Method ran nested dispatch loop but was
2490Sstevel@tonic-gate * abandoned. Cleanup and return without doing
2500Sstevel@tonic-gate * anything else; we're just unwinding the stack.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate authctxt->unwind_dispatch_loop = 0;
2530Sstevel@tonic-gate goto done;
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (m->postponed)
2570Sstevel@tonic-gate goto done; /* multi-round trip userauth not finished */
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate if (m->abandoned) {
2600Sstevel@tonic-gate /* multi-round trip userauth abandoned, log failure */
2610Sstevel@tonic-gate auth_log(authctxt, 0, method, " ssh2");
2620Sstevel@tonic-gate goto done;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate userauth_finish(authctxt, method);
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate done:
2690Sstevel@tonic-gate xfree(service);
2700Sstevel@tonic-gate xfree(user);
2710Sstevel@tonic-gate xfree(method);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate void
userauth_finish(Authctxt * authctxt,char * method)2750Sstevel@tonic-gate userauth_finish(Authctxt *authctxt, char *method)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate int authenticated, partial;
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if (authctxt == NULL)
2800Sstevel@tonic-gate fatal("%s: missing context", __func__);
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate /* unknown method handling -- must elicit userauth failure msg */
2830Sstevel@tonic-gate if (authctxt->method == NULL) {
2840Sstevel@tonic-gate authenticated = 0;
2850Sstevel@tonic-gate partial = 0;
2860Sstevel@tonic-gate goto done_checking;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate #ifndef USE_PAM
2900Sstevel@tonic-gate /* Special handling for root (done elsewhere for PAM) */
2915562Sjp161948 if (authctxt->method->authenticated &&
2920Sstevel@tonic-gate authctxt->pw != NULL && authctxt->pw->pw_uid == 0 &&
2930Sstevel@tonic-gate !auth_root_allowed(method))
2940Sstevel@tonic-gate authctxt->method->authenticated = 0;
2950Sstevel@tonic-gate #endif /* USE_PAM */
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate #ifdef _UNICOS
2980Sstevel@tonic-gate if (authctxt->method->authenticated &&
2990Sstevel@tonic-gate cray_access_denied(authctxt->user)) {
3000Sstevel@tonic-gate authctxt->method->authenticated = 0;
3010Sstevel@tonic-gate fatal("Access denied for user %s.",authctxt->user);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate #endif /* _UNICOS */
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate partial = userauth_check_partial_failure(authctxt);
3060Sstevel@tonic-gate authenticated = authctxt->method->authenticated;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate #ifdef USE_PAM
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * If the userauth method failed to complete PAM work then force
3110Sstevel@tonic-gate * partial failure.
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate if (authenticated && !AUTHPAM_DONE(authctxt))
3140Sstevel@tonic-gate partial = 1;
3150Sstevel@tonic-gate #endif /* USE_PAM */
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate * To properly support invalid userauth method names we set
3190Sstevel@tonic-gate * authenticated=0, partial=0 above and know that
3200Sstevel@tonic-gate * authctxt->method == NULL.
3210Sstevel@tonic-gate *
3220Sstevel@tonic-gate * No unguarded reference to authctxt->method allowed from here.
3230Sstevel@tonic-gate * Checking authenticated != 0 is a valid guard; authctxt->method
3240Sstevel@tonic-gate * MUST NOT be NULL if authenticated.
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate done_checking:
3270Sstevel@tonic-gate if (!authctxt->valid && authenticated) {
3280Sstevel@tonic-gate /*
32911251SErik.Trauschke@Sun.COM * We get here if the PreUserauthHook fails but the
33011251SErik.Trauschke@Sun.COM * user is otherwise valid.
33111251SErik.Trauschke@Sun.COM * An error in the PAM handling could also get us here
3320Sstevel@tonic-gate * but we need not panic, just treat as a failure.
3330Sstevel@tonic-gate */
3340Sstevel@tonic-gate authctxt->method->authenticated = 0;
3350Sstevel@tonic-gate authenticated = 0;
3360Sstevel@tonic-gate log("Ignoring authenticated invalid user %s",
3370Sstevel@tonic-gate authctxt->user);
3380Sstevel@tonic-gate auth_log(authctxt, 0, method, " ssh2");
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /* Log before sending the reply */
3420Sstevel@tonic-gate auth_log(authctxt, authenticated, method, " ssh2");
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if (authenticated && !partial) {
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /* turn off userauth */
3470Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3480Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_SUCCESS);
3490Sstevel@tonic-gate packet_send();
3500Sstevel@tonic-gate packet_write_wait();
3510Sstevel@tonic-gate /* now we can break out */
3520Sstevel@tonic-gate authctxt->success = 1;
3530Sstevel@tonic-gate } else {
3540Sstevel@tonic-gate char *methods;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (authctxt->method && authctxt->method->is_initial)
3570Sstevel@tonic-gate authctxt->init_failures++;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate authctxt->method = NULL;
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate #ifdef USE_PAM
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate * Keep track of last PAM error (or PERM_DENIED) for BSM
3640Sstevel@tonic-gate * login failure auditing, which may run after the PAM
3650Sstevel@tonic-gate * state has been cleaned up.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate authctxt->pam_retval = AUTHPAM_ERROR(authctxt, PAM_PERM_DENIED);
3680Sstevel@tonic-gate #endif /* USE_PAM */
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (authctxt->failures++ > options.max_auth_tries) {
3710Sstevel@tonic-gate #ifdef HAVE_BSM
3720Sstevel@tonic-gate fatal_remove_cleanup(audit_failed_login_cleanup,
3730Sstevel@tonic-gate authctxt);
3748064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_MAXTRIES,
3758064SBrent.Paulson@Sun.COM authctxt->user);
3760Sstevel@tonic-gate #endif /* HAVE_BSM */
3770Sstevel@tonic-gate packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate #ifdef _UNICOS
3810Sstevel@tonic-gate if (strcmp(method, "password") == 0)
3820Sstevel@tonic-gate cray_login_failure(authctxt->user, IA_UDBERR);
3830Sstevel@tonic-gate #endif /* _UNICOS */
3840Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_FAILURE);
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate * If (partial) then authmethods_get() will return only
3880Sstevel@tonic-gate * required methods, likely only "keyboard-interactive;"
3890Sstevel@tonic-gate * (methods == NULL) implies failure, even if (partial == 1)
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate methods = authmethods_get();
3920Sstevel@tonic-gate packet_put_cstring(methods);
3930Sstevel@tonic-gate packet_put_char((authenticated && partial && methods) ? 1 : 0);
3940Sstevel@tonic-gate if (methods)
3950Sstevel@tonic-gate xfree(methods);
3960Sstevel@tonic-gate packet_send();
3970Sstevel@tonic-gate packet_write_wait();
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate /* get current user */
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate struct passwd*
auth_get_user(void)4040Sstevel@tonic-gate auth_get_user(void)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate #define DELIM ","
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate #if 0
4120Sstevel@tonic-gate static char *
4130Sstevel@tonic-gate authmethods_get_kbdint(void)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate Buffer b;
4160Sstevel@tonic-gate int i;
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
4190Sstevel@tonic-gate if (strcmp(authmethods[i]->name, "keyboard-interactive") != 0)
4200Sstevel@tonic-gate continue;
4210Sstevel@tonic-gate return xstrdup(authmethods[i]->name);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate return NULL;
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate #endif
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate void
userauth_user_svc_change(Authctxt * authctxt,char * user,char * service)4280Sstevel@tonic-gate userauth_user_svc_change(Authctxt *authctxt, char *user, char *service)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate * NOTE:
4320Sstevel@tonic-gate *
4330Sstevel@tonic-gate * SSHv2 services should be abstracted and service changes during
4340Sstevel@tonic-gate * userauth should be supported as per the userauth draft. In the PAM
4350Sstevel@tonic-gate * case, support for multiple SSHv2 services means that we have to
4360Sstevel@tonic-gate * format the PAM service name according to the SSHv2 service *and* the
4370Sstevel@tonic-gate * SSHv2 userauth being attempted ("passwd", "kbdint" and "other").
4380Sstevel@tonic-gate *
4390Sstevel@tonic-gate * We'll cross that bridge when we come to it. For now disallow service
4400Sstevel@tonic-gate * changes during userauth if using PAM, but allow username changes.
4410Sstevel@tonic-gate */
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate /* authctxt->service must == ssh-connection here */
4440Sstevel@tonic-gate if (service != NULL && strcmp(service, authctxt->service) != 0) {
4450Sstevel@tonic-gate packet_disconnect("Change of service not "
4460Sstevel@tonic-gate "allowed: %s and %s",
4470Sstevel@tonic-gate authctxt->service, service);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate if (user != NULL && authctxt->user != NULL &&
4500Sstevel@tonic-gate strcmp(user, authctxt->user) == 0)
4510Sstevel@tonic-gate return;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate /* All good; update authctxt */
4540Sstevel@tonic-gate xfree(authctxt->user);
4550Sstevel@tonic-gate authctxt->user = xstrdup(user);
4560Sstevel@tonic-gate pwfree(&authctxt->pw);
4575562Sjp161948 authctxt->pw = getpwnamallow(user);
4580Sstevel@tonic-gate authctxt->valid = (authctxt->pw != NULL);
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate /* Forget method state; abandon postponed userauths */
4610Sstevel@tonic-gate userauth_reset_methods();
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate int
userauth_check_partial_failure(Authctxt * authctxt)4650Sstevel@tonic-gate userauth_check_partial_failure(Authctxt *authctxt)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate int i;
4680Sstevel@tonic-gate int required = 0;
4690Sstevel@tonic-gate int sufficient = 0;
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate * v1 does not set authctxt->method
4730Sstevel@tonic-gate * partial userauth failure is a v2 concept
4740Sstevel@tonic-gate */
4750Sstevel@tonic-gate if (authctxt->method == NULL)
4760Sstevel@tonic-gate return 0;
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
4790Sstevel@tonic-gate if (authmethods[i]->required)
4800Sstevel@tonic-gate required++;
4810Sstevel@tonic-gate if (authmethods[i]->sufficient)
4820Sstevel@tonic-gate sufficient++;
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate if (required == 0 && sufficient == 0)
4860Sstevel@tonic-gate return !authctxt->method->authenticated;
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate if (required == 1 && authctxt->method->required)
4890Sstevel@tonic-gate return !authctxt->method->authenticated;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate if (sufficient && authctxt->method->sufficient)
4920Sstevel@tonic-gate return !authctxt->method->authenticated;
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate return 1;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate int
userauth_method_can_run(Authmethod * method)4980Sstevel@tonic-gate userauth_method_can_run(Authmethod *method)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate if (method->not_again)
5010Sstevel@tonic-gate return 0;
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate return 1;
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate static
5070Sstevel@tonic-gate void
userauth_reset_methods(void)5080Sstevel@tonic-gate userauth_reset_methods(void)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate int i;
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
5130Sstevel@tonic-gate /* note: counters not reset */
5140Sstevel@tonic-gate authmethods[i]->required = 0;
5150Sstevel@tonic-gate authmethods[i]->sufficient = 0;
5160Sstevel@tonic-gate authmethods[i]->authenticated = 0;
5170Sstevel@tonic-gate authmethods[i]->not_again = 0;
5180Sstevel@tonic-gate authmethods[i]->postponed = 0;
5190Sstevel@tonic-gate authmethods[i]->abandoned = 0;
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate void
userauth_force_kbdint(void)5240Sstevel@tonic-gate userauth_force_kbdint(void)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate int i;
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
5290Sstevel@tonic-gate authmethods[i]->required = 0;
5300Sstevel@tonic-gate authmethods[i]->sufficient = 0;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate method_kbdint.required = 1;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate * Check to see if a previously run multi-round trip userauth method has
5370Sstevel@tonic-gate * been abandoned and call its cleanup function.
5380Sstevel@tonic-gate *
5390Sstevel@tonic-gate * Abandoned userauth method invocations are counted as userauth failures.
5400Sstevel@tonic-gate */
5410Sstevel@tonic-gate static
5420Sstevel@tonic-gate char *
authmethods_check_abandonment(Authctxt * authctxt,Authmethod * method)5430Sstevel@tonic-gate authmethods_check_abandonment(Authctxt *authctxt, Authmethod *method)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate int i;
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /* optimization: check current method first */
5480Sstevel@tonic-gate if (method && method->postponed) {
5490Sstevel@tonic-gate method->postponed = 0;
5500Sstevel@tonic-gate if (method->abandon)
5510Sstevel@tonic-gate method->abandon(authctxt, method);
5520Sstevel@tonic-gate else
5530Sstevel@tonic-gate method->abandons++;
5540Sstevel@tonic-gate authctxt->failures++; /* abandonment -> failure */
5550Sstevel@tonic-gate if (method->is_initial)
5560Sstevel@tonic-gate authctxt->init_failures++;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate * Since we check for abandonment whenever a userauth is
5600Sstevel@tonic-gate * requested we know only one method could have been
5610Sstevel@tonic-gate * in postponed state, so we can return now.
5620Sstevel@tonic-gate */
5630Sstevel@tonic-gate return (method->name);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
5660Sstevel@tonic-gate if (!authmethods[i]->postponed)
5670Sstevel@tonic-gate continue;
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate /* some method was postponed and a diff one is being started */
5700Sstevel@tonic-gate if (method != authmethods[i]) {
5710Sstevel@tonic-gate authmethods[i]->postponed = 0;
5720Sstevel@tonic-gate if (authmethods[i]->abandon)
5730Sstevel@tonic-gate authmethods[i]->abandon(authctxt,
5740Sstevel@tonic-gate authmethods[i]);
5750Sstevel@tonic-gate else
5760Sstevel@tonic-gate authmethods[i]->abandons++;
5770Sstevel@tonic-gate authctxt->failures++;
5780Sstevel@tonic-gate if (authmethods[i]->is_initial)
5790Sstevel@tonic-gate authctxt->init_failures++;
5800Sstevel@tonic-gate return (authmethods[i]->name); /* see above */
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate return NULL;
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate static char *
authmethods_get(void)5880Sstevel@tonic-gate authmethods_get(void)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate Buffer b;
5910Sstevel@tonic-gate char *list;
5920Sstevel@tonic-gate int i;
5930Sstevel@tonic-gate int sufficient = 0;
5940Sstevel@tonic-gate int required = 0;
5950Sstevel@tonic-gate int authenticated = 0;
5960Sstevel@tonic-gate int partial = 0;
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate /*
5990Sstevel@tonic-gate * If at least one method succeeded partially then at least one
6000Sstevel@tonic-gate * authmethod will be required and only required methods should
6010Sstevel@tonic-gate * continue.
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
6040Sstevel@tonic-gate if (authmethods[i]->authenticated)
6050Sstevel@tonic-gate authenticated++;
6060Sstevel@tonic-gate if (authmethods[i]->required)
6070Sstevel@tonic-gate required++;
6080Sstevel@tonic-gate if (authmethods[i]->sufficient)
6090Sstevel@tonic-gate sufficient++;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate partial = (required + sufficient) > 0;
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate buffer_init(&b);
6150Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++) {
6160Sstevel@tonic-gate if (strcmp(authmethods[i]->name, "none") == 0)
6170Sstevel@tonic-gate continue;
6180Sstevel@tonic-gate if (required && !authmethods[i]->required)
6190Sstevel@tonic-gate continue;
6200Sstevel@tonic-gate if (sufficient && !required && !authmethods[i]->sufficient)
6210Sstevel@tonic-gate continue;
6220Sstevel@tonic-gate if (authmethods[i]->not_again)
6230Sstevel@tonic-gate continue;
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate if (authmethods[i]->required) {
6260Sstevel@tonic-gate if (buffer_len(&b) > 0)
6270Sstevel@tonic-gate buffer_append(&b, ",", 1);
6280Sstevel@tonic-gate buffer_append(&b, authmethods[i]->name,
6290Sstevel@tonic-gate strlen(authmethods[i]->name));
6300Sstevel@tonic-gate continue;
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate /*
6340Sstevel@tonic-gate * A method can be enabled (marked sufficient)
6350Sstevel@tonic-gate * dynamically provided that at least one other method
6360Sstevel@tonic-gate * has succeeded partially.
6370Sstevel@tonic-gate */
6380Sstevel@tonic-gate if ((partial && authmethods[i]->sufficient) ||
6390Sstevel@tonic-gate (authmethods[i]->enabled != NULL &&
6400Sstevel@tonic-gate *(authmethods[i]->enabled) != 0)) {
6410Sstevel@tonic-gate if (buffer_len(&b) > 0)
6420Sstevel@tonic-gate buffer_append(&b, ",", 1);
6430Sstevel@tonic-gate buffer_append(&b, authmethods[i]->name,
6440Sstevel@tonic-gate strlen(authmethods[i]->name));
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate buffer_append(&b, "\0", 1);
6480Sstevel@tonic-gate list = xstrdup(buffer_ptr(&b));
6490Sstevel@tonic-gate buffer_free(&b);
6500Sstevel@tonic-gate return list;
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate static Authmethod *
authmethod_lookup(const char * name)6540Sstevel@tonic-gate authmethod_lookup(const char *name)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate int i;
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate * Method must be sufficient, required or enabled and must not
6600Sstevel@tonic-gate * be marked as not able to run again
6610Sstevel@tonic-gate */
6620Sstevel@tonic-gate if (name != NULL)
6630Sstevel@tonic-gate for (i = 0; authmethods[i] != NULL; i++)
6640Sstevel@tonic-gate if (((authmethods[i]->sufficient ||
6650Sstevel@tonic-gate authmethods[i]->required) ||
6660Sstevel@tonic-gate (authmethods[i]->enabled != NULL &&
6670Sstevel@tonic-gate *(authmethods[i]->enabled) != 0)) &&
6680Sstevel@tonic-gate !authmethods[i]->not_again &&
6690Sstevel@tonic-gate strcmp(name, authmethods[i]->name) == 0)
6700Sstevel@tonic-gate return authmethods[i];
6710Sstevel@tonic-gate debug2("Unrecognized authentication method name: %s",
6720Sstevel@tonic-gate name ? name : "NULL");
6730Sstevel@tonic-gate return NULL;
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate static void
authmethod_count_attempt(Authmethod * method)6770Sstevel@tonic-gate authmethod_count_attempt(Authmethod *method)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate if (!method)
6800Sstevel@tonic-gate fatal("Internal error in authmethod_count_attempt()");
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate if (method->postponed)
6830Sstevel@tonic-gate return;
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate method->attempts++;
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate if (method->abandoned)
6880Sstevel@tonic-gate method->abandons++;
6890Sstevel@tonic-gate else if (method->authenticated)
6900Sstevel@tonic-gate method->successes++;
6910Sstevel@tonic-gate else
6920Sstevel@tonic-gate method->failures++;
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate return;
6950Sstevel@tonic-gate }
696