xref: /titanic_51/usr/src/cmd/pwconv/pwconv.c (revision 42487ff1899d230ad6c2d55cf9573489ca5eb770)
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*42487ff1Sas198278  * Common Development and Distribution License (the "License").
6*42487ff1Sas198278  * 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  */
21*42487ff1Sas198278 
227c478bd9Sstevel@tonic-gate /*
23*42487ff1Sas198278  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*  pwconv.c  */
337c478bd9Sstevel@tonic-gate /*  Conversion aid to copy appropriate fields from the	*/
347c478bd9Sstevel@tonic-gate /*  password file to the shadow file.			*/
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <time.h>
427c478bd9Sstevel@tonic-gate #include <shadow.h>
437c478bd9Sstevel@tonic-gate #include <grp.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate #include <stdlib.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
49*42487ff1Sas198278 #include <string.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	PRIVILEGED	0 			/* priviledged id */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* exit  code */
547c478bd9Sstevel@tonic-gate #define	SUCCESS	0	/* succeeded */
557c478bd9Sstevel@tonic-gate #define	NOPERM	1	/* No permission */
567c478bd9Sstevel@tonic-gate #define	BADSYN	2	/* Incorrect syntax */
577c478bd9Sstevel@tonic-gate #define	FMERR	3	/* File manipulation error */
587c478bd9Sstevel@tonic-gate #define	FATAL	4	/* Old file can not be recover */
597c478bd9Sstevel@tonic-gate #define	FBUSY	5	/* Lock file busy */
607c478bd9Sstevel@tonic-gate #define	BADSHW	6	/* Bad entry in shadow file  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	DELPTMP()	(void) unlink(PASSTEMP)
637c478bd9Sstevel@tonic-gate #define	DELSHWTMP()	(void) unlink(SHADTEMP)
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate char pwdflr[]	= "x";				/* password filler */
667c478bd9Sstevel@tonic-gate char *prognamp;
6749335bdeSbasabi void f_err(void), f_miss(void), f_bdshw(void);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * getspnan routine that ONLY looks at the local shadow file
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate struct spwd *
7349335bdeSbasabi local_getspnam(char *name)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	FILE *shadf;
767c478bd9Sstevel@tonic-gate 	struct spwd *sp;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if ((shadf = fopen("/etc/shadow", "r")) == NULL)
807c478bd9Sstevel@tonic-gate 		return (NULL);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	while ((sp = fgetspent(shadf)) != NULL) {
837c478bd9Sstevel@tonic-gate 		if (strcmp(sp->sp_namp, name) == 0)
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
87*42487ff1Sas198278 	(void) fclose(shadf);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	return (sp);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
9249335bdeSbasabi int
9349335bdeSbasabi main(int argc, char **argv)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	extern	int	errno;
9649335bdeSbasabi 	void  no_recover(void), no_convert(void);
977c478bd9Sstevel@tonic-gate 	struct  passwd  *pwdp;
987c478bd9Sstevel@tonic-gate 	struct	spwd	*sp, sp_pwd;		/* default entry */
997c478bd9Sstevel@tonic-gate 	struct stat buf;
1007c478bd9Sstevel@tonic-gate 	FILE	*tp_fp, *tsp_fp;
10149335bdeSbasabi 	time_t	when, minweeks, maxweeks;
1027c478bd9Sstevel@tonic-gate 	int file_exist = 1;
1037c478bd9Sstevel@tonic-gate 	int end_of_file = 0;
1047c478bd9Sstevel@tonic-gate 	mode_t mode;
105*42487ff1Sas198278 	mode_t pwd_mode;
1067c478bd9Sstevel@tonic-gate 	int pwerr = 0;
107*42487ff1Sas198278 	ushort_t i;
1087c478bd9Sstevel@tonic-gate 	gid_t pwd_gid, sp_gid;
1097c478bd9Sstevel@tonic-gate 	uid_t pwd_uid, sp_uid;
1107c478bd9Sstevel@tonic-gate 	FILE *pwf;
1117c478bd9Sstevel@tonic-gate 	int black_magic = 0;
1127c478bd9Sstevel@tonic-gate 	int count;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1177c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	prognamp = argv[0];
1227c478bd9Sstevel@tonic-gate 	/* only PRIVILEGED can execute this command */
1237c478bd9Sstevel@tonic-gate 	if (getuid() != PRIVILEGED) {
124*42487ff1Sas198278 		(void) fprintf(stderr, gettext("%s: Permission denied.\n"),
125*42487ff1Sas198278 		    prognamp);
1267c478bd9Sstevel@tonic-gate 		exit(NOPERM);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* No argument can be passed to the command */
1307c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1327c478bd9Sstevel@tonic-gate 		gettext("%s: Invalid command syntax.\n"),
1337c478bd9Sstevel@tonic-gate 			prognamp);
134*42487ff1Sas198278 		(void) fprintf(stderr, gettext("Usage: pwconv\n"));
1357c478bd9Sstevel@tonic-gate 		exit(BADSYN);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* lock file so that only one process can read or write at a time */
1397c478bd9Sstevel@tonic-gate 	if (lckpwdf() < 0) {
1407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1417c478bd9Sstevel@tonic-gate 		gettext("%s: Password file(s) busy.  Try again later.\n"),
1427c478bd9Sstevel@tonic-gate 			prognamp);
1437c478bd9Sstevel@tonic-gate 		exit(FBUSY);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* All signals will be ignored during the execution of pwconv */
1477c478bd9Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++)
148*42487ff1Sas198278 		(void) sigset(i, SIG_IGN);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* reset errno to avoid side effects of a failed */
1517c478bd9Sstevel@tonic-gate 	/* sigset (e.g., SIGKILL) */
1527c478bd9Sstevel@tonic-gate 	errno = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* check the file status of the password file */
1557c478bd9Sstevel@tonic-gate 	/* get the gid of the password file */
1567c478bd9Sstevel@tonic-gate 	if (stat(PASSWD, &buf) < 0) {
1577c478bd9Sstevel@tonic-gate 		(void) f_miss();
1587c478bd9Sstevel@tonic-gate 		exit(FATAL);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	pwd_gid = buf.st_gid;
1617c478bd9Sstevel@tonic-gate 	pwd_uid = buf.st_uid;
162*42487ff1Sas198278 	pwd_mode = buf.st_mode;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* mode for the password file should be read-only or less */
165*42487ff1Sas198278 	(void) umask(S_IAMB & ~(buf.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)));
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/* open temporary password file */
1687c478bd9Sstevel@tonic-gate 	if ((tp_fp = fopen(PASSTEMP, "w")) == NULL) {
1697c478bd9Sstevel@tonic-gate 		(void) f_err();
1707c478bd9Sstevel@tonic-gate 		exit(FMERR);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (chown(PASSTEMP, pwd_uid, pwd_gid) < 0) {
1747c478bd9Sstevel@tonic-gate 		DELPTMP();
1757c478bd9Sstevel@tonic-gate 		(void) f_err();
1767c478bd9Sstevel@tonic-gate 		exit(FMERR);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	/* default mode mask of the shadow file */
1797c478bd9Sstevel@tonic-gate 	mode = S_IAMB & ~(S_IRUSR);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* check the existence of  shadow file */
1827c478bd9Sstevel@tonic-gate 	/* if the shadow file exists, get mode mask and group id of the file */
1837c478bd9Sstevel@tonic-gate 	/* if file does not exist, the default group name will be the group  */
1847c478bd9Sstevel@tonic-gate 	/* name of the password file.  */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (access(SHADOW, F_OK) == 0) {
1877c478bd9Sstevel@tonic-gate 		if (stat(SHADOW, &buf) == 0) {
1887c478bd9Sstevel@tonic-gate 			mode  = S_IAMB & ~(buf.st_mode & S_IRUSR);
1897c478bd9Sstevel@tonic-gate 			sp_gid = buf.st_gid;
1907c478bd9Sstevel@tonic-gate 			sp_uid = buf.st_uid;
1917c478bd9Sstevel@tonic-gate 		} else {
1927c478bd9Sstevel@tonic-gate 			DELPTMP();
1937c478bd9Sstevel@tonic-gate 			(void) f_err();
1947c478bd9Sstevel@tonic-gate 			exit(FMERR);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	} else {
1977c478bd9Sstevel@tonic-gate 		sp_gid = pwd_gid;
1987c478bd9Sstevel@tonic-gate 		sp_uid = pwd_uid;
1997c478bd9Sstevel@tonic-gate 		file_exist = 0;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * get the mode of shadow password file  -- mode of the file should
2037c478bd9Sstevel@tonic-gate 	 * be read-only for user or less.
2047c478bd9Sstevel@tonic-gate 	 */
205*42487ff1Sas198278 	(void) umask(mode);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/* open temporary shadow file */
2087c478bd9Sstevel@tonic-gate 	if ((tsp_fp = fopen(SHADTEMP, "w")) == NULL) {
2097c478bd9Sstevel@tonic-gate 		DELPTMP();
2107c478bd9Sstevel@tonic-gate 		(void) f_err();
2117c478bd9Sstevel@tonic-gate 		exit(FMERR);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* change the group of the temporary shadow password file */
2157c478bd9Sstevel@tonic-gate 	if (chown(SHADTEMP, sp_uid, sp_gid) < 0) {
2167c478bd9Sstevel@tonic-gate 		(void) no_convert();
2177c478bd9Sstevel@tonic-gate 		exit(FMERR);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/* Reads the password file.  				*/
2217c478bd9Sstevel@tonic-gate 	/* If the shadow password file not exists, or		*/
2227c478bd9Sstevel@tonic-gate 	/* if an entry doesn't have a corresponding entry in    */
2237c478bd9Sstevel@tonic-gate 	/* the shadow file, entries/entry will be created.	*/
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
2267c478bd9Sstevel@tonic-gate 		no_recover();
2277c478bd9Sstevel@tonic-gate 		exit(FATAL);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	count = 0;
2317c478bd9Sstevel@tonic-gate 	while (!end_of_file) {
2327c478bd9Sstevel@tonic-gate 		count++;
2337c478bd9Sstevel@tonic-gate 		if ((pwdp = fgetpwent(pwf)) != NULL) {
2347c478bd9Sstevel@tonic-gate 			if (!file_exist ||
2357c478bd9Sstevel@tonic-gate 			    (sp = local_getspnam(pwdp->pw_name)) == NULL) {
2367c478bd9Sstevel@tonic-gate 				if (errno == EINVAL) {
2377c478bd9Sstevel@tonic-gate 				/* Bad entry in shadow exit */
2387c478bd9Sstevel@tonic-gate 					DELSHWTMP();
2397c478bd9Sstevel@tonic-gate 					DELPTMP();
2407c478bd9Sstevel@tonic-gate 					(void) f_bdshw();
2417c478bd9Sstevel@tonic-gate 					exit(BADSHW);
2427c478bd9Sstevel@tonic-gate 				}
2437c478bd9Sstevel@tonic-gate 				sp = &sp_pwd;
2447c478bd9Sstevel@tonic-gate 				sp->sp_namp = pwdp->pw_name;
2457c478bd9Sstevel@tonic-gate 				if (!pwdp->pw_passwd ||
2467c478bd9Sstevel@tonic-gate 				    (pwdp->pw_passwd &&
2477c478bd9Sstevel@tonic-gate 					*pwdp->pw_passwd == NULL)) {
2487c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
2497c478bd9Sstevel@tonic-gate 			gettext("%s: WARNING user %s has no password\n"),
2507c478bd9Sstevel@tonic-gate 					prognamp, sp->sp_namp);
2517c478bd9Sstevel@tonic-gate 				}
2527c478bd9Sstevel@tonic-gate 				/*
2537c478bd9Sstevel@tonic-gate 				 * copy the password field in the password
2547c478bd9Sstevel@tonic-gate 				 * file to the shadow file.
2557c478bd9Sstevel@tonic-gate 				 * replace the password field with an 'x'.
2567c478bd9Sstevel@tonic-gate 				 */
2577c478bd9Sstevel@tonic-gate 				sp->sp_pwdp = pwdp->pw_passwd;
2587c478bd9Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
2597c478bd9Sstevel@tonic-gate 				/*
2607c478bd9Sstevel@tonic-gate 				 * if aging, split the aging info
2617c478bd9Sstevel@tonic-gate 				 * into age, max and min
2627c478bd9Sstevel@tonic-gate 				 * convert aging info from weeks to days
2637c478bd9Sstevel@tonic-gate 				 */
2647c478bd9Sstevel@tonic-gate 				if (pwdp->pw_age && *pwdp->pw_age != NULL) {
2657c478bd9Sstevel@tonic-gate 					when = (long)a64l(pwdp->pw_age);
2667c478bd9Sstevel@tonic-gate 					maxweeks = when & 077;
2677c478bd9Sstevel@tonic-gate 					minweeks = (when >> 6) & 077;
2687c478bd9Sstevel@tonic-gate 					when >>= 12;
2697c478bd9Sstevel@tonic-gate 					sp->sp_lstchg = when * 7;
2707c478bd9Sstevel@tonic-gate 					sp->sp_min = minweeks * 7;
2717c478bd9Sstevel@tonic-gate 					sp->sp_max = maxweeks * 7;
2727c478bd9Sstevel@tonic-gate 					sp->sp_warn = -1;
2737c478bd9Sstevel@tonic-gate 					sp->sp_inact = -1;
2747c478bd9Sstevel@tonic-gate 					sp->sp_expire = -1;
2757c478bd9Sstevel@tonic-gate 					sp->sp_flag = 0;
2767c478bd9Sstevel@tonic-gate 					pwdp->pw_age = "";  /* do we care? */
2777c478bd9Sstevel@tonic-gate 				} else {
2787c478bd9Sstevel@tonic-gate 				/*
2797c478bd9Sstevel@tonic-gate 				 * if !aging, last_changed will be the day the
2807c478bd9Sstevel@tonic-gate 				 * conversion is done, min and max fields will
2817c478bd9Sstevel@tonic-gate 				 * be null - use timezone to get local time
2827c478bd9Sstevel@tonic-gate 				 */
2837c478bd9Sstevel@tonic-gate 					sp->sp_lstchg = DAY_NOW;
2847c478bd9Sstevel@tonic-gate 					sp->sp_min =  -1;
2857c478bd9Sstevel@tonic-gate 					sp->sp_max =  -1;
2867c478bd9Sstevel@tonic-gate 					sp->sp_warn = -1;
2877c478bd9Sstevel@tonic-gate 					sp->sp_inact = -1;
2887c478bd9Sstevel@tonic-gate 					sp->sp_expire = -1;
2897c478bd9Sstevel@tonic-gate 					sp->sp_flag = 0;
2907c478bd9Sstevel@tonic-gate 				}
2917c478bd9Sstevel@tonic-gate 	    } else {
2927c478bd9Sstevel@tonic-gate 			/*
2937c478bd9Sstevel@tonic-gate 			 * if the passwd field has a string other than 'x',
2947c478bd9Sstevel@tonic-gate 			 * the entry will be written into the shadow file
2957c478bd9Sstevel@tonic-gate 			 * and the character 'x' is re-written as the passwd
2967c478bd9Sstevel@tonic-gate 			 * if !aging, last_changed as above
2977c478bd9Sstevel@tonic-gate 			 */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			/*
3007c478bd9Sstevel@tonic-gate 			 * with NIS, only warn about password missing if entry
3017c478bd9Sstevel@tonic-gate 			 * is not a NIS-lookup entry ("+" or "-")
3027c478bd9Sstevel@tonic-gate 			 * black_magic from getpwnam_r.c
3037c478bd9Sstevel@tonic-gate 			 */
3047c478bd9Sstevel@tonic-gate 			black_magic = (*pwdp->pw_name == '+' ||
3057c478bd9Sstevel@tonic-gate 					*pwdp->pw_name == '-');
3067c478bd9Sstevel@tonic-gate 			/*
3077c478bd9Sstevel@tonic-gate 			 * moan about absence of non "+/-" passwd
3087c478bd9Sstevel@tonic-gate 			 * we could do more, but what?
3097c478bd9Sstevel@tonic-gate 			 */
3107c478bd9Sstevel@tonic-gate 			if ((!pwdp->pw_passwd ||
3117c478bd9Sstevel@tonic-gate 			    (pwdp->pw_passwd && *pwdp->pw_passwd == NULL)) &&
3127c478bd9Sstevel@tonic-gate 			    !black_magic) {
3137c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3147c478bd9Sstevel@tonic-gate 			gettext("%s: WARNING user %s has no password\n"),
3157c478bd9Sstevel@tonic-gate 					prognamp, sp->sp_namp);
3167c478bd9Sstevel@tonic-gate 			}
3177c478bd9Sstevel@tonic-gate 			if (pwdp->pw_passwd && *pwdp->pw_passwd) {
3187c478bd9Sstevel@tonic-gate 				if (strcmp(pwdp->pw_passwd, pwdflr)) {
3197c478bd9Sstevel@tonic-gate 					sp->sp_pwdp = pwdp->pw_passwd;
3207c478bd9Sstevel@tonic-gate 					pwdp->pw_passwd = pwdflr;
3217c478bd9Sstevel@tonic-gate 					if (!pwdp->pw_age ||
3227c478bd9Sstevel@tonic-gate 					    (pwdp->pw_age &&
3237c478bd9Sstevel@tonic-gate 						*pwdp->pw_age == NULL)) {
3247c478bd9Sstevel@tonic-gate 						sp->sp_lstchg = DAY_NOW;
3257c478bd9Sstevel@tonic-gate 						sp->sp_min =  -1;
3267c478bd9Sstevel@tonic-gate 						sp->sp_max =  -1;
3277c478bd9Sstevel@tonic-gate 						sp->sp_warn = -1;
3287c478bd9Sstevel@tonic-gate 						sp->sp_inact = -1;
3297c478bd9Sstevel@tonic-gate 						sp->sp_expire = -1;
3307c478bd9Sstevel@tonic-gate 						sp->sp_flag = 0;
3317c478bd9Sstevel@tonic-gate 					}
3327c478bd9Sstevel@tonic-gate 				}
3337c478bd9Sstevel@tonic-gate 			} else {
3347c478bd9Sstevel@tonic-gate 				/*
3357c478bd9Sstevel@tonic-gate 				 * black_magic needs a non-null passwd
3367c478bd9Sstevel@tonic-gate 				 * and pwdflr seem appropriate here
3377c478bd9Sstevel@tonic-gate 				 * clear garbage if any
3387c478bd9Sstevel@tonic-gate 				 */
3397c478bd9Sstevel@tonic-gate 				sp->sp_pwdp = "";
3407c478bd9Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
3417c478bd9Sstevel@tonic-gate 				sp->sp_lstchg = sp->sp_min = sp->sp_max = -1;
3427c478bd9Sstevel@tonic-gate 				sp->sp_warn = sp->sp_inact = sp->sp_expire = -1;
3437c478bd9Sstevel@tonic-gate 				sp->sp_flag = 0;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 			/*
3467c478bd9Sstevel@tonic-gate 			 * if aging, split the aging info
3477c478bd9Sstevel@tonic-gate 			 * into age, max and min
3487c478bd9Sstevel@tonic-gate 			 * convert aging info from weeks to days
3497c478bd9Sstevel@tonic-gate 			 */
3507c478bd9Sstevel@tonic-gate 			if (pwdp->pw_age && *pwdp->pw_age != NULL) {
3517c478bd9Sstevel@tonic-gate 				when = (long)a64l(pwdp->pw_age);
3527c478bd9Sstevel@tonic-gate 				maxweeks = when & 077;
3537c478bd9Sstevel@tonic-gate 				minweeks = (when >> 6) & 077;
3547c478bd9Sstevel@tonic-gate 				when >>= 12;
3557c478bd9Sstevel@tonic-gate 				sp->sp_lstchg = when * 7;
3567c478bd9Sstevel@tonic-gate 				sp->sp_min = minweeks * 7;
3577c478bd9Sstevel@tonic-gate 				sp->sp_max = maxweeks * 7;
3587c478bd9Sstevel@tonic-gate 				sp->sp_warn = -1;
3597c478bd9Sstevel@tonic-gate 				sp->sp_inact = -1;
3607c478bd9Sstevel@tonic-gate 				sp->sp_expire = -1;
3617c478bd9Sstevel@tonic-gate 				sp->sp_flag = 0;
3627c478bd9Sstevel@tonic-gate 				pwdp->pw_age = ""; /* do we care? */
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		/* write an entry to temporary password file */
3677c478bd9Sstevel@tonic-gate 		if ((putpwent(pwdp, tp_fp)) != 0) {
3687c478bd9Sstevel@tonic-gate 			(void) no_convert();
3697c478bd9Sstevel@tonic-gate 			exit(FMERR);
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		/* write an entry to temporary shadow password file */
3737c478bd9Sstevel@tonic-gate 		if (putspent(sp, tsp_fp) != 0) {
3747c478bd9Sstevel@tonic-gate 			(void) no_convert();
3757c478bd9Sstevel@tonic-gate 			exit(FMERR);
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 	    } else {
3787c478bd9Sstevel@tonic-gate 		if (feof(pwf)) {
3797c478bd9Sstevel@tonic-gate 			end_of_file = 1;
3807c478bd9Sstevel@tonic-gate 		} else {
3817c478bd9Sstevel@tonic-gate 			errno = 0;
3827c478bd9Sstevel@tonic-gate 			pwerr = 1;
3837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
384*42487ff1Sas198278 			    gettext("%s: ERROR: bad entry or blank "
385*42487ff1Sas198278 			    "line at line %d in /etc/passwd\n"),
3867c478bd9Sstevel@tonic-gate 			    prognamp, count);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	    }
3897c478bd9Sstevel@tonic-gate 	} /* end of while */
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	(void) fclose(pwf);
3927c478bd9Sstevel@tonic-gate 	(void) fclose(tsp_fp);
3937c478bd9Sstevel@tonic-gate 	(void) fclose(tp_fp);
3947c478bd9Sstevel@tonic-gate 	if (pwerr) {
3957c478bd9Sstevel@tonic-gate 		(void) no_convert();
3967c478bd9Sstevel@tonic-gate 		exit(FMERR);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/* delete old password file if it exists */
4007c478bd9Sstevel@tonic-gate 	if (unlink(OPASSWD) && (access(OPASSWD, F_OK) == 0)) {
4017c478bd9Sstevel@tonic-gate 		(void) no_convert();
4027c478bd9Sstevel@tonic-gate 		exit(FMERR);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/* rename the password file to old password file  */
4067c478bd9Sstevel@tonic-gate 	if (rename(PASSWD, OPASSWD) == -1) {
4077c478bd9Sstevel@tonic-gate 		(void) no_convert();
4087c478bd9Sstevel@tonic-gate 		exit(FMERR);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* rename temporary password file to password file */
4127c478bd9Sstevel@tonic-gate 	if (rename(PASSTEMP, PASSWD) == -1) {
4137c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4147c478bd9Sstevel@tonic-gate 		if (link(OPASSWD, PASSWD) < 0) {
4157c478bd9Sstevel@tonic-gate 			(void) no_recover();
4167c478bd9Sstevel@tonic-gate 			exit(FATAL);
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 		(void) no_convert();
4197c478bd9Sstevel@tonic-gate 		exit(FMERR);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/* delete old shadow password file if it exists */
4237c478bd9Sstevel@tonic-gate 	if (unlink(OSHADOW) && (access(OSHADOW, R_OK) == 0)) {
4247c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4257c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4267c478bd9Sstevel@tonic-gate 			(void) no_recover();
4277c478bd9Sstevel@tonic-gate 			exit(FATAL);
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		(void) no_convert();
4307c478bd9Sstevel@tonic-gate 		exit(FMERR);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/* link shadow password file to old shadow password file */
4347c478bd9Sstevel@tonic-gate 	if (file_exist && rename(SHADOW, OSHADOW)) {
4357c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4367c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4377c478bd9Sstevel@tonic-gate 			(void) no_recover();
4387c478bd9Sstevel@tonic-gate 			exit(FATAL);
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 		(void) no_convert();
4417c478bd9Sstevel@tonic-gate 		exit(FMERR);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/* link temporary shadow password file to shadow password file */
4467c478bd9Sstevel@tonic-gate 	if (rename(SHADTEMP, SHADOW) == -1) {
4477c478bd9Sstevel@tonic-gate 		/* link old shadow password file to shadow password file */
4487c478bd9Sstevel@tonic-gate 		if (file_exist && (link(OSHADOW, SHADOW))) {
4497c478bd9Sstevel@tonic-gate 			(void) no_recover();
4507c478bd9Sstevel@tonic-gate 			exit(FATAL);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4537c478bd9Sstevel@tonic-gate 			(void) no_recover();
4547c478bd9Sstevel@tonic-gate 			exit(FATAL);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		(void) no_convert();
4577c478bd9Sstevel@tonic-gate 		exit(FMERR);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
460*42487ff1Sas198278 	/* Make new mode same as old */
461*42487ff1Sas198278 	(void) chmod(PASSWD, pwd_mode);
462*42487ff1Sas198278 
4637c478bd9Sstevel@tonic-gate 	/* Change old password file to read only by owner   */
4647c478bd9Sstevel@tonic-gate 	/* If chmod fails, delete the old password file so that */
4657c478bd9Sstevel@tonic-gate 	/* the password fields can not be read by others */
4667c478bd9Sstevel@tonic-gate 	if (chmod(OPASSWD, S_IRUSR) < 0)
4677c478bd9Sstevel@tonic-gate 		(void) unlink(OPASSWD);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
47049335bdeSbasabi 	return (0);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate void
47449335bdeSbasabi no_recover(void)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	DELPTMP();
4777c478bd9Sstevel@tonic-gate 	DELSHWTMP();
4787c478bd9Sstevel@tonic-gate 	(void) f_miss();
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate void
48249335bdeSbasabi no_convert(void)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	DELPTMP();
4857c478bd9Sstevel@tonic-gate 	DELSHWTMP();
4867c478bd9Sstevel@tonic-gate 	(void) f_err();
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate void
49049335bdeSbasabi f_err(void)
4917c478bd9Sstevel@tonic-gate {
492*42487ff1Sas198278 	(void) fprintf(stderr,
4937c478bd9Sstevel@tonic-gate 		gettext("%s: Unexpected failure. Conversion not done.\n"),
4947c478bd9Sstevel@tonic-gate 			prognamp);
4957c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate void
49949335bdeSbasabi f_miss(void)
5007c478bd9Sstevel@tonic-gate {
501*42487ff1Sas198278 	(void) fprintf(stderr,
5027c478bd9Sstevel@tonic-gate 		gettext("%s: Unexpected failure. Password file(s) missing.\n"),
5037c478bd9Sstevel@tonic-gate 			prognamp);
5047c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate void
50849335bdeSbasabi f_bdshw(void)
5097c478bd9Sstevel@tonic-gate {
510*42487ff1Sas198278 	(void) fprintf(stderr,
5117c478bd9Sstevel@tonic-gate 		gettext("%s: Bad entry in /etc/shadow. Conversion not done.\n"),
5127c478bd9Sstevel@tonic-gate 			prognamp);
5137c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
5147c478bd9Sstevel@tonic-gate }
515