xref: /onnv-gate/usr/src/cmd/pwconv/pwconv.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*  pwconv.c  */
33*0Sstevel@tonic-gate /*  Conversion aid to copy appropriate fields from the	*/
34*0Sstevel@tonic-gate /*  password file to the shadow file.			*/
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <pwd.h>
37*0Sstevel@tonic-gate #include <fcntl.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/stat.h>
41*0Sstevel@tonic-gate #include <time.h>
42*0Sstevel@tonic-gate #include <shadow.h>
43*0Sstevel@tonic-gate #include <grp.h>
44*0Sstevel@tonic-gate #include <signal.h>
45*0Sstevel@tonic-gate #include <errno.h>
46*0Sstevel@tonic-gate #include <unistd.h>
47*0Sstevel@tonic-gate #include <stdlib.h>
48*0Sstevel@tonic-gate #include <locale.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	PRIVILEGED	0 			/* priviledged id */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /* exit  code */
53*0Sstevel@tonic-gate #define	SUCCESS	0	/* succeeded */
54*0Sstevel@tonic-gate #define	NOPERM	1	/* No permission */
55*0Sstevel@tonic-gate #define	BADSYN	2	/* Incorrect syntax */
56*0Sstevel@tonic-gate #define	FMERR	3	/* File manipulation error */
57*0Sstevel@tonic-gate #define	FATAL	4	/* Old file can not be recover */
58*0Sstevel@tonic-gate #define	FBUSY	5	/* Lock file busy */
59*0Sstevel@tonic-gate #define	BADSHW	6	/* Bad entry in shadow file  */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #define	DELPTMP()	(void) unlink(PASSTEMP)
62*0Sstevel@tonic-gate #define	DELSHWTMP()	(void) unlink(SHADTEMP)
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate char pwdflr[]	= "x";				/* password filler */
65*0Sstevel@tonic-gate char *prognamp;
66*0Sstevel@tonic-gate void f_err(), f_miss(), f_bdshw();
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * getspnan routine that ONLY looks at the local shadow file
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate struct spwd *
72*0Sstevel@tonic-gate local_getspnam(name)
73*0Sstevel@tonic-gate char *name;
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	FILE *shadf;
76*0Sstevel@tonic-gate 	struct spwd * sp;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	if ((shadf = fopen("/etc/shadow", "r")) == NULL)
80*0Sstevel@tonic-gate 		return (NULL);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	while ((sp = fgetspent(shadf)) != NULL) {
83*0Sstevel@tonic-gate 		if (strcmp(sp->sp_namp, name) == 0)
84*0Sstevel@tonic-gate 			break;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	fclose(shadf);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	return (sp);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate main(argc, argv)
93*0Sstevel@tonic-gate int argc;
94*0Sstevel@tonic-gate char **argv;
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	extern	int	errno;
97*0Sstevel@tonic-gate 	void  no_recover(), no_convert();
98*0Sstevel@tonic-gate 	struct  passwd  *pwdp;
99*0Sstevel@tonic-gate 	struct	spwd	*sp, sp_pwd;		/* default entry */
100*0Sstevel@tonic-gate 	struct stat buf;
101*0Sstevel@tonic-gate 	FILE	*tp_fp, *tsp_fp;
102*0Sstevel@tonic-gate 	register time_t	when, minweeks, maxweeks;
103*0Sstevel@tonic-gate 	int file_exist = 1;
104*0Sstevel@tonic-gate 	int end_of_file = 0;
105*0Sstevel@tonic-gate 	mode_t mode;
106*0Sstevel@tonic-gate 	int pwerr = 0;
107*0Sstevel@tonic-gate 	ushort i;
108*0Sstevel@tonic-gate 	gid_t pwd_gid, sp_gid;
109*0Sstevel@tonic-gate 	uid_t pwd_uid, sp_uid;
110*0Sstevel@tonic-gate 	FILE *pwf;
111*0Sstevel@tonic-gate 	int black_magic = 0;
112*0Sstevel@tonic-gate 	int count;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
117*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
118*0Sstevel@tonic-gate #endif
119*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	prognamp = argv[0];
122*0Sstevel@tonic-gate 	/* only PRIVILEGED can execute this command */
123*0Sstevel@tonic-gate 	if (getuid() != PRIVILEGED) {
124*0Sstevel@tonic-gate 		fprintf(stderr, gettext("%s: Permission denied.\n"), prognamp);
125*0Sstevel@tonic-gate 		exit(NOPERM);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	/* No argument can be passed to the command */
129*0Sstevel@tonic-gate 	if (argc > 1) {
130*0Sstevel@tonic-gate 		(void) fprintf(stderr,
131*0Sstevel@tonic-gate 		gettext("%s: Invalid command syntax.\n"),
132*0Sstevel@tonic-gate 			prognamp);
133*0Sstevel@tonic-gate 		fprintf(stderr, gettext("Usage: pwconv\n"));
134*0Sstevel@tonic-gate 		exit(BADSYN);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	/* lock file so that only one process can read or write at a time */
138*0Sstevel@tonic-gate 	if (lckpwdf() < 0) {
139*0Sstevel@tonic-gate 		(void) fprintf(stderr,
140*0Sstevel@tonic-gate 		gettext("%s: Password file(s) busy.  Try again later.\n"),
141*0Sstevel@tonic-gate 			prognamp);
142*0Sstevel@tonic-gate 		exit(FBUSY);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/* All signals will be ignored during the execution of pwconv */
146*0Sstevel@tonic-gate 	for (i = 1; i < NSIG; i++)
147*0Sstevel@tonic-gate 		sigset(i, SIG_IGN);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* reset errno to avoid side effects of a failed */
150*0Sstevel@tonic-gate 	/* sigset (e.g., SIGKILL) */
151*0Sstevel@tonic-gate 	errno = 0;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	/* check the file status of the password file */
154*0Sstevel@tonic-gate 	/* get the gid of the password file */
155*0Sstevel@tonic-gate 	if (stat(PASSWD, &buf) < 0) {
156*0Sstevel@tonic-gate 		(void) f_miss();
157*0Sstevel@tonic-gate 		exit(FATAL);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 	pwd_gid = buf.st_gid;
160*0Sstevel@tonic-gate 	pwd_uid = buf.st_uid;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	/* mode for the password file should be read-only or less */
163*0Sstevel@tonic-gate 	umask(S_IAMB & ~(buf.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)));
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	/* open temporary password file */
166*0Sstevel@tonic-gate 	if ((tp_fp = fopen(PASSTEMP, "w")) == NULL) {
167*0Sstevel@tonic-gate 		(void) f_err();
168*0Sstevel@tonic-gate 		exit(FMERR);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (chown(PASSTEMP, pwd_uid, pwd_gid) < 0) {
172*0Sstevel@tonic-gate 		DELPTMP();
173*0Sstevel@tonic-gate 		(void) f_err();
174*0Sstevel@tonic-gate 		exit(FMERR);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 	/* default mode mask of the shadow file */
177*0Sstevel@tonic-gate 	mode = S_IAMB & ~(S_IRUSR);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	/* check the existence of  shadow file */
180*0Sstevel@tonic-gate 	/* if the shadow file exists, get mode mask and group id of the file */
181*0Sstevel@tonic-gate 	/* if file does not exist, the default group name will be the group  */
182*0Sstevel@tonic-gate 	/* name of the password file.  */
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (access(SHADOW, F_OK) == 0) {
185*0Sstevel@tonic-gate 		if (stat(SHADOW, &buf) == 0) {
186*0Sstevel@tonic-gate 			mode  = S_IAMB & ~(buf.st_mode & S_IRUSR);
187*0Sstevel@tonic-gate 			sp_gid = buf.st_gid;
188*0Sstevel@tonic-gate 			sp_uid = buf.st_uid;
189*0Sstevel@tonic-gate 		} else {
190*0Sstevel@tonic-gate 			DELPTMP();
191*0Sstevel@tonic-gate 			(void) f_err();
192*0Sstevel@tonic-gate 			exit(FMERR);
193*0Sstevel@tonic-gate 		}
194*0Sstevel@tonic-gate 	} else {
195*0Sstevel@tonic-gate 		sp_gid = pwd_gid;
196*0Sstevel@tonic-gate 		sp_uid = pwd_uid;
197*0Sstevel@tonic-gate 		file_exist = 0;
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 	/*
200*0Sstevel@tonic-gate 	 * get the mode of shadow password file  -- mode of the file should
201*0Sstevel@tonic-gate 	 * be read-only for user or less.
202*0Sstevel@tonic-gate 	 */
203*0Sstevel@tonic-gate 	umask(mode);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	/* open temporary shadow file */
206*0Sstevel@tonic-gate 	if ((tsp_fp = fopen(SHADTEMP, "w")) == NULL) {
207*0Sstevel@tonic-gate 		DELPTMP();
208*0Sstevel@tonic-gate 		(void) f_err();
209*0Sstevel@tonic-gate 		exit(FMERR);
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	/* change the group of the temporary shadow password file */
213*0Sstevel@tonic-gate 	if (chown(SHADTEMP, sp_uid, sp_gid) < 0) {
214*0Sstevel@tonic-gate 		(void) no_convert();
215*0Sstevel@tonic-gate 		exit(FMERR);
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/* Reads the password file.  				*/
219*0Sstevel@tonic-gate 	/* If the shadow password file not exists, or		*/
220*0Sstevel@tonic-gate 	/* if an entry doesn't have a corresponding entry in    */
221*0Sstevel@tonic-gate 	/* the shadow file, entries/entry will be created.	*/
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
224*0Sstevel@tonic-gate 		no_recover();
225*0Sstevel@tonic-gate 		exit(FATAL);
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	count = 0;
229*0Sstevel@tonic-gate 	while (!end_of_file) {
230*0Sstevel@tonic-gate 		count++;
231*0Sstevel@tonic-gate 		if ((pwdp = fgetpwent(pwf)) != NULL) {
232*0Sstevel@tonic-gate 			if (!file_exist ||
233*0Sstevel@tonic-gate 			    (sp = local_getspnam(pwdp->pw_name)) == NULL) {
234*0Sstevel@tonic-gate 				if (errno == EINVAL) {
235*0Sstevel@tonic-gate 				/* Bad entry in shadow exit */
236*0Sstevel@tonic-gate 					DELSHWTMP();
237*0Sstevel@tonic-gate 					DELPTMP();
238*0Sstevel@tonic-gate 					(void) f_bdshw();
239*0Sstevel@tonic-gate 					exit(BADSHW);
240*0Sstevel@tonic-gate 				}
241*0Sstevel@tonic-gate 				sp = &sp_pwd;
242*0Sstevel@tonic-gate 				sp->sp_namp = pwdp->pw_name;
243*0Sstevel@tonic-gate 				if (!pwdp->pw_passwd ||
244*0Sstevel@tonic-gate 				    (pwdp->pw_passwd &&
245*0Sstevel@tonic-gate 					*pwdp->pw_passwd == NULL)) {
246*0Sstevel@tonic-gate 					(void) fprintf(stderr,
247*0Sstevel@tonic-gate 			gettext("%s: WARNING user %s has no password\n"),
248*0Sstevel@tonic-gate 					prognamp, sp->sp_namp);
249*0Sstevel@tonic-gate 				}
250*0Sstevel@tonic-gate 				/*
251*0Sstevel@tonic-gate 				 * copy the password field in the password
252*0Sstevel@tonic-gate 				 * file to the shadow file.
253*0Sstevel@tonic-gate 				 * replace the password field with an 'x'.
254*0Sstevel@tonic-gate 				 */
255*0Sstevel@tonic-gate 				sp->sp_pwdp = pwdp->pw_passwd;
256*0Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
257*0Sstevel@tonic-gate 				/*
258*0Sstevel@tonic-gate 				 * if aging, split the aging info
259*0Sstevel@tonic-gate 				 * into age, max and min
260*0Sstevel@tonic-gate 				 * convert aging info from weeks to days
261*0Sstevel@tonic-gate 				 */
262*0Sstevel@tonic-gate 				if (pwdp->pw_age && *pwdp->pw_age != NULL) {
263*0Sstevel@tonic-gate 					when = (long) a64l(pwdp->pw_age);
264*0Sstevel@tonic-gate 					maxweeks = when & 077;
265*0Sstevel@tonic-gate 					minweeks = (when >> 6) & 077;
266*0Sstevel@tonic-gate 					when >>= 12;
267*0Sstevel@tonic-gate 					sp->sp_lstchg = when * 7;
268*0Sstevel@tonic-gate 					sp->sp_min = minweeks * 7;
269*0Sstevel@tonic-gate 					sp->sp_max = maxweeks * 7;
270*0Sstevel@tonic-gate 					sp->sp_warn = -1;
271*0Sstevel@tonic-gate 					sp->sp_inact = -1;
272*0Sstevel@tonic-gate 					sp->sp_expire = -1;
273*0Sstevel@tonic-gate 					sp->sp_flag = 0;
274*0Sstevel@tonic-gate 					pwdp->pw_age = "";  /* do we care? */
275*0Sstevel@tonic-gate 				} else {
276*0Sstevel@tonic-gate 				/*
277*0Sstevel@tonic-gate 				 * if !aging, last_changed will be the day the
278*0Sstevel@tonic-gate 				 * conversion is done, min and max fields will
279*0Sstevel@tonic-gate 				 * be null - use timezone to get local time
280*0Sstevel@tonic-gate 				 */
281*0Sstevel@tonic-gate 					sp->sp_lstchg = DAY_NOW;
282*0Sstevel@tonic-gate 					sp->sp_min =  -1;
283*0Sstevel@tonic-gate 					sp->sp_max =  -1;
284*0Sstevel@tonic-gate 					sp->sp_warn = -1;
285*0Sstevel@tonic-gate 					sp->sp_inact = -1;
286*0Sstevel@tonic-gate 					sp->sp_expire = -1;
287*0Sstevel@tonic-gate 					sp->sp_flag = 0;
288*0Sstevel@tonic-gate 				}
289*0Sstevel@tonic-gate 	    } else {
290*0Sstevel@tonic-gate 			/*
291*0Sstevel@tonic-gate 			 * if the passwd field has a string other than 'x',
292*0Sstevel@tonic-gate 			 * the entry will be written into the shadow file
293*0Sstevel@tonic-gate 			 * and the character 'x' is re-written as the passwd
294*0Sstevel@tonic-gate 			 * if !aging, last_changed as above
295*0Sstevel@tonic-gate 			 */
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 			/*
298*0Sstevel@tonic-gate 			 * with NIS, only warn about password missing if entry
299*0Sstevel@tonic-gate 			 * is not a NIS-lookup entry ("+" or "-")
300*0Sstevel@tonic-gate 			 * black_magic from getpwnam_r.c
301*0Sstevel@tonic-gate 			 */
302*0Sstevel@tonic-gate 			black_magic = (*pwdp->pw_name == '+' ||
303*0Sstevel@tonic-gate 					*pwdp->pw_name == '-');
304*0Sstevel@tonic-gate 			/*
305*0Sstevel@tonic-gate 			 * moan about absence of non "+/-" passwd
306*0Sstevel@tonic-gate 			 * we could do more, but what?
307*0Sstevel@tonic-gate 			 */
308*0Sstevel@tonic-gate 			if ((!pwdp->pw_passwd ||
309*0Sstevel@tonic-gate 			    (pwdp->pw_passwd && *pwdp->pw_passwd == NULL)) &&
310*0Sstevel@tonic-gate 			    !black_magic) {
311*0Sstevel@tonic-gate 				(void) fprintf(stderr,
312*0Sstevel@tonic-gate 			gettext("%s: WARNING user %s has no password\n"),
313*0Sstevel@tonic-gate 					prognamp, sp->sp_namp);
314*0Sstevel@tonic-gate 			}
315*0Sstevel@tonic-gate 			if (pwdp->pw_passwd && *pwdp->pw_passwd) {
316*0Sstevel@tonic-gate 				if (strcmp(pwdp->pw_passwd, pwdflr)) {
317*0Sstevel@tonic-gate 					sp->sp_pwdp = pwdp->pw_passwd;
318*0Sstevel@tonic-gate 					pwdp->pw_passwd = pwdflr;
319*0Sstevel@tonic-gate 					if (!pwdp->pw_age ||
320*0Sstevel@tonic-gate 					    (pwdp->pw_age &&
321*0Sstevel@tonic-gate 						*pwdp->pw_age == NULL)) {
322*0Sstevel@tonic-gate 						sp->sp_lstchg = DAY_NOW;
323*0Sstevel@tonic-gate 						sp->sp_min =  -1;
324*0Sstevel@tonic-gate 						sp->sp_max =  -1;
325*0Sstevel@tonic-gate 						sp->sp_warn = -1;
326*0Sstevel@tonic-gate 						sp->sp_inact = -1;
327*0Sstevel@tonic-gate 						sp->sp_expire = -1;
328*0Sstevel@tonic-gate 						sp->sp_flag = 0;
329*0Sstevel@tonic-gate 					}
330*0Sstevel@tonic-gate 				}
331*0Sstevel@tonic-gate 			} else {
332*0Sstevel@tonic-gate 				/*
333*0Sstevel@tonic-gate 				 * black_magic needs a non-null passwd
334*0Sstevel@tonic-gate 				 * and pwdflr seem appropriate here
335*0Sstevel@tonic-gate 				 * clear garbage if any
336*0Sstevel@tonic-gate 				 */
337*0Sstevel@tonic-gate 				sp->sp_pwdp = "";
338*0Sstevel@tonic-gate 				pwdp->pw_passwd = pwdflr;
339*0Sstevel@tonic-gate 				sp->sp_lstchg = sp->sp_min = sp->sp_max = -1;
340*0Sstevel@tonic-gate 				sp->sp_warn = sp->sp_inact = sp->sp_expire = -1;
341*0Sstevel@tonic-gate 				sp->sp_flag = 0;
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 			/*
344*0Sstevel@tonic-gate 			 * if aging, split the aging info
345*0Sstevel@tonic-gate 			 * into age, max and min
346*0Sstevel@tonic-gate 			 * convert aging info from weeks to days
347*0Sstevel@tonic-gate 			 */
348*0Sstevel@tonic-gate 			if (pwdp->pw_age && *pwdp->pw_age != NULL) {
349*0Sstevel@tonic-gate 				when = (long) a64l(pwdp->pw_age);
350*0Sstevel@tonic-gate 				maxweeks = when & 077;
351*0Sstevel@tonic-gate 				minweeks = (when >> 6) & 077;
352*0Sstevel@tonic-gate 				when >>= 12;
353*0Sstevel@tonic-gate 				sp->sp_lstchg = when * 7;
354*0Sstevel@tonic-gate 				sp->sp_min = minweeks * 7;
355*0Sstevel@tonic-gate 				sp->sp_max = maxweeks * 7;
356*0Sstevel@tonic-gate 				sp->sp_warn = -1;
357*0Sstevel@tonic-gate 				sp->sp_inact = -1;
358*0Sstevel@tonic-gate 				sp->sp_expire = -1;
359*0Sstevel@tonic-gate 				sp->sp_flag = 0;
360*0Sstevel@tonic-gate 				pwdp->pw_age = ""; /* do we care? */
361*0Sstevel@tonic-gate 			}
362*0Sstevel@tonic-gate 		}
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 		/* write an entry to temporary password file */
365*0Sstevel@tonic-gate 		if ((putpwent(pwdp, tp_fp)) != 0) {
366*0Sstevel@tonic-gate 			(void) no_convert();
367*0Sstevel@tonic-gate 			exit(FMERR);
368*0Sstevel@tonic-gate 		}
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 		/* write an entry to temporary shadow password file */
371*0Sstevel@tonic-gate 		if (putspent(sp, tsp_fp) != 0) {
372*0Sstevel@tonic-gate 			(void) no_convert();
373*0Sstevel@tonic-gate 			exit(FMERR);
374*0Sstevel@tonic-gate 		}
375*0Sstevel@tonic-gate 	    } else {
376*0Sstevel@tonic-gate 		if (feof(pwf)) {
377*0Sstevel@tonic-gate 			end_of_file = 1;
378*0Sstevel@tonic-gate 		} else {
379*0Sstevel@tonic-gate 			errno = 0;
380*0Sstevel@tonic-gate 			pwerr = 1;
381*0Sstevel@tonic-gate 			(void) fprintf(stderr,
382*0Sstevel@tonic-gate 			gettext("%s: ERROR: bad entry or blank line at line %d in /etc/passwd\n"),
383*0Sstevel@tonic-gate 					prognamp, count);
384*0Sstevel@tonic-gate 		}
385*0Sstevel@tonic-gate 	    }
386*0Sstevel@tonic-gate 	} /* end of while */
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	(void) fclose(pwf);
389*0Sstevel@tonic-gate 	(void) fclose(tsp_fp);
390*0Sstevel@tonic-gate 	(void) fclose(tp_fp);
391*0Sstevel@tonic-gate 	if (pwerr) {
392*0Sstevel@tonic-gate 		(void) no_convert();
393*0Sstevel@tonic-gate 		exit(FMERR);
394*0Sstevel@tonic-gate 	}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	/* delete old password file if it exists */
397*0Sstevel@tonic-gate 	if (unlink(OPASSWD) && (access(OPASSWD, F_OK) == 0)) {
398*0Sstevel@tonic-gate 		(void) no_convert();
399*0Sstevel@tonic-gate 		exit(FMERR);
400*0Sstevel@tonic-gate 	}
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	/* rename the password file to old password file  */
403*0Sstevel@tonic-gate 	if (rename(PASSWD, OPASSWD) == -1) {
404*0Sstevel@tonic-gate 		(void) no_convert();
405*0Sstevel@tonic-gate 		exit(FMERR);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	/* rename temporary password file to password file */
409*0Sstevel@tonic-gate 	if (rename(PASSTEMP, PASSWD) == -1) {
410*0Sstevel@tonic-gate 		/* link old password file to password file */
411*0Sstevel@tonic-gate 		if (link(OPASSWD, PASSWD) < 0) {
412*0Sstevel@tonic-gate 			(void) no_recover();
413*0Sstevel@tonic-gate 			exit(FATAL);
414*0Sstevel@tonic-gate 		}
415*0Sstevel@tonic-gate 		(void) no_convert();
416*0Sstevel@tonic-gate 		exit(FMERR);
417*0Sstevel@tonic-gate 	}
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	/* delete old shadow password file if it exists */
420*0Sstevel@tonic-gate 	if (unlink(OSHADOW) && (access(OSHADOW, R_OK) == 0)) {
421*0Sstevel@tonic-gate 		/* link old password file to password file */
422*0Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
423*0Sstevel@tonic-gate 			(void) no_recover();
424*0Sstevel@tonic-gate 			exit(FATAL);
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 		(void) no_convert();
427*0Sstevel@tonic-gate 		exit(FMERR);
428*0Sstevel@tonic-gate 	}
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	/* link shadow password file to old shadow password file */
431*0Sstevel@tonic-gate 	if (file_exist && rename(SHADOW, OSHADOW)) {
432*0Sstevel@tonic-gate 		/* link old password file to password file */
433*0Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
434*0Sstevel@tonic-gate 			(void) no_recover();
435*0Sstevel@tonic-gate 			exit(FATAL);
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 		(void) no_convert();
438*0Sstevel@tonic-gate 		exit(FMERR);
439*0Sstevel@tonic-gate 	}
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	/* link temporary shadow password file to shadow password file */
443*0Sstevel@tonic-gate 	if (rename(SHADTEMP, SHADOW) == -1) {
444*0Sstevel@tonic-gate 		/* link old shadow password file to shadow password file */
445*0Sstevel@tonic-gate 		if (file_exist && (link(OSHADOW, SHADOW))) {
446*0Sstevel@tonic-gate 			(void) no_recover();
447*0Sstevel@tonic-gate 			exit(FATAL);
448*0Sstevel@tonic-gate 		}
449*0Sstevel@tonic-gate 		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
450*0Sstevel@tonic-gate 			(void) no_recover();
451*0Sstevel@tonic-gate 			exit(FATAL);
452*0Sstevel@tonic-gate 		}
453*0Sstevel@tonic-gate 		(void) no_convert();
454*0Sstevel@tonic-gate 		exit(FMERR);
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	/* Change old password file to read only by owner   */
458*0Sstevel@tonic-gate 	/* If chmod fails, delete the old password file so that */
459*0Sstevel@tonic-gate 	/* the password fields can not be read by others */
460*0Sstevel@tonic-gate 	if (chmod(OPASSWD, S_IRUSR) < 0)
461*0Sstevel@tonic-gate 		(void) unlink(OPASSWD);
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	(void) ulckpwdf();
464*0Sstevel@tonic-gate 	exit(0);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate void
468*0Sstevel@tonic-gate no_recover()
469*0Sstevel@tonic-gate {
470*0Sstevel@tonic-gate 	DELPTMP();
471*0Sstevel@tonic-gate 	DELSHWTMP();
472*0Sstevel@tonic-gate 	(void) f_miss();
473*0Sstevel@tonic-gate }
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate void
476*0Sstevel@tonic-gate no_convert()
477*0Sstevel@tonic-gate {
478*0Sstevel@tonic-gate 	DELPTMP();
479*0Sstevel@tonic-gate 	DELSHWTMP();
480*0Sstevel@tonic-gate 	(void) f_err();
481*0Sstevel@tonic-gate }
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate void
484*0Sstevel@tonic-gate f_err()
485*0Sstevel@tonic-gate {
486*0Sstevel@tonic-gate 	fprintf(stderr,
487*0Sstevel@tonic-gate 		gettext("%s: Unexpected failure. Conversion not done.\n"),
488*0Sstevel@tonic-gate 			prognamp);
489*0Sstevel@tonic-gate 	(void) ulckpwdf();
490*0Sstevel@tonic-gate }
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate void
493*0Sstevel@tonic-gate f_miss()
494*0Sstevel@tonic-gate {
495*0Sstevel@tonic-gate 	fprintf(stderr,
496*0Sstevel@tonic-gate 		gettext("%s: Unexpected failure. Password file(s) missing.\n"),
497*0Sstevel@tonic-gate 			prognamp);
498*0Sstevel@tonic-gate 	(void) ulckpwdf();
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate void
502*0Sstevel@tonic-gate f_bdshw()
503*0Sstevel@tonic-gate {
504*0Sstevel@tonic-gate 	fprintf(stderr,
505*0Sstevel@tonic-gate 		gettext("%s: Bad entry in /etc/shadow. Conversion not done.\n"),
506*0Sstevel@tonic-gate 			prognamp);
507*0Sstevel@tonic-gate 	(void) ulckpwdf();
508*0Sstevel@tonic-gate }
509