xref: /onnv-gate/usr/src/cmd/ssh/sshd/auth-pam.c (revision 5562:0f12179b71ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2000 Damien Miller.  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 /*
253908Sjp161948  * Copyright 2007 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 
310Sstevel@tonic-gate #ifdef USE_PAM
320Sstevel@tonic-gate #include "xmalloc.h"
330Sstevel@tonic-gate #include "log.h"
340Sstevel@tonic-gate #include "auth.h"
350Sstevel@tonic-gate #include "auth-options.h"
360Sstevel@tonic-gate #include "auth-pam.h"
370Sstevel@tonic-gate #include "servconf.h"
380Sstevel@tonic-gate #include "canohost.h"
390Sstevel@tonic-gate #include "compat.h"
400Sstevel@tonic-gate #include "misc.h"
410Sstevel@tonic-gate #include "sshlogin.h"
42*5562Sjp161948 #include "ssh-gss.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <security/pam_appl.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate extern char *__progname;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate extern u_int utmp_len;
490Sstevel@tonic-gate extern ServerOptions options;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate extern Authmethod method_kbdint;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate RCSID("$Id: auth-pam.c,v 1.54 2002/07/28 20:24:08 stevesk Exp $");
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #define NEW_AUTHTOK_MSG \
580Sstevel@tonic-gate 	"Warning: Your password has expired, please change it now."
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */
610Sstevel@tonic-gate static int do_pam_conversation(int num_msg, const struct pam_message **msg,
620Sstevel@tonic-gate 	struct pam_response **resp, void *appdata_ptr);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void do_pam_cleanup_proc(void *context);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static char *get_method_name(Authctxt *authctxt);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* PAM conversation for non-interactive userauth methods */
690Sstevel@tonic-gate static struct pam_conv conv = {
700Sstevel@tonic-gate 	(int (*)())do_pam_conversation,
710Sstevel@tonic-gate 	NULL
720Sstevel@tonic-gate };
730Sstevel@tonic-gate static char *__pam_msg = NULL;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static
760Sstevel@tonic-gate char *
770Sstevel@tonic-gate get_method_name(Authctxt *authctxt)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	if (!authctxt)
800Sstevel@tonic-gate 		return "(unknown)";
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (!compat20)
830Sstevel@tonic-gate 		return (authctxt->v1_auth_name) ? authctxt->v1_auth_name :
840Sstevel@tonic-gate 						  "(sshv1-unknown)";
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (!authctxt->method || !authctxt->method->name)
870Sstevel@tonic-gate 			return "(sshv2-unknown)";
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	return authctxt->method->name;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate const
930Sstevel@tonic-gate char *
940Sstevel@tonic-gate derive_pam_svc_name(Authmethod *method)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	if (compat20 && method) {
970Sstevel@tonic-gate 		char *method_name = method->name;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		if (!method_name)
1000Sstevel@tonic-gate 			fatal("Userauth method unknown while starting PAM");
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		/* For SSHv2 we use "sshd-<userauth name> */
1030Sstevel@tonic-gate 		if (strcmp(method_name, "none") == 0) {
1040Sstevel@tonic-gate 			return "sshd-none";
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 		if (strcmp(method_name, "password") == 0) {
1070Sstevel@tonic-gate 			return "sshd-password";
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 		if (strcmp(method_name, "keyboard-interactive") == 0) {
1100Sstevel@tonic-gate 			/* "keyboard-interactive" is too long, shorten it */
1110Sstevel@tonic-gate 			return "sshd-kbdint";
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 		if (strcmp(method_name, "publickey") == 0) {
1140Sstevel@tonic-gate 			/* "publickey" is too long, shorten it */
1150Sstevel@tonic-gate 			return "sshd-pubkey";
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 		if (strcmp(method_name, "hostbased") == 0) {
1180Sstevel@tonic-gate 			/* "hostbased" can't really be shortened... */
1190Sstevel@tonic-gate 			return "sshd-hostbased";
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 		if (strncmp(method_name, "gss", 3) == 0) {
1221411Sme23304 			/* "gss" is too short, elongate it */
1230Sstevel@tonic-gate 			return "sshd-gssapi";
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return "sshd-v1"; /* SSHv1 doesn't get to be so cool */
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate void
1310Sstevel@tonic-gate new_start_pam(Authctxt *authctxt, struct pam_conv *conv)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int		retval;
1340Sstevel@tonic-gate 	pam_handle_t	*pamh;
1350Sstevel@tonic-gate 	const char	*rhost, *svc;
1360Sstevel@tonic-gate 	char		*user = NULL;
1370Sstevel@tonic-gate 	pam_stuff	*pam;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (authctxt == NULL)
1400Sstevel@tonic-gate 		fatal("Internal error during userauth");
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (compat20 && authctxt->method == NULL)
1430Sstevel@tonic-gate 		fatal("Userauth method unknown while starting PAM");
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/* PAM service selected here */
1460Sstevel@tonic-gate 	svc = derive_pam_svc_name(authctxt->method);
1470Sstevel@tonic-gate 	debug2("Starting PAM service %s for method %s", svc,
1480Sstevel@tonic-gate 		get_method_name(authctxt));
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (authctxt->user != NULL)
1510Sstevel@tonic-gate 		user = authctxt->user;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* Cleanup previous PAM state */
1540Sstevel@tonic-gate 	if (authctxt->pam != NULL) {
1550Sstevel@tonic-gate 		fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam);
1560Sstevel@tonic-gate 		do_pam_cleanup_proc(authctxt->pam);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	pam = xmalloc(sizeof(pam_stuff));
1600Sstevel@tonic-gate 	(void) memset(pam, 0, sizeof(pam_stuff));
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * pam->last_pam_retval has to be and is considered
1640Sstevel@tonic-gate 	 * along with pam->state.
1650Sstevel@tonic-gate 	 *
1660Sstevel@tonic-gate 	 * pam->state = 0; -> no PAM auth, account, etc, work
1670Sstevel@tonic-gate 	 * done yet.  (Set by memset() above.)
1680Sstevel@tonic-gate 	 *
1690Sstevel@tonic-gate 	 * pam->last_pam_retval = PAM_SUCCESS; -> meaningless at
1700Sstevel@tonic-gate 	 * this point.
1710Sstevel@tonic-gate 	 *
1720Sstevel@tonic-gate 	 * See finish_userauth_do_pam() below.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	pam->authctxt = authctxt;
1750Sstevel@tonic-gate 	pam->last_pam_retval = PAM_SUCCESS;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	authctxt->pam = pam;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* Free any previously stored text/error PAM prompts */
1800Sstevel@tonic-gate 	if (__pam_msg) {
1810Sstevel@tonic-gate 		xfree(__pam_msg);
1820Sstevel@tonic-gate 		__pam_msg = NULL;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if ((retval = pam_start(svc, user, conv, &pamh)) != PAM_SUCCESS) {
1860Sstevel@tonic-gate 		fatal("PAM initialization failed during %s userauth",
1870Sstevel@tonic-gate 			get_method_name(authctxt));
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	fatal_add_cleanup((void (*)(void *)) &do_pam_cleanup_proc,
1910Sstevel@tonic-gate 			  (void *) authctxt->pam);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	rhost = get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping);
1940Sstevel@tonic-gate 	if ((retval = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) {
1950Sstevel@tonic-gate 		(void) pam_end(pamh, retval);
1960Sstevel@tonic-gate 		fatal("Could not set PAM_RHOST item during %s userauth",
1970Sstevel@tonic-gate 			get_method_name(authctxt));
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if ((retval = pam_set_item(pamh, PAM_TTY, "sshd")) != PAM_SUCCESS) {
2010Sstevel@tonic-gate 		(void) pam_end(pamh, retval);
2020Sstevel@tonic-gate 		fatal("Could not set PAM_TTY item during %s userauth",
2030Sstevel@tonic-gate 			get_method_name(authctxt));
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2063908Sjp161948 	if (authctxt->cuser != NULL)
2073908Sjp161948 		if ((retval = pam_set_item(pamh, PAM_AUSER, authctxt->cuser)) != PAM_SUCCESS) {
2083908Sjp161948 			(void) pam_end(pamh, retval);
2093908Sjp161948 			fatal("Could not set PAM_AUSER item during %s userauth",
2103908Sjp161948 				get_method_name(authctxt));
2113908Sjp161948 		}
2123908Sjp161948 
2130Sstevel@tonic-gate 	authctxt->pam->h = pamh;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * To be called from userauth methods, directly (as in keyboard-interactive) or
2180Sstevel@tonic-gate  * indirectly (from auth_pam_password() or from do_pam_non_initial_userauth().
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * The caller is responsible for calling new_start_pam() first.
2210Sstevel@tonic-gate  *
2220Sstevel@tonic-gate  * PAM state is not cleaned up here on error.  This is left to subsequent calls
2230Sstevel@tonic-gate  * to new_start_pam() or to the cleanup function upon authentication error.
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate int
2260Sstevel@tonic-gate finish_userauth_do_pam(Authctxt *authctxt)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	int retval;
2290Sstevel@tonic-gate 	char *user, *method;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* Various checks; fail gracefully */
2320Sstevel@tonic-gate 	if (authctxt == NULL || authctxt->pam == NULL)
2330Sstevel@tonic-gate 		return PAM_SYSTEM_ERR;	/* shouldn't happen */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (compat20) {
2360Sstevel@tonic-gate 		if (authctxt->method == NULL || authctxt->method->name == NULL)
2370Sstevel@tonic-gate 			return PAM_SYSTEM_ERR;	/* shouldn't happen */
2380Sstevel@tonic-gate 		method = authctxt->method->name;
2390Sstevel@tonic-gate 	} else if ((method = authctxt->v1_auth_name) == NULL)
2400Sstevel@tonic-gate 		return PAM_SYSTEM_ERR;	/* shouldn't happen */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (AUTHPAM_DONE(authctxt))
2430Sstevel@tonic-gate 		return PAM_SYSTEM_ERR;	/* shouldn't happen */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (!(authctxt->pam->state & PAM_S_DONE_ACCT_MGMT)) {
2460Sstevel@tonic-gate 		retval = pam_acct_mgmt(authctxt->pam->h, 0);
2470Sstevel@tonic-gate 		authctxt->pam->last_pam_retval = retval;
2480Sstevel@tonic-gate 		if (retval == PAM_NEW_AUTHTOK_REQD) {
2490Sstevel@tonic-gate 			userauth_force_kbdint();
2500Sstevel@tonic-gate 			return retval;
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 		if (retval != PAM_SUCCESS)
2530Sstevel@tonic-gate 			return retval;
2540Sstevel@tonic-gate 		authctxt->pam->state |= PAM_S_DONE_ACCT_MGMT;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Handle PAM_USER change, if any.
2590Sstevel@tonic-gate 	 *
2600Sstevel@tonic-gate 	 * We do this before pam_open_session() because we need the PAM_USER's
2610Sstevel@tonic-gate 	 * UID for:
2620Sstevel@tonic-gate 	 *
2630Sstevel@tonic-gate 	 * a) PermitRootLogin checking
2640Sstevel@tonic-gate 	 * b) to get at the lastlog entry before pam_open_session() updates it.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	retval = pam_get_item(authctxt->pam->h, PAM_USER, (void **) &user);
2670Sstevel@tonic-gate 	if (retval != PAM_SUCCESS) {
2680Sstevel@tonic-gate 		fatal("PAM failure: pam_get_item(PAM_USER) "
2690Sstevel@tonic-gate 		      "returned %d: %.200s", retval,
2700Sstevel@tonic-gate 		      PAM_STRERROR(authctxt->pam->h, retval));
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
2740Sstevel@tonic-gate 		debug("PAM set NULL PAM_USER");
2750Sstevel@tonic-gate 		return PAM_PERM_DENIED;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (strcmp(user, authctxt->user) != 0) {
2790Sstevel@tonic-gate 		log("PAM changed the SSH username");
2800Sstevel@tonic-gate 		pwfree(&authctxt->pw);
281*5562Sjp161948 		authctxt->pw = getpwnamallow(user);
2820Sstevel@tonic-gate 		authctxt->valid = (authctxt->pw != NULL);
2830Sstevel@tonic-gate 		xfree(authctxt->user);
2840Sstevel@tonic-gate 		authctxt->user = xstrdup(user);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (!authctxt->valid) {
2880Sstevel@tonic-gate 		debug2("PAM set PAM_USER to unknown user");
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * Return success, userauth_finish() will catch
2910Sstevel@tonic-gate 		 * this and send back a failure message.
2920Sstevel@tonic-gate 		 */
2930Sstevel@tonic-gate 		return PAM_SUCCESS;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/* Check PermitRootLogin semantics */
2970Sstevel@tonic-gate 	if (authctxt->pw->pw_uid == 0 && !auth_root_allowed(method))
2980Sstevel@tonic-gate 		return PAM_PERM_DENIED;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (!(authctxt->pam->state & PAM_S_DONE_SETCRED)) {
3010Sstevel@tonic-gate 		retval = pam_setcred(authctxt->pam->h,
3020Sstevel@tonic-gate 				     PAM_ESTABLISH_CRED);
3030Sstevel@tonic-gate 		authctxt->pam->last_pam_retval = retval;
3040Sstevel@tonic-gate 		if (retval != PAM_SUCCESS)
3050Sstevel@tonic-gate 			return retval;
3060Sstevel@tonic-gate 		authctxt->pam->state |= PAM_S_DONE_SETCRED;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate #ifdef GSSAPI
3090Sstevel@tonic-gate 		/*
3100Sstevel@tonic-gate 		 * Store GSS-API delegated creds after pam_setcred(), which may
3110Sstevel@tonic-gate 		 * have set the current credential store.
3120Sstevel@tonic-gate 		 */
3130Sstevel@tonic-gate 		ssh_gssapi_storecreds(NULL, authctxt);
3140Sstevel@tonic-gate #endif /* GSSAPI */
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/*
3180Sstevel@tonic-gate 	 * On Solaris pam_unix_session.so updates the lastlog, but does
3190Sstevel@tonic-gate 	 * not converse a PAM_TEXT_INFO message about it.  So we need to
3200Sstevel@tonic-gate 	 * fetch the lastlog entry here and save it for use later.
3210Sstevel@tonic-gate 	 */
3220Sstevel@tonic-gate 	authctxt->last_login_time =
3230Sstevel@tonic-gate 		get_last_login_time(authctxt->pw->pw_uid,
3240Sstevel@tonic-gate 			authctxt->pw->pw_name,
3250Sstevel@tonic-gate 			authctxt->last_login_host,
3260Sstevel@tonic-gate 			sizeof(authctxt->last_login_host));
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (!(authctxt->pam->state & PAM_S_DONE_OPEN_SESSION)) {
3290Sstevel@tonic-gate 		retval = pam_open_session(authctxt->pam->h, 0);
3300Sstevel@tonic-gate 		authctxt->pam->last_pam_retval = retval;
3310Sstevel@tonic-gate 		if (retval != PAM_SUCCESS)
3320Sstevel@tonic-gate 			return retval;
3330Sstevel@tonic-gate 		authctxt->pam->state |= PAM_S_DONE_OPEN_SESSION;
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * All PAM work done successfully.
3380Sstevel@tonic-gate 	 *
3390Sstevel@tonic-gate 	 * PAM handle stays around so we can call pam_close_session() on
3400Sstevel@tonic-gate 	 * it later.
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	return PAM_SUCCESS;
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * PAM conversation function for non-interactive userauth methods that
3470Sstevel@tonic-gate  * really cannot do any prompting.  Password userauth and CHANGEREQ can
3480Sstevel@tonic-gate  * always set the PAM_AUTHTOK and PAM_OLDAUTHTOK items to avoid
3490Sstevel@tonic-gate  * conversation (and if they do and nonetheless some module tries to
3500Sstevel@tonic-gate  * converse, then password userauth / CHANGEREQ MUST fail).
3510Sstevel@tonic-gate  *
3520Sstevel@tonic-gate  * Except, PAM_TEXT_INFO and PAM_ERROR_MSG prompts can be squirelled
3530Sstevel@tonic-gate  * away and shown to the user later.
3540Sstevel@tonic-gate  *
3550Sstevel@tonic-gate  * Keyboard-interactive userauth has its own much more interesting
3560Sstevel@tonic-gate  * conversation function.
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate static int
3600Sstevel@tonic-gate do_pam_conversation(int num_msg, const struct pam_message **msg,
3610Sstevel@tonic-gate 	struct pam_response **resp, void *appdata_ptr)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	struct pam_response *reply;
3640Sstevel@tonic-gate 	int count;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/* PAM will free this later */
3670Sstevel@tonic-gate 	reply = xmalloc(num_msg * sizeof(*reply));
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	(void) memset(reply, 0, num_msg * sizeof(*reply));
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	for (count = 0; count < num_msg; count++) {
3720Sstevel@tonic-gate 		/*
3730Sstevel@tonic-gate 		 * We can't use stdio yet, queue messages for
3740Sstevel@tonic-gate 		 * printing later
3750Sstevel@tonic-gate 		 */
3760Sstevel@tonic-gate 		switch(PAM_MSG_MEMBER(msg, count, msg_style)) {
3770Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
3780Sstevel@tonic-gate 			xfree(reply);
3790Sstevel@tonic-gate 			return PAM_CONV_ERR;
3800Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
3810Sstevel@tonic-gate 			xfree(reply);
3820Sstevel@tonic-gate 			return PAM_CONV_ERR;
3830Sstevel@tonic-gate 			break;
3840Sstevel@tonic-gate 		case PAM_ERROR_MSG:
3850Sstevel@tonic-gate 		case PAM_TEXT_INFO:
3860Sstevel@tonic-gate 			if (PAM_MSG_MEMBER(msg, count, msg) != NULL) {
3870Sstevel@tonic-gate 				message_cat(&__pam_msg,
3880Sstevel@tonic-gate 				    PAM_MSG_MEMBER(msg, count, msg));
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 			reply[count].resp = xstrdup("");
3910Sstevel@tonic-gate 			reply[count].resp_retcode = PAM_SUCCESS;
3920Sstevel@tonic-gate 			break;
3930Sstevel@tonic-gate 		default:
3940Sstevel@tonic-gate 			xfree(reply);
3950Sstevel@tonic-gate 			return PAM_CONV_ERR;
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	*resp = reply;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	return PAM_SUCCESS;
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /* Called at exit to cleanly shutdown PAM */
4050Sstevel@tonic-gate static void
4060Sstevel@tonic-gate do_pam_cleanup_proc(void *context)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	int pam_retval;
4090Sstevel@tonic-gate 	pam_stuff *pam = (pam_stuff *) context;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (pam == NULL)
4120Sstevel@tonic-gate 		return;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (pam->authctxt != NULL && pam->authctxt->pam == pam) {
4150Sstevel@tonic-gate 		pam->authctxt->pam_retval = pam->last_pam_retval;
4160Sstevel@tonic-gate 		pam->authctxt->pam = NULL;
4170Sstevel@tonic-gate 		pam->authctxt = NULL;
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (pam->h == NULL)
4210Sstevel@tonic-gate 		return;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * We're in fatal_cleanup() or not in userauth or without a
4250Sstevel@tonic-gate 	 * channel -- can't converse now, too bad.
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	pam_retval = pam_set_item(pam->h, PAM_CONV, NULL);
4280Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS) {
4290Sstevel@tonic-gate 		log("Cannot remove PAM conv, close session or delete creds[%d]: %.200s",
4300Sstevel@tonic-gate 			pam_retval, PAM_STRERROR(pam->h, pam_retval));
4310Sstevel@tonic-gate 		goto cleanup;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (pam->state & PAM_S_DONE_OPEN_SESSION) {
4350Sstevel@tonic-gate 		pam_retval = pam_close_session(pam->h, 0);
4360Sstevel@tonic-gate 		if (pam_retval != PAM_SUCCESS)
4370Sstevel@tonic-gate 			log("Cannot close PAM session[%d]: %.200s",
4380Sstevel@tonic-gate 			    pam_retval, PAM_STRERROR(pam->h, pam_retval));
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (pam->state & PAM_S_DONE_SETCRED) {
4420Sstevel@tonic-gate 		pam_retval = pam_setcred(pam->h, PAM_DELETE_CRED);
4430Sstevel@tonic-gate 		if (pam_retval != PAM_SUCCESS)
4440Sstevel@tonic-gate 			debug("Cannot delete credentials[%d]: %.200s",
4450Sstevel@tonic-gate 			    pam_retval, PAM_STRERROR(pam->h, pam_retval));
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate cleanup:
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/* Use the previous PAM result, if not PAM_SUCCESS for pam_end() */
4510Sstevel@tonic-gate 	if (pam->last_pam_retval != PAM_SUCCESS)
4520Sstevel@tonic-gate 		pam_retval = pam_end(pam->h, pam->last_pam_retval);
4530Sstevel@tonic-gate 	else if (pam_retval != PAM_SUCCESS)
4540Sstevel@tonic-gate 		pam_retval = pam_end(pam->h, pam_retval);
4550Sstevel@tonic-gate 	else
4560Sstevel@tonic-gate 		pam_retval = pam_end(pam->h, PAM_ABORT);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (pam_retval != PAM_SUCCESS)
4590Sstevel@tonic-gate 		log("Cannot release PAM authentication[%d]: %.200s",
4600Sstevel@tonic-gate 		    pam_retval, PAM_STRERROR(pam->h, pam_retval));
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	xfree(pam);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /* Attempt password authentation using PAM */
4660Sstevel@tonic-gate int
4670Sstevel@tonic-gate auth_pam_password(Authctxt *authctxt, const char *password)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	int retval;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* Ensure we have a fresh PAM handle / state */
4720Sstevel@tonic-gate 	new_start_pam(authctxt, &conv);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	retval = pam_set_item(authctxt->pam->h, PAM_AUTHTOK, password);
4750Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
4760Sstevel@tonic-gate 		return 1;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	retval = pam_authenticate(authctxt->pam->h,
4790Sstevel@tonic-gate 			options.permit_empty_passwd ?  0 :
4800Sstevel@tonic-gate 			PAM_DISALLOW_NULL_AUTHTOK);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	if (retval != PAM_SUCCESS)
4830Sstevel@tonic-gate 		return 0;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	if ((retval = finish_userauth_do_pam(authctxt)) != PAM_SUCCESS)
4860Sstevel@tonic-gate 		return 0;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (authctxt->method)
4890Sstevel@tonic-gate 		authctxt->method->authenticated = 1;	/* SSHv2 */
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	return 1;
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate int
4950Sstevel@tonic-gate do_pam_non_initial_userauth(Authctxt *authctxt)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	new_start_pam(authctxt, NULL);
4980Sstevel@tonic-gate 	return (finish_userauth_do_pam(authctxt) == PAM_SUCCESS);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /* Cleanly shutdown PAM */
5020Sstevel@tonic-gate void finish_pam(Authctxt *authctxt)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	fatal_remove_cleanup(&do_pam_cleanup_proc, authctxt->pam);
5050Sstevel@tonic-gate 	do_pam_cleanup_proc(authctxt->pam);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate static
5090Sstevel@tonic-gate char **
5100Sstevel@tonic-gate find_env(char **env, char *var)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	char **p;
5130Sstevel@tonic-gate 	int len;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (strchr(var, '=') == NULL)
5160Sstevel@tonic-gate 		len = strlen(var);
5170Sstevel@tonic-gate 	else
5180Sstevel@tonic-gate 		len = (strchr(var, '=') - var) + 1;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	for ( p = env ; p != NULL && *p != NULL ; p++ ) {
5210Sstevel@tonic-gate 		if (strncmp(*p, var, len) == 0)
5220Sstevel@tonic-gate 			return (p);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	return (NULL);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /* Return list of PAM environment strings */
5290Sstevel@tonic-gate char **
5300Sstevel@tonic-gate fetch_pam_environment(Authctxt *authctxt)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate #ifdef HAVE_PAM_GETENVLIST
5330Sstevel@tonic-gate 	char	**penv;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (authctxt == NULL || authctxt->pam == NULL ||
5360Sstevel@tonic-gate 	    authctxt->pam->h == NULL)
5370Sstevel@tonic-gate 		return (NULL);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	penv = pam_getenvlist(authctxt->pam->h);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	return (penv);
5420Sstevel@tonic-gate #else /* HAVE_PAM_GETENVLIST */
5430Sstevel@tonic-gate 	return(NULL);
5440Sstevel@tonic-gate #endif /* HAVE_PAM_GETENVLIST */
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate void free_pam_environment(char **env)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	int i;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (env != NULL) {
5520Sstevel@tonic-gate 		for (i = 0; env[i] != NULL; i++)
5530Sstevel@tonic-gate 			xfree(env[i]);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	xfree(env);
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate /* Print any messages that have been generated during authentication */
5600Sstevel@tonic-gate /* or account checking to stderr */
5610Sstevel@tonic-gate void print_pam_messages(void)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	if (__pam_msg != NULL)
5640Sstevel@tonic-gate 		(void) fputs(__pam_msg, stderr);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /* Append a message to buffer */
5680Sstevel@tonic-gate void message_cat(char **p, const char *a)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate 	char *cp;
5710Sstevel@tonic-gate 	size_t new_len;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	new_len = strlen(a);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if (*p) {
5760Sstevel@tonic-gate 		size_t len = strlen(*p);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		*p = xrealloc(*p, new_len + len + 2);
5790Sstevel@tonic-gate 		cp = *p + len;
5800Sstevel@tonic-gate 	} else
5810Sstevel@tonic-gate 		*p = cp = xmalloc(new_len + 2);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	(void) memcpy(cp, a, new_len);
5840Sstevel@tonic-gate 	cp[new_len] = '\n';
5850Sstevel@tonic-gate 	cp[new_len + 1] = '\0';
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate #endif /* USE_PAM */
589