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