xref: /onnv-gate/usr/src/lib/pam_modules/dhkeys/dhkeys.c (revision 3641:4488c321dec8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3641Ssemery  * Common Development and Distribution License (the "License").
6*3641Ssemery  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3641Ssemery  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <syslog.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <rpc/rpc.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/wait.h>
380Sstevel@tonic-gate #include <limits.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <rpcsvc/nis.h>
410Sstevel@tonic-gate #include <rpcsvc/nispasswd.h>
420Sstevel@tonic-gate #include <rpcsvc/yppasswd.h>
430Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
440Sstevel@tonic-gate #include <rpc/key_prot.h>
450Sstevel@tonic-gate #include <rpc/rpc.h>
460Sstevel@tonic-gate #include <nfs/nfs.h>
470Sstevel@tonic-gate #include <nfs/nfssys.h>
480Sstevel@tonic-gate #include <nss_dbdefs.h>
490Sstevel@tonic-gate #include <nsswitch.h>
500Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <security/pam_appl.h>
530Sstevel@tonic-gate #include <security/pam_modules.h>
540Sstevel@tonic-gate #include <security/pam_impl.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <libintl.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/mman.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <passwdutil.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include "key_call_uid.h"
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* to keep track of codepath */
650Sstevel@tonic-gate #define	CODEPATH_PAM_SM_AUTHENTICATE	0
660Sstevel@tonic-gate #define	CODEPATH_PAM_SM_SETCRED		1
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #define	SUNW_OLDRPCPASS	"SUNW-OLD-RPC-PASSWORD"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate extern	int	_nfssys(int, void *);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * int msg(pamh, ...)
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * display message to the user
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate /*PRINTFLIKE2*/
780Sstevel@tonic-gate int
790Sstevel@tonic-gate msg(pam_handle_t *pamh, char *fmt, ...)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	va_list	ap;
820Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	va_start(ap, fmt);
850Sstevel@tonic-gate 	(void) vsnprintf(messages[0], sizeof (messages[0]), fmt, ap);
860Sstevel@tonic-gate 	va_end(ap);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	return (__pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL));
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Get the secret key for the given netname, key length, and algorithm
940Sstevel@tonic-gate  * type and send it to keyserv if the given pw decrypts it.  Update the
950Sstevel@tonic-gate  * following counter args as necessary: get_seckey_cnt, good_pw_cnt, and
960Sstevel@tonic-gate  * set_seckey_cnt.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * Returns 0 on malloc failure, else 1.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate static int
1010Sstevel@tonic-gate get_and_set_seckey(
1020Sstevel@tonic-gate 	pam_handle_t	*pamh,			/* in */
1030Sstevel@tonic-gate 	const char	*netname,		/* in */
1040Sstevel@tonic-gate 	keylen_t	keylen,			/* in */
1050Sstevel@tonic-gate 	algtype_t	algtype,		/* in */
1060Sstevel@tonic-gate 	const char	*pw,			/* in */
1070Sstevel@tonic-gate 	uid_t		uid,			/* in */
1080Sstevel@tonic-gate 	gid_t		gid,			/* in */
1090Sstevel@tonic-gate 	int		*get_seckey_cnt,	/* out */
1100Sstevel@tonic-gate 	int		*good_pw_cnt,		/* out */
1110Sstevel@tonic-gate 	int		*set_seckey_cnt,	/* out */
1120Sstevel@tonic-gate 	int		flags,			/* in */
1130Sstevel@tonic-gate 	int		debug)			/* in */
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	char	*skey;
1160Sstevel@tonic-gate 	int	skeylen;
1170Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	skeylen = BITS2NIBBLES(keylen) + 1;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if ((skey = malloc(skeylen)) == NULL) {
1220Sstevel@tonic-gate 		return (0);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (getsecretkey_g(netname, keylen, algtype, skey, skeylen, pw)) {
1260Sstevel@tonic-gate 		(*get_seckey_cnt)++;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		if (skey[0]) {
1290Sstevel@tonic-gate 			/* password does decrypt secret key */
1300Sstevel@tonic-gate 			(*good_pw_cnt)++;
1310Sstevel@tonic-gate 			if (key_setnet_g_uid(netname, skey, keylen, NULL, 0,
1320Sstevel@tonic-gate 						algtype, uid, gid) >= 0) {
1330Sstevel@tonic-gate 				(*set_seckey_cnt)++;
1340Sstevel@tonic-gate 			} else {
1350Sstevel@tonic-gate 				if (debug)
1360Sstevel@tonic-gate 					syslog(LOG_DEBUG, "pam_dhkeys: "
1370Sstevel@tonic-gate 						"get_and_set_seckey: could not "
1380Sstevel@tonic-gate 						"set secret key for keytype "
1390Sstevel@tonic-gate 						"%d-%d", keylen, algtype);
1400Sstevel@tonic-gate 			}
1410Sstevel@tonic-gate 		} else {
1420Sstevel@tonic-gate 			if (pamh && !(flags & PAM_SILENT)) {
1430Sstevel@tonic-gate 				(void) snprintf(messages[0],
1440Sstevel@tonic-gate 				    sizeof (messages[0]),
1450Sstevel@tonic-gate 				    dgettext(TEXT_DOMAIN,
1460Sstevel@tonic-gate 					"Password does not "
1470Sstevel@tonic-gate 					"decrypt secret key (type = %d-%d) "
1480Sstevel@tonic-gate 					"for '%s'."), keylen, algtype, netname);
1490Sstevel@tonic-gate 				(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
1500Sstevel@tonic-gate 							messages, NULL);
1510Sstevel@tonic-gate 			}
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 	} else {
1540Sstevel@tonic-gate 		if (debug)
1550Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: get_and_set_seckey: "
1560Sstevel@tonic-gate 				"could not get secret key for keytype %d-%d",
1570Sstevel@tonic-gate 				keylen, algtype);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	free(skey);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	return (1);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * int establish_key(pamh, flags, debug, netname)
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  * This routine established the Secure RPC Credentials for the
1690Sstevel@tonic-gate  * user specified in PAM_USER, using the password in PAM_AUTHTOK.
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * Because this routine is used for both pam_authenticate *and*
1720Sstevel@tonic-gate  * pam_setcred, we have to be somewhat careful:
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  *      - if called from pam_sm_authenticate:
1750Sstevel@tonic-gate  *		1. if we don't need creds (no NIS+ or not tight), we don't
1760Sstevel@tonic-gate  *		   set them (they will be set by pam_sm_setcred()) and return
1770Sstevel@tonic-gate  *		   PAM_IGNORE.
1780Sstevel@tonic-gate  *              2. if we do need to set them (passwd == "*NP*"), we try to
1790Sstevel@tonic-gate  *		   do so. Not having credentials in this case results in
1800Sstevel@tonic-gate  *		   PAM_AUTH_ERR.
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  *	- if called from pam_sm_setcred:
1830Sstevel@tonic-gate  *		If we are root (uid == 0), we do nothing and return PAM_IGNORE.
1840Sstevel@tonic-gate  *		Otherwise, we try to establish the credentials.
1850Sstevel@tonic-gate  *		Not having credentials in this case results in PAM_IGNORE.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  *	For both modi, we return PAM_IGNORE if the creds are established.
1880Sstevel@tonic-gate  *	If we fail, we return
1890Sstevel@tonic-gate  *	   - PAM_AUTH_ERR if the password didn't decrypt the cred
1900Sstevel@tonic-gate  *	   - PAM_SYSTEM_ERR if the cred's could not be stored.
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  * This routine returns the user's netname in "netname".
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  * All tools--but the PAM stack--currently use getpass() to obtain
1950Sstevel@tonic-gate  * the user's secure RPC password. We must make sure we don't use more than
1960Sstevel@tonic-gate  * the first des_block (eight) characters of whatever is handed down to us.
1970Sstevel@tonic-gate  * Therefore, we use a local variable "short_pass" to hold those 8 char's.
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate int
2000Sstevel@tonic-gate establish_key(pam_handle_t *pamh, int flags, int codepath, int debug,
2010Sstevel@tonic-gate 	char *netname)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	char	*user;
2040Sstevel@tonic-gate 	char	*passwd;
2050Sstevel@tonic-gate 	char	short_pass[sizeof (des_block)+1], *short_passp;
2060Sstevel@tonic-gate 	int	result;
2070Sstevel@tonic-gate 	uid_t	uid;
2080Sstevel@tonic-gate 	gid_t	gid;
2090Sstevel@tonic-gate 	int	err;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	struct passwd pw;	/* Needed to obtain uid */
2120Sstevel@tonic-gate 	char	*scratch;
2130Sstevel@tonic-gate 	int	scratchlen;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	int	need_cred;	/* is not having credentials set a failure? */
2160Sstevel@tonic-gate 	char *repository_name = NULL;	/* which repository are we using */
2170Sstevel@tonic-gate 	char *repository_pass = NULL;	/* user's password from that rep */
2180Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
2190Sstevel@tonic-gate 	struct pam_repository *auth_rep;
2200Sstevel@tonic-gate 	attrlist attr_pw[2];
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	mechanism_t	**mechs;
2230Sstevel@tonic-gate 	mechanism_t	**mpp;
2240Sstevel@tonic-gate 	int	get_seckey_cnt = 0;
2250Sstevel@tonic-gate 	int	set_seckey_cnt = 0;
2260Sstevel@tonic-gate 	int	good_pw_cnt = 0;
2270Sstevel@tonic-gate 	int	valid_mech_cnt = 0;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
2320Sstevel@tonic-gate 		if (debug)
2330Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
2340Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	scratchlen = sysconf(_SC_GETPW_R_SIZE_MAX);
2400Sstevel@tonic-gate 	if ((scratch = malloc(scratchlen)) == NULL)
2410Sstevel@tonic-gate 		return (PAM_BUF_ERR);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (getpwnam_r(user, &pw, scratch, scratchlen) == NULL) {
2440Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
2450Sstevel@tonic-gate 		goto out;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	uid = pw.pw_uid;
2490Sstevel@tonic-gate 	gid = pw.pw_gid;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * We don't set credentials when root logs in.
2530Sstevel@tonic-gate 	 * We do, however, need to set the credentials if the NIS+ permissions
2540Sstevel@tonic-gate 	 * require so. Thus, we only bail out if we're root and we're
2550Sstevel@tonic-gate 	 * called from pam_setcred.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	if (uid == 0 && codepath == CODEPATH_PAM_SM_SETCRED) {
2580Sstevel@tonic-gate 		result = PAM_IGNORE;
2590Sstevel@tonic-gate 		goto out;
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Check to see if we REALLY need to set the credentials, i.e.
2640Sstevel@tonic-gate 	 * whether not being able to do so is an error or whether we
2650Sstevel@tonic-gate 	 * can ignore it.
2660Sstevel@tonic-gate 	 * We need to get the password from the repository that we're
2670Sstevel@tonic-gate 	 * currently authenticating against. IFF this password equals
2680Sstevel@tonic-gate 	 * "*NP" *AND* we are authenticating against NIS+, we actually
2690Sstevel@tonic-gate 	 * do need to set the credentials. In all other cases, we
2700Sstevel@tonic-gate 	 * can forget about them.
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
2730Sstevel@tonic-gate 	if (auth_rep != NULL) {
2740Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
2750Sstevel@tonic-gate 			return (PAM_BUF_ERR);
2760Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
2770Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
2780Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
2790Sstevel@tonic-gate 	} else
2800Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	attr_pw[0].type = ATTR_PASSWD; attr_pw[0].next = &attr_pw[1];
2830Sstevel@tonic-gate 	attr_pw[1].type = ATTR_REP_NAME; attr_pw[1].next = NULL;
2840Sstevel@tonic-gate 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
2870Sstevel@tonic-gate 		free(pwu_rep);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND) {
2900Sstevel@tonic-gate 		if (debug)
2910Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user %s not found",
2920Sstevel@tonic-gate 				user);
2930Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
2940Sstevel@tonic-gate 		goto out;
2950Sstevel@tonic-gate 	} else if (result != PWU_SUCCESS) {
2960Sstevel@tonic-gate 		result = PAM_PERM_DENIED;
2970Sstevel@tonic-gate 		goto out;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	repository_name = attr_pw[1].data.val_s;
3010Sstevel@tonic-gate 	repository_pass = attr_pw[0].data.val_s;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	need_cred = (strcmp(repository_name, "nisplus") == 0 &&
3040Sstevel@tonic-gate 		strcmp(repository_pass, "*NP*") == 0);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (codepath == CODEPATH_PAM_SM_AUTHENTICATE && need_cred == 0) {
3070Sstevel@tonic-gate 		/*
3080Sstevel@tonic-gate 		 * No need to set credentials right now.
3090Sstevel@tonic-gate 		 * Will do so later through pam_sm_setcred()
3100Sstevel@tonic-gate 		 */
3110Sstevel@tonic-gate 		result = PAM_IGNORE;
3120Sstevel@tonic-gate 		goto out;
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (uid == 0)		/* "root", need to create a host-netname */
3160Sstevel@tonic-gate 		err = host2netname(netname, NULL, NULL);
3170Sstevel@tonic-gate 	else
3180Sstevel@tonic-gate 		err = user2netname(netname, uid, NULL);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (err != 1) {
3210Sstevel@tonic-gate 		if (debug)
3220Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user2netname failed");
3230Sstevel@tonic-gate 		if (need_cred) {
3240Sstevel@tonic-gate 			syslog(LOG_ALERT, "pam_dhkeys: user %s needs "
3250Sstevel@tonic-gate 			    "Secure RPC Credentials to login.", user);
3260Sstevel@tonic-gate 			result = PAM_SERVICE_ERR;
3270Sstevel@tonic-gate 		} else
3280Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
3290Sstevel@tonic-gate 		goto out;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/* passwd can be NULL (no passwd or su as root) */
3330Sstevel@tonic-gate 	if (passwd) {
3340Sstevel@tonic-gate 		(void) strlcpy(short_pass, passwd, sizeof (short_pass));
3350Sstevel@tonic-gate 		short_passp = short_pass;
3360Sstevel@tonic-gate 	} else
3370Sstevel@tonic-gate 		short_passp = NULL;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (mechs = __nis_get_mechanisms(FALSE)) {
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		for (mpp = mechs; *mpp; mpp++) {
3420Sstevel@tonic-gate 			mechanism_t *mp = *mpp;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 			if (AUTH_DES_COMPAT_CHK(mp))
3450Sstevel@tonic-gate 				break;	/* fall through to AUTH_DES below */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 			if (!VALID_MECH_ENTRY(mp))
3480Sstevel@tonic-gate 				continue;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 			if (debug)
3510Sstevel@tonic-gate 				syslog(LOG_DEBUG, "pam_dhkeys: trying "
3520Sstevel@tonic-gate 					"key type = %d-%d", mp->keylen,
3530Sstevel@tonic-gate 					mp->algtype);
3540Sstevel@tonic-gate 			valid_mech_cnt++;
3550Sstevel@tonic-gate 			if (!get_and_set_seckey(pamh, netname, mp->keylen,
3560Sstevel@tonic-gate 						mp->algtype, short_passp,
3570Sstevel@tonic-gate 						uid, gid,
3580Sstevel@tonic-gate 						&get_seckey_cnt, &good_pw_cnt,
3590Sstevel@tonic-gate 						&set_seckey_cnt, flags,
3600Sstevel@tonic-gate 						debug)) {
3610Sstevel@tonic-gate 				result = PAM_BUF_ERR;
3620Sstevel@tonic-gate 				goto out;
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		__nis_release_mechanisms(mechs);
3660Sstevel@tonic-gate 		/* fall through to AUTH_DES below */
3670Sstevel@tonic-gate 	} else {
3680Sstevel@tonic-gate 		/*
3690Sstevel@tonic-gate 		 * No usable mechs found in NIS+ security cf thus
3700Sstevel@tonic-gate 		 * fallback to AUTH_DES compat.
3710Sstevel@tonic-gate 		 */
3720Sstevel@tonic-gate 		if (debug)
3730Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: no valid mechs "
3740Sstevel@tonic-gate 				"found. Trying AUTH_DES.");
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * We always perform AUTH_DES for the benefit of non-NIS+
3790Sstevel@tonic-gate 	 * services (e.g. NFS) that may depend on the classic des
3800Sstevel@tonic-gate 	 * 192bit key being set.
3810Sstevel@tonic-gate 	 */
3820Sstevel@tonic-gate 	if (!get_and_set_seckey(pamh, netname, AUTH_DES_KEYLEN,
3830Sstevel@tonic-gate 	    AUTH_DES_ALGTYPE, short_passp, uid, gid, &get_seckey_cnt,
3840Sstevel@tonic-gate 	    &good_pw_cnt, &set_seckey_cnt, flags, debug)) {
3850Sstevel@tonic-gate 		result = PAM_BUF_ERR;
3860Sstevel@tonic-gate 		goto out;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (debug) {
3900Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: mech key totals:\n");
3910Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d valid mechanism(s)",
3920Sstevel@tonic-gate 			valid_mech_cnt);
3930Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) retrieved",
3940Sstevel@tonic-gate 			get_seckey_cnt);
3950Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d passwd decrypt successes",
3960Sstevel@tonic-gate 			good_pw_cnt);
3970Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) set",
3980Sstevel@tonic-gate 			set_seckey_cnt);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if (get_seckey_cnt == 0) {		/* No credentials */
4020Sstevel@tonic-gate 		result = need_cred ? PAM_AUTH_ERR : PAM_IGNORE;
4030Sstevel@tonic-gate 		goto out;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (good_pw_cnt == 0) {			/* wrong password */
4070Sstevel@tonic-gate 		result = PAM_AUTH_ERR;
4080Sstevel@tonic-gate 		goto out;
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (set_seckey_cnt == 0) {
4120Sstevel@tonic-gate 		result = PAM_SYSTEM_ERR;
4130Sstevel@tonic-gate 		goto out;
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	result = PAM_IGNORE;
4170Sstevel@tonic-gate out:
4180Sstevel@tonic-gate 	if (repository_name)
4190Sstevel@tonic-gate 		free(repository_name);
4200Sstevel@tonic-gate 	if (repository_pass)
4210Sstevel@tonic-gate 		free(repository_pass);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	free(scratch);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	return (result);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate int
4310Sstevel@tonic-gate pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	int	i;
4340Sstevel@tonic-gate 	int	debug = 0;
4350Sstevel@tonic-gate 	int	result;
4360Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
4390Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
4400Sstevel@tonic-gate 			debug = 1;
4410Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
4420Sstevel@tonic-gate 			flags |= PAM_SILENT;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	result = establish_key(pamh, flags, CODEPATH_PAM_SM_AUTHENTICATE, debug,
4460Sstevel@tonic-gate 				netname);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	return (result);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate int
4520Sstevel@tonic-gate remove_key(pam_handle_t *pamh, int flags, int debug)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	int result;
4550Sstevel@tonic-gate 	char *uname;
4560Sstevel@tonic-gate 	attrlist attr_pw[2];
4570Sstevel@tonic-gate 	struct pam_repository *auth_rep = NULL;
4580Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
4590Sstevel@tonic-gate 	uid_t uid;
4600Sstevel@tonic-gate 	gid_t gid;
4610Sstevel@tonic-gate 	struct nfs_revauth_args nra;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&uname);
4640Sstevel@tonic-gate 	if (uname == NULL || *uname == NULL) {
4650Sstevel@tonic-gate 		if (debug)
4660Sstevel@tonic-gate 			syslog(LOG_DEBUG,
4670Sstevel@tonic-gate 			    "pam_dhkeys: user NULL or empty in remove_key()");
4680Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (strcmp(uname, "root") == 0) {
4720Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
4730Sstevel@tonic-gate 			char msg[3][PAM_MAX_MSG_SIZE];
4740Sstevel@tonic-gate 			(void) snprintf(msg[0], sizeof (msg[0]),
4750Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
4760Sstevel@tonic-gate 				"removing root credentials would"
4770Sstevel@tonic-gate 				" break the rpc services that"));
4780Sstevel@tonic-gate 			(void) snprintf(msg[1], sizeof (msg[1]),
4790Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
4800Sstevel@tonic-gate 				"use secure rpc on this host!"));
4810Sstevel@tonic-gate 			(void) snprintf(msg[2], sizeof (msg[2]),
4820Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
4830Sstevel@tonic-gate 				"root may use keylogout -f to do"
4840Sstevel@tonic-gate 				" this (at your own risk)!"));
4850Sstevel@tonic-gate 			(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 3,
4860Sstevel@tonic-gate 			    msg, NULL);
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
4920Sstevel@tonic-gate 	if (auth_rep != NULL) {
4930Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
4940Sstevel@tonic-gate 			return (PAM_BUF_ERR);
4950Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
4960Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
4970Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
4980Sstevel@tonic-gate 	} else
4990Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/* Retrieve user's uid/gid from the password repository */
5020Sstevel@tonic-gate 	attr_pw[0].type = ATTR_UID; attr_pw[0].next = &attr_pw[1];
5030Sstevel@tonic-gate 	attr_pw[1].type = ATTR_GID; attr_pw[1].next = NULL;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	result = __get_authtoken_attr(uname, pwu_rep, attr_pw);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
5080Sstevel@tonic-gate 		free(pwu_rep);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND)
5110Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
5120Sstevel@tonic-gate 	if (result == PWU_DENIED)
5130Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
5140Sstevel@tonic-gate 	if (result != PWU_SUCCESS)
5150Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	uid = (uid_t)attr_pw[0].data.val_i;
5180Sstevel@tonic-gate 	gid = (gid_t)attr_pw[1].data.val_i;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	(void) key_removesecret_g_uid(uid, gid);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/* Revoke NFS DES credentials */
5230Sstevel@tonic-gate 	nra.authtype = AUTH_DES;
5240Sstevel@tonic-gate 	nra.uid = uid;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (_nfssys(NFS_REVAUTH, &nra) < 0) {
5270Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5280Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
5290Sstevel@tonic-gate 				"Warning: NFS credentials not destroyed"));
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 		return (PAM_AUTH_ERR);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	return (PAM_IGNORE);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate int
5380Sstevel@tonic-gate pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	int	i;
5410Sstevel@tonic-gate 	int	debug = 0;
5420Sstevel@tonic-gate 	int	result;
5430Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
5460Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
5470Sstevel@tonic-gate 			debug = 1;
5480Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
5490Sstevel@tonic-gate 			flags |= PAM_SILENT;
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/* Check for invalid flags */
5530Sstevel@tonic-gate 	if (flags && (flags & PAM_ESTABLISH_CRED) == 0 &&
5540Sstevel@tonic-gate 			(flags & PAM_REINITIALIZE_CRED) == 0 &&
5550Sstevel@tonic-gate 			(flags & PAM_REFRESH_CRED) == 0 &&
5560Sstevel@tonic-gate 			(flags & PAM_DELETE_CRED) == 0 &&
5570Sstevel@tonic-gate 			(flags & PAM_SILENT) == 0) {
5580Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_dhkeys: pam_setcred: illegal flags %d",
5590Sstevel@tonic-gate 				flags);
5600Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	if ((flags & PAM_REINITIALIZE_CRED) || (flags & PAM_REFRESH_CRED)) {
5650Sstevel@tonic-gate 		/* doesn't apply to UNIX */
5660Sstevel@tonic-gate 		if (debug)
5670Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: cred reinit/refresh "
5680Sstevel@tonic-gate 			    "ignored\n");
5690Sstevel@tonic-gate 		return (PAM_IGNORE);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (flags & PAM_DELETE_CRED) {
5730Sstevel@tonic-gate 		if (debug)
5740Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: removing creds\n");
5750Sstevel@tonic-gate 		result = remove_key(pamh, flags, debug);
5760Sstevel@tonic-gate 	} else {
5770Sstevel@tonic-gate 		result = establish_key(pamh, flags, CODEPATH_PAM_SM_SETCRED,
5780Sstevel@tonic-gate 					debug, netname);
5790Sstevel@tonic-gate 		/* Some diagnostics */
5800Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5810Sstevel@tonic-gate 			if (result == PAM_AUTH_ERR)
5820Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
5830Sstevel@tonic-gate 				    "Password does not decrypt any secret "
5840Sstevel@tonic-gate 				    "keys for %s."), netname);
5850Sstevel@tonic-gate 			else if (result == PAM_SYSTEM_ERR && netname[0])
5860Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
5870Sstevel@tonic-gate 				    "Could not set secret key(s) for %s. "
5880Sstevel@tonic-gate 				    "The key server may be down."), netname);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		/* Not having credentials set is not an error... */
5920Sstevel@tonic-gate 		result = PAM_IGNORE;
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	return (result);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate /*ARGSUSED*/
5990Sstevel@tonic-gate void
6000Sstevel@tonic-gate rpc_cleanup(pam_handle_t *pamh, void *data, int pam_status)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	if (data) {
6030Sstevel@tonic-gate 		(void) memset(data, 0, strlen(data));
6040Sstevel@tonic-gate 		free(data);
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate int
6090Sstevel@tonic-gate pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	int i;
6120Sstevel@tonic-gate 	int debug = 0;
6130Sstevel@tonic-gate 	int res;
6140Sstevel@tonic-gate 	pam_repository_t *pam_rep;
6150Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
6160Sstevel@tonic-gate 	char *oldpw;
6170Sstevel@tonic-gate 	char *user;
6180Sstevel@tonic-gate 	int tries;
6190Sstevel@tonic-gate 	int oldpw_ok;
6200Sstevel@tonic-gate 	char *oldrpcpw;
6210Sstevel@tonic-gate 	char *oldrpcpass;
6220Sstevel@tonic-gate 	char *data;
6230Sstevel@tonic-gate 	/* password truncated at 8 chars, see comment at establish_key() */
6240Sstevel@tonic-gate 	char short_pass[sizeof (des_block)+1], *short_passp;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	for (i = 0; i < argc; i++)
6270Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
6280Sstevel@tonic-gate 			debug = 1;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (debug)
6310Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: entered pam_sm_chauthtok()");
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if ((flags & PAM_PRELIM_CHECK) == 0)
6340Sstevel@tonic-gate 		return (PAM_IGNORE);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * See if the old secure-rpc password has already been set
6380Sstevel@tonic-gate 	 */
6390Sstevel@tonic-gate 	res = pam_get_data(pamh, SUNW_OLDRPCPASS, (const void **)&oldrpcpass);
6400Sstevel@tonic-gate 	if (res == PAM_SUCCESS) {
6410Sstevel@tonic-gate 		if (debug)
6420Sstevel@tonic-gate 			syslog(LOG_DEBUG,
6430Sstevel@tonic-gate 			    "pam_dhkeys: OLDRPCPASS already set");
6440Sstevel@tonic-gate 		return (PAM_IGNORE);
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&pam_rep);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&oldpw);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
6540Sstevel@tonic-gate 		if (debug)
6550Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
6560Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* oldpw can be NULL (eg. root changing someone's passwd) */
6600Sstevel@tonic-gate 	if (oldpw) {
6610Sstevel@tonic-gate 		(void) strlcpy(short_pass, oldpw, sizeof (short_pass));
6620Sstevel@tonic-gate 		short_passp = short_pass;
6630Sstevel@tonic-gate 	} else
6640Sstevel@tonic-gate 		short_passp = NULL;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/*
6670Sstevel@tonic-gate 	 * For NIS+ we need to check whether the old password equals
6680Sstevel@tonic-gate 	 * the RPC password. If it doesn't, we won't be able to update
6690Sstevel@tonic-gate 	 * the secure RPC credentials later on in the process.
6700Sstevel@tonic-gate 	 */
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (pam_rep == NULL)
6730Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
6740Sstevel@tonic-gate 	else {
6750Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
6760Sstevel@tonic-gate 			return (PAM_BUF_ERR);
6770Sstevel@tonic-gate 		pwu_rep->type = pam_rep->type;
6780Sstevel@tonic-gate 		pwu_rep->scope = pam_rep->scope;
6790Sstevel@tonic-gate 		pwu_rep->scope_len = pam_rep->scope_len;
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	switch (__verify_rpc_passwd(user, short_passp, pwu_rep)) {
6830Sstevel@tonic-gate 	case PWU_SUCCESS:
6840Sstevel@tonic-gate 		/* oldpw matches RPC password, or no RPC password needed */
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
6870Sstevel@tonic-gate 			free(pwu_rep);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		if (short_passp) {
6900Sstevel@tonic-gate 			if ((data = strdup(short_pass)) == NULL) {
6910Sstevel@tonic-gate 				(void) memset(short_pass, '\0',
6920Sstevel@tonic-gate 				    sizeof (short_pass));
6930Sstevel@tonic-gate 				return (PAM_BUF_ERR);
6940Sstevel@tonic-gate 			}
6950Sstevel@tonic-gate 		} else
6960Sstevel@tonic-gate 			data = NULL;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		(void) pam_set_data(pamh, SUNW_OLDRPCPASS, data, rpc_cleanup);
6990Sstevel@tonic-gate 		return (PAM_IGNORE);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	case PWU_NOT_FOUND:
7020Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7030Sstevel@tonic-gate 			free(pwu_rep);
7040Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7050Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
706*3641Ssemery 	case PWU_BAD_CREDPASS:
7070Sstevel@tonic-gate 		/* The old password does not decrypt any credentials */
7080Sstevel@tonic-gate 		break;
709*3641Ssemery 	case PWU_CRED_ERROR:
710*3641Ssemery 		/*
711*3641Ssemery 		 * Indicates that the user's credentials could not be
712*3641Ssemery 		 * retrieved or removed.  This could occur when a NIS+
713*3641Ssemery 		 * user is in transition to another account authority.
714*3641Ssemery 		 */
715*3641Ssemery 		if (pwu_rep != PWU_DEFAULT_REP)
716*3641Ssemery 			free(pwu_rep);
717*3641Ssemery 		(void) memset(short_pass, '\0', sizeof (short_pass));
718*3641Ssemery 		return (PAM_AUTHTOK_ERR);
7190Sstevel@tonic-gate 	default:
7200Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7210Sstevel@tonic-gate 			free(pwu_rep);
7220Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7230Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/*
7270Sstevel@tonic-gate 	 * We got here because the OLDAUTHTOK doesn't match the Secure RPC
7280Sstevel@tonic-gate 	 * password. In compliance with the old behavior, we give the
7290Sstevel@tonic-gate 	 * user two chances to get the password right. If that succeeds
7300Sstevel@tonic-gate 	 * all is well; if it doesn't, we'll return an error.
7310Sstevel@tonic-gate 	 */
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	(void) msg(pamh, dgettext(TEXT_DOMAIN,
7340Sstevel@tonic-gate 		"This password differs from your secure RPC password."));
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	tries = 0;
7370Sstevel@tonic-gate 	oldpw_ok = 0;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	while (oldpw_ok == 0 && ++tries < 3) {
7400Sstevel@tonic-gate 		if (tries > 1)
7410Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
7420Sstevel@tonic-gate 				"This password does not decrypt your "
7430Sstevel@tonic-gate 				"secure RPC password."));
7440Sstevel@tonic-gate 		res = __pam_get_authtok(pamh, PAM_PROMPT, 0,
7450Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN,
7460Sstevel@tonic-gate 		    "Please enter your old Secure RPC password: "), &oldpw);
7470Sstevel@tonic-gate 		if (res != PAM_SUCCESS) {
7480Sstevel@tonic-gate 			if (pwu_rep != PWU_DEFAULT_REP)
7490Sstevel@tonic-gate 				free(pwu_rep);
7500Sstevel@tonic-gate 			return (res);
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		(void) strlcpy(short_pass, oldpw, sizeof (short_pass));
7530Sstevel@tonic-gate 		(void) memset(oldpw, 0, strlen(oldpw));
7540Sstevel@tonic-gate 		free(oldpw);
7550Sstevel@tonic-gate 		oldpw = NULL;
7560Sstevel@tonic-gate 		if (__verify_rpc_passwd(user, short_pass, pwu_rep) ==
7570Sstevel@tonic-gate 		    PWU_SUCCESS)
7580Sstevel@tonic-gate 			oldpw_ok = 1;
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
7620Sstevel@tonic-gate 		free(pwu_rep);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	if (oldpw_ok == 0) {
7650Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7660Sstevel@tonic-gate 		return (PAM_AUTHTOK_ERR);
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	/*
7700Sstevel@tonic-gate 	 * Since the PAM framework only provides space for two different
7710Sstevel@tonic-gate 	 * password (one old and one current), there is officially no
7720Sstevel@tonic-gate 	 * place to put additional passwords (like our old rpc password).
7730Sstevel@tonic-gate 	 * We have no choice but to stuff it in a data item, and hope it
7740Sstevel@tonic-gate 	 * will be picked up by the password-update routines.
7750Sstevel@tonic-gate 	 */
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	oldrpcpw = strdup(short_pass);
7780Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (oldrpcpw == NULL)
7810Sstevel@tonic-gate 		return (PAM_BUF_ERR);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	res = pam_set_data(pamh, SUNW_OLDRPCPASS, oldrpcpw, rpc_cleanup);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	return (res);
7860Sstevel@tonic-gate }
787