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