Home | History | Annotate | Line # | Download | only in pwd_mkdb
pwd_mkdb.c revision 1.22
      1  1.22       ad /*	$NetBSD: pwd_mkdb.c,v 1.22 2001/08/18 19:29:32 ad Exp $	*/
      2  1.19       ad 
      3  1.11     fair /*
      4   1.5  mycroft  * Copyright (c) 1991, 1993, 1994
      5   1.5  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.6     phil  * Portions Copyright(C) 1994, Jason Downs.  All rights reserved.
      7   1.1      cgd  *
      8   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1      cgd  * modification, are permitted provided that the following conditions
     10   1.1      cgd  * are met:
     11   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1      cgd  *    must display the following acknowledgement:
     18   1.1      cgd  *	This product includes software developed by the University of
     19   1.1      cgd  *	California, Berkeley and its contributors.
     20   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1      cgd  *    may be used to endorse or promote products derived from this software
     22   1.1      cgd  *    without specific prior written permission.
     23   1.1      cgd  *
     24   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1      cgd  * SUCH DAMAGE.
     35   1.1      cgd  */
     36   1.1      cgd 
     37  1.10    lukem #include <sys/cdefs.h>
     38   1.1      cgd #ifndef lint
     39  1.17  mycroft __COPYRIGHT("@(#) Copyright (c) 2000\n\
     40  1.17  mycroft 	The NetBSD Foundation, Inc.  All rights reserved.\n\
     41  1.17  mycroft Copyright (c) 1991, 1993, 1994\n\
     42  1.10    lukem 	The Regents of the University of California.  All rights reserved.\n");
     43  1.17  mycroft __SCCSID("from: @(#)pwd_mkdb.c	8.5 (Berkeley) 4/20/94");
     44  1.22       ad __RCSID("$NetBSD: pwd_mkdb.c,v 1.22 2001/08/18 19:29:32 ad Exp $");
     45   1.1      cgd #endif /* not lint */
     46   1.1      cgd 
     47   1.1      cgd #include <sys/param.h>
     48   1.1      cgd #include <sys/stat.h>
     49   1.5  mycroft 
     50   1.1      cgd #include <db.h>
     51   1.5  mycroft #include <err.h>
     52   1.1      cgd #include <errno.h>
     53   1.5  mycroft #include <fcntl.h>
     54   1.1      cgd #include <limits.h>
     55   1.5  mycroft #include <pwd.h>
     56   1.5  mycroft #include <signal.h>
     57   1.1      cgd #include <stdio.h>
     58   1.5  mycroft #include <stdlib.h>
     59   1.1      cgd #include <string.h>
     60   1.5  mycroft #include <unistd.h>
     61   1.7      jtc #include <util.h>
     62   1.1      cgd 
     63  1.22       ad #define	MAX_CACHESIZE	8*1024*1024
     64  1.22       ad #define	MIN_CACHESIZE	2*1024*1024
     65  1.22       ad 
     66  1.19       ad #define	PERM_INSECURE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
     67  1.19       ad #define	PERM_SECURE	(S_IRUSR | S_IWUSR)
     68   1.1      cgd 
     69  1.22       ad /* Pull this out of the C library. */
     70   1.6     phil extern const char __yp_token[];
     71   1.6     phil 
     72   1.5  mycroft HASHINFO openinfo = {
     73   1.5  mycroft 	4096,		/* bsize */
     74   1.5  mycroft 	32,		/* ffactor */
     75   1.5  mycroft 	256,		/* nelem */
     76  1.22       ad 	0,		/* cachesize */
     77   1.5  mycroft 	NULL,		/* hash() */
     78   1.5  mycroft 	0		/* lorder */
     79   1.5  mycroft };
     80   1.1      cgd 
     81  1.22       ad #define	FILE_INSECURE	0x01
     82  1.22       ad #define	FILE_SECURE	0x02
     83  1.22       ad #define	FILE_ORIG	0x04
     84  1.22       ad 
     85  1.22       ad static char	*pname;				/* password file name */
     86  1.22       ad static char	prefix[MAXPATHLEN];
     87  1.22       ad static char	oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
     88  1.22       ad static char	pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
     89  1.22       ad static char	pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
     90  1.22       ad static int 	lorder = BYTE_ORDER;
     91  1.22       ad static int	clean;
     92  1.22       ad 
     93  1.22       ad void	bailout(void);
     94  1.22       ad void	cp(const char *, const char *, mode_t);
     95  1.22       ad int	deldbent(DB *, const char *, int, void *);
     96  1.19       ad void	error(const char *);
     97  1.22       ad int	getdbent(DB *, const char *, int, void *, struct passwd **);
     98  1.22       ad void	inconsistancy(void);
     99  1.22       ad void	install(const char *, const char *);
    100  1.19       ad int	main(int, char **);
    101  1.22       ad void	putdbents(DB *, struct passwd *, const char *, int, const char *, int,
    102  1.22       ad 		  int, int);
    103  1.22       ad void	putyptoken(DB *, const char *);
    104  1.19       ad void	rm(const char *);
    105  1.19       ad int	scan(FILE *, struct passwd *, int *, int *);
    106  1.19       ad void	usage(void);
    107  1.22       ad void	wr_error(const char *);
    108   1.5  mycroft 
    109   1.5  mycroft int
    110  1.19       ad main(int argc, char *argv[])
    111   1.1      cgd {
    112  1.22       ad 	int ch, makeold, tfd, lineno, found, rv, hasyp, secureonly;
    113  1.22       ad 	struct passwd pwd, *tpwd;
    114  1.22       ad 	char *username;
    115   1.5  mycroft 	DB *dp, *edp;
    116   1.1      cgd 	FILE *fp, *oldfp;
    117   1.1      cgd 	sigset_t set;
    118  1.22       ad 	int dbflg, uid_dbflg, newuser, olduid, flags;
    119  1.22       ad 	char buf[MAXPATHLEN];
    120  1.22       ad 	struct stat st;
    121  1.22       ad 	u_int cachesize;
    122   1.1      cgd 
    123  1.21     tron 	prefix[0] = '\0';
    124   1.1      cgd 	makeold = 0;
    125  1.22       ad 	oldfp = NULL;
    126  1.22       ad 	username = NULL;
    127  1.22       ad 	hasyp = 0;
    128  1.22       ad 	secureonly = 0;
    129  1.22       ad 
    130  1.22       ad 	while ((ch = getopt(argc, argv, "BLd:psu:v")) != -1)
    131  1.22       ad 		switch (ch) {
    132  1.22       ad 		case 'B':			/* big-endian output */
    133  1.22       ad 			lorder = BIG_ENDIAN;
    134  1.22       ad 			break;
    135  1.22       ad 		case 'L':			/* little-endian output */
    136  1.22       ad 			lorder = LITTLE_ENDIAN;
    137  1.22       ad 			break;
    138  1.19       ad 		case 'd':			/* set prefix */
    139   1.8    lukem 			strncpy(prefix, optarg, sizeof(prefix));
    140   1.8    lukem 			prefix[sizeof(prefix)-1] = '\0';
    141   1.8    lukem 			break;
    142   1.1      cgd 		case 'p':			/* create V7 "file.orig" */
    143   1.1      cgd 			makeold = 1;
    144   1.1      cgd 			break;
    145  1.22       ad 		case 's':			/* modify secure db only */
    146  1.22       ad 			secureonly = 1;
    147   1.1      cgd 			break;
    148  1.22       ad 		case 'u':			/* modify one user only */
    149  1.22       ad 			username = optarg;
    150  1.17  mycroft 			break;
    151  1.22       ad 		case 'v':			/* backward compatible */
    152  1.17  mycroft 			break;
    153   1.1      cgd 		case '?':
    154   1.1      cgd 		default:
    155   1.1      cgd 			usage();
    156   1.1      cgd 		}
    157   1.1      cgd 	argc -= optind;
    158   1.1      cgd 	argv += optind;
    159   1.1      cgd 
    160   1.1      cgd 	if (argc != 1)
    161   1.1      cgd 		usage();
    162  1.22       ad 	if (username != NULL)
    163  1.22       ad 		if (username[0] == '+' || username[0] == '-')
    164  1.22       ad 			usage();
    165  1.22       ad 	if (secureonly)
    166  1.22       ad 		makeold = 0;
    167   1.4      cgd 
    168   1.1      cgd 	/*
    169   1.5  mycroft 	 * This could be changed to allow the user to interrupt.
    170   1.5  mycroft 	 * Probably not worth the effort.
    171   1.1      cgd 	 */
    172   1.1      cgd 	sigemptyset(&set);
    173   1.1      cgd 	sigaddset(&set, SIGTSTP);
    174   1.1      cgd 	sigaddset(&set, SIGHUP);
    175   1.1      cgd 	sigaddset(&set, SIGINT);
    176   1.1      cgd 	sigaddset(&set, SIGQUIT);
    177   1.1      cgd 	sigaddset(&set, SIGTERM);
    178   1.1      cgd 	(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
    179   1.1      cgd 
    180   1.5  mycroft 	/* We don't care what the user wants. */
    181   1.5  mycroft 	(void)umask(0);
    182   1.5  mycroft 
    183  1.22       ad 	if (username == NULL)
    184  1.22       ad 		flags = O_RDWR | O_CREAT | O_EXCL;
    185  1.22       ad 	else
    186  1.22       ad 		flags = O_RDWR;
    187  1.22       ad 
    188   1.1      cgd 	pname = *argv;
    189   1.1      cgd 	/* Open the original password file */
    190  1.19       ad 	if ((fp = fopen(pname, "r")) == NULL)
    191   1.1      cgd 		error(pname);
    192   1.1      cgd 
    193  1.17  mycroft 	openinfo.lorder = lorder;
    194  1.17  mycroft 
    195  1.22       ad 	if (fstat(fileno(fp), &st) == -1)
    196  1.22       ad 		error(pname);
    197  1.22       ad 
    198  1.22       ad 	/* Tweak openinfo values for large passwd files. */
    199  1.22       ad 	cachesize = st.st_size * 20;
    200  1.22       ad 	if (cachesize > MAX_CACHESIZE)
    201  1.22       ad 		cachesize = MAX_CACHESIZE;
    202  1.22       ad 	else if (cachesize < MIN_CACHESIZE)
    203  1.22       ad 		cachesize = MIN_CACHESIZE;
    204  1.22       ad 	openinfo.cachesize = cachesize;
    205  1.22       ad 
    206   1.1      cgd 	/* Open the temporary insecure password database. */
    207  1.22       ad 	if (!secureonly) {
    208  1.22       ad 		(void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp",
    209  1.22       ad 		    prefix, _PATH_MP_DB);
    210  1.22       ad 		if (username != NULL) {
    211  1.22       ad 			snprintf(buf, sizeof(buf), "%s" _PATH_MP_DB, prefix);
    212  1.22       ad 			cp(buf, pwd_db_tmp, PERM_INSECURE);
    213  1.22       ad 		}
    214  1.22       ad 		dp = dbopen(pwd_db_tmp, flags, PERM_INSECURE, DB_HASH,
    215  1.22       ad 		    &openinfo);
    216  1.22       ad 		if (dp == NULL)
    217  1.22       ad 			error(pwd_db_tmp);
    218  1.22       ad 		clean |= FILE_INSECURE;
    219  1.22       ad 	}
    220  1.22       ad 
    221  1.22       ad 	/* Open the temporary encrypted password database. */
    222  1.22       ad 	(void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
    223  1.22       ad 		_PATH_SMP_DB);
    224  1.22       ad 	if (username != NULL) {
    225  1.22       ad 		snprintf(buf, sizeof(buf), "%s" _PATH_SMP_DB, prefix);
    226  1.22       ad 		cp(buf, pwd_Sdb_tmp, PERM_SECURE);
    227  1.22       ad 	}
    228  1.22       ad 	edp = dbopen(pwd_Sdb_tmp, flags, PERM_SECURE, DB_HASH, &openinfo);
    229  1.22       ad 	if (!edp)
    230  1.22       ad 		error(pwd_Sdb_tmp);
    231  1.22       ad 	clean |= FILE_SECURE;
    232   1.1      cgd 
    233   1.1      cgd 	/*
    234   1.1      cgd 	 * Open file for old password file.  Minor trickiness -- don't want to
    235   1.1      cgd 	 * chance the file already existing, since someone (stupidly) might
    236   1.1      cgd 	 * still be using this for permission checking.  So, open it first and
    237   1.5  mycroft 	 * fdopen the resulting fd.  The resulting file should be readable by
    238   1.5  mycroft 	 * everyone.
    239   1.1      cgd 	 */
    240   1.1      cgd 	if (makeold) {
    241  1.11     fair 		(void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
    242  1.19       ad 		    pname);
    243  1.19       ad 		if ((tfd = open(oldpwdfile, O_WRONLY | O_CREAT | O_EXCL,
    244  1.19       ad 		    PERM_INSECURE)) < 0)
    245  1.11     fair 			error(oldpwdfile);
    246  1.22       ad 		clean |= FILE_ORIG;
    247   1.5  mycroft 		if ((oldfp = fdopen(tfd, "w")) == NULL)
    248  1.11     fair 			error(oldpwdfile);
    249  1.22       ad 	}
    250  1.22       ad 
    251  1.22       ad 	if (username != NULL) {
    252  1.22       ad 		uid_dbflg = 0;
    253  1.22       ad 		dbflg = 0;
    254  1.22       ad 		found = 0;
    255  1.22       ad 
    256  1.22       ad 		/*
    257  1.22       ad 		 * Determine if this is a new entry.
    258  1.22       ad 		 */
    259  1.22       ad 		if (getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYNAME, username, &tpwd))
    260  1.22       ad 			newuser = 1;
    261  1.22       ad 		else {
    262  1.22       ad 			newuser = 0;
    263  1.22       ad 			olduid = tpwd->pw_uid;
    264  1.22       ad 		}
    265  1.22       ad 
    266  1.22       ad 	} else {
    267  1.22       ad 		uid_dbflg = R_NOOVERWRITE;
    268  1.22       ad 		dbflg = R_NOOVERWRITE;
    269   1.1      cgd 	}
    270   1.1      cgd 
    271   1.1      cgd 	/*
    272   1.6     phil 	 * If we see something go by that looks like YP, we save a special
    273   1.6     phil 	 * pointer record, which if YP is enabled in the C lib, will speed
    274   1.6     phil 	 * things up.
    275   1.1      cgd 	 */
    276  1.19       ad 	for (lineno = 0; scan(fp, &pwd, &flags, &lineno);) {
    277   1.9  thorpej 		/*
    278  1.22       ad 		 * Create original format password file entry.
    279   1.9  thorpej 		 */
    280  1.11     fair 		if (makeold) {
    281   1.5  mycroft 			(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
    282   1.5  mycroft 			    pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
    283   1.5  mycroft 			    pwd.pw_dir, pwd.pw_shell);
    284  1.19       ad 			if (ferror(oldfp))
    285  1.11     fair 				wr_error(oldpwdfile);
    286  1.11     fair 		}
    287  1.22       ad 
    288  1.22       ad 		if (username == NULL) {
    289  1.22       ad 			/* Look like YP? */
    290  1.22       ad 			if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')
    291  1.22       ad 				hasyp++;
    292  1.22       ad 
    293  1.22       ad 			/* Warn about potentially unsafe uid/gid overrides. */
    294  1.22       ad 			if (pwd.pw_name[0] == '+') {
    295  1.22       ad 				if ((flags & _PASSWORD_NOUID) == 0 &&
    296  1.22       ad 				    pwd.pw_uid == 0)
    297  1.22       ad 					warnx("line %d: superuser override "
    298  1.22       ad 					    "in YP inclusion", lineno);
    299  1.22       ad 				if ((flags & _PASSWORD_NOGID) == 0 &&
    300  1.22       ad 				    pwd.pw_gid == 0)
    301  1.22       ad 					warnx("line %d: wheel override "
    302  1.22       ad 					    "in YP inclusion", lineno);
    303  1.22       ad 			}
    304  1.22       ad 
    305  1.22       ad 			/* Write the database entry out. */
    306  1.22       ad 			if (!secureonly)
    307  1.22       ad 				putdbents(dp, &pwd, "*", flags, pwd_db_tmp,
    308  1.22       ad 				    lineno, dbflg, uid_dbflg);
    309  1.22       ad 			continue;
    310  1.22       ad 		} else if (strcmp(username, pwd.pw_name) != 0)
    311  1.22       ad 			continue;
    312  1.22       ad 
    313  1.22       ad 		if (found) {
    314  1.22       ad 			warnx("user `%s' listed twice in password file",
    315  1.22       ad 			    username);
    316  1.22       ad 			bailout();
    317  1.22       ad 		}
    318  1.22       ad 
    319  1.22       ad 		/*
    320  1.22       ad 		 * Ensure that the text file and database agree on
    321  1.22       ad 		 * which line the record is from.
    322  1.22       ad 		 */
    323  1.22       ad 		rv = getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYNUM, &lineno, &tpwd);
    324  1.22       ad 		if (newuser) {
    325  1.22       ad 			if (rv == 0)
    326  1.22       ad 				inconsistancy();
    327  1.22       ad 		} else if (rv == -1 ||
    328  1.22       ad 			strcmp(username, tpwd->pw_name) != 0)
    329  1.22       ad 			inconsistancy();
    330  1.22       ad 		else if (olduid != pwd.pw_uid) {
    331  1.22       ad 			/*
    332  1.22       ad 			 * If we're changing UID, remove the BYUID
    333  1.22       ad 			 * record for the old UID only if it has the
    334  1.22       ad 			 * same username.
    335  1.22       ad 			 */
    336  1.22       ad 			if (!getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYUID, &olduid,
    337  1.22       ad 			    &tpwd)) {
    338  1.22       ad 				if (strcmp(username, tpwd->pw_name) == 0) {
    339  1.22       ad 					if (!secureonly)
    340  1.22       ad 						deldbent(dp, pwd_db_tmp,
    341  1.22       ad 						    _PW_KEYBYUID, &olduid);
    342  1.22       ad 					deldbent(edp, pwd_Sdb_tmp,
    343  1.22       ad 					    _PW_KEYBYUID, &olduid);
    344  1.22       ad 				}
    345  1.22       ad 			} else
    346  1.22       ad 				inconsistancy();
    347  1.22       ad 		}
    348  1.22       ad 
    349  1.22       ad 		/*
    350  1.22       ad 		 * If there's an existing BYUID record for the new UID and
    351  1.22       ad 		 * the username doesn't match then be sure not to overwrite
    352  1.22       ad 		 * it.
    353  1.22       ad 		 */
    354  1.22       ad 		if (!getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYUID, &pwd.pw_uid,
    355  1.22       ad 		    &tpwd))
    356  1.22       ad 			if (strcmp(username, tpwd->pw_name) != 0)
    357  1.22       ad 				uid_dbflg = R_NOOVERWRITE;
    358  1.22       ad 
    359  1.22       ad 		/* Write the database entries out */
    360  1.22       ad 		if (!secureonly)
    361  1.22       ad 			putdbents(dp, &pwd, "*", flags, pwd_db_tmp, lineno,
    362  1.22       ad 			    dbflg, uid_dbflg);
    363  1.22       ad 		putdbents(edp, &pwd, pwd.pw_passwd, flags, pwd_Sdb_tmp,
    364  1.22       ad 		    lineno, dbflg, uid_dbflg);
    365  1.22       ad 
    366  1.22       ad 		found = 1;
    367  1.22       ad 		if (!makeold)
    368  1.22       ad 			break;
    369  1.22       ad 	}
    370  1.22       ad 
    371  1.22       ad 	if (!secureonly) {
    372  1.22       ad 		/* Store YP token if needed. */
    373  1.22       ad 		if (hasyp)
    374  1.22       ad 			putyptoken(dp, pwd_db_tmp);
    375  1.22       ad 
    376  1.22       ad 		/* Close the insecure database. */
    377  1.22       ad 		if ((*dp->close)(dp) < 0)
    378  1.22       ad 			wr_error(pwd_db_tmp);
    379  1.22       ad 	}
    380  1.22       ad 
    381  1.22       ad 	/*
    382  1.22       ad 	 * If rebuilding the databases, we re-parse the text file and write
    383  1.22       ad 	 * the secure entries out in a separate pass.
    384  1.22       ad 	 */
    385  1.22       ad 	if (username == NULL) {
    386  1.22       ad 		rewind(fp);
    387  1.22       ad 		for (lineno = 0; scan(fp, &pwd, &flags, &lineno);)
    388  1.22       ad 			putdbents(edp, &pwd, pwd.pw_passwd, flags, pwd_Sdb_tmp,
    389  1.22       ad 			    lineno, dbflg, uid_dbflg);
    390  1.22       ad 
    391  1.22       ad 		/* Store YP token if needed. */
    392  1.22       ad 		if (hasyp)
    393  1.22       ad 			putyptoken(edp, pwd_Sdb_tmp);
    394  1.22       ad 	} else if (!found) {
    395  1.22       ad 		warnx("user `%s' not found in password file", username);
    396  1.22       ad 		bailout();
    397   1.5  mycroft 	}
    398   1.6     phil 
    399  1.22       ad 	/* Close the secure database. */
    400  1.22       ad 	if ((*edp->close)(edp) < 0)
    401  1.22       ad 		wr_error(pwd_Sdb_tmp);
    402  1.22       ad 
    403  1.22       ad 	/* Install as the real password files. */
    404  1.22       ad 	if (!secureonly)
    405  1.22       ad 		install(pwd_db_tmp, _PATH_MP_DB);
    406  1.22       ad 	install(pwd_Sdb_tmp, _PATH_SMP_DB);
    407   1.6     phil 
    408  1.22       ad 	/* Install the V7 password file. */
    409   1.5  mycroft 	if (makeold) {
    410  1.19       ad 		if (fflush(oldfp) == EOF)
    411  1.11     fair 			wr_error(oldpwdfile);
    412  1.19       ad 		if (fclose(oldfp) == EOF)
    413  1.11     fair 			wr_error(oldpwdfile);
    414  1.22       ad 		install(oldpwdfile, _PATH_PASSWD);
    415   1.5  mycroft 	}
    416   1.5  mycroft 
    417   1.1      cgd 	/* Set master.passwd permissions, in case caller forgot. */
    418   1.1      cgd 	(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
    419  1.19       ad 	if (fclose(fp) == EOF)
    420  1.11     fair 		wr_error(pname);
    421   1.1      cgd 
    422  1.19       ad 	/*
    423  1.22       ad 	 * Move the temporary master password file LAST -- chpass(1),
    424  1.22       ad 	 * passwd(1), vipw(8) and friends all use its existance to block
    425  1.22       ad 	 * other incarnations of themselves.  The rename means that
    426  1.22       ad 	 * everything is unlocked, as the original file can no longer be
    427  1.22       ad 	 * accessed.
    428  1.19       ad 	 */
    429  1.19       ad 	install(pname, _PATH_MASTERPASSWD);
    430  1.19       ad 	exit(EXIT_SUCCESS);
    431  1.19       ad 	/* NOTREACHED */
    432   1.1      cgd }
    433   1.1      cgd 
    434   1.5  mycroft int
    435  1.19       ad scan(FILE *fp, struct passwd *pw, int *flags, int *lineno)
    436   1.1      cgd {
    437   1.1      cgd 	static char line[LINE_MAX];
    438   1.1      cgd 	char *p;
    439  1.17  mycroft 	int oflags;
    440   1.1      cgd 
    441  1.19       ad 	if (fgets(line, sizeof(line), fp) == NULL)
    442   1.5  mycroft 		return (0);
    443  1.19       ad 	(*lineno)++;
    444  1.19       ad 
    445   1.1      cgd 	/*
    446   1.1      cgd 	 * ``... if I swallow anything evil, put your fingers down my
    447   1.1      cgd 	 * throat...''
    448   1.1      cgd 	 *	-- The Who
    449   1.1      cgd 	 */
    450  1.19       ad 	if ((p = strchr(line, '\n')) == NULL) {
    451   1.5  mycroft 		warnx("line too long");
    452  1.19       ad 		errno = EFTYPE;	/* XXX */
    453  1.19       ad 		error(pname);
    454   1.1      cgd 	}
    455   1.1      cgd 	*p = '\0';
    456  1.15    lukem 	if (strcmp(line, "+") == 0)
    457  1.15    lukem 		strcpy(line, "+:::::::::");	/* pw_scan() can't handle "+" */
    458  1.17  mycroft 	oflags = 0;
    459  1.17  mycroft 	if (!pw_scan(line, pw, &oflags)) {
    460  1.19       ad 		warnx("at line #%d", *lineno);
    461  1.19       ad 		errno = EFTYPE;	/* XXX */
    462   1.1      cgd 		error(pname);
    463   1.1      cgd 	}
    464  1.17  mycroft 	*flags = oflags;
    465   1.5  mycroft 
    466   1.5  mycroft 	return (1);
    467   1.1      cgd }
    468   1.1      cgd 
    469   1.5  mycroft void
    470  1.19       ad install(const char *from, const char *to)
    471   1.1      cgd {
    472   1.1      cgd 	char buf[MAXPATHLEN];
    473  1.19       ad 	int sverrno;
    474   1.1      cgd 
    475  1.19       ad 	snprintf(buf, sizeof(buf), "%s%s", prefix, to);
    476  1.19       ad 	if (rename(from, buf)) {
    477  1.19       ad 		sverrno = errno;
    478  1.19       ad 		(void)snprintf(buf, sizeof(buf), "%s to %s", from, buf);
    479   1.1      cgd 		errno = sverrno;
    480   1.1      cgd 		error(buf);
    481   1.1      cgd 	}
    482   1.1      cgd }
    483   1.1      cgd 
    484   1.5  mycroft void
    485  1.22       ad rm(const char *victim)
    486  1.22       ad {
    487  1.22       ad 
    488  1.22       ad 	if (unlink(victim) < 0)
    489  1.22       ad 		warn("unlink(%s)", victim);
    490  1.22       ad }
    491  1.22       ad 
    492  1.22       ad void
    493  1.22       ad cp(const char *from, const char *to, mode_t mode)
    494  1.22       ad {
    495  1.22       ad 	static char buf[MAXBSIZE];
    496  1.22       ad 	int from_fd, rcount, to_fd, wcount, sverrno;
    497  1.22       ad 
    498  1.22       ad 	if ((from_fd = open(from, O_RDONLY, 0)) < 0)
    499  1.22       ad 		error(from);
    500  1.22       ad 	if ((to_fd = open(to, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0)
    501  1.22       ad 		error(to);
    502  1.22       ad 	while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
    503  1.22       ad 		wcount = write(to_fd, buf, rcount);
    504  1.22       ad 		if (rcount != wcount || wcount == -1) {
    505  1.22       ad 			sverrno = errno;
    506  1.22       ad 			(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
    507  1.22       ad 			errno = sverrno;
    508  1.22       ad 			error(buf);
    509  1.22       ad 		}
    510  1.22       ad 	}
    511  1.22       ad 
    512  1.22       ad 	if (rcount < 0) {
    513  1.22       ad 		sverrno = errno;
    514  1.22       ad 		(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
    515  1.22       ad 		errno = sverrno;
    516  1.22       ad 		error(buf);
    517  1.22       ad 	}
    518  1.22       ad }
    519  1.22       ad 
    520  1.22       ad void
    521  1.19       ad wr_error(const char *str)
    522  1.11     fair {
    523  1.19       ad 	char errbuf[BUFSIZ];
    524  1.19       ad 	int sverrno;
    525  1.19       ad 
    526  1.19       ad 	sverrno = errno;
    527  1.11     fair 
    528  1.11     fair 	(void)snprintf(errbuf, sizeof(errbuf),
    529  1.19       ad 		"attempt to write %s failed", str);
    530  1.11     fair 
    531  1.11     fair 	errno = sverrno;
    532  1.11     fair 	error(errbuf);
    533  1.11     fair }
    534  1.11     fair 
    535  1.11     fair void
    536  1.19       ad error(const char *str)
    537   1.1      cgd {
    538   1.5  mycroft 
    539  1.19       ad 	warn("%s", str);
    540  1.22       ad 	bailout();
    541   1.1      cgd }
    542   1.1      cgd 
    543   1.5  mycroft void
    544  1.22       ad inconsistancy(void)
    545  1.11     fair {
    546  1.19       ad 
    547  1.22       ad 	warnx("text files and databases are inconsistent");
    548  1.22       ad 	warnx("re-build the databases without -u");
    549  1.22       ad 	bailout();
    550  1.11     fair }
    551  1.11     fair 
    552  1.11     fair void
    553  1.22       ad bailout(void)
    554   1.1      cgd {
    555  1.19       ad 
    556  1.22       ad 	if ((clean & FILE_ORIG) != 0)
    557  1.11     fair 		rm(oldpwdfile);
    558  1.22       ad 	if ((clean & FILE_SECURE) != 0)
    559  1.11     fair 		rm(pwd_Sdb_tmp);
    560  1.22       ad 	if ((clean & FILE_INSECURE) != 0)
    561  1.11     fair 		rm(pwd_db_tmp);
    562  1.22       ad 
    563  1.22       ad 	exit(EXIT_FAILURE);
    564   1.1      cgd }
    565   1.1      cgd 
    566  1.19       ad /*
    567  1.19       ad  * Write entries to a database for a single user.
    568  1.19       ad  *
    569  1.19       ad  * The databases actually contain three copies of the original data.  Each
    570  1.19       ad  * password file entry is converted into a rough approximation of a ``struct
    571  1.19       ad  * passwd'', with the strings placed inline.  This object is then stored as
    572  1.19       ad  * the data for three separate keys.  The first key * is the pw_name field
    573  1.19       ad  * prepended by the _PW_KEYBYNAME character.  The second key is the pw_uid
    574  1.19       ad  * field prepended by the _PW_KEYBYUID character.  The third key is the line
    575  1.19       ad  * number in the original file prepended by the _PW_KEYBYNUM character.
    576  1.19       ad  * (The special characters are prepended to ensure that the keys do not
    577  1.19       ad  * collide.)
    578  1.19       ad  */
    579  1.19       ad #define	COMPACT(e)	for (t = e; (*p++ = *t++) != '\0';)
    580  1.19       ad 
    581  1.19       ad void
    582  1.22       ad putdbents(DB *dp, struct passwd *pw, const char *passwd, int flags,
    583  1.22       ad 	  const char *fn, int lineno, int dbflg, int uid_dbflg)
    584  1.19       ad {
    585  1.19       ad 	struct passwd pwd;
    586  1.19       ad 	char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024], *p;
    587  1.19       ad 	DBT data, key;
    588  1.19       ad 	const char *t;
    589  1.19       ad 	u_int32_t x;
    590  1.19       ad 	int len;
    591  1.19       ad 
    592  1.19       ad 	memcpy(&pwd, pw, sizeof(pwd));
    593  1.19       ad 	data.data = (u_char *)buf;
    594  1.19       ad 	key.data = (u_char *)tbuf;
    595  1.19       ad 
    596  1.19       ad 	if (lorder != BYTE_ORDER) {
    597  1.19       ad 		M_32_SWAP(pwd.pw_uid);
    598  1.19       ad 		M_32_SWAP(pwd.pw_gid);
    599  1.19       ad 		M_32_SWAP(pwd.pw_change);
    600  1.19       ad 		M_32_SWAP(pwd.pw_expire);
    601  1.19       ad 	}
    602  1.19       ad 
    603  1.19       ad 	/* Create insecure data. */
    604  1.19       ad 	p = buf;
    605  1.19       ad 	COMPACT(pwd.pw_name);
    606  1.19       ad 	COMPACT(passwd);
    607  1.19       ad 	memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
    608  1.19       ad 	p += sizeof(pwd.pw_uid);
    609  1.19       ad 	memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
    610  1.19       ad 	p += sizeof(pwd.pw_gid);
    611  1.19       ad 	memmove(p, &pwd.pw_change, sizeof(pwd.pw_change));
    612  1.19       ad 	p += sizeof(pwd.pw_change);
    613  1.19       ad 	COMPACT(pwd.pw_class);
    614  1.19       ad 	COMPACT(pwd.pw_gecos);
    615  1.19       ad 	COMPACT(pwd.pw_dir);
    616  1.19       ad 	COMPACT(pwd.pw_shell);
    617  1.19       ad 	memmove(p, &pwd.pw_expire, sizeof(pwd.pw_expire));
    618  1.19       ad 	p += sizeof(pwd.pw_expire);
    619  1.19       ad 	x = flags;
    620  1.19       ad 	if (lorder != BYTE_ORDER)
    621  1.19       ad 		M_32_SWAP(x);
    622  1.19       ad 	memmove(p, &x, sizeof(x));
    623  1.19       ad 	p += sizeof(flags);
    624  1.19       ad 	data.size = p - buf;
    625  1.19       ad 
    626  1.19       ad 	/* Store insecure by name. */
    627  1.19       ad 	tbuf[0] = _PW_KEYBYNAME;
    628  1.19       ad 	len = strlen(pwd.pw_name);
    629  1.19       ad 	memmove(tbuf + 1, pwd.pw_name, len);
    630  1.19       ad 	key.size = len + 1;
    631  1.22       ad 	if ((*dp->put)(dp, &key, &data, dbflg) == -1)
    632  1.19       ad 		wr_error(fn);
    633  1.22       ad 
    634  1.19       ad 	/* Store insecure by number. */
    635  1.19       ad 	tbuf[0] = _PW_KEYBYNUM;
    636  1.19       ad 	x = lineno;
    637  1.19       ad 	if (lorder != BYTE_ORDER)
    638  1.19       ad 		M_32_SWAP(x);
    639  1.19       ad 	memmove(tbuf + 1, &x, sizeof(x));
    640  1.19       ad 	key.size = sizeof(x) + 1;
    641  1.22       ad 	if ((*dp->put)(dp, &key, &data, dbflg) == -1)
    642  1.19       ad 		wr_error(fn);
    643  1.19       ad 
    644  1.19       ad 	/* Store insecure by uid. */
    645  1.19       ad 	tbuf[0] = _PW_KEYBYUID;
    646  1.19       ad 	memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
    647  1.19       ad 	key.size = sizeof(pwd.pw_uid) + 1;
    648  1.22       ad 	if ((*dp->put)(dp, &key, &data, uid_dbflg) == -1)
    649  1.22       ad 		wr_error(fn);
    650  1.22       ad }
    651  1.22       ad 
    652  1.22       ad int
    653  1.22       ad deldbent(DB *dp, const char *fn, int type, void *keyp)
    654  1.22       ad {
    655  1.22       ad 	char tbuf[1024];
    656  1.22       ad 	DBT key;
    657  1.22       ad 	u_int32_t x;
    658  1.22       ad 	int len, rv;
    659  1.22       ad 
    660  1.22       ad 	key.data = (u_char *)tbuf;
    661  1.22       ad 
    662  1.22       ad 	switch (tbuf[0] = type) {
    663  1.22       ad 	case _PW_KEYBYNAME:
    664  1.22       ad 		len = strlen((char *)keyp);
    665  1.22       ad 		memcpy(tbuf + 1, keyp, len);
    666  1.22       ad 		key.size = len + 1;
    667  1.22       ad 		break;
    668  1.22       ad 
    669  1.22       ad 	case _PW_KEYBYNUM:
    670  1.22       ad 	case _PW_KEYBYUID:
    671  1.22       ad 		x = *(int *)keyp;
    672  1.22       ad 		if (lorder != BYTE_ORDER)
    673  1.22       ad 			M_32_SWAP(x);
    674  1.22       ad 		memmove(tbuf + 1, &x, sizeof(x));
    675  1.22       ad 		key.size = sizeof(x) + 1;
    676  1.22       ad 		break;
    677  1.22       ad 	}
    678  1.22       ad 
    679  1.22       ad 	if ((rv = (*dp->del)(dp, &key, 0)) == -1)
    680  1.19       ad 		wr_error(fn);
    681  1.22       ad 	return (rv);
    682  1.22       ad }
    683  1.22       ad 
    684  1.22       ad int
    685  1.22       ad getdbent(DB *dp, const char *fn, int type, void *keyp, struct passwd **tpwd)
    686  1.22       ad {
    687  1.22       ad 	static char buf[MAX(MAXPATHLEN, LINE_MAX * 2)];
    688  1.22       ad 	static struct passwd pwd;
    689  1.22       ad 	char tbuf[1024], *p;
    690  1.22       ad 	DBT key, data;
    691  1.22       ad 	u_int32_t x;
    692  1.22       ad 	int len, rv;
    693  1.22       ad 
    694  1.22       ad 	data.data = (u_char *)buf;
    695  1.22       ad 	data.size = sizeof(buf);
    696  1.22       ad 	key.data = (u_char *)tbuf;
    697  1.22       ad 
    698  1.22       ad 	switch (tbuf[0] = type) {
    699  1.22       ad 	case _PW_KEYBYNAME:
    700  1.22       ad 		len = strlen((char *)keyp);
    701  1.22       ad 		memcpy(tbuf + 1, keyp, len);
    702  1.22       ad 		key.size = len + 1;
    703  1.22       ad 		break;
    704  1.22       ad 
    705  1.22       ad 	case _PW_KEYBYNUM:
    706  1.22       ad 	case _PW_KEYBYUID:
    707  1.22       ad 		x = *(int *)keyp;
    708  1.22       ad 		if (lorder != BYTE_ORDER)
    709  1.22       ad 			M_32_SWAP(x);
    710  1.22       ad 		memmove(tbuf + 1, &x, sizeof(x));
    711  1.22       ad 		key.size = sizeof(x) + 1;
    712  1.22       ad 		break;
    713  1.22       ad 	}
    714  1.22       ad 
    715  1.22       ad 	if ((rv = (*dp->get)(dp, &key, &data, 0)) == 1)
    716  1.22       ad 		return (rv);
    717  1.22       ad 	if (rv == -1)
    718  1.22       ad 		error(pwd_Sdb_tmp);
    719  1.22       ad 
    720  1.22       ad 	p = (char *)data.data;
    721  1.22       ad 
    722  1.22       ad 	pwd.pw_name = p;
    723  1.22       ad 	while (*p++ != '\0')
    724  1.22       ad 		;
    725  1.22       ad 	pwd.pw_passwd = p;
    726  1.22       ad 	while (*p++ != '\0')
    727  1.22       ad 		;
    728  1.22       ad 
    729  1.22       ad 	memcpy(&pwd.pw_uid, p, sizeof(pwd.pw_uid));
    730  1.22       ad 	p += sizeof(pwd.pw_uid);
    731  1.22       ad 	memcpy(&pwd.pw_gid, p, sizeof(pwd.pw_gid));
    732  1.22       ad 	p += sizeof(pwd.pw_gid);
    733  1.22       ad 	memcpy(&pwd.pw_change, p, sizeof(pwd.pw_change));
    734  1.22       ad 	p += sizeof(pwd.pw_change);
    735  1.22       ad 
    736  1.22       ad 	pwd.pw_class = p;
    737  1.22       ad 	while (*p++ != '\0')
    738  1.22       ad 		;
    739  1.22       ad 	pwd.pw_gecos = p;
    740  1.22       ad 	while (*p++ != '\0')
    741  1.22       ad 		;
    742  1.22       ad 	pwd.pw_dir = p;
    743  1.22       ad 	while (*p++ != '\0')
    744  1.22       ad 		;
    745  1.22       ad 	pwd.pw_shell = p;
    746  1.22       ad 	while (*p++ != '\0')
    747  1.22       ad 		;
    748  1.22       ad 
    749  1.22       ad 	memcpy(&pwd.pw_expire, p, sizeof(pwd.pw_expire));
    750  1.22       ad 	p += sizeof(pwd.pw_expire);
    751  1.22       ad 
    752  1.22       ad 	if (lorder != BYTE_ORDER) {
    753  1.22       ad 		M_32_SWAP(pwd.pw_uid);
    754  1.22       ad 		M_32_SWAP(pwd.pw_gid);
    755  1.22       ad 		M_32_SWAP(pwd.pw_change);
    756  1.22       ad 		M_32_SWAP(pwd.pw_expire);
    757  1.22       ad 	}
    758  1.22       ad 
    759  1.22       ad 	*tpwd = &pwd;
    760  1.22       ad 	return (0);
    761  1.19       ad }
    762  1.19       ad 
    763  1.19       ad void
    764  1.19       ad putyptoken(DB *dp, const char *fn)
    765  1.19       ad {
    766  1.19       ad 	DBT data, key;
    767  1.19       ad 
    768  1.19       ad 	key.data = (u_char *)__yp_token;
    769  1.19       ad 	key.size = strlen(__yp_token);
    770  1.19       ad 	data.data = (u_char *)NULL;
    771  1.19       ad 	data.size = 0;
    772  1.19       ad 
    773  1.22       ad 	if ((*dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
    774  1.19       ad 		wr_error(fn);
    775  1.19       ad }
    776  1.19       ad 
    777   1.5  mycroft void
    778  1.19       ad usage(void)
    779   1.1      cgd {
    780   1.5  mycroft 
    781  1.22       ad 	(void)fprintf(stderr,
    782  1.22       ad 	    "usage: pwd_mkdb [-BLps] [-d directory] [-u user] file\n");
    783  1.19       ad 	exit(EXIT_FAILURE);
    784   1.1      cgd }
    785