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