Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.15
      1  1.11     fair /*
      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.10    lukem #include <sys/cdefs.h>
     36   1.1      cgd #ifndef lint
     37  1.10    lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
     38  1.10    lukem 	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.10    lukem #if 0
     43  1.10    lukem static char sccsid[] = "from: @(#)pwd_mkdb.c	8.5 (Berkeley) 4/20/94";
     44  1.10    lukem #else
     45  1.15    lukem __RCSID("$NetBSD: pwd_mkdb.c,v 1.15 1998/06/08 03:23:07 lukem Exp $");
     46  1.10    lukem #endif
     47   1.1      cgd #endif /* not lint */
     48   1.1      cgd 
     49   1.1      cgd #include <sys/param.h>
     50   1.1      cgd #include <sys/stat.h>
     51   1.5  mycroft 
     52   1.1      cgd #include <db.h>
     53   1.5  mycroft #include <err.h>
     54   1.1      cgd #include <errno.h>
     55   1.5  mycroft #include <fcntl.h>
     56   1.1      cgd #include <limits.h>
     57   1.5  mycroft #include <pwd.h>
     58   1.5  mycroft #include <signal.h>
     59   1.1      cgd #include <stdio.h>
     60   1.5  mycroft #include <stdlib.h>
     61   1.1      cgd #include <string.h>
     62   1.5  mycroft #include <unistd.h>
     63   1.7      jtc #include <util.h>
     64   1.1      cgd 
     65   1.1      cgd #define	INSECURE	1
     66   1.1      cgd #define	SECURE		2
     67   1.1      cgd #define	PERM_INSECURE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
     68   1.1      cgd #define	PERM_SECURE	(S_IRUSR|S_IWUSR)
     69   1.1      cgd 
     70   1.6     phil /* pull this out of the C library. */
     71   1.6     phil extern const char __yp_token[];
     72   1.6     phil 
     73   1.5  mycroft HASHINFO openinfo = {
     74   1.5  mycroft 	4096,		/* bsize */
     75   1.5  mycroft 	32,		/* ffactor */
     76   1.5  mycroft 	256,		/* nelem */
     77   1.5  mycroft 	2048 * 1024,	/* cachesize */
     78   1.5  mycroft 	NULL,		/* hash() */
     79   1.5  mycroft 	0		/* lorder */
     80   1.5  mycroft };
     81   1.1      cgd 
     82   1.1      cgd static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
     83   1.1      cgd static struct passwd pwd;			/* password structure */
     84   1.1      cgd static char *pname;				/* password file name */
     85   1.8    lukem static char prefix[MAXPATHLEN];
     86  1.11     fair static char oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
     87  1.11     fair static char pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
     88  1.11     fair static char pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
     89   1.1      cgd 
     90   1.5  mycroft void	cleanup __P((void));
     91   1.5  mycroft void	error __P((char *));
     92  1.11     fair void	wr_error __P((char *));
     93  1.10    lukem int	main __P((int, char **));
     94   1.5  mycroft void	mv __P((char *, char *));
     95  1.11     fair void	rm __P((char *));
     96   1.6     phil int	scan __P((FILE *, struct passwd *, int *));
     97   1.5  mycroft void	usage __P((void));
     98   1.5  mycroft 
     99   1.5  mycroft int
    100   1.1      cgd main(argc, argv)
    101   1.1      cgd 	int argc;
    102   1.5  mycroft 	char *argv[];
    103   1.1      cgd {
    104   1.5  mycroft 	DB *dp, *edp;
    105   1.5  mycroft 	DBT data, key;
    106   1.1      cgd 	FILE *fp, *oldfp;
    107   1.1      cgd 	sigset_t set;
    108   1.6     phil 	int ch, cnt, len, makeold, tfd, flags;
    109   1.5  mycroft 	char *p, *t;
    110  1.11     fair 	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
    111   1.6     phil 	int hasyp = 0;
    112   1.6     phil 	DBT ypdata, ypkey;
    113   1.1      cgd 
    114  1.10    lukem 	oldfp = NULL;
    115   1.8    lukem 	strcpy(prefix, "/");
    116   1.1      cgd 	makeold = 0;
    117  1.10    lukem 	while ((ch = getopt(argc, argv, "d:pv")) != -1)
    118   1.1      cgd 		switch(ch) {
    119   1.8    lukem 		case 'd':
    120   1.8    lukem 			strncpy(prefix, optarg, sizeof(prefix));
    121   1.8    lukem 			prefix[sizeof(prefix)-1] = '\0';
    122   1.8    lukem 			break;
    123   1.1      cgd 		case 'p':			/* create V7 "file.orig" */
    124   1.1      cgd 			makeold = 1;
    125   1.1      cgd 			break;
    126   1.1      cgd 		case 'v':			/* backward compatible */
    127   1.1      cgd 			break;
    128   1.1      cgd 		case '?':
    129   1.1      cgd 		default:
    130   1.1      cgd 			usage();
    131   1.1      cgd 		}
    132   1.1      cgd 	argc -= optind;
    133   1.1      cgd 	argv += optind;
    134   1.1      cgd 
    135   1.1      cgd 	if (argc != 1)
    136   1.1      cgd 		usage();
    137   1.4      cgd 
    138   1.1      cgd 	/*
    139   1.5  mycroft 	 * This could be changed to allow the user to interrupt.
    140   1.5  mycroft 	 * Probably not worth the effort.
    141   1.1      cgd 	 */
    142   1.1      cgd 	sigemptyset(&set);
    143   1.1      cgd 	sigaddset(&set, SIGTSTP);
    144   1.1      cgd 	sigaddset(&set, SIGHUP);
    145   1.1      cgd 	sigaddset(&set, SIGINT);
    146   1.1      cgd 	sigaddset(&set, SIGQUIT);
    147   1.1      cgd 	sigaddset(&set, SIGTERM);
    148   1.1      cgd 	(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
    149   1.1      cgd 
    150   1.5  mycroft 	/* We don't care what the user wants. */
    151   1.5  mycroft 	(void)umask(0);
    152   1.5  mycroft 
    153   1.1      cgd 	pname = *argv;
    154   1.1      cgd 	/* Open the original password file */
    155   1.1      cgd 	if (!(fp = fopen(pname, "r")))
    156   1.1      cgd 		error(pname);
    157   1.1      cgd 
    158   1.1      cgd 	/* Open the temporary insecure password database. */
    159  1.11     fair 	(void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp", prefix,
    160  1.11     fair 		_PATH_MP_DB);
    161  1.11     fair 	dp = dbopen(pwd_db_tmp,
    162   1.5  mycroft 	    O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
    163   1.5  mycroft 	if (dp == NULL)
    164  1.11     fair 		error(pwd_db_tmp);
    165   1.1      cgd 	clean = FILE_INSECURE;
    166   1.1      cgd 
    167   1.1      cgd 	/*
    168   1.1      cgd 	 * Open file for old password file.  Minor trickiness -- don't want to
    169   1.1      cgd 	 * chance the file already existing, since someone (stupidly) might
    170   1.1      cgd 	 * still be using this for permission checking.  So, open it first and
    171   1.5  mycroft 	 * fdopen the resulting fd.  The resulting file should be readable by
    172   1.5  mycroft 	 * everyone.
    173   1.1      cgd 	 */
    174   1.1      cgd 	if (makeold) {
    175  1.11     fair 		(void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
    176  1.11     fair 			pname);
    177  1.11     fair 		if ((tfd = open(oldpwdfile,
    178   1.1      cgd 		    O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
    179  1.11     fair 			error(oldpwdfile);
    180   1.5  mycroft 		if ((oldfp = fdopen(tfd, "w")) == NULL)
    181  1.11     fair 			error(oldpwdfile);
    182   1.1      cgd 		clean = FILE_ORIG;
    183   1.1      cgd 	}
    184   1.1      cgd 
    185   1.1      cgd 	/*
    186   1.1      cgd 	 * The databases actually contain three copies of the original data.
    187   1.1      cgd 	 * Each password file entry is converted into a rough approximation
    188   1.1      cgd 	 * of a ``struct passwd'', with the strings placed inline.  This
    189   1.1      cgd 	 * object is then stored as the data for three separate keys.  The
    190   1.1      cgd 	 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
    191   1.1      cgd 	 * character.  The second key is the pw_uid field prepended by the
    192   1.1      cgd 	 * _PW_KEYBYUID character.  The third key is the line number in the
    193   1.1      cgd 	 * original file prepended by the _PW_KEYBYNUM character.  (The special
    194   1.1      cgd 	 * characters are prepended to ensure that the keys do not collide.)
    195   1.6     phil 	 *
    196   1.6     phil 	 * If we see something go by that looks like YP, we save a special
    197   1.6     phil 	 * pointer record, which if YP is enabled in the C lib, will speed
    198   1.6     phil 	 * things up.
    199   1.1      cgd 	 */
    200   1.1      cgd 	data.data = (u_char *)buf;
    201   1.1      cgd 	key.data = (u_char *)tbuf;
    202   1.6     phil 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    203   1.8    lukem #define	COMPACT(e)	t = e; while ((*p++ = *t++));
    204   1.6     phil 
    205   1.6     phil 		/* look like YP? */
    206   1.6     phil 		if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
    207   1.6     phil 			hasyp++;
    208   1.9  thorpej 
    209   1.9  thorpej 		/*
    210   1.9  thorpej 		 * Warn about potentially unsafe uid/gid overrides.
    211   1.9  thorpej 		 */
    212   1.9  thorpej 		if (pwd.pw_name[0] == '+') {
    213   1.9  thorpej 			if ((flags & _PASSWORD_NOUID) == 0 && pwd.pw_uid == 0)
    214  1.15    lukem 				warnx(
    215  1.15    lukem 				"line %d: superuser override in YP inclusion",
    216  1.15    lukem 				    cnt);
    217   1.9  thorpej 			if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
    218  1.15    lukem 				warnx("line %d: wheel override in YP inclusion",
    219  1.15    lukem 				    cnt);
    220   1.9  thorpej 		}
    221   1.6     phil 
    222   1.1      cgd 		/* Create insecure data. */
    223   1.1      cgd 		p = buf;
    224   1.1      cgd 		COMPACT(pwd.pw_name);
    225   1.1      cgd 		COMPACT("*");
    226   1.5  mycroft 		memmove(p, &pwd.pw_uid, sizeof(int));
    227   1.1      cgd 		p += sizeof(int);
    228   1.5  mycroft 		memmove(p, &pwd.pw_gid, sizeof(int));
    229   1.1      cgd 		p += sizeof(int);
    230   1.5  mycroft 		memmove(p, &pwd.pw_change, sizeof(time_t));
    231   1.1      cgd 		p += sizeof(time_t);
    232   1.1      cgd 		COMPACT(pwd.pw_class);
    233   1.1      cgd 		COMPACT(pwd.pw_gecos);
    234   1.1      cgd 		COMPACT(pwd.pw_dir);
    235   1.1      cgd 		COMPACT(pwd.pw_shell);
    236   1.5  mycroft 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    237   1.1      cgd 		p += sizeof(time_t);
    238   1.6     phil 		memmove(p, &flags, sizeof(int));
    239   1.6     phil 		p += sizeof(int);
    240   1.1      cgd 		data.size = p - buf;
    241   1.1      cgd 
    242   1.1      cgd 		/* Store insecure by name. */
    243   1.1      cgd 		tbuf[0] = _PW_KEYBYNAME;
    244   1.1      cgd 		len = strlen(pwd.pw_name);
    245   1.5  mycroft 		memmove(tbuf + 1, pwd.pw_name, len);
    246   1.1      cgd 		key.size = len + 1;
    247   1.1      cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    248  1.11     fair 			wr_error(pwd_db_tmp);
    249   1.1      cgd 
    250   1.1      cgd 		/* Store insecure by number. */
    251   1.1      cgd 		tbuf[0] = _PW_KEYBYNUM;
    252   1.5  mycroft 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    253   1.1      cgd 		key.size = sizeof(cnt) + 1;
    254   1.1      cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    255  1.11     fair 			wr_error(pwd_db_tmp);
    256   1.1      cgd 
    257   1.1      cgd 		/* Store insecure by uid. */
    258   1.1      cgd 		tbuf[0] = _PW_KEYBYUID;
    259   1.5  mycroft 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    260   1.1      cgd 		key.size = sizeof(pwd.pw_uid) + 1;
    261   1.1      cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    262  1.11     fair 			wr_error(pwd_db_tmp);
    263   1.1      cgd 
    264   1.5  mycroft 		/* Create original format password file entry */
    265  1.11     fair 		if (makeold) {
    266   1.5  mycroft 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    267   1.5  mycroft 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    268   1.5  mycroft 			    pwd.pw_dir, pwd.pw_shell);
    269  1.11     fair 			if (ferror(oldfp)) {
    270  1.11     fair 				wr_error(oldpwdfile);
    271  1.11     fair 			}
    272  1.11     fair 		}
    273   1.5  mycroft 	}
    274   1.6     phil 
    275   1.6     phil 	/* Store YP token, if needed. */
    276   1.6     phil 	if(hasyp) {
    277   1.6     phil 		ypkey.data = (u_char *)__yp_token;
    278   1.6     phil 		ypkey.size = strlen(__yp_token);
    279   1.6     phil 		ypdata.data = (u_char *)NULL;
    280   1.6     phil 		ypdata.size = 0;
    281   1.6     phil 
    282   1.6     phil 		if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    283  1.11     fair 			wr_error(pwd_db_tmp);
    284   1.6     phil 	}
    285   1.6     phil 
    286  1.11     fair 	if ((dp->close)(dp) < 0) {
    287  1.11     fair 		wr_error(pwd_db_tmp);
    288  1.11     fair 	}
    289   1.5  mycroft 	if (makeold) {
    290  1.11     fair 		if (fflush(oldfp) == EOF) {
    291  1.11     fair 			wr_error(oldpwdfile);
    292  1.11     fair 		}
    293  1.11     fair 		if (fclose(oldfp) == EOF) {
    294  1.11     fair 			wr_error(oldpwdfile);
    295  1.11     fair 		}
    296   1.5  mycroft 	}
    297   1.5  mycroft 
    298   1.5  mycroft 	/* Open the temporary encrypted password database. */
    299  1.11     fair 	(void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
    300  1.11     fair 		_PATH_SMP_DB);
    301  1.11     fair 	edp = dbopen(pwd_Sdb_tmp,
    302   1.5  mycroft 	    O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
    303   1.5  mycroft 	if (!edp)
    304  1.11     fair 		error(pwd_Sdb_tmp);
    305   1.5  mycroft 	clean = FILE_SECURE;
    306   1.5  mycroft 
    307   1.5  mycroft 	rewind(fp);
    308   1.6     phil 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    309   1.5  mycroft 
    310   1.1      cgd 		/* Create secure data. */
    311   1.1      cgd 		p = buf;
    312   1.1      cgd 		COMPACT(pwd.pw_name);
    313   1.1      cgd 		COMPACT(pwd.pw_passwd);
    314   1.5  mycroft 		memmove(p, &pwd.pw_uid, sizeof(int));
    315   1.1      cgd 		p += sizeof(int);
    316   1.5  mycroft 		memmove(p, &pwd.pw_gid, sizeof(int));
    317   1.1      cgd 		p += sizeof(int);
    318   1.5  mycroft 		memmove(p, &pwd.pw_change, sizeof(time_t));
    319   1.1      cgd 		p += sizeof(time_t);
    320   1.1      cgd 		COMPACT(pwd.pw_class);
    321   1.1      cgd 		COMPACT(pwd.pw_gecos);
    322   1.1      cgd 		COMPACT(pwd.pw_dir);
    323   1.1      cgd 		COMPACT(pwd.pw_shell);
    324   1.5  mycroft 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    325   1.1      cgd 		p += sizeof(time_t);
    326   1.6     phil 		memmove(p, &flags, sizeof(int));
    327   1.6     phil 		p += sizeof(int);
    328   1.1      cgd 		data.size = p - buf;
    329   1.1      cgd 
    330   1.1      cgd 		/* Store secure by name. */
    331   1.1      cgd 		tbuf[0] = _PW_KEYBYNAME;
    332   1.1      cgd 		len = strlen(pwd.pw_name);
    333   1.5  mycroft 		memmove(tbuf + 1, pwd.pw_name, len);
    334   1.1      cgd 		key.size = len + 1;
    335  1.11     fair 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    336  1.12     fair 			wr_error(pwd_Sdb_tmp);
    337   1.1      cgd 
    338   1.1      cgd 		/* Store secure by number. */
    339   1.1      cgd 		tbuf[0] = _PW_KEYBYNUM;
    340   1.5  mycroft 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    341   1.1      cgd 		key.size = sizeof(cnt) + 1;
    342  1.11     fair 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    343  1.12     fair 			wr_error(pwd_Sdb_tmp);
    344   1.1      cgd 
    345   1.1      cgd 		/* Store secure by uid. */
    346   1.1      cgd 		tbuf[0] = _PW_KEYBYUID;
    347   1.5  mycroft 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    348   1.1      cgd 		key.size = sizeof(pwd.pw_uid) + 1;
    349  1.11     fair 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    350  1.12     fair 			wr_error(pwd_Sdb_tmp);
    351   1.5  mycroft 	}
    352   1.1      cgd 
    353   1.6     phil 	/* Store YP token, if needed. */
    354   1.6     phil 	if(hasyp) {
    355   1.6     phil 		ypkey.data = (u_char *)__yp_token;
    356   1.6     phil 		ypkey.size = strlen(__yp_token);
    357   1.6     phil 		ypdata.data = (u_char *)NULL;
    358   1.6     phil 		ypdata.size = 0;
    359   1.6     phil 
    360  1.11     fair 		if((edp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    361  1.12     fair 			wr_error(pwd_Sdb_tmp);
    362   1.6     phil 	}
    363   1.6     phil 
    364  1.11     fair 	if ((edp->close)(edp) < 0) {
    365  1.11     fair 		wr_error(_PATH_SMP_DB);
    366  1.11     fair 	}
    367   1.1      cgd 
    368   1.1      cgd 	/* Set master.passwd permissions, in case caller forgot. */
    369   1.1      cgd 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    370  1.11     fair 	if (fclose(fp) == EOF) {
    371  1.11     fair 		wr_error(pname);
    372  1.11     fair 	}
    373   1.1      cgd 
    374   1.1      cgd 	/* Install as the real password files. */
    375  1.11     fair 	{
    376  1.11     fair 		char	destination[MAXPATHLEN];
    377  1.11     fair 
    378  1.11     fair 		(void)snprintf(destination, sizeof(destination),
    379  1.11     fair 			"%s%s", prefix, _PATH_MP_DB);
    380  1.11     fair 		mv(pwd_db_tmp, destination);
    381  1.11     fair 
    382  1.11     fair 		(void)snprintf(destination, sizeof(destination),
    383  1.11     fair 			"%s%s", prefix, _PATH_SMP_DB);
    384  1.11     fair 		mv(pwd_Sdb_tmp, destination);
    385  1.11     fair 
    386  1.11     fair 		if (makeold) {
    387  1.11     fair 			(void)snprintf(destination, sizeof(destination),
    388  1.11     fair 				"%s%s", prefix, _PATH_PASSWD);
    389  1.11     fair 			mv(oldpwdfile, destination);
    390  1.11     fair 		}
    391  1.11     fair 
    392  1.11     fair 		/*
    393  1.11     fair 		 * Move the master password LAST -- chpass(1),
    394  1.11     fair 		 * passwd(1) and vipw(8) all use flock(2) on it to
    395  1.11     fair 		 * block other incarnations of themselves. The rename
    396  1.11     fair 		 * means that everything is unlocked, as the original
    397  1.11     fair 		 * file can no longer be accessed.
    398  1.11     fair 		 */
    399  1.11     fair 		(void)snprintf(destination, sizeof(destination),
    400  1.11     fair 			"%s%s", prefix, _PATH_MASTERPASSWD);
    401  1.11     fair 		mv(pname, destination);
    402   1.1      cgd 	}
    403   1.1      cgd 	exit(0);
    404   1.1      cgd }
    405   1.1      cgd 
    406   1.5  mycroft int
    407   1.6     phil scan(fp, pw, flags)
    408   1.1      cgd 	FILE *fp;
    409   1.1      cgd 	struct passwd *pw;
    410   1.6     phil 	int *flags;
    411   1.1      cgd {
    412   1.1      cgd 	static int lcnt;
    413   1.1      cgd 	static char line[LINE_MAX];
    414   1.1      cgd 	char *p;
    415   1.1      cgd 
    416   1.1      cgd 	if (!fgets(line, sizeof(line), fp))
    417   1.5  mycroft 		return (0);
    418   1.1      cgd 	++lcnt;
    419   1.1      cgd 	/*
    420   1.1      cgd 	 * ``... if I swallow anything evil, put your fingers down my
    421   1.1      cgd 	 * throat...''
    422   1.1      cgd 	 *	-- The Who
    423   1.1      cgd 	 */
    424   1.5  mycroft 	if (!(p = strchr(line, '\n'))) {
    425   1.5  mycroft 		warnx("line too long");
    426   1.1      cgd 		goto fmt;
    427   1.1      cgd 
    428   1.1      cgd 	}
    429   1.1      cgd 	*p = '\0';
    430  1.15    lukem 	if (strcmp(line, "+") == 0)
    431  1.15    lukem 		strcpy(line, "+:::::::::");	/* pw_scan() can't handle "+" */
    432  1.14    lukem 	*flags = 0;
    433   1.6     phil 	if (!pw_scan(line, pw, flags)) {
    434   1.5  mycroft 		warnx("at line #%d", lcnt);
    435   1.5  mycroft fmt:		errno = EFTYPE;	/* XXX */
    436   1.1      cgd 		error(pname);
    437   1.1      cgd 	}
    438   1.5  mycroft 
    439   1.5  mycroft 	return (1);
    440   1.1      cgd }
    441   1.1      cgd 
    442   1.5  mycroft void
    443   1.1      cgd mv(from, to)
    444   1.1      cgd 	char *from, *to;
    445   1.1      cgd {
    446   1.1      cgd 	char buf[MAXPATHLEN];
    447   1.1      cgd 
    448   1.1      cgd 	if (rename(from, to)) {
    449   1.5  mycroft 		int sverrno = errno;
    450   1.5  mycroft 		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
    451   1.1      cgd 		errno = sverrno;
    452   1.1      cgd 		error(buf);
    453   1.1      cgd 	}
    454   1.1      cgd }
    455   1.1      cgd 
    456   1.5  mycroft void
    457  1.11     fair wr_error(name)
    458  1.11     fair 	char *name;
    459  1.11     fair {
    460  1.11     fair 	char	errbuf[BUFSIZ];
    461  1.11     fair 	int	sverrno = errno;
    462  1.11     fair 
    463  1.11     fair 	(void)snprintf(errbuf, sizeof(errbuf),
    464  1.11     fair 		"attempt to write %s failed", name);
    465  1.11     fair 
    466  1.11     fair 	errno = sverrno;
    467  1.11     fair 	error(errbuf);
    468  1.11     fair }
    469  1.11     fair 
    470  1.11     fair void
    471   1.1      cgd error(name)
    472   1.1      cgd 	char *name;
    473   1.1      cgd {
    474   1.5  mycroft 
    475   1.5  mycroft 	warn(name);
    476   1.1      cgd 	cleanup();
    477  1.11     fair #ifdef think_about_this_a_while_longer
    478  1.11     fair 	fputs("NOTE: possible inconsistencies between text files and databases\n", stderr);
    479  1.11     fair 	fputs("re-run pwd_mkdb when you have fixed the problem.\n", stderr);
    480  1.11     fair #endif
    481   1.1      cgd 	exit(1);
    482   1.1      cgd }
    483   1.1      cgd 
    484   1.5  mycroft void
    485  1.11     fair rm(victim)
    486  1.11     fair 	char *victim;
    487  1.11     fair {
    488  1.11     fair 	if (unlink(victim) < 0) {
    489  1.11     fair 		warn("unlink(%s)", victim);
    490  1.11     fair 	}
    491  1.11     fair }
    492  1.11     fair 
    493  1.11     fair void
    494   1.1      cgd cleanup()
    495   1.1      cgd {
    496   1.1      cgd 	switch(clean) {
    497   1.1      cgd 	case FILE_ORIG:
    498  1.11     fair 		rm(oldpwdfile);
    499   1.1      cgd 		/* FALLTHROUGH */
    500   1.1      cgd 	case FILE_SECURE:
    501  1.11     fair 		rm(pwd_Sdb_tmp);
    502   1.1      cgd 		/* FALLTHROUGH */
    503   1.1      cgd 	case FILE_INSECURE:
    504  1.11     fair 		rm(pwd_db_tmp);
    505   1.1      cgd 	}
    506   1.1      cgd }
    507   1.1      cgd 
    508   1.5  mycroft void
    509   1.1      cgd usage()
    510   1.1      cgd {
    511   1.5  mycroft 
    512   1.8    lukem 	(void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
    513   1.1      cgd 	exit(1);
    514   1.1      cgd }
    515