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