pwd_mkdb.c revision 28385
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1991, 1993, 1994\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)pwd_mkdb.c	8.5 (Berkeley) 4/20/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/stat.h>
46
47#include <db.h>
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <limits.h>
52#include <pwd.h>
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58
59#include "pw_scan.h"
60
61#define	INSECURE	1
62#define	SECURE		2
63#define	PERM_INSECURE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
64#define	PERM_SECURE	(S_IRUSR|S_IWUSR)
65
66HASHINFO openinfo = {
67	4096,		/* bsize */
68	32,		/* ffactor */
69	256,		/* nelem */
70	2048 * 1024,	/* cachesize */
71	NULL,		/* hash() */
72	0		/* lorder */
73};
74
75static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
76static struct passwd pwd;			/* password structure */
77static char *pname;				/* password file name */
78static char prefix[MAXPATHLEN];
79
80static int Cflag;	/* flag for comments */
81static char line[LINE_MAX];
82
83void	cleanup __P((void));
84void	error __P((char *));
85void	cp __P((char *, char *, mode_t mode));
86void	mv __P((char *, char *));
87int	scan __P((FILE *, struct passwd *));
88void	usage __P((void));
89
90int
91main(argc, argv)
92	int argc;
93	char *argv[];
94{
95	DB *dp, *sdp, *pw_db;
96	DBT data, sdata, key;
97	FILE *fp, *oldfp;
98	sigset_t set;
99	int ch, cnt, ypcnt, len, makeold, tfd, yp_enabled = 0;
100	char *p, *t;
101	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
102	char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
103	char buf2[MAXPATHLEN];
104	char sbuf2[MAXPATHLEN];
105	char *username;
106	u_int method, methoduid;
107	int cflag;
108
109	cflag = 0;
110	strcpy(prefix, _PATH_PWD);
111	makeold = 0;
112	username = NULL;
113	while ((ch = getopt(argc, argv, "cd:pu:v")) != -1)
114		switch(ch) {
115		case 'c':                       /* verify only */
116			cflag = 1;
117			break;
118		case 'd':
119			strncpy(prefix, optarg, sizeof prefix - 1);
120			break;
121		case 'p':			/* create V7 "file.orig" */
122			makeold = 1;
123			break;
124		case 'u':			/* only update this record */
125			username = optarg;
126			break;
127		case 'v':                       /* backward compatible */
128			break;
129		default:
130			usage();
131		}
132	argc -= optind;
133	argv += optind;
134
135	if (argc != 1 || (username && (*username == '+' || *username == '-')))
136		usage();
137
138	/*
139	 * This could be changed to allow the user to interrupt.
140	 * Probably not worth the effort.
141	 */
142	sigemptyset(&set);
143	sigaddset(&set, SIGTSTP);
144	sigaddset(&set, SIGHUP);
145	sigaddset(&set, SIGINT);
146	sigaddset(&set, SIGQUIT);
147	sigaddset(&set, SIGTERM);
148	(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
149
150	/* We don't care what the user wants. */
151	(void)umask(0);
152
153	pname = *argv;
154	/* Open the original password file */
155	if (!(fp = fopen(pname, "r")))
156		error(pname);
157
158	/* check only if password database is valid */
159	if (cflag) {
160		for (cnt = 1; scan(fp, &pwd); ++cnt);
161		exit(0);
162	}
163
164	/* Open the temporary insecure password database. */
165	(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
166	(void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
167	if (username) {
168		(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
169		(void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
170
171		clean = FILE_INSECURE;
172		cp(buf2, buf, PERM_INSECURE);
173		dp = dbopen(buf,
174		    O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
175		if (dp == NULL)
176			error(buf);
177
178		clean = FILE_SECURE;
179		cp(sbuf2, sbuf, PERM_SECURE);
180		sdp = dbopen(sbuf,
181		    O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
182		if (sdp == NULL)
183			error(sbuf);
184
185		/*
186		 * Do some trouble to check if we should store this users
187		 * uid. Don't use getpwnam/getpwuid as that interferes
188		 * with NIS.
189		 */
190		pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
191		if (!pw_db)
192			error(_MP_DB);
193		buf[0] = _PW_KEYBYNAME;
194		len = strlen(username);
195
196		/* Only check that username fits in buffer */
197		memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
198		key.data = (u_char *)buf;
199		key.size = len + 1;
200		if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
201			p = (char *)data.data;
202
203			/* jump over pw_name and pw_passwd, to get to pw_uid */
204			while (*p++)
205				;
206			while (*p++)
207				;
208
209			buf[0] = _PW_KEYBYUID;
210			memmove(buf + 1, p, sizeof(int));
211			key.data = (u_char *)buf;
212			key.size = sizeof(int) + 1;
213
214			if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
215				/* First field of data.data holds pw_pwname */
216				if (!strcmp(data.data, username))
217					methoduid = 0;
218				else
219					methoduid = R_NOOVERWRITE;
220			} else {
221				methoduid = R_NOOVERWRITE;
222			}
223		} else {
224			methoduid = R_NOOVERWRITE;
225		}
226		(void)(pw_db->close)(pw_db);
227		method = 0;
228	} else {
229		dp = dbopen(buf,
230		    O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
231		if (dp == NULL)
232			error(buf);
233		clean = FILE_INSECURE;
234
235		sdp = dbopen(sbuf,
236		    O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
237		if (sdp == NULL)
238			error(sbuf);
239		clean = FILE_SECURE;
240
241		method = R_NOOVERWRITE;
242		methoduid = R_NOOVERWRITE;
243	}
244
245	/*
246	 * Open file for old password file.  Minor trickiness -- don't want to
247	 * chance the file already existing, since someone (stupidly) might
248	 * still be using this for permission checking.  So, open it first and
249	 * fdopen the resulting fd.  The resulting file should be readable by
250	 * everyone.
251	 */
252	if (makeold) {
253		(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
254		if ((tfd = open(buf,
255		    O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
256			error(buf);
257		if ((oldfp = fdopen(tfd, "w")) == NULL)
258			error(buf);
259		clean = FILE_ORIG;
260	}
261
262	/*
263	 * The databases actually contain three copies of the original data.
264	 * Each password file entry is converted into a rough approximation
265	 * of a ``struct passwd'', with the strings placed inline.  This
266	 * object is then stored as the data for three separate keys.  The
267	 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
268	 * character.  The second key is the pw_uid field prepended by the
269	 * _PW_KEYBYUID character.  The third key is the line number in the
270	 * original file prepended by the _PW_KEYBYNUM character.  (The special
271	 * characters are prepended to ensure that the keys do not collide.)
272	 */
273	ypcnt = 1;
274	data.data = (u_char *)buf;
275	sdata.data = (u_char *)sbuf;
276	key.data = (u_char *)tbuf;
277	for (cnt = 1; scan(fp, &pwd); ++cnt) {
278		if (!Cflag &&
279		    (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
280			yp_enabled = 1;
281#define	COMPACT(e)	t = e; while (*p++ = *t++);
282		if (!Cflag &&
283		    (!username || (strcmp(username, pwd.pw_name) == 0))) {
284			/* Create insecure data. */
285			p = buf;
286			COMPACT(pwd.pw_name);
287			COMPACT("*");
288			memmove(p, &pwd.pw_uid, sizeof(int));
289			p += sizeof(int);
290			memmove(p, &pwd.pw_gid, sizeof(int));
291			p += sizeof(int);
292			memmove(p, &pwd.pw_change, sizeof(time_t));
293			p += sizeof(time_t);
294			COMPACT(pwd.pw_class);
295			COMPACT(pwd.pw_gecos);
296			COMPACT(pwd.pw_dir);
297			COMPACT(pwd.pw_shell);
298			memmove(p, &pwd.pw_expire, sizeof(time_t));
299			p += sizeof(time_t);
300			memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
301			p += sizeof pwd.pw_fields;
302			data.size = p - buf;
303
304			/* Create secure data. */
305			p = sbuf;
306			COMPACT(pwd.pw_name);
307			COMPACT(pwd.pw_passwd);
308			memmove(p, &pwd.pw_uid, sizeof(int));
309			p += sizeof(int);
310			memmove(p, &pwd.pw_gid, sizeof(int));
311			p += sizeof(int);
312			memmove(p, &pwd.pw_change, sizeof(time_t));
313			p += sizeof(time_t);
314			COMPACT(pwd.pw_class);
315			COMPACT(pwd.pw_gecos);
316			COMPACT(pwd.pw_dir);
317			COMPACT(pwd.pw_shell);
318			memmove(p, &pwd.pw_expire, sizeof(time_t));
319			p += sizeof(time_t);
320			memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
321			p += sizeof pwd.pw_fields;
322			sdata.size = p - sbuf;
323
324			/* Store insecure by name. */
325			tbuf[0] = _PW_KEYBYNAME;
326			len = strlen(pwd.pw_name);
327			memmove(tbuf + 1, pwd.pw_name, len);
328			key.size = len + 1;
329			if ((dp->put)(dp, &key, &data, method) == -1)
330				error("put");
331
332			/* Store insecure by number. */
333			tbuf[0] = _PW_KEYBYNUM;
334			memmove(tbuf + 1, &cnt, sizeof(cnt));
335			key.size = sizeof(cnt) + 1;
336			if ((dp->put)(dp, &key, &data, method) == -1)
337				error("put");
338
339			/* Store insecure by uid. */
340			tbuf[0] = _PW_KEYBYUID;
341			memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
342			key.size = sizeof(pwd.pw_uid) + 1;
343			if ((dp->put)(dp, &key, &data, methoduid) == -1)
344				error("put");
345
346			/* Store secure by name. */
347			tbuf[0] = _PW_KEYBYNAME;
348			len = strlen(pwd.pw_name);
349			memmove(tbuf + 1, pwd.pw_name, len);
350			key.size = len + 1;
351			if ((sdp->put)(sdp, &key, &sdata, method) == -1)
352				error("put");
353
354			/* Store secure by number. */
355			tbuf[0] = _PW_KEYBYNUM;
356			memmove(tbuf + 1, &cnt, sizeof(cnt));
357			key.size = sizeof(cnt) + 1;
358			if ((sdp->put)(sdp, &key, &sdata, method) == -1)
359				error("put");
360
361			/* Store secure by uid. */
362			tbuf[0] = _PW_KEYBYUID;
363			memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
364			key.size = sizeof(pwd.pw_uid) + 1;
365			if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
366				error("put");
367
368			/* Store insecure and secure special plus and special minus */
369			if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
370				tbuf[0] = _PW_KEYYPBYNUM;
371				memmove(tbuf + 1, &ypcnt, sizeof(cnt));
372				ypcnt++;
373				key.size = sizeof(cnt) + 1;
374				if ((dp->put)(dp, &key, &data, method) == -1)
375					error("put");
376				if ((sdp->put)(sdp, &key, &sdata, method) == -1)
377					error("put");
378			}
379		}
380		/* Create original format password file entry */
381		if (Cflag && makeold)	/* copy comments */
382			(void)fprintf(oldfp, "%s\n", line);
383		else if (makeold) {
384			char uidstr[20];
385			char gidstr[20];
386
387			snprintf(uidstr, sizeof(uidstr), "%d", pwd.pw_uid);
388			snprintf(gidstr, sizeof(gidstr), "%d", pwd.pw_gid);
389
390			(void)fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
391			    pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
392			    pwd.pw_fields & _PWF_GID ? gidstr : "",
393			    pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell);
394		}
395	}
396	/* If YP enabled, set flag. */
397	if (yp_enabled) {
398		buf[0] = yp_enabled + 2;
399		data.size = 1;
400		tbuf[0] = _PW_KEYYPENABLED;
401		key.size = 1;
402		if ((dp->put)(dp, &key, &data, method) == -1)
403			error("put");
404		if ((sdp->put)(sdp, &key, &data, method) == -1)
405			error("put");
406	}
407
408	if ((dp->close)(dp) == -1)
409		error("close");
410	if ((sdp->close)(sdp) == -1)
411		error("close");
412	if (makeold) {
413		(void)fflush(oldfp);
414		(void)fclose(oldfp);
415	}
416
417	/* Set master.passwd permissions, in case caller forgot. */
418	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
419	(void)fclose(fp);
420
421	/* Install as the real password files. */
422	(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
423	(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
424	mv(buf, buf2);
425	(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
426	(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
427	mv(buf, buf2);
428	if (makeold) {
429		(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
430		(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
431		mv(buf, buf2);
432	}
433	/*
434	 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
435	 * all use flock(2) on it to block other incarnations of themselves.
436	 * The rename means that everything is unlocked, as the original file
437	 * can no longer be accessed.
438	 */
439	(void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
440	mv(pname, buf);
441	exit(0);
442}
443
444int
445scan(fp, pw)
446	FILE *fp;
447	struct passwd *pw;
448{
449	static int lcnt;
450	char *p;
451
452	if (!fgets(line, sizeof(line), fp))
453		return (0);
454	++lcnt;
455	/*
456	 * ``... if I swallow anything evil, put your fingers down my
457	 * throat...''
458	 *	-- The Who
459	 */
460	if (!(p = strchr(line, '\n'))) {
461		warnx("line too long");
462		goto fmt;
463
464	}
465	*p = '\0';
466
467#ifdef PASSWD_IGNORE_COMMENTS
468	/*
469	 * Ignore comments: ^[ \t]*#
470	 */
471	for (p = line; *p != '\0'; p++)
472		if (*p != ' ' && *p != '\t')
473			break;
474	if (*p == '#' || *p == '\0') {
475		Cflag = 1;
476		return(1);
477	} else
478		Cflag = 0;
479#endif
480
481	if (!pw_scan(line, pw)) {
482		warnx("at line #%d", lcnt);
483fmt:		errno = EFTYPE;	/* XXX */
484		error(pname);
485	}
486
487	return (1);
488}
489
490void
491cp(from, to, mode)
492	char *from, *to;
493	mode_t mode;
494{
495	static char buf[MAXBSIZE];
496	int from_fd, rcount, to_fd, wcount;
497
498	if ((from_fd = open(from, O_RDONLY, 0)) < 0)
499		error(from);
500	if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
501		error(to);
502	while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
503		wcount = write(to_fd, buf, rcount);
504		if (rcount != wcount || wcount == -1) {
505			int sverrno = errno;
506
507			(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
508			errno = sverrno;
509			error(buf);
510		}
511	}
512	if (rcount < 0) {
513		int sverrno = errno;
514
515		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
516		errno = sverrno;
517		error(buf);
518	}
519}
520
521
522void
523mv(from, to)
524	char *from, *to;
525{
526	char buf[MAXPATHLEN];
527
528	if (rename(from, to)) {
529		int sverrno = errno;
530		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
531		errno = sverrno;
532		error(buf);
533	}
534}
535
536void
537error(name)
538	char *name;
539{
540
541	warn(name);
542	cleanup();
543	exit(1);
544}
545
546void
547cleanup()
548{
549	char buf[MAXPATHLEN];
550
551	switch(clean) {
552	case FILE_ORIG:
553		(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
554		(void)unlink(buf);
555		/* FALLTHROUGH */
556	case FILE_SECURE:
557		(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
558		(void)unlink(buf);
559		/* FALLTHROUGH */
560	case FILE_INSECURE:
561		(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
562		(void)unlink(buf);
563	}
564}
565
566void
567usage()
568{
569
570	(void)fprintf(stderr, "usage: pwd_mkdb [-c] [-p] [-d <dest dir>] [-u <local username>] file\n");
571	exit(1);
572}
573