xref: /titanic_44/usr/src/lib/passwdutil/files_attr.c (revision 4a7ceb24cfcc0a97f96d86cfe5852ae445b50e57)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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 /*
22997ec710Sgww  * Copyright 2008 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 
158004388ebScasper 	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 
195004388ebScasper 	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;
229997ec710Sgww 	if (pwbuf == NULL)
230997ec710Sgww 		return (PWU_NOMEM);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * determine which password structure (/etc/passwd or /etc/shadow)
2347c478bd9Sstevel@tonic-gate 	 * we need for the items we need to update
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	for (p = items; p != NULL; p = p->next) {
2377c478bd9Sstevel@tonic-gate 		switch (p->type) {
2387c478bd9Sstevel@tonic-gate 		case ATTR_NAME:
2397c478bd9Sstevel@tonic-gate 		case ATTR_UID:
2407c478bd9Sstevel@tonic-gate 		case ATTR_GID:
2417c478bd9Sstevel@tonic-gate 		case ATTR_AGE:
2427c478bd9Sstevel@tonic-gate 		case ATTR_COMMENT:
2437c478bd9Sstevel@tonic-gate 		case ATTR_GECOS:
2447c478bd9Sstevel@tonic-gate 		case ATTR_HOMEDIR:
2457c478bd9Sstevel@tonic-gate 		case ATTR_SHELL:
2467c478bd9Sstevel@tonic-gate 			if (pwbuf->pwd == NULL) {
2477c478bd9Sstevel@tonic-gate 				pwbuf->pwd = malloc(sizeof (struct passwd));
2487c478bd9Sstevel@tonic-gate 				if (pwbuf->pwd == NULL) {
2497c478bd9Sstevel@tonic-gate 					err = PWU_NOMEM;
2507c478bd9Sstevel@tonic-gate 					goto error;
2517c478bd9Sstevel@tonic-gate 				}
2527c478bd9Sstevel@tonic-gate 			}
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD:
2557c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD_SERVER_POLICY:
2567c478bd9Sstevel@tonic-gate 		case ATTR_LSTCHG:
2577c478bd9Sstevel@tonic-gate 		case ATTR_MIN:
2587c478bd9Sstevel@tonic-gate 		case ATTR_MAX:
2597c478bd9Sstevel@tonic-gate 		case ATTR_WARN:
2607c478bd9Sstevel@tonic-gate 		case ATTR_INACT:
2617c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE:
2627c478bd9Sstevel@tonic-gate 		case ATTR_FLAG:
2637c478bd9Sstevel@tonic-gate 		case ATTR_LOCK_ACCOUNT:
2647c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE_PASSWORD:
2657c478bd9Sstevel@tonic-gate 		case ATTR_FAILED_LOGINS:
2667c478bd9Sstevel@tonic-gate 		case ATTR_INCR_FAILED_LOGINS:
2677c478bd9Sstevel@tonic-gate 		case ATTR_RST_FAILED_LOGINS:
2687c478bd9Sstevel@tonic-gate 		case ATTR_NOLOGIN_ACCOUNT:
2697c478bd9Sstevel@tonic-gate 		case ATTR_UNLOCK_ACCOUNT:
2707c478bd9Sstevel@tonic-gate 			if (pwbuf->spwd == NULL) {
2717c478bd9Sstevel@tonic-gate 				pwbuf->spwd = malloc(sizeof (struct spwd));
2727c478bd9Sstevel@tonic-gate 				if (pwbuf->spwd == NULL) {
2737c478bd9Sstevel@tonic-gate 					err = PWU_NOMEM;
2747c478bd9Sstevel@tonic-gate 					goto error;
2757c478bd9Sstevel@tonic-gate 				}
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		default:
2797c478bd9Sstevel@tonic-gate 			/*
2807c478bd9Sstevel@tonic-gate 			 * Some other repository might have different values
2817c478bd9Sstevel@tonic-gate 			 * so we ignore those.
2827c478bd9Sstevel@tonic-gate 			 */
2837c478bd9Sstevel@tonic-gate 			break;
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) {
2887c478bd9Sstevel@tonic-gate 		if ((pwbuf->pwd_scratch = malloc(PWD_SCRATCH_SIZE)) == NULL) {
2897c478bd9Sstevel@tonic-gate 			err = PWU_NOMEM;
2907c478bd9Sstevel@tonic-gate 			goto error;
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		if (private_getpwnam_r(name, pwbuf->pwd, pwbuf->pwd_scratch,
2937c478bd9Sstevel@tonic-gate 		    PWD_SCRATCH_SIZE) == NULL) {
2947c478bd9Sstevel@tonic-gate 			err = PWU_NOT_FOUND;
2957c478bd9Sstevel@tonic-gate 			goto error;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) {
3007c478bd9Sstevel@tonic-gate 		if ((pwbuf->spwd_scratch = malloc(SPW_SCRATCH_SIZE)) == NULL) {
3017c478bd9Sstevel@tonic-gate 			err = PWU_NOMEM;
3027c478bd9Sstevel@tonic-gate 			goto error;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		if (private_getspnam_r(name, pwbuf->spwd, pwbuf->spwd_scratch,
3057c478bd9Sstevel@tonic-gate 		    SPW_SCRATCH_SIZE) == NULL) {
3067c478bd9Sstevel@tonic-gate 			err = PWU_NOT_FOUND;
3077c478bd9Sstevel@tonic-gate 			goto error;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
3127c478bd9Sstevel@tonic-gate error:
3137c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) free(pwbuf->pwd);
3147c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch);
3157c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) free(pwbuf->spwd);
3167c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch);
3177c478bd9Sstevel@tonic-gate 	free(pwbuf);
3187c478bd9Sstevel@tonic-gate 	*buf = NULL;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (err);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * int files_user_to_authenticate(name, rep, auth_user, privileged)
3257c478bd9Sstevel@tonic-gate  * Determine which user needs to be authenticated. For files, the
3267c478bd9Sstevel@tonic-gate  * possible return values are:
3277c478bd9Sstevel@tonic-gate  * 	PWU_NOT_FOUND
3287c478bd9Sstevel@tonic-gate  *	PWU_SUCCESS	and (auth_user == NULL || auth_user = user)
3297c478bd9Sstevel@tonic-gate  *	PWU_DENIED
330*4a7ceb24Sjjj  *	PWU_NOMEM
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3337c478bd9Sstevel@tonic-gate int
3347c478bd9Sstevel@tonic-gate files_user_to_authenticate(char *user, pwu_repository_t *rep,
3357c478bd9Sstevel@tonic-gate 	char **auth_user, int *privileged)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf;
3387c478bd9Sstevel@tonic-gate 	int res;
3397c478bd9Sstevel@tonic-gate 	attrlist attr_tmp[1] = { { ATTR_UID, NULL, NULL } };
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* check to see if target user is present in files */
3427c478bd9Sstevel@tonic-gate 	res = files_getpwnam(user, &attr_tmp[0], rep, (void **)&pwbuf);
3437c478bd9Sstevel@tonic-gate 	if (res != PWU_SUCCESS)
3447c478bd9Sstevel@tonic-gate 		return (res);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (files_privileged()) {
3477c478bd9Sstevel@tonic-gate 		*auth_user = NULL;
3487c478bd9Sstevel@tonic-gate 		*privileged = 1;
3497c478bd9Sstevel@tonic-gate 		res = PWU_SUCCESS;
3507c478bd9Sstevel@tonic-gate 	} else {
3517c478bd9Sstevel@tonic-gate 		*privileged = 0;
3527c478bd9Sstevel@tonic-gate 		if (getuid() == pwbuf->pwd->pw_uid) {
353*4a7ceb24Sjjj 			if ((*auth_user = strdup(user)) == NULL) {
354*4a7ceb24Sjjj 				res = PWU_NOMEM;
355*4a7ceb24Sjjj 			} else {
3567c478bd9Sstevel@tonic-gate 				res = PWU_SUCCESS;
357*4a7ceb24Sjjj 			}
3587c478bd9Sstevel@tonic-gate 		} else {
3597c478bd9Sstevel@tonic-gate 			res = PWU_DENIED;
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) free(pwbuf->pwd);
3647c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch);
3657c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) free(pwbuf->spwd);
3667c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch);
3677c478bd9Sstevel@tonic-gate 	free(pwbuf);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	return (res);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  *	Password history file format:
3747c478bd9Sstevel@tonic-gate  *		user:crypw1: ... crypwn: such that n <= MAXHISTORY
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate #define	HISTORY		"/etc/security/passhistory"
3777c478bd9Sstevel@tonic-gate #define	HISTEMP		"/etc/security/pwhistemp"
3787c478bd9Sstevel@tonic-gate #define	OHISTORY	"/etc/security/opwhistory"
3797c478bd9Sstevel@tonic-gate #define	HISTMODE	S_IRUSR	/* mode to create history file */
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * XXX
3827c478bd9Sstevel@tonic-gate  *	3*LOGNAME_MAX just in case there are long user names.
3837c478bd9Sstevel@tonic-gate  *	Traditionally Solaris LOGNAME_MAX (_POSIX_LOGIN_NAME_MAX) is 13,
3847c478bd9Sstevel@tonic-gate  *	but some sites often user more.
3857c478bd9Sstevel@tonic-gate  *	If LOGNAME_MAX ever becomes reasonable (128) and actually enforced,
3867c478bd9Sstevel@tonic-gate  *	fix up here.
3877c478bd9Sstevel@tonic-gate  * XXX
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate #define	MAX_LOGNAME (3 * LOGNAME_MAX)
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  *	files_checkhistory - check if a user's new password is in the user's
3937c478bd9Sstevel@tonic-gate  *		old password history.
3947c478bd9Sstevel@tonic-gate  *
3957c478bd9Sstevel@tonic-gate  *	Entry
3967c478bd9Sstevel@tonic-gate  *		user = username.
3977c478bd9Sstevel@tonic-gate  *		passwd = new clear text password.
3987c478bd9Sstevel@tonic-gate  *
3997c478bd9Sstevel@tonic-gate  *	Exit
4007c478bd9Sstevel@tonic-gate  *		PWU_SUCCESS, passwd found in user's old password history.
4017c478bd9Sstevel@tonic-gate  *			The caller should only be interested and fail if
4027c478bd9Sstevel@tonic-gate  *			PWU_SUCCESS is returned.
4037c478bd9Sstevel@tonic-gate  *		PWU_NOT_FOUND, passwd not in user's old password history.
4047c478bd9Sstevel@tonic-gate  *		PWU_errors, PWU_ errors from other routines.
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate int
4087c478bd9Sstevel@tonic-gate files_checkhistory(char *user, char *passwd, pwu_repository_t *rep)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	attrlist attr;
4117c478bd9Sstevel@tonic-gate 	int res;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	attr.type = ATTR_HISTORY;
4147c478bd9Sstevel@tonic-gate 	attr.data.val_s = NULL;
4157c478bd9Sstevel@tonic-gate 	attr.next = NULL;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	debug("files_checkhistory(user=%s)", user);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * XXX
4217c478bd9Sstevel@tonic-gate 	 *	This depends on the underlying files_getattr implementation
4227c478bd9Sstevel@tonic-gate 	 *	treating user not found in backing store or no history as
4237c478bd9Sstevel@tonic-gate 	 *	an error.
4247c478bd9Sstevel@tonic-gate 	 * XXX
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if ((res = files_getattr(user, &attr, rep)) == PWU_SUCCESS) {
4287c478bd9Sstevel@tonic-gate 		char	*s;
4297c478bd9Sstevel@tonic-gate 		char	*crypt_passwd;
4307c478bd9Sstevel@tonic-gate 		int	histsize;
4317c478bd9Sstevel@tonic-gate 		char	*last = attr.data.val_s;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		if ((histsize = def_getint("HISTORY=", DEFHISTORY)) == 0) {
4347c478bd9Sstevel@tonic-gate 			debug("files_checkhistory: no history requested");
4357c478bd9Sstevel@tonic-gate 			res = PWU_NOT_FOUND;
4367c478bd9Sstevel@tonic-gate 			goto out;
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		debug("files_checkhistory: histsize = %d", histsize);
4407c478bd9Sstevel@tonic-gate 		if (histsize > MAXHISTORY)
4417c478bd9Sstevel@tonic-gate 			histsize = MAXHISTORY;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		debug("line to test\n\t%s", last);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		/* compare crypt_passwd to attr.data.val_s strings. */
4467c478bd9Sstevel@tonic-gate 		res = PWU_NOT_FOUND;
4477c478bd9Sstevel@tonic-gate 		while ((histsize-- > 0) &&
4487c478bd9Sstevel@tonic-gate 		    (((s = strtok_r(NULL, ":", &last)) != NULL) &&
4497c478bd9Sstevel@tonic-gate 		    (*s != '\n'))) {
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			crypt_passwd = crypt(passwd, s);
4527c478bd9Sstevel@tonic-gate 			debug("files_checkhistory: user_pw=%s, history_pw=%s",
4537c478bd9Sstevel@tonic-gate 			    crypt_passwd, s);
4547c478bd9Sstevel@tonic-gate 			if (strcmp(crypt_passwd, s) == 0) {
4557c478bd9Sstevel@tonic-gate 				res = PWU_SUCCESS;
4567c478bd9Sstevel@tonic-gate 				break;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		debug("files_checkhistory(%s, %s) = %d", user, crypt_passwd,
4607c478bd9Sstevel@tonic-gate 		    res);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate out:
4637c478bd9Sstevel@tonic-gate 	if (attr.data.val_s != NULL)
4647c478bd9Sstevel@tonic-gate 		free(attr.data.val_s);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	return (res);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate  * files_getattr(name, items, rep)
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * Get attributes specified in list 'items'
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate int
4757c478bd9Sstevel@tonic-gate files_getattr(char *name, attrlist *items, pwu_repository_t *rep)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf;
4787c478bd9Sstevel@tonic-gate 	struct passwd *pw;
4797c478bd9Sstevel@tonic-gate 	struct spwd *spw;
4807c478bd9Sstevel@tonic-gate 	attrlist *w;
4817c478bd9Sstevel@tonic-gate 	int res;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	res = files_getpwnam(name, items, rep, (void **)&pwbuf);
4847c478bd9Sstevel@tonic-gate 	if (res != PWU_SUCCESS)
4857c478bd9Sstevel@tonic-gate 		return (res);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	pw = pwbuf->pwd;
4887c478bd9Sstevel@tonic-gate 	spw = pwbuf->spwd;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	for (w = items; res == PWU_SUCCESS && w != NULL; w = w->next) {
4917c478bd9Sstevel@tonic-gate 		switch (w->type) {
4927c478bd9Sstevel@tonic-gate 		case ATTR_NAME:
4937c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_name)) == NULL)
4947c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
4957c478bd9Sstevel@tonic-gate 			break;
4967c478bd9Sstevel@tonic-gate 		case ATTR_COMMENT:
4977c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_comment)) == NULL)
4987c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
4997c478bd9Sstevel@tonic-gate 			break;
5007c478bd9Sstevel@tonic-gate 		case ATTR_GECOS:
5017c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_gecos)) == NULL)
5027c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5037c478bd9Sstevel@tonic-gate 			break;
5047c478bd9Sstevel@tonic-gate 		case ATTR_HOMEDIR:
5057c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_dir)) == NULL)
5067c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 		case ATTR_SHELL:
5097c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_shell)) == NULL)
5107c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5117c478bd9Sstevel@tonic-gate 			break;
5127c478bd9Sstevel@tonic-gate 		/*
5137c478bd9Sstevel@tonic-gate 		 * Nothing special needs to be done for
5147c478bd9Sstevel@tonic-gate 		 * server policy
5157c478bd9Sstevel@tonic-gate 		 */
5167c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD:
5177c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD_SERVER_POLICY:
5187c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(spw->sp_pwdp)) == NULL)
5197c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5207c478bd9Sstevel@tonic-gate 			break;
5217c478bd9Sstevel@tonic-gate 		case ATTR_AGE:
5227c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup(pw->pw_age)) == NULL)
5237c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5247c478bd9Sstevel@tonic-gate 			break;
5257c478bd9Sstevel@tonic-gate 		case ATTR_REP_NAME:
5267c478bd9Sstevel@tonic-gate 			if ((w->data.val_s = strdup("files")) == NULL)
5277c478bd9Sstevel@tonic-gate 				res = PWU_NOMEM;
5287c478bd9Sstevel@tonic-gate 			break;
5297c478bd9Sstevel@tonic-gate 		case ATTR_HISTORY: {
5307c478bd9Sstevel@tonic-gate 			FILE	*history;
5317c478bd9Sstevel@tonic-gate 			char	buf[MAX_LOGNAME + MAXHISTORY +
5327c478bd9Sstevel@tonic-gate 			    (MAXHISTORY * CRYPT_MAXCIPHERTEXTLEN)+1];
5337c478bd9Sstevel@tonic-gate 			char	*s, *s1;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 			debug("files_getattr: Get password history for %s ",
5367c478bd9Sstevel@tonic-gate 			    name);
5377c478bd9Sstevel@tonic-gate 
538004388ebScasper 			if ((history = fopen(HISTORY, "rF")) == NULL) {
5397c478bd9Sstevel@tonic-gate 				debug("files_getattr: %s not found", HISTORY);
5407c478bd9Sstevel@tonic-gate 				res = PWU_OPEN_FAILED;
5417c478bd9Sstevel@tonic-gate 				goto getattr_exit;
5427c478bd9Sstevel@tonic-gate 			}
5437c478bd9Sstevel@tonic-gate 			res = PWU_NOT_FOUND;
5447c478bd9Sstevel@tonic-gate 			while ((s = fgets(buf, sizeof (buf), history)) !=
5457c478bd9Sstevel@tonic-gate 			    NULL) {
5467c478bd9Sstevel@tonic-gate 				s1 = strchr(s, ':');
5477c478bd9Sstevel@tonic-gate 				if (s1 != NULL) {
5487c478bd9Sstevel@tonic-gate 					*s1 = '\0';
5497c478bd9Sstevel@tonic-gate 				} else {
5507c478bd9Sstevel@tonic-gate 					res = PWU_NOT_FOUND;
5517c478bd9Sstevel@tonic-gate 					break;
5527c478bd9Sstevel@tonic-gate 				}
5537c478bd9Sstevel@tonic-gate #ifdef	DEBUG
5547c478bd9Sstevel@tonic-gate 				debug("got history line for %s", s);
5557c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
5567c478bd9Sstevel@tonic-gate 				if (strcmp(s, name) == 0) {
5577c478bd9Sstevel@tonic-gate 					/* found user */
5587c478bd9Sstevel@tonic-gate 					if ((items->data.val_s =
5597c478bd9Sstevel@tonic-gate 					    strdup(s1+1)) == NULL)
5607c478bd9Sstevel@tonic-gate 						res = PWU_NOMEM;
5617c478bd9Sstevel@tonic-gate 					else
5627c478bd9Sstevel@tonic-gate 						res = PWU_SUCCESS;
5637c478bd9Sstevel@tonic-gate 					break;
5647c478bd9Sstevel@tonic-gate 				}
5657c478bd9Sstevel@tonic-gate 			}
5667c478bd9Sstevel@tonic-gate 			(void) fclose(history);
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		/* integer values */
5717c478bd9Sstevel@tonic-gate 		case ATTR_UID:
5727c478bd9Sstevel@tonic-gate 			w->data.val_i = pw->pw_uid;
5737c478bd9Sstevel@tonic-gate 			break;
5747c478bd9Sstevel@tonic-gate 		case ATTR_GID:
5757c478bd9Sstevel@tonic-gate 			w->data.val_i = pw->pw_gid;
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		case ATTR_LSTCHG:
5787c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_lstchg;
5797c478bd9Sstevel@tonic-gate 			break;
5807c478bd9Sstevel@tonic-gate 		case ATTR_MIN:
5817c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_min;
5827c478bd9Sstevel@tonic-gate 			break;
5837c478bd9Sstevel@tonic-gate 		case ATTR_MAX:
5847c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_max;
5857c478bd9Sstevel@tonic-gate 			break;
5867c478bd9Sstevel@tonic-gate 		case ATTR_WARN:
5877c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_warn;
5887c478bd9Sstevel@tonic-gate 			break;
5897c478bd9Sstevel@tonic-gate 		case ATTR_INACT:
5907c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_inact;
5917c478bd9Sstevel@tonic-gate 			break;
5927c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE:
5937c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_expire;
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 		case ATTR_FLAG:
5967c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_flag;
5977c478bd9Sstevel@tonic-gate 			break;
5987c478bd9Sstevel@tonic-gate 		case ATTR_FAILED_LOGINS:
5997c478bd9Sstevel@tonic-gate 			w->data.val_i = spw->sp_flag & FAILCOUNT_MASK;
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		default:
6027c478bd9Sstevel@tonic-gate 			break;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate getattr_exit:
6077c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) free(pwbuf->pwd);
6087c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch);
6097c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) free(pwbuf->spwd);
610a009b9b5Sps57422 	if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch);
6117c478bd9Sstevel@tonic-gate 	free(pwbuf);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	return (res);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * max_present(list)
6187c478bd9Sstevel@tonic-gate  *
6197c478bd9Sstevel@tonic-gate  * see if attribute ATTR_MAX, with value != -1, is present in
6207c478bd9Sstevel@tonic-gate  * attribute-list "list".
6217c478bd9Sstevel@tonic-gate  *
6227c478bd9Sstevel@tonic-gate  * returns 1 if present, 0 otherwise.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate static int
6257c478bd9Sstevel@tonic-gate max_present(attrlist *list)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	while (list != NULL)
6287c478bd9Sstevel@tonic-gate 		if (list->type == ATTR_MAX && list->data.val_i != -1)
6297c478bd9Sstevel@tonic-gate 			return (1);
6307c478bd9Sstevel@tonic-gate 		else
6317c478bd9Sstevel@tonic-gate 			list = list->next;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	return (0);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * files_update(items, rep, buf)
6387c478bd9Sstevel@tonic-gate  *
6397c478bd9Sstevel@tonic-gate  * update the information in buf with the attributes specified in
6407c478bd9Sstevel@tonic-gate  * items.
6417c478bd9Sstevel@tonic-gate  */
6427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6437c478bd9Sstevel@tonic-gate int
6447c478bd9Sstevel@tonic-gate files_update(attrlist *items, pwu_repository_t *rep, void *buf)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf = (struct pwbuf *)buf;
6477c478bd9Sstevel@tonic-gate 	struct passwd *pw;
6487c478bd9Sstevel@tonic-gate 	struct spwd *spw;
6497c478bd9Sstevel@tonic-gate 	attrlist *p;
6507c478bd9Sstevel@tonic-gate 	int aging_needed = 0;
6517c478bd9Sstevel@tonic-gate 	int aging_set = 0;
6527c478bd9Sstevel@tonic-gate 	int disable_aging;
6537c478bd9Sstevel@tonic-gate 	char *pword;
6547c478bd9Sstevel@tonic-gate 	int len;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	pw = pwbuf->pwd;
6577c478bd9Sstevel@tonic-gate 	spw = pwbuf->spwd;
6587c478bd9Sstevel@tonic-gate 	pwbuf->update_history = 0;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * if sp_max==0 : disable passwd aging after updating the password
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 	disable_aging = (spw != NULL && spw->sp_max == 0);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	for (p = items; p != NULL; p = p->next) {
6667c478bd9Sstevel@tonic-gate 		switch (p->type) {
6677c478bd9Sstevel@tonic-gate 		case ATTR_NAME:
6687c478bd9Sstevel@tonic-gate 			break;	/* We are able to handle this, but... */
6697c478bd9Sstevel@tonic-gate 		case ATTR_UID:
6707c478bd9Sstevel@tonic-gate 			pw->pw_uid = (uid_t)p->data.val_i;
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 		case ATTR_GID:
6737c478bd9Sstevel@tonic-gate 			pw->pw_gid = (gid_t)p->data.val_i;
6747c478bd9Sstevel@tonic-gate 			break;
6757c478bd9Sstevel@tonic-gate 		case ATTR_AGE:
6767c478bd9Sstevel@tonic-gate 			pw->pw_age = p->data.val_s;
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 		case ATTR_COMMENT:
6797c478bd9Sstevel@tonic-gate 			pw->pw_comment = p->data.val_s;
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 		case ATTR_GECOS:
6827c478bd9Sstevel@tonic-gate 			pw->pw_gecos = p->data.val_s;
6837c478bd9Sstevel@tonic-gate 			break;
6847c478bd9Sstevel@tonic-gate 		case ATTR_HOMEDIR:
6857c478bd9Sstevel@tonic-gate 			pw->pw_dir = p->data.val_s;
6867c478bd9Sstevel@tonic-gate 			break;
6877c478bd9Sstevel@tonic-gate 		case ATTR_SHELL:
6887c478bd9Sstevel@tonic-gate 			pw->pw_shell = p->data.val_s;
6897c478bd9Sstevel@tonic-gate 			break;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * Nothing special needs to be done for
6937c478bd9Sstevel@tonic-gate 		 * server policy
6947c478bd9Sstevel@tonic-gate 		 */
6957c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD:
6967c478bd9Sstevel@tonic-gate 		case ATTR_PASSWD_SERVER_POLICY:
6977c478bd9Sstevel@tonic-gate 			/*
6987c478bd9Sstevel@tonic-gate 			 * There is a special case only for files: if the
6997c478bd9Sstevel@tonic-gate 			 * password is to be deleted (-d to passwd),
7007c478bd9Sstevel@tonic-gate 			 * p->data.val_s will be NULL.
7017c478bd9Sstevel@tonic-gate 			 */
7027c478bd9Sstevel@tonic-gate 			if (p->data.val_s == NULL) {
7037c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = "";
7047c478bd9Sstevel@tonic-gate 			} else {
7057c478bd9Sstevel@tonic-gate 				char *salt = NULL;
7067c478bd9Sstevel@tonic-gate 				char *hash = NULL;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 				salt = crypt_gensalt(spw->sp_pwdp, pw);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 				if (salt == NULL) {
7117c478bd9Sstevel@tonic-gate 					if (errno == ENOMEM)
7127c478bd9Sstevel@tonic-gate 						return (PWU_NOMEM);
7137c478bd9Sstevel@tonic-gate 					/* algorithm problem? */
7147c478bd9Sstevel@tonic-gate 					syslog(LOG_AUTH | LOG_ALERT,
7157c478bd9Sstevel@tonic-gate 					    "passwdutil: crypt_gensalt %m");
7167c478bd9Sstevel@tonic-gate 					return (PWU_UPDATE_FAILED);
7177c478bd9Sstevel@tonic-gate 				}
7187c478bd9Sstevel@tonic-gate 				hash = crypt(p->data.val_s, salt);
7197c478bd9Sstevel@tonic-gate 				free(salt);
7207c478bd9Sstevel@tonic-gate 				if (hash == NULL) {
7217c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
7227c478bd9Sstevel@tonic-gate 					return (PWU_NOMEM);
7237c478bd9Sstevel@tonic-gate 				}
7247c478bd9Sstevel@tonic-gate 				pword = strdup(hash);
7257c478bd9Sstevel@tonic-gate 				if (pword == NULL) {
7267c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
7277c478bd9Sstevel@tonic-gate 					return (PWU_NOMEM);
7287c478bd9Sstevel@tonic-gate 				}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 				if (pwbuf->new_sp_pwdp)
7317c478bd9Sstevel@tonic-gate 					free(pwbuf->new_sp_pwdp);
7327c478bd9Sstevel@tonic-gate 				pwbuf->new_sp_pwdp = pword;
7337c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = pword;
7347c478bd9Sstevel@tonic-gate 				aging_needed = 1;
7357c478bd9Sstevel@tonic-gate 				pwbuf->update_history = 1;
7367c478bd9Sstevel@tonic-gate 			}
7377c478bd9Sstevel@tonic-gate 			spw->sp_flag &= ~FAILCOUNT_MASK; /* reset count */
7387c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7397c478bd9Sstevel@tonic-gate 			break;
7407c478bd9Sstevel@tonic-gate 		case ATTR_LOCK_ACCOUNT:
7417c478bd9Sstevel@tonic-gate 			if (spw->sp_pwdp == NULL) {
7427c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = LOCKSTRING;
7437c478bd9Sstevel@tonic-gate 			} else if (strncmp(spw->sp_pwdp, LOCKSTRING,
7447c478bd9Sstevel@tonic-gate 			    sizeof (LOCKSTRING)-1) != 0) {
7457c478bd9Sstevel@tonic-gate 				len = sizeof (LOCKSTRING)-1 +
7467c478bd9Sstevel@tonic-gate 				    strlen(spw->sp_pwdp) + 1;
7477c478bd9Sstevel@tonic-gate 				pword = malloc(len);
7487c478bd9Sstevel@tonic-gate 				if (pword == NULL) {
7497c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
7507c478bd9Sstevel@tonic-gate 					return (PWU_NOMEM);
7517c478bd9Sstevel@tonic-gate 				}
7527c478bd9Sstevel@tonic-gate 				(void) strlcpy(pword, LOCKSTRING, len);
7537c478bd9Sstevel@tonic-gate 				(void) strlcat(pword, spw->sp_pwdp, len);
7547c478bd9Sstevel@tonic-gate 				if (pwbuf->new_sp_pwdp)
7557c478bd9Sstevel@tonic-gate 					free(pwbuf->new_sp_pwdp);
7567c478bd9Sstevel@tonic-gate 				pwbuf->new_sp_pwdp = pword;
7577c478bd9Sstevel@tonic-gate 				spw->sp_pwdp = pword;
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7607c478bd9Sstevel@tonic-gate 			break;
7617c478bd9Sstevel@tonic-gate 		case ATTR_UNLOCK_ACCOUNT:
7627c478bd9Sstevel@tonic-gate 			if (spw->sp_pwdp != NULL &&
7637c478bd9Sstevel@tonic-gate 			    strncmp(spw->sp_pwdp, LOCKSTRING,
7647c478bd9Sstevel@tonic-gate 			    sizeof (LOCKSTRING)-1) == 0) {
7657c478bd9Sstevel@tonic-gate 				(void) strcpy(spw->sp_pwdp, spw->sp_pwdp +
7667c478bd9Sstevel@tonic-gate 				    sizeof (LOCKSTRING)-1);
7677c478bd9Sstevel@tonic-gate 			}
7687c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		case ATTR_NOLOGIN_ACCOUNT:
7717c478bd9Sstevel@tonic-gate 			spw->sp_pwdp = NOLOGINSTRING;
7727c478bd9Sstevel@tonic-gate 			if (pwbuf->new_sp_pwdp) {
7737c478bd9Sstevel@tonic-gate 				free(pwbuf->new_sp_pwdp);
7747c478bd9Sstevel@tonic-gate 				pwbuf->new_sp_pwdp = NULL;
7757c478bd9Sstevel@tonic-gate 			}
7767c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = DAY_NOW_32;
7777c478bd9Sstevel@tonic-gate 			break;
7787c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE_PASSWORD:
7797c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = 0;
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 		case ATTR_LSTCHG:
7827c478bd9Sstevel@tonic-gate 			spw->sp_lstchg = p->data.val_i;
7837c478bd9Sstevel@tonic-gate 			break;
7847c478bd9Sstevel@tonic-gate 		case ATTR_MIN:
7857c478bd9Sstevel@tonic-gate 			if (spw->sp_max == -1 &&
7867c478bd9Sstevel@tonic-gate 			    p->data.val_i != -1 && max_present(p->next) == 0)
7877c478bd9Sstevel@tonic-gate 				return (PWU_AGING_DISABLED);
7887c478bd9Sstevel@tonic-gate 			spw->sp_min = p->data.val_i;
7897c478bd9Sstevel@tonic-gate 			aging_set = 1;
7907c478bd9Sstevel@tonic-gate 			break;
7917c478bd9Sstevel@tonic-gate 		case ATTR_MAX:
7927c478bd9Sstevel@tonic-gate 			if (p->data.val_i == -1) {
7937c478bd9Sstevel@tonic-gate 				/* Turn aging off -> Reset min and warn too */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 				spw->sp_min = -1;
7967c478bd9Sstevel@tonic-gate 				spw->sp_warn = -1;
7977c478bd9Sstevel@tonic-gate 			} else {
7987c478bd9Sstevel@tonic-gate 				/* Turn aging on */
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 				if (spw->sp_min == -1) {
8017c478bd9Sstevel@tonic-gate 					/*
8027c478bd9Sstevel@tonic-gate 					 * If minage has not been set with
8037c478bd9Sstevel@tonic-gate 					 * a command-line option, we set it
8047c478bd9Sstevel@tonic-gate 					 * to zero.
8057c478bd9Sstevel@tonic-gate 					 */
8067c478bd9Sstevel@tonic-gate 					spw->sp_min = 0;
8077c478bd9Sstevel@tonic-gate 				}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 				/*
8107c478bd9Sstevel@tonic-gate 				 * If aging was turned off, we update lstchg.
8117c478bd9Sstevel@tonic-gate 				 *
8127c478bd9Sstevel@tonic-gate 				 * We take care not to update lstchg if the
8137c478bd9Sstevel@tonic-gate 				 * user has no password, otherwise the user
8147c478bd9Sstevel@tonic-gate 				 * might not be required to provide a password
8157c478bd9Sstevel@tonic-gate 				 * the next time [s]he logs-in.
8167c478bd9Sstevel@tonic-gate 				 *
8177c478bd9Sstevel@tonic-gate 				 * Also, if lstchg != -1 (i.e., not set in
8187c478bd9Sstevel@tonic-gate 				 * /etc/shadow), we keep the old value.
8197c478bd9Sstevel@tonic-gate 				 */
8207c478bd9Sstevel@tonic-gate 				if (spw->sp_max == -1 &&
8217c478bd9Sstevel@tonic-gate 				    spw->sp_pwdp != NULL && *spw->sp_pwdp &&
8227c478bd9Sstevel@tonic-gate 				    spw->sp_lstchg == -1) {
8237c478bd9Sstevel@tonic-gate 					spw->sp_lstchg = DAY_NOW_32;
8247c478bd9Sstevel@tonic-gate 				}
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			spw->sp_max = p->data.val_i;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 			aging_set = 1;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case ATTR_WARN:
8337c478bd9Sstevel@tonic-gate 			if (spw->sp_max == -1 && p->data.val_i != -1 &&
8347c478bd9Sstevel@tonic-gate 			    max_present(p->next) == 0)
8357c478bd9Sstevel@tonic-gate 				return (PWU_AGING_DISABLED);
8367c478bd9Sstevel@tonic-gate 			spw->sp_warn =  p->data.val_i;
8377c478bd9Sstevel@tonic-gate 			break;
8387c478bd9Sstevel@tonic-gate 		case ATTR_INACT:
8397c478bd9Sstevel@tonic-gate 			spw->sp_inact = p->data.val_i;
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 		case ATTR_EXPIRE:
8427c478bd9Sstevel@tonic-gate 			spw->sp_expire = p->data.val_i;
8437c478bd9Sstevel@tonic-gate 			break;
8447c478bd9Sstevel@tonic-gate 		case ATTR_FLAG:
8457c478bd9Sstevel@tonic-gate 			spw->sp_flag = p->data.val_i;
8467c478bd9Sstevel@tonic-gate 			break;
8477c478bd9Sstevel@tonic-gate 		case ATTR_INCR_FAILED_LOGINS:
8487c478bd9Sstevel@tonic-gate 			{
8497c478bd9Sstevel@tonic-gate 			int count = (spw->sp_flag & FAILCOUNT_MASK) + 1;
8507c478bd9Sstevel@tonic-gate 			spw->sp_flag &= ~FAILCOUNT_MASK;
8517c478bd9Sstevel@tonic-gate 			spw->sp_flag |= min(FAILCOUNT_MASK, count);
8527c478bd9Sstevel@tonic-gate 			p->data.val_i = count;
8537c478bd9Sstevel@tonic-gate 			}
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		case ATTR_RST_FAILED_LOGINS:
8567c478bd9Sstevel@tonic-gate 			p->data.val_i = spw->sp_flag & FAILCOUNT_MASK;
8577c478bd9Sstevel@tonic-gate 			spw->sp_flag &= ~FAILCOUNT_MASK;
8587c478bd9Sstevel@tonic-gate 			break;
8597c478bd9Sstevel@tonic-gate 		default:
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/*
8657c478bd9Sstevel@tonic-gate 	 * What should the new aging values look like?
8667c478bd9Sstevel@tonic-gate 	 *
8677c478bd9Sstevel@tonic-gate 	 * There are a number of different conditions
8687c478bd9Sstevel@tonic-gate 	 *
8697c478bd9Sstevel@tonic-gate 	 *  a) aging is already configured: don't touch it
8707c478bd9Sstevel@tonic-gate 	 *
8717c478bd9Sstevel@tonic-gate 	 *  b) disable_aging is set: disable aging
8727c478bd9Sstevel@tonic-gate 	 *
8737c478bd9Sstevel@tonic-gate 	 *  c) aging is not configured: turn on default aging;
8747c478bd9Sstevel@tonic-gate 	 *
8757c478bd9Sstevel@tonic-gate 	 *  b) and c) of course only if aging_needed and !aging_set.
8767c478bd9Sstevel@tonic-gate 	 *  (i.e., password changed, and aging values not changed)
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if (spw != NULL && spw->sp_max <= 0) {
8807c478bd9Sstevel@tonic-gate 		/* a) aging not yet configured */
8817c478bd9Sstevel@tonic-gate 		if (aging_needed && !aging_set) {
8827c478bd9Sstevel@tonic-gate 			if (disable_aging) {
8837c478bd9Sstevel@tonic-gate 				/* b) turn off aging */
8847c478bd9Sstevel@tonic-gate 				spw->sp_min = spw->sp_max = spw->sp_warn = -1;
8857c478bd9Sstevel@tonic-gate 			} else {
8867c478bd9Sstevel@tonic-gate 				/* c) */
8877c478bd9Sstevel@tonic-gate 				turn_on_default_aging(spw);
8887c478bd9Sstevel@tonic-gate 			}
8897c478bd9Sstevel@tonic-gate 		}
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate  * files_update_shadow(char *name, struct spwd *spwd)
8977c478bd9Sstevel@tonic-gate  *
8987c478bd9Sstevel@tonic-gate  * update the shadow password file SHADOW to contain the spwd structure
8997c478bd9Sstevel@tonic-gate  * "spwd" for user "name"
9007c478bd9Sstevel@tonic-gate  */
9017c478bd9Sstevel@tonic-gate int
9027c478bd9Sstevel@tonic-gate files_update_shadow(char *name, struct spwd *spwd)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate 	struct stat64 stbuf;
9057c478bd9Sstevel@tonic-gate 	FILE *dst;
9067c478bd9Sstevel@tonic-gate 	FILE *src;
9077c478bd9Sstevel@tonic-gate 	struct spwd cur;
9087c478bd9Sstevel@tonic-gate 	char buf[SPW_SCRATCH_SIZE];
9097c478bd9Sstevel@tonic-gate 	int tempfd;
9107c478bd9Sstevel@tonic-gate 	mode_t filemode;
9117c478bd9Sstevel@tonic-gate 	int result = -1;
9127c478bd9Sstevel@tonic-gate 	int err = PWU_SUCCESS;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* Mode of the shadow file should be 400 or 000 */
9157c478bd9Sstevel@tonic-gate 	if (stat64(SHADOW, &stbuf) < 0) {
9167c478bd9Sstevel@tonic-gate 		err = PWU_STAT_FAILED;
9177c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/* copy mode from current shadow file (0400 or 0000) */
9217c478bd9Sstevel@tonic-gate 	filemode = stbuf.st_mode & S_IRUSR;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/*
9247c478bd9Sstevel@tonic-gate 	 * we can't specify filemodes to fopen(), and we SHOULD NOT
9257c478bd9Sstevel@tonic-gate 	 * set umask in multi-thread safe libraries, so we use
9267c478bd9Sstevel@tonic-gate 	 * a combination of open() and fdopen()
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	tempfd = open(SHADTEMP, O_WRONLY|O_CREAT|O_TRUNC, filemode);
9297c478bd9Sstevel@tonic-gate 	if (tempfd < 0) {
9307c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
9317c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 	(void) fchown(tempfd, (uid_t)0, stbuf.st_gid);
9347c478bd9Sstevel@tonic-gate 
935004388ebScasper 	if ((dst = fdopen(tempfd, "wF")) == NULL) {
9367c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
9377c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 
940004388ebScasper 	if ((src = fopen(SHADOW, "rF")) == NULL) {
9417c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
9427c478bd9Sstevel@tonic-gate 		(void) fclose(dst);
9437c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9447c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * copy old shadow to temporary file while replacing the entry
9497c478bd9Sstevel@tonic-gate 	 * that matches "name".
9507c478bd9Sstevel@tonic-gate 	 */
9517c478bd9Sstevel@tonic-gate 	while (fgetspent_r(src, &cur, buf, sizeof (buf)) != NULL) {
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		if (strcmp(cur.sp_namp, name) == 0)
9547c478bd9Sstevel@tonic-gate 			result = putspent(spwd, dst);
9557c478bd9Sstevel@tonic-gate 		else
9567c478bd9Sstevel@tonic-gate 			result = putspent(&cur, dst);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		if (result != 0) {
9597c478bd9Sstevel@tonic-gate 			err = PWU_WRITE_FAILED;
9607c478bd9Sstevel@tonic-gate 			(void) fclose(src);
9617c478bd9Sstevel@tonic-gate 			(void) fclose(dst);
9627c478bd9Sstevel@tonic-gate 			goto shadow_exit;
9637c478bd9Sstevel@tonic-gate 		}
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	(void) fclose(src);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (fclose(dst) != 0) {
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * Something went wrong (ENOSPC for example). Don't
9717c478bd9Sstevel@tonic-gate 		 * use the resulting temporary file!
9727c478bd9Sstevel@tonic-gate 		 */
9737c478bd9Sstevel@tonic-gate 		err = PWU_CLOSE_FAILED;
9747c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9757c478bd9Sstevel@tonic-gate 		goto shadow_exit;
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Rename stmp to shadow:
9807c478bd9Sstevel@tonic-gate 	 *   1. make sure /etc/oshadow is gone
9817c478bd9Sstevel@tonic-gate 	 *   2. ln /etc/shadow /etc/oshadow
9827c478bd9Sstevel@tonic-gate 	 *   3. mv /etc/stmp /etc/shadow
9837c478bd9Sstevel@tonic-gate 	 */
9847c478bd9Sstevel@tonic-gate 	if (unlink(OSHADOW) && access(OSHADOW, 0) == 0) {
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 (link(SHADOW, OSHADOW) == -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 
9967c478bd9Sstevel@tonic-gate 	if (rename(SHADTEMP, SHADOW) == -1) {
9977c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
9987c478bd9Sstevel@tonic-gate 		(void) unlink(SHADTEMP);
9997c478bd9Sstevel@tonic-gate 		goto shadow_exit;
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 	(void) unlink(OSHADOW);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate shadow_exit:
10047c478bd9Sstevel@tonic-gate 	return (err);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate int
10087c478bd9Sstevel@tonic-gate files_update_passwd(char *name, struct passwd *pwd)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 	struct stat64 stbuf;
10117c478bd9Sstevel@tonic-gate 	FILE *src, *dst;
10127c478bd9Sstevel@tonic-gate 	int tempfd;
10137c478bd9Sstevel@tonic-gate 	struct passwd cur;
10147c478bd9Sstevel@tonic-gate 	char buf[PWD_SCRATCH_SIZE];
10157c478bd9Sstevel@tonic-gate 	int result;
10167c478bd9Sstevel@tonic-gate 	int err = PWU_SUCCESS;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if (stat64(PASSWD, &stbuf) < 0) {
10197c478bd9Sstevel@tonic-gate 		err = PWU_STAT_FAILED;
10207c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/* see files_update_shadow() for open()+fdopen() rationale */
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if ((tempfd = open(PASSTEMP, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
10267c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
10277c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10287c478bd9Sstevel@tonic-gate 	}
1029004388ebScasper 	if ((dst = fdopen(tempfd, "wF")) == NULL) {
10307c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
10317c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10327c478bd9Sstevel@tonic-gate 	}
1033004388ebScasper 	if ((src = fopen(PASSWD, "rF")) == NULL) {
10347c478bd9Sstevel@tonic-gate 		err = PWU_OPEN_FAILED;
10357c478bd9Sstevel@tonic-gate 		(void) fclose(dst);
10367c478bd9Sstevel@tonic-gate 		(void) unlink(PASSTEMP);
10377c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * copy old password entries to temporary file while replacing
10427c478bd9Sstevel@tonic-gate 	 * the entry that matches "name"
10437c478bd9Sstevel@tonic-gate 	 */
10447c478bd9Sstevel@tonic-gate 	while (fgetpwent_r(src, &cur, buf, sizeof (buf)) != NULL) {
10457c478bd9Sstevel@tonic-gate 		if (strcmp(cur.pw_name, name) == 0)
10467c478bd9Sstevel@tonic-gate 			result = putpwent(pwd, dst);
10477c478bd9Sstevel@tonic-gate 		else
10487c478bd9Sstevel@tonic-gate 			result = putpwent(&cur, dst);
10497c478bd9Sstevel@tonic-gate 		if (result != 0) {
10507c478bd9Sstevel@tonic-gate 			err = PWU_WRITE_FAILED;
10517c478bd9Sstevel@tonic-gate 			(void) fclose(src);
10527c478bd9Sstevel@tonic-gate 			(void) fclose(dst);
10537c478bd9Sstevel@tonic-gate 			goto passwd_exit;
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	(void) fclose(src);
10587c478bd9Sstevel@tonic-gate 	if (fclose(dst) != 0) {
10597c478bd9Sstevel@tonic-gate 		err = PWU_CLOSE_FAILED;
10607c478bd9Sstevel@tonic-gate 		goto passwd_exit; /* Don't trust the temporary file */
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/* Rename temp to passwd */
10647c478bd9Sstevel@tonic-gate 	if (unlink(OPASSWD) && access(OPASSWD, 0) == 0) {
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 (link(PASSWD, OPASSWD) == -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 	if (rename(PASSTEMP, PASSWD) == -1) {
10777c478bd9Sstevel@tonic-gate 		err = PWU_UPDATE_FAILED;
10787c478bd9Sstevel@tonic-gate 		(void) unlink(PASSTEMP);
10797c478bd9Sstevel@tonic-gate 		goto passwd_exit;
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	(void) chmod(PASSWD, 0644);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate passwd_exit:
10857c478bd9Sstevel@tonic-gate 	return (err);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate /*
10907c478bd9Sstevel@tonic-gate  * files_putpwnam(name, oldpw, dummy, rep, buf)
10917c478bd9Sstevel@tonic-gate  *
10927c478bd9Sstevel@tonic-gate  * store the password attributes contained in "buf" in /etc/passwd and
10937c478bd9Sstevel@tonic-gate  * /etc/shadow. The dummy parameter is a placeholder for NIS+
10947c478bd9Sstevel@tonic-gate  * updates where the "oldrpc" password is passed.
10957c478bd9Sstevel@tonic-gate  */
10967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10977c478bd9Sstevel@tonic-gate int
10987c478bd9Sstevel@tonic-gate files_putpwnam(char *name, char *oldpw, char *dummy,
10997c478bd9Sstevel@tonic-gate     pwu_repository_t *rep, void *buf)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	struct pwbuf *pwbuf = (struct pwbuf *)buf;
11027c478bd9Sstevel@tonic-gate 	int result = PWU_SUCCESS;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) {
11057c478bd9Sstevel@tonic-gate 		result = files_update_passwd(name, pwbuf->pwd);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (result == PWU_SUCCESS && pwbuf->spwd) {
11097c478bd9Sstevel@tonic-gate 		if (pwbuf->update_history != 0) {
11107c478bd9Sstevel@tonic-gate 			debug("update_history = %d", pwbuf->update_history);
11117c478bd9Sstevel@tonic-gate 			result = files_update_history(name, pwbuf->spwd);
11127c478bd9Sstevel@tonic-gate 		} else {
11137c478bd9Sstevel@tonic-gate 			debug("no password change");
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 		if (result == PWU_SUCCESS) {
11167c478bd9Sstevel@tonic-gate 			result = files_update_shadow(name, pwbuf->spwd);
11177c478bd9Sstevel@tonic-gate 		}
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	if (pwbuf->pwd) {
11217c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->pwd, 0, sizeof (struct passwd));
11227c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->pwd_scratch, 0, PWD_SCRATCH_SIZE);
11237c478bd9Sstevel@tonic-gate 		free(pwbuf->pwd);
11247c478bd9Sstevel@tonic-gate 		free(pwbuf->pwd_scratch);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 	if (pwbuf->spwd) {
11277c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->spwd, 0, sizeof (struct spwd));
11287c478bd9Sstevel@tonic-gate 		(void) memset(pwbuf->spwd_scratch, 0, SPW_SCRATCH_SIZE);
11297c478bd9Sstevel@tonic-gate 		free(pwbuf->spwd);
11307c478bd9Sstevel@tonic-gate 		free(pwbuf->spwd_scratch);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	if (pwbuf->new_sp_pwdp) {
11337c478bd9Sstevel@tonic-gate 		free(pwbuf->new_sp_pwdp);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	return (result);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate /*
11407c478bd9Sstevel@tonic-gate  *	NOTE:  This is all covered under the repository lock held for updating
11417c478bd9Sstevel@tonic-gate  *	passwd(4) and shadow(4).
11427c478bd9Sstevel@tonic-gate  */
11437c478bd9Sstevel@tonic-gate int
11447c478bd9Sstevel@tonic-gate files_update_history(char *name, struct spwd *spwd)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	int	histsize;
11477c478bd9Sstevel@tonic-gate 	int	tmpfd;
11487c478bd9Sstevel@tonic-gate 	FILE	*src;	/* history database file */
11497c478bd9Sstevel@tonic-gate 	FILE	*dst;	/* temp history database being updated */
11507c478bd9Sstevel@tonic-gate 	struct	stat64 statbuf;
11517c478bd9Sstevel@tonic-gate 	char buf[MAX_LOGNAME + MAXHISTORY +
11527c478bd9Sstevel@tonic-gate 	    (MAXHISTORY * CRYPT_MAXCIPHERTEXTLEN)+1];
11537c478bd9Sstevel@tonic-gate 	int	found;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	if ((histsize = def_getint("HISTORY=", DEFHISTORY)) == 0) {
11567c478bd9Sstevel@tonic-gate 		debug("files_update_history(%s) no history, unlinking", name);
11577c478bd9Sstevel@tonic-gate 		(void) unlink(HISTORY);
11587c478bd9Sstevel@tonic-gate 		return (PWU_SUCCESS);	/* no history update defined */
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	debug("files_update_history(%s, %s) histsize = %d", name, spwd->sp_pwdp,
11617c478bd9Sstevel@tonic-gate 	    histsize);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (histsize > MAXHISTORY)
11647c478bd9Sstevel@tonic-gate 		histsize = MAXHISTORY;
11657c478bd9Sstevel@tonic-gate 	if ((tmpfd = open(HISTEMP, O_WRONLY|O_CREAT|O_TRUNC, HISTMODE)) < 0) {
11667c478bd9Sstevel@tonic-gate 		return (PWU_OPEN_FAILED);
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 	(void) fchown(tmpfd, (uid_t)0, (gid_t)0);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/* get ready to copy */
1171004388ebScasper 	if (((src = fopen(HISTORY, "rF")) == NULL) &&
11727c478bd9Sstevel@tonic-gate 	    (errno != ENOENT)) {
11737c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
11747c478bd9Sstevel@tonic-gate 		return (PWU_OPEN_FAILED);
11757c478bd9Sstevel@tonic-gate 	}
1176004388ebScasper 	if ((dst = fdopen(tmpfd, "wF")) == NULL) {
11777c478bd9Sstevel@tonic-gate 		(void) fclose(src);
11787c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
11797c478bd9Sstevel@tonic-gate 		return (PWU_OPEN_FAILED);
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/* Copy and update if found.  Add if not found. */
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	found = 0;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	while ((src != NULL) &&
11877c478bd9Sstevel@tonic-gate 	    (fgets(buf, sizeof (buf), src) != NULL)) {
11887c478bd9Sstevel@tonic-gate 		char	*user;
11897c478bd9Sstevel@tonic-gate 		char	*last;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		/* get username field */
11927c478bd9Sstevel@tonic-gate 		user = strtok_r(buf, ":", &last);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate #ifdef	DEBUG
11957c478bd9Sstevel@tonic-gate 		debug("files_update_history: read=\"%s\"", user);
11967c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		if (strcmp(user, name) == 0) {
11997c478bd9Sstevel@tonic-gate 			char	*crypt;
12007c478bd9Sstevel@tonic-gate 			int	i;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 			/* found user, update */
12037c478bd9Sstevel@tonic-gate 			found++;
12047c478bd9Sstevel@tonic-gate 			(void) fprintf(dst, "%s:%s:", name, spwd->sp_pwdp);
12057c478bd9Sstevel@tonic-gate 			debug("files_update_history: update user\n"
12067c478bd9Sstevel@tonic-gate 			    "\t%s:%s:", name, spwd->sp_pwdp);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 			/* get old crypted password history */
12097c478bd9Sstevel@tonic-gate 			for (i = 0; i < MAXHISTORY-1; i++) {
12107c478bd9Sstevel@tonic-gate 				crypt = strtok_r(NULL, ":", &last);
12117c478bd9Sstevel@tonic-gate 				if (crypt == NULL ||
12127c478bd9Sstevel@tonic-gate 				    *crypt == '\n') {
12137c478bd9Sstevel@tonic-gate 					break;
12147c478bd9Sstevel@tonic-gate 				}
12157c478bd9Sstevel@tonic-gate 				(void) fprintf(dst, "%s:", crypt);
12167c478bd9Sstevel@tonic-gate 				debug("\t%d = %s:", i+1, crypt);
12177c478bd9Sstevel@tonic-gate 			}
12187c478bd9Sstevel@tonic-gate 			(void) fprintf(dst, "\n");
12197c478bd9Sstevel@tonic-gate 		} else {
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 			/* copy other users to updated file */
12227c478bd9Sstevel@tonic-gate 			(void) fprintf(dst, "%s:%s", user, last);
12237c478bd9Sstevel@tonic-gate #ifdef	DEBUG
12247c478bd9Sstevel@tonic-gate 			debug("files_update_history: copy line %s",
12257c478bd9Sstevel@tonic-gate 			    user);
12267c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	if (found == 0) {
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		/* user not found, add to history file */
12337c478bd9Sstevel@tonic-gate 		(void) fprintf(dst, "%s:%s:\n", name, spwd->sp_pwdp);
12347c478bd9Sstevel@tonic-gate 		debug("files_update_history: add line\n"
12357c478bd9Sstevel@tonic-gate 		    "\t%s:%s:", name, spwd->sp_pwdp);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	(void) fclose(src);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/* If something messed up in file system, loose the update */
12417c478bd9Sstevel@tonic-gate 	if (fclose(dst) != 0) {
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		debug("files_update_history: update file close failed %d",
12447c478bd9Sstevel@tonic-gate 		    errno);
12457c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
12467c478bd9Sstevel@tonic-gate 		return (PWU_CLOSE_FAILED);
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	/*
12507c478bd9Sstevel@tonic-gate 	 * rename history to ohistory,
12517c478bd9Sstevel@tonic-gate 	 * rename tmp to history,
12527c478bd9Sstevel@tonic-gate 	 * unlink ohistory.
12537c478bd9Sstevel@tonic-gate 	 */
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	(void) unlink(OHISTORY);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if (stat64(OHISTORY, &statbuf) == 0 ||
12587c478bd9Sstevel@tonic-gate 	    ((src != NULL) && (link(HISTORY, OHISTORY) != 0)) ||
12597c478bd9Sstevel@tonic-gate 	    rename(HISTEMP, HISTORY) != 0) {
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 		/* old history won't go away, loose the update */
12627c478bd9Sstevel@tonic-gate 		debug("files_update_history: update file rename failed %d",
12637c478bd9Sstevel@tonic-gate 		    errno);
12647c478bd9Sstevel@tonic-gate 		(void) unlink(HISTEMP);
12657c478bd9Sstevel@tonic-gate 		return (PWU_UPDATE_FAILED);
12667c478bd9Sstevel@tonic-gate 	}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	(void) unlink(OHISTORY);
12697c478bd9Sstevel@tonic-gate 	return (PWU_SUCCESS);
12707c478bd9Sstevel@tonic-gate }
1271