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