xref: /onnv-gate/usr/src/lib/pam_modules/dhkeys/dhkeys.c (revision 4405:6d1bc9962bf5)
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
53641Ssemery  * Common Development and Distribution License (the "License").
63641Ssemery  * 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 /*
223641Ssemery  * 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>
39*4405Sjjj #include <signal.h>
40*4405Sjjj #include <pthread.h>
41*4405Sjjj #include <synch.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <rpcsvc/nis.h>
440Sstevel@tonic-gate #include <rpcsvc/nispasswd.h>
450Sstevel@tonic-gate #include <rpcsvc/yppasswd.h>
460Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
470Sstevel@tonic-gate #include <rpc/key_prot.h>
480Sstevel@tonic-gate #include <rpc/rpc.h>
490Sstevel@tonic-gate #include <nfs/nfs.h>
500Sstevel@tonic-gate #include <nfs/nfssys.h>
510Sstevel@tonic-gate #include <nss_dbdefs.h>
520Sstevel@tonic-gate #include <nsswitch.h>
530Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <security/pam_appl.h>
560Sstevel@tonic-gate #include <security/pam_modules.h>
570Sstevel@tonic-gate #include <security/pam_impl.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <libintl.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <sys/mman.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <passwdutil.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include "key_call_uid.h"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /* to keep track of codepath */
680Sstevel@tonic-gate #define	CODEPATH_PAM_SM_AUTHENTICATE	0
690Sstevel@tonic-gate #define	CODEPATH_PAM_SM_SETCRED		1
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #define	SUNW_OLDRPCPASS	"SUNW-OLD-RPC-PASSWORD"
720Sstevel@tonic-gate 
730Sstevel@tonic-gate extern	int	_nfssys(int, void *);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * int msg(pamh, ...)
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * display message to the user
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate /*PRINTFLIKE2*/
81*4405Sjjj static int
820Sstevel@tonic-gate msg(pam_handle_t *pamh, char *fmt, ...)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	va_list	ap;
850Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	va_start(ap, fmt);
880Sstevel@tonic-gate 	(void) vsnprintf(messages[0], sizeof (messages[0]), fmt, ap);
890Sstevel@tonic-gate 	va_end(ap);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	return (__pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL));
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Get the secret key for the given netname, key length, and algorithm
970Sstevel@tonic-gate  * type and send it to keyserv if the given pw decrypts it.  Update the
980Sstevel@tonic-gate  * following counter args as necessary: get_seckey_cnt, good_pw_cnt, and
990Sstevel@tonic-gate  * set_seckey_cnt.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Returns 0 on malloc failure, else 1.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static int
1040Sstevel@tonic-gate get_and_set_seckey(
1050Sstevel@tonic-gate 	pam_handle_t	*pamh,			/* in */
1060Sstevel@tonic-gate 	const char	*netname,		/* in */
1070Sstevel@tonic-gate 	keylen_t	keylen,			/* in */
1080Sstevel@tonic-gate 	algtype_t	algtype,		/* in */
1090Sstevel@tonic-gate 	const char	*pw,			/* in */
1100Sstevel@tonic-gate 	uid_t		uid,			/* in */
1110Sstevel@tonic-gate 	gid_t		gid,			/* in */
1120Sstevel@tonic-gate 	int		*get_seckey_cnt,	/* out */
1130Sstevel@tonic-gate 	int		*good_pw_cnt,		/* out */
1140Sstevel@tonic-gate 	int		*set_seckey_cnt,	/* out */
1150Sstevel@tonic-gate 	int		flags,			/* in */
1160Sstevel@tonic-gate 	int		debug)			/* in */
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	char	*skey;
1190Sstevel@tonic-gate 	int	skeylen;
1200Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	skeylen = BITS2NIBBLES(keylen) + 1;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if ((skey = malloc(skeylen)) == NULL) {
1250Sstevel@tonic-gate 		return (0);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (getsecretkey_g(netname, keylen, algtype, skey, skeylen, pw)) {
1290Sstevel@tonic-gate 		(*get_seckey_cnt)++;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 		if (skey[0]) {
1320Sstevel@tonic-gate 			/* password does decrypt secret key */
1330Sstevel@tonic-gate 			(*good_pw_cnt)++;
1340Sstevel@tonic-gate 			if (key_setnet_g_uid(netname, skey, keylen, NULL, 0,
135*4405Sjjj 			    algtype, uid, gid) >= 0) {
1360Sstevel@tonic-gate 				(*set_seckey_cnt)++;
1370Sstevel@tonic-gate 			} else {
1380Sstevel@tonic-gate 				if (debug)
1390Sstevel@tonic-gate 					syslog(LOG_DEBUG, "pam_dhkeys: "
140*4405Sjjj 					    "get_and_set_seckey: could not "
141*4405Sjjj 					    "set secret key for keytype "
142*4405Sjjj 					    "%d-%d", keylen, algtype);
1430Sstevel@tonic-gate 			}
1440Sstevel@tonic-gate 		} else {
1450Sstevel@tonic-gate 			if (pamh && !(flags & PAM_SILENT)) {
1460Sstevel@tonic-gate 				(void) snprintf(messages[0],
1470Sstevel@tonic-gate 				    sizeof (messages[0]),
1480Sstevel@tonic-gate 				    dgettext(TEXT_DOMAIN,
149*4405Sjjj 				    "Password does not "
150*4405Sjjj 				    "decrypt secret key (type = %d-%d) "
151*4405Sjjj 				    "for '%s'."), keylen, algtype, netname);
1520Sstevel@tonic-gate 				(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
153*4405Sjjj 				    messages, NULL);
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	} else {
1570Sstevel@tonic-gate 		if (debug)
1580Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: get_and_set_seckey: "
159*4405Sjjj 			    "could not get secret key for keytype %d-%d",
160*4405Sjjj 			    keylen, algtype);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	free(skey);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	return (1);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * int establish_key(pamh, flags, debug, netname)
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * This routine established the Secure RPC Credentials for the
1720Sstevel@tonic-gate  * user specified in PAM_USER, using the password in PAM_AUTHTOK.
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * Because this routine is used for both pam_authenticate *and*
1750Sstevel@tonic-gate  * pam_setcred, we have to be somewhat careful:
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *      - if called from pam_sm_authenticate:
1780Sstevel@tonic-gate  *		1. if we don't need creds (no NIS+ or not tight), we don't
1790Sstevel@tonic-gate  *		   set them (they will be set by pam_sm_setcred()) and return
1800Sstevel@tonic-gate  *		   PAM_IGNORE.
1810Sstevel@tonic-gate  *              2. if we do need to set them (passwd == "*NP*"), we try to
1820Sstevel@tonic-gate  *		   do so. Not having credentials in this case results in
1830Sstevel@tonic-gate  *		   PAM_AUTH_ERR.
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  *	- if called from pam_sm_setcred:
1860Sstevel@tonic-gate  *		If we are root (uid == 0), we do nothing and return PAM_IGNORE.
1870Sstevel@tonic-gate  *		Otherwise, we try to establish the credentials.
1880Sstevel@tonic-gate  *		Not having credentials in this case results in PAM_IGNORE.
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  *	For both modi, we return PAM_IGNORE if the creds are established.
1910Sstevel@tonic-gate  *	If we fail, we return
1920Sstevel@tonic-gate  *	   - PAM_AUTH_ERR if the password didn't decrypt the cred
1930Sstevel@tonic-gate  *	   - PAM_SYSTEM_ERR if the cred's could not be stored.
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  * This routine returns the user's netname in "netname".
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  * All tools--but the PAM stack--currently use getpass() to obtain
1980Sstevel@tonic-gate  * the user's secure RPC password. We must make sure we don't use more than
1990Sstevel@tonic-gate  * the first des_block (eight) characters of whatever is handed down to us.
2000Sstevel@tonic-gate  * Therefore, we use a local variable "short_pass" to hold those 8 char's.
2010Sstevel@tonic-gate  */
202*4405Sjjj static int
2030Sstevel@tonic-gate establish_key(pam_handle_t *pamh, int flags, int codepath, int debug,
2040Sstevel@tonic-gate 	char *netname)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	char	*user;
2070Sstevel@tonic-gate 	char	*passwd;
2080Sstevel@tonic-gate 	char	short_pass[sizeof (des_block)+1], *short_passp;
2090Sstevel@tonic-gate 	int	result;
2100Sstevel@tonic-gate 	uid_t	uid;
2110Sstevel@tonic-gate 	gid_t	gid;
2120Sstevel@tonic-gate 	int	err;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	struct passwd pw;	/* Needed to obtain uid */
2150Sstevel@tonic-gate 	char	*scratch;
2160Sstevel@tonic-gate 	int	scratchlen;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	int	need_cred;	/* is not having credentials set a failure? */
2190Sstevel@tonic-gate 	char *repository_name = NULL;	/* which repository are we using */
2200Sstevel@tonic-gate 	char *repository_pass = NULL;	/* user's password from that rep */
2210Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
2220Sstevel@tonic-gate 	struct pam_repository *auth_rep;
2230Sstevel@tonic-gate 	attrlist attr_pw[2];
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	mechanism_t	**mechs;
2260Sstevel@tonic-gate 	mechanism_t	**mpp;
2270Sstevel@tonic-gate 	int	get_seckey_cnt = 0;
2280Sstevel@tonic-gate 	int	set_seckey_cnt = 0;
2290Sstevel@tonic-gate 	int	good_pw_cnt = 0;
2300Sstevel@tonic-gate 	int	valid_mech_cnt = 0;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
2350Sstevel@tonic-gate 		if (debug)
2360Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
2370Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	scratchlen = sysconf(_SC_GETPW_R_SIZE_MAX);
2430Sstevel@tonic-gate 	if ((scratch = malloc(scratchlen)) == NULL)
2440Sstevel@tonic-gate 		return (PAM_BUF_ERR);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (getpwnam_r(user, &pw, scratch, scratchlen) == NULL) {
2470Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
2480Sstevel@tonic-gate 		goto out;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	uid = pw.pw_uid;
2520Sstevel@tonic-gate 	gid = pw.pw_gid;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * We don't set credentials when root logs in.
2560Sstevel@tonic-gate 	 * We do, however, need to set the credentials if the NIS+ permissions
2570Sstevel@tonic-gate 	 * require so. Thus, we only bail out if we're root and we're
2580Sstevel@tonic-gate 	 * called from pam_setcred.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	if (uid == 0 && codepath == CODEPATH_PAM_SM_SETCRED) {
2610Sstevel@tonic-gate 		result = PAM_IGNORE;
2620Sstevel@tonic-gate 		goto out;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * Check to see if we REALLY need to set the credentials, i.e.
2670Sstevel@tonic-gate 	 * whether not being able to do so is an error or whether we
2680Sstevel@tonic-gate 	 * can ignore it.
2690Sstevel@tonic-gate 	 * We need to get the password from the repository that we're
2700Sstevel@tonic-gate 	 * currently authenticating against. IFF this password equals
2710Sstevel@tonic-gate 	 * "*NP" *AND* we are authenticating against NIS+, we actually
2720Sstevel@tonic-gate 	 * do need to set the credentials. In all other cases, we
2730Sstevel@tonic-gate 	 * can forget about them.
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
2760Sstevel@tonic-gate 	if (auth_rep != NULL) {
2770Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
2780Sstevel@tonic-gate 			return (PAM_BUF_ERR);
2790Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
2800Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
2810Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
2820Sstevel@tonic-gate 	} else
2830Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	attr_pw[0].type = ATTR_PASSWD; attr_pw[0].next = &attr_pw[1];
2860Sstevel@tonic-gate 	attr_pw[1].type = ATTR_REP_NAME; attr_pw[1].next = NULL;
2870Sstevel@tonic-gate 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
2900Sstevel@tonic-gate 		free(pwu_rep);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND) {
2930Sstevel@tonic-gate 		if (debug)
2940Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user %s not found",
295*4405Sjjj 			    user);
2960Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
2970Sstevel@tonic-gate 		goto out;
2980Sstevel@tonic-gate 	} else if (result != PWU_SUCCESS) {
2990Sstevel@tonic-gate 		result = PAM_PERM_DENIED;
3000Sstevel@tonic-gate 		goto out;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	repository_name = attr_pw[1].data.val_s;
3040Sstevel@tonic-gate 	repository_pass = attr_pw[0].data.val_s;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	need_cred = (strcmp(repository_name, "nisplus") == 0 &&
307*4405Sjjj 	    strcmp(repository_pass, "*NP*") == 0);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (codepath == CODEPATH_PAM_SM_AUTHENTICATE && need_cred == 0) {
3100Sstevel@tonic-gate 		/*
3110Sstevel@tonic-gate 		 * No need to set credentials right now.
3120Sstevel@tonic-gate 		 * Will do so later through pam_sm_setcred()
3130Sstevel@tonic-gate 		 */
3140Sstevel@tonic-gate 		result = PAM_IGNORE;
3150Sstevel@tonic-gate 		goto out;
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (uid == 0)		/* "root", need to create a host-netname */
3190Sstevel@tonic-gate 		err = host2netname(netname, NULL, NULL);
3200Sstevel@tonic-gate 	else
3210Sstevel@tonic-gate 		err = user2netname(netname, uid, NULL);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (err != 1) {
3240Sstevel@tonic-gate 		if (debug)
3250Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user2netname failed");
3260Sstevel@tonic-gate 		if (need_cred) {
3270Sstevel@tonic-gate 			syslog(LOG_ALERT, "pam_dhkeys: user %s needs "
3280Sstevel@tonic-gate 			    "Secure RPC Credentials to login.", user);
3290Sstevel@tonic-gate 			result = PAM_SERVICE_ERR;
3300Sstevel@tonic-gate 		} else
3310Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
3320Sstevel@tonic-gate 		goto out;
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/* passwd can be NULL (no passwd or su as root) */
3360Sstevel@tonic-gate 	if (passwd) {
3370Sstevel@tonic-gate 		(void) strlcpy(short_pass, passwd, sizeof (short_pass));
3380Sstevel@tonic-gate 		short_passp = short_pass;
3390Sstevel@tonic-gate 	} else
3400Sstevel@tonic-gate 		short_passp = NULL;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (mechs = __nis_get_mechanisms(FALSE)) {
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		for (mpp = mechs; *mpp; mpp++) {
3450Sstevel@tonic-gate 			mechanism_t *mp = *mpp;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 			if (AUTH_DES_COMPAT_CHK(mp))
3480Sstevel@tonic-gate 				break;	/* fall through to AUTH_DES below */
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 			if (!VALID_MECH_ENTRY(mp))
3510Sstevel@tonic-gate 				continue;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 			if (debug)
3540Sstevel@tonic-gate 				syslog(LOG_DEBUG, "pam_dhkeys: trying "
355*4405Sjjj 				    "key type = %d-%d", mp->keylen,
356*4405Sjjj 				    mp->algtype);
3570Sstevel@tonic-gate 			valid_mech_cnt++;
3580Sstevel@tonic-gate 			if (!get_and_set_seckey(pamh, netname, mp->keylen,
359*4405Sjjj 			    mp->algtype, short_passp, uid, gid,
360*4405Sjjj 			    &get_seckey_cnt, &good_pw_cnt, &set_seckey_cnt,
361*4405Sjjj 			    flags, debug)) {
3620Sstevel@tonic-gate 				result = PAM_BUF_ERR;
3630Sstevel@tonic-gate 				goto out;
3640Sstevel@tonic-gate 			}
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 		__nis_release_mechanisms(mechs);
3670Sstevel@tonic-gate 		/* fall through to AUTH_DES below */
3680Sstevel@tonic-gate 	} else {
3690Sstevel@tonic-gate 		/*
3700Sstevel@tonic-gate 		 * No usable mechs found in NIS+ security cf thus
3710Sstevel@tonic-gate 		 * fallback to AUTH_DES compat.
3720Sstevel@tonic-gate 		 */
3730Sstevel@tonic-gate 		if (debug)
3740Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: no valid mechs "
375*4405Sjjj 			    "found. Trying AUTH_DES.");
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * We always perform AUTH_DES for the benefit of non-NIS+
3800Sstevel@tonic-gate 	 * services (e.g. NFS) that may depend on the classic des
3810Sstevel@tonic-gate 	 * 192bit key being set.
3820Sstevel@tonic-gate 	 */
3830Sstevel@tonic-gate 	if (!get_and_set_seckey(pamh, netname, AUTH_DES_KEYLEN,
3840Sstevel@tonic-gate 	    AUTH_DES_ALGTYPE, short_passp, uid, gid, &get_seckey_cnt,
3850Sstevel@tonic-gate 	    &good_pw_cnt, &set_seckey_cnt, flags, debug)) {
3860Sstevel@tonic-gate 		result = PAM_BUF_ERR;
3870Sstevel@tonic-gate 		goto out;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (debug) {
3910Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: mech key totals:\n");
3920Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d valid mechanism(s)",
393*4405Sjjj 		    valid_mech_cnt);
3940Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) retrieved",
395*4405Sjjj 		    get_seckey_cnt);
3960Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d passwd decrypt successes",
397*4405Sjjj 		    good_pw_cnt);
3980Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) set",
399*4405Sjjj 		    set_seckey_cnt);
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (get_seckey_cnt == 0) {		/* No credentials */
4030Sstevel@tonic-gate 		result = need_cred ? PAM_AUTH_ERR : PAM_IGNORE;
4040Sstevel@tonic-gate 		goto out;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (good_pw_cnt == 0) {			/* wrong password */
4080Sstevel@tonic-gate 		result = PAM_AUTH_ERR;
4090Sstevel@tonic-gate 		goto out;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if (set_seckey_cnt == 0) {
4130Sstevel@tonic-gate 		result = PAM_SYSTEM_ERR;
4140Sstevel@tonic-gate 		goto out;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	result = PAM_IGNORE;
4180Sstevel@tonic-gate out:
4190Sstevel@tonic-gate 	if (repository_name)
4200Sstevel@tonic-gate 		free(repository_name);
4210Sstevel@tonic-gate 	if (repository_pass)
4220Sstevel@tonic-gate 		free(repository_pass);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	free(scratch);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	return (result);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate int
4320Sstevel@tonic-gate pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	int	i;
4350Sstevel@tonic-gate 	int	debug = 0;
4360Sstevel@tonic-gate 	int	result;
4370Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
4400Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
4410Sstevel@tonic-gate 			debug = 1;
4420Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
4430Sstevel@tonic-gate 			flags |= PAM_SILENT;
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	result = establish_key(pamh, flags, CODEPATH_PAM_SM_AUTHENTICATE, debug,
447*4405Sjjj 	    netname);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	return (result);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
452*4405Sjjj 
453*4405Sjjj typedef struct argres {
454*4405Sjjj 	uid_t uid;
455*4405Sjjj 	int result;
456*4405Sjjj } argres_t;
457*4405Sjjj 
458*4405Sjjj /*
459*4405Sjjj  * Revoke NFS DES credentials.
460*4405Sjjj  * NFS may not be installed so we need to deal with SIGSYS
461*4405Sjjj  * when we call _nfssys(); we thus call _nfssys() in a seperate thread that
462*4405Sjjj  * is created specifically for this call. The thread specific signalmask
463*4405Sjjj  * is set to ignore SIGSYS. After the call to _nfssys(), the thread
464*4405Sjjj  * ceases to exist.
465*4405Sjjj  */
466*4405Sjjj static void *
467*4405Sjjj revoke_nfs_cred(void *ap)
468*4405Sjjj {
469*4405Sjjj 	struct nfs_revauth_args nra;
470*4405Sjjj 	sigset_t isigset;
471*4405Sjjj 	argres_t *argres = (argres_t *)ap;
472*4405Sjjj 
473*4405Sjjj 	nra.authtype = AUTH_DES;
474*4405Sjjj 	nra.uid = argres->uid;
475*4405Sjjj 
476*4405Sjjj 	(void) sigemptyset(&isigset);
477*4405Sjjj 	(void) sigaddset(&isigset, SIGSYS);
478*4405Sjjj 
479*4405Sjjj 	if (pthread_sigmask(SIG_BLOCK, &isigset, NULL) == 0) {
480*4405Sjjj 		argres->result = _nfssys(NFS_REVAUTH, &nra);
481*4405Sjjj 		if (argres->result < 0 && errno == ENOSYS) {
482*4405Sjjj 			argres->result = 0;
483*4405Sjjj 		}
484*4405Sjjj 	} else {
485*4405Sjjj 		argres->result = -1;
486*4405Sjjj 	}
487*4405Sjjj 	return (NULL);
488*4405Sjjj }
489*4405Sjjj 
490*4405Sjjj static int
4910Sstevel@tonic-gate remove_key(pam_handle_t *pamh, int flags, int debug)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	int result;
4940Sstevel@tonic-gate 	char *uname;
4950Sstevel@tonic-gate 	attrlist attr_pw[2];
4960Sstevel@tonic-gate 	struct pam_repository *auth_rep = NULL;
4970Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
4980Sstevel@tonic-gate 	uid_t uid;
4990Sstevel@tonic-gate 	gid_t gid;
500*4405Sjjj 	argres_t argres;
501*4405Sjjj 	thread_t tid;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&uname);
5040Sstevel@tonic-gate 	if (uname == NULL || *uname == NULL) {
5050Sstevel@tonic-gate 		if (debug)
5060Sstevel@tonic-gate 			syslog(LOG_DEBUG,
5070Sstevel@tonic-gate 			    "pam_dhkeys: user NULL or empty in remove_key()");
5080Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (strcmp(uname, "root") == 0) {
5120Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5130Sstevel@tonic-gate 			char msg[3][PAM_MAX_MSG_SIZE];
5140Sstevel@tonic-gate 			(void) snprintf(msg[0], sizeof (msg[0]),
5150Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
516*4405Sjjj 			    "removing root credentials would"
517*4405Sjjj 			    " break the rpc services that"));
5180Sstevel@tonic-gate 			(void) snprintf(msg[1], sizeof (msg[1]),
5190Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
520*4405Sjjj 			    "use secure rpc on this host!"));
5210Sstevel@tonic-gate 			(void) snprintf(msg[2], sizeof (msg[2]),
5220Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
523*4405Sjjj 			    "root may use keylogout -f to do"
524*4405Sjjj 			    " this (at your own risk)!"));
5250Sstevel@tonic-gate 			(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 3,
5260Sstevel@tonic-gate 			    msg, NULL);
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
5320Sstevel@tonic-gate 	if (auth_rep != NULL) {
5330Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
5340Sstevel@tonic-gate 			return (PAM_BUF_ERR);
5350Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
5360Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
5370Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
5380Sstevel@tonic-gate 	} else
5390Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/* Retrieve user's uid/gid from the password repository */
5420Sstevel@tonic-gate 	attr_pw[0].type = ATTR_UID; attr_pw[0].next = &attr_pw[1];
5430Sstevel@tonic-gate 	attr_pw[1].type = ATTR_GID; attr_pw[1].next = NULL;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	result = __get_authtoken_attr(uname, pwu_rep, attr_pw);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
5480Sstevel@tonic-gate 		free(pwu_rep);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND)
5510Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
5520Sstevel@tonic-gate 	if (result == PWU_DENIED)
5530Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
5540Sstevel@tonic-gate 	if (result != PWU_SUCCESS)
5550Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	uid = (uid_t)attr_pw[0].data.val_i;
5580Sstevel@tonic-gate 	gid = (gid_t)attr_pw[1].data.val_i;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	(void) key_removesecret_g_uid(uid, gid);
5610Sstevel@tonic-gate 
562*4405Sjjj 	argres.uid = uid;
563*4405Sjjj 	argres.result = -1;
5640Sstevel@tonic-gate 
565*4405Sjjj 	if (pthread_create(&tid, NULL, revoke_nfs_cred, (void *)&argres) == 0)
566*4405Sjjj 		(void) pthread_join(tid, NULL);
567*4405Sjjj 
568*4405Sjjj 	if (argres.result < 0) {
5690Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5700Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
571*4405Sjjj 			    "Warning: NFS credentials not destroyed"));
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 		return (PAM_AUTH_ERR);
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	return (PAM_IGNORE);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate int
5800Sstevel@tonic-gate pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	int	i;
5830Sstevel@tonic-gate 	int	debug = 0;
5840Sstevel@tonic-gate 	int	result;
5850Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
5880Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
5890Sstevel@tonic-gate 			debug = 1;
5900Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
5910Sstevel@tonic-gate 			flags |= PAM_SILENT;
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/* Check for invalid flags */
5950Sstevel@tonic-gate 	if (flags && (flags & PAM_ESTABLISH_CRED) == 0 &&
596*4405Sjjj 	    (flags & PAM_REINITIALIZE_CRED) == 0 &&
597*4405Sjjj 	    (flags & PAM_REFRESH_CRED) == 0 &&
598*4405Sjjj 	    (flags & PAM_DELETE_CRED) == 0 &&
599*4405Sjjj 	    (flags & PAM_SILENT) == 0) {
6000Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_dhkeys: pam_setcred: illegal flags %d",
601*4405Sjjj 		    flags);
6020Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if ((flags & PAM_REINITIALIZE_CRED) || (flags & PAM_REFRESH_CRED)) {
6070Sstevel@tonic-gate 		/* doesn't apply to UNIX */
6080Sstevel@tonic-gate 		if (debug)
6090Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: cred reinit/refresh "
6100Sstevel@tonic-gate 			    "ignored\n");
6110Sstevel@tonic-gate 		return (PAM_IGNORE);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (flags & PAM_DELETE_CRED) {
6150Sstevel@tonic-gate 		if (debug)
6160Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: removing creds\n");
6170Sstevel@tonic-gate 		result = remove_key(pamh, flags, debug);
6180Sstevel@tonic-gate 	} else {
6190Sstevel@tonic-gate 		result = establish_key(pamh, flags, CODEPATH_PAM_SM_SETCRED,
620*4405Sjjj 		    debug, netname);
6210Sstevel@tonic-gate 		/* Some diagnostics */
6220Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
6230Sstevel@tonic-gate 			if (result == PAM_AUTH_ERR)
6240Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
6250Sstevel@tonic-gate 				    "Password does not decrypt any secret "
6260Sstevel@tonic-gate 				    "keys for %s."), netname);
6270Sstevel@tonic-gate 			else if (result == PAM_SYSTEM_ERR && netname[0])
6280Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
6290Sstevel@tonic-gate 				    "Could not set secret key(s) for %s. "
6300Sstevel@tonic-gate 				    "The key server may be down."), netname);
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		/* Not having credentials set is not an error... */
6340Sstevel@tonic-gate 		result = PAM_IGNORE;
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	return (result);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /*ARGSUSED*/
6410Sstevel@tonic-gate void
6420Sstevel@tonic-gate rpc_cleanup(pam_handle_t *pamh, void *data, int pam_status)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate 	if (data) {
6450Sstevel@tonic-gate 		(void) memset(data, 0, strlen(data));
6460Sstevel@tonic-gate 		free(data);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate int
6510Sstevel@tonic-gate pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	int i;
6540Sstevel@tonic-gate 	int debug = 0;
6550Sstevel@tonic-gate 	int res;
6560Sstevel@tonic-gate 	pam_repository_t *pam_rep;
6570Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
6580Sstevel@tonic-gate 	char *oldpw;
6590Sstevel@tonic-gate 	char *user;
6600Sstevel@tonic-gate 	int tries;
6610Sstevel@tonic-gate 	int oldpw_ok;
6620Sstevel@tonic-gate 	char *oldrpcpw;
6630Sstevel@tonic-gate 	char *oldrpcpass;
6640Sstevel@tonic-gate 	char *data;
6650Sstevel@tonic-gate 	/* password truncated at 8 chars, see comment at establish_key() */
6660Sstevel@tonic-gate 	char short_pass[sizeof (des_block)+1], *short_passp;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	for (i = 0; i < argc; i++)
6690Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
6700Sstevel@tonic-gate 			debug = 1;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (debug)
6730Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: entered pam_sm_chauthtok()");
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if ((flags & PAM_PRELIM_CHECK) == 0)
6760Sstevel@tonic-gate 		return (PAM_IGNORE);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * See if the old secure-rpc password has already been set
6800Sstevel@tonic-gate 	 */
6810Sstevel@tonic-gate 	res = pam_get_data(pamh, SUNW_OLDRPCPASS, (const void **)&oldrpcpass);
6820Sstevel@tonic-gate 	if (res == PAM_SUCCESS) {
6830Sstevel@tonic-gate 		if (debug)
6840Sstevel@tonic-gate 			syslog(LOG_DEBUG,
6850Sstevel@tonic-gate 			    "pam_dhkeys: OLDRPCPASS already set");
6860Sstevel@tonic-gate 		return (PAM_IGNORE);
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&pam_rep);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&oldpw);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
6960Sstevel@tonic-gate 		if (debug)
6970Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
6980Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/* oldpw can be NULL (eg. root changing someone's passwd) */
7020Sstevel@tonic-gate 	if (oldpw) {
7030Sstevel@tonic-gate 		(void) strlcpy(short_pass, oldpw, sizeof (short_pass));
7040Sstevel@tonic-gate 		short_passp = short_pass;
7050Sstevel@tonic-gate 	} else
7060Sstevel@tonic-gate 		short_passp = NULL;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/*
7090Sstevel@tonic-gate 	 * For NIS+ we need to check whether the old password equals
7100Sstevel@tonic-gate 	 * the RPC password. If it doesn't, we won't be able to update
7110Sstevel@tonic-gate 	 * the secure RPC credentials later on in the process.
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (pam_rep == NULL)
7150Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
7160Sstevel@tonic-gate 	else {
7170Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
7180Sstevel@tonic-gate 			return (PAM_BUF_ERR);
7190Sstevel@tonic-gate 		pwu_rep->type = pam_rep->type;
7200Sstevel@tonic-gate 		pwu_rep->scope = pam_rep->scope;
7210Sstevel@tonic-gate 		pwu_rep->scope_len = pam_rep->scope_len;
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	switch (__verify_rpc_passwd(user, short_passp, pwu_rep)) {
7250Sstevel@tonic-gate 	case PWU_SUCCESS:
7260Sstevel@tonic-gate 		/* oldpw matches RPC password, or no RPC password needed */
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7290Sstevel@tonic-gate 			free(pwu_rep);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		if (short_passp) {
7320Sstevel@tonic-gate 			if ((data = strdup(short_pass)) == NULL) {
7330Sstevel@tonic-gate 				(void) memset(short_pass, '\0',
7340Sstevel@tonic-gate 				    sizeof (short_pass));
7350Sstevel@tonic-gate 				return (PAM_BUF_ERR);
7360Sstevel@tonic-gate 			}
7370Sstevel@tonic-gate 		} else
7380Sstevel@tonic-gate 			data = NULL;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		(void) pam_set_data(pamh, SUNW_OLDRPCPASS, data, rpc_cleanup);
7410Sstevel@tonic-gate 		return (PAM_IGNORE);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	case PWU_NOT_FOUND:
7440Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7450Sstevel@tonic-gate 			free(pwu_rep);
7460Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7470Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
7483641Ssemery 	case PWU_BAD_CREDPASS:
7490Sstevel@tonic-gate 		/* The old password does not decrypt any credentials */
7500Sstevel@tonic-gate 		break;
7513641Ssemery 	case PWU_CRED_ERROR:
7523641Ssemery 		/*
7533641Ssemery 		 * Indicates that the user's credentials could not be
7543641Ssemery 		 * retrieved or removed.  This could occur when a NIS+
7553641Ssemery 		 * user is in transition to another account authority.
7563641Ssemery 		 */
7573641Ssemery 		if (pwu_rep != PWU_DEFAULT_REP)
7583641Ssemery 			free(pwu_rep);
7593641Ssemery 		(void) memset(short_pass, '\0', sizeof (short_pass));
7603641Ssemery 		return (PAM_AUTHTOK_ERR);
7610Sstevel@tonic-gate 	default:
7620Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7630Sstevel@tonic-gate 			free(pwu_rep);
7640Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7650Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	/*
7690Sstevel@tonic-gate 	 * We got here because the OLDAUTHTOK doesn't match the Secure RPC
7700Sstevel@tonic-gate 	 * password. In compliance with the old behavior, we give the
7710Sstevel@tonic-gate 	 * user two chances to get the password right. If that succeeds
7720Sstevel@tonic-gate 	 * all is well; if it doesn't, we'll return an error.
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	(void) msg(pamh, dgettext(TEXT_DOMAIN,
776*4405Sjjj 	    "This password differs from your secure RPC password."));
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	tries = 0;
7790Sstevel@tonic-gate 	oldpw_ok = 0;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	while (oldpw_ok == 0 && ++tries < 3) {
7820Sstevel@tonic-gate 		if (tries > 1)
7830Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
784*4405Sjjj 			    "This password does not decrypt your "
785*4405Sjjj 			    "secure RPC password."));
7860Sstevel@tonic-gate 		res = __pam_get_authtok(pamh, PAM_PROMPT, 0,
7870Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN,
7880Sstevel@tonic-gate 		    "Please enter your old Secure RPC password: "), &oldpw);
7890Sstevel@tonic-gate 		if (res != PAM_SUCCESS) {
7900Sstevel@tonic-gate 			if (pwu_rep != PWU_DEFAULT_REP)
7910Sstevel@tonic-gate 				free(pwu_rep);
7920Sstevel@tonic-gate 			return (res);
7930Sstevel@tonic-gate 		}
7940Sstevel@tonic-gate 		(void) strlcpy(short_pass, oldpw, sizeof (short_pass));
7950Sstevel@tonic-gate 		(void) memset(oldpw, 0, strlen(oldpw));
7960Sstevel@tonic-gate 		free(oldpw);
7970Sstevel@tonic-gate 		oldpw = NULL;
7980Sstevel@tonic-gate 		if (__verify_rpc_passwd(user, short_pass, pwu_rep) ==
7990Sstevel@tonic-gate 		    PWU_SUCCESS)
8000Sstevel@tonic-gate 			oldpw_ok = 1;
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
8040Sstevel@tonic-gate 		free(pwu_rep);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (oldpw_ok == 0) {
8070Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
8080Sstevel@tonic-gate 		return (PAM_AUTHTOK_ERR);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/*
8120Sstevel@tonic-gate 	 * Since the PAM framework only provides space for two different
8130Sstevel@tonic-gate 	 * password (one old and one current), there is officially no
8140Sstevel@tonic-gate 	 * place to put additional passwords (like our old rpc password).
8150Sstevel@tonic-gate 	 * We have no choice but to stuff it in a data item, and hope it
8160Sstevel@tonic-gate 	 * will be picked up by the password-update routines.
8170Sstevel@tonic-gate 	 */
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	oldrpcpw = strdup(short_pass);
8200Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (oldrpcpw == NULL)
8230Sstevel@tonic-gate 		return (PAM_BUF_ERR);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	res = pam_set_data(pamh, SUNW_OLDRPCPASS, oldrpcpw, rpc_cleanup);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	return (res);
8280Sstevel@tonic-gate }
829