17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (c) 2000 Damien Miller. All rights reserved. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 57c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 67c478bd9Sstevel@tonic-gate * are met: 77c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 87c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 97c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 107c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 117c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 147c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 157c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 167c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 177c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 187c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 197c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 207c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 217c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 227c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate /* 25*b350d31dSme23304 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 267c478bd9Sstevel@tonic-gate * Use is subject to license terms. 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include "includes.h" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifdef USE_PAM 327c478bd9Sstevel@tonic-gate #include "xmalloc.h" 337c478bd9Sstevel@tonic-gate #include "log.h" 347c478bd9Sstevel@tonic-gate #include "auth.h" 357c478bd9Sstevel@tonic-gate #include "auth-options.h" 367c478bd9Sstevel@tonic-gate #include "auth-pam.h" 377c478bd9Sstevel@tonic-gate #include "servconf.h" 387c478bd9Sstevel@tonic-gate #include "canohost.h" 397c478bd9Sstevel@tonic-gate #include "compat.h" 407c478bd9Sstevel@tonic-gate #include "misc.h" 417c478bd9Sstevel@tonic-gate #include "sshlogin.h" 427c478bd9Sstevel@tonic-gate #include "monitor_wrap.h" 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <security/pam_appl.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate extern char *__progname; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate extern int use_privsep; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate extern u_int utmp_len; 517c478bd9Sstevel@tonic-gate extern ServerOptions options; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate extern Authmethod method_kbdint; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate RCSID("$Id: auth-pam.c,v 1.54 2002/07/28 20:24:08 stevesk Exp $"); 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #define NEW_AUTHTOK_MSG \ 607c478bd9Sstevel@tonic-gate "Warning: Your password has expired, please change it now." 617c478bd9Sstevel@tonic-gate #define NEW_AUTHTOK_MSG_PRIVSEP \ 627c478bd9Sstevel@tonic-gate "Your password has expired, the session cannot proceed." 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */ 657c478bd9Sstevel@tonic-gate static int do_pam_conversation(int num_msg, const struct pam_message **msg, 667c478bd9Sstevel@tonic-gate struct pam_response **resp, void *appdata_ptr); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static void do_pam_cleanup_proc(void *context); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static char *get_method_name(Authctxt *authctxt); 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */ 737c478bd9Sstevel@tonic-gate static struct pam_conv conv = { 747c478bd9Sstevel@tonic-gate (int (*)())do_pam_conversation, 757c478bd9Sstevel@tonic-gate NULL 767c478bd9Sstevel@tonic-gate }; 777c478bd9Sstevel@tonic-gate static char *__pam_msg = NULL; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate static 807c478bd9Sstevel@tonic-gate char * 817c478bd9Sstevel@tonic-gate get_method_name(Authctxt *authctxt) 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate if (!authctxt) 847c478bd9Sstevel@tonic-gate return "(unknown)"; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (!compat20) 877c478bd9Sstevel@tonic-gate return (authctxt->v1_auth_name) ? authctxt->v1_auth_name : 887c478bd9Sstevel@tonic-gate "(sshv1-unknown)"; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate if (!authctxt->method || !authctxt->method->name) 917c478bd9Sstevel@tonic-gate return "(sshv2-unknown)"; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate return authctxt->method->name; 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate const 977c478bd9Sstevel@tonic-gate char * 987c478bd9Sstevel@tonic-gate derive_pam_svc_name(Authmethod *method) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate if (compat20 && method) { 1017c478bd9Sstevel@tonic-gate char *method_name = method->name; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate if (!method_name) 1047c478bd9Sstevel@tonic-gate fatal("Userauth method unknown while starting PAM"); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* For SSHv2 we use "sshd-<userauth name> */ 1077c478bd9Sstevel@tonic-gate if (strcmp(method_name, "none") == 0) { 1087c478bd9Sstevel@tonic-gate return "sshd-none"; 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate if (strcmp(method_name, "password") == 0) { 1117c478bd9Sstevel@tonic-gate return "sshd-password"; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate if (strcmp(method_name, "keyboard-interactive") == 0) { 1147c478bd9Sstevel@tonic-gate /* "keyboard-interactive" is too long, shorten it */ 1157c478bd9Sstevel@tonic-gate return "sshd-kbdint"; 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate if (strcmp(method_name, "publickey") == 0) { 1187c478bd9Sstevel@tonic-gate /* "publickey" is too long, shorten it */ 1197c478bd9Sstevel@tonic-gate return "sshd-pubkey"; 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate if (strcmp(method_name, "hostbased") == 0) { 1227c478bd9Sstevel@tonic-gate /* "hostbased" can't really be shortened... */ 1237c478bd9Sstevel@tonic-gate return "sshd-hostbased"; 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate if (strncmp(method_name, "gss", 3) == 0) { 126*b350d31dSme23304 /* "gss" is too short, elongate it */ 1277c478bd9Sstevel@tonic-gate return "sshd-gssapi"; 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate return "sshd-v1"; /* SSHv1 doesn't get to be so cool */ 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate void 1357c478bd9Sstevel@tonic-gate new_start_pam(Authctxt *authctxt, struct pam_conv *conv) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate int retval; 1387c478bd9Sstevel@tonic-gate pam_handle_t *pamh; 1397c478bd9Sstevel@tonic-gate const char *rhost, *svc; 1407c478bd9Sstevel@tonic-gate char *user = NULL; 1417c478bd9Sstevel@tonic-gate pam_stuff *pam; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate if (authctxt == NULL) 1447c478bd9Sstevel@tonic-gate fatal("Internal error during userauth"); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if (compat20 && authctxt->method == NULL) 1477c478bd9Sstevel@tonic-gate fatal("Userauth method unknown while starting PAM"); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* PAM service selected here */ 1507c478bd9Sstevel@tonic-gate svc = derive_pam_svc_name(authctxt->method); 1517c478bd9Sstevel@tonic-gate debug2("Starting PAM service %s for method %s", svc, 1527c478bd9Sstevel@tonic-gate get_method_name(authctxt)); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate if (authctxt->user != NULL) 1557c478bd9Sstevel@tonic-gate user = authctxt->user; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* Cleanup previous PAM state */ 1587c478bd9Sstevel@tonic-gate if (authctxt->pam != NULL) { 1597c478bd9Sstevel@tonic-gate fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam); 1607c478bd9Sstevel@tonic-gate do_pam_cleanup_proc(authctxt->pam); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate pam = xmalloc(sizeof(pam_stuff)); 1647c478bd9Sstevel@tonic-gate (void) memset(pam, 0, sizeof(pam_stuff)); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * pam->last_pam_retval has to be and is considered 1687c478bd9Sstevel@tonic-gate * along with pam->state. 1697c478bd9Sstevel@tonic-gate * 1707c478bd9Sstevel@tonic-gate * pam->state = 0; -> no PAM auth, account, etc, work 1717c478bd9Sstevel@tonic-gate * done yet. (Set by memset() above.) 1727c478bd9Sstevel@tonic-gate * 1737c478bd9Sstevel@tonic-gate * pam->last_pam_retval = PAM_SUCCESS; -> meaningless at 1747c478bd9Sstevel@tonic-gate * this point. 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * See finish_userauth_do_pam() below. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate pam->authctxt = authctxt; 1797c478bd9Sstevel@tonic-gate pam->last_pam_retval = PAM_SUCCESS; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate authctxt->pam = pam; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* Free any previously stored text/error PAM prompts */ 1847c478bd9Sstevel@tonic-gate if (__pam_msg) { 1857c478bd9Sstevel@tonic-gate xfree(__pam_msg); 1867c478bd9Sstevel@tonic-gate __pam_msg = NULL; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if ((retval = pam_start(svc, user, conv, &pamh)) != PAM_SUCCESS) { 1907c478bd9Sstevel@tonic-gate fatal("PAM initialization failed during %s userauth", 1917c478bd9Sstevel@tonic-gate get_method_name(authctxt)); 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate fatal_add_cleanup((void (*)(void *)) &do_pam_cleanup_proc, 1957c478bd9Sstevel@tonic-gate (void *) authctxt->pam); 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate rhost = get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping); 1987c478bd9Sstevel@tonic-gate if ((retval = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) { 1997c478bd9Sstevel@tonic-gate (void) pam_end(pamh, retval); 2007c478bd9Sstevel@tonic-gate fatal("Could not set PAM_RHOST item during %s userauth", 2017c478bd9Sstevel@tonic-gate get_method_name(authctxt)); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if ((retval = pam_set_item(pamh, PAM_TTY, "sshd")) != PAM_SUCCESS) { 2057c478bd9Sstevel@tonic-gate (void) pam_end(pamh, retval); 2067c478bd9Sstevel@tonic-gate fatal("Could not set PAM_TTY item during %s userauth", 2077c478bd9Sstevel@tonic-gate get_method_name(authctxt)); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate authctxt->pam->h = pamh; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * To be called from userauth methods, directly (as in keyboard-interactive) or 2157c478bd9Sstevel@tonic-gate * indirectly (from auth_pam_password() or from do_pam_non_initial_userauth(). 2167c478bd9Sstevel@tonic-gate * 2177c478bd9Sstevel@tonic-gate * The caller is responsible for calling new_start_pam() first. 2187c478bd9Sstevel@tonic-gate * 2197c478bd9Sstevel@tonic-gate * PAM state is not cleaned up here on error. This is left to subsequent calls 2207c478bd9Sstevel@tonic-gate * to new_start_pam() or to the cleanup function upon authentication error. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate int 2237c478bd9Sstevel@tonic-gate finish_userauth_do_pam(Authctxt *authctxt) 2247c478bd9Sstevel@tonic-gate { 2257c478bd9Sstevel@tonic-gate int retval; 2267c478bd9Sstevel@tonic-gate char *user, *method; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* Various checks; fail gracefully */ 2297c478bd9Sstevel@tonic-gate if (authctxt == NULL || authctxt->pam == NULL) 2307c478bd9Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if (compat20) { 2337c478bd9Sstevel@tonic-gate if (authctxt->method == NULL || authctxt->method->name == NULL) 2347c478bd9Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 2357c478bd9Sstevel@tonic-gate method = authctxt->method->name; 2367c478bd9Sstevel@tonic-gate } else if ((method = authctxt->v1_auth_name) == NULL) 2377c478bd9Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (AUTHPAM_DONE(authctxt)) 2407c478bd9Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_ACCT_MGMT)) { 2437c478bd9Sstevel@tonic-gate retval = pam_acct_mgmt(authctxt->pam->h, 0); 2447c478bd9Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval; 2457c478bd9Sstevel@tonic-gate if (retval == PAM_NEW_AUTHTOK_REQD) { 2467c478bd9Sstevel@tonic-gate userauth_force_kbdint(); 2477c478bd9Sstevel@tonic-gate return retval; 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate if (retval != PAM_SUCCESS) 2507c478bd9Sstevel@tonic-gate return retval; 2517c478bd9Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_ACCT_MGMT; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate /* 2557c478bd9Sstevel@tonic-gate * Handle PAM_USER change, if any. 2567c478bd9Sstevel@tonic-gate * 2577c478bd9Sstevel@tonic-gate * We do this before pam_open_session() because we need the PAM_USER's 2587c478bd9Sstevel@tonic-gate * UID for: 2597c478bd9Sstevel@tonic-gate * 2607c478bd9Sstevel@tonic-gate * a) PermitRootLogin checking 2617c478bd9Sstevel@tonic-gate * b) to get at the lastlog entry before pam_open_session() updates it. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate retval = pam_get_item(authctxt->pam->h, PAM_USER, (void **) &user); 2647c478bd9Sstevel@tonic-gate if (retval != PAM_SUCCESS) { 2657c478bd9Sstevel@tonic-gate fatal("PAM failure: pam_get_item(PAM_USER) " 2667c478bd9Sstevel@tonic-gate "returned %d: %.200s", retval, 2677c478bd9Sstevel@tonic-gate PAM_STRERROR(authctxt->pam->h, retval)); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if (user == NULL || *user == '\0') { 2717c478bd9Sstevel@tonic-gate debug("PAM set NULL PAM_USER"); 2727c478bd9Sstevel@tonic-gate return PAM_PERM_DENIED; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate if (strcmp(user, authctxt->user) != 0) { 2767c478bd9Sstevel@tonic-gate log("PAM changed the SSH username"); 2777c478bd9Sstevel@tonic-gate pwfree(&authctxt->pw); 2787c478bd9Sstevel@tonic-gate authctxt->pw = PRIVSEP(getpwnamallow(user)); 2797c478bd9Sstevel@tonic-gate authctxt->valid = (authctxt->pw != NULL); 2807c478bd9Sstevel@tonic-gate xfree(authctxt->user); 2817c478bd9Sstevel@tonic-gate authctxt->user = xstrdup(user); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate if (!authctxt->valid) { 2857c478bd9Sstevel@tonic-gate debug2("PAM set PAM_USER to unknown user"); 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Return success, userauth_finish() will catch 2887c478bd9Sstevel@tonic-gate * this and send back a failure message. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate return PAM_SUCCESS; 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* Check PermitRootLogin semantics */ 2947c478bd9Sstevel@tonic-gate if (authctxt->pw->pw_uid == 0 && !auth_root_allowed(method)) 2957c478bd9Sstevel@tonic-gate return PAM_PERM_DENIED; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_SETCRED)) { 2987c478bd9Sstevel@tonic-gate retval = pam_setcred(authctxt->pam->h, 2997c478bd9Sstevel@tonic-gate PAM_ESTABLISH_CRED); 3007c478bd9Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval; 3017c478bd9Sstevel@tonic-gate if (retval != PAM_SUCCESS) 3027c478bd9Sstevel@tonic-gate return retval; 3037c478bd9Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_SETCRED; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate #ifdef GSSAPI 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * Store GSS-API delegated creds after pam_setcred(), which may 3087c478bd9Sstevel@tonic-gate * have set the current credential store. 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate ssh_gssapi_storecreds(NULL, authctxt); 3117c478bd9Sstevel@tonic-gate #endif /* GSSAPI */ 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate /* 3157c478bd9Sstevel@tonic-gate * On Solaris pam_unix_session.so updates the lastlog, but does 3167c478bd9Sstevel@tonic-gate * not converse a PAM_TEXT_INFO message about it. So we need to 3177c478bd9Sstevel@tonic-gate * fetch the lastlog entry here and save it for use later. 3187c478bd9Sstevel@tonic-gate */ 3197c478bd9Sstevel@tonic-gate authctxt->last_login_time = 3207c478bd9Sstevel@tonic-gate get_last_login_time(authctxt->pw->pw_uid, 3217c478bd9Sstevel@tonic-gate authctxt->pw->pw_name, 3227c478bd9Sstevel@tonic-gate authctxt->last_login_host, 3237c478bd9Sstevel@tonic-gate sizeof(authctxt->last_login_host)); 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_OPEN_SESSION)) { 3267c478bd9Sstevel@tonic-gate retval = pam_open_session(authctxt->pam->h, 0); 3277c478bd9Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval; 3287c478bd9Sstevel@tonic-gate if (retval != PAM_SUCCESS) 3297c478bd9Sstevel@tonic-gate return retval; 3307c478bd9Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_OPEN_SESSION; 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * All PAM work done successfully. 3357c478bd9Sstevel@tonic-gate * 3367c478bd9Sstevel@tonic-gate * PAM handle stays around so we can call pam_close_session() on 3377c478bd9Sstevel@tonic-gate * it later. 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate return PAM_SUCCESS; 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate /* 3437c478bd9Sstevel@tonic-gate * PAM conversation function for non-interactive userauth methods that 3447c478bd9Sstevel@tonic-gate * really cannot do any prompting. Password userauth and CHANGEREQ can 3457c478bd9Sstevel@tonic-gate * always set the PAM_AUTHTOK and PAM_OLDAUTHTOK items to avoid 3467c478bd9Sstevel@tonic-gate * conversation (and if they do and nonetheless some module tries to 3477c478bd9Sstevel@tonic-gate * converse, then password userauth / CHANGEREQ MUST fail). 3487c478bd9Sstevel@tonic-gate * 3497c478bd9Sstevel@tonic-gate * Except, PAM_TEXT_INFO and PAM_ERROR_MSG prompts can be squirelled 3507c478bd9Sstevel@tonic-gate * away and shown to the user later. 3517c478bd9Sstevel@tonic-gate * 3527c478bd9Sstevel@tonic-gate * Keyboard-interactive userauth has its own much more interesting 3537c478bd9Sstevel@tonic-gate * conversation function. 3547c478bd9Sstevel@tonic-gate * 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate static int 3577c478bd9Sstevel@tonic-gate do_pam_conversation(int num_msg, const struct pam_message **msg, 3587c478bd9Sstevel@tonic-gate struct pam_response **resp, void *appdata_ptr) 3597c478bd9Sstevel@tonic-gate { 3607c478bd9Sstevel@tonic-gate struct pam_response *reply; 3617c478bd9Sstevel@tonic-gate int count; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate /* PAM will free this later */ 3647c478bd9Sstevel@tonic-gate reply = xmalloc(num_msg * sizeof(*reply)); 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate (void) memset(reply, 0, num_msg * sizeof(*reply)); 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate for (count = 0; count < num_msg; count++) { 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * We can't use stdio yet, queue messages for 3717c478bd9Sstevel@tonic-gate * printing later 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate switch(PAM_MSG_MEMBER(msg, count, msg_style)) { 3747c478bd9Sstevel@tonic-gate case PAM_PROMPT_ECHO_ON: 3757c478bd9Sstevel@tonic-gate xfree(reply); 3767c478bd9Sstevel@tonic-gate return PAM_CONV_ERR; 3777c478bd9Sstevel@tonic-gate case PAM_PROMPT_ECHO_OFF: 3787c478bd9Sstevel@tonic-gate xfree(reply); 3797c478bd9Sstevel@tonic-gate return PAM_CONV_ERR; 3807c478bd9Sstevel@tonic-gate break; 3817c478bd9Sstevel@tonic-gate case PAM_ERROR_MSG: 3827c478bd9Sstevel@tonic-gate case PAM_TEXT_INFO: 3837c478bd9Sstevel@tonic-gate if (PAM_MSG_MEMBER(msg, count, msg) != NULL) { 3847c478bd9Sstevel@tonic-gate message_cat(&__pam_msg, 3857c478bd9Sstevel@tonic-gate PAM_MSG_MEMBER(msg, count, msg)); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate reply[count].resp = xstrdup(""); 3887c478bd9Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS; 3897c478bd9Sstevel@tonic-gate break; 3907c478bd9Sstevel@tonic-gate default: 3917c478bd9Sstevel@tonic-gate xfree(reply); 3927c478bd9Sstevel@tonic-gate return PAM_CONV_ERR; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate *resp = reply; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate return PAM_SUCCESS; 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate /* Called at exit to cleanly shutdown PAM */ 4027c478bd9Sstevel@tonic-gate static void 4037c478bd9Sstevel@tonic-gate do_pam_cleanup_proc(void *context) 4047c478bd9Sstevel@tonic-gate { 4057c478bd9Sstevel@tonic-gate int pam_retval; 4067c478bd9Sstevel@tonic-gate pam_stuff *pam = (pam_stuff *) context; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if (pam == NULL) 4097c478bd9Sstevel@tonic-gate return; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate if (pam->authctxt != NULL && pam->authctxt->pam == pam) { 4127c478bd9Sstevel@tonic-gate pam->authctxt->pam_retval = pam->last_pam_retval; 4137c478bd9Sstevel@tonic-gate pam->authctxt->pam = NULL; 4147c478bd9Sstevel@tonic-gate pam->authctxt = NULL; 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (pam->h == NULL) 4187c478bd9Sstevel@tonic-gate return; 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * We're in fatal_cleanup() or not in userauth or without a 4227c478bd9Sstevel@tonic-gate * channel -- can't converse now, too bad. 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate pam_retval = pam_set_item(pam->h, PAM_CONV, NULL); 4257c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) { 4267c478bd9Sstevel@tonic-gate log("Cannot remove PAM conv, close session or delete creds[%d]: %.200s", 4277c478bd9Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 4287c478bd9Sstevel@tonic-gate goto cleanup; 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate if (pam->state & PAM_S_DONE_OPEN_SESSION) { 4327c478bd9Sstevel@tonic-gate pam_retval = pam_close_session(pam->h, 0); 4337c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 4347c478bd9Sstevel@tonic-gate log("Cannot close PAM session[%d]: %.200s", 4357c478bd9Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate if (pam->state & PAM_S_DONE_SETCRED) { 4397c478bd9Sstevel@tonic-gate pam_retval = pam_setcred(pam->h, PAM_DELETE_CRED); 4407c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 4417c478bd9Sstevel@tonic-gate debug("Cannot delete credentials[%d]: %.200s", 4427c478bd9Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate cleanup: 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate /* Use the previous PAM result, if not PAM_SUCCESS for pam_end() */ 4487c478bd9Sstevel@tonic-gate if (pam->last_pam_retval != PAM_SUCCESS) 4497c478bd9Sstevel@tonic-gate pam_retval = pam_end(pam->h, pam->last_pam_retval); 4507c478bd9Sstevel@tonic-gate else if (pam_retval != PAM_SUCCESS) 4517c478bd9Sstevel@tonic-gate pam_retval = pam_end(pam->h, pam_retval); 4527c478bd9Sstevel@tonic-gate else 4537c478bd9Sstevel@tonic-gate pam_retval = pam_end(pam->h, PAM_ABORT); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 4567c478bd9Sstevel@tonic-gate log("Cannot release PAM authentication[%d]: %.200s", 4577c478bd9Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate xfree(pam); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate /* Attempt password authentation using PAM */ 4637c478bd9Sstevel@tonic-gate int 4647c478bd9Sstevel@tonic-gate auth_pam_password(Authctxt *authctxt, const char *password) 4657c478bd9Sstevel@tonic-gate { 4667c478bd9Sstevel@tonic-gate int retval; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* Ensure we have a fresh PAM handle / state */ 4697c478bd9Sstevel@tonic-gate new_start_pam(authctxt, &conv); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate retval = pam_set_item(authctxt->pam->h, PAM_AUTHTOK, password); 4727c478bd9Sstevel@tonic-gate if (retval != PAM_SUCCESS) 4737c478bd9Sstevel@tonic-gate return 1; 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate retval = pam_authenticate(authctxt->pam->h, 4767c478bd9Sstevel@tonic-gate options.permit_empty_passwd ? 0 : 4777c478bd9Sstevel@tonic-gate PAM_DISALLOW_NULL_AUTHTOK); 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate if (retval != PAM_SUCCESS) 4807c478bd9Sstevel@tonic-gate return 0; 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate if ((retval = finish_userauth_do_pam(authctxt)) != PAM_SUCCESS) 4837c478bd9Sstevel@tonic-gate return 0; 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate if (authctxt->method) 4867c478bd9Sstevel@tonic-gate authctxt->method->authenticated = 1; /* SSHv2 */ 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate return 1; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate int 4927c478bd9Sstevel@tonic-gate do_pam_non_initial_userauth(Authctxt *authctxt) 4937c478bd9Sstevel@tonic-gate { 4947c478bd9Sstevel@tonic-gate new_start_pam(authctxt, NULL); 4957c478bd9Sstevel@tonic-gate return (finish_userauth_do_pam(authctxt) == PAM_SUCCESS); 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate /* Cleanly shutdown PAM */ 4997c478bd9Sstevel@tonic-gate void finish_pam(Authctxt *authctxt) 5007c478bd9Sstevel@tonic-gate { 5017c478bd9Sstevel@tonic-gate fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam); 5027c478bd9Sstevel@tonic-gate do_pam_cleanup_proc(authctxt->pam); 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate static 5067c478bd9Sstevel@tonic-gate char ** 5077c478bd9Sstevel@tonic-gate find_env(char **env, char *var) 5087c478bd9Sstevel@tonic-gate { 5097c478bd9Sstevel@tonic-gate char **p; 5107c478bd9Sstevel@tonic-gate int len; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate if (strchr(var, '=') == NULL) 5137c478bd9Sstevel@tonic-gate len = strlen(var); 5147c478bd9Sstevel@tonic-gate else 5157c478bd9Sstevel@tonic-gate len = (strchr(var, '=') - var) + 1; 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate for ( p = env ; p != NULL && *p != NULL ; p++ ) { 5187c478bd9Sstevel@tonic-gate if (strncmp(*p, var, len) == 0) 5197c478bd9Sstevel@tonic-gate return (p); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate return (NULL); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate /* Return list of PAM environment strings */ 5267c478bd9Sstevel@tonic-gate char ** 5277c478bd9Sstevel@tonic-gate fetch_pam_environment(Authctxt *authctxt) 5287c478bd9Sstevel@tonic-gate { 5297c478bd9Sstevel@tonic-gate #ifdef HAVE_PAM_GETENVLIST 5307c478bd9Sstevel@tonic-gate char **penv; 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate if (authctxt == NULL || authctxt->pam == NULL || 5337c478bd9Sstevel@tonic-gate authctxt->pam->h == NULL) 5347c478bd9Sstevel@tonic-gate return (NULL); 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate penv = pam_getenvlist(authctxt->pam->h); 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate return (penv); 5397c478bd9Sstevel@tonic-gate #else /* HAVE_PAM_GETENVLIST */ 5407c478bd9Sstevel@tonic-gate return(NULL); 5417c478bd9Sstevel@tonic-gate #endif /* HAVE_PAM_GETENVLIST */ 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate void free_pam_environment(char **env) 5457c478bd9Sstevel@tonic-gate { 5467c478bd9Sstevel@tonic-gate int i; 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate if (env != NULL) { 5497c478bd9Sstevel@tonic-gate for (i = 0; env[i] != NULL; i++) 5507c478bd9Sstevel@tonic-gate xfree(env[i]); 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate xfree(env); 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* Print any messages that have been generated during authentication */ 5577c478bd9Sstevel@tonic-gate /* or account checking to stderr */ 5587c478bd9Sstevel@tonic-gate void print_pam_messages(void) 5597c478bd9Sstevel@tonic-gate { 5607c478bd9Sstevel@tonic-gate if (__pam_msg != NULL) 5617c478bd9Sstevel@tonic-gate (void) fputs(__pam_msg, stderr); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate /* Append a message to buffer */ 5657c478bd9Sstevel@tonic-gate void message_cat(char **p, const char *a) 5667c478bd9Sstevel@tonic-gate { 5677c478bd9Sstevel@tonic-gate char *cp; 5687c478bd9Sstevel@tonic-gate size_t new_len; 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate new_len = strlen(a); 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate if (*p) { 5737c478bd9Sstevel@tonic-gate size_t len = strlen(*p); 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate *p = xrealloc(*p, new_len + len + 2); 5767c478bd9Sstevel@tonic-gate cp = *p + len; 5777c478bd9Sstevel@tonic-gate } else 5787c478bd9Sstevel@tonic-gate *p = cp = xmalloc(new_len + 2); 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate (void) memcpy(cp, a, new_len); 5817c478bd9Sstevel@tonic-gate cp[new_len] = '\n'; 5827c478bd9Sstevel@tonic-gate cp[new_len + 1] = '\0'; 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate #endif /* USE_PAM */ 586