xref: /titanic_51/usr/src/lib/passwdutil/files_attr.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <pwd.h>
347c478bd9Sstevel@tonic-gate #include <shadow.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
407c478bd9Sstevel@tonic-gate #include <macros.h>
417c478bd9Sstevel@tonic-gate #include <syslog.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <limits.h>		/* LOGNAME_MAX -- max Solaris user name */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "passwdutil.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int files_lock(void);
487c478bd9Sstevel@tonic-gate int files_unlock(void);
497c478bd9Sstevel@tonic-gate int files_checkhistory(char *user, char *passwd, pwu_repository_t *rep);
507c478bd9Sstevel@tonic-gate int files_getattr(char *name, attrlist *item, pwu_repository_t *rep);
517c478bd9Sstevel@tonic-gate int files_getpwnam(char *name, attrlist *items, pwu_repository_t *rep,
527c478bd9Sstevel@tonic-gate     void **buf);
537c478bd9Sstevel@tonic-gate int files_update(attrlist *items, pwu_repository_t *rep, void *buf);
547c478bd9Sstevel@tonic-gate int files_putpwnam(char *name, char *oldpw, char *dummy,
557c478bd9Sstevel@tonic-gate 	pwu_repository_t *rep, void *buf);
567c478bd9Sstevel@tonic-gate int files_user_to_authenticate(char *name, pwu_repository_t *rep,
577c478bd9Sstevel@tonic-gate 	char **auth_user, int *privileged);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int files_update_history(char *name, struct spwd *spwd);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * files function pointer table, used by passwdutil_init to initialize
637c478bd9Sstevel@tonic-gate  * the global Repository-OPerations table "rops"
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate struct repops files_repops = {
667c478bd9Sstevel@tonic-gate 	files_checkhistory,
677c478bd9Sstevel@tonic-gate 	files_getattr,
687c478bd9Sstevel@tonic-gate 	files_getpwnam,
697c478bd9Sstevel@tonic-gate 	files_update,
707c478bd9Sstevel@tonic-gate 	files_putpwnam,
717c478bd9Sstevel@tonic-gate 	files_user_to_authenticate,
727c478bd9Sstevel@tonic-gate 	files_lock,
737c478bd9Sstevel@tonic-gate 	files_unlock
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * this structure defines the buffer used to keep state between
787c478bd9Sstevel@tonic-gate  * get/update/put calls
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate struct pwbuf {
817c478bd9Sstevel@tonic-gate 	int	update_history;
827c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
837c478bd9Sstevel@tonic-gate 	char   *pwd_scratch;
847c478bd9Sstevel@tonic-gate 	struct spwd *spwd;
857c478bd9Sstevel@tonic-gate 	char   *spwd_scratch;
867c478bd9Sstevel@tonic-gate 	char   *new_sp_pwdp;
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * We should use sysconf, but there is no sysconf name for SHADOW
917c478bd9Sstevel@tonic-gate  * so we use these from nss_dbdefs
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	PWD_SCRATCH_SIZE NSS_LINELEN_PASSWD
947c478bd9Sstevel@tonic-gate #define	SPW_SCRATCH_SIZE NSS_LINELEN_SHADOW
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * lock functions for files repository
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate int
1007c478bd9Sstevel@tonic-gate files_lock(void)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	int res;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (lckpwdf()) {
1057c478bd9Sstevel@tonic-gate 		switch (errno) {
1067c478bd9Sstevel@tonic-gate 		case EINTR:
1077c478bd9Sstevel@tonic-gate 			res = PWU_BUSY;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 		case EACCES:
1107c478bd9Sstevel@tonic-gate 			res = PWU_DENIED;
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case 0:
1137c478bd9Sstevel@tonic-gate 			res = PWU_SUCCESS;
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	} else
1177c478bd9Sstevel@tonic-gate 		res = PWU_SUCCESS;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	return (res);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
1237c478bd9Sstevel@tonic-gate files_unlock(void)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	if (ulckpwdf())
1267c478bd9Sstevel@tonic-gate 		return (PWU_SYSTEM_ERROR);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * files_privileged
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  * Are we a privileged user with regard to the files repository?
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate int
1377c478bd9Sstevel@tonic-gate files_privileged(void)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	return (getuid() == 0);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * private_getpwnam_r()
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * A private implementation of getpwnam_r which does *not* fall back to
1477c478bd9Sstevel@tonic-gate  * other services possibly defined in nsswitch.conf
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * behaves like getpwnam_r().
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate struct passwd *
1527c478bd9Sstevel@tonic-gate private_getpwnam_r(const char *name, struct passwd *result, char *buffer,
1537c478bd9Sstevel@tonic-gate     int buflen)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	FILE *fp;
1567c478bd9Sstevel@tonic-gate 	int found;
1577c478bd9Sstevel@tonic-gate 
158*004388ebScasper 	if ((fp = fopen(PASSWD, "rF")) == NULL)
1597c478bd9Sstevel@tonic-gate 		return (NULL);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	found = 0;
1627c478bd9Sstevel@tonic-gate 	while (!found && fgetpwent_r(fp, result, buffer, buflen) != NULL) {
1637c478bd9Sstevel@tonic-gate 		if (strcmp(name, result->pw_name) == 0)
1647c478bd9Sstevel@tonic-gate 			found = 1;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (!found) {
1707c478bd9Sstevel@tonic-gate 		(void) memset(buffer, 0, buflen);
1717c478bd9Sstevel@tonic-gate 		(void) memset(result, 0, sizeof (*result));
1727c478bd9Sstevel@tonic-gate 		return (NULL);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	return (result);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * private_getspnam_r()
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  * A private implementation of getspnam_r which does *not* fall back to
1827c478bd9Sstevel@tonic-gate  * other services possibly defined in nsswitch.conf.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * Behaves like getspnam_r(). Since we use fgetspent_t(), all numeric
1857c478bd9Sstevel@tonic-gate  * fields that are undefined in /etc/shadow will be set to -1.
1867c478bd9Sstevel@tonic-gate  *
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate struct spwd *
1897c478bd9Sstevel@tonic-gate private_getspnam_r(const char *name, struct spwd *result, char *buffer,
1907c478bd9Sstevel@tonic-gate     int buflen)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	FILE *fp;
1937c478bd9Sstevel@tonic-gate 	int found;
1947c478bd9Sstevel@tonic-gate 
195*004388ebScasper 	fp = fopen(SHADOW, "rF");
1967c478bd9Sstevel@tonic-gate 	if (fp == NULL)
1977c478bd9Sstevel@tonic-gate 		return (NULL);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	found = 0;
2007c478bd9Sstevel@tonic-gate 	while (!found && fgetspent_r(fp, result, buffer, buflen) != NULL) {
2017c478bd9Sstevel@tonic-gate 		if (strcmp(name, result->sp_namp) == 0)
2027c478bd9Sstevel@tonic-gate 			found = 1;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (!found) {
2087c478bd9Sstevel@tonic-gate 		(void) memset(buffer, 0, buflen);
2097c478bd9Sstevel@tonic-gate 		(void) memset(result, 0, sizeof (*result));
2107c478bd9Sstevel@tonic-gate 		return (NULL);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	return (result);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * files_getpwnam(name, items, rep, buf)
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2207c478bd9Sstevel@tonic-gate int
2217c478bd9Sstevel@tonic-gate files_getpwnam(char *name, attrlist *items, pwu_repository_t *rep, void **buf)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	attrlist *p;
2247c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf;
2257c478bd9Sstevel@tonic-gate 	int err = PWU_SUCCESS;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	*buf = calloc(1, sizeof (struct pwbuf));
2287c478bd9Sstevel@tonic-gate 	pwbuf = (struct pwbuf *)*buf;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * determine which password structure (/etc/passwd or /etc/shadow)
2327c478bd9Sstevel@tonic-gate 	 * we need for the items we need to update
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	for (p = items; p != NULL; p = p->next) {
2357c478bd9Sstevel@tonic-gate 		switch (p->type) {
2367c478bd9Sstevel@tonic-gate 		case ATTR_NAME:
2377c478bd9Sstevel@tonic-gate 		case ATTR_UID:
2387c478bd9Sstevel@tonic-gate 		case ATTR_GID:
2397c478bd9Sstevel@tonic-gate 		case ATTR_AGE:
2407c478bd9Sstevel@tonic-gate 		case ATTR_COMMENT:
2417c478bd9Sstevel@tonic-gate 		case ATTR_GECOS:
2427c478bd9Sstevel@tonic-gate 		case ATTR_HOMEDIR:
2437c478bd9Sstevel@tonic-gate 		case ATTR_SHELL:
2447c478bd9Sstevel@tonic-gate 			if (pwbuf->pwd == NULL) {
2457c478bd9Sstevel@tonic-gate 				pwbuf->pwd = malloc(sizeof (struct passwd));
2467c478bd9Sstevel@tonic-gate 				if (pwbuf->pwd == NULL) {
2477c478bd9Sstevel@tonic-gate 					err = PWU_NOMEM;
2487c478bd9Sstevel@tonic-gate 					goto error;
2497c478bd9Sstevel@tonic-gate 				}
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD:
2537c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD_SERVER_POLICY:
2547c478bd9Sstevel@tonic-gate 		case ATTR_LSTCHG:
2557c478bd9Sstevel@tonic-gate 		case ATTR_MIN:
2567c478bd9Sstevel@tonic-gate 		case ATTR_MAX:
2577c478bd9Sstevel@tonic-gate 		case ATTR_WARN:
2587c478bd9Sstevel@tonic-gate 		case ATTR_INACT:
2597c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE:
2607c478bd9Sstevel@tonic-gate 		case ATTR_FLAG:
2617c478bd9Sstevel@tonic-gate 		case ATTR_LOCK_ACCOUNT:
2627c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE_PASSWORD:
2637c478bd9Sstevel@tonic-gate 		case ATTR_FAILED_LOGINS:
2647c478bd9Sstevel@tonic-gate 		case ATTR_INCR_FAILED_LOGINS:
2657c478bd9Sstevel@tonic-gate 		case ATTR_RST_FAILED_LOGINS:
2667c478bd9Sstevel@tonic-gate 		case ATTR_NOLOGIN_ACCOUNT:
2677c478bd9Sstevel@tonic-gate 		case ATTR_UNLOCK_ACCOUNT:
2687c478bd9Sstevel@tonic-gate 			if (pwbuf->spwd == NULL) {
2697c478bd9Sstevel@tonic-gate 				pwbuf->spwd = malloc(sizeof (struct spwd));
2707c478bd9Sstevel@tonic-gate 				if (pwbuf->spwd == NULL) {
2717c478bd9Sstevel@tonic-gate 					err = PWU_NOMEM;
2727c478bd9Sstevel@tonic-gate 					goto error;
2737c478bd9Sstevel@tonic-gate 				}
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 		default:
2777c478bd9Sstevel@tonic-gate 			/*
2787c478bd9Sstevel@tonic-gate 			 * Some other repository might have different values
2797c478bd9Sstevel@tonic-gate 			 * so we ignore those.
2807c478bd9Sstevel@tonic-gate 			 */
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) {
2867c478bd9Sstevel@tonic-gate 		if ((pwbuf->pwd_scratch = malloc(PWD_SCRATCH_SIZE)) == NULL) {
2877c478bd9Sstevel@tonic-gate 			err = PWU_NOMEM;
2887c478bd9Sstevel@tonic-gate 			goto error;
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 		if (private_getpwnam_r(name, pwbuf->pwd, pwbuf->pwd_scratch,
2917c478bd9Sstevel@tonic-gate 		    PWD_SCRATCH_SIZE) == NULL) {
2927c478bd9Sstevel@tonic-gate 			err = PWU_NOT_FOUND;
2937c478bd9Sstevel@tonic-gate 			goto error;
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) {
2987c478bd9Sstevel@tonic-gate 		if ((pwbuf->spwd_scratch = malloc(SPW_SCRATCH_SIZE)) == NULL) {
2997c478bd9Sstevel@tonic-gate 			err = PWU_NOMEM;
3007c478bd9Sstevel@tonic-gate 			goto error;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 		if (private_getspnam_r(name, pwbuf->spwd, pwbuf->spwd_scratch,
3037c478bd9Sstevel@tonic-gate 		    SPW_SCRATCH_SIZE) == NULL) {
3047c478bd9Sstevel@tonic-gate 			err = PWU_NOT_FOUND;
3057c478bd9Sstevel@tonic-gate 			goto error;
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
3107c478bd9Sstevel@tonic-gate error:
3117c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) free(pwbuf->pwd);
3127c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch);
3137c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) free(pwbuf->spwd);
3147c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch);
3157c478bd9Sstevel@tonic-gate 	free(pwbuf);
3167c478bd9Sstevel@tonic-gate 	*buf = NULL;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return (err);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * int files_user_to_authenticate(name, rep, auth_user, privileged)
3237c478bd9Sstevel@tonic-gate  * Determine which user needs to be authenticated. For files, the
3247c478bd9Sstevel@tonic-gate  * possible return values are:
3257c478bd9Sstevel@tonic-gate  * 	PWU_NOT_FOUND
3267c478bd9Sstevel@tonic-gate  *	PWU_SUCCESS	and (auth_user == NULL || auth_user = user)
3277c478bd9Sstevel@tonic-gate  *	PWU_DENIED
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3307c478bd9Sstevel@tonic-gate int
3317c478bd9Sstevel@tonic-gate files_user_to_authenticate(char *user, pwu_repository_t *rep,
3327c478bd9Sstevel@tonic-gate 	char **auth_user, int *privileged)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf;
3357c478bd9Sstevel@tonic-gate 	int res;
3367c478bd9Sstevel@tonic-gate 	attrlist attr_tmp[1] = { { ATTR_UID, NULL, NULL } };
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* check to see if target user is present in files */
3397c478bd9Sstevel@tonic-gate 	res = files_getpwnam(user, &attr_tmp[0], rep, (void **)&pwbuf);
3407c478bd9Sstevel@tonic-gate 	if (res != PWU_SUCCESS)
3417c478bd9Sstevel@tonic-gate 		return (res);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (files_privileged()) {
3447c478bd9Sstevel@tonic-gate 		*auth_user = NULL;
3457c478bd9Sstevel@tonic-gate 		*privileged = 1;
3467c478bd9Sstevel@tonic-gate 		res = PWU_SUCCESS;
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		*privileged = 0;
3497c478bd9Sstevel@tonic-gate 		if (getuid() == pwbuf->pwd->pw_uid) {
3507c478bd9Sstevel@tonic-gate 			*auth_user = strdup(user);
3517c478bd9Sstevel@tonic-gate 			res = PWU_SUCCESS;
3527c478bd9Sstevel@tonic-gate 		} else {
3537c478bd9Sstevel@tonic-gate 			res = PWU_DENIED;
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) free(pwbuf->pwd);
3587c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch);
3597c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) free(pwbuf->spwd);
3607c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch);
3617c478bd9Sstevel@tonic-gate 	free(pwbuf);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	return (res);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  *	Password history file format:
3687c478bd9Sstevel@tonic-gate  *		user:crypw1: ... crypwn: such that n <= MAXHISTORY
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate #define	HISTORY		"/etc/security/passhistory"
3717c478bd9Sstevel@tonic-gate #define	HISTEMP		"/etc/security/pwhistemp"
3727c478bd9Sstevel@tonic-gate #define	OHISTORY	"/etc/security/opwhistory"
3737c478bd9Sstevel@tonic-gate #define	HISTMODE	S_IRUSR	/* mode to create history file */
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * XXX
3767c478bd9Sstevel@tonic-gate  *	3*LOGNAME_MAX just in case there are long user names.
3777c478bd9Sstevel@tonic-gate  *	Traditionally Solaris LOGNAME_MAX (_POSIX_LOGIN_NAME_MAX) is 13,
3787c478bd9Sstevel@tonic-gate  *	but some sites often user more.
3797c478bd9Sstevel@tonic-gate  *	If LOGNAME_MAX ever becomes reasonable (128) and actually enforced,
3807c478bd9Sstevel@tonic-gate  *	fix up here.
3817c478bd9Sstevel@tonic-gate  * XXX
3827c478bd9Sstevel@tonic-gate  */
3837c478bd9Sstevel@tonic-gate #define	MAX_LOGNAME (3 * LOGNAME_MAX)
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  *	files_checkhistory - check if a user's new password is in the user's
3877c478bd9Sstevel@tonic-gate  *		old password history.
3887c478bd9Sstevel@tonic-gate  *
3897c478bd9Sstevel@tonic-gate  *	Entry
3907c478bd9Sstevel@tonic-gate  *		user = username.
3917c478bd9Sstevel@tonic-gate  *		passwd = new clear text password.
3927c478bd9Sstevel@tonic-gate  *
3937c478bd9Sstevel@tonic-gate  *	Exit
3947c478bd9Sstevel@tonic-gate  *		PWU_SUCCESS, passwd found in user's old password history.
3957c478bd9Sstevel@tonic-gate  *			The caller should only be interested and fail if
3967c478bd9Sstevel@tonic-gate  *			PWU_SUCCESS is returned.
3977c478bd9Sstevel@tonic-gate  *		PWU_NOT_FOUND, passwd not in user's old password history.
3987c478bd9Sstevel@tonic-gate  *		PWU_errors, PWU_ errors from other routines.
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate int
4027c478bd9Sstevel@tonic-gate files_checkhistory(char *user, char *passwd, pwu_repository_t *rep)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	attrlist attr;
4057c478bd9Sstevel@tonic-gate 	int res;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	attr.type = ATTR_HISTORY;
4087c478bd9Sstevel@tonic-gate 	attr.data.val_s = NULL;
4097c478bd9Sstevel@tonic-gate 	attr.next = NULL;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	debug("files_checkhistory(user=%s)", user);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * XXX
4157c478bd9Sstevel@tonic-gate 	 *	This depends on the underlying files_getattr implementation
4167c478bd9Sstevel@tonic-gate 	 *	treating user not found in backing store or no history as
4177c478bd9Sstevel@tonic-gate 	 *	an error.
4187c478bd9Sstevel@tonic-gate 	 * XXX
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if ((res = files_getattr(user, &attr, rep)) == PWU_SUCCESS) {
4227c478bd9Sstevel@tonic-gate 		char	*s;
4237c478bd9Sstevel@tonic-gate 		char	*crypt_passwd;
4247c478bd9Sstevel@tonic-gate 		int	histsize;
4257c478bd9Sstevel@tonic-gate 		char	*last = attr.data.val_s;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		if ((histsize = def_getint("HISTORY=", DEFHISTORY)) == 0) {
4287c478bd9Sstevel@tonic-gate 			debug("files_checkhistory: no history requested");
4297c478bd9Sstevel@tonic-gate 			res = PWU_NOT_FOUND;
4307c478bd9Sstevel@tonic-gate 			goto out;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		debug("files_checkhistory: histsize = %d", histsize);
4347c478bd9Sstevel@tonic-gate 		if (histsize > MAXHISTORY)
4357c478bd9Sstevel@tonic-gate 			histsize = MAXHISTORY;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		debug("line to test\n\t%s", last);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		/* compare crypt_passwd to attr.data.val_s strings. */
4407c478bd9Sstevel@tonic-gate 		res = PWU_NOT_FOUND;
4417c478bd9Sstevel@tonic-gate 		while ((histsize-- > 0) &&
4427c478bd9Sstevel@tonic-gate 		    (((s = strtok_r(NULL, ":", &last)) != NULL) &&
4437c478bd9Sstevel@tonic-gate 		    (*s != '\n'))) {
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 			crypt_passwd = crypt(passwd, s);
4467c478bd9Sstevel@tonic-gate 			debug("files_checkhistory: user_pw=%s, history_pw=%s",
4477c478bd9Sstevel@tonic-gate 			    crypt_passwd, s);
4487c478bd9Sstevel@tonic-gate 			if (strcmp(crypt_passwd, s) == 0) {
4497c478bd9Sstevel@tonic-gate 				res = PWU_SUCCESS;
4507c478bd9Sstevel@tonic-gate 				break;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 		}
4537c478bd9Sstevel@tonic-gate 		debug("files_checkhistory(%s, %s) = %d", user, crypt_passwd,
4547c478bd9Sstevel@tonic-gate 		    res);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate out:
4577c478bd9Sstevel@tonic-gate 	if (attr.data.val_s != NULL)
4587c478bd9Sstevel@tonic-gate 		free(attr.data.val_s);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (res);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * files_getattr(name, items, rep)
4657c478bd9Sstevel@tonic-gate  *
4667c478bd9Sstevel@tonic-gate  * Get attributes specified in list 'items'
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate int
4697c478bd9Sstevel@tonic-gate files_getattr(char *name, attrlist *items, pwu_repository_t *rep)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf;
4727c478bd9Sstevel@tonic-gate 	struct passwd *pw;
4737c478bd9Sstevel@tonic-gate 	struct spwd *spw;
4747c478bd9Sstevel@tonic-gate 	attrlist *w;
4757c478bd9Sstevel@tonic-gate 	int res;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	res = files_getpwnam(name, items, rep, (void **)&pwbuf);
4787c478bd9Sstevel@tonic-gate 	if (res != PWU_SUCCESS)
4797c478bd9Sstevel@tonic-gate 		return (res);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	pw = pwbuf->pwd;
4827c478bd9Sstevel@tonic-gate 	spw = pwbuf->spwd;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	for (w = items; res == PWU_SUCCESS && w != NULL; w = w->next) {
4857c478bd9Sstevel@tonic-gate 		switch (w->type) {
4867c478bd9Sstevel@tonic-gate 		case ATTR_NAME:
4877c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_name)) == NULL)
4887c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
4897c478bd9Sstevel@tonic-gate 			break;
4907c478bd9Sstevel@tonic-gate 		case ATTR_COMMENT:
4917c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_comment)) == NULL)
4927c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 		case ATTR_GECOS:
4957c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_gecos)) == NULL)
4967c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
4977c478bd9Sstevel@tonic-gate 			break;
4987c478bd9Sstevel@tonic-gate 		case ATTR_HOMEDIR:
4997c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_dir)) == NULL)
5007c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5017c478bd9Sstevel@tonic-gate 			break;
5027c478bd9Sstevel@tonic-gate 		case ATTR_SHELL:
5037c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_shell)) == NULL)
5047c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5057c478bd9Sstevel@tonic-gate 			break;
5067c478bd9Sstevel@tonic-gate 		/*
5077c478bd9Sstevel@tonic-gate 		 * Nothing special needs to be done for
5087c478bd9Sstevel@tonic-gate 		 * server policy
5097c478bd9Sstevel@tonic-gate 		 */
5107c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD:
5117c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD_SERVER_POLICY:
5127c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(spw->sp_pwdp)) == NULL)
5137c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5147c478bd9Sstevel@tonic-gate 			break;
5157c478bd9Sstevel@tonic-gate 		case ATTR_AGE:
5167c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_age)) == NULL)
5177c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 		case ATTR_REP_NAME:
5207c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup("files")) == NULL)
5217c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		case ATTR_HISTORY: {
5247c478bd9Sstevel@tonic-gate 			FILE	*history;
5257c478bd9Sstevel@tonic-gate 			char	buf[MAX_LOGNAME + MAXHISTORY +
5267c478bd9Sstevel@tonic-gate 			    (MAXHISTORY * CRYPT_MAXCIPHERTEXTLEN)+1];
5277c478bd9Sstevel@tonic-gate 			char	*s, *s1;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 			debug("files_getattr: Get password history for %s ",
5307c478bd9Sstevel@tonic-gate 			    name);
5317c478bd9Sstevel@tonic-gate 
532*004388ebScasper 			if ((history = fopen(HISTORY, "rF")) == NULL) {
5337c478bd9Sstevel@tonic-gate 				debug("files_getattr: %s not found", HISTORY);
5347c478bd9Sstevel@tonic-gate 				res = PWU_OPEN_FAILED;
5357c478bd9Sstevel@tonic-gate 				goto getattr_exit;
5367c478bd9Sstevel@tonic-gate 			}
5377c478bd9Sstevel@tonic-gate 			res = PWU_NOT_FOUND;
5387c478bd9Sstevel@tonic-gate 			while ((s = fgets(buf, sizeof (buf), history)) !=
5397c478bd9Sstevel@tonic-gate 			    NULL) {
5407c478bd9Sstevel@tonic-gate 				s1 = strchr(s, ':');
5417c478bd9Sstevel@tonic-gate 				if (s1 != NULL) {
5427c478bd9Sstevel@tonic-gate 					*s1 = '\0';
5437c478bd9Sstevel@tonic-gate 				} else {
5447c478bd9Sstevel@tonic-gate 					res = PWU_NOT_FOUND;
5457c478bd9Sstevel@tonic-gate 					break;
5467c478bd9Sstevel@tonic-gate 				}
5477c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5487c478bd9Sstevel@tonic-gate 				debug("got history line for %s", s);
5497c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
5507c478bd9Sstevel@tonic-gate 				if (strcmp(s, name) == 0) {
5517c478bd9Sstevel@tonic-gate 					/* found user */
5527c478bd9Sstevel@tonic-gate 					if ((items->data.val_s =
5537c478bd9Sstevel@tonic-gate 					    strdup(s1+1)) == NULL)
5547c478bd9Sstevel@tonic-gate 						res = PWU_NOMEM;
5557c478bd9Sstevel@tonic-gate 					else
5567c478bd9Sstevel@tonic-gate 						res = PWU_SUCCESS;
5577c478bd9Sstevel@tonic-gate 					break;
5587c478bd9Sstevel@tonic-gate 				}
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 			(void) fclose(history);
5617c478bd9Sstevel@tonic-gate 			break;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		/* integer values */
5657c478bd9Sstevel@tonic-gate 		case ATTR_UID:
5667c478bd9Sstevel@tonic-gate 			w->data.val_i = pw->pw_uid;
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 		case ATTR_GID:
5697c478bd9Sstevel@tonic-gate 			w->data.val_i = pw->pw_gid;
5707c478bd9Sstevel@tonic-gate 			break;
5717c478bd9Sstevel@tonic-gate 		case ATTR_LSTCHG:
5727c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_lstchg;
5737c478bd9Sstevel@tonic-gate 			break;
5747c478bd9Sstevel@tonic-gate 		case ATTR_MIN:
5757c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_min;
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		case ATTR_MAX:
5787c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_max;
5797c478bd9Sstevel@tonic-gate 			break;
5807c478bd9Sstevel@tonic-gate 		case ATTR_WARN:
5817c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_warn;
5827c478bd9Sstevel@tonic-gate 			break;
5837c478bd9Sstevel@tonic-gate 		case ATTR_INACT:
5847c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_inact;
5857c478bd9Sstevel@tonic-gate 			break;
5867c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE:
5877c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_expire;
5887c478bd9Sstevel@tonic-gate 			break;
5897c478bd9Sstevel@tonic-gate 		case ATTR_FLAG:
5907c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_flag;
5917c478bd9Sstevel@tonic-gate 			break;
5927c478bd9Sstevel@tonic-gate 		case ATTR_FAILED_LOGINS:
5937c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_flag & FAILCOUNT_MASK;
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 		default:
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate getattr_exit:
6017c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) free(pwbuf->pwd);
6027c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch);
6037c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) free(pwbuf->spwd);
6047c478bd9Sstevel@tonic-gate 	free(pwbuf);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	return (res);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * max_present(list)
6117c478bd9Sstevel@tonic-gate  *
6127c478bd9Sstevel@tonic-gate  * see if attribute ATTR_MAX, with value != -1, is present in
6137c478bd9Sstevel@tonic-gate  * attribute-list "list".
6147c478bd9Sstevel@tonic-gate  *
6157c478bd9Sstevel@tonic-gate  * returns 1 if present, 0 otherwise.
6167c478bd9Sstevel@tonic-gate  */
6177c478bd9Sstevel@tonic-gate static int
6187c478bd9Sstevel@tonic-gate max_present(attrlist *list)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	while (list != NULL)
6217c478bd9Sstevel@tonic-gate 		if (list->type == ATTR_MAX && list->data.val_i != -1)
6227c478bd9Sstevel@tonic-gate 			return (1);
6237c478bd9Sstevel@tonic-gate 		else
6247c478bd9Sstevel@tonic-gate 			list = list->next;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /*
6307c478bd9Sstevel@tonic-gate  * files_update(items, rep, buf)
6317c478bd9Sstevel@tonic-gate  *
6327c478bd9Sstevel@tonic-gate  * update the information in buf with the attributes specified in
6337c478bd9Sstevel@tonic-gate  * items.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6367c478bd9Sstevel@tonic-gate int
6377c478bd9Sstevel@tonic-gate files_update(attrlist *items, pwu_repository_t *rep, void *buf)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf = (struct pwbuf *)buf;
6407c478bd9Sstevel@tonic-gate 	struct passwd *pw;
6417c478bd9Sstevel@tonic-gate 	struct spwd *spw;
6427c478bd9Sstevel@tonic-gate 	attrlist *p;
6437c478bd9Sstevel@tonic-gate 	int aging_needed = 0;
6447c478bd9Sstevel@tonic-gate 	int aging_set = 0;
6457c478bd9Sstevel@tonic-gate 	int disable_aging;
6467c478bd9Sstevel@tonic-gate 	char *pword;
6477c478bd9Sstevel@tonic-gate 	int len;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	pw = pwbuf->pwd;
6507c478bd9Sstevel@tonic-gate 	spw = pwbuf->spwd;
6517c478bd9Sstevel@tonic-gate 	pwbuf->update_history = 0;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * if sp_max==0 : disable passwd aging after updating the password
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	disable_aging = (spw != NULL && spw->sp_max == 0);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	for (p = items; p != NULL; p = p->next) {
6597c478bd9Sstevel@tonic-gate 		switch (p->type) {
6607c478bd9Sstevel@tonic-gate 		case ATTR_NAME:
6617c478bd9Sstevel@tonic-gate 			break;	/* We are able to handle this, but... */
6627c478bd9Sstevel@tonic-gate 		case ATTR_UID:
6637c478bd9Sstevel@tonic-gate 			pw->pw_uid = (uid_t)p->data.val_i;
6647c478bd9Sstevel@tonic-gate 			break;
6657c478bd9Sstevel@tonic-gate 		case ATTR_GID:
6667c478bd9Sstevel@tonic-gate 			pw->pw_gid = (gid_t)p->data.val_i;
6677c478bd9Sstevel@tonic-gate 			break;
6687c478bd9Sstevel@tonic-gate 		case ATTR_AGE:
6697c478bd9Sstevel@tonic-gate 			pw->pw_age = p->data.val_s;
6707c478bd9Sstevel@tonic-gate 			break;
6717c478bd9Sstevel@tonic-gate 		case ATTR_COMMENT:
6727c478bd9Sstevel@tonic-gate 			pw->pw_comment = p->data.val_s;
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 		case ATTR_GECOS:
6757c478bd9Sstevel@tonic-gate 			pw->pw_gecos = p->data.val_s;
6767c478bd9Sstevel@tonic-gate 			break;
6777c478bd9Sstevel@tonic-gate 		case ATTR_HOMEDIR:
6787c478bd9Sstevel@tonic-gate 			pw->pw_dir = p->data.val_s;
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 		case ATTR_SHELL:
6817c478bd9Sstevel@tonic-gate 			pw->pw_shell = p->data.val_s;
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Nothing special needs to be done for
6867c478bd9Sstevel@tonic-gate 		 * server policy
6877c478bd9Sstevel@tonic-gate 		 */
6887c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD:
6897c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD_SERVER_POLICY:
6907c478bd9Sstevel@tonic-gate 			/*
6917c478bd9Sstevel@tonic-gate 			 * There is a special case only for files: if the
6927c478bd9Sstevel@tonic-gate 			 * password is to be deleted (-d to passwd),
6937c478bd9Sstevel@tonic-gate 			 * p->data.val_s will be NULL.
6947c478bd9Sstevel@tonic-gate 			 */
6957c478bd9Sstevel@tonic-gate 			if (p->data.val_s == NULL) {
6967c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = "";
6977c478bd9Sstevel@tonic-gate 			} else {
6987c478bd9Sstevel@tonic-gate 				char *salt = NULL;
6997c478bd9Sstevel@tonic-gate 				char *hash = NULL;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 				salt = crypt_gensalt(spw->sp_pwdp, pw);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 				if (salt == NULL) {
7047c478bd9Sstevel@tonic-gate 					if (errno == ENOMEM)
7057c478bd9Sstevel@tonic-gate 						return (PWU_NOMEM);
7067c478bd9Sstevel@tonic-gate 					/* algorithm problem? */
7077c478bd9Sstevel@tonic-gate 					syslog(LOG_AUTH | LOG_ALERT,
7087c478bd9Sstevel@tonic-gate 						"passwdutil: crypt_gensalt %m");
7097c478bd9Sstevel@tonic-gate 					return (PWU_UPDATE_FAILED);
7107c478bd9Sstevel@tonic-gate 				}
7117c478bd9Sstevel@tonic-gate 				hash = crypt(p->data.val_s, salt);
7127c478bd9Sstevel@tonic-gate 				free(salt);
7137c478bd9Sstevel@tonic-gate 				if (hash == NULL) {
7147c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
7157c478bd9Sstevel@tonic-gate 					return (PWU_NOMEM);
7167c478bd9Sstevel@tonic-gate 				}
7177c478bd9Sstevel@tonic-gate 				pword = strdup(hash);
7187c478bd9Sstevel@tonic-gate 				free(hash);
7197c478bd9Sstevel@tonic-gate 				if (pword == NULL) {
7207c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
7217c478bd9Sstevel@tonic-gate 					return (PWU_NOMEM);
7227c478bd9Sstevel@tonic-gate 				}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 				if (pwbuf->new_sp_pwdp)
7257c478bd9Sstevel@tonic-gate 					free(pwbuf->new_sp_pwdp);
7267c478bd9Sstevel@tonic-gate 				pwbuf->new_sp_pwdp = pword;
7277c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = pword;
7287c478bd9Sstevel@tonic-gate 				aging_needed = 1;
7297c478bd9Sstevel@tonic-gate 				pwbuf->update_history = 1;
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 			spw->sp_flag &= ~FAILCOUNT_MASK; /* reset count */
7327c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7337c478bd9Sstevel@tonic-gate 			break;
7347c478bd9Sstevel@tonic-gate 		case ATTR_LOCK_ACCOUNT:
7357c478bd9Sstevel@tonic-gate 			if (spw->sp_pwdp == NULL) {
7367c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = LOCKSTRING;
7377c478bd9Sstevel@tonic-gate 			} else if (strncmp(spw->sp_pwdp, LOCKSTRING,
7387c478bd9Sstevel@tonic-gate 			    sizeof (LOCKSTRING)-1) != 0) {
7397c478bd9Sstevel@tonic-gate 				len = sizeof (LOCKSTRING)-1 +
7407c478bd9Sstevel@tonic-gate 				    strlen(spw->sp_pwdp) + 1;
7417c478bd9Sstevel@tonic-gate 				pword = malloc(len);
7427c478bd9Sstevel@tonic-gate 				if (pword == NULL) {
7437c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
7447c478bd9Sstevel@tonic-gate 					return (PWU_NOMEM);
7457c478bd9Sstevel@tonic-gate 				}
7467c478bd9Sstevel@tonic-gate 				(void) strlcpy(pword, LOCKSTRING, len);
7477c478bd9Sstevel@tonic-gate 				(void) strlcat(pword, spw->sp_pwdp, len);
7487c478bd9Sstevel@tonic-gate 				if (pwbuf->new_sp_pwdp)
7497c478bd9Sstevel@tonic-gate 					free(pwbuf->new_sp_pwdp);
7507c478bd9Sstevel@tonic-gate 				pwbuf->new_sp_pwdp = pword;
7517c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = pword;
7527c478bd9Sstevel@tonic-gate 			}
7537c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7547c478bd9Sstevel@tonic-gate 			break;
7557c478bd9Sstevel@tonic-gate 		case ATTR_UNLOCK_ACCOUNT:
7567c478bd9Sstevel@tonic-gate 			if (spw->sp_pwdp != NULL &&
7577c478bd9Sstevel@tonic-gate 			    strncmp(spw->sp_pwdp, LOCKSTRING,
7587c478bd9Sstevel@tonic-gate 			    sizeof (LOCKSTRING)-1) == 0) {
7597c478bd9Sstevel@tonic-gate 				(void) strcpy(spw->sp_pwdp, spw->sp_pwdp +
7607c478bd9Sstevel@tonic-gate 					sizeof (LOCKSTRING)-1);
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7637c478bd9Sstevel@tonic-gate 			break;
7647c478bd9Sstevel@tonic-gate 		case ATTR_NOLOGIN_ACCOUNT:
7657c478bd9Sstevel@tonic-gate 			spw->sp_pwdp = NOLOGINSTRING;
7667c478bd9Sstevel@tonic-gate 			if (pwbuf->new_sp_pwdp) {
7677c478bd9Sstevel@tonic-gate 				free(pwbuf->new_sp_pwdp);
7687c478bd9Sstevel@tonic-gate 				pwbuf->new_sp_pwdp = NULL;
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7717c478bd9Sstevel@tonic-gate 			break;
7727c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE_PASSWORD:
7737c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = 0;
7747c478bd9Sstevel@tonic-gate 			break;
7757c478bd9Sstevel@tonic-gate 		case ATTR_LSTCHG:
7767c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = p->data.val_i;
7777c478bd9Sstevel@tonic-gate 			break;
7787c478bd9Sstevel@tonic-gate 		case ATTR_MIN:
7797c478bd9Sstevel@tonic-gate 			if (spw->sp_max == -1 &&
7807c478bd9Sstevel@tonic-gate 			    p->data.val_i != -1 && max_present(p->next) == 0)
7817c478bd9Sstevel@tonic-gate 				return (PWU_AGING_DISABLED);
7827c478bd9Sstevel@tonic-gate 			spw->sp_min = p->data.val_i;
7837c478bd9Sstevel@tonic-gate 			aging_set = 1;
7847c478bd9Sstevel@tonic-gate 			break;
7857c478bd9Sstevel@tonic-gate 		case ATTR_MAX:
7867c478bd9Sstevel@tonic-gate 			if (p->data.val_i == -1) {
7877c478bd9Sstevel@tonic-gate 				/* Turn aging off -> Reset min and warn too */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 				spw->sp_min = -1;
7907c478bd9Sstevel@tonic-gate 				spw->sp_warn = -1;
7917c478bd9Sstevel@tonic-gate 			} else {
7927c478bd9Sstevel@tonic-gate 				/* Turn aging on */
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 				if (spw->sp_min == -1) {
7957c478bd9Sstevel@tonic-gate 					/*
7967c478bd9Sstevel@tonic-gate 					 * If minage has not been set with
7977c478bd9Sstevel@tonic-gate 					 * a command-line option, we set it
7987c478bd9Sstevel@tonic-gate 					 * to zero.
7997c478bd9Sstevel@tonic-gate 					 */
8007c478bd9Sstevel@tonic-gate 					spw->sp_min = 0;
8017c478bd9Sstevel@tonic-gate 				}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 				/*
8047c478bd9Sstevel@tonic-gate 				 * If aging was turned off, we update lstchg.
8057c478bd9Sstevel@tonic-gate 				 *
8067c478bd9Sstevel@tonic-gate 				 * We take care not to update lstchg if the
8077c478bd9Sstevel@tonic-gate 				 * user has no password, otherwise the user
8087c478bd9Sstevel@tonic-gate 				 * might not be required to provide a password
8097c478bd9Sstevel@tonic-gate 				 * the next time [s]he logs-in.
8107c478bd9Sstevel@tonic-gate 				 *
8117c478bd9Sstevel@tonic-gate 				 * Also, if lstchg != -1 (i.e., not set in
8127c478bd9Sstevel@tonic-gate 				 * /etc/shadow), we keep the old value.
8137c478bd9Sstevel@tonic-gate 				 */
8147c478bd9Sstevel@tonic-gate 				if (spw->sp_max == -1 &&
8157c478bd9Sstevel@tonic-gate 				    spw->sp_pwdp != NULL && *spw->sp_pwdp &&
8167c478bd9Sstevel@tonic-gate 				    spw->sp_lstchg == -1) {
8177c478bd9Sstevel@tonic-gate 					spw->sp_lstchg = DAY_NOW_32;
8187c478bd9Sstevel@tonic-gate 				}
8197c478bd9Sstevel@tonic-gate 			}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 			spw->sp_max = p->data.val_i;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 			aging_set = 1;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 			break;
8267c478bd9Sstevel@tonic-gate 		case ATTR_WARN:
8277c478bd9Sstevel@tonic-gate 			if (spw->sp_max == -1 && p->data.val_i != -1 &&
8287c478bd9Sstevel@tonic-gate 			    max_present(p->next) == 0)
8297c478bd9Sstevel@tonic-gate 				return (PWU_AGING_DISABLED);
8307c478bd9Sstevel@tonic-gate 			spw->sp_warn =  p->data.val_i;
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case ATTR_INACT:
8337c478bd9Sstevel@tonic-gate 			spw->sp_inact = p->data.val_i;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE:
8367c478bd9Sstevel@tonic-gate 			spw->sp_expire = p->data.val_i;
8377c478bd9Sstevel@tonic-gate 			break;
8387c478bd9Sstevel@tonic-gate 		case ATTR_FLAG:
8397c478bd9Sstevel@tonic-gate 			spw->sp_flag = p->data.val_i;
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 		case ATTR_INCR_FAILED_LOGINS:
8427c478bd9Sstevel@tonic-gate 			{
8437c478bd9Sstevel@tonic-gate 			int count = (spw->sp_flag & FAILCOUNT_MASK) + 1;
8447c478bd9Sstevel@tonic-gate 			spw->sp_flag &= ~FAILCOUNT_MASK;
8457c478bd9Sstevel@tonic-gate 			spw->sp_flag |= min(FAILCOUNT_MASK, count);
8467c478bd9Sstevel@tonic-gate 			p->data.val_i = count;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 		case ATTR_RST_FAILED_LOGINS:
8507c478bd9Sstevel@tonic-gate 			p->data.val_i = spw->sp_flag & FAILCOUNT_MASK;
8517c478bd9Sstevel@tonic-gate 			spw->sp_flag &= ~FAILCOUNT_MASK;
8527c478bd9Sstevel@tonic-gate 			break;
8537c478bd9Sstevel@tonic-gate 		default:
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * What should the new aging values look like?
8607c478bd9Sstevel@tonic-gate 	 *
8617c478bd9Sstevel@tonic-gate 	 * There are a number of different conditions
8627c478bd9Sstevel@tonic-gate 	 *
8637c478bd9Sstevel@tonic-gate 	 *  a) aging is already configured: don't touch it
8647c478bd9Sstevel@tonic-gate 	 *
8657c478bd9Sstevel@tonic-gate 	 *  b) disable_aging is set: disable aging
8667c478bd9Sstevel@tonic-gate 	 *
8677c478bd9Sstevel@tonic-gate 	 *  c) aging is not configured: turn on default aging;
8687c478bd9Sstevel@tonic-gate 	 *
8697c478bd9Sstevel@tonic-gate 	 *  b) and c) of course only if aging_needed and !aging_set.
8707c478bd9Sstevel@tonic-gate 	 *  (i.e., password changed, and aging values not changed)
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (spw != NULL && spw->sp_max <= 0) {
8747c478bd9Sstevel@tonic-gate 		/* a) aging not yet configured */
8757c478bd9Sstevel@tonic-gate 		if (aging_needed && !aging_set) {
8767c478bd9Sstevel@tonic-gate 			if (disable_aging) {
8777c478bd9Sstevel@tonic-gate 				/* b) turn off aging */
8787c478bd9Sstevel@tonic-gate 				spw->sp_min = spw->sp_max = spw->sp_warn = -1;
8797c478bd9Sstevel@tonic-gate 			} else {
8807c478bd9Sstevel@tonic-gate 				/* c) */
8817c478bd9Sstevel@tonic-gate 				turn_on_default_aging(spw);
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * files_update_shadow(char *name, struct spwd *spwd)
8917c478bd9Sstevel@tonic-gate  *
8927c478bd9Sstevel@tonic-gate  * update the shadow password file SHADOW to contain the spwd structure
8937c478bd9Sstevel@tonic-gate  * "spwd" for user "name"
8947c478bd9Sstevel@tonic-gate  */
8957c478bd9Sstevel@tonic-gate int
8967c478bd9Sstevel@tonic-gate files_update_shadow(char *name, struct spwd *spwd)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	struct stat64 stbuf;
8997c478bd9Sstevel@tonic-gate 	FILE *dst;
9007c478bd9Sstevel@tonic-gate 	FILE *src;
9017c478bd9Sstevel@tonic-gate 	struct spwd cur;
9027c478bd9Sstevel@tonic-gate 	char buf[SPW_SCRATCH_SIZE];
9037c478bd9Sstevel@tonic-gate 	int tempfd;
9047c478bd9Sstevel@tonic-gate 	mode_t filemode;
9057c478bd9Sstevel@tonic-gate 	int result = -1;
9067c478bd9Sstevel@tonic-gate 	int err = PWU_SUCCESS;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/* Mode of the shadow file should be 400 or 000 */
9097c478bd9Sstevel@tonic-gate 	if (stat64(SHADOW, &stbuf) < 0) {
9107c478bd9Sstevel@tonic-gate 		err = PWU_STAT_FAILED;
9117c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* copy mode from current shadow file (0400 or 0000) */
9157c478bd9Sstevel@tonic-gate 	filemode = stbuf.st_mode & S_IRUSR;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/*
9187c478bd9Sstevel@tonic-gate 	 * we can't specify filemodes to fopen(), and we SHOULD NOT
9197c478bd9Sstevel@tonic-gate 	 * set umask in multi-thread safe libraries, so we use
9207c478bd9Sstevel@tonic-gate 	 * a combination of open() and fdopen()
9217c478bd9Sstevel@tonic-gate 	 */
9227c478bd9Sstevel@tonic-gate 	tempfd = open(SHADTEMP, O_WRONLY|O_CREAT|O_TRUNC, filemode);
9237c478bd9Sstevel@tonic-gate 	if (tempfd < 0) {
9247c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
9257c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	(void) fchown(tempfd, (uid_t)0, stbuf.st_gid);
9287c478bd9Sstevel@tonic-gate 
929*004388ebScasper 	if ((dst = fdopen(tempfd, "wF")) == NULL) {
9307c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
9317c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
934*004388ebScasper 	if ((src = fopen(SHADOW, "rF")) == NULL) {
9357c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
9367c478bd9Sstevel@tonic-gate 		(void) fclose(dst);
9377c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9387c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	/*
9427c478bd9Sstevel@tonic-gate 	 * copy old shadow to temporary file while replacing the entry
9437c478bd9Sstevel@tonic-gate 	 * that matches "name".
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	while (fgetspent_r(src, &cur, buf, sizeof (buf)) != NULL) {
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		if (strcmp(cur.sp_namp, name) == 0)
9487c478bd9Sstevel@tonic-gate 			result = putspent(spwd, dst);
9497c478bd9Sstevel@tonic-gate 		else
9507c478bd9Sstevel@tonic-gate 			result = putspent(&cur, dst);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		if (result != 0) {
9537c478bd9Sstevel@tonic-gate 			err = PWU_WRITE_FAILED;
9547c478bd9Sstevel@tonic-gate 			(void) fclose(src);
9557c478bd9Sstevel@tonic-gate 			(void) fclose(dst);
9567c478bd9Sstevel@tonic-gate 			goto shadow_exit;
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	(void) fclose(src);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (fclose(dst) != 0) {
9637c478bd9Sstevel@tonic-gate 		/*
9647c478bd9Sstevel@tonic-gate 		 * Something went wrong (ENOSPC for example). Don't
9657c478bd9Sstevel@tonic-gate 		 * use the resulting temporary file!
9667c478bd9Sstevel@tonic-gate 		 */
9677c478bd9Sstevel@tonic-gate 		err = PWU_CLOSE_FAILED;
9687c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9697c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/*
9737c478bd9Sstevel@tonic-gate 	 * Rename stmp to shadow:
9747c478bd9Sstevel@tonic-gate 	 *   1. make sure /etc/oshadow is gone
9757c478bd9Sstevel@tonic-gate 	 *   2. ln /etc/shadow /etc/oshadow
9767c478bd9Sstevel@tonic-gate 	 *   3. mv /etc/stmp /etc/shadow
9777c478bd9Sstevel@tonic-gate 	 */
9787c478bd9Sstevel@tonic-gate 	if (unlink(OSHADOW) && access(OSHADOW, 0) == 0) {
9797c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
9807c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9817c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	if (link(SHADOW, OSHADOW) == -1) {
9857c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
9867c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9877c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (rename(SHADTEMP, SHADOW) == -1) {
9917c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
9927c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9937c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 	(void) unlink(OSHADOW);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate shadow_exit:
9987c478bd9Sstevel@tonic-gate 	return (err);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate int
10027c478bd9Sstevel@tonic-gate files_update_passwd(char *name, struct passwd *pwd)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	struct stat64 stbuf;
10057c478bd9Sstevel@tonic-gate 	FILE *src, *dst;
10067c478bd9Sstevel@tonic-gate 	int tempfd;
10077c478bd9Sstevel@tonic-gate 	struct passwd cur;
10087c478bd9Sstevel@tonic-gate 	char buf[PWD_SCRATCH_SIZE];
10097c478bd9Sstevel@tonic-gate 	int result;
10107c478bd9Sstevel@tonic-gate 	int err = PWU_SUCCESS;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	if (stat64(PASSWD, &stbuf) < 0) {
10137c478bd9Sstevel@tonic-gate 		err = PWU_STAT_FAILED;
10147c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	/* see files_update_shadow() for open()+fdopen() rationale */
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if ((tempfd = open(PASSTEMP, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
10207c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
10217c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10227c478bd9Sstevel@tonic-gate 	}
1023*004388ebScasper 	if ((dst = fdopen(tempfd, "wF")) == NULL) {
10247c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
10257c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10267c478bd9Sstevel@tonic-gate 	}
1027*004388ebScasper 	if ((src = fopen(PASSWD, "rF")) == NULL) {
10287c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
10297c478bd9Sstevel@tonic-gate 		(void) fclose(dst);
10307c478bd9Sstevel@tonic-gate 		(void) unlink(PASSTEMP);
10317c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/*
10357c478bd9Sstevel@tonic-gate 	 * copy old password entries to temporary file while replacing
10367c478bd9Sstevel@tonic-gate 	 * the entry that matches "name"
10377c478bd9Sstevel@tonic-gate 	 */
10387c478bd9Sstevel@tonic-gate 	while (fgetpwent_r(src, &cur, buf, sizeof (buf)) != NULL) {
10397c478bd9Sstevel@tonic-gate 		if (strcmp(cur.pw_name, name) == 0)
10407c478bd9Sstevel@tonic-gate 			result = putpwent(pwd, dst);
10417c478bd9Sstevel@tonic-gate 		else
10427c478bd9Sstevel@tonic-gate 			result = putpwent(&cur, dst);
10437c478bd9Sstevel@tonic-gate 		if (result != 0) {
10447c478bd9Sstevel@tonic-gate 			err = PWU_WRITE_FAILED;
10457c478bd9Sstevel@tonic-gate 			(void) fclose(src);
10467c478bd9Sstevel@tonic-gate 			(void) fclose(dst);
10477c478bd9Sstevel@tonic-gate 			goto passwd_exit;
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	(void) fclose(src);
10527c478bd9Sstevel@tonic-gate 	if (fclose(dst) != 0) {
10537c478bd9Sstevel@tonic-gate 		err = PWU_CLOSE_FAILED;
10547c478bd9Sstevel@tonic-gate 		goto passwd_exit; /* Don't trust the temporary file */
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/* Rename temp to passwd */
10587c478bd9Sstevel@tonic-gate 	if (unlink(OPASSWD) && access(OPASSWD, 0) == 0) {
10597c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
10607c478bd9Sstevel@tonic-gate 		(void) unlink(PASSTEMP);
10617c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	if (link(PASSWD, OPASSWD) == -1) {
10657c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
10667c478bd9Sstevel@tonic-gate 		(void) unlink(PASSTEMP);
10677c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if (rename(PASSTEMP, PASSWD) == -1) {
10717c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
10727c478bd9Sstevel@tonic-gate 		(void) unlink(PASSTEMP);
10737c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	(void) chmod(PASSWD, 0644);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate passwd_exit:
10797c478bd9Sstevel@tonic-gate 	return (err);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate  * files_putpwnam(name, oldpw, dummy, rep, buf)
10857c478bd9Sstevel@tonic-gate  *
10867c478bd9Sstevel@tonic-gate  * store the password attributes contained in "buf" in /etc/passwd and
10877c478bd9Sstevel@tonic-gate  * /etc/shadow. The dummy parameter is a placeholder for NIS+
10887c478bd9Sstevel@tonic-gate  * updates where the "oldrpc" password is passed.
10897c478bd9Sstevel@tonic-gate  */
10907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10917c478bd9Sstevel@tonic-gate int
10927c478bd9Sstevel@tonic-gate files_putpwnam(char *name, char *oldpw, char *dummy,
10937c478bd9Sstevel@tonic-gate     pwu_repository_t *rep, void *buf)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf = (struct pwbuf *)buf;
10967c478bd9Sstevel@tonic-gate 	int result = PWU_SUCCESS;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) {
10997c478bd9Sstevel@tonic-gate 		result = files_update_passwd(name, pwbuf->pwd);
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	if (result == PWU_SUCCESS && pwbuf->spwd) {
11037c478bd9Sstevel@tonic-gate 		if (pwbuf->update_history != 0) {
11047c478bd9Sstevel@tonic-gate 			debug("update_history = %d", pwbuf->update_history);
11057c478bd9Sstevel@tonic-gate 			result = files_update_history(name, pwbuf->spwd);
11067c478bd9Sstevel@tonic-gate 		} else {
11077c478bd9Sstevel@tonic-gate 			debug("no password change");
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 		if (result == PWU_SUCCESS) {
11107c478bd9Sstevel@tonic-gate 			result = files_update_shadow(name, pwbuf->spwd);
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) {
11157c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->pwd, 0, sizeof (struct passwd));
11167c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->pwd_scratch, 0, PWD_SCRATCH_SIZE);
11177c478bd9Sstevel@tonic-gate 		free(pwbuf->pwd);
11187c478bd9Sstevel@tonic-gate 		free(pwbuf->pwd_scratch);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) {
11217c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->spwd, 0, sizeof (struct spwd));
11227c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->spwd_scratch, 0, SPW_SCRATCH_SIZE);
11237c478bd9Sstevel@tonic-gate 		free(pwbuf->spwd);
11247c478bd9Sstevel@tonic-gate 		free(pwbuf->spwd_scratch);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 	if (pwbuf->new_sp_pwdp) {
11277c478bd9Sstevel@tonic-gate 		free(pwbuf->new_sp_pwdp);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	return (result);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  *	NOTE:  This is all covered under the repository lock held for updating
11357c478bd9Sstevel@tonic-gate  *	passwd(4) and shadow(4).
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate int
11387c478bd9Sstevel@tonic-gate files_update_history(char *name, struct spwd *spwd)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	int	histsize;
11417c478bd9Sstevel@tonic-gate 	int	tmpfd;
11427c478bd9Sstevel@tonic-gate 	FILE	*src;	/* history database file */
11437c478bd9Sstevel@tonic-gate 	FILE	*dst;	/* temp history database being updated */
11447c478bd9Sstevel@tonic-gate 	struct	stat64 statbuf;
11457c478bd9Sstevel@tonic-gate 	char buf[MAX_LOGNAME + MAXHISTORY +
11467c478bd9Sstevel@tonic-gate 		(MAXHISTORY * CRYPT_MAXCIPHERTEXTLEN)+1];
11477c478bd9Sstevel@tonic-gate 	int	found;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if ((histsize = def_getint("HISTORY=", DEFHISTORY)) == 0) {
11507c478bd9Sstevel@tonic-gate 		debug("files_update_history(%s) no history, unlinking", name);
11517c478bd9Sstevel@tonic-gate 		(void) unlink(HISTORY);
11527c478bd9Sstevel@tonic-gate 		return (PWU_SUCCESS);	/* no history update defined */
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 	debug("files_update_history(%s, %s) histsize = %d", name, spwd->sp_pwdp,
11557c478bd9Sstevel@tonic-gate 	    histsize);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (histsize > MAXHISTORY)
11587c478bd9Sstevel@tonic-gate 		histsize = MAXHISTORY;
11597c478bd9Sstevel@tonic-gate 	if ((tmpfd = open(HISTEMP, O_WRONLY|O_CREAT|O_TRUNC, HISTMODE)) < 0) {
11607c478bd9Sstevel@tonic-gate 		return (PWU_OPEN_FAILED);
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 	(void) fchown(tmpfd, (uid_t)0, (gid_t)0);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	/* get ready to copy */
1165*004388ebScasper 	if (((src = fopen(HISTORY, "rF")) == NULL) &&
11667c478bd9Sstevel@tonic-gate 	    (errno != ENOENT)) {
11677c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
11687c478bd9Sstevel@tonic-gate 		return (PWU_OPEN_FAILED);
11697c478bd9Sstevel@tonic-gate 	}
1170*004388ebScasper 	if ((dst = fdopen(tmpfd, "wF")) == NULL) {
11717c478bd9Sstevel@tonic-gate 		(void) fclose(src);
11727c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
11737c478bd9Sstevel@tonic-gate 		return (PWU_OPEN_FAILED);
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	/* Copy and update if found.  Add if not found. */
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	found = 0;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	while ((src != NULL) &&
11817c478bd9Sstevel@tonic-gate 	    (fgets(buf, sizeof (buf), src) != NULL)) {
11827c478bd9Sstevel@tonic-gate 		char	*user;
11837c478bd9Sstevel@tonic-gate 		char	*last;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		/* get username field */
11867c478bd9Sstevel@tonic-gate 		user = strtok_r(buf, ":", &last);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate #ifdef	DEBUG
11897c478bd9Sstevel@tonic-gate 		debug("files_update_history: read=\"%s\"", user);
11907c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		if (strcmp(user, name) == 0) {
11937c478bd9Sstevel@tonic-gate 			char	*crypt;
11947c478bd9Sstevel@tonic-gate 			int	i;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 			/* found user, update */
11977c478bd9Sstevel@tonic-gate 			found++;
11987c478bd9Sstevel@tonic-gate 			(void) fprintf(dst, "%s:%s:", name, spwd->sp_pwdp);
11997c478bd9Sstevel@tonic-gate 			debug("files_update_history: update user\n"
12007c478bd9Sstevel@tonic-gate 			    "\t%s:%s:", name, spwd->sp_pwdp);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 			/* get old crypted password history */
12037c478bd9Sstevel@tonic-gate 			for (i = 0; i < MAXHISTORY-1; i++) {
12047c478bd9Sstevel@tonic-gate 				crypt = strtok_r(NULL, ":", &last);
12057c478bd9Sstevel@tonic-gate 				if (crypt == NULL ||
12067c478bd9Sstevel@tonic-gate 				    *crypt == '\n') {
12077c478bd9Sstevel@tonic-gate 					break;
12087c478bd9Sstevel@tonic-gate 				}
12097c478bd9Sstevel@tonic-gate 				(void) fprintf(dst, "%s:", crypt);
12107c478bd9Sstevel@tonic-gate 				debug("\t%d = %s:", i+1, crypt);
12117c478bd9Sstevel@tonic-gate 			}
12127c478bd9Sstevel@tonic-gate 			(void) fprintf(dst, "\n");
12137c478bd9Sstevel@tonic-gate 		} else {
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 			/* copy other users to updated file */
12167c478bd9Sstevel@tonic-gate 			(void) fprintf(dst, "%s:%s", user, last);
12177c478bd9Sstevel@tonic-gate #ifdef	DEBUG
12187c478bd9Sstevel@tonic-gate 			debug("files_update_history: copy line %s",
12197c478bd9Sstevel@tonic-gate 			    user);
12207c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
12217c478bd9Sstevel@tonic-gate 		}
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (found == 0) {
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 		/* user not found, add to history file */
12277c478bd9Sstevel@tonic-gate 		(void) fprintf(dst, "%s:%s:\n", name, spwd->sp_pwdp);
12287c478bd9Sstevel@tonic-gate 		debug("files_update_history: add line\n"
12297c478bd9Sstevel@tonic-gate 		    "\t%s:%s:", name, spwd->sp_pwdp);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	(void) fclose(src);
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	/* If something messed up in file system, loose the update */
12357c478bd9Sstevel@tonic-gate 	if (fclose(dst) != 0) {
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		debug("files_update_history: update file close failed %d",
12387c478bd9Sstevel@tonic-gate 		    errno);
12397c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
12407c478bd9Sstevel@tonic-gate 		return (PWU_CLOSE_FAILED);
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * rename history to ohistory,
12457c478bd9Sstevel@tonic-gate 	 * rename tmp to history,
12467c478bd9Sstevel@tonic-gate 	 * unlink ohistory.
12477c478bd9Sstevel@tonic-gate 	 */
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	(void) unlink(OHISTORY);
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	if (stat64(OHISTORY, &statbuf) == 0 ||
12527c478bd9Sstevel@tonic-gate 	    ((src != NULL) && (link(HISTORY, OHISTORY) != 0)) ||
12537c478bd9Sstevel@tonic-gate 	    rename(HISTEMP, HISTORY) != 0) {
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 		/* old history won't go away, loose the update */
12567c478bd9Sstevel@tonic-gate 		debug("files_update_history: update file rename failed %d",
12577c478bd9Sstevel@tonic-gate 		    errno);
12587c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
12597c478bd9Sstevel@tonic-gate 		return (PWU_UPDATE_FAILED);
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	(void) unlink(OHISTORY);
12637c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
12647c478bd9Sstevel@tonic-gate }
1265