Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.9
      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 #ifndef lint
     36 static char copyright[] =
     37 "@(#) 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 /*static char sccsid[] = "from: @(#)pwd_mkdb.c	8.5 (Berkeley) 4/20/94";*/
     43 static char *rcsid = "$Id: pwd_mkdb.c,v 1.9 1996/11/24 21:13:27 thorpej Exp $";
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 
     49 #include <db.h>
     50 #include <err.h>
     51 #include <errno.h>
     52 #include <fcntl.h>
     53 #include <limits.h>
     54 #include <pwd.h>
     55 #include <signal.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include <util.h>
     61 
     62 #define	INSECURE	1
     63 #define	SECURE		2
     64 #define	PERM_INSECURE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
     65 #define	PERM_SECURE	(S_IRUSR|S_IWUSR)
     66 
     67 /* pull this out of the C library. */
     68 extern const char __yp_token[];
     69 
     70 HASHINFO openinfo = {
     71 	4096,		/* bsize */
     72 	32,		/* ffactor */
     73 	256,		/* nelem */
     74 	2048 * 1024,	/* cachesize */
     75 	NULL,		/* hash() */
     76 	0		/* lorder */
     77 };
     78 
     79 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
     80 static struct passwd pwd;			/* password structure */
     81 static char *pname;				/* password file name */
     82 static char prefix[MAXPATHLEN];
     83 
     84 void	cleanup __P((void));
     85 void	error __P((char *));
     86 void	mv __P((char *, char *));
     87 int	scan __P((FILE *, struct passwd *, int *));
     88 void	usage __P((void));
     89 
     90 int
     91 main(argc, argv)
     92 	int argc;
     93 	char *argv[];
     94 {
     95 	DB *dp, *edp;
     96 	DBT data, key;
     97 	FILE *fp, *oldfp;
     98 	sigset_t set;
     99 	int ch, cnt, len, makeold, tfd, flags;
    100 	char *p, *t;
    101 	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], buf2[MAXPATHLEN], tbuf[1024];
    102 	int hasyp = 0;
    103 	DBT ypdata, ypkey;
    104 
    105 	strcpy(prefix, "/");
    106 	makeold = 0;
    107 	while ((ch = getopt(argc, argv, "d:pv")) != EOF)
    108 		switch(ch) {
    109 		case 'd':
    110 			strncpy(prefix, optarg, sizeof(prefix));
    111 			prefix[sizeof(prefix)-1] = '\0';
    112 			break;
    113 		case 'p':			/* create V7 "file.orig" */
    114 			makeold = 1;
    115 			break;
    116 		case 'v':			/* backward compatible */
    117 			break;
    118 		case '?':
    119 		default:
    120 			usage();
    121 		}
    122 	argc -= optind;
    123 	argv += optind;
    124 
    125 	if (argc != 1)
    126 		usage();
    127 
    128 	/*
    129 	 * This could be changed to allow the user to interrupt.
    130 	 * Probably not worth the effort.
    131 	 */
    132 	sigemptyset(&set);
    133 	sigaddset(&set, SIGTSTP);
    134 	sigaddset(&set, SIGHUP);
    135 	sigaddset(&set, SIGINT);
    136 	sigaddset(&set, SIGQUIT);
    137 	sigaddset(&set, SIGTERM);
    138 	(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
    139 
    140 	/* We don't care what the user wants. */
    141 	(void)umask(0);
    142 
    143 	pname = *argv;
    144 	/* Open the original password file */
    145 	if (!(fp = fopen(pname, "r")))
    146 		error(pname);
    147 
    148 	/* Open the temporary insecure password database. */
    149 	(void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_MP_DB);
    150 	dp = dbopen(buf,
    151 	    O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
    152 	if (dp == NULL)
    153 		error(buf);
    154 	clean = FILE_INSECURE;
    155 
    156 	/*
    157 	 * Open file for old password file.  Minor trickiness -- don't want to
    158 	 * chance the file already existing, since someone (stupidly) might
    159 	 * still be using this for permission checking.  So, open it first and
    160 	 * fdopen the resulting fd.  The resulting file should be readable by
    161 	 * everyone.
    162 	 */
    163 	if (makeold) {
    164 		(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
    165 		if ((tfd = open(buf,
    166 		    O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
    167 			error(buf);
    168 		if ((oldfp = fdopen(tfd, "w")) == NULL)
    169 			error(buf);
    170 		clean = FILE_ORIG;
    171 	}
    172 
    173 	/*
    174 	 * The databases actually contain three copies of the original data.
    175 	 * Each password file entry is converted into a rough approximation
    176 	 * of a ``struct passwd'', with the strings placed inline.  This
    177 	 * object is then stored as the data for three separate keys.  The
    178 	 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
    179 	 * character.  The second key is the pw_uid field prepended by the
    180 	 * _PW_KEYBYUID character.  The third key is the line number in the
    181 	 * original file prepended by the _PW_KEYBYNUM character.  (The special
    182 	 * characters are prepended to ensure that the keys do not collide.)
    183 	 *
    184 	 * If we see something go by that looks like YP, we save a special
    185 	 * pointer record, which if YP is enabled in the C lib, will speed
    186 	 * things up.
    187 	 */
    188 	data.data = (u_char *)buf;
    189 	key.data = (u_char *)tbuf;
    190 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    191 #define	COMPACT(e)	t = e; while ((*p++ = *t++));
    192 
    193 		/* look like YP? */
    194 		if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
    195 			hasyp++;
    196 
    197 		/*
    198 		 * Warn about potentially unsafe uid/gid overrides.
    199 		 */
    200 		if (pwd.pw_name[0] == '+') {
    201 			if ((flags & _PASSWORD_NOUID) == 0 && pwd.pw_uid == 0)
    202 				warnx("line %d: superuser override in YP inclusion", cnt);
    203 			if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
    204 				warnx("line %d: wheel override in YP inclusion", cnt);
    205 		}
    206 
    207 		/* Create insecure data. */
    208 		p = buf;
    209 		COMPACT(pwd.pw_name);
    210 		COMPACT("*");
    211 		memmove(p, &pwd.pw_uid, sizeof(int));
    212 		p += sizeof(int);
    213 		memmove(p, &pwd.pw_gid, sizeof(int));
    214 		p += sizeof(int);
    215 		memmove(p, &pwd.pw_change, sizeof(time_t));
    216 		p += sizeof(time_t);
    217 		COMPACT(pwd.pw_class);
    218 		COMPACT(pwd.pw_gecos);
    219 		COMPACT(pwd.pw_dir);
    220 		COMPACT(pwd.pw_shell);
    221 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    222 		p += sizeof(time_t);
    223 		memmove(p, &flags, sizeof(int));
    224 		p += sizeof(int);
    225 		data.size = p - buf;
    226 
    227 		/* Store insecure by name. */
    228 		tbuf[0] = _PW_KEYBYNAME;
    229 		len = strlen(pwd.pw_name);
    230 		memmove(tbuf + 1, pwd.pw_name, len);
    231 		key.size = len + 1;
    232 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    233 			error("put");
    234 
    235 		/* Store insecure by number. */
    236 		tbuf[0] = _PW_KEYBYNUM;
    237 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    238 		key.size = sizeof(cnt) + 1;
    239 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    240 			error("put");
    241 
    242 		/* Store insecure by uid. */
    243 		tbuf[0] = _PW_KEYBYUID;
    244 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    245 		key.size = sizeof(pwd.pw_uid) + 1;
    246 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    247 			error("put");
    248 
    249 		/* Create original format password file entry */
    250 		if (makeold)
    251 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    252 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    253 			    pwd.pw_dir, pwd.pw_shell);
    254 	}
    255 
    256 	/* Store YP token, if needed. */
    257 	if(hasyp) {
    258 		ypkey.data = (u_char *)__yp_token;
    259 		ypkey.size = strlen(__yp_token);
    260 		ypdata.data = (u_char *)NULL;
    261 		ypdata.size = 0;
    262 
    263 		if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    264 			error("put");
    265 	}
    266 
    267 	(void)(dp->close)(dp);
    268 	if (makeold) {
    269 		(void)fflush(oldfp);
    270 		(void)fclose(oldfp);
    271 	}
    272 
    273 	/* Open the temporary encrypted password database. */
    274 	(void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_SMP_DB);
    275 	edp = dbopen(buf,
    276 	    O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
    277 	if (!edp)
    278 		error(buf);
    279 	clean = FILE_SECURE;
    280 
    281 	rewind(fp);
    282 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    283 
    284 		/* Create secure data. */
    285 		p = buf;
    286 		COMPACT(pwd.pw_name);
    287 		COMPACT(pwd.pw_passwd);
    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, &flags, sizeof(int));
    301 		p += sizeof(int);
    302 		data.size = p - buf;
    303 
    304 		/* Store secure by name. */
    305 		tbuf[0] = _PW_KEYBYNAME;
    306 		len = strlen(pwd.pw_name);
    307 		memmove(tbuf + 1, pwd.pw_name, len);
    308 		key.size = len + 1;
    309 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    310 			error("put");
    311 
    312 		/* Store secure by number. */
    313 		tbuf[0] = _PW_KEYBYNUM;
    314 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    315 		key.size = sizeof(cnt) + 1;
    316 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    317 			error("put");
    318 
    319 		/* Store secure by uid. */
    320 		tbuf[0] = _PW_KEYBYUID;
    321 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    322 		key.size = sizeof(pwd.pw_uid) + 1;
    323 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    324 			error("put");
    325 	}
    326 
    327 	/* Store YP token, if needed. */
    328 	if(hasyp) {
    329 		ypkey.data = (u_char *)__yp_token;
    330 		ypkey.size = strlen(__yp_token);
    331 		ypdata.data = (u_char *)NULL;
    332 		ypdata.size = 0;
    333 
    334 		if((dp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    335 			error("put");
    336 	}
    337 
    338 	(void)(edp->close)(edp);
    339 
    340 	/* Set master.passwd permissions, in case caller forgot. */
    341 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    342 	(void)fclose(fp);
    343 
    344 	/* Install as the real password files. */
    345 	(void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_MP_DB);
    346 	(void)snprintf(buf2, sizeof(buf2), "%s%s", prefix, _PATH_MP_DB);
    347 	mv(buf, buf2);
    348 	(void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_SMP_DB);
    349 	(void)snprintf(buf2, sizeof(buf2), "%s%s", prefix, _PATH_SMP_DB);
    350 	mv(buf, buf2);
    351 	if (makeold) {
    352 		(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
    353 		(void)snprintf(buf2, sizeof(buf2), "%s%s", prefix,
    354 		    _PATH_PASSWD);
    355 		mv(buf, buf2);
    356 	}
    357 	/*
    358 	 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
    359 	 * all use flock(2) on it to block other incarnations of themselves.
    360 	 * The rename means that everything is unlocked, as the original file
    361 	 * can no longer be accessed.
    362 	 */
    363 	(void)snprintf(buf, sizeof(buf), "%s%s", prefix, _PATH_MASTERPASSWD);
    364 	mv(pname, buf);
    365 	exit(0);
    366 }
    367 
    368 int
    369 scan(fp, pw, flags)
    370 	FILE *fp;
    371 	struct passwd *pw;
    372 	int *flags;
    373 {
    374 	static int lcnt;
    375 	static char line[LINE_MAX];
    376 	char *p;
    377 
    378 	if (!fgets(line, sizeof(line), fp))
    379 		return (0);
    380 	++lcnt;
    381 	/*
    382 	 * ``... if I swallow anything evil, put your fingers down my
    383 	 * throat...''
    384 	 *	-- The Who
    385 	 */
    386 	if (!(p = strchr(line, '\n'))) {
    387 		warnx("line too long");
    388 		goto fmt;
    389 
    390 	}
    391 	*p = '\0';
    392 	if (!pw_scan(line, pw, flags)) {
    393 		warnx("at line #%d", lcnt);
    394 fmt:		errno = EFTYPE;	/* XXX */
    395 		error(pname);
    396 	}
    397 
    398 	return (1);
    399 }
    400 
    401 void
    402 mv(from, to)
    403 	char *from, *to;
    404 {
    405 	char buf[MAXPATHLEN];
    406 
    407 	if (rename(from, to)) {
    408 		int sverrno = errno;
    409 		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
    410 		errno = sverrno;
    411 		error(buf);
    412 	}
    413 }
    414 
    415 void
    416 error(name)
    417 	char *name;
    418 {
    419 
    420 	warn(name);
    421 	cleanup();
    422 	exit(1);
    423 }
    424 
    425 void
    426 cleanup()
    427 {
    428 	char buf[MAXPATHLEN];
    429 
    430 	switch(clean) {
    431 	case FILE_ORIG:
    432 		(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
    433 		(void)unlink(buf);
    434 		/* FALLTHROUGH */
    435 	case FILE_SECURE:
    436 		(void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix,
    437 		    _PATH_SMP_DB);
    438 		(void)unlink(buf);
    439 		/* FALLTHROUGH */
    440 	case FILE_INSECURE:
    441 		(void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix,
    442 		    _PATH_MP_DB);
    443 		(void)unlink(buf);
    444 	}
    445 }
    446 
    447 void
    448 usage()
    449 {
    450 
    451 	(void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
    452 	exit(1);
    453 }
    454