xref: /onnv-gate/usr/src/lib/pam_modules/dhkeys/dhkeys.c (revision 11268:fc12274ea2f9)
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 /*
2211262SRajagopal.Andra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <syslog.h>
280Sstevel@tonic-gate #include <errno.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <rpc/rpc.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/wait.h>
360Sstevel@tonic-gate #include <limits.h>
374405Sjjj #include <signal.h>
384405Sjjj #include <pthread.h>
394405Sjjj #include <synch.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <rpcsvc/nis.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"
637422SJohn.Sonnenschein@Sun.COM #include <shadow.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate extern	int	_nfssys(int, void *);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * int msg(pamh, ...)
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * display message to the user
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate /*PRINTFLIKE2*/
734405Sjjj static int
msg(pam_handle_t * pamh,char * fmt,...)740Sstevel@tonic-gate msg(pam_handle_t *pamh, char *fmt, ...)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	va_list	ap;
770Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	va_start(ap, fmt);
800Sstevel@tonic-gate 	(void) vsnprintf(messages[0], sizeof (messages[0]), fmt, ap);
810Sstevel@tonic-gate 	va_end(ap);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	return (__pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL));
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Get the secret key for the given netname, key length, and algorithm
890Sstevel@tonic-gate  * type and send it to keyserv if the given pw decrypts it.  Update the
900Sstevel@tonic-gate  * following counter args as necessary: get_seckey_cnt, good_pw_cnt, and
910Sstevel@tonic-gate  * set_seckey_cnt.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * Returns 0 on malloc failure, else 1.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate static int
get_and_set_seckey(pam_handle_t * pamh,const char * netname,keylen_t keylen,algtype_t algtype,const char * pw,uid_t uid,gid_t gid,int * get_seckey_cnt,int * good_pw_cnt,int * set_seckey_cnt,int flags,int debug)960Sstevel@tonic-gate get_and_set_seckey(
970Sstevel@tonic-gate 	pam_handle_t	*pamh,			/* in */
980Sstevel@tonic-gate 	const char	*netname,		/* in */
990Sstevel@tonic-gate 	keylen_t	keylen,			/* in */
1000Sstevel@tonic-gate 	algtype_t	algtype,		/* in */
1010Sstevel@tonic-gate 	const char	*pw,			/* in */
1020Sstevel@tonic-gate 	uid_t		uid,			/* in */
1030Sstevel@tonic-gate 	gid_t		gid,			/* in */
1040Sstevel@tonic-gate 	int		*get_seckey_cnt,	/* out */
1050Sstevel@tonic-gate 	int		*good_pw_cnt,		/* out */
1060Sstevel@tonic-gate 	int		*set_seckey_cnt,	/* out */
1070Sstevel@tonic-gate 	int		flags,			/* in */
1080Sstevel@tonic-gate 	int		debug)			/* in */
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	char	*skey;
1110Sstevel@tonic-gate 	int	skeylen;
1120Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	skeylen = BITS2NIBBLES(keylen) + 1;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if ((skey = malloc(skeylen)) == NULL) {
1170Sstevel@tonic-gate 		return (0);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (getsecretkey_g(netname, keylen, algtype, skey, skeylen, pw)) {
1210Sstevel@tonic-gate 		(*get_seckey_cnt)++;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		if (skey[0]) {
1240Sstevel@tonic-gate 			/* password does decrypt secret key */
1250Sstevel@tonic-gate 			(*good_pw_cnt)++;
1260Sstevel@tonic-gate 			if (key_setnet_g_uid(netname, skey, keylen, NULL, 0,
1274405Sjjj 			    algtype, uid, gid) >= 0) {
1280Sstevel@tonic-gate 				(*set_seckey_cnt)++;
1290Sstevel@tonic-gate 			} else {
1300Sstevel@tonic-gate 				if (debug)
1310Sstevel@tonic-gate 					syslog(LOG_DEBUG, "pam_dhkeys: "
1324405Sjjj 					    "get_and_set_seckey: could not "
1334405Sjjj 					    "set secret key for keytype "
1344405Sjjj 					    "%d-%d", keylen, algtype);
1350Sstevel@tonic-gate 			}
1360Sstevel@tonic-gate 		} else {
1370Sstevel@tonic-gate 			if (pamh && !(flags & PAM_SILENT)) {
1380Sstevel@tonic-gate 				(void) snprintf(messages[0],
1390Sstevel@tonic-gate 				    sizeof (messages[0]),
1400Sstevel@tonic-gate 				    dgettext(TEXT_DOMAIN,
1414405Sjjj 				    "Password does not "
1424405Sjjj 				    "decrypt secret key (type = %d-%d) "
1434405Sjjj 				    "for '%s'."), keylen, algtype, netname);
1440Sstevel@tonic-gate 				(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
1454405Sjjj 				    messages, NULL);
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 	} else {
1490Sstevel@tonic-gate 		if (debug)
1500Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: get_and_set_seckey: "
1514405Sjjj 			    "could not get secret key for keytype %d-%d",
1524405Sjjj 			    keylen, algtype);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	free(skey);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	return (1);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * int establish_key(pamh, flags, debug, netname)
1620Sstevel@tonic-gate  *
1635945Ssdussud  * This routine establishes the Secure RPC Credentials for the
1640Sstevel@tonic-gate  * user specified in PAM_USER, using the password in PAM_AUTHTOK.
1650Sstevel@tonic-gate  *
1665945Ssdussud  * Establishing RPC credentials is considered a "helper" function for the PAM
1675945Ssdussud  * stack so we should only return failures or PAM_IGNORE. Returning PAM_SUCCESS
1685945Ssdussud  * may short circuit the stack and circumvent later critical checks.
1695945Ssdussud  *
17011262SRajagopal.Andra@Sun.COM  * we are called from pam_sm_setcred:
17111262SRajagopal.Andra@Sun.COM  *	1. if we are root (uid == 0), we do nothing and return
17211262SRajagopal.Andra@Sun.COM  *	   PAM_IGNORE.
17311262SRajagopal.Andra@Sun.COM  *	2. else, we try to establish credentials.
1740Sstevel@tonic-gate  *
17511262SRajagopal.Andra@Sun.COM  * We return framework errors as appropriate such as PAM_USER_UNKNOWN,
17611262SRajagopal.Andra@Sun.COM  * PAM_BUF_ERR, PAM_PERM_DENIED.
1775945Ssdussud  *
17811262SRajagopal.Andra@Sun.COM  * If we succeed in establishing credentials we return PAM_IGNORE.
1790Sstevel@tonic-gate  *
18011262SRajagopal.Andra@Sun.COM  * If we fail to establish credentials then we return:
18111262SRajagopal.Andra@Sun.COM  *    - PAM_SERVICE_ERR (credentials needed) or PAM_SYSTEM_ERR
18211262SRajagopal.Andra@Sun.COM  *      (credentials not needed) if netname could not be created;
18311262SRajagopal.Andra@Sun.COM  *    - PAM_AUTH_ERR (credentials needed) or PAM_IGNORE (credentials
18411262SRajagopal.Andra@Sun.COM  *      not needed) if no credentials were retrieved;
18511262SRajagopal.Andra@Sun.COM  *    - PAM_AUTH_ERR if the password didn't decrypt the cred;
18611262SRajagopal.Andra@Sun.COM  *    - PAM_SYSTEM_ERR if the cred's could not be stored.
1870Sstevel@tonic-gate  *
1880Sstevel@tonic-gate  * This routine returns the user's netname in "netname".
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  * All tools--but the PAM stack--currently use getpass() to obtain
1910Sstevel@tonic-gate  * the user's secure RPC password. We must make sure we don't use more than
1920Sstevel@tonic-gate  * the first des_block (eight) characters of whatever is handed down to us.
1930Sstevel@tonic-gate  * Therefore, we use a local variable "short_pass" to hold those 8 char's.
1940Sstevel@tonic-gate  */
1954405Sjjj static int
establish_key(pam_handle_t * pamh,int flags,int debug,char * netname)19611262SRajagopal.Andra@Sun.COM establish_key(pam_handle_t *pamh, int flags, int debug, char *netname)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	char	*user;
1990Sstevel@tonic-gate 	char	*passwd;
2000Sstevel@tonic-gate 	char	short_pass[sizeof (des_block)+1], *short_passp;
2010Sstevel@tonic-gate 	int	result;
2020Sstevel@tonic-gate 	uid_t	uid;
2030Sstevel@tonic-gate 	gid_t	gid;
2040Sstevel@tonic-gate 	int	err;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	struct passwd pw;	/* Needed to obtain uid */
2070Sstevel@tonic-gate 	char	*scratch;
2080Sstevel@tonic-gate 	int	scratchlen;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	mechanism_t	**mechs;
2110Sstevel@tonic-gate 	mechanism_t	**mpp;
2120Sstevel@tonic-gate 	int	get_seckey_cnt = 0;
2130Sstevel@tonic-gate 	int	set_seckey_cnt = 0;
2140Sstevel@tonic-gate 	int	good_pw_cnt = 0;
2150Sstevel@tonic-gate 	int	valid_mech_cnt = 0;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&user);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (user == NULL || *user == '\0') {
2200Sstevel@tonic-gate 		if (debug)
2210Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user NULL or empty");
2220Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	scratchlen = sysconf(_SC_GETPW_R_SIZE_MAX);
2280Sstevel@tonic-gate 	if ((scratch = malloc(scratchlen)) == NULL)
2290Sstevel@tonic-gate 		return (PAM_BUF_ERR);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (getpwnam_r(user, &pw, scratch, scratchlen) == NULL) {
2320Sstevel@tonic-gate 		result = PAM_USER_UNKNOWN;
2330Sstevel@tonic-gate 		goto out;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	uid = pw.pw_uid;
2370Sstevel@tonic-gate 	gid = pw.pw_gid;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * We don't set credentials when root logs in.
2410Sstevel@tonic-gate 	 */
24211262SRajagopal.Andra@Sun.COM 	if (uid == 0) {
2430Sstevel@tonic-gate 		result = PAM_IGNORE;
2440Sstevel@tonic-gate 		goto out;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
24711262SRajagopal.Andra@Sun.COM 	err = user2netname(netname, uid, NULL);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if (err != 1) {
2500Sstevel@tonic-gate 		if (debug)
2510Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: user2netname failed");
2520Sstevel@tonic-gate 			result = PAM_SYSTEM_ERR;
2530Sstevel@tonic-gate 		goto out;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/* passwd can be NULL (no passwd or su as root) */
2570Sstevel@tonic-gate 	if (passwd) {
2580Sstevel@tonic-gate 		(void) strlcpy(short_pass, passwd, sizeof (short_pass));
2590Sstevel@tonic-gate 		short_passp = short_pass;
2600Sstevel@tonic-gate 	} else
2610Sstevel@tonic-gate 		short_passp = NULL;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (mechs = __nis_get_mechanisms(FALSE)) {
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		for (mpp = mechs; *mpp; mpp++) {
2660Sstevel@tonic-gate 			mechanism_t *mp = *mpp;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 			if (AUTH_DES_COMPAT_CHK(mp))
2690Sstevel@tonic-gate 				break;	/* fall through to AUTH_DES below */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 			if (!VALID_MECH_ENTRY(mp))
2720Sstevel@tonic-gate 				continue;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 			if (debug)
2750Sstevel@tonic-gate 				syslog(LOG_DEBUG, "pam_dhkeys: trying "
2764405Sjjj 				    "key type = %d-%d", mp->keylen,
2774405Sjjj 				    mp->algtype);
2780Sstevel@tonic-gate 			valid_mech_cnt++;
2790Sstevel@tonic-gate 			if (!get_and_set_seckey(pamh, netname, mp->keylen,
2804405Sjjj 			    mp->algtype, short_passp, uid, gid,
2814405Sjjj 			    &get_seckey_cnt, &good_pw_cnt, &set_seckey_cnt,
28211262SRajagopal.Andra@Sun.COM 			    flags, debug)) {
2830Sstevel@tonic-gate 				result = PAM_BUF_ERR;
2840Sstevel@tonic-gate 				goto out;
2850Sstevel@tonic-gate 			}
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 		__nis_release_mechanisms(mechs);
2880Sstevel@tonic-gate 		/* fall through to AUTH_DES below */
2890Sstevel@tonic-gate 	} else {
2900Sstevel@tonic-gate 		/*
29111262SRajagopal.Andra@Sun.COM 		 * No usable mechs found in security congifuration file thus
2920Sstevel@tonic-gate 		 * fallback to AUTH_DES compat.
2930Sstevel@tonic-gate 		 */
2940Sstevel@tonic-gate 		if (debug)
2950Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: no valid mechs "
2964405Sjjj 			    "found. Trying AUTH_DES.");
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/*
30011262SRajagopal.Andra@Sun.COM 	 * We always perform AUTH_DES for the benefit of services like NFS
30111262SRajagopal.Andra@Sun.COM 	 * that may depend on the classic des 192bit key being set.
3020Sstevel@tonic-gate 	 */
3030Sstevel@tonic-gate 	if (!get_and_set_seckey(pamh, netname, AUTH_DES_KEYLEN,
3040Sstevel@tonic-gate 	    AUTH_DES_ALGTYPE, short_passp, uid, gid, &get_seckey_cnt,
30511262SRajagopal.Andra@Sun.COM 	    &good_pw_cnt, &set_seckey_cnt, flags, debug)) {
3060Sstevel@tonic-gate 		result = PAM_BUF_ERR;
3070Sstevel@tonic-gate 		goto out;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (debug) {
3110Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: mech key totals:\n");
3120Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d valid mechanism(s)",
3134405Sjjj 		    valid_mech_cnt);
3140Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) retrieved",
3154405Sjjj 		    get_seckey_cnt);
3160Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d passwd decrypt successes",
3174405Sjjj 		    good_pw_cnt);
3180Sstevel@tonic-gate 		syslog(LOG_DEBUG, "pam_dhkeys: %d secret key(s) set",
3194405Sjjj 		    set_seckey_cnt);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (get_seckey_cnt == 0) {		/* No credentials */
32311262SRajagopal.Andra@Sun.COM 		result = PAM_IGNORE;
3240Sstevel@tonic-gate 		goto out;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (good_pw_cnt == 0) {			/* wrong password */
3285945Ssdussud 		result = PAM_AUTH_ERR;
3290Sstevel@tonic-gate 		goto out;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (set_seckey_cnt == 0) {
3335945Ssdussud 		result = PAM_SYSTEM_ERR;
3340Sstevel@tonic-gate 		goto out;
3350Sstevel@tonic-gate 	}
33611262SRajagopal.Andra@Sun.COM 	/* Credentials have been successfully established, return PAM_IGNORE */
3370Sstevel@tonic-gate 	result = PAM_IGNORE;
3380Sstevel@tonic-gate out:
3395945Ssdussud 	/*
3405945Ssdussud 	 * If we are authenticating we attempt to establish credentials
3415945Ssdussud 	 * where appropriate. Failure to do so is only an error if we
3425945Ssdussud 	 * definitely needed them. Thus always return PAM_IGNORE
3435945Ssdussud 	 * if we are authenticating and credentials were not needed.
3445945Ssdussud 	 */
3450Sstevel@tonic-gate 	free(scratch);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	(void) memset(short_pass, '\0', sizeof (short_pass));
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	return (result);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
35211262SRajagopal.Andra@Sun.COM /*ARGSUSED*/
3530Sstevel@tonic-gate int
pam_sm_authenticate(pam_handle_t * pamh,int flags,int argc,const char ** argv)3540Sstevel@tonic-gate pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
3550Sstevel@tonic-gate {
35611262SRajagopal.Andra@Sun.COM 	return (PAM_IGNORE);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3594405Sjjj 
3604405Sjjj typedef struct argres {
3614405Sjjj 	uid_t uid;
3624405Sjjj 	int result;
3634405Sjjj } argres_t;
3644405Sjjj 
3654405Sjjj /*
3664405Sjjj  * Revoke NFS DES credentials.
3674405Sjjj  * NFS may not be installed so we need to deal with SIGSYS
3684405Sjjj  * when we call _nfssys(); we thus call _nfssys() in a seperate thread that
3694405Sjjj  * is created specifically for this call. The thread specific signalmask
3704405Sjjj  * is set to ignore SIGSYS. After the call to _nfssys(), the thread
3714405Sjjj  * ceases to exist.
3724405Sjjj  */
3734405Sjjj static void *
revoke_nfs_cred(void * ap)3744405Sjjj revoke_nfs_cred(void *ap)
3754405Sjjj {
3764405Sjjj 	struct nfs_revauth_args nra;
3774405Sjjj 	sigset_t isigset;
3784405Sjjj 	argres_t *argres = (argres_t *)ap;
3794405Sjjj 
3804405Sjjj 	nra.authtype = AUTH_DES;
3814405Sjjj 	nra.uid = argres->uid;
3824405Sjjj 
3834405Sjjj 	(void) sigemptyset(&isigset);
3844405Sjjj 	(void) sigaddset(&isigset, SIGSYS);
3854405Sjjj 
3864405Sjjj 	if (pthread_sigmask(SIG_BLOCK, &isigset, NULL) == 0) {
3874405Sjjj 		argres->result = _nfssys(NFS_REVAUTH, &nra);
3884405Sjjj 		if (argres->result < 0 && errno == ENOSYS) {
3894405Sjjj 			argres->result = 0;
3904405Sjjj 		}
3914405Sjjj 	} else {
3924405Sjjj 		argres->result = -1;
3934405Sjjj 	}
3944405Sjjj 	return (NULL);
3954405Sjjj }
3964405Sjjj 
3974405Sjjj static int
remove_key(pam_handle_t * pamh,int flags,int debug)3980Sstevel@tonic-gate remove_key(pam_handle_t *pamh, int flags, int debug)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	int result;
4010Sstevel@tonic-gate 	char *uname;
4020Sstevel@tonic-gate 	attrlist attr_pw[2];
4030Sstevel@tonic-gate 	struct pam_repository *auth_rep = NULL;
4040Sstevel@tonic-gate 	pwu_repository_t *pwu_rep;
4050Sstevel@tonic-gate 	uid_t uid;
4060Sstevel@tonic-gate 	gid_t gid;
4074405Sjjj 	argres_t argres;
408*11268SRajagopal.Andra@Sun.COM 	pthread_t tid;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void **)&uname);
4110Sstevel@tonic-gate 	if (uname == NULL || *uname == NULL) {
4120Sstevel@tonic-gate 		if (debug)
4130Sstevel@tonic-gate 			syslog(LOG_DEBUG,
4140Sstevel@tonic-gate 			    "pam_dhkeys: user NULL or empty in remove_key()");
4150Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (strcmp(uname, "root") == 0) {
4190Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
4200Sstevel@tonic-gate 			char msg[3][PAM_MAX_MSG_SIZE];
4210Sstevel@tonic-gate 			(void) snprintf(msg[0], sizeof (msg[0]),
4220Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
4234405Sjjj 			    "removing root credentials would"
4244405Sjjj 			    " break the rpc services that"));
4250Sstevel@tonic-gate 			(void) snprintf(msg[1], sizeof (msg[1]),
4260Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
4274405Sjjj 			    "use secure rpc on this host!"));
4280Sstevel@tonic-gate 			(void) snprintf(msg[2], sizeof (msg[2]),
4290Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
4304405Sjjj 			    "root may use keylogout -f to do"
4314405Sjjj 			    " this (at your own risk)!"));
4320Sstevel@tonic-gate 			(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 3,
4330Sstevel@tonic-gate 			    msg, NULL);
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
4390Sstevel@tonic-gate 	if (auth_rep != NULL) {
4400Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
4410Sstevel@tonic-gate 			return (PAM_BUF_ERR);
4420Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
4430Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
4440Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
4450Sstevel@tonic-gate 	} else
4460Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/* Retrieve user's uid/gid from the password repository */
4490Sstevel@tonic-gate 	attr_pw[0].type = ATTR_UID; attr_pw[0].next = &attr_pw[1];
4500Sstevel@tonic-gate 	attr_pw[1].type = ATTR_GID; attr_pw[1].next = NULL;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	result = __get_authtoken_attr(uname, pwu_rep, attr_pw);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP)
4550Sstevel@tonic-gate 		free(pwu_rep);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND)
4580Sstevel@tonic-gate 		return (PAM_USER_UNKNOWN);
4590Sstevel@tonic-gate 	if (result == PWU_DENIED)
4600Sstevel@tonic-gate 		return (PAM_PERM_DENIED);
4610Sstevel@tonic-gate 	if (result != PWU_SUCCESS)
4620Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	uid = (uid_t)attr_pw[0].data.val_i;
4650Sstevel@tonic-gate 	gid = (gid_t)attr_pw[1].data.val_i;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	(void) key_removesecret_g_uid(uid, gid);
4680Sstevel@tonic-gate 
4694405Sjjj 	argres.uid = uid;
4704405Sjjj 	argres.result = -1;
4710Sstevel@tonic-gate 
4724405Sjjj 	if (pthread_create(&tid, NULL, revoke_nfs_cred, (void *)&argres) == 0)
4734405Sjjj 		(void) pthread_join(tid, NULL);
4744405Sjjj 
4754405Sjjj 	if (argres.result < 0) {
4760Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
4770Sstevel@tonic-gate 			(void) msg(pamh, dgettext(TEXT_DOMAIN,
4784405Sjjj 			    "Warning: NFS credentials not destroyed"));
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		return (PAM_AUTH_ERR);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	return (PAM_IGNORE);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate int
pam_sm_setcred(pam_handle_t * pamh,int flags,int argc,const char ** argv)4870Sstevel@tonic-gate pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	int	i;
4900Sstevel@tonic-gate 	int	debug = 0;
4910Sstevel@tonic-gate 	int	result;
4920Sstevel@tonic-gate 	char	netname[MAXNETNAMELEN + 1];
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
4950Sstevel@tonic-gate 		if (strcmp(argv[i], "debug") == 0)
4960Sstevel@tonic-gate 			debug = 1;
4970Sstevel@tonic-gate 		else if (strcmp(argv[i], "nowarn") == 0)
4980Sstevel@tonic-gate 			flags |= PAM_SILENT;
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/* Check for invalid flags */
5020Sstevel@tonic-gate 	if (flags && (flags & PAM_ESTABLISH_CRED) == 0 &&
5034405Sjjj 	    (flags & PAM_REINITIALIZE_CRED) == 0 &&
5044405Sjjj 	    (flags & PAM_REFRESH_CRED) == 0 &&
5054405Sjjj 	    (flags & PAM_DELETE_CRED) == 0 &&
5064405Sjjj 	    (flags & PAM_SILENT) == 0) {
5070Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_dhkeys: pam_setcred: illegal flags %d",
5084405Sjjj 		    flags);
5090Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if ((flags & PAM_REINITIALIZE_CRED) || (flags & PAM_REFRESH_CRED)) {
5140Sstevel@tonic-gate 		/* doesn't apply to UNIX */
5150Sstevel@tonic-gate 		if (debug)
5160Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: cred reinit/refresh "
5170Sstevel@tonic-gate 			    "ignored\n");
5180Sstevel@tonic-gate 		return (PAM_IGNORE);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (flags & PAM_DELETE_CRED) {
5220Sstevel@tonic-gate 		if (debug)
5230Sstevel@tonic-gate 			syslog(LOG_DEBUG, "pam_dhkeys: removing creds\n");
5240Sstevel@tonic-gate 		result = remove_key(pamh, flags, debug);
5250Sstevel@tonic-gate 	} else {
52611262SRajagopal.Andra@Sun.COM 		result = establish_key(pamh, flags, debug, netname);
5270Sstevel@tonic-gate 		/* Some diagnostics */
5280Sstevel@tonic-gate 		if ((flags & PAM_SILENT) == 0) {
5290Sstevel@tonic-gate 			if (result == PAM_AUTH_ERR)
5300Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
5310Sstevel@tonic-gate 				    "Password does not decrypt any secret "
5320Sstevel@tonic-gate 				    "keys for %s."), netname);
5330Sstevel@tonic-gate 			else if (result == PAM_SYSTEM_ERR && netname[0])
5340Sstevel@tonic-gate 				(void) msg(pamh, dgettext(TEXT_DOMAIN,
5350Sstevel@tonic-gate 				    "Could not set secret key(s) for %s. "
5360Sstevel@tonic-gate 				    "The key server may be down."), netname);
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		/* Not having credentials set is not an error... */
5400Sstevel@tonic-gate 		result = PAM_IGNORE;
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	return (result);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate /*ARGSUSED*/
5470Sstevel@tonic-gate void
rpc_cleanup(pam_handle_t * pamh,void * data,int pam_status)5480Sstevel@tonic-gate rpc_cleanup(pam_handle_t *pamh, void *data, int pam_status)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	if (data) {
5510Sstevel@tonic-gate 		(void) memset(data, 0, strlen(data));
5520Sstevel@tonic-gate 		free(data);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
55611262SRajagopal.Andra@Sun.COM /*ARGSUSED*/
5570Sstevel@tonic-gate int
pam_sm_chauthtok(pam_handle_t * pamh,int flags,int argc,const char ** argv)5580Sstevel@tonic-gate pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
5590Sstevel@tonic-gate {
56011262SRajagopal.Andra@Sun.COM 	return (PAM_IGNORE);
5610Sstevel@tonic-gate }
562