summaryrefslogtreecommitdiff
path: root/usr/src/cmd/pwconv/pwconv.c
blob: 0c7f1387a82163c1ddb84b9ef816b8610df08563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*  pwconv.c  */
/*  Conversion aid to copy appropriate fields from the	*/
/*  password file to the shadow file.			*/

#include <pwd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <shadow.h>
#include <grp.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

#define	PRIVILEGED	0 			/* priviledged id */

/* exit  code */
#define	SUCCESS	0	/* succeeded */
#define	NOPERM	1	/* No permission */
#define	BADSYN	2	/* Incorrect syntax */
#define	FMERR	3	/* File manipulation error */
#define	FATAL	4	/* Old file can not be recover */
#define	FBUSY	5	/* Lock file busy */
#define	BADSHW	6	/* Bad entry in shadow file  */

#define	DELPTMP()	(void) unlink(PASSTEMP)
#define	DELSHWTMP()	(void) unlink(SHADTEMP)

char pwdflr[]	= "x";				/* password filler */
char *prognamp;
void f_err(void), f_miss(void), f_bdshw(void);

/*
 * getspnan routine that ONLY looks at the local shadow file
 */
struct spwd *
local_getspnam(char *name)
{
	FILE *shadf;
	struct spwd *sp;


	if ((shadf = fopen("/etc/shadow", "r")) == NULL)
		return (NULL);

	while ((sp = fgetspent(shadf)) != NULL) {
		if (strcmp(sp->sp_namp, name) == 0)
			break;
	}

	(void) fclose(shadf);

	return (sp);
}

