/*
* Copyright (c) 2000, 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Portions Copyright(C) 1994, Jason Downs. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 2000, 2009\
The NetBSD Foundation, Inc. All rights reserved.\
Copyright (c) 1991, 1993, 1994\
The Regents of the University of California. All rights reserved.");
__RCSID("$NetBSD: pwd_mkdb.c,v 1.61 2024/10/04 20:32:20 christos Exp $");
#endif /* not lint */
if (warning && db->rversion == 0 && db->wversion == 0) {
mkpw_warning("Database %s is a version %u database.",
db->fname, db->rversion);
mkpw_warning("Use %s -V 1 to upgrade once you've recompiled "
"all your binaries.", getprogname());
}
if (db->wversion != db->rversion) {
if (username != NULL) {
mkpw_warning("You cannot change a single "
"record from version %u to version %u\n",
db->rversion, db->wversion);
bailout();
} else if (verbose) {
mkpw_warning("Changing %s from version %u to version %u",
db->fname, db->rversion, db->wversion);
}
} else {
if (verbose)
mkpw_warning("File `%s' version %u requested %u",
db->fname, db->rversion, db->wversion);
}
setversion(db);
}
int
main(int argc, char *argv[])
{
int ch, makeold, tfd, lineno, found, rv, hasyp, secureonly;
struct passwd pwd, *tpwd;
char *username;
FILE *fp, *oldfp;
sigset_t set;
u_int dbflg, uid_dbflg;
int newuser, olduid, flags;
struct stat st;
u_int cachesize;
uint32_t req_version;
while ((ch = getopt(argc, argv, "BLc:d:lpsu:V:vw")) != -1)
switch (ch) {
case 'B': /* big-endian output */
lorder = BIG_ENDIAN;
break;
case 'L': /* little-endian output */
lorder = LITTLE_ENDIAN;
break;
case 'c':
cachesize = atoi(optarg) * 1024 * 1024;
break;
case 'd': /* set prefix */
(void)strlcpy(prefix, optarg, sizeof(prefix));
break;
case 'l':
openlog(getprogname(), LOG_PID, LOG_AUTH);
logsyslog = 1;
break;
case 'p': /* create V7 "file.orig" */
makeold = 1;
break;
case 's': /* modify secure db only */
secureonly = 1;
break;
case 'u': /* modify one user only */
username = optarg;
break;
case 'V':
req_version = (uint32_t)atoi(optarg);
if (req_version > 1) {
mkpw_warning("Unknown version %u", req_version);
return EXIT_FAILURE;
}
break;
case 'v':
verbose++;
break;
case 'w':
warning++;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
if (username != NULL)
if (username[0] == '+' || username[0] == '-')
usage();
if (secureonly)
makeold = 0;
/*
* This could be changed to allow the user to interrupt.
* Probably not worth the effort.
*/
(void)sigemptyset(&set);
(void)sigaddset(&set, SIGTSTP);
(void)sigaddset(&set, SIGHUP);
(void)sigaddset(&set, SIGINT);
(void)sigaddset(&set, SIGQUIT);
(void)sigaddset(&set, SIGTERM);
(void)sigprocmask(SIG_BLOCK, &set, NULL);
/* We don't care what the user wants. */
(void)umask(0);
pname = *argv;
/* Open the original password file */
if ((fp = fopen(pname, "r")) == NULL)
mkpw_error("Cannot open `%s'", pname);
openinfo.lorder = lorder;
if (fstat(fileno(fp), &st) == -1)
mkpw_error("Cannot stat `%s'", pname);
if (cachesize) {
openinfo.cachesize = cachesize;
} else {
/* Tweak openinfo values for large passwd files. */
cachesize = (u_int)(st.st_size * 20);
if (cachesize > MAX_CACHESIZE)
cachesize = MAX_CACHESIZE;
else if (cachesize < MIN_CACHESIZE)
cachesize = MIN_CACHESIZE;
openinfo.cachesize = cachesize;
}
/* Open the temporary insecure password database. */
if (!secureonly) {
opendb(&idb, _PATH_MP_DB, username, req_version,
flags, PERM_INSECURE);
clean |= FILE_INSECURE;
}
/* Open the temporary encrypted password database. */
opendb(&sdb, _PATH_SMP_DB, username, req_version, flags, PERM_SECURE);
clean |= FILE_SECURE;
/*
* Open file for old password file. Minor trickiness -- don't want to
* chance the file already existing, since someone (stupidly) might
* still be using this for permission checking. So, open it first and
* fdopen the resulting fd. The resulting file should be readable by
* everyone.
*/
if (makeold) {
(void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
pname);
if ((tfd = open(oldpwdfile, O_WRONLY | O_CREAT | O_EXCL,
PERM_INSECURE)) < 0)
mkpw_error("Cannot create `%s'", oldpwdfile);
clean |= FILE_ORIG;
if ((oldfp = fdopen(tfd, "w")) == NULL)
mkpw_error("Cannot fdopen `%s'", oldpwdfile);
}
if (username != NULL) {
uid_dbflg = 0;
dbflg = 0;
/*
* Determine if this is a new entry.
*/
if (getdbent(&sdb, _PW_KEYBYNAME, username, &tpwd))
newuser = 1;
else {
newuser = 0;
olduid = tpwd->pw_uid;
}
/*
* If we see something go by that looks like YP, we save a special
* pointer record, which if YP is enabled in the C lib, will speed
* things up.
*/
for (lineno = 0; scan(fp, &pwd, &flags, &lineno);) {
/*
* Create original format password file entry.
*/
if (makeold) {
(void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
pwd.pw_dir, pwd.pw_shell);
if (ferror(oldfp))
wr_error(oldpwdfile);
}
if (username == NULL) {
/* Look like YP? */
if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')
hasyp++;
/* Warn about potentially unsafe uid/gid overrides. */
if (pwd.pw_name[0] == '+') {
if ((flags & _PASSWORD_NOUID) == 0 &&
pwd.pw_uid == 0)
mkpw_warning("line %d: superuser "
"override in YP inclusion", lineno);
if ((flags & _PASSWORD_NOGID) == 0 &&
pwd.pw_gid == 0)
mkpw_warning("line %d: wheel override "
"in YP inclusion", lineno);
}
/* Write the database entry out. */
if (!secureonly)
putdbents(&idb, &pwd, "*", flags, lineno, dbflg,
uid_dbflg);
continue;
} else if (strcmp(username, pwd.pw_name) != 0)
continue;
if (found) {
mkpw_warning("user `%s' listed twice in password file",
username);
bailout();
}
/*
* Ensure that the text file and database agree on
* which line the record is from.
*/
rv = getdbent(&sdb, _PW_KEYBYNUM, &lineno, &tpwd);
if (newuser) {
if (rv == 0)
inconsistency();
} else if (rv == 1 || strcmp(username, tpwd->pw_name) != 0)
inconsistency();
else if ((uid_t)olduid != pwd.pw_uid) {
/*
* If we're changing UID, remove the BYUID
* record for the old UID only if it has the
* same username.
*/
if (!getdbent(&sdb, _PW_KEYBYUID, &olduid, &tpwd)) {
if (strcmp(username, tpwd->pw_name) == 0) {
if (!secureonly)
deldbent(&idb, _PW_KEYBYUID,
&olduid);
deldbent(&sdb, _PW_KEYBYUID, &olduid);
}
} else
inconsistency();
}
/*
* If there's an existing BYUID record for the new UID and
* the username doesn't match then be sure not to overwrite
* it.
*/
if (!getdbent(&sdb, _PW_KEYBYUID, &pwd.pw_uid, &tpwd))
if (strcmp(username, tpwd->pw_name) != 0)
uid_dbflg = R_NOOVERWRITE;
/* Write the database entries out */
if (!secureonly)
putdbents(&idb, &pwd, "*", flags, lineno, dbflg,
uid_dbflg);
putdbents(&sdb, &pwd, pwd.pw_passwd, flags, lineno, dbflg,
uid_dbflg);
found = 1;
if (!makeold)
break;
}
if (!secureonly) {
/* Store YP token if needed. */
if (hasyp)
putyptoken(&idb);
/* Close the insecure database. */
closedb(&idb);
}
/*
* If rebuilding the databases, we re-parse the text file and write
* the secure entries out in a separate pass.
*/
if (username == NULL) {
rewind(fp);
for (lineno = 0; scan(fp, &pwd, &flags, &lineno);)
putdbents(&sdb, &pwd, pwd.pw_passwd, flags,
lineno, dbflg, uid_dbflg);
/* Store YP token if needed. */
if (hasyp)
putyptoken(&sdb);
} else if (!found) {
mkpw_warning("user `%s' not found in password file", username);
bailout();
}
/* Close the secure database. */
closedb(&sdb);
/* Install as the real password files. */
if (!secureonly)
install(idb.dbname, idb.fname);
install(sdb.dbname, sdb.fname);
/* Install the V7 password file. */
if (makeold) {
if (fflush(oldfp) == EOF)
wr_error(oldpwdfile);
if (fclose(oldfp) == EOF)
wr_error(oldpwdfile);
install(oldpwdfile, _PATH_PASSWD);
}
/* Set master.passwd permissions, in case caller forgot. */
(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
if (fclose(fp) == EOF)
wr_error(pname);
/*
* Move the temporary master password file LAST -- chpass(1),
* passwd(1), vipw(8) and friends all use its existence to block
* other incarnations of themselves. The rename means that
* everything is unlocked, as the original file can no longer be
* accessed.
*/
install(pname, _PATH_MASTERPASSWD);
exit(EXIT_SUCCESS);
/* NOTREACHED */
}
static int
scan(FILE *fp, struct passwd *pw, int *flags, int *lineno)
{
static char line[LINE_MAX];
char *p;
int oflags;
if (fgets(line, (int)sizeof(line), fp) == NULL)
return (0);
(*lineno)++;
/*
* ``... if I swallow anything evil, put your fingers down my
* throat...''
* -- The Who
*/
if ((p = strchr(line, '\n')) == NULL) {
errno = EFTYPE; /* XXX */
mkpw_error("%s, %d: line too long", pname, *lineno);
}
*p = '\0';
if (strcmp(line, "+") == 0) {
/* pw_scan() can't handle "+" */
(void)strcpy(line, "+:::::::::");
}
oflags = 0;
if (!pw_scan(line, pw, &oflags)) {
errno = EFTYPE; /* XXX */
mkpw_error("%s, %d: Syntax mkpw_error", pname, *lineno);
}
*flags = oflags;
mkpw_warning("text files and databases are inconsistent");
mkpw_warning("re-build the databases without -u");
bailout();
}
static void
bailout(void)
{
if ((clean & FILE_ORIG) != 0)
rm(oldpwdfile);
if ((clean & FILE_SECURE) != 0)
rm(sdb.dbname);
if ((clean & FILE_INSECURE) != 0)
rm(idb.dbname);
exit(EXIT_FAILURE);
}
static uint32_t
getversion(const char *fname)
{
DBT data, key;
int ret;
uint32_t version = 0;
DB *db;
db = dbopen(fname, O_RDONLY, PERM_INSECURE, DB_HASH, NULL);
if (db == NULL) {
/* If we are building on a separate root, assume version 1 */
if ((errno == EACCES && prefix[0]) || errno == ENOENT)
return 1;
mkpw_warning("Cannot open database `%s'", fname);
bailout();
}
key.data = __UNCONST("VERSION");
key.size = strlen((const char *)key.data) + 1;
switch (ret = (*db->get)(db, &key, &data, 0)) {
case -1: /* Error */
mkpw_warning("Cannot get VERSION record from database `%s'",
fname);
goto out;
case 0:
if (data.size != sizeof(version)) {
mkpw_warning("Bad VERSION record in database `%s'", fname);
goto out;
}
(void)memcpy(&version, data.data, sizeof(version));
/*FALLTHROUGH*/
case 1:
if (ret == 1)
mkpw_warning("Database `%s' has no version info",
fname);
(*db->close)(db);
return version;
default:
mkpw_warning("internal mkpw_error db->get returns %d", ret);
goto out;
}
out:
(*db->close)(db);
bailout();
/*NOTREACHED*/
}
if ((*db->db->put)(db->db, &key, &data, 0) != 0) {
mkpw_warning("Can't write VERSION record to `%s'", db->dbname);
bailout();
}
}
/*
* Write entries to a database for a single user.
*
* The databases actually contain three copies of the original data. Each
* password file entry is converted into a rough approximation of a ``struct
* passwd'', with the strings placed inline. This object is then stored as
* the data for three separate keys. The first key * is the pw_name field
* prepended by the _PW_KEYBYNAME character. The second key is the pw_uid
* field prepended by the _PW_KEYBYUID character. The third key is the line
* number in the original file prepended by the _PW_KEYBYNUM character.
* (The special characters are prepended to ensure that the keys do not
* collide.)
*/
#define COMPACT(e) for (t = e; (*p++ = *t++) != '\0';)