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