xref: /illumos-gate/usr/src/cmd/pwconv/pwconv.c (revision 2c7de944)
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
542487ff1Sas  * Common Development and Distribution License (the "License").
642487ff1Sas  * 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  */
2142487ff1Sas 
227c478bd9Sstevel@tonic-gate /*
2342487ff1Sas  * 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	*/
28*2c7de944SToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*  pwconv.c  */
317c478bd9Sstevel@tonic-gate /*  Conversion aid to copy appropriate fields from the	*/
327c478bd9Sstevel@tonic-gate /*  password file to the shadow file.			*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <pwd.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <time.h>
407c478bd9Sstevel@tonic-gate #include <shadow.h>
417c478bd9Sstevel@tonic-gate #include <grp.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <locale.h>
4742487ff1Sas #include <string.h>
487c478bd9Sstevel@tonic-gate 
49*2c7de944SToomas Soome #define	PRIVILEGED	0			/* privileged id */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* exit  code */
527c478bd9Sstevel@tonic-gate #define	SUCCESS	0	/* succeeded */
537c478bd9Sstevel@tonic-gate #define	NOPERM	1	/* No permission */
547c478bd9Sstevel@tonic-gate #define	BADSYN	2	/* Incorrect syntax */
557c478bd9Sstevel@tonic-gate #define	FMERR	3	/* File manipulation error */
567c478bd9Sstevel@tonic-gate #define	FATAL	4	/* Old file can not be recover */
577c478bd9Sstevel@tonic-gate #define	FBUSY	5	/* Lock file busy */
587c478bd9Sstevel@tonic-gate #define	BADSHW	6	/* Bad entry in shadow file  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	DELPTMP()	(void) unlink(PASSTEMP)
617c478bd9Sstevel@tonic-gate #define	DELSHWTMP()	(void) unlink(SHADTEMP)
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate char pwdflr[]	= "x";				/* password filler */
647c478bd9Sstevel@tonic-gate char *prognamp;
6549335bdeSbasabi void f_err(void), f_miss(void), f_bdshw(void);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * getspnan routine that ONLY looks at the local shadow file
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate struct spwd *
local_getspnam(char * name)7149335bdeSbasabi local_getspnam(char *name)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	FILE *shadf;
7442487ff1Sas 	struct spwd *sp;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if ((shadf = fopen("/etc/shadow", "r")) == NULL)
787c478bd9Sstevel@tonic-gate 		return (NULL);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	while ((sp = fgetspent(shadf)) != NULL) {
817c478bd9Sstevel@tonic-gate 		if (strcmp(sp->sp_namp, name) == 0)
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
8542487ff1Sas 	(void) fclose(shadf);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	return (sp);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
9049335bdeSbasabi int
main(int argc,char ** argv)9149335bdeSbasabi main(int argc, char **argv)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	extern	int	errno;
9449335bdeSbasabi 	void  no_recover(void), no_convert(void);
957c478bd9Sstevel@tonic-gate 	struct  passwd  *pwdp;
967c478bd9Sstevel@tonic-gate 	struct	spwd	*sp, sp_pwd;		/* default entry */
977c478bd9Sstevel@tonic-gate 	struct stat buf;
987c478bd9Sstevel@tonic-gate 	FILE	*tp_fp, *tsp_fp;
9949335bdeSbasabi 	time_t	when, minweeks, maxweeks;
1007c478bd9Sstevel@tonic-gate 	int file_exist = 1;
1017c478bd9Sstevel@tonic-gate 	int end_of_file = 0;
1027c478bd9Sstevel@tonic-gate 	mode_t mode;
10342487ff1Sas 	mode_t pwd_mode;
1047c478bd9Sstevel@tonic-gate 	int pwerr = 0;
10542487ff1Sas 	ushort_t 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) {
12242487ff1Sas 		(void) fprintf(stderr, gettext("%s: Permission denied.\n"),
12342487ff1Sas 		    prognamp);
1247c478bd9Sstevel@tonic-gate 		exit(NOPERM);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* No argument can be passed to the command */
1287c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
130*2c7de944SToomas Soome 		    gettext("%s: Invalid command syntax.\n"), prognamp);
13142487ff1Sas 		(void) 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,
138*2c7de944SToomas Soome 		    gettext("%s: Password file(s) busy.  Try again later.\n"),
139*2c7de944SToomas Soome 		    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++)
14542487ff1Sas 		(void) 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;
15942487ff1Sas 	pwd_mode = buf.st_mode;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/* mode for the password file should be read-only or less */
16242487ff1Sas 	(void) umask(S_IAMB & ~(buf.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)));
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* open temporary password file */
1657c478bd9Sstevel@tonic-gate 	if ((tp_fp = fopen(PASSTEMP, "w")) == NULL) {
1667c478bd9Sstevel@tonic-gate 		(void) f_err();
1677c478bd9Sstevel@tonic-gate 		exit(FMERR);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (chown(PASSTEMP, pwd_uid, pwd_gid) < 0) {
1717c478bd9Sstevel@tonic-gate 		DELPTMP();
1727c478bd9Sstevel@tonic-gate 		(void) f_err();
1737c478bd9Sstevel@tonic-gate 		exit(FMERR);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	/* default mode mask of the shadow file */
1767c478bd9Sstevel@tonic-gate 	mode = S_IAMB & ~(S_IRUSR);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* check the existence of  shadow file */
1797c478bd9Sstevel@tonic-gate 	/* if the shadow file exists, get mode mask and group id of the file */
1807c478bd9Sstevel@tonic-gate 	/* if file does not exist, the default group name will be the group  */
1817c478bd9Sstevel@tonic-gate 	/* name of the password file.  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (access(SHADOW, F_OK) == 0) {
1847c478bd9Sstevel@tonic-gate 		if (stat(SHADOW, &buf) == 0) {
1857c478bd9Sstevel@tonic-gate 			mode  = S_IAMB & ~(buf.st_mode & S_IRUSR);
1867c478bd9Sstevel@tonic-gate 			sp_gid = buf.st_gid;
1877c478bd9Sstevel@tonic-gate 			sp_uid = buf.st_uid;
1887c478bd9Sstevel@tonic-gate 		} else {
1897c478bd9Sstevel@tonic-gate 			DELPTMP();
1907c478bd9Sstevel@tonic-gate 			(void) f_err();
1917c478bd9Sstevel@tonic-gate 			exit(FMERR);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	} else {
1947c478bd9Sstevel@tonic-gate 		sp_gid = pwd_gid;
1957c478bd9Sstevel@tonic-gate 		sp_uid = pwd_uid;
1967c478bd9Sstevel@tonic-gate 		file_exist = 0;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * get the mode of shadow password file  -- mode of the file should
2007c478bd9Sstevel@tonic-gate 	 * be read-only for user or less.
2017c478bd9Sstevel@tonic-gate 	 */
20242487ff1Sas 	(void) umask(mode);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* open temporary shadow file */
2057c478bd9Sstevel@tonic-gate 	if ((tsp_fp = fopen(SHADTEMP, "w")) == NULL) {
2067c478bd9Sstevel@tonic-gate 		DELPTMP();
2077c478bd9Sstevel@tonic-gate 		(void) f_err();
2087c478bd9Sstevel@tonic-gate 		exit(FMERR);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* change the group of the temporary shadow password file */
2127c478bd9Sstevel@tonic-gate 	if (chown(SHADTEMP, sp_uid, sp_gid) < 0) {
2137c478bd9Sstevel@tonic-gate 		(void) no_convert();
2147c478bd9Sstevel@tonic-gate 		exit(FMERR);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
217*2c7de944SToomas Soome 	/* Reads the password file.				*/
2187c478bd9Sstevel@tonic-gate 	/* If the shadow password file not exists, or		*/
2197c478bd9Sstevel@tonic-gate 	/* if an entry doesn't have a corresponding entry in    */
2207c478bd9Sstevel@tonic-gate 	/* the shadow file, entries/entry will be created.	*/
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
2237c478bd9Sstevel@tonic-gate 		no_recover();
2247c478bd9Sstevel@tonic-gate 		exit(FATAL);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	count = 0;
2287c478bd9Sstevel@tonic-gate 	while (!end_of_file) {
2297c478bd9Sstevel@tonic-gate 		count++;
2307c478bd9Sstevel@tonic-gate 		if ((pwdp = fgetpwent(pwf)) != NULL) {
2317c478bd9Sstevel@tonic-gate 			if (!file_exist ||
2327c478bd9Sstevel@tonic-gate 			    (sp = local_getspnam(pwdp->pw_name)) == NULL) {
2337c478bd9Sstevel@tonic-gate 				if (errno == EINVAL) {
2347c478bd9Sstevel@tonic-gate 				/* Bad entry in shadow exit */
2357c478bd9Sstevel@tonic-gate 					DELSHWTMP();
2367c478bd9Sstevel@tonic-gate 					DELPTMP();
2377c478bd9Sstevel@tonic-gate 					(void) f_bdshw();
2387c478bd9Sstevel@tonic-gate 					exit(BADSHW);
2397c478bd9Sstevel@tonic-gate 				}
2407c478bd9Sstevel@tonic-gate 				sp = &sp_pwd;
2417c478bd9Sstevel@tonic-gate 				sp->sp_namp = pwdp->pw_name;
2427c478bd9Sstevel@tonic-gate 				if (!pwdp->pw_passwd ||
2437c478bd9Sstevel@tonic-gate 				    (pwdp->pw_passwd &&
244*2c7de944SToomas Soome 				    *pwdp->pw_passwd == '\0')) {
245*2c7de944SToomas Soome 					(void) fprintf(stderr, gettext(
246*2c7de944SToomas Soome 					    "%s: WARNING user %s has no "
247*2c7de944SToomas Soome 					    "password\n"),
248*2c7de944SToomas Soome 					    prognamp, sp->sp_namp);
2497c478bd9Sstevel@tonic-gate 				}
2507c478bd9Sstevel@tonic-gate 				/*
2517c478bd9Sstevel@tonic-gate 				 * copy the password field in the password
2527c478bd9Sstevel@tonic-gate 				 * file to the shadow file.
2537c478bd9Sstevel@tonic-gate 				 * replace the password field with an 'x'.
2547c478bd9Sstevel@tonic-gate 				 */
2557c478bd9Sstevel@tonic-gate 				sp->sp_pwdp = pwdp->pw_passwd;
2567c478bd9Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
2577c478bd9Sstevel@tonic-gate 				/*
2587c478bd9Sstevel@tonic-gate 				 * if aging, split the aging info
2597c478bd9Sstevel@tonic-gate 				 * into age, max and min
2607c478bd9Sstevel@tonic-gate 				 * convert aging info from weeks to days
2617c478bd9Sstevel@tonic-gate 				 */
262*2c7de944SToomas Soome 				if (pwdp->pw_age && *pwdp->pw_age != 0) {
26342487ff1Sas 					when = (long)a64l(pwdp->pw_age);
2647c478bd9Sstevel@tonic-gate 					maxweeks = when & 077;
2657c478bd9Sstevel@tonic-gate 					minweeks = (when >> 6) & 077;
2667c478bd9Sstevel@tonic-gate 					when >>= 12;
2677c478bd9Sstevel@tonic-gate 					sp->sp_lstchg = when * 7;
2687c478bd9Sstevel@tonic-gate 					sp->sp_min = minweeks * 7;
2697c478bd9Sstevel@tonic-gate 					sp->sp_max = maxweeks * 7;
2707c478bd9Sstevel@tonic-gate 					sp->sp_warn = -1;
2717c478bd9Sstevel@tonic-gate 					sp->sp_inact = -1;
2727c478bd9Sstevel@tonic-gate 					sp->sp_expire = -1;
2737c478bd9Sstevel@tonic-gate 					sp->sp_flag = 0;
2747c478bd9Sstevel@tonic-gate 					pwdp->pw_age = "";  /* do we care? */
2757c478bd9Sstevel@tonic-gate 				} else {
2767c478bd9Sstevel@tonic-gate 				/*
2777c478bd9Sstevel@tonic-gate 				 * if !aging, last_changed will be the day the
2787c478bd9Sstevel@tonic-gate 				 * conversion is done, min and max fields will
2797c478bd9Sstevel@tonic-gate 				 * be null - use timezone to get local time
2807c478bd9Sstevel@tonic-gate 				 */
2817c478bd9Sstevel@tonic-gate 					sp->sp_lstchg = DAY_NOW;
2827c478bd9Sstevel@tonic-gate 					sp->sp_min =  -1;
2837c478bd9Sstevel@tonic-gate 					sp->sp_max =  -1;
2847c478bd9Sstevel@tonic-gate 					sp->sp_warn = -1;
2857c478bd9Sstevel@tonic-gate 					sp->sp_inact = -1;
2867c478bd9Sstevel@tonic-gate 					sp->sp_expire = -1;
2877c478bd9Sstevel@tonic-gate 					sp->sp_flag = 0;
2887c478bd9Sstevel@tonic-gate 				}
289*2c7de944SToomas Soome 			} else {
290*2c7de944SToomas Soome 				/*
291*2c7de944SToomas Soome 				 * if the passwd field has a string other than
292*2c7de944SToomas Soome 				 * 'x', the entry will be written into the
293*2c7de944SToomas Soome 				 * shadow file and the character 'x' is
294*2c7de944SToomas Soome 				 * re-written as the passwd if !aging,
295*2c7de944SToomas Soome 				 * last_changed as above
296*2c7de944SToomas Soome 				 */
297*2c7de944SToomas Soome 
298*2c7de944SToomas Soome 				/*
299*2c7de944SToomas Soome 				 * with NIS, only warn about password missing
300*2c7de944SToomas Soome 				 * if entry is not a NIS-lookup entry
301*2c7de944SToomas Soome 				 * ("+" or "-") black_magic from getpwnam_r.c
302*2c7de944SToomas Soome 				 */
303*2c7de944SToomas Soome 				black_magic = (*pwdp->pw_name == '+' ||
304*2c7de944SToomas Soome 				    *pwdp->pw_name == '-');
305*2c7de944SToomas Soome 				/*
306*2c7de944SToomas Soome 				 * moan about absence of non "+/-" passwd
307*2c7de944SToomas Soome 				 * we could do more, but what?
308*2c7de944SToomas Soome 				 */
309*2c7de944SToomas Soome 				if ((!pwdp->pw_passwd ||
310*2c7de944SToomas Soome 				    (pwdp->pw_passwd &&
311*2c7de944SToomas Soome 				    *pwdp->pw_passwd == '\0')) &&
312*2c7de944SToomas Soome 				    !black_magic) {
313*2c7de944SToomas Soome 					(void) fprintf(stderr, gettext(
314*2c7de944SToomas Soome 					    "%s: WARNING user %s has no "
315*2c7de944SToomas Soome 					    "password\n"),
316*2c7de944SToomas Soome 					    prognamp, sp->sp_namp);
317*2c7de944SToomas Soome 				}
318*2c7de944SToomas Soome 				if (pwdp->pw_passwd && *pwdp->pw_passwd) {
319*2c7de944SToomas Soome 					if (strcmp(pwdp->pw_passwd, pwdflr)) {
320*2c7de944SToomas Soome 						sp->sp_pwdp = pwdp->pw_passwd;
321*2c7de944SToomas Soome 						pwdp->pw_passwd = pwdflr;
322*2c7de944SToomas Soome 						if (!pwdp->pw_age ||
323*2c7de944SToomas Soome 						    (pwdp->pw_age &&
324*2c7de944SToomas Soome 						    *pwdp->pw_age == 0)) {
325*2c7de944SToomas Soome 							sp->sp_lstchg = DAY_NOW;
326*2c7de944SToomas Soome 							sp->sp_min =  -1;
327*2c7de944SToomas Soome 							sp->sp_max =  -1;
328*2c7de944SToomas Soome 							sp->sp_warn = -1;
329*2c7de944SToomas Soome 							sp->sp_inact = -1;
330*2c7de944SToomas Soome 							sp->sp_expire = -1;
331*2c7de944SToomas Soome 							sp->sp_flag = 0;
332*2c7de944SToomas Soome 						}
3337c478bd9Sstevel@tonic-gate 					}
334*2c7de944SToomas Soome 				} else {
335*2c7de944SToomas Soome 					/*
336*2c7de944SToomas Soome 					 * black_magic needs a non-null passwd
337*2c7de944SToomas Soome 					 * and pwdflr seem appropriate here
338*2c7de944SToomas Soome 					 * clear garbage if any
339*2c7de944SToomas Soome 					 */
340*2c7de944SToomas Soome 					sp->sp_pwdp = "";
341*2c7de944SToomas Soome 					pwdp->pw_passwd = pwdflr;
342*2c7de944SToomas Soome 					sp->sp_lstchg = sp->sp_min =
343*2c7de944SToomas Soome 					    sp->sp_max = -1;
344*2c7de944SToomas Soome 					sp->sp_warn = sp->sp_inact =
345*2c7de944SToomas Soome 					    sp->sp_expire = -1;
346*2c7de944SToomas Soome 					sp->sp_flag = 0;
3477c478bd9Sstevel@tonic-gate 				}
3487c478bd9Sstevel@tonic-gate 				/*
349*2c7de944SToomas Soome 				 * if aging, split the aging info
350*2c7de944SToomas Soome 				 * into age, max and min
351*2c7de944SToomas Soome 				 * convert aging info from weeks to days
3527c478bd9Sstevel@tonic-gate 				 */
353*2c7de944SToomas Soome 				if (pwdp->pw_age && *pwdp->pw_age != '\0') {
354*2c7de944SToomas Soome 					when = (long)a64l(pwdp->pw_age);
355*2c7de944SToomas Soome 					maxweeks = when & 077;
356*2c7de944SToomas Soome 					minweeks = (when >> 6) & 077;
357*2c7de944SToomas Soome 					when >>= 12;
358*2c7de944SToomas Soome 					sp->sp_lstchg = when * 7;
359*2c7de944SToomas Soome 					sp->sp_min = minweeks * 7;
360*2c7de944SToomas Soome 					sp->sp_max = maxweeks * 7;
361*2c7de944SToomas Soome 					sp->sp_warn = -1;
362*2c7de944SToomas Soome 					sp->sp_inact = -1;
363*2c7de944SToomas Soome 					sp->sp_expire = -1;
364*2c7de944SToomas Soome 					sp->sp_flag = 0;
365*2c7de944SToomas Soome 					pwdp->pw_age = ""; /* do we care? */
366*2c7de944SToomas Soome 				}
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 
369*2c7de944SToomas Soome 			/* write an entry to temporary password file */
370*2c7de944SToomas Soome 			if ((putpwent(pwdp, tp_fp)) != 0) {
371*2c7de944SToomas Soome 				(void) no_convert();
372*2c7de944SToomas Soome 				exit(FMERR);
373*2c7de944SToomas Soome 			}
3747c478bd9Sstevel@tonic-gate 
375*2c7de944SToomas Soome 			/* write an entry to temporary shadow password file */
376*2c7de944SToomas Soome 			if (putspent(sp, tsp_fp) != 0) {
377*2c7de944SToomas Soome 				(void) no_convert();
378*2c7de944SToomas Soome 				exit(FMERR);
379*2c7de944SToomas Soome 			}
3807c478bd9Sstevel@tonic-gate 		} else {
381*2c7de944SToomas Soome 			if (feof(pwf)) {
382*2c7de944SToomas Soome 				end_of_file = 1;
383*2c7de944SToomas Soome 			} else {
384*2c7de944SToomas Soome 				errno = 0;
385*2c7de944SToomas Soome 				pwerr = 1;
386*2c7de944SToomas Soome 				(void) fprintf(stderr,
387*2c7de944SToomas Soome 				    gettext("%s: ERROR: bad entry or blank "
388*2c7de944SToomas Soome 				    "line at line %d in /etc/passwd\n"),
389*2c7de944SToomas Soome 				    prognamp, count);
390*2c7de944SToomas Soome 			}
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 	} /* end of while */
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	(void) fclose(pwf);
3957c478bd9Sstevel@tonic-gate 	(void) fclose(tsp_fp);
3967c478bd9Sstevel@tonic-gate 	(void) fclose(tp_fp);
3977c478bd9Sstevel@tonic-gate 	if (pwerr) {
3987c478bd9Sstevel@tonic-gate 		(void) no_convert();
3997c478bd9Sstevel@tonic-gate 		exit(FMERR);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/* delete old password file if it exists */
4037c478bd9Sstevel@tonic-gate 	if (unlink(OPASSWD) && (access(OPASSWD, F_OK) == 0)) {
4047c478bd9Sstevel@tonic-gate 		(void) no_convert();
4057c478bd9Sstevel@tonic-gate 		exit(FMERR);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/* rename the password file to old password file  */
4097c478bd9Sstevel@tonic-gate 	if (rename(PASSWD, OPASSWD) == -1) {
4107c478bd9Sstevel@tonic-gate 		(void) no_convert();
4117c478bd9Sstevel@tonic-gate 		exit(FMERR);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* rename temporary password file to password file */
4157c478bd9Sstevel@tonic-gate 	if (rename(PASSTEMP, PASSWD) == -1) {
4167c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4177c478bd9Sstevel@tonic-gate 		if (link(OPASSWD, PASSWD) < 0) {
4187c478bd9Sstevel@tonic-gate 			(void) no_recover();
4197c478bd9Sstevel@tonic-gate 			exit(FATAL);
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		(void) no_convert();
4227c478bd9Sstevel@tonic-gate 		exit(FMERR);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* delete old shadow password file if it exists */
4267c478bd9Sstevel@tonic-gate 	if (unlink(OSHADOW) && (access(OSHADOW, R_OK) == 0)) {
4277c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4287c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4297c478bd9Sstevel@tonic-gate 			(void) no_recover();
4307c478bd9Sstevel@tonic-gate 			exit(FATAL);
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		(void) no_convert();
4337c478bd9Sstevel@tonic-gate 		exit(FMERR);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/* link shadow password file to old shadow password file */
4377c478bd9Sstevel@tonic-gate 	if (file_exist && rename(SHADOW, OSHADOW)) {
4387c478bd9Sstevel@tonic-gate 		/* link old password file to password file */
4397c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4407c478bd9Sstevel@tonic-gate 			(void) no_recover();
4417c478bd9Sstevel@tonic-gate 			exit(FATAL);
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		(void) no_convert();
4447c478bd9Sstevel@tonic-gate 		exit(FMERR);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/* link temporary shadow password file to shadow password file */
4497c478bd9Sstevel@tonic-gate 	if (rename(SHADTEMP, SHADOW) == -1) {
4507c478bd9Sstevel@tonic-gate 		/* link old shadow password file to shadow password file */
4517c478bd9Sstevel@tonic-gate 		if (file_exist && (link(OSHADOW, SHADOW))) {
4527c478bd9Sstevel@tonic-gate 			(void) no_recover();
4537c478bd9Sstevel@tonic-gate 			exit(FATAL);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
4567c478bd9Sstevel@tonic-gate 			(void) no_recover();
4577c478bd9Sstevel@tonic-gate 			exit(FATAL);
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		(void) no_convert();
4607c478bd9Sstevel@tonic-gate 		exit(FMERR);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
46342487ff1Sas 	/* Make new mode same as old */
46442487ff1Sas 	(void) chmod(PASSWD, pwd_mode);
46542487ff1Sas 
4667c478bd9Sstevel@tonic-gate 	/* Change old password file to read only by owner   */
4677c478bd9Sstevel@tonic-gate 	/* If chmod fails, delete the old password file so that */
4687c478bd9Sstevel@tonic-gate 	/* the password fields can not be read by others */
4697c478bd9Sstevel@tonic-gate 	if (chmod(OPASSWD, S_IRUSR) < 0)
4707c478bd9Sstevel@tonic-gate 		(void) unlink(OPASSWD);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
47349335bdeSbasabi 	return (0);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate void
no_recover(void)47749335bdeSbasabi no_recover(void)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	DELPTMP();
4807c478bd9Sstevel@tonic-gate 	DELSHWTMP();
4817c478bd9Sstevel@tonic-gate 	(void) f_miss();
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate void
no_convert(void)48549335bdeSbasabi no_convert(void)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	DELPTMP();
4887c478bd9Sstevel@tonic-gate 	DELSHWTMP();
4897c478bd9Sstevel@tonic-gate 	(void) f_err();
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate void
f_err(void)49349335bdeSbasabi f_err(void)
4947c478bd9Sstevel@tonic-gate {
49542487ff1Sas 	(void) fprintf(stderr,
496*2c7de944SToomas Soome 	    gettext("%s: Unexpected failure. Conversion not done.\n"),
497*2c7de944SToomas Soome 	    prognamp);
4987c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate void
f_miss(void)50249335bdeSbasabi f_miss(void)
5037c478bd9Sstevel@tonic-gate {
50442487ff1Sas 	(void) fprintf(stderr,
505*2c7de944SToomas Soome 	    gettext("%s: Unexpected failure. Password file(s) missing.\n"),
506*2c7de944SToomas Soome 	    prognamp);
5077c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate void
f_bdshw(void)51149335bdeSbasabi f_bdshw(void)
5127c478bd9Sstevel@tonic-gate {
51342487ff1Sas 	(void) fprintf(stderr,
514*2c7de944SToomas Soome 	    gettext("%s: Bad entry in /etc/shadow. Conversion not done.\n"),
515*2c7de944SToomas Soome 	    prognamp);
5167c478bd9Sstevel@tonic-gate 	(void) ulckpwdf();
5177c478bd9Sstevel@tonic-gate }
518