Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.11
      1 /*
      2  * Copyright (c) 1991, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  * Portions Copyright(C) 1994, Jason Downs.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
     38 	The Regents of the University of California.  All rights reserved.\n");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "from: @(#)pwd_mkdb.c	8.5 (Berkeley) 4/20/94";
     44 #else
     45 __RCSID("$NetBSD: pwd_mkdb.c,v 1.11 1998/04/13 23:12:45 fair Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #include <sys/stat.h>
     51 
     52 #include <db.h>
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <fcntl.h>
     56 #include <limits.h>
     57 #include <pwd.h>
     58 #include <signal.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 #include <util.h>
     64 
     65 #define	INSECURE	1
     66 #define	SECURE		2
     67 #define	PERM_INSECURE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
     68 #define	PERM_SECURE	(S_IRUSR|S_IWUSR)
     69 
     70 /* pull this out of the C library. */
     71 extern const char __yp_token[];
     72 
     73 HASHINFO openinfo = {
     74 	4096,		/* bsize */
     75 	32,		/* ffactor */
     76 	256,		/* nelem */
     77 	2048 * 1024,	/* cachesize */
     78 	NULL,		/* hash() */
     79 	0		/* lorder */
     80 };
     81 
     82 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
     83 static struct passwd pwd;			/* password structure */
     84 static char *pname;				/* password file name */
     85 static char prefix[MAXPATHLEN];
     86 static char oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
     87 static char pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
     88 static char pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
     89 
     90 void	cleanup __P((void));
     91 void	error __P((char *));
     92 void	wr_error __P((char *));
     93 int	main __P((int, char **));
     94 void	mv __P((char *, char *));
     95 void	rm __P((char *));
     96 int	scan __P((FILE *, struct passwd *, int *));
     97 void	usage __P((void));
     98 
     99 int
    100 main(argc, argv)
    101 	int argc;
    102 	char *argv[];
    103 {
    104 	DB *dp, *edp;
    105 	DBT data, key;
    106 	FILE *fp, *oldfp;
    107 	sigset_t set;
    108 	int ch, cnt, len, makeold, tfd, flags;
    109 	char *p, *t;
    110 	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
    111 	int hasyp = 0;
    112 	DBT ypdata, ypkey;
    113 
    114 	oldfp = NULL;
    115 	strcpy(prefix, "/");
    116 	makeold = 0;
    117 	while ((ch = getopt(argc, argv, "d:pv")) != -1)
    118 		switch(ch) {
    119 		case 'd':
    120 			strncpy(prefix, optarg, sizeof(prefix));
    121 			prefix[sizeof(prefix)-1] = '\0';
    122 			break;
    123 		case 'p':			/* create V7 "file.orig" */
    124 			makeold = 1;
    125 			break;
    126 		case 'v':			/* backward compatible */
    127 			break;
    128 		case '?':
    129 		default:
    130 			usage();
    131 		}
    132 	argc -= optind;
    133 	argv += optind;
    134 
    135 	if (argc != 1)
    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 	/* Open the temporary insecure password database. */
    159 	(void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp", prefix,
    160 		_PATH_MP_DB);
    161 	dp = dbopen(pwd_db_tmp,
    162 	    O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
    163 	if (dp == NULL)
    164 		error(pwd_db_tmp);
    165 	clean = FILE_INSECURE;
    166 
    167 	/*
    168 	 * Open file for old password file.  Minor trickiness -- don't want to
    169 	 * chance the file already existing, since someone (stupidly) might
    170 	 * still be using this for permission checking.  So, open it first and
    171 	 * fdopen the resulting fd.  The resulting file should be readable by
    172 	 * everyone.
    173 	 */
    174 	if (makeold) {
    175 		(void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
    176 			pname);
    177 		if ((tfd = open(oldpwdfile,
    178 		    O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
    179 			error(oldpwdfile);
    180 		if ((oldfp = fdopen(tfd, "w")) == NULL)
    181 			error(oldpwdfile);
    182 		clean = FILE_ORIG;
    183 	}
    184 
    185 	/*
    186 	 * The databases actually contain three copies of the original data.
    187 	 * Each password file entry is converted into a rough approximation
    188 	 * of a ``struct passwd'', with the strings placed inline.  This
    189 	 * object is then stored as the data for three separate keys.  The
    190 	 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
    191 	 * character.  The second key is the pw_uid field prepended by the
    192 	 * _PW_KEYBYUID character.  The third key is the line number in the
    193 	 * original file prepended by the _PW_KEYBYNUM character.  (The special
    194 	 * characters are prepended to ensure that the keys do not collide.)
    195 	 *
    196 	 * If we see something go by that looks like YP, we save a special
    197 	 * pointer record, which if YP is enabled in the C lib, will speed
    198 	 * things up.
    199 	 */
    200 	data.data = (u_char *)buf;
    201 	key.data = (u_char *)tbuf;
    202 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    203 #define	COMPACT(e)	t = e; while ((*p++ = *t++));
    204 
    205 		/* look like YP? */
    206 		if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
    207 			hasyp++;
    208 
    209 		/*
    210 		 * Warn about potentially unsafe uid/gid overrides.
    211 		 */
    212 		if (pwd.pw_name[0] == '+') {
    213 			if ((flags & _PASSWORD_NOUID) == 0 && pwd.pw_uid == 0)
    214 				warnx("line %d: superuser override in YP inclusion", cnt);
    215 			if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
    216 				warnx("line %d: wheel override in YP inclusion", cnt);
    217 		}
    218 
    219 		/* Create insecure data. */
    220 		p = buf;
    221 		COMPACT(pwd.pw_name);
    222 		COMPACT("*");
    223 		memmove(p, &pwd.pw_uid, sizeof(int));
    224 		p += sizeof(int);
    225 		memmove(p, &pwd.pw_gid, sizeof(int));
    226 		p += sizeof(int);
    227 		memmove(p, &pwd.pw_change, sizeof(time_t));
    228 		p += sizeof(time_t);
    229 		COMPACT(pwd.pw_class);
    230 		COMPACT(pwd.pw_gecos);
    231 		COMPACT(pwd.pw_dir);
    232 		COMPACT(pwd.pw_shell);
    233 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    234 		p += sizeof(time_t);
    235 		memmove(p, &flags, sizeof(int));
    236 		p += sizeof(int);
    237 		data.size = p - buf;
    238 
    239 		/* Store insecure by name. */
    240 		tbuf[0] = _PW_KEYBYNAME;
    241 		len = strlen(pwd.pw_name);
    242 		memmove(tbuf + 1, pwd.pw_name, len);
    243 		key.size = len + 1;
    244 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    245 			wr_error(pwd_db_tmp);
    246 
    247 		/* Store insecure by number. */
    248 		tbuf[0] = _PW_KEYBYNUM;
    249 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    250 		key.size = sizeof(cnt) + 1;
    251 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    252 			wr_error(pwd_db_tmp);
    253 
    254 		/* Store insecure by uid. */
    255 		tbuf[0] = _PW_KEYBYUID;
    256 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    257 		key.size = sizeof(pwd.pw_uid) + 1;
    258 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    259 			wr_error(pwd_db_tmp);
    260 
    261 		/* Create original format password file entry */
    262 		if (makeold) {
    263 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    264 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    265 			    pwd.pw_dir, pwd.pw_shell);
    266 			if (ferror(oldfp)) {
    267 				wr_error(oldpwdfile);
    268 			}
    269 		}
    270 	}
    271 
    272 	/* Store YP token, if needed. */
    273 	if(hasyp) {
    274 		ypkey.data = (u_char *)__yp_token;
    275 		ypkey.size = strlen(__yp_token);
    276 		ypdata.data = (u_char *)NULL;
    277 		ypdata.size = 0;
    278 
    279 		if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    280 			wr_error(pwd_db_tmp);
    281 	}
    282 
    283 	if ((dp->close)(dp) < 0) {
    284 		wr_error(pwd_db_tmp);
    285 	}
    286 	if (makeold) {
    287 		if (fflush(oldfp) == EOF) {
    288 			wr_error(oldpwdfile);
    289 		}
    290 		if (fclose(oldfp) == EOF) {
    291 			wr_error(oldpwdfile);
    292 		}
    293 	}
    294 
    295 	/* Open the temporary encrypted password database. */
    296 	(void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
    297 		_PATH_SMP_DB);
    298 	edp = dbopen(pwd_Sdb_tmp,
    299 	    O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
    300 	if (!edp)
    301 		error(pwd_Sdb_tmp);
    302 	clean = FILE_SECURE;
    303 
    304 	rewind(fp);
    305 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    306 
    307 		/* Create secure data. */
    308 		p = buf;
    309 		COMPACT(pwd.pw_name);
    310 		COMPACT(pwd.pw_passwd);
    311 		memmove(p, &pwd.pw_uid, sizeof(int));
    312 		p += sizeof(int);
    313 		memmove(p, &pwd.pw_gid, sizeof(int));
    314 		p += sizeof(int);
    315 		memmove(p, &pwd.pw_change, sizeof(time_t));
    316 		p += sizeof(time_t);
    317 		COMPACT(pwd.pw_class);
    318 		COMPACT(pwd.pw_gecos);
    319 		COMPACT(pwd.pw_dir);
    320 		COMPACT(pwd.pw_shell);
    321 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    322 		p += sizeof(time_t);
    323 		memmove(p, &flags, sizeof(int));
    324 		p += sizeof(int);
    325 		data.size = p - buf;
    326 
    327 		/* Store secure by name. */
    328 		tbuf[0] = _PW_KEYBYNAME;
    329 		len = strlen(pwd.pw_name);
    330 		memmove(tbuf + 1, pwd.pw_name, len);
    331 		key.size = len + 1;
    332 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    333 			error("put");
    334 
    335 		/* Store secure by number. */
    336 		tbuf[0] = _PW_KEYBYNUM;
    337 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    338 		key.size = sizeof(cnt) + 1;
    339 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    340 			error("put");
    341 
    342 		/* Store secure by uid. */
    343 		tbuf[0] = _PW_KEYBYUID;
    344 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    345 		key.size = sizeof(pwd.pw_uid) + 1;
    346 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    347 			error("put");
    348 	}
    349 
    350 	/* Store YP token, if needed. */
    351 	if(hasyp) {
    352 		ypkey.data = (u_char *)__yp_token;
    353 		ypkey.size = strlen(__yp_token);
    354 		ypdata.data = (u_char *)NULL;
    355 		ypdata.size = 0;
    356 
    357 		if((edp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    358 			error("put");
    359 	}
    360 
    361 	if ((edp->close)(edp) < 0) {
    362 		wr_error(_PATH_SMP_DB);
    363 	}
    364 
    365 	/* Set master.passwd permissions, in case caller forgot. */
    366 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    367 	if (fclose(fp) == EOF) {
    368 		wr_error(pname);
    369 	}
    370 
    371 	/* Install as the real password files. */
    372 	{
    373 		char	destination[MAXPATHLEN];
    374 
    375 		(void)snprintf(destination, sizeof(destination),
    376 			"%s%s", prefix, _PATH_MP_DB);
    377 		mv(pwd_db_tmp, destination);
    378 
    379 		(void)snprintf(destination, sizeof(destination),
    380 			"%s%s", prefix, _PATH_SMP_DB);
    381 		mv(pwd_Sdb_tmp, destination);
    382 
    383 		if (makeold) {
    384 			(void)snprintf(destination, sizeof(destination),
    385 				"%s%s", prefix, _PATH_PASSWD);
    386 			mv(oldpwdfile, destination);
    387 		}
    388 
    389 		/*
    390 		 * Move the master password LAST -- chpass(1),
    391 		 * passwd(1) and vipw(8) all use flock(2) on it to
    392 		 * block other incarnations of themselves. The rename
    393 		 * means that everything is unlocked, as the original
    394 		 * file can no longer be accessed.
    395 		 */
    396 		(void)snprintf(destination, sizeof(destination),
    397 			"%s%s", prefix, _PATH_MASTERPASSWD);
    398 		mv(pname, destination);
    399 	}
    400 	exit(0);
    401 }
    402 
    403 int
    404 scan(fp, pw, flags)
    405 	FILE *fp;
    406 	struct passwd *pw;
    407 	int *flags;
    408 {
    409 	static int lcnt;
    410 	static char line[LINE_MAX];
    411 	char *p;
    412 
    413 	if (!fgets(line, sizeof(line), fp))
    414 		return (0);
    415 	++lcnt;
    416 	/*
    417 	 * ``... if I swallow anything evil, put your fingers down my
    418 	 * throat...''
    419 	 *	-- The Who
    420 	 */
    421 	if (!(p = strchr(line, '\n'))) {
    422 		warnx("line too long");
    423 		goto fmt;
    424 
    425 	}
    426 	*p = '\0';
    427 	if (!pw_scan(line, pw, flags)) {
    428 		warnx("at line #%d", lcnt);
    429 fmt:		errno = EFTYPE;	/* XXX */
    430 		error(pname);
    431 	}
    432 
    433 	return (1);
    434 }
    435 
    436 void
    437 mv(from, to)
    438 	char *from, *to;
    439 {
    440 	char buf[MAXPATHLEN];
    441 
    442 	if (rename(from, to)) {
    443 		int sverrno = errno;
    444 		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
    445 		errno = sverrno;
    446 		error(buf);
    447 	}
    448 }
    449 
    450 void
    451 wr_error(name)
    452 	char *name;
    453 {
    454 	char	errbuf[BUFSIZ];
    455 	int	sverrno = errno;
    456 
    457 	(void)snprintf(errbuf, sizeof(errbuf),
    458 		"attempt to write %s failed", name);
    459 
    460 	errno = sverrno;
    461 	error(errbuf);
    462 }
    463 
    464 void
    465 error(name)
    466 	char *name;
    467 {
    468 
    469 	warn(name);
    470 	cleanup();
    471 #ifdef think_about_this_a_while_longer
    472 	fputs("NOTE: possible inconsistencies between text files and databases\n", stderr);
    473 	fputs("re-run pwd_mkdb when you have fixed the problem.\n", stderr);
    474 #endif
    475 	exit(1);
    476 }
    477 
    478 void
    479 rm(victim)
    480 	char *victim;
    481 {
    482 	if (unlink(victim) < 0) {
    483 		warn("unlink(%s)", victim);
    484 	}
    485 }
    486 
    487 void
    488 cleanup()
    489 {
    490 	char buf[MAXPATHLEN];
    491 
    492 	switch(clean) {
    493 	case FILE_ORIG:
    494 		rm(oldpwdfile);
    495 		/* FALLTHROUGH */
    496 	case FILE_SECURE:
    497 		rm(pwd_Sdb_tmp);
    498 		/* FALLTHROUGH */
    499 	case FILE_INSECURE:
    500 		rm(pwd_db_tmp);
    501 	}
    502 }
    503 
    504 void
    505 usage()
    506 {
    507 
    508 	(void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
    509 	exit(1);
    510 }
    511