int
main(int argc, char **argv)
{
	extern	int	errno;
	void  no_recover(void), no_convert(void);
	struct  passwd  *pwdp;
	struct	spwd	*sp, sp_pwd;		/* default entry */
	struct stat buf;
	FILE	*tp_fp, *tsp_fp;
	time_t	when, minweeks, maxweeks;
	int file_exist = 1;
	int end_of_file = 0;
	mode_t mode;
	mode_t pwd_mode;
	int pwerr = 0;
	ushort_t i;
	gid_t pwd_gid, sp_gid;
	uid_t pwd_uid, sp_uid;
	FILE *pwf;
	int black_magic = 0;
	int count;

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	prognamp = argv[0];
	/* only PRIVILEGED can execute this command */
	if (getuid() != PRIVILEGED) {
		(void) fprintf(stderr, gettext("%s: Permission denied.\n"),
		    prognamp);
		exit(NOPERM);
	}

	/* No argument can be passed to the command */
	if (argc > 1) {
		(void) fprintf(stderr,
		gettext("%s: Invalid command syntax.\n"),
			prognamp);
		(void) fprintf(stderr, gettext("Usage: pwconv\n"));
		exit(BADSYN);
	}

	/* lock file so that only one process can read or write at a time */
	if (lckpwdf() < 0) {
		(void) fprintf(stderr,
		gettext("%s: Password file(s) busy.  Try again later.\n"),
			prognamp);
		exit(FBUSY);
	}

	/* All signals will be ignored during the execution of pwconv */
	for (i = 1; i < NSIG; i++)
		(void) sigset(i, SIG_IGN);

	/* reset errno to avoid side effects of a failed */
	/* sigset (e.g., SIGKILL) */
	errno = 0;

	/* check the file status of the password file */
	/* get the gid of the password file */
	if (stat(PASSWD, &buf) < 0) {
		(void) f_miss();
		exit(FATAL);
	}
	pwd_gid = buf.st_gid;
	pwd_uid = buf.st_uid;
	pwd_mode = buf.st_mode;

	/* mode for the password file should be read-only or less */
	(void) umask(S_IAMB & ~(buf.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)));

	/* open temporary password file */
	if ((tp_fp = fopen(PASSTEMP, "w")) == NULL) {
		(void) f_err();
		exit(FMERR);
	}

	if (chown(PASSTEMP, pwd_uid, pwd_gid) < 0) {
		DELPTMP();
		(void) f_err();
		exit(FMERR);
	}
	/* default mode mask of the shadow file */
	mode = S_IAMB & ~(S_IRUSR);

	/* check the existence of  shadow file */
	/* if the shadow file exists, get mode mask and group id of the file */
	/* if file does not exist, the default group name will be the group  */
	/* name of the password file.  */

	if (access(SHADOW, F_OK) == 0) {
		if (stat(SHADOW, &buf) == 0) {
			mode  = S_IAMB & ~(buf.st_mode & S_IRUSR);
			sp_gid = buf.st_gid;
			sp_uid = buf.st_uid;
		} else {
			DELPTMP();
			(void) f_err();
			exit(FMERR);
		}
	} else {
		sp_gid = pwd_gid;
		sp_uid = pwd_uid;
		file_exist = 0;
	}
	/*
	 * get the mode of shadow password file  -- mode of the file should
	 * be read-only for user or less.
	 */
	(void) umask(mode);

	/* open temporary shadow file */
	if ((tsp_fp = fopen(SHADTEMP, "w")) == NULL) {
		DELPTMP();
		(void) f_err();
		exit(FMERR);
	}

	/* change the group of the temporary shadow password file */
	if (chown(SHADTEMP, sp_uid, sp_gid) < 0) {
		(void) no_convert();
		exit(FMERR);
	}

	/* Reads the password file.  				*/
	/* If the shadow password file not exists, or		*/
	/* if an entry doesn't have a corresponding entry in    */
	/* the shadow file, entries/entry will be created.	*/

	if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
		no_recover();
		exit(FATAL);
	}

	count = 0;
	while (!end_of_file) {
		count++;
		if ((pwdp = fgetpwent(pwf)) != NULL) {
			if (!file_exist ||
			    (sp = local_getspnam(pwdp->pw_name)) == NULL) {
				if (errno == EINVAL) {
				/* Bad entry in shadow exit */
					DELSHWTMP();
					DELPTMP();
					(void) f_bdshw();
					exit(BADSHW);
				}
				sp = &sp_pwd;
				sp->sp_namp = pwdp->pw_name;
				if (!pwdp->pw_passwd ||
				    (pwdp->pw_passwd &&
					*pwdp->pw_passwd == NULL)) {
					(void) fprintf(stderr,
			gettext("%s: WARNING user %s has no password\n"),
					prognamp, sp->sp_namp);
				}
				/*
				 * copy the password field in the password
				 * file to the shadow file.
				 * replace the password field with an 'x'.
				 */
				sp->sp_pwdp = pwdp->pw_passwd;
				pwdp->pw_passwd = pwdflr;
				/*
				 * if aging, split the aging info
				 * into age, max and min
				 * convert aging info from weeks to days
				 */
				if (pwdp->pw_age && *pwdp->pw_age != NULL) {
					when = (long)a64l(pwdp->pw_age);
					maxweeks = when & 077;
					minweeks = (when >> 6) & 077;
					when >>= 12;
					sp->sp_lstchg = when * 7;
					sp->sp_min = minweeks * 7;
					sp->sp_max = maxweeks * 7;
					sp->sp_warn = -1;
					sp->sp_inact = -1;
					sp->sp_expire = -1;
					sp->sp_flag = 0;
					pwdp->pw_age = "";  /* do we care? */
				} else {
				/*
				 * if !aging, last_changed will be the day the
				 * conversion is done, min and max fields will
				 * be null - use timezone to get local time
				 */
					sp->sp_lstchg = DAY_NOW;
					sp->sp_min =  -1;
					sp->sp_max =  -1;
					sp->sp_warn = -1;
					sp->sp_inact = -1;
					sp->sp_expire = -1;
					sp->sp_flag = 0;
				}
	    } else {
			/*
			 * if the passwd field has a string other than 'x',
			 * the entry will be written into the shadow file
			 * and the character 'x' is re-written as the passwd
			 * if !aging, last_changed as above
			 */

			/*
			 * with NIS, only warn about password missing if entry
			 * is not a NIS-lookup entry ("+" or "-")
			 * black_magic from getpwnam_r.c
			 */
			black_magic = (*pwdp->pw_name == '+' ||
					*pwdp->pw_name == '-');
			/*
			 * moan about absence of non "+/-" passwd
			 * we could do more, but what?
			 */
			if ((!pwdp->pw_passwd ||
			    (pwdp->pw_passwd && *pwdp->pw_passwd == NULL)) &&
			    !black_magic) {
				(void) fprintf(stderr,
			gettext("%s: WARNING user %s has no password\n"),
					prognamp, sp->sp_namp);
			}
			if (pwdp->pw_passwd && *pwdp->pw_passwd) {
				if (strcmp(pwdp->pw_passwd, pwdflr)) {
					sp->sp_pwdp = pwdp->pw_passwd;
					pwdp->pw_passwd = pwdflr;
					if (!pwdp->pw_age ||
					    (pwdp->pw_age &&
						*pwdp->pw_age == NULL)) {
						sp->sp_lstchg = DAY_NOW;
						sp->sp_min =  -1;
						sp->sp_max =  -1;
						sp->sp_warn = -1;
						sp->sp_inact = -1;
						sp->sp_expire = -1;
						sp->sp_flag = 0;
					}
				}
			} else {
				/*
				 * black_magic needs a non-null passwd
				 * and pwdflr seem appropriate here
				 * clear garbage if any
				 */
				sp->sp_pwdp = "";
				pwdp->pw_passwd = pwdflr;
				sp->sp_lstchg = sp->sp_min = sp->sp_max = -1;
				sp->sp_warn = sp->sp_inact = sp->sp_expire = -1;
				sp->sp_flag = 0;
			}
			/*
			 * if aging, split the aging info
			 * into age, max and min
			 * convert aging info from weeks to days
			 */
			if (pwdp->pw_age && *pwdp->pw_age != NULL) {
				when = (long)a64l(pwdp->pw_age);
				maxweeks = when & 077;
				minweeks = (when >> 6) & 077;
				when >>= 12;
				sp->sp_lstchg = when * 7;
				sp->sp_min = minweeks * 7;
				sp->sp_max = maxweeks * 7;
				sp->sp_warn = -1;
				sp->sp_inact = -1;
				sp->sp_expire = -1;
				sp->sp_flag = 0;
				pwdp->pw_age = ""; /* do we care? */
			}
		}

		/* write an entry to temporary password file */
		if ((putpwent(pwdp, tp_fp)) != 0) {
			(void) no_convert();
			exit(FMERR);
		}

		/* write an entry to temporary shadow password file */
		if (putspent(sp, tsp_fp) != 0) {
			(void) no_convert();
			exit(FMERR);
		}
	    } else {
		if (feof(pwf)) {
			end_of_file = 1;
		} else {
			errno = 0;
			pwerr = 1;
			(void) fprintf(stderr,
			    gettext("%s: ERROR: bad entry or blank "
			    "line at line %d in /etc/passwd\n"),
			    prognamp, count);
		}
	    }
	} /* end of while */

	(void) fclose(pwf);
	(void) fclose(tsp_fp);
	(void) fclose(tp_fp);
	if (pwerr) {
		(void) no_convert();
		exit(FMERR);
	}

	/* delete old password file if it exists */
	if (unlink(OPASSWD) && (access(OPASSWD, F_OK) == 0)) {
		(void) no_convert();
		exit(FMERR);
	}

	/* rename the password file to old password file  */
	if (rename(PASSWD, OPASSWD) == -1) {
		(void) no_convert();
		exit(FMERR);
	}

	/* rename temporary password file to password file */
	if (rename(PASSTEMP, PASSWD) == -1) {
		/* link old password file to password file */
		if (link(OPASSWD, PASSWD) < 0) {
			(void) no_recover();
			exit(FATAL);
		}
		(void) no_convert();
		exit(FMERR);
	}

	/* delete old shadow password file if it exists */
	if (unlink(OSHADOW) && (access(OSHADOW, R_OK) == 0)) {
		/* link old password file to password file */
		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
			(void) no_recover();
			exit(FATAL);
		}
		(void) no_convert();
		exit(FMERR);
	}

	/* link shadow password file to old shadow password file */
	if (file_exist && rename(SHADOW, OSHADOW)) {
		/* link old password file to password file */
		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
			(void) no_recover();
			exit(FATAL);
		}
		(void) no_convert();
		exit(FMERR);
	}


	/* link temporary shadow password file to shadow password file */
	if (rename(SHADTEMP, SHADOW) == -1) {
		/* link old shadow password file to shadow password file */
		if (file_exist && (link(OSHADOW, SHADOW))) {
			(void) no_recover();
			exit(FATAL);
		}
		if (unlink(PASSWD) || link(OPASSWD, PASSWD)) {
			(void) no_recover();
			exit(FATAL);
		}
		(void) no_convert();
		exit(FMERR);
	}

	/* Make new mode same as old */
	(void) chmod(PASSWD, pwd_mode);

	/* Change old password file to read only by owner   */
	/* If chmod fails, delete the old password file so that */
	/* the password fields can not be read by others */
	if (chmod(OPASSWD, S_IRUSR) < 0)
		(void) unlink(OPASSWD);

	(void) ulckpwdf();
	return (0);
}

void
no_recover(void)
{
	DELPTMP();
	DELSHWTMP();
	(void) f_miss();
}

void
no_convert(void)
{
	DELPTMP();
	DELSHWTMP();
	(void) f_err();
}

void
f_err(void)
{
	(void) fprintf(stderr,
		gettext("%s: Unexpected failure. Conversion not done.\n"),
			prognamp);
	(void) ulckpwdf();
}

void
f_miss(void)
{
	(void) fprintf(stderr,
		gettext("%s: Unexpected failure. Password file(s) missing.\n"),
			prognamp);
	(void) ulckpwdf();
}

void
f_bdshw(void)
{
	(void) fprintf(stderr,
		gettext("%s: Bad entry in /etc/shadow. Conversion not done.\n"),
			prognamp);
	(void) ulckpwdf();
}