Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.4
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * 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
     35 char copyright[] =
     36 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)pwd_mkdb.c	5.5 (Berkeley) 5/6/91";*/
     42 static char rcsid[] = "$Id: pwd_mkdb.c,v 1.4 1994/04/10 07:05:59 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <signal.h>
     48 #include <fcntl.h>
     49 #include <db.h>
     50 #include <pwd.h>
     51 #include <errno.h>
     52 #include <limits.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 
     56 #define	INSECURE	1
     57 #define	SECURE		2
     58 #define	PERM_INSECURE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
     59 #define	PERM_SECURE	(S_IRUSR|S_IWUSR)
     60 
     61 char *progname = "pwd_mkdb";
     62 
     63 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
     64 static struct passwd pwd;			/* password structure */
     65 static char *pname;				/* password file name */
     66 
     67 main(argc, argv)
     68 	int argc;
     69 	char **argv;
     70 {
     71 	extern int optind;
     72 	register int len, makeold;
     73 	register char *p, *t;
     74 	FILE *fp, *oldfp;
     75 	DB *dp, *edp;
     76 	sigset_t set;
     77 	DBT data, key;
     78 	int ch, cnt, tfd;
     79 	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
     80 
     81 	makeold = 0;
     82 	while ((ch = getopt(argc, argv, "pv")) != EOF)
     83 		switch(ch) {
     84 		case 'p':			/* create V7 "file.orig" */
     85 			makeold = 1;
     86 			break;
     87 		case 'v':			/* backward compatible */
     88 			break;
     89 		case '?':
     90 		default:
     91 			usage();
     92 		}
     93 	argc -= optind;
     94 	argv += optind;
     95 
     96 	if (argc != 1)
     97 		usage();
     98 
     99 	/* set umask explicitly, so that 077 doesn't mess up /etc/passwd */
    100 	umask(S_IWGRP|S_IWOTH);
    101 
    102 	/*
    103 	 * This could be done to allow the user to interrupt.  Probably
    104 	 * not worth the effort.
    105 	 */
    106 	sigemptyset(&set);
    107 	sigaddset(&set, SIGTSTP);
    108 	sigaddset(&set, SIGHUP);
    109 	sigaddset(&set, SIGINT);
    110 	sigaddset(&set, SIGQUIT);
    111 	sigaddset(&set, SIGTERM);
    112 	(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
    113 
    114 	pname = *argv;
    115 	/* Open the original password file */
    116 	if (!(fp = fopen(pname, "r")))
    117 		error(pname);
    118 
    119 	/* Open the temporary insecure password database. */
    120 	(void)sprintf(buf, "%s.tmp", _PATH_MP_DB);
    121 	dp = dbopen(buf, O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, NULL);
    122 	if (!dp)
    123 		error(buf);
    124 	clean = FILE_INSECURE;
    125 
    126 	/* Open the temporary encrypted password database. */
    127 	(void)sprintf(buf, "%s.tmp", _PATH_SMP_DB);
    128 	edp = dbopen(buf, O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, NULL);
    129 	if (!edp)
    130 		error(buf);
    131 	clean = FILE_SECURE;
    132 
    133 	/*
    134 	 * Open file for old password file.  Minor trickiness -- don't want to
    135 	 * chance the file already existing, since someone (stupidly) might
    136 	 * still be using this for permission checking.  So, open it first and
    137 	 * fdopen the resulting fd.  Don't really care who reads it.
    138 	 */
    139 	if (makeold) {
    140 		(void)sprintf(buf, "%s.orig", pname);
    141 		if ((tfd = open(buf,
    142 		    O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
    143 			error(buf);
    144 		if (!(oldfp = fdopen(tfd, "w")))
    145 			error(buf);
    146 		clean = FILE_ORIG;
    147 	}
    148 
    149 	/*
    150 	 * The databases actually contain three copies of the original data.
    151 	 * Each password file entry is converted into a rough approximation
    152 	 * of a ``struct passwd'', with the strings placed inline.  This
    153 	 * object is then stored as the data for three separate keys.  The
    154 	 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
    155 	 * character.  The second key is the pw_uid field prepended by the
    156 	 * _PW_KEYBYUID character.  The third key is the line number in the
    157 	 * original file prepended by the _PW_KEYBYNUM character.  (The special
    158 	 * characters are prepended to ensure that the keys do not collide.)
    159 	 */
    160 	data.data = (u_char *)buf;
    161 	key.data = (u_char *)tbuf;
    162 	for (cnt = 1; scan(fp, &pwd); ++cnt) {
    163 #define	COMPACT(e)	t = e; while (*p++ = *t++);
    164 		/* Create insecure data. */
    165 		p = buf;
    166 		COMPACT(pwd.pw_name);
    167 		COMPACT("*");
    168 		bcopy((char *)&pwd.pw_uid, p, sizeof(int));
    169 		p += sizeof(int);
    170 		bcopy((char *)&pwd.pw_gid, p, sizeof(int));
    171 		p += sizeof(int);
    172 		bcopy((char *)&pwd.pw_change, p, sizeof(time_t));
    173 		p += sizeof(time_t);
    174 		COMPACT(pwd.pw_class);
    175 		COMPACT(pwd.pw_gecos);
    176 		COMPACT(pwd.pw_dir);
    177 		COMPACT(pwd.pw_shell);
    178 		bcopy((char *)&pwd.pw_expire, p, sizeof(time_t));
    179 		p += sizeof(time_t);
    180 		data.size = p - buf;
    181 
    182 		/* Store insecure by name. */
    183 		tbuf[0] = _PW_KEYBYNAME;
    184 		len = strlen(pwd.pw_name);
    185 		bcopy(pwd.pw_name, tbuf + 1, len);
    186 		key.size = len + 1;
    187 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    188 			error("put");
    189 
    190 		/* Store insecure by number. */
    191 		tbuf[0] = _PW_KEYBYNUM;
    192 		bcopy((char *)&cnt, tbuf + 1, sizeof(cnt));
    193 		key.size = sizeof(cnt) + 1;
    194 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    195 			error("put");
    196 
    197 		/* Store insecure by uid. */
    198 		tbuf[0] = _PW_KEYBYUID;
    199 		bcopy((char *)&pwd.pw_uid, tbuf + 1, sizeof(pwd.pw_uid));
    200 		key.size = sizeof(pwd.pw_uid) + 1;
    201 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    202 			error("put");
    203 
    204 		/* Create secure data. */
    205 		p = buf;
    206 		COMPACT(pwd.pw_name);
    207 		COMPACT(pwd.pw_passwd);
    208 		bcopy((char *)&pwd.pw_uid, p, sizeof(int));
    209 		p += sizeof(int);
    210 		bcopy((char *)&pwd.pw_gid, p, sizeof(int));
    211 		p += sizeof(int);
    212 		bcopy((char *)&pwd.pw_change, p, sizeof(time_t));
    213 		p += sizeof(time_t);
    214 		COMPACT(pwd.pw_class);
    215 		COMPACT(pwd.pw_gecos);
    216 		COMPACT(pwd.pw_dir);
    217 		COMPACT(pwd.pw_shell);
    218 		bcopy((char *)&pwd.pw_expire, p, sizeof(time_t));
    219 		p += sizeof(time_t);
    220 		data.size = p - buf;
    221 
    222 		/* Store secure by name. */
    223 		tbuf[0] = _PW_KEYBYNAME;
    224 		len = strlen(pwd.pw_name);
    225 		bcopy(pwd.pw_name, tbuf + 1, len);
    226 		key.size = len + 1;
    227 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    228 			error("put");
    229 
    230 		/* Store secure by number. */
    231 		tbuf[0] = _PW_KEYBYNUM;
    232 		bcopy((char *)&cnt, tbuf + 1, sizeof(cnt));
    233 		key.size = sizeof(cnt) + 1;
    234 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    235 			error("put");
    236 
    237 		/* Store secure by uid. */
    238 		tbuf[0] = _PW_KEYBYUID;
    239 		bcopy((char *)&pwd.pw_uid, tbuf + 1, sizeof(pwd.pw_uid));
    240 		key.size = sizeof(pwd.pw_uid) + 1;
    241 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    242 			error("put");
    243 
    244 		/* Create original format password file entry */
    245 		if (makeold)
    246 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    247 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    248 			    pwd.pw_dir, pwd.pw_shell);
    249 	}
    250 	(void)(dp->close)(dp);
    251 	(void)(edp->close)(edp);
    252 	if (makeold) {
    253 		(void)fsync(oldfp);
    254 		(void)fclose(oldfp);
    255 	}
    256 
    257 	/* Set master.passwd permissions, in case caller forgot. */
    258 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    259 	(void)fclose(fp);
    260 
    261 	/* Install as the real password files. */
    262 	(void)sprintf(buf, "%s.tmp", _PATH_MP_DB);
    263 	mv(buf, _PATH_MP_DB);
    264 	(void)sprintf(buf, "%s.tmp", _PATH_SMP_DB);
    265 	mv(buf, _PATH_SMP_DB);
    266 	if (makeold) {
    267 		(void)sprintf(buf, "%s.orig", pname);
    268 		mv(buf, _PATH_PASSWD);
    269 	}
    270 	/*
    271 	 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
    272 	 * all use flock(2) on it to block other incarnations of themselves.
    273 	 * The rename means that everything is unlocked, as the original file
    274 	 * can no longer be accessed.
    275 	 */
    276 	mv(pname, _PATH_MASTERPASSWD);
    277 	exit(0);
    278 }
    279 
    280 scan(fp, pw)
    281 	FILE *fp;
    282 	struct passwd *pw;
    283 {
    284 	static int lcnt;
    285 	static char line[LINE_MAX];
    286 	char *p;
    287 
    288 	if (!fgets(line, sizeof(line), fp))
    289 		return(0);
    290 	++lcnt;
    291 	/*
    292 	 * ``... if I swallow anything evil, put your fingers down my
    293 	 * throat...''
    294 	 *	-- The Who
    295 	 */
    296 	if (!(p = index(line, '\n'))) {
    297 		(void)fprintf(stderr, "pwd_mkdb: line too long\n");
    298 		goto fmt;
    299 
    300 	}
    301 	*p = '\0';
    302 	if (!pw_scan(line, pw)) {
    303 		(void)fprintf(stderr, "pwd_mkdb: at line #%d.\n", lcnt);
    304 fmt:		errno = EFTYPE;
    305 		error(pname);
    306 		exit(1);
    307 	}
    308 }
    309 
    310 mv(from, to)
    311 	char *from, *to;
    312 {
    313 	int sverrno;
    314 	char buf[MAXPATHLEN];
    315 
    316 	if (rename(from, to)) {
    317 		sverrno = errno;
    318 		(void)sprintf(buf, "%s to %s", from, to);
    319 		errno = sverrno;
    320 		error(buf);
    321 	}
    322 }
    323 
    324 error(name)
    325 	char *name;
    326 {
    327 	(void)fprintf(stderr, "pwd_mkdb: %s: %s\n", name, strerror(errno));
    328 	cleanup();
    329 	exit(1);
    330 }
    331 
    332 cleanup()
    333 {
    334 	char buf[MAXPATHLEN];
    335 
    336 	switch(clean) {
    337 	case FILE_ORIG:
    338 		(void)sprintf(buf, "%s.orig", pname);
    339 		(void)unlink(buf);
    340 		/* FALLTHROUGH */
    341 	case FILE_SECURE:
    342 		(void)sprintf(buf, "%s.tmp", _PATH_SMP_DB);
    343 		(void)unlink(buf);
    344 		/* FALLTHROUGH */
    345 	case FILE_INSECURE:
    346 		(void)sprintf(buf, "%s.tmp", _PATH_MP_DB);
    347 		(void)unlink(buf);
    348 	}
    349 }
    350 
    351 usage()
    352 {
    353 	(void)fprintf(stderr, "usage: pwd_mkdb [-p] file\n");
    354 	exit(1);
    355 }
    356