xref: /onnv-gate/usr/src/lib/pam_modules/dhkeys/dhkeys.c (revision 5533:aa3e363e5ae3)
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>
394405Sjjj #include <signal.h>
404405Sjjj #include <pthread.h>
414405Sjjj #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*/
814405Sjjj 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,
1354405Sjjj 			    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: "
1404405Sjjj 					    "get_and_set_seckey: could not "
1414405Sjjj 					    "set secret key for keytype "
1424405Sjjj 					    "%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,
1494405Sjjj 				    "Password does not "
1504405Sjjj 				    "decrypt secret key (type = %d-%d) "
1514405Sjjj 				    "for '%s'."), keylen, algtype, netname);
1520Sstevel@tonic-gate 				(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
1534405Sjjj 				    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: "
1594405Sjjj 			    "could not get secret key for keytype %d-%d",
1604405Sjjj 			    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:
178*5533Ssdussud  *		1. if we don't need creds (no NIS+), we don't set them
179*5533Ssdussud  *		   and return PAM_IGNORE.
180*5533Ssdussud  *              2. else, we always try to establish credentials;
181*5533Ssdussud  *                 if (passwd == "*NP*"), not having credentials results
182*5533Ssdussud  *                 in PAM_AUTH_ERR.
183*5533Ssdussud  *                 if (passwd != "*NP*"), any failure to set credentials
184*5533Ssdussud  *                 results in PAM_IGNORE
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  *	- if called from pam_sm_setcred:
1870Sstevel@tonic-gate  *		If we are root (uid == 0), we do nothing and return PAM_IGNORE.
1880Sstevel@tonic-gate  *		Otherwise, we try to establish the credentials.
1890Sstevel@tonic-gate  *		Not having credentials in this case results in PAM_IGNORE.
1900Sstevel@tonic-gate  *
1910Sstevel@tonic-gate  *	For both modi, we return PAM_IGNORE if the creds are established.
1920Sstevel@tonic-gate  *	If we fail, we return
1930Sstevel@tonic-gate  *	   - PAM_AUTH_ERR if the password didn't decrypt the cred
1940Sstevel@tonic-gate  *	   - PAM_SYSTEM_ERR if the cred's could not be stored.
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  * This routine returns the user's netname in "netname".
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  * All tools--but the PAM stack--currently use getpass() to obtain
1990Sstevel@tonic-gate  * the user's secure RPC password. We must make sure we don't use more than
2000Sstevel@tonic-gate  * the first des_block (eight) characters of whatever is handed down to us.
2010Sstevel@tonic-gate  * Therefore, we use a local variable "short_pass" to hold those 8 char's.
2020Sstevel@tonic-gate  */
2034405Sjjj static int
2040Sstevel@tonic-gate establish_key(pam_handle_t *pamh, int flags, int codepath, int debug,
2050Sstevel@tonic-gate 	char *netname)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	char	*user;
2080Sstevel@tonic-gate 	char	*passwd;
2090Sstevel@tonic-gate 	char	short_pass[sizeof (des_block)+1], *short_passp;
2100Sstevel@tonic-gate 	int	result;
2110Sstevel@tonic-gate 	uid_t	uid;
2120Sstevel@tonic-gate 	gid_t	gid;
2130Sstevel@tonic-gate 	int	err;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	struct passwd pw;	/* Needed to obtain uid */
2160Sstevel@tonic-gate 	char	*scratch;
2170Sstevel@tonic-gate 	int	scratchlen;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	int	need_cred;	/* is not having credentials set a failure? */
220*5533Ssdussud 	int	auth_cred_flags;
221*5533Ssdussud 				/*
222*5533Ssdussud 				 * no_warn if creds not needed and
223*5533Ssdussud 				 * authenticating
224*5533Ssdussud 				 */
225*5533Ssdussud 	int	auth_path = (codepath == CODEPATH_PAM_SM_AUTHENTICATE);
2260Sstevel@tonic-gate 	char *repository_name = NULL;	/* which repository are we using */
2270Sstevel@tonic-gate 	char *repository_pass = NULL;	/* user's password from that rep */
2280Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
2290Sstevel@tonic-gate 	struct pam_repository *auth_rep;
2300Sstevel@tonic-gate 	attrlist attr_pw[2];
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	mechanism_t	**mechs;
2330Sstevel@tonic-gate 	mechanism_t	**mpp;
2340Sstevel@tonic-gate 	int	get_seckey_cnt = 0;
2350Sstevel@tonic-gate 	int	set_seckey_cnt = 0;
2360Sstevel@tonic-gate 	int	good_pw_cnt = 0;
2370Sstevel@tonic-gate 	int	valid_mech_cnt = 0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
2420Sstevel@tonic-gate 		if (debug)
2430Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
2440Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	scratchlen = sysconf(_SC_GETPW_R_SIZE_MAX);
2500Sstevel@tonic-gate 	if ((scratch = malloc(scratchlen)) == NULL)
2510Sstevel@tonic-gate 		return (PAM_BUF_ERR);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (getpwnam_r(user, &pw, scratch, scratchlen) == NULL) {
2540Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
2550Sstevel@tonic-gate 		goto out;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	uid = pw.pw_uid;
2590Sstevel@tonic-gate 	gid = pw.pw_gid;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * We don't set credentials when root logs in.
2630Sstevel@tonic-gate 	 * We do, however, need to set the credentials if the NIS+ permissions
2640Sstevel@tonic-gate 	 * require so. Thus, we only bail out if we're root and we're
2650Sstevel@tonic-gate 	 * called from pam_setcred.
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	if (uid == 0 && codepath == CODEPATH_PAM_SM_SETCRED) {
2680Sstevel@tonic-gate 		result = PAM_IGNORE;
2690Sstevel@tonic-gate 		goto out;
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Check to see if we REALLY need to set the credentials, i.e.
2740Sstevel@tonic-gate 	 * whether not being able to do so is an error or whether we
2750Sstevel@tonic-gate 	 * can ignore it.
2760Sstevel@tonic-gate 	 * We need to get the password from the repository that we're
2770Sstevel@tonic-gate 	 * currently authenticating against. IFF this password equals
2780Sstevel@tonic-gate 	 * "*NP" *AND* we are authenticating against NIS+, we actually
2790Sstevel@tonic-gate 	 * do need to set the credentials. In all other cases, we
2800Sstevel@tonic-gate 	 * can forget about them.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
2830Sstevel@tonic-gate 	if (auth_rep != NULL) {
2840Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
2850Sstevel@tonic-gate 			return (PAM_BUF_ERR);
2860Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
2870Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
2880Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
2890Sstevel@tonic-gate 	} else
2900Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	attr_pw[0].type = ATTR_PASSWD; attr_pw[0].next = &attr_pw[1];
2930Sstevel@tonic-gate 	attr_pw[1].type = ATTR_REP_NAME; attr_pw[1].next = NULL;
2940Sstevel@tonic-gate 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
2970Sstevel@tonic-gate 		free(pwu_rep);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND) {
3000Sstevel@tonic-gate 		if (debug)
3010Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user %s not found",
3024405Sjjj 			    user);
3030Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
3040Sstevel@tonic-gate 		goto out;
3050Sstevel@tonic-gate 	} else if (result != PWU_SUCCESS) {
3060Sstevel@tonic-gate 		result = PAM_PERM_DENIED;
3070Sstevel@tonic-gate 		goto out;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	repository_name = attr_pw[1].data.val_s;
3110Sstevel@tonic-gate 	repository_pass = attr_pw[0].data.val_s;
3120Sstevel@tonic-gate 
313*5533Ssdussud 	if (auth_path && (strcmp(repository_name, "nisplus") != 0)) {
3140Sstevel@tonic-gate 		result = PAM_IGNORE;
3150Sstevel@tonic-gate 		goto out;
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
318*5533Ssdussud 	need_cred = (strcmp(repository_pass, "*NP*") == 0);
319*5533Ssdussud 	if (auth_path) {
320*5533Ssdussud 		auth_cred_flags =
321*5533Ssdussud 		    (need_cred ? flags : flags | PAM_SILENT);
322*5533Ssdussud 	} else {
323*5533Ssdussud 		auth_cred_flags = flags;
324*5533Ssdussud 	}
325*5533Ssdussud 
3260Sstevel@tonic-gate 	if (uid == 0)		/* "root", need to create a host-netname */
3270Sstevel@tonic-gate 		err = host2netname(netname, NULL, NULL);
3280Sstevel@tonic-gate 	else
3290Sstevel@tonic-gate 		err = user2netname(netname, uid, NULL);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (err != 1) {
3320Sstevel@tonic-gate 		if (debug)
3330Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user2netname failed");
3340Sstevel@tonic-gate 		if (need_cred) {
3350Sstevel@tonic-gate 			syslog(LOG_ALERT, "pam_dhkeys: user %s needs "
3360Sstevel@tonic-gate 			    "Secure RPC Credentials to login.", user);
3370Sstevel@tonic-gate 			result = PAM_SERVICE_ERR;
3380Sstevel@tonic-gate 		} else
3390Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
3400Sstevel@tonic-gate 		goto out;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/* passwd can be NULL (no passwd or su as root) */
3440Sstevel@tonic-gate 	if (passwd) {
3450Sstevel@tonic-gate 		(void) strlcpy(short_pass, passwd, sizeof (short_pass));
3460Sstevel@tonic-gate 		short_passp = short_pass;
3470Sstevel@tonic-gate 	} else
3480Sstevel@tonic-gate 		short_passp = NULL;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (mechs = __nis_get_mechanisms(FALSE)) {
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		for (mpp = mechs; *mpp; mpp++) {
3530Sstevel@tonic-gate 			mechanism_t *mp = *mpp;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 			if (AUTH_DES_COMPAT_CHK(mp))
3560Sstevel@tonic-gate 				break;	/* fall through to AUTH_DES below */
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 			if (!VALID_MECH_ENTRY(mp))
3590Sstevel@tonic-gate 				continue;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 			if (debug)
3620Sstevel@tonic-gate 				syslog(LOG_DEBUG, "pam_dhkeys: trying "
3634405Sjjj 				    "key type = %d-%d", mp->keylen,
3644405Sjjj 				    mp->algtype);
3650Sstevel@tonic-gate 			valid_mech_cnt++;
3660Sstevel@tonic-gate 			if (!get_and_set_seckey(pamh, netname, mp->keylen,
3674405Sjjj 			    mp->algtype, short_passp, uid, gid,
3684405Sjjj 			    &get_seckey_cnt, &good_pw_cnt, &set_seckey_cnt,
369*5533Ssdussud 			    auth_cred_flags, debug)) {
3700Sstevel@tonic-gate 				result = PAM_BUF_ERR;
3710Sstevel@tonic-gate 				goto out;
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 		__nis_release_mechanisms(mechs);
3750Sstevel@tonic-gate 		/* fall through to AUTH_DES below */
3760Sstevel@tonic-gate 	} else {
3770Sstevel@tonic-gate 		/*
3780Sstevel@tonic-gate 		 * No usable mechs found in NIS+ security cf thus
3790Sstevel@tonic-gate 		 * fallback to AUTH_DES compat.
3800Sstevel@tonic-gate 		 */
3810Sstevel@tonic-gate 		if (debug)
3820Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: no valid mechs "
3834405Sjjj 			    "found. Trying AUTH_DES.");
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * We always perform AUTH_DES for the benefit of non-NIS+
3880Sstevel@tonic-gate 	 * services (e.g. NFS) that may depend on the classic des
3890Sstevel@tonic-gate 	 * 192bit key being set.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	if (!get_and_set_seckey(pamh, netname, AUTH_DES_KEYLEN,
3920Sstevel@tonic-gate 	    AUTH_DES_ALGTYPE, short_passp, uid, gid, &get_seckey_cnt,
393*5533Ssdussud 	    &good_pw_cnt, &set_seckey_cnt, auth_cred_flags, debug)) {
3940Sstevel@tonic-gate 		result = PAM_BUF_ERR;
3950Sstevel@tonic-gate 		goto out;
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if (debug) {
3990Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: mech key totals:\n");
4000Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d valid mechanism(s)",
4014405Sjjj 		    valid_mech_cnt);
4020Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) retrieved",
4034405Sjjj 		    get_seckey_cnt);
4040Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d passwd decrypt successes",
4054405Sjjj 		    good_pw_cnt);
4060Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) set",
4074405Sjjj 		    set_seckey_cnt);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (get_seckey_cnt == 0) {		/* No credentials */
4110Sstevel@tonic-gate 		result = need_cred ? PAM_AUTH_ERR : PAM_IGNORE;
4120Sstevel@tonic-gate 		goto out;
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (good_pw_cnt == 0) {			/* wrong password */
416*5533Ssdussud 		if (auth_path) {
417*5533Ssdussud 			result = need_cred ? PAM_AUTH_ERR : PAM_IGNORE;
418*5533Ssdussud 		} else {
419*5533Ssdussud 			result = PAM_AUTH_ERR;
420*5533Ssdussud 		}
4210Sstevel@tonic-gate 		goto out;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (set_seckey_cnt == 0) {
425*5533Ssdussud 		if (auth_path) {
426*5533Ssdussud 			result = need_cred ? PAM_SYSTEM_ERR : PAM_IGNORE;
427*5533Ssdussud 		} else {
428*5533Ssdussud 			result = PAM_SYSTEM_ERR;
429*5533Ssdussud 		}
4300Sstevel@tonic-gate 		goto out;
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	result = PAM_IGNORE;
4340Sstevel@tonic-gate out:
4350Sstevel@tonic-gate 	if (repository_name)
4360Sstevel@tonic-gate 		free(repository_name);
4370Sstevel@tonic-gate 	if (repository_pass)
4380Sstevel@tonic-gate 		free(repository_pass);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	free(scratch);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	return (result);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate int
4480Sstevel@tonic-gate pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	int	i;
4510Sstevel@tonic-gate 	int	debug = 0;
4520Sstevel@tonic-gate 	int	result;
4530Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
4560Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
4570Sstevel@tonic-gate 			debug = 1;
4580Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
4590Sstevel@tonic-gate 			flags |= PAM_SILENT;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	result = establish_key(pamh, flags, CODEPATH_PAM_SM_AUTHENTICATE, debug,
4634405Sjjj 	    netname);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	return (result);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4684405Sjjj 
4694405Sjjj typedef struct argres {
4704405Sjjj 	uid_t uid;
4714405Sjjj 	int result;
4724405Sjjj } argres_t;
4734405Sjjj 
4744405Sjjj /*
4754405Sjjj  * Revoke NFS DES credentials.
4764405Sjjj  * NFS may not be installed so we need to deal with SIGSYS
4774405Sjjj  * when we call _nfssys(); we thus call _nfssys() in a seperate thread that
4784405Sjjj  * is created specifically for this call. The thread specific signalmask
4794405Sjjj  * is set to ignore SIGSYS. After the call to _nfssys(), the thread
4804405Sjjj  * ceases to exist.
4814405Sjjj  */
4824405Sjjj static void *
4834405Sjjj revoke_nfs_cred(void *ap)
4844405Sjjj {
4854405Sjjj 	struct nfs_revauth_args nra;
4864405Sjjj 	sigset_t isigset;
4874405Sjjj 	argres_t *argres = (argres_t *)ap;
4884405Sjjj 
4894405Sjjj 	nra.authtype = AUTH_DES;
4904405Sjjj 	nra.uid = argres->uid;
4914405Sjjj 
4924405Sjjj 	(void) sigemptyset(&isigset);
4934405Sjjj 	(void) sigaddset(&isigset, SIGSYS);
4944405Sjjj 
4954405Sjjj 	if (pthread_sigmask(SIG_BLOCK, &isigset, NULL) == 0) {
4964405Sjjj 		argres->result = _nfssys(NFS_REVAUTH, &nra);
4974405Sjjj 		if (argres->result < 0 && errno == ENOSYS) {
4984405Sjjj 			argres->result = 0;
4994405Sjjj 		}
5004405Sjjj 	} else {
5014405Sjjj 		argres->result = -1;
5024405Sjjj 	}
5034405Sjjj 	return (NULL);
5044405Sjjj }
5054405Sjjj 
5064405Sjjj static int
5070Sstevel@tonic-gate remove_key(pam_handle_t *pamh, int flags, int debug)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	int result;
5100Sstevel@tonic-gate 	char *uname;
5110Sstevel@tonic-gate 	attrlist attr_pw[2];
5120Sstevel@tonic-gate 	struct pam_repository *auth_rep = NULL;
5130Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
5140Sstevel@tonic-gate 	uid_t uid;
5150Sstevel@tonic-gate 	gid_t gid;
5164405Sjjj 	argres_t argres;
5174405Sjjj 	thread_t tid;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&uname);
5200Sstevel@tonic-gate 	if (uname == NULL || *uname == NULL) {
5210Sstevel@tonic-gate 		if (debug)
5220Sstevel@tonic-gate 			syslog(LOG_DEBUG,
5230Sstevel@tonic-gate 			    "pam_dhkeys: user NULL or empty in remove_key()");
5240Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (strcmp(uname, "root") == 0) {
5280Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5290Sstevel@tonic-gate 			char msg[3][PAM_MAX_MSG_SIZE];
5300Sstevel@tonic-gate 			(void) snprintf(msg[0], sizeof (msg[0]),
5310Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
5324405Sjjj 			    "removing root credentials would"
5334405Sjjj 			    " break the rpc services that"));
5340Sstevel@tonic-gate 			(void) snprintf(msg[1], sizeof (msg[1]),
5350Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
5364405Sjjj 			    "use secure rpc on this host!"));
5370Sstevel@tonic-gate 			(void) snprintf(msg[2], sizeof (msg[2]),
5380Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
5394405Sjjj 			    "root may use keylogout -f to do"
5404405Sjjj 			    " this (at your own risk)!"));
5410Sstevel@tonic-gate 			(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 3,
5420Sstevel@tonic-gate 			    msg, NULL);
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
5480Sstevel@tonic-gate 	if (auth_rep != NULL) {
5490Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
5500Sstevel@tonic-gate 			return (PAM_BUF_ERR);
5510Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
5520Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
5530Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
5540Sstevel@tonic-gate 	} else
5550Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/* Retrieve user's uid/gid from the password repository */
5580Sstevel@tonic-gate 	attr_pw[0].type = ATTR_UID; attr_pw[0].next = &attr_pw[1];
5590Sstevel@tonic-gate 	attr_pw[1].type = ATTR_GID; attr_pw[1].next = NULL;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	result = __get_authtoken_attr(uname, pwu_rep, attr_pw);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
5640Sstevel@tonic-gate 		free(pwu_rep);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND)
5670Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
5680Sstevel@tonic-gate 	if (result == PWU_DENIED)
5690Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
5700Sstevel@tonic-gate 	if (result != PWU_SUCCESS)
5710Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	uid = (uid_t)attr_pw[0].data.val_i;
5740Sstevel@tonic-gate 	gid = (gid_t)attr_pw[1].data.val_i;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	(void) key_removesecret_g_uid(uid, gid);
5770Sstevel@tonic-gate 
5784405Sjjj 	argres.uid = uid;
5794405Sjjj 	argres.result = -1;
5800Sstevel@tonic-gate 
5814405Sjjj 	if (pthread_create(&tid, NULL, revoke_nfs_cred, (void *)&argres) == 0)
5824405Sjjj 		(void) pthread_join(tid, NULL);
5834405Sjjj 
5844405Sjjj 	if (argres.result < 0) {
5850Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5860Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
5874405Sjjj 			    "Warning: NFS credentials not destroyed"));
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 		return (PAM_AUTH_ERR);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	return (PAM_IGNORE);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate int
5960Sstevel@tonic-gate pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	int	i;
5990Sstevel@tonic-gate 	int	debug = 0;
6000Sstevel@tonic-gate 	int	result;
6010Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
6040Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
6050Sstevel@tonic-gate 			debug = 1;
6060Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
6070Sstevel@tonic-gate 			flags |= PAM_SILENT;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/* Check for invalid flags */
6110Sstevel@tonic-gate 	if (flags && (flags & PAM_ESTABLISH_CRED) == 0 &&
6124405Sjjj 	    (flags & PAM_REINITIALIZE_CRED) == 0 &&
6134405Sjjj 	    (flags & PAM_REFRESH_CRED) == 0 &&
6144405Sjjj 	    (flags & PAM_DELETE_CRED) == 0 &&
6154405Sjjj 	    (flags & PAM_SILENT) == 0) {
6160Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_dhkeys: pam_setcred: illegal flags %d",
6174405Sjjj 		    flags);
6180Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if ((flags & PAM_REINITIALIZE_CRED) || (flags & PAM_REFRESH_CRED)) {
6230Sstevel@tonic-gate 		/* doesn't apply to UNIX */
6240Sstevel@tonic-gate 		if (debug)
6250Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: cred reinit/refresh "
6260Sstevel@tonic-gate 			    "ignored\n");
6270Sstevel@tonic-gate 		return (PAM_IGNORE);
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (flags & PAM_DELETE_CRED) {
6310Sstevel@tonic-gate 		if (debug)
6320Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: removing creds\n");
6330Sstevel@tonic-gate 		result = remove_key(pamh, flags, debug);
6340Sstevel@tonic-gate 	} else {
6350Sstevel@tonic-gate 		result = establish_key(pamh, flags, CODEPATH_PAM_SM_SETCRED,
6364405Sjjj 		    debug, netname);
6370Sstevel@tonic-gate 		/* Some diagnostics */
6380Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
6390Sstevel@tonic-gate 			if (result == PAM_AUTH_ERR)
6400Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
6410Sstevel@tonic-gate 				    "Password does not decrypt any secret "
6420Sstevel@tonic-gate 				    "keys for %s."), netname);
6430Sstevel@tonic-gate 			else if (result == PAM_SYSTEM_ERR && netname[0])
6440Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
6450Sstevel@tonic-gate 				    "Could not set secret key(s) for %s. "
6460Sstevel@tonic-gate 				    "The key server may be down."), netname);
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		/* Not having credentials set is not an error... */
6500Sstevel@tonic-gate 		result = PAM_IGNORE;
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	return (result);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*ARGSUSED*/
6570Sstevel@tonic-gate void
6580Sstevel@tonic-gate rpc_cleanup(pam_handle_t *pamh, void *data, int pam_status)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate 	if (data) {
6610Sstevel@tonic-gate 		(void) memset(data, 0, strlen(data));
6620Sstevel@tonic-gate 		free(data);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate int
6670Sstevel@tonic-gate pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	int i;
6700Sstevel@tonic-gate 	int debug = 0;
6710Sstevel@tonic-gate 	int res;
6720Sstevel@tonic-gate 	pam_repository_t *pam_rep;
6730Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
6740Sstevel@tonic-gate 	char *oldpw;
6750Sstevel@tonic-gate 	char *user;
6760Sstevel@tonic-gate 	int tries;
6770Sstevel@tonic-gate 	int oldpw_ok;
6780Sstevel@tonic-gate 	char *oldrpcpw;
6790Sstevel@tonic-gate 	char *oldrpcpass;
6800Sstevel@tonic-gate 	char *data;
6810Sstevel@tonic-gate 	/* password truncated at 8 chars, see comment at establish_key() */
6820Sstevel@tonic-gate 	char short_pass[sizeof (des_block)+1], *short_passp;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	for (i = 0; i < argc; i++)
6850Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
6860Sstevel@tonic-gate 			debug = 1;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (debug)
6890Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: entered pam_sm_chauthtok()");
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if ((flags & PAM_PRELIM_CHECK) == 0)
6920Sstevel@tonic-gate 		return (PAM_IGNORE);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	/*
6950Sstevel@tonic-gate 	 * See if the old secure-rpc password has already been set
6960Sstevel@tonic-gate 	 */
6970Sstevel@tonic-gate 	res = pam_get_data(pamh, SUNW_OLDRPCPASS, (const void **)&oldrpcpass);
6980Sstevel@tonic-gate 	if (res == PAM_SUCCESS) {
6990Sstevel@tonic-gate 		if (debug)
7000Sstevel@tonic-gate 			syslog(LOG_DEBUG,
7010Sstevel@tonic-gate 			    "pam_dhkeys: OLDRPCPASS already set");
7020Sstevel@tonic-gate 		return (PAM_IGNORE);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&pam_rep);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&oldpw);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
7120Sstevel@tonic-gate 		if (debug)
7130Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
7140Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/* oldpw can be NULL (eg. root changing someone's passwd) */
7180Sstevel@tonic-gate 	if (oldpw) {
7190Sstevel@tonic-gate 		(void) strlcpy(short_pass, oldpw, sizeof (short_pass));
7200Sstevel@tonic-gate 		short_passp = short_pass;
7210Sstevel@tonic-gate 	} else
7220Sstevel@tonic-gate 		short_passp = NULL;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/*
7250Sstevel@tonic-gate 	 * For NIS+ we need to check whether the old password equals
7260Sstevel@tonic-gate 	 * the RPC password. If it doesn't, we won't be able to update
7270Sstevel@tonic-gate 	 * the secure RPC credentials later on in the process.
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (pam_rep == NULL)
7310Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
7320Sstevel@tonic-gate 	else {
7330Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
7340Sstevel@tonic-gate 			return (PAM_BUF_ERR);
7350Sstevel@tonic-gate 		pwu_rep->type = pam_rep->type;
7360Sstevel@tonic-gate 		pwu_rep->scope = pam_rep->scope;
7370Sstevel@tonic-gate 		pwu_rep->scope_len = pam_rep->scope_len;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	switch (__verify_rpc_passwd(user, short_passp, pwu_rep)) {
7410Sstevel@tonic-gate 	case PWU_SUCCESS:
7420Sstevel@tonic-gate 		/* oldpw matches RPC password, or no RPC password needed */
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7450Sstevel@tonic-gate 			free(pwu_rep);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		if (short_passp) {
7480Sstevel@tonic-gate 			if ((data = strdup(short_pass)) == NULL) {
7490Sstevel@tonic-gate 				(void) memset(short_pass, '\0',
7500Sstevel@tonic-gate 				    sizeof (short_pass));
7510Sstevel@tonic-gate 				return (PAM_BUF_ERR);
7520Sstevel@tonic-gate 			}
7530Sstevel@tonic-gate 		} else
7540Sstevel@tonic-gate 			data = NULL;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 		(void) pam_set_data(pamh, SUNW_OLDRPCPASS, data, rpc_cleanup);
7570Sstevel@tonic-gate 		return (PAM_IGNORE);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	case PWU_NOT_FOUND:
7600Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7610Sstevel@tonic-gate 			free(pwu_rep);
7620Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7630Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
7643641Ssemery 	case PWU_BAD_CREDPASS:
7650Sstevel@tonic-gate 		/* The old password does not decrypt any credentials */
7660Sstevel@tonic-gate 		break;
7673641Ssemery 	case PWU_CRED_ERROR:
7683641Ssemery 		/*
7693641Ssemery 		 * Indicates that the user's credentials could not be
7703641Ssemery 		 * retrieved or removed.  This could occur when a NIS+
7713641Ssemery 		 * user is in transition to another account authority.
7723641Ssemery 		 */
7733641Ssemery 		if (pwu_rep != PWU_DEFAULT_REP)
7743641Ssemery 			free(pwu_rep);
7753641Ssemery 		(void) memset(short_pass, '\0', sizeof (short_pass));
7763641Ssemery 		return (PAM_AUTHTOK_ERR);
7770Sstevel@tonic-gate 	default:
7780Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
7790Sstevel@tonic-gate 			free(pwu_rep);
7800Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
7810Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * We got here because the OLDAUTHTOK doesn't match the Secure RPC
7860Sstevel@tonic-gate 	 * password. In compliance with the old behavior, we give the
7870Sstevel@tonic-gate 	 * user two chances to get the password right. If that succeeds
7880Sstevel@tonic-gate 	 * all is well; if it doesn't, we'll return an error.
7890Sstevel@tonic-gate 	 */
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	(void) msg(pamh, dgettext(TEXT_DOMAIN,
7924405Sjjj 	    "This password differs from your secure RPC password."));
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	tries = 0;
7950Sstevel@tonic-gate 	oldpw_ok = 0;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	while (oldpw_ok == 0 && ++tries < 3) {
7980Sstevel@tonic-gate 		if (tries > 1)
7990Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
8004405Sjjj 			    "This password does not decrypt your "
8014405Sjjj 			    "secure RPC password."));
8020Sstevel@tonic-gate 		res = __pam_get_authtok(pamh, PAM_PROMPT, 0,
8030Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN,
8040Sstevel@tonic-gate 		    "Please enter your old Secure RPC password: "), &oldpw);
8050Sstevel@tonic-gate 		if (res != PAM_SUCCESS) {
8060Sstevel@tonic-gate 			if (pwu_rep != PWU_DEFAULT_REP)
8070Sstevel@tonic-gate 				free(pwu_rep);
8080Sstevel@tonic-gate 			return (res);
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 		(void) strlcpy(short_pass, oldpw, sizeof (short_pass));
8110Sstevel@tonic-gate 		(void) memset(oldpw, 0, strlen(oldpw));
8120Sstevel@tonic-gate 		free(oldpw);
8130Sstevel@tonic-gate 		oldpw = NULL;
8140Sstevel@tonic-gate 		if (__verify_rpc_passwd(user, short_pass, pwu_rep) ==
8150Sstevel@tonic-gate 		    PWU_SUCCESS)
8160Sstevel@tonic-gate 			oldpw_ok = 1;
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
8200Sstevel@tonic-gate 		free(pwu_rep);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (oldpw_ok == 0) {
8230Sstevel@tonic-gate 		(void) memset(short_pass, '\0', sizeof (short_pass));
8240Sstevel@tonic-gate 		return (PAM_AUTHTOK_ERR);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * Since the PAM framework only provides space for two different
8290Sstevel@tonic-gate 	 * password (one old and one current), there is officially no
8300Sstevel@tonic-gate 	 * place to put additional passwords (like our old rpc password).
8310Sstevel@tonic-gate 	 * We have no choice but to stuff it in a data item, and hope it
8320Sstevel@tonic-gate 	 * will be picked up by the password-update routines.
8330Sstevel@tonic-gate 	 */
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	oldrpcpw = strdup(short_pass);
8360Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	if (oldrpcpw == NULL)
8390Sstevel@tonic-gate 		return (PAM_BUF_ERR);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	res = pam_set_data(pamh, SUNW_OLDRPCPASS, oldrpcpw, rpc_cleanup);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	return (res);
8440Sstevel@tonic-gate }
845