xref: /illumos-gate/usr/src/ucbcmd/vipw/vipw.c (revision 8c0b080c)
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
5622200adScf  * Common Development and Distribution License (the "License").
6622200adScf  * 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 /*
22622200adScf  * 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 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
42956e8222Scf #include <stdlib.h>
43956e8222Scf #include <strings.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Password file editor with locking.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	DEFAULT_EDITOR	"/usr/bin/vi"
507c478bd9Sstevel@tonic-gate 
51956e8222Scf static int copyfile(char *, char *);
52956e8222Scf static int editfile(char *, char *, char *, time_t *);
53956e8222Scf static int sanity_check(char *, time_t *, char *);
54956e8222Scf static int validsh(char *);
55956e8222Scf 
567c478bd9Sstevel@tonic-gate char	*ptemp = "/etc/ptmp";
577c478bd9Sstevel@tonic-gate char	*stemp = "/etc/stmp";
587c478bd9Sstevel@tonic-gate char	*passwd = "/etc/passwd";
597c478bd9Sstevel@tonic-gate char	*shadow = "/etc/shadow";
607c478bd9Sstevel@tonic-gate char	buf[BUFSIZ];
617c478bd9Sstevel@tonic-gate 
62956e8222Scf int
main(void)63956e8222Scf main(void)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	int fd;
667c478bd9Sstevel@tonic-gate 	FILE *ft, *fp;
677c478bd9Sstevel@tonic-gate 	char *editor;
687c478bd9Sstevel@tonic-gate 	int ok = 0;
697c478bd9Sstevel@tonic-gate 	time_t o_mtime, n_mtime;
707c478bd9Sstevel@tonic-gate 	struct stat osbuf, sbuf, oshdbuf, shdbuf;
717c478bd9Sstevel@tonic-gate 	char c;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	(void)signal(SIGINT, SIG_IGN);
747c478bd9Sstevel@tonic-gate 	(void)signal(SIGQUIT, SIG_IGN);
757c478bd9Sstevel@tonic-gate 	(void)signal(SIGHUP, SIG_IGN);
767c478bd9Sstevel@tonic-gate 	setbuf(stderr, (char *)NULL);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	editor = getenv("VISUAL");
797c478bd9Sstevel@tonic-gate 	if (editor == 0)
807c478bd9Sstevel@tonic-gate 		editor = getenv("EDITOR");
817c478bd9Sstevel@tonic-gate 	if (editor == 0)
827c478bd9Sstevel@tonic-gate 		editor = DEFAULT_EDITOR;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	(void)umask(0077);
857c478bd9Sstevel@tonic-gate 	if (stat(passwd, &osbuf) < 0) {
867c478bd9Sstevel@tonic-gate                 (void)fprintf(stderr,"vipw: can't stat passwd file.\n");
877c478bd9Sstevel@tonic-gate                 goto bad;
88*8c0b080cSToomas Soome         }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (copyfile(passwd, ptemp))
917c478bd9Sstevel@tonic-gate 		goto bad;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (stat(ptemp, &sbuf) < 0) {
947c478bd9Sstevel@tonic-gate                 (void)fprintf(stderr,
957c478bd9Sstevel@tonic-gate 			"vipw: can't stat ptemp file, %s unchanged\n",
967c478bd9Sstevel@tonic-gate 			passwd);
977c478bd9Sstevel@tonic-gate 		goto bad;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	o_mtime = sbuf.st_mtime;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (editfile(editor, ptemp, passwd, &n_mtime)) {
1037c478bd9Sstevel@tonic-gate 		if (sanity_check(ptemp, &n_mtime, passwd))
1047c478bd9Sstevel@tonic-gate 			goto bad;
1057c478bd9Sstevel@tonic-gate 		if (o_mtime >= n_mtime)
1067c478bd9Sstevel@tonic-gate 			goto bad;
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	ok++;
1107c478bd9Sstevel@tonic-gate 	if (o_mtime < n_mtime) {
1117c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\nYou have modified the password file.\n");
1127c478bd9Sstevel@tonic-gate 		fprintf(stdout,
1137c478bd9Sstevel@tonic-gate 	"Press 'e' to edit the shadow file for consistency,\n 'q' to quit: ");
1147c478bd9Sstevel@tonic-gate 		if ((c = getchar()) == 'q') {
1157c478bd9Sstevel@tonic-gate 			if (chmod(ptemp, (osbuf.st_mode & 0644)) < 0) {
1167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s: ", ptemp);
1177c478bd9Sstevel@tonic-gate 				perror("chmod");
1187c478bd9Sstevel@tonic-gate 				goto bad;
1197c478bd9Sstevel@tonic-gate 			}
1207c478bd9Sstevel@tonic-gate 			if (rename(ptemp, passwd) < 0) {
1217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s: ", ptemp);
1227c478bd9Sstevel@tonic-gate 				perror("rename");
1237c478bd9Sstevel@tonic-gate 				goto bad;
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 			if (((osbuf.st_gid != sbuf.st_gid) ||
1267c478bd9Sstevel@tonic-gate 					(osbuf.st_uid != sbuf.st_uid)) &&
1277c478bd9Sstevel@tonic-gate 			(chown(passwd, osbuf.st_uid, osbuf.st_gid) < 0)) {
1287c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s ", ptemp);
1297c478bd9Sstevel@tonic-gate 				perror("chown");
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 			goto bad;
1327c478bd9Sstevel@tonic-gate 		} else if (c == 'e') {
1337c478bd9Sstevel@tonic-gate 			if (stat(shadow, &oshdbuf) < 0) {
1347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1357c478bd9Sstevel@tonic-gate 					"vipw: can't stat shadow file.\n");
1367c478bd9Sstevel@tonic-gate 				goto bad;
1377c478bd9Sstevel@tonic-gate 			}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 			if (copyfile(shadow, stemp))
1407c478bd9Sstevel@tonic-gate 				goto bad;
1417c478bd9Sstevel@tonic-gate 			if (stat(stemp, &shdbuf) < 0) {
1427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1437c478bd9Sstevel@tonic-gate 					"vipw: can't stat stmp file.\n");
1447c478bd9Sstevel@tonic-gate 				goto bad;
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 			if (editfile(editor, stemp, shadow, &o_mtime))
1487c478bd9Sstevel@tonic-gate 				goto bad;
1497c478bd9Sstevel@tonic-gate 			ok++;
1507c478bd9Sstevel@tonic-gate 			if (chmod(ptemp, (osbuf.st_mode & 0644)) < 0) {
1517c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s: ", ptemp);
1527c478bd9Sstevel@tonic-gate 				perror("chmod");
1537c478bd9Sstevel@tonic-gate 				goto bad;
1547c478bd9Sstevel@tonic-gate 			}
1557c478bd9Sstevel@tonic-gate 			if (chmod(stemp, (oshdbuf.st_mode & 0400)) < 0) {
1567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s: ", stemp);
1577c478bd9Sstevel@tonic-gate 				perror("chmod");
1587c478bd9Sstevel@tonic-gate 				goto bad;
1597c478bd9Sstevel@tonic-gate 			}
1607c478bd9Sstevel@tonic-gate 			if (rename(ptemp, passwd) < 0) {
1617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s: ", ptemp);
1627c478bd9Sstevel@tonic-gate 				perror("rename");
1637c478bd9Sstevel@tonic-gate 				goto bad;
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			if (((osbuf.st_gid != sbuf.st_gid) ||
1667c478bd9Sstevel@tonic-gate 					(osbuf.st_uid != sbuf.st_uid)) &&
1677c478bd9Sstevel@tonic-gate 			(chown(passwd, osbuf.st_uid, osbuf.st_gid) < 0)) {
1687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s ", ptemp);
1697c478bd9Sstevel@tonic-gate 				perror("chown");
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			if (rename(stemp, shadow) < 0) {
1727c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s: ", stemp);
1737c478bd9Sstevel@tonic-gate 				perror("rename");
1747c478bd9Sstevel@tonic-gate 				goto bad;
1757c478bd9Sstevel@tonic-gate 			} else if (((oshdbuf.st_gid != shdbuf.st_gid) ||
1767c478bd9Sstevel@tonic-gate 					(oshdbuf.st_uid != shdbuf.st_uid)) &&
1777c478bd9Sstevel@tonic-gate 			(chown(shadow, oshdbuf.st_uid, oshdbuf.st_gid) < 0)) {
1787c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "vipw: %s ", stemp);
1797c478bd9Sstevel@tonic-gate 				perror("chown");
1807c478bd9Sstevel@tonic-gate 				}
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate bad:
1847c478bd9Sstevel@tonic-gate 	(void) unlink(ptemp);
1857c478bd9Sstevel@tonic-gate 	(void) unlink(stemp);
186956e8222Scf 	return (ok ? 0 : 1);
1877c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 
191956e8222Scf int
copyfile(char * from,char * to)192956e8222Scf copyfile(char *from, char *to)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	int fd;
1957c478bd9Sstevel@tonic-gate 	FILE *fp, *ft;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	fd = open(to, O_WRONLY|O_CREAT|O_EXCL, 0600);
1987c478bd9Sstevel@tonic-gate 	if (fd < 0) {
1997c478bd9Sstevel@tonic-gate 		if (errno == EEXIST) {
2007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "vipw: %s file busy\n", from);
2017c478bd9Sstevel@tonic-gate 			exit(1);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "vipw: "); perror(to);
2047c478bd9Sstevel@tonic-gate 		exit(1);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	ft = fdopen(fd, "w");
2077c478bd9Sstevel@tonic-gate 	if (ft == NULL) {
2087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "vipw: "); perror(to);
2097c478bd9Sstevel@tonic-gate 		return( 1 );
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	fp = fopen(from, "r");
2127c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "vipw: "); perror(from);
2147c478bd9Sstevel@tonic-gate 		return( 1 );
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf) - 1, fp) != NULL)
2177c478bd9Sstevel@tonic-gate 		fputs(buf, ft);
2187c478bd9Sstevel@tonic-gate 	(void) fclose(ft);
2197c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
2207c478bd9Sstevel@tonic-gate 	return( 0 );
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
223956e8222Scf int
editfile(char * editor,char * temp,char * orig,time_t * mtime)224956e8222Scf editfile(char *editor, char *temp, char *orig, time_t *mtime)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	(void)sprintf(buf, "%s %s", editor, temp);
2277c478bd9Sstevel@tonic-gate 	if (system(buf) == 0) {
2287c478bd9Sstevel@tonic-gate 		return (sanity_check(temp, mtime, orig));
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	return(1);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
234956e8222Scf int
validsh(char * rootsh)235956e8222Scf validsh(char *rootsh)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	char	*sh, *getusershell();
2397c478bd9Sstevel@tonic-gate 	int	ret = 0;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	setusershell();
2427c478bd9Sstevel@tonic-gate 	while((sh = getusershell()) != NULL ) {
2437c478bd9Sstevel@tonic-gate 		if( strcmp( rootsh, sh) == 0 ) {
2447c478bd9Sstevel@tonic-gate 			ret = 1;
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	endusershell();
2497c478bd9Sstevel@tonic-gate 	return(ret);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * sanity checks
2547c478bd9Sstevel@tonic-gate  * return 0 if ok, 1 otherwise
2557c478bd9Sstevel@tonic-gate  */
256956e8222Scf int
sanity_check(char * temp,time_t * mtime,char * orig)257956e8222Scf sanity_check(char *temp, time_t *mtime, char *orig)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int i, ok = 0;
2607c478bd9Sstevel@tonic-gate 	FILE *ft;
261622200adScf 	struct stat sbuf, statbuf;
262622200adScf 	char *ldir;
2637c478bd9Sstevel@tonic-gate 	int isshadow = 0;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (!strcmp(orig, shadow))
2667c478bd9Sstevel@tonic-gate 		isshadow = 1;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/* sanity checks */
2697c478bd9Sstevel@tonic-gate 	if (stat(temp, &sbuf) < 0) {
2707c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr,
2717c478bd9Sstevel@tonic-gate 		    "vipw: can't stat %s file, %s unchanged\n",
2727c478bd9Sstevel@tonic-gate 		    temp, orig);
2737c478bd9Sstevel@tonic-gate 		return(1);
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	*mtime = sbuf.st_mtime;
2767c478bd9Sstevel@tonic-gate 	if (sbuf.st_size == 0) {
2777c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr, "vipw: bad %s file, %s unchanged\n",
2787c478bd9Sstevel@tonic-gate 		    temp, orig);
2797c478bd9Sstevel@tonic-gate 		return(1);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	ft = fopen(temp, "r");
2827c478bd9Sstevel@tonic-gate 	if (ft == NULL) {
2837c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr,
2847c478bd9Sstevel@tonic-gate 		    "vipw: can't reopen %s file, %s unchanged\n",
2857c478bd9Sstevel@tonic-gate 		    temp, orig);
2867c478bd9Sstevel@tonic-gate 		return(1);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf) - 1, ft) != NULL) {
290956e8222Scf 		char *cp;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		cp = index(buf, '\n');
2937c478bd9Sstevel@tonic-gate 		if (cp == 0)
2947c478bd9Sstevel@tonic-gate 			continue;	/* ??? allow very long lines
2957c478bd9Sstevel@tonic-gate 					 * and passwd files that do
2967c478bd9Sstevel@tonic-gate 					 * not end in '\n' ???
2977c478bd9Sstevel@tonic-gate 					 */
2987c478bd9Sstevel@tonic-gate 		*cp = '\0';
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		cp = index(buf, ':');
3017c478bd9Sstevel@tonic-gate 		if (cp == 0)		/* lines without colon
3027c478bd9Sstevel@tonic-gate 					 * separated fields
3037c478bd9Sstevel@tonic-gate 					 */
3047c478bd9Sstevel@tonic-gate 			continue;
3057c478bd9Sstevel@tonic-gate 		*cp = '\0';
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		if (strcmp(buf, "root"))
3087c478bd9Sstevel@tonic-gate 			continue;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		/* root password */
3117c478bd9Sstevel@tonic-gate 		*cp = ':';
3127c478bd9Sstevel@tonic-gate 		cp = index(cp + 1, ':');
3137c478bd9Sstevel@tonic-gate 		if (cp == 0)
3147c478bd9Sstevel@tonic-gate 			goto bad_root;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		/* root uid for password */
3177c478bd9Sstevel@tonic-gate 		if (!isshadow)
3187c478bd9Sstevel@tonic-gate 			if (atoi(cp + 1) != 0) {
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 				(void)fprintf(stderr, "root UID != 0:\n%s\n",
3217c478bd9Sstevel@tonic-gate 				    buf);
3227c478bd9Sstevel@tonic-gate 				break;
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 		/* root uid for passwd and sp_lstchg for shadow */
3257c478bd9Sstevel@tonic-gate 		cp = index(cp + 1, ':');
3267c478bd9Sstevel@tonic-gate 		if (cp == 0)
3277c478bd9Sstevel@tonic-gate 			goto bad_root;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		/* root's gid for passwd and sp_min for shadow*/
3307c478bd9Sstevel@tonic-gate 		cp = index(cp + 1, ':');
3317c478bd9Sstevel@tonic-gate 		if (cp == 0)
3327c478bd9Sstevel@tonic-gate 			goto bad_root;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		/* root's gecos for passwd and sp_max for shadow*/
3357c478bd9Sstevel@tonic-gate 		cp = index(cp + 1, ':');
3367c478bd9Sstevel@tonic-gate 		if (isshadow) {
3377c478bd9Sstevel@tonic-gate 			for (i=0; i<3; i++)
3387c478bd9Sstevel@tonic-gate 				if ((cp = index(cp + 1, ':')) == 0)
3397c478bd9Sstevel@tonic-gate 					goto bad_root;
3407c478bd9Sstevel@tonic-gate 		} else {
3417c478bd9Sstevel@tonic-gate 			if (cp == 0) {
3427c478bd9Sstevel@tonic-gate bad_root:		(void)fprintf(stderr,
3437c478bd9Sstevel@tonic-gate 				    "Missing fields in root entry:\n%s\n", buf);
3447c478bd9Sstevel@tonic-gate 				break;
3457c478bd9Sstevel@tonic-gate 			}
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 		if (!isshadow) {
3487c478bd9Sstevel@tonic-gate 			/* root's login directory */
349622200adScf 			ldir = ++cp;
350622200adScf 			cp = index(cp, ':');
351622200adScf 			if (cp == 0)
352622200adScf 				goto bad_root;
353622200adScf 			*cp = '\0';
354622200adScf 			if (stat(ldir, &statbuf) < 0) {
355622200adScf 				*cp = ':';
356622200adScf 				(void) fprintf(stderr,
357622200adScf 				    "root login dir doesn't exist:\n%s\n",
358622200adScf 				    buf);
359622200adScf 				break;
360622200adScf 			} else if (!S_ISDIR(statbuf.st_mode)) {
361622200adScf 				*cp = ':';
362622200adScf 				(void) fprintf(stderr,
363622200adScf 				    "root login dir is not a directory:\n%s\n",
364622200adScf 				    buf);
3657c478bd9Sstevel@tonic-gate 				break;
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 
368622200adScf 			*cp = ':';
3697c478bd9Sstevel@tonic-gate 			/* root's login shell */
370622200adScf 			++cp;
3717c478bd9Sstevel@tonic-gate 			if (*cp && ! validsh(cp)) {
3727c478bd9Sstevel@tonic-gate 				(void)fprintf(stderr,
3737c478bd9Sstevel@tonic-gate 				    "Invalid root shell:\n%s\n", buf);
3747c478bd9Sstevel@tonic-gate 				break;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		ok++;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	(void)fclose(ft);
3817c478bd9Sstevel@tonic-gate 	if (ok)
3827c478bd9Sstevel@tonic-gate 		return(0);
3837c478bd9Sstevel@tonic-gate 	else {
3847c478bd9Sstevel@tonic-gate 		(void)fprintf(stderr,
3857c478bd9Sstevel@tonic-gate 		    "vipw: you mangled the %s file, %s unchanged\n",
3867c478bd9Sstevel@tonic-gate 		    temp, orig);
3877c478bd9Sstevel@tonic-gate 		return(1);
388*8c0b080cSToomas Soome 	}
3897c478bd9Sstevel@tonic-gate }
390