Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd char copyright[] =
     36  1.1  cgd "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
     37  1.1  cgd  All rights reserved.\n";
     38  1.1  cgd #endif /* not lint */
     39  1.1  cgd 
     40  1.1  cgd #ifndef lint
     41  1.1  cgd static char sccsid[] = "@(#)pwd_mkdb.c	5.5 (Berkeley) 5/6/91";
     42  1.1  cgd #endif /* not lint */
     43  1.1  cgd 
     44  1.1  cgd #include <sys/param.h>
     45  1.1  cgd #include <sys/stat.h>
     46  1.1  cgd #include <signal.h>
     47  1.1  cgd #include <fcntl.h>
     48  1.1  cgd #include <db.h>
     49  1.1  cgd #include <pwd.h>
     50  1.1  cgd #include <errno.h>
     51  1.1  cgd #include <limits.h>
     52  1.1  cgd #include <stdio.h>
     53  1.1  cgd #include <string.h>
     54  1.1  cgd 
     55  1.1  cgd #define	INSECURE	1
     56  1.1  cgd #define	SECURE		2
     57  1.1  cgd #define	PERM_INSECURE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
     58  1.1  cgd #define	PERM_SECURE	(S_IRUSR|S_IWUSR)
     59  1.1  cgd 
     60  1.1  cgd char *progname = "pwd_mkdb";
     61  1.1  cgd 
     62  1.1  cgd static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
     63  1.1  cgd static struct passwd pwd;			/* password structure */
     64  1.1  cgd static char *pname;				/* password file name */
     65  1.1  cgd 
     66  1.1  cgd main(argc, argv)
     67  1.1  cgd 	int argc;
     68  1.1  cgd 	char **argv;
     69  1.1  cgd {
     70  1.1  cgd 	extern int optind;
     71  1.1  cgd 	register int len, makeold;
     72  1.1  cgd 	register char *p, *t;
     73  1.1  cgd 	FILE *fp, *oldfp;
     74  1.1  cgd 	DB *dp, *edp;
     75  1.1  cgd 	sigset_t set;
     76  1.1  cgd 	DBT data, key;
     77  1.1  cgd 	int ch, cnt, tfd;
     78  1.1  cgd 	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
     79  1.1  cgd 
     80  1.1  cgd 	makeold = 0;
     81  1.1  cgd 	while ((ch = getopt(argc, argv, "pv")) != EOF)
     82  1.1  cgd 		switch(ch) {
     83  1.1  cgd 		case 'p':			/* create V7 "file.orig" */
     84  1.1  cgd 			makeold = 1;
     85  1.1  cgd 			break;
     86  1.1  cgd 		case 'v':			/* backward compatible */
     87  1.1  cgd 			break;
     88  1.1  cgd 		case '?':
     89  1.1  cgd 		default:
     90  1.1  cgd 			usage();
     91  1.1  cgd 		}
     92  1.1  cgd 	argc -= optind;
     93  1.1  cgd 	argv += optind;
     94  1.1  cgd 
     95  1.1  cgd 	if (argc != 1)
     96  1.1  cgd 		usage();
     97  1.1  cgd 
     98  1.1  cgd 	/*
     99  1.1  cgd 	 * This could be done to allow the user to interrupt.  Probably
    100  1.1  cgd 	 * not worth the effort.
    101  1.1  cgd 	 */
    102  1.1  cgd 	sigemptyset(&set);
    103  1.1  cgd 	sigaddset(&set, SIGTSTP);
    104  1.1  cgd 	sigaddset(&set, SIGHUP);
    105  1.1  cgd 	sigaddset(&set, SIGINT);
    106  1.1  cgd 	sigaddset(&set, SIGQUIT);
    107  1.1  cgd 	sigaddset(&set, SIGTERM);
    108  1.1  cgd 	(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
    109  1.1  cgd 
    110  1.1  cgd 	pname = *argv;
    111  1.1  cgd 	/* Open the original password file */
    112  1.1  cgd 	if (!(fp = fopen(pname, "r")))
    113  1.1  cgd 		error(pname);
    114  1.1  cgd 
    115  1.1  cgd 	/* Open the temporary insecure password database. */
    116  1.1  cgd 	(void)sprintf(buf, "%s.tmp", _PATH_MP_DB);
    117  1.1  cgd 	dp = hash_open(buf, O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE, NULL);
    118  1.1  cgd 	if (!dp)
    119  1.1  cgd 		error(buf);
    120  1.1  cgd 	clean = FILE_INSECURE;
    121  1.1  cgd 
    122  1.1  cgd 	/* Open the temporary encrypted password database. */
    123  1.1  cgd 	(void)sprintf(buf, "%s.tmp", _PATH_SMP_DB);
    124  1.1  cgd 	edp = hash_open(buf, O_WRONLY|O_CREAT|O_EXCL, PERM_SECURE, NULL);
    125  1.1  cgd 	if (!edp)
    126  1.1  cgd 		error(buf);
    127  1.1  cgd 	clean = FILE_SECURE;
    128  1.1  cgd 
    129  1.1  cgd 	/*
    130  1.1  cgd 	 * Open file for old password file.  Minor trickiness -- don't want to
    131  1.1  cgd 	 * chance the file already existing, since someone (stupidly) might
    132  1.1  cgd 	 * still be using this for permission checking.  So, open it first and
    133  1.1  cgd 	 * fdopen the resulting fd.  Don't really care who reads it.
    134  1.1  cgd 	 */
    135  1.1  cgd 	if (makeold) {
    136  1.1  cgd 		(void)sprintf(buf, "%s.orig", pname);
    137  1.1  cgd 		if ((tfd = open(buf,
    138  1.1  cgd 		    O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
    139  1.1  cgd 			error(buf);
    140  1.1  cgd 		if (!(oldfp = fdopen(tfd, "w")))
    141  1.1  cgd 			error(buf);
    142  1.1  cgd 		clean = FILE_ORIG;
    143  1.1  cgd 	}
    144  1.1  cgd 
    145  1.1  cgd 	/*
    146  1.1  cgd 	 * The databases actually contain three copies of the original data.
    147  1.1  cgd 	 * Each password file entry is converted into a rough approximation
    148  1.1  cgd 	 * of a ``struct passwd'', with the strings placed inline.  This
    149  1.1  cgd 	 * object is then stored as the data for three separate keys.  The
    150  1.1  cgd 	 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
    151  1.1  cgd 	 * character.  The second key is the pw_uid field prepended by the
    152  1.1  cgd 	 * _PW_KEYBYUID character.  The third key is the line number in the
    153  1.1  cgd 	 * original file prepended by the _PW_KEYBYNUM character.  (The special
    154  1.1  cgd 	 * characters are prepended to ensure that the keys do not collide.)
    155  1.1  cgd 	 */
    156  1.1  cgd 	data.data = (u_char *)buf;
    157  1.1  cgd 	key.data = (u_char *)tbuf;
    158  1.1  cgd 	for (cnt = 1; scan(fp, &pwd); ++cnt) {
    159  1.1  cgd #define	COMPACT(e)	t = e; while (*p++ = *t++);
    160  1.1  cgd 		/* Create insecure data. */
    161  1.1  cgd 		p = buf;
    162  1.1  cgd 		COMPACT(pwd.pw_name);
    163  1.1  cgd 		COMPACT("*");
    164  1.1  cgd 		bcopy((char *)&pwd.pw_uid, p, sizeof(int));
    165  1.1  cgd 		p += sizeof(int);
    166  1.1  cgd 		bcopy((char *)&pwd.pw_gid, p, sizeof(int));
    167  1.1  cgd 		p += sizeof(int);
    168  1.1  cgd 		bcopy((char *)&pwd.pw_change, p, sizeof(time_t));
    169  1.1  cgd 		p += sizeof(time_t);
    170  1.1  cgd 		COMPACT(pwd.pw_class);
    171  1.1  cgd 		COMPACT(pwd.pw_gecos);
    172  1.1  cgd 		COMPACT(pwd.pw_dir);
    173  1.1  cgd 		COMPACT(pwd.pw_shell);
    174  1.1  cgd 		bcopy((char *)&pwd.pw_expire, p, sizeof(time_t));
    175  1.1  cgd 		p += sizeof(time_t);
    176  1.1  cgd 		data.size = p - buf;
    177  1.1  cgd 
    178  1.1  cgd 		/* Store insecure by name. */
    179  1.1  cgd 		tbuf[0] = _PW_KEYBYNAME;
    180  1.1  cgd 		len = strlen(pwd.pw_name);
    181  1.1  cgd 		bcopy(pwd.pw_name, tbuf + 1, len);
    182  1.1  cgd 		key.size = len + 1;
    183  1.1  cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    184  1.1  cgd 			error("put");
    185  1.1  cgd 
    186  1.1  cgd 		/* Store insecure by number. */
    187  1.1  cgd 		tbuf[0] = _PW_KEYBYNUM;
    188  1.1  cgd 		bcopy((char *)&cnt, tbuf + 1, sizeof(cnt));
    189  1.1  cgd 		key.size = sizeof(cnt) + 1;
    190  1.1  cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    191  1.1  cgd 			error("put");
    192  1.1  cgd 
    193  1.1  cgd 		/* Store insecure by uid. */
    194  1.1  cgd 		tbuf[0] = _PW_KEYBYUID;
    195  1.1  cgd 		bcopy((char *)&pwd.pw_uid, tbuf + 1, sizeof(pwd.pw_uid));
    196  1.1  cgd 		key.size = sizeof(pwd.pw_uid) + 1;
    197  1.1  cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    198  1.1  cgd 			error("put");
    199  1.1  cgd 
    200  1.1  cgd 		/* Create secure data. */
    201  1.1  cgd 		p = buf;
    202  1.1  cgd 		COMPACT(pwd.pw_name);
    203  1.1  cgd 		COMPACT(pwd.pw_passwd);
    204  1.1  cgd 		bcopy((char *)&pwd.pw_uid, p, sizeof(int));
    205  1.1  cgd 		p += sizeof(int);
    206  1.1  cgd 		bcopy((char *)&pwd.pw_gid, p, sizeof(int));
    207  1.1  cgd 		p += sizeof(int);
    208  1.1  cgd 		bcopy((char *)&pwd.pw_change, p, sizeof(time_t));
    209  1.1  cgd 		p += sizeof(time_t);
    210  1.1  cgd 		COMPACT(pwd.pw_class);
    211  1.1  cgd 		COMPACT(pwd.pw_gecos);
    212  1.1  cgd 		COMPACT(pwd.pw_dir);
    213  1.1  cgd 		COMPACT(pwd.pw_shell);
    214  1.1  cgd 		bcopy((char *)&pwd.pw_expire, p, sizeof(time_t));
    215  1.1  cgd 		p += sizeof(time_t);
    216  1.1  cgd 		data.size = p - buf;
    217  1.1  cgd 
    218  1.1  cgd 		/* Store secure by name. */
    219  1.1  cgd 		tbuf[0] = _PW_KEYBYNAME;
    220  1.1  cgd 		len = strlen(pwd.pw_name);
    221  1.1  cgd 		bcopy(pwd.pw_name, tbuf + 1, len);
    222  1.1  cgd 		key.size = len + 1;
    223  1.1  cgd 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    224  1.1  cgd 			error("put");
    225  1.1  cgd 
    226  1.1  cgd 		/* Store secure by number. */
    227  1.1  cgd 		tbuf[0] = _PW_KEYBYNUM;
    228  1.1  cgd 		bcopy((char *)&cnt, tbuf + 1, sizeof(cnt));
    229  1.1  cgd 		key.size = sizeof(cnt) + 1;
    230  1.1  cgd 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    231  1.1  cgd 			error("put");
    232  1.1  cgd 
    233  1.1  cgd 		/* Store secure by uid. */
    234  1.1  cgd 		tbuf[0] = _PW_KEYBYUID;
    235  1.1  cgd 		bcopy((char *)&pwd.pw_uid, tbuf + 1, sizeof(pwd.pw_uid));
    236  1.1  cgd 		key.size = sizeof(pwd.pw_uid) + 1;
    237  1.1  cgd 		if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    238  1.1  cgd 			error("put");
    239  1.1  cgd 
    240  1.1  cgd 		/* Create original format password file entry */
    241  1.1  cgd 		if (makeold)
    242  1.1  cgd 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    243  1.1  cgd 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    244  1.1  cgd 			    pwd.pw_dir, pwd.pw_shell);
    245  1.1  cgd 	}
    246  1.1  cgd 	(void)(dp->close)(dp);
    247  1.1  cgd 	(void)(edp->close)(edp);
    248  1.1  cgd 	if (makeold) {
    249  1.1  cgd 		(void)fsync(oldfp);
    250  1.1  cgd 		(void)fclose(oldfp);
    251  1.1  cgd 	}
    252  1.1  cgd 
    253  1.1  cgd 	/* Set master.passwd permissions, in case caller forgot. */
    254  1.1  cgd 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    255  1.1  cgd 	(void)fclose(fp);
    256  1.1  cgd 
    257  1.1  cgd 	/* Install as the real password files. */
    258  1.1  cgd 	(void)sprintf(buf, "%s.tmp", _PATH_MP_DB);
    259  1.1  cgd 	mv(buf, _PATH_MP_DB);
    260  1.1  cgd 	(void)sprintf(buf, "%s.tmp", _PATH_SMP_DB);
    261  1.1  cgd 	mv(buf, _PATH_SMP_DB);
    262  1.1  cgd 	if (makeold) {
    263  1.1  cgd 		(void)sprintf(buf, "%s.orig", pname);
    264  1.1  cgd 		mv(buf, _PATH_PASSWD);
    265  1.1  cgd 	}
    266  1.1  cgd 	/*
    267  1.1  cgd 	 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
    268  1.1  cgd 	 * all use flock(2) on it to block other incarnations of themselves.
    269  1.1  cgd 	 * The rename means that everything is unlocked, as the original file
    270  1.1  cgd 	 * can no longer be accessed.
    271  1.1  cgd 	 */
    272  1.1  cgd 	mv(pname, _PATH_MASTERPASSWD);
    273  1.1  cgd 	exit(0);
    274  1.1  cgd }
    275  1.1  cgd 
    276  1.1  cgd scan(fp, pw)
    277  1.1  cgd 	FILE *fp;
    278  1.1  cgd 	struct passwd *pw;
    279  1.1  cgd {
    280  1.1  cgd 	static int lcnt;
    281  1.1  cgd 	static char line[LINE_MAX];
    282  1.1  cgd 	char *p;
    283  1.1  cgd 
    284  1.1  cgd 	if (!fgets(line, sizeof(line), fp))
    285  1.1  cgd 		return(0);
    286  1.1  cgd 	++lcnt;
    287  1.1  cgd 	/*
    288  1.1  cgd 	 * ``... if I swallow anything evil, put your fingers down my
    289  1.1  cgd 	 * throat...''
    290  1.1  cgd 	 *	-- The Who
    291  1.1  cgd 	 */
    292  1.1  cgd 	if (!(p = index(line, '\n'))) {
    293  1.1  cgd 		(void)fprintf(stderr, "pwd_mkdb: line too long\n");
    294  1.1  cgd 		goto fmt;
    295  1.1  cgd 
    296  1.1  cgd 	}
    297  1.1  cgd 	*p = '\0';
    298  1.1  cgd 	if (!pw_scan(line, pw)) {
    299  1.1  cgd 		(void)fprintf(stderr, "pwd_mkdb: at line #%d.\n", lcnt);
    300  1.1  cgd fmt:		errno = EFTYPE;
    301  1.1  cgd 		error(pname);
    302  1.1  cgd 		exit(1);
    303  1.1  cgd 	}
    304  1.1  cgd }
    305  1.1  cgd 
    306  1.1  cgd mv(from, to)
    307  1.1  cgd 	char *from, *to;
    308  1.1  cgd {
    309  1.1  cgd 	int sverrno;
    310  1.1  cgd 	char buf[MAXPATHLEN];
    311  1.1  cgd 
    312  1.1  cgd 	if (rename(from, to)) {
    313  1.1  cgd 		sverrno = errno;
    314  1.1  cgd 		(void)sprintf(buf, "%s to %s", from, to);
    315  1.1  cgd 		errno = sverrno;
    316  1.1  cgd 		error(buf);
    317  1.1  cgd 	}
    318  1.1  cgd }
    319  1.1  cgd 
    320  1.1  cgd error(name)
    321  1.1  cgd 	char *name;
    322  1.1  cgd {
    323  1.1  cgd 	(void)fprintf(stderr, "pwd_mkdb: %s: %s\n", name, strerror(errno));
    324  1.1  cgd 	cleanup();
    325  1.1  cgd 	exit(1);
    326  1.1  cgd }
    327  1.1  cgd 
    328  1.1  cgd cleanup()
    329  1.1  cgd {
    330  1.1  cgd 	char buf[MAXPATHLEN];
    331  1.1  cgd 
    332  1.1  cgd 	switch(clean) {
    333  1.1  cgd 	case FILE_ORIG:
    334  1.1  cgd 		(void)sprintf(buf, "%s.orig", pname);
    335  1.1  cgd 		(void)unlink(buf);
    336  1.1  cgd 		/* FALLTHROUGH */
    337  1.1  cgd 	case FILE_SECURE:
    338  1.1  cgd 		(void)sprintf(buf, "%s.tmp", _PATH_SMP_DB);
    339  1.1  cgd 		(void)unlink(buf);
    340  1.1  cgd 		/* FALLTHROUGH */
    341  1.1  cgd 	case FILE_INSECURE:
    342  1.1  cgd 		(void)sprintf(buf, "%s.tmp", _PATH_MP_DB);
    343  1.1  cgd 		(void)unlink(buf);
    344  1.1  cgd 	}
    345  1.1  cgd }
    346  1.1  cgd 
    347  1.1  cgd usage()
    348  1.1  cgd {
    349  1.1  cgd 	(void)fprintf(stderr, "usage: pwd_mkdb [-p] file\n");
    350  1.1  cgd 	exit(1);
    351  1.1  cgd }
    352