1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 2000 Damien Miller. All rights reserved. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 5*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 6*0Sstevel@tonic-gate * are met: 7*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 8*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 9*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 10*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 11*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23*0Sstevel@tonic-gate */ 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 26*0Sstevel@tonic-gate * Use is subject to license terms. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "includes.h" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #ifdef USE_PAM 32*0Sstevel@tonic-gate #include "xmalloc.h" 33*0Sstevel@tonic-gate #include "log.h" 34*0Sstevel@tonic-gate #include "auth.h" 35*0Sstevel@tonic-gate #include "auth-options.h" 36*0Sstevel@tonic-gate #include "auth-pam.h" 37*0Sstevel@tonic-gate #include "servconf.h" 38*0Sstevel@tonic-gate #include "canohost.h" 39*0Sstevel@tonic-gate #include "compat.h" 40*0Sstevel@tonic-gate #include "misc.h" 41*0Sstevel@tonic-gate #include "sshlogin.h" 42*0Sstevel@tonic-gate #include "monitor_wrap.h" 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #include <security/pam_appl.h> 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate extern char *__progname; 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate extern int use_privsep; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate extern u_int utmp_len; 51*0Sstevel@tonic-gate extern ServerOptions options; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate extern Authmethod method_kbdint; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate RCSID("$Id: auth-pam.c,v 1.54 2002/07/28 20:24:08 stevesk Exp $"); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #define NEW_AUTHTOK_MSG \ 60*0Sstevel@tonic-gate "Warning: Your password has expired, please change it now." 61*0Sstevel@tonic-gate #define NEW_AUTHTOK_MSG_PRIVSEP \ 62*0Sstevel@tonic-gate "Your password has expired, the session cannot proceed." 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */ 65*0Sstevel@tonic-gate static int do_pam_conversation(int num_msg, const struct pam_message **msg, 66*0Sstevel@tonic-gate struct pam_response **resp, void *appdata_ptr); 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate static void do_pam_cleanup_proc(void *context); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate static char *get_method_name(Authctxt *authctxt); 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */ 73*0Sstevel@tonic-gate static struct pam_conv conv = { 74*0Sstevel@tonic-gate (int (*)())do_pam_conversation, 75*0Sstevel@tonic-gate NULL 76*0Sstevel@tonic-gate }; 77*0Sstevel@tonic-gate static char *__pam_msg = NULL; 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate static 80*0Sstevel@tonic-gate char * 81*0Sstevel@tonic-gate get_method_name(Authctxt *authctxt) 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate if (!authctxt) 84*0Sstevel@tonic-gate return "(unknown)"; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate if (!compat20) 87*0Sstevel@tonic-gate return (authctxt->v1_auth_name) ? authctxt->v1_auth_name : 88*0Sstevel@tonic-gate "(sshv1-unknown)"; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate if (!authctxt->method || !authctxt->method->name) 91*0Sstevel@tonic-gate return "(sshv2-unknown)"; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate return authctxt->method->name; 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate const 97*0Sstevel@tonic-gate char * 98*0Sstevel@tonic-gate derive_pam_svc_name(Authmethod *method) 99*0Sstevel@tonic-gate { 100*0Sstevel@tonic-gate if (compat20 && method) { 101*0Sstevel@tonic-gate char *method_name = method->name; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate if (!method_name) 104*0Sstevel@tonic-gate fatal("Userauth method unknown while starting PAM"); 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* For SSHv2 we use "sshd-<userauth name> */ 107*0Sstevel@tonic-gate if (strcmp(method_name, "none") == 0) { 108*0Sstevel@tonic-gate return "sshd-none"; 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate if (strcmp(method_name, "password") == 0) { 111*0Sstevel@tonic-gate return "sshd-password"; 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate if (strcmp(method_name, "keyboard-interactive") == 0) { 114*0Sstevel@tonic-gate /* "keyboard-interactive" is too long, shorten it */ 115*0Sstevel@tonic-gate return "sshd-kbdint"; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate if (strcmp(method_name, "publickey") == 0) { 118*0Sstevel@tonic-gate /* "publickey" is too long, shorten it */ 119*0Sstevel@tonic-gate return "sshd-pubkey"; 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate if (strcmp(method_name, "hostbased") == 0) { 122*0Sstevel@tonic-gate /* "hostbased" can't really be shortened... */ 123*0Sstevel@tonic-gate return "sshd-hostbased"; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate if (strncmp(method_name, "gss", 3) == 0) { 126*0Sstevel@tonic-gate /* "hostbased" can't really be shortened... */ 127*0Sstevel@tonic-gate return "sshd-gssapi"; 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate return "sshd-v1"; /* SSHv1 doesn't get to be so cool */ 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate void 135*0Sstevel@tonic-gate new_start_pam(Authctxt *authctxt, struct pam_conv *conv) 136*0Sstevel@tonic-gate { 137*0Sstevel@tonic-gate int retval; 138*0Sstevel@tonic-gate pam_handle_t *pamh; 139*0Sstevel@tonic-gate const char *rhost, *svc; 140*0Sstevel@tonic-gate char *user = NULL; 141*0Sstevel@tonic-gate pam_stuff *pam; 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate if (authctxt == NULL) 144*0Sstevel@tonic-gate fatal("Internal error during userauth"); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate if (compat20 && authctxt->method == NULL) 147*0Sstevel@tonic-gate fatal("Userauth method unknown while starting PAM"); 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate /* PAM service selected here */ 150*0Sstevel@tonic-gate svc = derive_pam_svc_name(authctxt->method); 151*0Sstevel@tonic-gate debug2("Starting PAM service %s for method %s", svc, 152*0Sstevel@tonic-gate get_method_name(authctxt)); 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate if (authctxt->user != NULL) 155*0Sstevel@tonic-gate user = authctxt->user; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate /* Cleanup previous PAM state */ 158*0Sstevel@tonic-gate if (authctxt->pam != NULL) { 159*0Sstevel@tonic-gate fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam); 160*0Sstevel@tonic-gate do_pam_cleanup_proc(authctxt->pam); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate pam = xmalloc(sizeof(pam_stuff)); 164*0Sstevel@tonic-gate (void) memset(pam, 0, sizeof(pam_stuff)); 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* 167*0Sstevel@tonic-gate * pam->last_pam_retval has to be and is considered 168*0Sstevel@tonic-gate * along with pam->state. 169*0Sstevel@tonic-gate * 170*0Sstevel@tonic-gate * pam->state = 0; -> no PAM auth, account, etc, work 171*0Sstevel@tonic-gate * done yet. (Set by memset() above.) 172*0Sstevel@tonic-gate * 173*0Sstevel@tonic-gate * pam->last_pam_retval = PAM_SUCCESS; -> meaningless at 174*0Sstevel@tonic-gate * this point. 175*0Sstevel@tonic-gate * 176*0Sstevel@tonic-gate * See finish_userauth_do_pam() below. 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate pam->authctxt = authctxt; 179*0Sstevel@tonic-gate pam->last_pam_retval = PAM_SUCCESS; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate authctxt->pam = pam; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate /* Free any previously stored text/error PAM prompts */ 184*0Sstevel@tonic-gate if (__pam_msg) { 185*0Sstevel@tonic-gate xfree(__pam_msg); 186*0Sstevel@tonic-gate __pam_msg = NULL; 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate if ((retval = pam_start(svc, user, conv, &pamh)) != PAM_SUCCESS) { 190*0Sstevel@tonic-gate fatal("PAM initialization failed during %s userauth", 191*0Sstevel@tonic-gate get_method_name(authctxt)); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate fatal_add_cleanup((void (*)(void *)) &do_pam_cleanup_proc, 195*0Sstevel@tonic-gate (void *) authctxt->pam); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate rhost = get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping); 198*0Sstevel@tonic-gate if ((retval = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) { 199*0Sstevel@tonic-gate (void) pam_end(pamh, retval); 200*0Sstevel@tonic-gate fatal("Could not set PAM_RHOST item during %s userauth", 201*0Sstevel@tonic-gate get_method_name(authctxt)); 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate if ((retval = pam_set_item(pamh, PAM_TTY, "sshd")) != PAM_SUCCESS) { 205*0Sstevel@tonic-gate (void) pam_end(pamh, retval); 206*0Sstevel@tonic-gate fatal("Could not set PAM_TTY item during %s userauth", 207*0Sstevel@tonic-gate get_method_name(authctxt)); 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate authctxt->pam->h = pamh; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* 214*0Sstevel@tonic-gate * To be called from userauth methods, directly (as in keyboard-interactive) or 215*0Sstevel@tonic-gate * indirectly (from auth_pam_password() or from do_pam_non_initial_userauth(). 216*0Sstevel@tonic-gate * 217*0Sstevel@tonic-gate * The caller is responsible for calling new_start_pam() first. 218*0Sstevel@tonic-gate * 219*0Sstevel@tonic-gate * PAM state is not cleaned up here on error. This is left to subsequent calls 220*0Sstevel@tonic-gate * to new_start_pam() or to the cleanup function upon authentication error. 221*0Sstevel@tonic-gate */ 222*0Sstevel@tonic-gate int 223*0Sstevel@tonic-gate finish_userauth_do_pam(Authctxt *authctxt) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate int retval; 226*0Sstevel@tonic-gate char *user, *method; 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate /* Various checks; fail gracefully */ 229*0Sstevel@tonic-gate if (authctxt == NULL || authctxt->pam == NULL) 230*0Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate if (compat20) { 233*0Sstevel@tonic-gate if (authctxt->method == NULL || authctxt->method->name == NULL) 234*0Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 235*0Sstevel@tonic-gate method = authctxt->method->name; 236*0Sstevel@tonic-gate } else if ((method = authctxt->v1_auth_name) == NULL) 237*0Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate if (AUTHPAM_DONE(authctxt)) 240*0Sstevel@tonic-gate return PAM_SYSTEM_ERR; /* shouldn't happen */ 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_ACCT_MGMT)) { 243*0Sstevel@tonic-gate retval = pam_acct_mgmt(authctxt->pam->h, 0); 244*0Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval; 245*0Sstevel@tonic-gate if (retval == PAM_NEW_AUTHTOK_REQD) { 246*0Sstevel@tonic-gate userauth_force_kbdint(); 247*0Sstevel@tonic-gate return retval; 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate if (retval != PAM_SUCCESS) 250*0Sstevel@tonic-gate return retval; 251*0Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_ACCT_MGMT; 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate /* 255*0Sstevel@tonic-gate * Handle PAM_USER change, if any. 256*0Sstevel@tonic-gate * 257*0Sstevel@tonic-gate * We do this before pam_open_session() because we need the PAM_USER's 258*0Sstevel@tonic-gate * UID for: 259*0Sstevel@tonic-gate * 260*0Sstevel@tonic-gate * a) PermitRootLogin checking 261*0Sstevel@tonic-gate * b) to get at the lastlog entry before pam_open_session() updates it. 262*0Sstevel@tonic-gate */ 263*0Sstevel@tonic-gate retval = pam_get_item(authctxt->pam->h, PAM_USER, (void **) &user); 264*0Sstevel@tonic-gate if (retval != PAM_SUCCESS) { 265*0Sstevel@tonic-gate fatal("PAM failure: pam_get_item(PAM_USER) " 266*0Sstevel@tonic-gate "returned %d: %.200s", retval, 267*0Sstevel@tonic-gate PAM_STRERROR(authctxt->pam->h, retval)); 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate if (user == NULL || *user == '\0') { 271*0Sstevel@tonic-gate debug("PAM set NULL PAM_USER"); 272*0Sstevel@tonic-gate return PAM_PERM_DENIED; 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate if (strcmp(user, authctxt->user) != 0) { 276*0Sstevel@tonic-gate log("PAM changed the SSH username"); 277*0Sstevel@tonic-gate pwfree(&authctxt->pw); 278*0Sstevel@tonic-gate authctxt->pw = PRIVSEP(getpwnamallow(user)); 279*0Sstevel@tonic-gate authctxt->valid = (authctxt->pw != NULL); 280*0Sstevel@tonic-gate xfree(authctxt->user); 281*0Sstevel@tonic-gate authctxt->user = xstrdup(user); 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate if (!authctxt->valid) { 285*0Sstevel@tonic-gate debug2("PAM set PAM_USER to unknown user"); 286*0Sstevel@tonic-gate /* 287*0Sstevel@tonic-gate * Return success, userauth_finish() will catch 288*0Sstevel@tonic-gate * this and send back a failure message. 289*0Sstevel@tonic-gate */ 290*0Sstevel@tonic-gate return PAM_SUCCESS; 291*0Sstevel@tonic-gate } 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate /* Check PermitRootLogin semantics */ 294*0Sstevel@tonic-gate if (authctxt->pw->pw_uid == 0 && !auth_root_allowed(method)) 295*0Sstevel@tonic-gate return PAM_PERM_DENIED; 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_SETCRED)) { 298*0Sstevel@tonic-gate retval = pam_setcred(authctxt->pam->h, 299*0Sstevel@tonic-gate PAM_ESTABLISH_CRED); 300*0Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval; 301*0Sstevel@tonic-gate if (retval != PAM_SUCCESS) 302*0Sstevel@tonic-gate return retval; 303*0Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_SETCRED; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate #ifdef GSSAPI 306*0Sstevel@tonic-gate /* 307*0Sstevel@tonic-gate * Store GSS-API delegated creds after pam_setcred(), which may 308*0Sstevel@tonic-gate * have set the current credential store. 309*0Sstevel@tonic-gate */ 310*0Sstevel@tonic-gate ssh_gssapi_storecreds(NULL, authctxt); 311*0Sstevel@tonic-gate #endif /* GSSAPI */ 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate /* 315*0Sstevel@tonic-gate * On Solaris pam_unix_session.so updates the lastlog, but does 316*0Sstevel@tonic-gate * not converse a PAM_TEXT_INFO message about it. So we need to 317*0Sstevel@tonic-gate * fetch the lastlog entry here and save it for use later. 318*0Sstevel@tonic-gate */ 319*0Sstevel@tonic-gate authctxt->last_login_time = 320*0Sstevel@tonic-gate get_last_login_time(authctxt->pw->pw_uid, 321*0Sstevel@tonic-gate authctxt->pw->pw_name, 322*0Sstevel@tonic-gate authctxt->last_login_host, 323*0Sstevel@tonic-gate sizeof(authctxt->last_login_host)); 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate if (!(authctxt->pam->state & PAM_S_DONE_OPEN_SESSION)) { 326*0Sstevel@tonic-gate retval = pam_open_session(authctxt->pam->h, 0); 327*0Sstevel@tonic-gate authctxt->pam->last_pam_retval = retval; 328*0Sstevel@tonic-gate if (retval != PAM_SUCCESS) 329*0Sstevel@tonic-gate return retval; 330*0Sstevel@tonic-gate authctxt->pam->state |= PAM_S_DONE_OPEN_SESSION; 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate /* 334*0Sstevel@tonic-gate * All PAM work done successfully. 335*0Sstevel@tonic-gate * 336*0Sstevel@tonic-gate * PAM handle stays around so we can call pam_close_session() on 337*0Sstevel@tonic-gate * it later. 338*0Sstevel@tonic-gate */ 339*0Sstevel@tonic-gate return PAM_SUCCESS; 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate /* 343*0Sstevel@tonic-gate * PAM conversation function for non-interactive userauth methods that 344*0Sstevel@tonic-gate * really cannot do any prompting. Password userauth and CHANGEREQ can 345*0Sstevel@tonic-gate * always set the PAM_AUTHTOK and PAM_OLDAUTHTOK items to avoid 346*0Sstevel@tonic-gate * conversation (and if they do and nonetheless some module tries to 347*0Sstevel@tonic-gate * converse, then password userauth / CHANGEREQ MUST fail). 348*0Sstevel@tonic-gate * 349*0Sstevel@tonic-gate * Except, PAM_TEXT_INFO and PAM_ERROR_MSG prompts can be squirelled 350*0Sstevel@tonic-gate * away and shown to the user later. 351*0Sstevel@tonic-gate * 352*0Sstevel@tonic-gate * Keyboard-interactive userauth has its own much more interesting 353*0Sstevel@tonic-gate * conversation function. 354*0Sstevel@tonic-gate * 355*0Sstevel@tonic-gate */ 356*0Sstevel@tonic-gate static int 357*0Sstevel@tonic-gate do_pam_conversation(int num_msg, const struct pam_message **msg, 358*0Sstevel@tonic-gate struct pam_response **resp, void *appdata_ptr) 359*0Sstevel@tonic-gate { 360*0Sstevel@tonic-gate struct pam_response *reply; 361*0Sstevel@tonic-gate int count; 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate /* PAM will free this later */ 364*0Sstevel@tonic-gate reply = xmalloc(num_msg * sizeof(*reply)); 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate (void) memset(reply, 0, num_msg * sizeof(*reply)); 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate for (count = 0; count < num_msg; count++) { 369*0Sstevel@tonic-gate /* 370*0Sstevel@tonic-gate * We can't use stdio yet, queue messages for 371*0Sstevel@tonic-gate * printing later 372*0Sstevel@tonic-gate */ 373*0Sstevel@tonic-gate switch(PAM_MSG_MEMBER(msg, count, msg_style)) { 374*0Sstevel@tonic-gate case PAM_PROMPT_ECHO_ON: 375*0Sstevel@tonic-gate xfree(reply); 376*0Sstevel@tonic-gate return PAM_CONV_ERR; 377*0Sstevel@tonic-gate case PAM_PROMPT_ECHO_OFF: 378*0Sstevel@tonic-gate xfree(reply); 379*0Sstevel@tonic-gate return PAM_CONV_ERR; 380*0Sstevel@tonic-gate break; 381*0Sstevel@tonic-gate case PAM_ERROR_MSG: 382*0Sstevel@tonic-gate case PAM_TEXT_INFO: 383*0Sstevel@tonic-gate if (PAM_MSG_MEMBER(msg, count, msg) != NULL) { 384*0Sstevel@tonic-gate message_cat(&__pam_msg, 385*0Sstevel@tonic-gate PAM_MSG_MEMBER(msg, count, msg)); 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate reply[count].resp = xstrdup(""); 388*0Sstevel@tonic-gate reply[count].resp_retcode = PAM_SUCCESS; 389*0Sstevel@tonic-gate break; 390*0Sstevel@tonic-gate default: 391*0Sstevel@tonic-gate xfree(reply); 392*0Sstevel@tonic-gate return PAM_CONV_ERR; 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate *resp = reply; 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate return PAM_SUCCESS; 399*0Sstevel@tonic-gate } 400*0Sstevel@tonic-gate 401*0Sstevel@tonic-gate /* Called at exit to cleanly shutdown PAM */ 402*0Sstevel@tonic-gate static void 403*0Sstevel@tonic-gate do_pam_cleanup_proc(void *context) 404*0Sstevel@tonic-gate { 405*0Sstevel@tonic-gate int pam_retval; 406*0Sstevel@tonic-gate pam_stuff *pam = (pam_stuff *) context; 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate if (pam == NULL) 409*0Sstevel@tonic-gate return; 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate if (pam->authctxt != NULL && pam->authctxt->pam == pam) { 412*0Sstevel@tonic-gate pam->authctxt->pam_retval = pam->last_pam_retval; 413*0Sstevel@tonic-gate pam->authctxt->pam = NULL; 414*0Sstevel@tonic-gate pam->authctxt = NULL; 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate if (pam->h == NULL) 418*0Sstevel@tonic-gate return; 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate /* 421*0Sstevel@tonic-gate * We're in fatal_cleanup() or not in userauth or without a 422*0Sstevel@tonic-gate * channel -- can't converse now, too bad. 423*0Sstevel@tonic-gate */ 424*0Sstevel@tonic-gate pam_retval = pam_set_item(pam->h, PAM_CONV, NULL); 425*0Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) { 426*0Sstevel@tonic-gate log("Cannot remove PAM conv, close session or delete creds[%d]: %.200s", 427*0Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 428*0Sstevel@tonic-gate goto cleanup; 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate if (pam->state & PAM_S_DONE_OPEN_SESSION) { 432*0Sstevel@tonic-gate pam_retval = pam_close_session(pam->h, 0); 433*0Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 434*0Sstevel@tonic-gate log("Cannot close PAM session[%d]: %.200s", 435*0Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate if (pam->state & PAM_S_DONE_SETCRED) { 439*0Sstevel@tonic-gate pam_retval = pam_setcred(pam->h, PAM_DELETE_CRED); 440*0Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 441*0Sstevel@tonic-gate debug("Cannot delete credentials[%d]: %.200s", 442*0Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate cleanup: 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate /* Use the previous PAM result, if not PAM_SUCCESS for pam_end() */ 448*0Sstevel@tonic-gate if (pam->last_pam_retval != PAM_SUCCESS) 449*0Sstevel@tonic-gate pam_retval = pam_end(pam->h, pam->last_pam_retval); 450*0Sstevel@tonic-gate else if (pam_retval != PAM_SUCCESS) 451*0Sstevel@tonic-gate pam_retval = pam_end(pam->h, pam_retval); 452*0Sstevel@tonic-gate else 453*0Sstevel@tonic-gate pam_retval = pam_end(pam->h, PAM_ABORT); 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gate if (pam_retval != PAM_SUCCESS) 456*0Sstevel@tonic-gate log("Cannot release PAM authentication[%d]: %.200s", 457*0Sstevel@tonic-gate pam_retval, PAM_STRERROR(pam->h, pam_retval)); 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate xfree(pam); 460*0Sstevel@tonic-gate } 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate /* Attempt password authentation using PAM */ 463*0Sstevel@tonic-gate int 464*0Sstevel@tonic-gate auth_pam_password(Authctxt *authctxt, const char *password) 465*0Sstevel@tonic-gate { 466*0Sstevel@tonic-gate int retval; 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate /* Ensure we have a fresh PAM handle / state */ 469*0Sstevel@tonic-gate new_start_pam(authctxt, &conv); 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate retval = pam_set_item(authctxt->pam->h, PAM_AUTHTOK, password); 472*0Sstevel@tonic-gate if (retval != PAM_SUCCESS) 473*0Sstevel@tonic-gate return 1; 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate retval = pam_authenticate(authctxt->pam->h, 476*0Sstevel@tonic-gate options.permit_empty_passwd ? 0 : 477*0Sstevel@tonic-gate PAM_DISALLOW_NULL_AUTHTOK); 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate if (retval != PAM_SUCCESS) 480*0Sstevel@tonic-gate return 0; 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate if ((retval = finish_userauth_do_pam(authctxt)) != PAM_SUCCESS) 483*0Sstevel@tonic-gate return 0; 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate if (authctxt->method) 486*0Sstevel@tonic-gate authctxt->method->authenticated = 1; /* SSHv2 */ 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate return 1; 489*0Sstevel@tonic-gate } 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gate int 492*0Sstevel@tonic-gate do_pam_non_initial_userauth(Authctxt *authctxt) 493*0Sstevel@tonic-gate { 494*0Sstevel@tonic-gate new_start_pam(authctxt, NULL); 495*0Sstevel@tonic-gate return (finish_userauth_do_pam(authctxt) == PAM_SUCCESS); 496*0Sstevel@tonic-gate } 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate /* Cleanly shutdown PAM */ 499*0Sstevel@tonic-gate void finish_pam(Authctxt *authctxt) 500*0Sstevel@tonic-gate { 501*0Sstevel@tonic-gate fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam); 502*0Sstevel@tonic-gate do_pam_cleanup_proc(authctxt->pam); 503*0Sstevel@tonic-gate } 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate static 506*0Sstevel@tonic-gate char ** 507*0Sstevel@tonic-gate find_env(char **env, char *var) 508*0Sstevel@tonic-gate { 509*0Sstevel@tonic-gate char **p; 510*0Sstevel@tonic-gate int len; 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate if (strchr(var, '=') == NULL) 513*0Sstevel@tonic-gate len = strlen(var); 514*0Sstevel@tonic-gate else 515*0Sstevel@tonic-gate len = (strchr(var, '=') - var) + 1; 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gate for ( p = env ; p != NULL && *p != NULL ; p++ ) { 518*0Sstevel@tonic-gate if (strncmp(*p, var, len) == 0) 519*0Sstevel@tonic-gate return (p); 520*0Sstevel@tonic-gate } 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate return (NULL); 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate /* Return list of PAM environment strings */ 526*0Sstevel@tonic-gate char ** 527*0Sstevel@tonic-gate fetch_pam_environment(Authctxt *authctxt) 528*0Sstevel@tonic-gate { 529*0Sstevel@tonic-gate #ifdef HAVE_PAM_GETENVLIST 530*0Sstevel@tonic-gate char **penv; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate if (authctxt == NULL || authctxt->pam == NULL || 533*0Sstevel@tonic-gate authctxt->pam->h == NULL) 534*0Sstevel@tonic-gate return (NULL); 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate penv = pam_getenvlist(authctxt->pam->h); 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gate return (penv); 539*0Sstevel@tonic-gate #else /* HAVE_PAM_GETENVLIST */ 540*0Sstevel@tonic-gate return(NULL); 541*0Sstevel@tonic-gate #endif /* HAVE_PAM_GETENVLIST */ 542*0Sstevel@tonic-gate } 543*0Sstevel@tonic-gate 544*0Sstevel@tonic-gate void free_pam_environment(char **env) 545*0Sstevel@tonic-gate { 546*0Sstevel@tonic-gate int i; 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate if (env != NULL) { 549*0Sstevel@tonic-gate for (i = 0; env[i] != NULL; i++) 550*0Sstevel@tonic-gate xfree(env[i]); 551*0Sstevel@tonic-gate } 552*0Sstevel@tonic-gate 553*0Sstevel@tonic-gate xfree(env); 554*0Sstevel@tonic-gate } 555*0Sstevel@tonic-gate 556*0Sstevel@tonic-gate /* Print any messages that have been generated during authentication */ 557*0Sstevel@tonic-gate /* or account checking to stderr */ 558*0Sstevel@tonic-gate void print_pam_messages(void) 559*0Sstevel@tonic-gate { 560*0Sstevel@tonic-gate if (__pam_msg != NULL) 561*0Sstevel@tonic-gate (void) fputs(__pam_msg, stderr); 562*0Sstevel@tonic-gate } 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate /* Append a message to buffer */ 565*0Sstevel@tonic-gate void message_cat(char **p, const char *a) 566*0Sstevel@tonic-gate { 567*0Sstevel@tonic-gate char *cp; 568*0Sstevel@tonic-gate size_t new_len; 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gate new_len = strlen(a); 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate if (*p) { 573*0Sstevel@tonic-gate size_t len = strlen(*p); 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate *p = xrealloc(*p, new_len + len + 2); 576*0Sstevel@tonic-gate cp = *p + len; 577*0Sstevel@tonic-gate } else 578*0Sstevel@tonic-gate *p = cp = xmalloc(new_len + 2); 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate (void) memcpy(cp, a, new_len); 581*0Sstevel@tonic-gate cp[new_len] = '\n'; 582*0Sstevel@tonic-gate cp[new_len + 1] = '\0'; 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate #endif /* USE_PAM */ 586