Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.14
      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.14    lukem __RCSID("$NetBSD: pwd_mkdb.c,v 1.14 1998/06/07 14:40:29 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.9  thorpej 				warnx("line %d: superuser override in YP inclusion", cnt);
    215   1.9  thorpej 			if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
    216   1.9  thorpej 				warnx("line %d: wheel override in YP inclusion", cnt);
    217   1.9  thorpej 		}
    218   1.6     phil 
    219   1.1      cgd 		/* Create insecure data. */
    220   1.1      cgd 		p = buf;
    221   1.1      cgd 		COMPACT(pwd.pw_name);
    222   1.1      cgd 		COMPACT("*");
    223   1.5  mycroft 		memmove(p, &pwd.pw_uid, sizeof(int));
    224   1.1      cgd 		p += sizeof(int);
    225   1.5  mycroft 		memmove(p, &pwd.pw_gid, sizeof(int));
    226   1.1      cgd 		p += sizeof(int);
    227   1.5  mycroft 		memmove(p, &pwd.pw_change, sizeof(time_t));
    228   1.1      cgd 		p += sizeof(time_t);
    229   1.1      cgd 		COMPACT(pwd.pw_class);
    230   1.1      cgd 		COMPACT(pwd.pw_gecos);
    231   1.1      cgd 		COMPACT(pwd.pw_dir);
    232   1.1      cgd 		COMPACT(pwd.pw_shell);
    233   1.5  mycroft 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    234   1.1      cgd 		p += sizeof(time_t);
    235   1.6     phil 		memmove(p, &flags, sizeof(int));
    236   1.6     phil 		p += sizeof(int);
    237   1.1      cgd 		data.size = p - buf;
    238   1.1      cgd 
    239   1.1      cgd 		/* Store insecure by name. */
    240   1.1      cgd 		tbuf[0] = _PW_KEYBYNAME;
    241   1.1      cgd 		len = strlen(pwd.pw_name);
    242   1.5  mycroft 		memmove(tbuf + 1, pwd.pw_name, len);
    243   1.1      cgd 		key.size = len + 1;
    244   1.1      cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    245  1.11     fair 			wr_error(pwd_db_tmp);
    246   1.1      cgd 
    247   1.1      cgd 		/* Store insecure by number. */
    248   1.1      cgd 		tbuf[0] = _PW_KEYBYNUM;
    249   1.5  mycroft 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    250   1.1      cgd 		key.size = sizeof(cnt) + 1;
    251   1.1      cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    252  1.11     fair 			wr_error(pwd_db_tmp);
    253   1.1      cgd 
    254   1.1      cgd 		/* Store insecure by uid. */
    255   1.1      cgd 		tbuf[0] = _PW_KEYBYUID;
    256   1.5  mycroft 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    257   1.1      cgd 		key.size = sizeof(pwd.pw_uid) + 1;
    258   1.1      cgd 		if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    259  1.11     fair 			wr_error(pwd_db_tmp);
    260   1.1      cgd 
    261   1.5  mycroft 		/* Create original format password file entry */
    262  1.11     fair 		if (makeold) {
    263   1.5  mycroft 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    264   1.5  mycroft 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    265   1.5  mycroft 			    pwd.pw_dir, pwd.pw_shell);
    266  1.11     fair 			if (ferror(oldfp)) {
    267  1.11     fair 				wr_error(oldpwdfile);
    268  1.11     fair 			}
    269  1.11     fair 		}
    270   1.5  mycroft 	}
    271   1.6     phil 
    272   1.6     phil 	/* Store YP token, if needed. */
    273   1.6     phil 	if(hasyp) {
    274   1.6     phil 		ypkey.data = (u_char *)__yp_token;
    275   1.6     phil 		ypkey.size = strlen(__yp_token);
    276   1.6     phil 		ypdata.data = (u_char *)NULL;
    277   1.6     phil 		ypdata.size = 0;
    278   1.6     phil 
    279   1.6     phil 		if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    280  1.11     fair 			wr_error(pwd_db_tmp);
    281   1.6     phil 	}
    282   1.6     phil 
    283  1.11     fair 	if ((dp->close)(dp) < 0) {
    284  1.11     fair 		wr_error(pwd_db_tmp);
    285  1.11     fair 	}
    286   1.5  mycroft 	if (makeold) {
    287  1.11     fair 		if (fflush(oldfp) == EOF) {
    288  1.11     fair 			wr_error(oldpwdfile);
    289  1.11     fair 		}
    290  1.11     fair 		if (fclose(oldfp) == EOF) {
    291  1.11     fair 			wr_error(oldpwdfile);
    292  1.11     fair 		}
    293   1.5  mycroft 	}
    294   1.5  mycroft 
    295   1.5  mycroft 	/* Open the temporary encrypted password database. */
    296  1.11     fair 	(void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
    297  1.11     fair 		_PATH_SMP_DB);
    298  1.11     fair 	edp = dbopen(pwd_Sdb_tmp,
    299   1.5  mycroft 	    O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
    300   1.5  mycroft 	if (!edp)
    301  1.11     fair 		error(pwd_Sdb_tmp);
    302   1.5  mycroft 	clean = FILE_SECURE;
    303   1.5  mycroft 
    304   1.5  mycroft 	rewind(fp);
    305   1.6     phil 	for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
    306   1.5  mycroft 
    307   1.1      cgd 		/* Create secure data. */
    308   1.1      cgd 		p = buf;
    309   1.1      cgd 		COMPACT(pwd.pw_name);
    310   1.1      cgd 		COMPACT(pwd.pw_passwd);
    311   1.5  mycroft 		memmove(p, &pwd.pw_uid, sizeof(int));
    312   1.1      cgd 		p += sizeof(int);
    313   1.5  mycroft 		memmove(p, &pwd.pw_gid, sizeof(int));
    314   1.1      cgd 		p += sizeof(int);
    315   1.5  mycroft 		memmove(p, &pwd.pw_change, sizeof(time_t));
    316   1.1      cgd 		p += sizeof(time_t);
    317   1.1      cgd 		COMPACT(pwd.pw_class);
    318   1.1      cgd 		COMPACT(pwd.pw_gecos);
    319   1.1      cgd 		COMPACT(pwd.pw_dir);
    320   1.1      cgd 		COMPACT(pwd.pw_shell);
    321   1.5  mycroft 		memmove(p, &pwd.pw_expire, sizeof(time_t));
    322   1.1      cgd 		p += sizeof(time_t);
    323   1.6     phil 		memmove(p, &flags, sizeof(int));
    324   1.6     phil 		p += sizeof(int);
    325   1.1      cgd 		data.size = p - buf;
    326   1.1      cgd 
    327   1.1      cgd 		/* Store secure by name. */
    328   1.1      cgd 		tbuf[0] = _PW_KEYBYNAME;
    329   1.1      cgd 		len = strlen(pwd.pw_name);
    330   1.5  mycroft 		memmove(tbuf + 1, pwd.pw_name, len);
    331   1.1      cgd 		key.size = len + 1;
    332  1.11     fair 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    333  1.12     fair 			wr_error(pwd_Sdb_tmp);
    334   1.1      cgd 
    335   1.1      cgd 		/* Store secure by number. */
    336   1.1      cgd 		tbuf[0] = _PW_KEYBYNUM;
    337   1.5  mycroft 		memmove(tbuf + 1, &cnt, sizeof(cnt));
    338   1.1      cgd 		key.size = sizeof(cnt) + 1;
    339  1.11     fair 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    340  1.12     fair 			wr_error(pwd_Sdb_tmp);
    341   1.1      cgd 
    342   1.1      cgd 		/* Store secure by uid. */
    343   1.1      cgd 		tbuf[0] = _PW_KEYBYUID;
    344   1.5  mycroft 		memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    345   1.1      cgd 		key.size = sizeof(pwd.pw_uid) + 1;
    346  1.11     fair 		if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
    347  1.12     fair 			wr_error(pwd_Sdb_tmp);
    348   1.5  mycroft 	}
    349   1.1      cgd 
    350   1.6     phil 	/* Store YP token, if needed. */
    351   1.6     phil 	if(hasyp) {
    352   1.6     phil 		ypkey.data = (u_char *)__yp_token;
    353   1.6     phil 		ypkey.size = strlen(__yp_token);
    354   1.6     phil 		ypdata.data = (u_char *)NULL;
    355   1.6     phil 		ypdata.size = 0;
    356   1.6     phil 
    357  1.11     fair 		if((edp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
    358  1.12     fair 			wr_error(pwd_Sdb_tmp);
    359   1.6     phil 	}
    360   1.6     phil 
    361  1.11     fair 	if ((edp->close)(edp) < 0) {
    362  1.11     fair 		wr_error(_PATH_SMP_DB);
    363  1.11     fair 	}
    364   1.1      cgd 
    365   1.1      cgd 	/* Set master.passwd permissions, in case caller forgot. */
    366   1.1      cgd 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    367  1.11     fair 	if (fclose(fp) == EOF) {
    368  1.11     fair 		wr_error(pname);
    369  1.11     fair 	}
    370   1.1      cgd 
    371   1.1      cgd 	/* Install as the real password files. */
    372  1.11     fair 	{
    373  1.11     fair 		char	destination[MAXPATHLEN];
    374  1.11     fair 
    375  1.11     fair 		(void)snprintf(destination, sizeof(destination),
    376  1.11     fair 			"%s%s", prefix, _PATH_MP_DB);
    377  1.11     fair 		mv(pwd_db_tmp, destination);
    378  1.11     fair 
    379  1.11     fair 		(void)snprintf(destination, sizeof(destination),
    380  1.11     fair 			"%s%s", prefix, _PATH_SMP_DB);
    381  1.11     fair 		mv(pwd_Sdb_tmp, destination);
    382  1.11     fair 
    383  1.11     fair 		if (makeold) {
    384  1.11     fair 			(void)snprintf(destination, sizeof(destination),
    385  1.11     fair 				"%s%s", prefix, _PATH_PASSWD);
    386  1.11     fair 			mv(oldpwdfile, destination);
    387  1.11     fair 		}
    388  1.11     fair 
    389  1.11     fair 		/*
    390  1.11     fair 		 * Move the master password LAST -- chpass(1),
    391  1.11     fair 		 * passwd(1) and vipw(8) all use flock(2) on it to
    392  1.11     fair 		 * block other incarnations of themselves. The rename
    393  1.11     fair 		 * means that everything is unlocked, as the original
    394  1.11     fair 		 * file can no longer be accessed.
    395  1.11     fair 		 */
    396  1.11     fair 		(void)snprintf(destination, sizeof(destination),
    397  1.11     fair 			"%s%s", prefix, _PATH_MASTERPASSWD);
    398  1.11     fair 		mv(pname, destination);
    399   1.1      cgd 	}
    400   1.1      cgd 	exit(0);
    401   1.1      cgd }
    402   1.1      cgd 
    403   1.5  mycroft int
    404   1.6     phil scan(fp, pw, flags)
    405   1.1      cgd 	FILE *fp;
    406   1.1      cgd 	struct passwd *pw;
    407   1.6     phil 	int *flags;
    408   1.1      cgd {
    409   1.1      cgd 	static int lcnt;
    410   1.1      cgd 	static char line[LINE_MAX];
    411   1.1      cgd 	char *p;
    412   1.1      cgd 
    413   1.1      cgd 	if (!fgets(line, sizeof(line), fp))
    414   1.5  mycroft 		return (0);
    415   1.1      cgd 	++lcnt;
    416   1.1      cgd 	/*
    417   1.1      cgd 	 * ``... if I swallow anything evil, put your fingers down my
    418   1.1      cgd 	 * throat...''
    419   1.1      cgd 	 *	-- The Who
    420   1.1      cgd 	 */
    421   1.5  mycroft 	if (!(p = strchr(line, '\n'))) {
    422   1.5  mycroft 		warnx("line too long");
    423   1.1      cgd 		goto fmt;
    424   1.1      cgd 
    425   1.1      cgd 	}
    426   1.1      cgd 	*p = '\0';
    427  1.14    lukem 	*flags = 0;
    428   1.6     phil 	if (!pw_scan(line, pw, flags)) {
    429   1.5  mycroft 		warnx("at line #%d", lcnt);
    430   1.5  mycroft fmt:		errno = EFTYPE;	/* XXX */
    431   1.1      cgd 		error(pname);
    432   1.1      cgd 	}
    433   1.5  mycroft 
    434   1.5  mycroft 	return (1);
    435   1.1      cgd }
    436   1.1      cgd 
    437   1.5  mycroft void
    438   1.1      cgd mv(from, to)
    439   1.1      cgd 	char *from, *to;
    440   1.1      cgd {
    441   1.1      cgd 	char buf[MAXPATHLEN];
    442   1.1      cgd 
    443   1.1      cgd 	if (rename(from, to)) {
    444   1.5  mycroft 		int sverrno = errno;
    445   1.5  mycroft 		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
    446   1.1      cgd 		errno = sverrno;
    447   1.1      cgd 		error(buf);
    448   1.1      cgd 	}
    449   1.1      cgd }
    450   1.1      cgd 
    451   1.5  mycroft void
    452  1.11     fair wr_error(name)
    453  1.11     fair 	char *name;
    454  1.11     fair {
    455  1.11     fair 	char	errbuf[BUFSIZ];
    456  1.11     fair 	int	sverrno = errno;
    457  1.11     fair 
    458  1.11     fair 	(void)snprintf(errbuf, sizeof(errbuf),
    459  1.11     fair 		"attempt to write %s failed", name);
    460  1.11     fair 
    461  1.11     fair 	errno = sverrno;
    462  1.11     fair 	error(errbuf);
    463  1.11     fair }
    464  1.11     fair 
    465  1.11     fair void
    466   1.1      cgd error(name)
    467   1.1      cgd 	char *name;
    468   1.1      cgd {
    469   1.5  mycroft 
    470   1.5  mycroft 	warn(name);
    471   1.1      cgd 	cleanup();
    472  1.11     fair #ifdef think_about_this_a_while_longer
    473  1.11     fair 	fputs("NOTE: possible inconsistencies between text files and databases\n", stderr);
    474  1.11     fair 	fputs("re-run pwd_mkdb when you have fixed the problem.\n", stderr);
    475  1.11     fair #endif
    476   1.1      cgd 	exit(1);
    477   1.1      cgd }
    478   1.1      cgd 
    479   1.5  mycroft void
    480  1.11     fair rm(victim)
    481  1.11     fair 	char *victim;
    482  1.11     fair {
    483  1.11     fair 	if (unlink(victim) < 0) {
    484  1.11     fair 		warn("unlink(%s)", victim);
    485  1.11     fair 	}
    486  1.11     fair }
    487  1.11     fair 
    488  1.11     fair void
    489   1.1      cgd cleanup()
    490   1.1      cgd {
    491   1.1      cgd 	switch(clean) {
    492   1.1      cgd 	case FILE_ORIG:
    493  1.11     fair 		rm(oldpwdfile);
    494   1.1      cgd 		/* FALLTHROUGH */
    495   1.1      cgd 	case FILE_SECURE:
    496  1.11     fair 		rm(pwd_Sdb_tmp);
    497   1.1      cgd 		/* FALLTHROUGH */
    498   1.1      cgd 	case FILE_INSECURE:
    499  1.11     fair 		rm(pwd_db_tmp);
    500   1.1      cgd 	}
    501   1.1      cgd }
    502   1.1      cgd 
    503   1.5  mycroft void
    504   1.1      cgd usage()
    505   1.1      cgd {
    506   1.5  mycroft 
    507   1.8    lukem 	(void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
    508   1.1      cgd 	exit(1);
    509   1.1      cgd }
    510