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