xref: /titanic_51/usr/src/cmd/pwconv/pwconv.c (revision 49335bdececa5fc091b6c63763f9176524390b5e)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*49335bdeSbasabi  * Copyright 2005 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>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	PRIVILEGED	0 			/* priviledged id */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* exit  code */
537c478bd9Sstevel@tonic-gate #define	SUCCESS	0	/* succeeded */
547c478bd9Sstevel@tonic-gate #define	NOPERM	1	/* No permission */
557c478bd9Sstevel@tonic-gate #define	BADSYN	2	/* Incorrect syntax */
567c478bd9Sstevel@tonic-gate #define	FMERR	3	/* File manipulation error */
577c478bd9Sstevel@tonic-gate #define	FATAL	4	/* Old file can not be recover */
587c478bd9Sstevel@tonic-gate #define	FBUSY	5	/* Lock file busy */
597c478bd9Sstevel@tonic-gate #define	BADSHW	6	/* Bad entry in shadow file  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	DELPTMP()	(void) unlink(PASSTEMP)
627c478bd9Sstevel@tonic-gate #define	DELSHWTMP()	(void) unlink(SHADTEMP)
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate char pwdflr[]	= "x";				/* password filler */
657c478bd9Sstevel@tonic-gate char *prognamp;
66*49335bdeSbasabi void f_err(void), f_miss(void), f_bdshw(void);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * getspnan routine that ONLY looks at the local shadow file
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate struct spwd *
72*49335bdeSbasabi local_getspnam(char *name)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	FILE *shadf;
757c478bd9Sstevel@tonic-gate 	struct spwd * sp;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if ((shadf = fopen("/etc/shadow", "r")) == NULL)
797c478bd9Sstevel@tonic-gate 		return (NULL);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	while ((sp = fgetspent(shadf)) != NULL) {
827c478bd9Sstevel@tonic-gate 		if (strcmp(sp->sp_namp, name) == 0)
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	fclose(shadf);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	return (sp);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
91*49335bdeSbasabi int
92*49335bdeSbasabi main(int argc, char **argv)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	extern	int	errno;
95*49335bdeSbasabi 	void  no_recover(void), no_convert(void);
967c478bd9Sstevel@tonic-gate 	struct  passwd  *pwdp;
977c478bd9Sstevel@tonic-gate 	struct	spwd	*sp, sp_pwd;		/* default entry */
987c478bd9Sstevel@tonic-gate 	struct stat buf;
997c478bd9Sstevel@tonic-gate 	FILE	*tp_fp, *tsp_fp;
100*49335bdeSbasabi 	time_t	when, minweeks, maxweeks;
1017c478bd9Sstevel@tonic-gate 	int file_exist = 1;
1027c478bd9Sstevel@tonic-gate 	int end_of_file = 0;
1037c478bd9Sstevel@tonic-gate 	mode_t mode;
1047c478bd9Sstevel@tonic-gate 	int pwerr = 0;
1057c478bd9Sstevel@tonic-gate 	ushort i;
1067c478bd9Sstevel@tonic-gate 	gid_t pwd_gid, sp_gid;
1077c478bd9Sstevel@tonic-gate 	uid_t pwd_uid, sp_uid;
1087c478bd9Sstevel@tonic-gate 	FILE *pwf;
1097c478bd9Sstevel@tonic-gate 	int black_magic = 0;
1107c478bd9Sstevel@tonic-gate 	int count;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1157c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	prognamp = argv[0];
1207c478bd9Sstevel@tonic-gate 	/* only PRIVILEGED can execute this command */
1217c478bd9Sstevel@tonic-gate 	if (getuid() != PRIVILEGED) {
1227c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: Permission denied.\n"), prognamp);
1237c478bd9Sstevel@tonic-gate 		exit(NOPERM);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* No argument can be passed to the command */
1277c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1297c478bd9Sstevel@tonic-gate 		gettext("%s: Invalid command syntax.\n"),
1307c478bd9Sstevel@tonic-gate 			prognamp);
1317c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Usage: pwconv\n"));
1327c478bd9Sstevel@tonic-gate 		exit(BADSYN);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* lock file so that only one process can read or write at a time */
1367c478bd9Sstevel@tonic-gate 	if (lckpwdf() < 0) {
1377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1387c478bd9Sstevel@tonic-gate 		gettext("%s: Password file(s) busy.  Try again later.\n"),
1397c478bd9Sstevel@tonic-gate 			prognamp);
1407c478bd9Sstevel@tonic-gate 		exit(FBUSY);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/* All signals will be ignored during the execution of pwconv */
1447c478bd9Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++)
1457c478bd9Sstevel@tonic-gate 		sigset(i, SIG_IGN);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* reset errno to avoid side effects of a failed */
1487c478bd9Sstevel@tonic-gate 	/* sigset (e.g., SIGKILL) */
1497c478bd9Sstevel@tonic-gate 	errno = 0;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/* check the file status of the password file */
1527c478bd9Sstevel@tonic-gate 	/* get the gid of the password file */
1537c478bd9Sstevel@tonic-gate 	if (stat(PASSWD, &buf) < 0) {
1547c478bd9Sstevel@tonic-gate 		(void) f_miss();
1557c478bd9Sstevel@tonic-gate 		exit(FATAL);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	pwd_gid = buf.st_gid;
1587c478bd9Sstevel@tonic-gate 	pwd_uid = buf.st_uid;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/* mode for the password file should be read-only or less */
1617c478bd9Sstevel@tonic-gate 	umask(S_IAMB & ~(buf.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/* open temporary password file */
1647c478bd9Sstevel@tonic-gate 	if ((tp_fp = fopen(PASSTEMP, "w")) == NULL) {
1657c478bd9Sstevel@tonic-gate 		(void) f_err();
1667c478bd9Sstevel@tonic-gate 		exit(FMERR);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (chown(PASSTEMP, pwd_uid, pwd_gid) < 0) {
1707c478bd9Sstevel@tonic-gate 		DELPTMP();
1717c478bd9Sstevel@tonic-gate 		(void) f_err();
1727c478bd9Sstevel@tonic-gate 		exit(FMERR);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	/* default mode mask of the shadow file */
1757c478bd9Sstevel@tonic-gate 	mode = S_IAMB & ~(S_IRUSR);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* check the existence of  shadow file */
1787c478bd9Sstevel@tonic-gate 	/* if the shadow file exists, get mode mask and group id of the file */
1797c478bd9Sstevel@tonic-gate 	/* if file does not exist, the default group name will be the group  */
1807c478bd9Sstevel@tonic-gate 	/* name of the password file.  */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (access(SHADOW, F_OK) == 0) {
1837c478bd9Sstevel@tonic-gate 		if (stat(SHADOW, &buf) == 0) {
1847c478bd9Sstevel@tonic-gate 			mode  = S_IAMB & ~(buf.st_mode & S_IRUSR);
1857c478bd9Sstevel@tonic-gate 			sp_gid = buf.st_gid;
1867c478bd9Sstevel@tonic-gate 			sp_uid = buf.st_uid;
1877c478bd9Sstevel@tonic-gate 		} else {
1887c478bd9Sstevel@tonic-gate 			DELPTMP();
1897c478bd9Sstevel@tonic-gate 			(void) f_err();
1907c478bd9Sstevel@tonic-gate 			exit(FMERR);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	} else {
1937c478bd9Sstevel@tonic-gate 		sp_gid = pwd_gid;
1947c478bd9Sstevel@tonic-gate 		sp_uid = pwd_uid;
1957c478bd9Sstevel@tonic-gate 		file_exist = 0;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * get the mode of shadow password file  -- mode of the file should
1997c478bd9Sstevel@tonic-gate 	 * be read-only for user or less.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	umask(mode);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* open temporary shadow file */
2047c478bd9Sstevel@tonic-gate 	if ((tsp_fp = fopen(SHADTEMP, "w")) == NULL) {
2057c478bd9Sstevel@tonic-gate 		DELPTMP();
2067c478bd9Sstevel@tonic-gate 		(void) f_err();
2077c478bd9Sstevel@tonic-gate 		exit(FMERR);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* change the group of the temporary shadow password file */
2117c478bd9Sstevel@tonic-gate 	if (chown(SHADTEMP, sp_uid, sp_gid) < 0) {
2127c478bd9Sstevel@tonic-gate 		(void) no_convert();
2137c478bd9Sstevel@tonic-gate 		exit(FMERR);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* Reads the password file.  				*/
2177c478bd9Sstevel@tonic-gate 	/* If the shadow password file not exists, or		*/
2187c478bd9Sstevel@tonic-gate 	/* if an entry doesn't have a corresponding entry in    */
2197c478bd9Sstevel@tonic-gate 	/* the shadow file, entries/entry will be created.	*/
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
2227c478bd9Sstevel@tonic-gate 		no_recover();
2237c478bd9Sstevel@tonic-gate 		exit(FATAL);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	count = 0;
2277c478bd9Sstevel@tonic-gate 	while (!end_of_file) {
2287c478bd9Sstevel@tonic-gate 		count++;
2297c478bd9Sstevel@tonic-gate 		if ((pwdp = fgetpwent(pwf)) != NULL) {
2307c478bd9Sstevel@tonic-gate 			if (!file_exist ||
2317c478bd9Sstevel@tonic-gate 			    (sp = local_getspnam(pwdp->pw_name)) == NULL) {
2327c478bd9Sstevel@tonic-gate 				if (errno == EINVAL) {
2337c478bd9Sstevel@tonic-gate 				/* Bad entry in shadow exit */
2347c478bd9Sstevel@tonic-gate 					DELSHWTMP();
2357c478bd9Sstevel@tonic-gate 					DELPTMP();
2367c478bd9Sstevel@tonic-gate 					(void) f_bdshw();
2377c478bd9Sstevel@tonic-gate 					exit(BADSHW);
2387c478bd9Sstevel@tonic-gate 				}
2397c478bd9Sstevel@tonic-gate 				sp = &sp_pwd;
2407c478bd9Sstevel@tonic-gate 				sp->sp_namp = pwdp->pw_name;
2417c478bd9Sstevel@tonic-gate 				if (!pwdp->pw_passwd ||
2427c478bd9Sstevel@tonic-gate 				    (pwdp->pw_passwd &&
2437c478bd9Sstevel@tonic-gate 					*pwdp->pw_passwd == NULL)) {
2447c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
2457c478bd9Sstevel@tonic-gate 			gettext("%s: WARNING user %s has no password\n"),
2467c478bd9Sstevel@tonic-gate 					prognamp, sp->sp_namp);
2477c478bd9Sstevel@tonic-gate 				}
2487c478bd9Sstevel@tonic-gate 				/*
2497c478bd9Sstevel@tonic-gate 				 * copy the password field in the password
2507c478bd9Sstevel@tonic-gate 				 * file to the shadow file.
2517c478bd9Sstevel@tonic-gate 				 * replace the password field with an 'x'.
2527c478bd9Sstevel@tonic-gate 				 */
2537c478bd9Sstevel@tonic-gate 				sp->sp_pwdp = pwdp->pw_passwd;
2547c478bd9Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
2557c478bd9Sstevel@tonic-gate 				/*
2567c478bd9Sstevel@tonic-gate 				 * if aging, split the aging info
2577c478bd9Sstevel@tonic-gate 				 * into age, max and min
2587c478bd9Sstevel@tonic-gate 				 * convert aging info from weeks to days
2597c478bd9Sstevel@tonic-gate 				 */
2607c478bd9Sstevel@tonic-gate 				if (pwdp->pw_age && *pwdp->pw_age != NULL) {
2617c478bd9Sstevel@tonic-gate 					when = (long) a64l(pwdp->pw_age);
2627c478bd9Sstevel@tonic-gate 					maxweeks = when & 077;
2637c478bd9Sstevel@tonic-gate 					minweeks = (when >> 6) & 077;
2647c478bd9Sstevel@tonic-gate 					when >>= 12;
2657c478bd9Sstevel@tonic-gate 					sp->sp_lstchg = when * 7;
2667c478bd9Sstevel@tonic-gate 					sp->sp_min = minweeks * 7;
2677c478bd9Sstevel@tonic-gate 					sp->sp_max = maxweeks * 7;
2687c478bd9Sstevel@tonic-gate 					sp->sp_warn = -1;
2697c478bd9Sstevel@tonic-gate 					sp->sp_inact = -1;
2707c478bd9Sstevel@tonic-gate 					sp->sp_expire = -1;
2717c478bd9Sstevel@tonic-gate 					sp->sp_flag = 0;
2727c478bd9Sstevel@tonic-gate 					pwdp->pw_age = "";  /* do we care? */
2737c478bd9Sstevel@tonic-gate 				} else {
2747c478bd9Sstevel@tonic-gate 				/*
2757c478bd9Sstevel@tonic-gate 				 * if !aging, last_changed will be the day the
2767c478bd9Sstevel@tonic-gate 				 * conversion is done, min and max fields will
2777c478bd9Sstevel@tonic-gate 				 * be null - use timezone to get local time
2787c478bd9Sstevel@tonic-gate 				 */
2797c478bd9Sstevel@tonic-gate 					sp->sp_lstchg = DAY_NOW;
2807c478bd9Sstevel@tonic-gate 					sp->sp_min =  -1;
2817c478bd9Sstevel@tonic-gate 					sp->sp_max =  -1;
2827c478bd9Sstevel@tonic-gate 					sp->sp_warn = -1;
2837c478bd9Sstevel@tonic-gate 					sp->sp_inact = -1;
2847c478bd9Sstevel@tonic-gate 					sp->sp_expire = -1;
2857c478bd9Sstevel@tonic-gate 					sp->sp_flag = 0;
2867c478bd9Sstevel@tonic-gate 				}
2877c478bd9Sstevel@tonic-gate 	    } else {
2887c478bd9Sstevel@tonic-gate 			/*
2897c478bd9Sstevel@tonic-gate 			 * if the passwd field has a string other than 'x',
2907c478bd9Sstevel@tonic-gate 			 * the entry will be written into the shadow file
2917c478bd9Sstevel@tonic-gate 			 * and the character 'x' is re-written as the passwd
2927c478bd9Sstevel@tonic-gate 			 * if !aging, last_changed as above
2937c478bd9Sstevel@tonic-gate 			 */
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			/*
2967c478bd9Sstevel@tonic-gate 			 * with NIS, only warn about password missing if entry
2977c478bd9Sstevel@tonic-gate 			 * is not a NIS-lookup entry ("+" or "-")
2987c478bd9Sstevel@tonic-gate 			 * black_magic from getpwnam_r.c
2997c478bd9Sstevel@tonic-gate 			 */
3007c478bd9Sstevel@tonic-gate 			black_magic = (*pwdp->pw_name == '+' ||
3017c478bd9Sstevel@tonic-gate 					*pwdp->pw_name == '-');
3027c478bd9Sstevel@tonic-gate 			/*
3037c478bd9Sstevel@tonic-gate 			 * moan about absence of non "+/-" passwd
3047c478bd9Sstevel@tonic-gate 			 * we could do more, but what?
3057c478bd9Sstevel@tonic-gate 			 */
3067c478bd9Sstevel@tonic-gate 			if ((!pwdp->pw_passwd ||
3077c478bd9Sstevel@tonic-gate 			    (pwdp->pw_passwd && *pwdp->pw_passwd == NULL)) &&
3087c478bd9Sstevel@tonic-gate 			    !black_magic) {
3097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3107c478bd9Sstevel@tonic-gate 			gettext("%s: WARNING user %s has no password\n"),
3117c478bd9Sstevel@tonic-gate 					prognamp, sp->sp_namp);
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 			if (pwdp->pw_passwd && *pwdp->pw_passwd) {
3147c478bd9Sstevel@tonic-gate 				if (strcmp(pwdp->pw_passwd, pwdflr)) {
3157c478bd9Sstevel@tonic-gate 					sp->sp_pwdp = pwdp->pw_passwd;
3167c478bd9Sstevel@tonic-gate 					pwdp->pw_passwd = pwdflr;
3177c478bd9Sstevel@tonic-gate 					if (!pwdp->pw_age ||
3187c478bd9Sstevel@tonic-gate 					    (pwdp->pw_age &&
3197c478bd9Sstevel@tonic-gate 						*pwdp->pw_age == NULL)) {
3207c478bd9Sstevel@tonic-gate 						sp->sp_lstchg = DAY_NOW;
3217c478bd9Sstevel@tonic-gate 						sp->sp_min =  -1;
3227c478bd9Sstevel@tonic-gate 						sp->sp_max =  -1;
3237c478bd9Sstevel@tonic-gate 						sp->sp_warn = -1;
3247c478bd9Sstevel@tonic-gate 						sp->sp_inact = -1;
3257c478bd9Sstevel@tonic-gate 						sp->sp_expire = -1;
3267c478bd9Sstevel@tonic-gate 						sp->sp_flag = 0;
3277c478bd9Sstevel@tonic-gate 					}
3287c478bd9Sstevel@tonic-gate 				}
3297c478bd9Sstevel@tonic-gate 			} else {
3307c478bd9Sstevel@tonic-gate 				/*
3317c478bd9Sstevel@tonic-gate 				 * black_magic needs a non-null passwd
3327c478bd9Sstevel@tonic-gate 				 * and pwdflr seem appropriate here
3337c478bd9Sstevel@tonic-gate 				 * clear garbage if any
3347c478bd9Sstevel@tonic-gate 				 */
3357c478bd9Sstevel@tonic-gate 				sp->sp_pwdp = "";
3367c478bd9Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
3377c478bd9Sstevel@tonic-gate 				sp->sp_lstchg = sp->sp_min = sp->sp_max = -1;
3387c478bd9Sstevel@tonic-gate 				sp->sp_warn = sp->sp_inact = sp->sp_expire = -1;
3397c478bd9Sstevel@tonic-gate 				sp->sp_flag = 0;
3407c478bd9Sstevel@tonic-gate 			}
3417c478bd9Sstevel@tonic-gate 			/*
3427c478bd9Sstevel@tonic-gate 			 * if aging, split the aging info
3437c478bd9Sstevel@tonic-gate 			 * into age, max and min
3447c478bd9Sstevel@tonic-gate 			 * convert aging info from weeks to days
3457c478bd9Sstevel@tonic-gate 			 */
3467c478bd9Sstevel@tonic-gate 			if (pwdp->pw_age && *pwdp->pw_age != NULL) {
3477c478bd9Sstevel@tonic-gate 				when = (long) a64l(pwdp->pw_age);
3487c478bd9Sstevel@tonic-gate 				maxweeks = when & 077;
3497c478bd9Sstevel@tonic-gate 				minweeks = (when >> 6) & 077;
3507c478bd9Sstevel@tonic-gate 				when >>= 12;
3517c478bd9Sstevel@tonic-gate 				sp->sp_lstchg = when * 7;
3527c478bd9Sstevel@tonic-gate 				sp->sp_min = minweeks * 7;
3537c478bd9Sstevel@tonic-gate 				sp->sp_max = maxweeks * 7;
3547c478bd9Sstevel@tonic-gate 				sp->sp_warn = -1;
3557c478bd9Sstevel@tonic-gate 				sp->sp_inact = -1;
3567c478bd9Sstevel@tonic-gate 				sp->sp_expire = -1;
3577c478bd9Sstevel@tonic-gate 				sp->sp_flag = 0;
3587c478bd9Sstevel@tonic-gate 				pwdp->pw_age = ""; /* do we care? */
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		/* write an entry to temporary password file */
3637c478bd9Sstevel@tonic-gate 		if ((putpwent(pwdp, tp_fp)) != 0) {
3647c478bd9Sstevel@tonic-gate 			(void) no_convert();
3657c478bd9Sstevel@tonic-gate 			exit(FMERR);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		/* write an entry to temporary shadow password file */
3697c478bd9Sstevel@tonic-gate 		if (putspent(sp, tsp_fp) != 0) {
3707c478bd9Sstevel@tonic-gate 			(void) no_convert();
3717c478bd9Sstevel@tonic-gate 			exit(FMERR);
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 	    } else {
3747c478bd9Sstevel@tonic-gate 		if (feof(pwf)) {
3757c478bd9Sstevel@tonic-gate 			end_of_file = 1;
3767c478bd9Sstevel@tonic-gate 		} else {
3777c478bd9Sstevel@tonic-gate 			errno = 0;
3787c478bd9Sstevel@tonic-gate 			pwerr = 1;
3797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3807c478bd9Sstevel@tonic-gate 			gettext("%s: ERROR: bad entry or blank line at line %d in /etc/passwd\n"),
3817c478bd9Sstevel@tonic-gate 					prognamp, count);
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	    }
3847c478bd9Sstevel@tonic-gate 	} /* end of while */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	(void) fclose(pwf);
3877c478bd9Sstevel@tonic-gate 	(void) fclose(tsp_fp);
3887c478bd9Sstevel@tonic-gate 	(void) fclose(tp_fp);
3897c478bd9Sstevel@tonic-gate 	if (pwerr) {
3907c478bd9Sstevel@tonic-gate 		(void) no_convert();
3917c478bd9Sstevel@tonic-gate 		exit(FMERR);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* delete old password file if it exists */
3957c478bd9Sstevel@tonic-gate 	if (unlink(OPASSWD) && (access(OPASSWD, F_OK) == 0)) {
3967c478bd9Sstevel@tonic-gate 		(void) no_convert();
3977c478bd9Sstevel@tonic-gate 		exit(FMERR);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* rename the password file to old password file  */
4017c478bd9Sstevel@tonic-gate 	if (rename(PASSWD, OPASSWD) == -1) {
4027c478bd9Sstevel@tonic-gate 		(void) no_convert();
4037c478bd9Sstevel@tonic-gate 		exit(FMERR);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* rename temporary password file to password file */
4077c478bd9Sstevel@tonic-gate 	if (rename(PASSTEMP, PASSWD) == -1) {
4087c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4097c478bd9Sstevel@tonic-gate 		if (link(OPASSWD, PASSWD) < 0) {
4107c478bd9Sstevel@tonic-gate 			(void) no_recover();
4117c478bd9Sstevel@tonic-gate 			exit(FATAL);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		(void) no_convert();
4147c478bd9Sstevel@tonic-gate 		exit(FMERR);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/* delete old shadow password file if it exists */
4187c478bd9Sstevel@tonic-gate 	if (unlink(OSHADOW) && (access(OSHADOW, R_OK) == 0)) {
4197c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4207c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4217c478bd9Sstevel@tonic-gate 			(void) no_recover();
4227c478bd9Sstevel@tonic-gate 			exit(FATAL);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		(void) no_convert();
4257c478bd9Sstevel@tonic-gate 		exit(FMERR);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* link shadow password file to old shadow password file */
4297c478bd9Sstevel@tonic-gate 	if (file_exist && rename(SHADOW, OSHADOW)) {
4307c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4317c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4327c478bd9Sstevel@tonic-gate 			(void) no_recover();
4337c478bd9Sstevel@tonic-gate 			exit(FATAL);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 		(void) no_convert();
4367c478bd9Sstevel@tonic-gate 		exit(FMERR);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* link temporary shadow password file to shadow password file */
4417c478bd9Sstevel@tonic-gate 	if (rename(SHADTEMP, SHADOW) == -1) {
4427c478bd9Sstevel@tonic-gate 		/* link old shadow password file to shadow password file */
4437c478bd9Sstevel@tonic-gate 		if (file_exist && (link(OSHADOW, SHADOW))) {
4447c478bd9Sstevel@tonic-gate 			(void) no_recover();
4457c478bd9Sstevel@tonic-gate 			exit(FATAL);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4487c478bd9Sstevel@tonic-gate 			(void) no_recover();
4497c478bd9Sstevel@tonic-gate 			exit(FATAL);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		(void) no_convert();
4527c478bd9Sstevel@tonic-gate 		exit(FMERR);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* Change old password file to read only by owner   */
4567c478bd9Sstevel@tonic-gate 	/* If chmod fails, delete the old password file so that */
4577c478bd9Sstevel@tonic-gate 	/* the password fields can not be read by others */
4587c478bd9Sstevel@tonic-gate 	if (chmod(OPASSWD, S_IRUSR) < 0)
4597c478bd9Sstevel@tonic-gate 		(void) unlink(OPASSWD);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
462*49335bdeSbasabi 	return (0);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate void
466*49335bdeSbasabi no_recover(void)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	DELPTMP();
4697c478bd9Sstevel@tonic-gate 	DELSHWTMP();
4707c478bd9Sstevel@tonic-gate 	(void) f_miss();
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate void
474*49335bdeSbasabi no_convert(void)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	DELPTMP();
4777c478bd9Sstevel@tonic-gate 	DELSHWTMP();
4787c478bd9Sstevel@tonic-gate 	(void) f_err();
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate void
482*49335bdeSbasabi f_err(void)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	fprintf(stderr,
4857c478bd9Sstevel@tonic-gate 		gettext("%s: Unexpected failure. Conversion not done.\n"),
4867c478bd9Sstevel@tonic-gate 			prognamp);
4877c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate void
491*49335bdeSbasabi f_miss(void)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	fprintf(stderr,
4947c478bd9Sstevel@tonic-gate 		gettext("%s: Unexpected failure. Password file(s) missing.\n"),
4957c478bd9Sstevel@tonic-gate 			prognamp);
4967c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate void
500*49335bdeSbasabi f_bdshw(void)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	fprintf(stderr,
5037c478bd9Sstevel@tonic-gate 		gettext("%s: Bad entry in /etc/shadow. Conversion not done.\n"),
5047c478bd9Sstevel@tonic-gate 			prognamp);
5057c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
5067c478bd9Sstevel@tonic-gate }
507