xref: /onnv-gate/usr/src/lib/pam_modules/unix_account/unix_acct.c (revision 11262:b7ebfbf2359e)
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
57422SJohn.Sonnenschein@Sun.COM  * Common Development and Distribution License (the "License").
67422SJohn.Sonnenschein@Sun.COM  * 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 /*
228563SKenjiro.Tsuji@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 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/wait.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <fcntl.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <security/pam_appl.h>
330Sstevel@tonic-gate #include <security/pam_modules.h>
340Sstevel@tonic-gate #include <security/pam_impl.h>
350Sstevel@tonic-gate #include <syslog.h>
360Sstevel@tonic-gate #include <pwd.h>
370Sstevel@tonic-gate #include <shadow.h>
380Sstevel@tonic-gate #include <lastlog.h>
390Sstevel@tonic-gate #include <ctype.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <libintl.h>
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include <thread.h>
460Sstevel@tonic-gate #include <synch.h>
470Sstevel@tonic-gate #include <errno.h>
480Sstevel@tonic-gate #include <time.h>
490Sstevel@tonic-gate #include <string.h>
500Sstevel@tonic-gate #include <crypt.h>
510Sstevel@tonic-gate #include <assert.h>
520Sstevel@tonic-gate #include <deflt.h>
530Sstevel@tonic-gate #include <libintl.h>
540Sstevel@tonic-gate #include <passwdutil.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #define	LASTLOG		"/var/adm/lastlog"
570Sstevel@tonic-gate #define	LOGINADMIN	"/etc/default/login"
580Sstevel@tonic-gate #define	UNIX_AUTH_DATA		"SUNW-UNIX-AUTH-DATA"
590Sstevel@tonic-gate #define	UNIX_AUTHTOK_DATA	"SUNW-UNIX-AUTHTOK-DATA"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Function Declarations
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate extern void		setusershell();
650Sstevel@tonic-gate extern int		_nfssys(int, void *);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate typedef struct _unix_authtok_data_ {
680Sstevel@tonic-gate 	int age_status;
690Sstevel@tonic-gate }unix_authtok_data;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*ARGSUSED*/
720Sstevel@tonic-gate static void
unix_cleanup(pam_handle_t * pamh,void * data,int pam_status)730Sstevel@tonic-gate unix_cleanup(
740Sstevel@tonic-gate 	pam_handle_t *pamh,
750Sstevel@tonic-gate 	void *data,
760Sstevel@tonic-gate 	int pam_status)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	free((unix_authtok_data *)data);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * check_for_login_inactivity	- Check for login inactivity
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static int
check_for_login_inactivity(uid_t pw_uid,struct spwd * shpwd)870Sstevel@tonic-gate check_for_login_inactivity(
880Sstevel@tonic-gate 	uid_t		pw_uid,
890Sstevel@tonic-gate 	struct 	spwd 	*shpwd)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	int		fdl;
920Sstevel@tonic-gate 	struct lastlog	ll;
930Sstevel@tonic-gate 	int		retval;
940Sstevel@tonic-gate 	offset_t	offset;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	offset = (offset_t)pw_uid * (offset_t)sizeof (struct lastlog);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if ((fdl = open(LASTLOG, O_RDWR|O_CREAT, 0444)) >= 0) {
990Sstevel@tonic-gate 		/*
1000Sstevel@tonic-gate 		 * Read the last login (ll) time
1010Sstevel@tonic-gate 		 */
1020Sstevel@tonic-gate 		if (llseek(fdl, offset, SEEK_SET) != offset) {
1038126SJoep.Vesseur@Sun.COM 			__pam_log(LOG_AUTH | LOG_ERR,
1048126SJoep.Vesseur@Sun.COM 			    "pam_unix_acct: pam_sm_acct_mgmt: "
1050Sstevel@tonic-gate 			    "can't obtain last login info on uid %d "
1060Sstevel@tonic-gate 			    "(uid too large)", pw_uid);
1079137SJoep.Vesseur@Sun.COM 			(void) close(fdl);
1080Sstevel@tonic-gate 			return (0);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		retval = read(fdl, (char *)&ll, sizeof (ll));
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 		/* Check for login inactivity */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		if ((shpwd->sp_inact > 0) && (retval == sizeof (ll)) &&
1160Sstevel@tonic-gate 		    ll.ll_time) {
1170Sstevel@tonic-gate 			/*
1180Sstevel@tonic-gate 			 * account inactive too long.
1190Sstevel@tonic-gate 			 * and no update password set
1200Sstevel@tonic-gate 			 * and no last pwd change date in shadow file
1210Sstevel@tonic-gate 			 * and last pwd change more than inactive time
1220Sstevel@tonic-gate 			 * then account inactive too long and no access.
1230Sstevel@tonic-gate 			 */
1240Sstevel@tonic-gate 			if (((time_t)((ll.ll_time / DAY) + shpwd->sp_inact)
1258126SJoep.Vesseur@Sun.COM 			    < DAY_NOW) &&
1260Sstevel@tonic-gate 			    (shpwd->sp_lstchg != 0) &&
1270Sstevel@tonic-gate 			    (shpwd->sp_lstchg != -1) &&
1280Sstevel@tonic-gate 			    ((shpwd->sp_lstchg + shpwd->sp_inact) < DAY_NOW)) {
1290Sstevel@tonic-gate 				/*
1300Sstevel@tonic-gate 				 * Account inactive for too long
1310Sstevel@tonic-gate 				 */
1320Sstevel@tonic-gate 				(void) close(fdl);
1330Sstevel@tonic-gate 				return (1);
1340Sstevel@tonic-gate 			}
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		(void) close(fdl);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	return (0);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * new_password_check()
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * check to see if the user needs to change their password
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static int
new_password_check(shpwd,flags)1498126SJoep.Vesseur@Sun.COM new_password_check(shpwd, flags)
1500Sstevel@tonic-gate 	struct 	spwd 	*shpwd;
1510Sstevel@tonic-gate 	int 		flags;
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	time_t	now  = DAY_NOW;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * We want to make sure that we change the password only if
1570Sstevel@tonic-gate 	 * passwords are required for the system, the user does not
1580Sstevel@tonic-gate 	 * have a password, AND the user's NULL password can be changed
1590Sstevel@tonic-gate 	 * according to its password aging information
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if ((flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
1630Sstevel@tonic-gate 		if (shpwd->sp_pwdp[0] == '\0') {
1648126SJoep.Vesseur@Sun.COM 			if (((shpwd->sp_max == -1) ||
1650Sstevel@tonic-gate 				((time_t)shpwd->sp_lstchg > now) ||
1660Sstevel@tonic-gate 				((now >= (time_t)(shpwd->sp_lstchg +
1670Sstevel@tonic-gate 							shpwd->sp_min)) &&
1680Sstevel@tonic-gate 				(shpwd->sp_max >= shpwd->sp_min)))) {
1690Sstevel@tonic-gate 					return (PAM_NEW_AUTHTOK_REQD);
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	return (PAM_SUCCESS);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate  * perform_passwd_aging_check
1780Sstevel@tonic-gate  *		- Check for password exipration.
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate static	int
perform_passwd_aging_check(pam_handle_t * pamh,struct spwd * shpwd,int flags)1810Sstevel@tonic-gate perform_passwd_aging_check(
1820Sstevel@tonic-gate 	pam_handle_t *pamh,
1830Sstevel@tonic-gate 	struct 	spwd 	*shpwd,
1840Sstevel@tonic-gate 	int	flags)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	time_t 	now = DAY_NOW;
1870Sstevel@tonic-gate 	int	idledays = -1;
1880Sstevel@tonic-gate 	char	*ptr;
1890Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
1908563SKenjiro.Tsuji@Sun.COM 	void	*defp;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 
1938563SKenjiro.Tsuji@Sun.COM 	if ((defp = defopen_r(LOGINADMIN)) != NULL) {
1948563SKenjiro.Tsuji@Sun.COM 		if ((ptr = defread_r("IDLEWEEKS=", defp)) != NULL)
1950Sstevel@tonic-gate 			idledays = 7 * atoi(ptr);
1968563SKenjiro.Tsuji@Sun.COM 		defclose_r(defp);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * if (sp_lstchg == 0), the administrator has forced the
2010Sstevel@tonic-gate 	 * user to change his/her passwd
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	if (shpwd->sp_lstchg == 0)
2040Sstevel@tonic-gate 		return (PAM_NEW_AUTHTOK_REQD);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/* If password aging is disabled (or min>max), all is well */
2070Sstevel@tonic-gate 	if (shpwd->sp_max < 0 || shpwd->sp_max < shpwd->sp_min)
2080Sstevel@tonic-gate 		return (PAM_SUCCESS);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* Password aging is enabled. See if the password has aged */
2110Sstevel@tonic-gate 	if (now < (time_t)(shpwd->sp_lstchg + shpwd->sp_max))
2120Sstevel@tonic-gate 		return (PAM_SUCCESS);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/* Password has aged. Has it aged more than idledays ? */
2150Sstevel@tonic-gate 	if (idledays < 0)			/* IDLEWEEKS not configured */
2160Sstevel@tonic-gate 		return (PAM_NEW_AUTHTOK_REQD);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/* idledays is configured */
2190Sstevel@tonic-gate 	if (idledays > 0 && (now < (time_t)(shpwd->sp_lstchg + idledays)))
2200Sstevel@tonic-gate 		return (PAM_NEW_AUTHTOK_REQD);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/* password has aged more that allowed for by IDLEWEEKS */
2230Sstevel@tonic-gate 	if (!(flags & PAM_SILENT)) {
2240Sstevel@tonic-gate 		(void) strlcpy(messages[0], dgettext(TEXT_DOMAIN,
2250Sstevel@tonic-gate 		    "Your password has been expired for too long."),
2260Sstevel@tonic-gate 		    sizeof (messages[0]));
2270Sstevel@tonic-gate 		(void) strlcpy(messages[1], dgettext(TEXT_DOMAIN,
2280Sstevel@tonic-gate 		    "Please contact the system administrator."),
2290Sstevel@tonic-gate 		    sizeof (messages[0]));
2300Sstevel@tonic-gate 		(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 2, messages,
2310Sstevel@tonic-gate 		    NULL);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	return (PAM_AUTHTOK_EXPIRED);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * warn_user_passwd_will_expire	- warn the user when the password will
2380Sstevel@tonic-gate  *					  expire.
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void
warn_user_passwd_will_expire(pam_handle_t * pamh,struct spwd shpwd)2420Sstevel@tonic-gate warn_user_passwd_will_expire(
2430Sstevel@tonic-gate 	pam_handle_t *pamh,
2440Sstevel@tonic-gate 	struct 	spwd shpwd)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	time_t 	now	= DAY_NOW;
2470Sstevel@tonic-gate 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
2480Sstevel@tonic-gate 	time_t	days;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if ((shpwd.sp_warn > 0) && (shpwd.sp_max > 0) &&
2520Sstevel@tonic-gate 	    (now + shpwd.sp_warn) >= (time_t)(shpwd.sp_lstchg + shpwd.sp_max)) {
2530Sstevel@tonic-gate 		days = (time_t)(shpwd.sp_lstchg + shpwd.sp_max) - now;
2540Sstevel@tonic-gate 		if (days <= 0)
2550Sstevel@tonic-gate 			(void) snprintf(messages[0],
2568126SJoep.Vesseur@Sun.COM 			    sizeof (messages[0]),
2578126SJoep.Vesseur@Sun.COM 			    dgettext(TEXT_DOMAIN,
2588126SJoep.Vesseur@Sun.COM 			    "Your password will expire within 24 hours."));
2590Sstevel@tonic-gate 		else if (days == 1)
2600Sstevel@tonic-gate 			(void) snprintf(messages[0],
2618126SJoep.Vesseur@Sun.COM 			    sizeof (messages[0]),
2628126SJoep.Vesseur@Sun.COM 			    dgettext(TEXT_DOMAIN,
2638126SJoep.Vesseur@Sun.COM 			    "Your password will expire in 1 day."));
2640Sstevel@tonic-gate 		else
2650Sstevel@tonic-gate 			(void) snprintf(messages[0],
2668126SJoep.Vesseur@Sun.COM 			    sizeof (messages[0]),
2678126SJoep.Vesseur@Sun.COM 			    dgettext(TEXT_DOMAIN,
2688126SJoep.Vesseur@Sun.COM 			    "Your password will expire in %d days."),
2698126SJoep.Vesseur@Sun.COM 			    (int)days);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages,
2720Sstevel@tonic-gate 		    NULL);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * pam_sm_acct_mgmt	- 	main account managment routine.
2780Sstevel@tonic-gate  *			  Returns: module error or specific error on failure
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate int
pam_sm_acct_mgmt(pam_handle_t * pamh,int flags,int argc,const char ** argv)2820Sstevel@tonic-gate pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	uid_t			pw_uid;
2850Sstevel@tonic-gate 	char			*repository_name = NULL;
2860Sstevel@tonic-gate 	char    		*user;
2870Sstevel@tonic-gate 	attrlist		attr_pw[3];
2880Sstevel@tonic-gate 	attrlist		attr_spw[7];
2890Sstevel@tonic-gate 	pwu_repository_t	*pwu_rep = PWU_DEFAULT_REP;
2900Sstevel@tonic-gate 	pwu_repository_t	*auth_rep = NULL;
2910Sstevel@tonic-gate 	int 			error = PAM_ACCT_EXPIRED;
2920Sstevel@tonic-gate 	int			result;
2930Sstevel@tonic-gate 	int			i;
2940Sstevel@tonic-gate 	int			debug = 0;
2950Sstevel@tonic-gate 	int			server_policy = 0;
2960Sstevel@tonic-gate 	unix_authtok_data	*status;
2970Sstevel@tonic-gate 	struct 	spwd		shpwd = {NULL, NULL,
2980Sstevel@tonic-gate 					-1, -1, -1, -1, -1, -1, 0};
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
3010Sstevel@tonic-gate 		if (strcasecmp(argv[i], "debug") == 0)
3020Sstevel@tonic-gate 			debug = 1;
3030Sstevel@tonic-gate 		else if (strcasecmp(argv[i], "server_policy") == 0)
3040Sstevel@tonic-gate 			server_policy = 1;
3050Sstevel@tonic-gate 		else if (strcasecmp(argv[i], "nowarn") == 0) {
3060Sstevel@tonic-gate 			flags = flags | PAM_SILENT;
3070Sstevel@tonic-gate 		} else {
3088126SJoep.Vesseur@Sun.COM 			__pam_log(LOG_AUTH | LOG_ERR,
3098126SJoep.Vesseur@Sun.COM 			    "ACCOUNT:pam_sm_acct_mgmt: illegal option %s",
3108126SJoep.Vesseur@Sun.COM 			    argv[i]);
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (debug)
3158126SJoep.Vesseur@Sun.COM 		__pam_log(LOG_AUTH | LOG_DEBUG,
3160Sstevel@tonic-gate 		    "pam_unix_account: entering pam_sm_acct_mgmt()");
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if ((error = pam_get_item(pamh, PAM_USER, (void **)&user))
3198126SJoep.Vesseur@Sun.COM 	    != PAM_SUCCESS)
3200Sstevel@tonic-gate 		goto out;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (user == NULL) {
3230Sstevel@tonic-gate 		error = PAM_USER_UNKNOWN;
3240Sstevel@tonic-gate 		goto out;
3250Sstevel@tonic-gate 	} else
3260Sstevel@tonic-gate 		shpwd.sp_namp = user;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if ((error = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep))
3298126SJoep.Vesseur@Sun.COM 	    != PAM_SUCCESS)
3300Sstevel@tonic-gate 		goto out;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (auth_rep == NULL) {
3330Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
3340Sstevel@tonic-gate 	} else {
3350Sstevel@tonic-gate 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL) {
3360Sstevel@tonic-gate 			error = PAM_BUF_ERR;
3370Sstevel@tonic-gate 			goto out;
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 		pwu_rep->type = auth_rep->type;
3400Sstevel@tonic-gate 		pwu_rep->scope = auth_rep->scope;
3410Sstevel@tonic-gate 		pwu_rep->scope_len = auth_rep->scope_len;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * First get the password information
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	attr_pw[0].type =  ATTR_REP_NAME;	attr_pw[0].next = &attr_pw[1];
3480Sstevel@tonic-gate 	attr_pw[1].type =  ATTR_UID;		attr_pw[1].next = &attr_pw[2];
3490Sstevel@tonic-gate 	attr_pw[2].type =  ATTR_PASSWD;		attr_pw[2].next = NULL;
3500Sstevel@tonic-gate 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND) {
3530Sstevel@tonic-gate 		error = PAM_USER_UNKNOWN;
3540Sstevel@tonic-gate 		goto out;
3550Sstevel@tonic-gate 	} else if (result == PWU_DENIED) {
3560Sstevel@tonic-gate 		error = PAM_PERM_DENIED;
3570Sstevel@tonic-gate 		goto out;
3580Sstevel@tonic-gate 	} else if (result == PWU_NOMEM) {
3590Sstevel@tonic-gate 		error = PAM_BUF_ERR;
3600Sstevel@tonic-gate 		goto out;
3610Sstevel@tonic-gate 	} else if (result != PWU_SUCCESS) {
3620Sstevel@tonic-gate 		error = PAM_SERVICE_ERR;
3630Sstevel@tonic-gate 		goto out;
3640Sstevel@tonic-gate 	} else {
3650Sstevel@tonic-gate 		repository_name = attr_pw[0].data.val_s;
3660Sstevel@tonic-gate 		pw_uid = attr_pw[1].data.val_i;
3670Sstevel@tonic-gate 		shpwd.sp_pwdp = attr_pw[2].data.val_s;
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
371*11262SRajagopal.Andra@Sun.COM 	 * if repository is not files|nis, and user wants server_policy,
372*11262SRajagopal.Andra@Sun.COM 	 * we don't care about aging and hence return PAM_IGNORE
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	if (server_policy &&
3750Sstevel@tonic-gate 	    strcmp(repository_name, "files") != 0 &&
376*11262SRajagopal.Andra@Sun.COM 	    strcmp(repository_name, "nis") != 0) {
3770Sstevel@tonic-gate 		error = PAM_IGNORE;
3780Sstevel@tonic-gate 		goto out;
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3820Sstevel@tonic-gate 	 * Now get the aging information
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	attr_spw[0].type =  ATTR_LSTCHG;	attr_spw[0].next = &attr_spw[1];
3850Sstevel@tonic-gate 	attr_spw[1].type =  ATTR_MIN;		attr_spw[1].next = &attr_spw[2];
3860Sstevel@tonic-gate 	attr_spw[2].type =  ATTR_MAX;		attr_spw[2].next = &attr_spw[3];
3870Sstevel@tonic-gate 	attr_spw[3].type =  ATTR_WARN;		attr_spw[3].next = &attr_spw[4];
3880Sstevel@tonic-gate 	attr_spw[4].type =  ATTR_INACT;		attr_spw[4].next = &attr_spw[5];
3890Sstevel@tonic-gate 	attr_spw[5].type =  ATTR_EXPIRE;	attr_spw[5].next = &attr_spw[6];
3900Sstevel@tonic-gate 	attr_spw[6].type =  ATTR_FLAG;		attr_spw[6].next = NULL;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	result = __get_authtoken_attr(user, pwu_rep, attr_spw);
3930Sstevel@tonic-gate 	if (result == PWU_SUCCESS) {
3940Sstevel@tonic-gate 		shpwd.sp_lstchg = attr_spw[0].data.val_i;
3950Sstevel@tonic-gate 		shpwd.sp_min = attr_spw[1].data.val_i;
3960Sstevel@tonic-gate 		shpwd.sp_max = attr_spw[2].data.val_i;
3970Sstevel@tonic-gate 		shpwd.sp_warn = attr_spw[3].data.val_i;
3980Sstevel@tonic-gate 		shpwd.sp_inact = attr_spw[4].data.val_i;
3990Sstevel@tonic-gate 		shpwd.sp_expire = attr_spw[5].data.val_i;
4000Sstevel@tonic-gate 		shpwd.sp_flag = attr_spw[6].data.val_i;
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if (debug) {
4040Sstevel@tonic-gate 		char *pw = "Unix PW";
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		if (shpwd.sp_pwdp == NULL)
4070Sstevel@tonic-gate 			pw = "NULL";
4080Sstevel@tonic-gate 		else if (strncmp(shpwd.sp_pwdp, LOCKSTRING,
4090Sstevel@tonic-gate 		    sizeof (LOCKSTRING) - 1) == 0)
4100Sstevel@tonic-gate 			pw = LOCKSTRING;
4117422SJohn.Sonnenschein@Sun.COM 		else if (strcmp(shpwd.sp_pwdp, NOPWDRTR) == 0)
4127422SJohn.Sonnenschein@Sun.COM 			pw = NOPWDRTR;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		if (result ==  PWU_DENIED) {
4158126SJoep.Vesseur@Sun.COM 			__pam_log(LOG_AUTH | LOG_DEBUG,
4168126SJoep.Vesseur@Sun.COM 			    "pam_unix_account: %s: permission denied "
4178126SJoep.Vesseur@Sun.COM 			    "to access password aging information. "
4188126SJoep.Vesseur@Sun.COM 			    "Using defaults.", user);
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 
4218126SJoep.Vesseur@Sun.COM 		__pam_log(LOG_AUTH | LOG_DEBUG,
4220Sstevel@tonic-gate 		    "%s Policy:Unix, pw=%s, lstchg=%d, min=%d, max=%d, "
4230Sstevel@tonic-gate 		    "warn=%d, inact=%d, expire=%d",
4240Sstevel@tonic-gate 		    user, pw, shpwd.sp_lstchg, shpwd.sp_min, shpwd.sp_max,
4250Sstevel@tonic-gate 		    shpwd.sp_warn, shpwd.sp_inact, shpwd.sp_expire);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (pwu_rep != PWU_DEFAULT_REP) {
4290Sstevel@tonic-gate 		free(pwu_rep);
4300Sstevel@tonic-gate 		pwu_rep = PWU_DEFAULT_REP;
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (result == PWU_NOT_FOUND) {
4340Sstevel@tonic-gate 		error = PAM_USER_UNKNOWN;
4350Sstevel@tonic-gate 		goto out;
4360Sstevel@tonic-gate 	} else if (result == PWU_NOMEM) {
4370Sstevel@tonic-gate 		error = PAM_BUF_ERR;
4380Sstevel@tonic-gate 		goto out;
4390Sstevel@tonic-gate 	} else if (result != PWU_SUCCESS && result != PWU_DENIED) {
4400Sstevel@tonic-gate 		error = PAM_SERVICE_ERR;
4410Sstevel@tonic-gate 		goto out;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	/*
4450Sstevel@tonic-gate 	 * Check for locked account
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 	if (shpwd.sp_pwdp != NULL &&
4480Sstevel@tonic-gate 	    strncmp(shpwd.sp_pwdp, LOCKSTRING, sizeof (LOCKSTRING) - 1) == 0) {
4490Sstevel@tonic-gate 		char *service;
4500Sstevel@tonic-gate 		char *rhost = NULL;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		(void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
4530Sstevel@tonic-gate 		(void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
4540Sstevel@tonic-gate 		__pam_log(LOG_AUTH | LOG_NOTICE,
4550Sstevel@tonic-gate 		    "pam_unix_account: %s attempting to validate locked "
4560Sstevel@tonic-gate 		    "account %s from %s",
4570Sstevel@tonic-gate 		    service, user,
4580Sstevel@tonic-gate 		    (rhost != NULL && *rhost != '\0') ? rhost : "local host");
4590Sstevel@tonic-gate 		error = PAM_PERM_DENIED;
4600Sstevel@tonic-gate 		goto out;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/*
4648126SJoep.Vesseur@Sun.COM 	 * Check for NULL password and, if so, see if such is allowed
4658126SJoep.Vesseur@Sun.COM 	 */
4668126SJoep.Vesseur@Sun.COM 	if (shpwd.sp_pwdp[0] == '\0' &&
4678126SJoep.Vesseur@Sun.COM 	    (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
4688126SJoep.Vesseur@Sun.COM 		char *service;
4698126SJoep.Vesseur@Sun.COM 		char *rhost = NULL;
4708126SJoep.Vesseur@Sun.COM 
4718126SJoep.Vesseur@Sun.COM 		(void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
4728126SJoep.Vesseur@Sun.COM 		(void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
4738126SJoep.Vesseur@Sun.COM 
4748126SJoep.Vesseur@Sun.COM 		__pam_log(LOG_AUTH | LOG_NOTICE,
4758126SJoep.Vesseur@Sun.COM 		    "pam_unix_account: %s: empty password not allowed for "
4768126SJoep.Vesseur@Sun.COM 		    "account %s from %s", service, user,
4778126SJoep.Vesseur@Sun.COM 		    (rhost != NULL && *rhost != '\0') ? rhost : "local host");
4788126SJoep.Vesseur@Sun.COM 		error = PAM_PERM_DENIED;
4798126SJoep.Vesseur@Sun.COM 		goto out;
4808126SJoep.Vesseur@Sun.COM 	}
4818126SJoep.Vesseur@Sun.COM 
4828126SJoep.Vesseur@Sun.COM 	/*
4830Sstevel@tonic-gate 	 * Check for account expiration
4840Sstevel@tonic-gate 	 */
4850Sstevel@tonic-gate 	if (shpwd.sp_expire > 0 &&
4860Sstevel@tonic-gate 	    (time_t)shpwd.sp_expire < DAY_NOW) {
4870Sstevel@tonic-gate 		error = PAM_ACCT_EXPIRED;
4880Sstevel@tonic-gate 		goto out;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Check for excessive login account inactivity
4930Sstevel@tonic-gate 	 */
4940Sstevel@tonic-gate 	if (check_for_login_inactivity(pw_uid, &shpwd)) {
4950Sstevel@tonic-gate 		error = PAM_PERM_DENIED;
4960Sstevel@tonic-gate 		goto out;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * Check to see if the user needs to change their password
5010Sstevel@tonic-gate 	 */
5028126SJoep.Vesseur@Sun.COM 	if (error = new_password_check(&shpwd, flags)) {
5030Sstevel@tonic-gate 		goto out;
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/*
5070Sstevel@tonic-gate 	 * Check to make sure password aging information is okay
5080Sstevel@tonic-gate 	 */
5090Sstevel@tonic-gate 	if ((error = perform_passwd_aging_check(pamh, &shpwd, flags))
5108126SJoep.Vesseur@Sun.COM 	    != PAM_SUCCESS) {
5110Sstevel@tonic-gate 		goto out;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/*
5150Sstevel@tonic-gate 	 * Finally, warn the user if their password is about to expire.
5160Sstevel@tonic-gate 	 */
5170Sstevel@tonic-gate 	if (!(flags & PAM_SILENT)) {
5180Sstevel@tonic-gate 		warn_user_passwd_will_expire(pamh, shpwd);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * All done, return Success
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	error = PAM_SUCCESS;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate out:
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	{
5290Sstevel@tonic-gate 		int pam_res;
5300Sstevel@tonic-gate 		unix_authtok_data *authtok_data;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		if (debug) {
5338126SJoep.Vesseur@Sun.COM 			__pam_log(LOG_AUTH | LOG_DEBUG,
5348126SJoep.Vesseur@Sun.COM 			    "pam_unix_account: %s: %s",
5358126SJoep.Vesseur@Sun.COM 			    (user == NULL)?"NULL":user,
5368126SJoep.Vesseur@Sun.COM 			    pam_strerror(pamh, error));
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		if (repository_name)
5400Sstevel@tonic-gate 			free(repository_name);
5410Sstevel@tonic-gate 		if (pwu_rep != PWU_DEFAULT_REP)
5420Sstevel@tonic-gate 			free(pwu_rep);
5430Sstevel@tonic-gate 		if (shpwd.sp_pwdp) {
5440Sstevel@tonic-gate 			(void) memset(shpwd.sp_pwdp, 0, strlen(shpwd.sp_pwdp));
5450Sstevel@tonic-gate 			free(shpwd.sp_pwdp);
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		/* store the password aging status in the pam handle */
5498126SJoep.Vesseur@Sun.COM 		pam_res = pam_get_data(pamh, UNIX_AUTHTOK_DATA,
5508126SJoep.Vesseur@Sun.COM 		    (const void **)&authtok_data);
5510Sstevel@tonic-gate 
5528126SJoep.Vesseur@Sun.COM 		if ((status = (unix_authtok_data *)calloc(1,
5538126SJoep.Vesseur@Sun.COM 		    sizeof (unix_authtok_data))) == NULL) {
5540Sstevel@tonic-gate 			return (PAM_BUF_ERR);
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		if (pam_res == PAM_SUCCESS)
5580Sstevel@tonic-gate 			(void) memcpy(status, authtok_data,
5598126SJoep.Vesseur@Sun.COM 			    sizeof (unix_authtok_data));
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		status->age_status = error;
5620Sstevel@tonic-gate 		if (pam_set_data(pamh, UNIX_AUTHTOK_DATA, status, unix_cleanup)
5638126SJoep.Vesseur@Sun.COM 		    != PAM_SUCCESS) {
5640Sstevel@tonic-gate 			free(status);
5650Sstevel@tonic-gate 			return (PAM_SERVICE_ERR);
5660Sstevel@tonic-gate 		}
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	return (error);
5700Sstevel@tonic-gate }
571