1994-05-26 07:23:31 +02:00
|
|
|
/*-
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-10 08:27:07 +02:00
|
|
|
static const char copyright[] =
|
1994-05-26 07:23:31 +02:00
|
|
|
"@(#) Copyright (c) 1991, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-10 08:27:07 +02:00
|
|
|
#if 0
|
1994-05-26 07:23:31 +02:00
|
|
|
static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
|
1997-10-10 08:27:07 +02:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 03:35:59 +02:00
|
|
|
"$FreeBSD$";
|
1994-05-26 07:23:31 +02:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <db.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "pw_scan.h"
|
|
|
|
|
|
|
|
#define INSECURE 1
|
|
|
|
#define SECURE 2
|
|
|
|
#define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
|
|
|
|
#define PERM_SECURE (S_IRUSR|S_IWUSR)
|
|
|
|
|
|
|
|
HASHINFO openinfo = {
|
|
|
|
4096, /* bsize */
|
|
|
|
32, /* ffactor */
|
|
|
|
256, /* nelem */
|
|
|
|
2048 * 1024, /* cachesize */
|
|
|
|
NULL, /* hash() */
|
|
|
|
0 /* lorder */
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
|
|
|
|
static struct passwd pwd; /* password structure */
|
|
|
|
static char *pname; /* password file name */
|
1994-09-08 00:54:00 +02:00
|
|
|
static char prefix[MAXPATHLEN];
|
1994-05-26 07:23:31 +02:00
|
|
|
|
1998-09-29 22:01:21 +02:00
|
|
|
static int is_comment; /* flag for comments */
|
1997-03-08 15:09:24 +01:00
|
|
|
static char line[LINE_MAX];
|
|
|
|
|
1994-05-26 07:23:31 +02:00
|
|
|
void cleanup __P((void));
|
|
|
|
void error __P((char *));
|
1996-07-01 21:38:50 +02:00
|
|
|
void cp __P((char *, char *, mode_t mode));
|
1994-05-26 07:23:31 +02:00
|
|
|
void mv __P((char *, char *));
|
|
|
|
int scan __P((FILE *, struct passwd *));
|
1997-10-10 08:27:07 +02:00
|
|
|
static void usage __P((void));
|
1994-05-26 07:23:31 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
1996-07-01 21:38:50 +02:00
|
|
|
DB *dp, *sdp, *pw_db;
|
|
|
|
DBT data, sdata, key;
|
1994-05-26 07:23:31 +02:00
|
|
|
FILE *fp, *oldfp;
|
|
|
|
sigset_t set;
|
1996-04-16 02:23:59 +02:00
|
|
|
int ch, cnt, ypcnt, len, makeold, tfd, yp_enabled = 0;
|
1994-05-26 07:23:31 +02:00
|
|
|
char *p, *t;
|
|
|
|
char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
|
1996-07-01 21:38:50 +02:00
|
|
|
char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
|
1994-09-08 00:54:00 +02:00
|
|
|
char buf2[MAXPATHLEN];
|
1996-07-01 21:38:50 +02:00
|
|
|
char sbuf2[MAXPATHLEN];
|
|
|
|
char *username;
|
|
|
|
u_int method, methoduid;
|
1998-01-10 18:27:28 +01:00
|
|
|
int Cflag;
|
1998-12-13 02:53:50 +01:00
|
|
|
int nblock = 0;
|
1994-05-26 07:23:31 +02:00
|
|
|
|
1998-01-10 18:27:28 +01:00
|
|
|
Cflag = 0;
|
1994-09-08 00:54:00 +02:00
|
|
|
strcpy(prefix, _PATH_PWD);
|
1994-05-26 07:23:31 +02:00
|
|
|
makeold = 0;
|
1996-07-01 21:38:50 +02:00
|
|
|
username = NULL;
|
1999-12-02 17:39:15 +01:00
|
|
|
while ((ch = getopt(argc, argv, "Cd:ps:u:vN")) != -1)
|
1994-05-26 07:23:31 +02:00
|
|
|
switch(ch) {
|
1998-01-10 18:27:28 +01:00
|
|
|
case 'C': /* verify only */
|
|
|
|
Cflag = 1;
|
1996-08-19 18:03:15 +02:00
|
|
|
break;
|
1994-09-08 00:54:00 +02:00
|
|
|
case 'd':
|
1997-03-20 04:35:06 +01:00
|
|
|
strncpy(prefix, optarg, sizeof prefix - 1);
|
1994-09-08 00:54:00 +02:00
|
|
|
break;
|
1994-05-26 07:23:31 +02:00
|
|
|
case 'p': /* create V7 "file.orig" */
|
|
|
|
makeold = 1;
|
|
|
|
break;
|
1998-04-19 09:15:34 +02:00
|
|
|
case 's': /* change default cachesize */
|
|
|
|
openinfo.cachesize = atoi(optarg) * 1024 * 1024;
|
|
|
|
break;
|
1996-07-01 21:38:50 +02:00
|
|
|
case 'u': /* only update this record */
|
|
|
|
username = optarg;
|
|
|
|
break;
|
1994-09-08 00:54:00 +02:00
|
|
|
case 'v': /* backward compatible */
|
1994-05-26 07:23:31 +02:00
|
|
|
break;
|
1998-12-13 02:53:50 +01:00
|
|
|
case 'N': /* do not wait for lock */
|
|
|
|
nblock = LOCK_NB;
|
|
|
|
break;
|
1994-05-26 07:23:31 +02:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1996-07-03 23:43:48 +02:00
|
|
|
if (argc != 1 || (username && (*username == '+' || *username == '-')))
|
1994-05-26 07:23:31 +02:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This could be changed to allow the user to interrupt.
|
|
|
|
* Probably not worth the effort.
|
|
|
|
*/
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGTSTP);
|
|
|
|
sigaddset(&set, SIGHUP);
|
|
|
|
sigaddset(&set, SIGINT);
|
|
|
|
sigaddset(&set, SIGQUIT);
|
|
|
|
sigaddset(&set, SIGTERM);
|
|
|
|
(void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
|
|
|
|
|
|
|
|
/* We don't care what the user wants. */
|
|
|
|
(void)umask(0);
|
|
|
|
|
|
|
|
pname = *argv;
|
1998-12-13 02:53:50 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open and lock the original password file. We have to check
|
|
|
|
* the hardlink count after we get the lock to handle any potential
|
|
|
|
* unlink/rename race.
|
|
|
|
*
|
|
|
|
* This lock is necessary when someone runs pwd_mkdb manually, directly
|
|
|
|
* on master.passwd, to handle the case where a user might try to
|
|
|
|
* change his password while pwd_mkdb is running.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!(fp = fopen(pname, "r")))
|
|
|
|
error(pname);
|
|
|
|
if (flock(fileno(fp), LOCK_EX|nblock) < 0)
|
|
|
|
error("flock");
|
|
|
|
if (fstat(fileno(fp), &st) < 0)
|
|
|
|
error(pname);
|
|
|
|
if (st.st_nlink != 0)
|
|
|
|
break;
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
1994-05-26 07:23:31 +02:00
|
|
|
|
1996-08-19 18:03:15 +02:00
|
|
|
/* check only if password database is valid */
|
1998-01-10 18:27:28 +01:00
|
|
|
if (Cflag) {
|
1996-08-19 18:03:15 +02:00
|
|
|
for (cnt = 1; scan(fp, &pwd); ++cnt);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1994-05-26 07:23:31 +02:00
|
|
|
/* Open the temporary insecure password database. */
|
1994-09-08 00:54:00 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
|
1996-07-01 21:38:50 +02:00
|
|
|
(void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
|
1996-07-12 10:20:28 +02:00
|
|
|
if (username) {
|
1996-07-01 21:38:50 +02:00
|
|
|
(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
|
|
|
|
(void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
|
|
|
|
|
|
|
|
clean = FILE_INSECURE;
|
|
|
|
cp(buf2, buf, PERM_INSECURE);
|
|
|
|
dp = dbopen(buf,
|
|
|
|
O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
|
|
|
|
if (dp == NULL)
|
|
|
|
error(buf);
|
|
|
|
|
|
|
|
clean = FILE_SECURE;
|
|
|
|
cp(sbuf2, sbuf, PERM_SECURE);
|
|
|
|
sdp = dbopen(sbuf,
|
|
|
|
O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
|
|
|
|
if (sdp == NULL)
|
|
|
|
error(sbuf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do some trouble to check if we should store this users
|
|
|
|
* uid. Don't use getpwnam/getpwuid as that interferes
|
|
|
|
* with NIS.
|
|
|
|
*/
|
|
|
|
pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
|
|
|
|
if (!pw_db)
|
|
|
|
error(_MP_DB);
|
|
|
|
buf[0] = _PW_KEYBYNAME;
|
|
|
|
len = strlen(username);
|
|
|
|
|
|
|
|
/* Only check that username fits in buffer */
|
|
|
|
memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
|
|
|
|
key.data = (u_char *)buf;
|
|
|
|
key.size = len + 1;
|
|
|
|
if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
|
|
|
|
p = (char *)data.data;
|
|
|
|
|
|
|
|
/* jump over pw_name and pw_passwd, to get to pw_uid */
|
1996-07-12 10:20:28 +02:00
|
|
|
while (*p++)
|
|
|
|
;
|
|
|
|
while (*p++)
|
|
|
|
;
|
1996-07-01 21:38:50 +02:00
|
|
|
|
|
|
|
buf[0] = _PW_KEYBYUID;
|
|
|
|
memmove(buf + 1, p, sizeof(int));
|
|
|
|
key.data = (u_char *)buf;
|
|
|
|
key.size = sizeof(int) + 1;
|
|
|
|
|
|
|
|
if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
|
|
|
|
/* First field of data.data holds pw_pwname */
|
1996-07-12 10:20:28 +02:00
|
|
|
if (!strcmp(data.data, username))
|
1996-07-01 21:38:50 +02:00
|
|
|
methoduid = 0;
|
|
|
|
else
|
|
|
|
methoduid = R_NOOVERWRITE;
|
|
|
|
} else {
|
|
|
|
methoduid = R_NOOVERWRITE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
methoduid = R_NOOVERWRITE;
|
|
|
|
}
|
1998-02-15 23:31:40 +01:00
|
|
|
if ((pw_db->close)(pw_db))
|
|
|
|
error("close pw_db");
|
1996-07-01 21:38:50 +02:00
|
|
|
method = 0;
|
|
|
|
} else {
|
|
|
|
dp = dbopen(buf,
|
|
|
|
O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
|
|
|
|
if (dp == NULL)
|
|
|
|
error(buf);
|
|
|
|
clean = FILE_INSECURE;
|
|
|
|
|
|
|
|
sdp = dbopen(sbuf,
|
|
|
|
O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
|
|
|
|
if (sdp == NULL)
|
|
|
|
error(sbuf);
|
|
|
|
clean = FILE_SECURE;
|
|
|
|
|
|
|
|
method = R_NOOVERWRITE;
|
|
|
|
methoduid = R_NOOVERWRITE;
|
|
|
|
}
|
1994-05-26 07:23:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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(buf, sizeof(buf), "%s.orig", pname);
|
|
|
|
if ((tfd = open(buf,
|
|
|
|
O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
|
|
|
|
error(buf);
|
|
|
|
if ((oldfp = fdopen(tfd, "w")) == NULL)
|
|
|
|
error(buf);
|
|
|
|
clean = FILE_ORIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.)
|
|
|
|
*/
|
1996-04-16 02:23:59 +02:00
|
|
|
ypcnt = 1;
|
1994-05-26 07:23:31 +02:00
|
|
|
data.data = (u_char *)buf;
|
1996-07-01 21:38:50 +02:00
|
|
|
sdata.data = (u_char *)sbuf;
|
1994-05-26 07:23:31 +02:00
|
|
|
key.data = (u_char *)tbuf;
|
|
|
|
for (cnt = 1; scan(fp, &pwd); ++cnt) {
|
1998-09-29 22:01:21 +02:00
|
|
|
if (!is_comment &&
|
1997-03-08 15:09:24 +01:00
|
|
|
(pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
|
1995-03-23 18:22:37 +01:00
|
|
|
yp_enabled = 1;
|
1999-09-06 19:30:03 +02:00
|
|
|
if (is_comment)
|
|
|
|
--cnt;
|
1997-10-10 08:27:07 +02:00
|
|
|
#define COMPACT(e) t = e; while ((*p++ = *t++));
|
1998-09-29 22:01:21 +02:00
|
|
|
if (!is_comment &&
|
1997-03-08 15:09:24 +01:00
|
|
|
(!username || (strcmp(username, pwd.pw_name) == 0))) {
|
1996-07-01 21:38:50 +02:00
|
|
|
/* Create insecure data. */
|
|
|
|
p = buf;
|
|
|
|
COMPACT(pwd.pw_name);
|
|
|
|
COMPACT("*");
|
2000-03-09 19:11:16 +01:00
|
|
|
memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
|
1996-07-01 21:38:50 +02:00
|
|
|
p += sizeof(int);
|
2000-03-09 19:11:16 +01:00
|
|
|
memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
|
1996-07-01 21:38:50 +02:00
|
|
|
p += sizeof(int);
|
|
|
|
memmove(p, &pwd.pw_change, sizeof(time_t));
|
|
|
|
p += sizeof(time_t);
|
|
|
|
COMPACT(pwd.pw_class);
|
|
|
|
COMPACT(pwd.pw_gecos);
|
|
|
|
COMPACT(pwd.pw_dir);
|
|
|
|
COMPACT(pwd.pw_shell);
|
|
|
|
memmove(p, &pwd.pw_expire, sizeof(time_t));
|
|
|
|
p += sizeof(time_t);
|
|
|
|
memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
|
|
|
|
p += sizeof pwd.pw_fields;
|
|
|
|
data.size = p - buf;
|
|
|
|
|
|
|
|
/* Create secure data. */
|
|
|
|
p = sbuf;
|
|
|
|
COMPACT(pwd.pw_name);
|
|
|
|
COMPACT(pwd.pw_passwd);
|
2000-03-09 19:11:16 +01:00
|
|
|
memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
|
1996-07-01 21:38:50 +02:00
|
|
|
p += sizeof(int);
|
2000-03-09 19:11:16 +01:00
|
|
|
memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
|
1996-07-01 21:38:50 +02:00
|
|
|
p += sizeof(int);
|
|
|
|
memmove(p, &pwd.pw_change, sizeof(time_t));
|
|
|
|
p += sizeof(time_t);
|
|
|
|
COMPACT(pwd.pw_class);
|
|
|
|
COMPACT(pwd.pw_gecos);
|
|
|
|
COMPACT(pwd.pw_dir);
|
|
|
|
COMPACT(pwd.pw_shell);
|
|
|
|
memmove(p, &pwd.pw_expire, sizeof(time_t));
|
|
|
|
p += sizeof(time_t);
|
|
|
|
memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
|
|
|
|
p += sizeof pwd.pw_fields;
|
|
|
|
sdata.size = p - sbuf;
|
|
|
|
|
|
|
|
/* Store insecure by name. */
|
|
|
|
tbuf[0] = _PW_KEYBYNAME;
|
|
|
|
len = strlen(pwd.pw_name);
|
|
|
|
memmove(tbuf + 1, pwd.pw_name, len);
|
|
|
|
key.size = len + 1;
|
|
|
|
if ((dp->put)(dp, &key, &data, method) == -1)
|
|
|
|
error("put");
|
1994-05-26 07:23:31 +02:00
|
|
|
|
1996-07-01 21:38:50 +02:00
|
|
|
/* Store insecure by number. */
|
|
|
|
tbuf[0] = _PW_KEYBYNUM;
|
|
|
|
memmove(tbuf + 1, &cnt, sizeof(cnt));
|
|
|
|
key.size = sizeof(cnt) + 1;
|
|
|
|
if ((dp->put)(dp, &key, &data, method) == -1)
|
|
|
|
error("put");
|
1994-05-26 07:23:31 +02:00
|
|
|
|
1996-07-01 21:38:50 +02:00
|
|
|
/* Store insecure by uid. */
|
|
|
|
tbuf[0] = _PW_KEYBYUID;
|
|
|
|
memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
|
|
|
|
key.size = sizeof(pwd.pw_uid) + 1;
|
|
|
|
if ((dp->put)(dp, &key, &data, methoduid) == -1)
|
|
|
|
error("put");
|
|
|
|
|
|
|
|
/* Store secure by name. */
|
|
|
|
tbuf[0] = _PW_KEYBYNAME;
|
|
|
|
len = strlen(pwd.pw_name);
|
|
|
|
memmove(tbuf + 1, pwd.pw_name, len);
|
|
|
|
key.size = len + 1;
|
|
|
|
if ((sdp->put)(sdp, &key, &sdata, method) == -1)
|
|
|
|
error("put");
|
1994-05-26 07:23:31 +02:00
|
|
|
|
1996-07-01 21:38:50 +02:00
|
|
|
/* Store secure by number. */
|
|
|
|
tbuf[0] = _PW_KEYBYNUM;
|
|
|
|
memmove(tbuf + 1, &cnt, sizeof(cnt));
|
1995-03-23 01:54:57 +01:00
|
|
|
key.size = sizeof(cnt) + 1;
|
1996-07-01 21:38:50 +02:00
|
|
|
if ((sdp->put)(sdp, &key, &sdata, method) == -1)
|
|
|
|
error("put");
|
|
|
|
|
|
|
|
/* Store secure by uid. */
|
|
|
|
tbuf[0] = _PW_KEYBYUID;
|
|
|
|
memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
|
|
|
|
key.size = sizeof(pwd.pw_uid) + 1;
|
|
|
|
if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
|
1995-03-23 01:54:57 +01:00
|
|
|
error("put");
|
|
|
|
|
1996-07-01 21:38:50 +02:00
|
|
|
/* Store insecure and secure special plus and special minus */
|
|
|
|
if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
|
|
|
|
tbuf[0] = _PW_KEYYPBYNUM;
|
|
|
|
memmove(tbuf + 1, &ypcnt, sizeof(cnt));
|
|
|
|
ypcnt++;
|
|
|
|
key.size = sizeof(cnt) + 1;
|
|
|
|
if ((dp->put)(dp, &key, &data, method) == -1)
|
|
|
|
error("put");
|
|
|
|
if ((sdp->put)(sdp, &key, &sdata, method) == -1)
|
|
|
|
error("put");
|
|
|
|
}
|
|
|
|
}
|
1994-05-26 07:23:31 +02:00
|
|
|
/* Create original format password file entry */
|
1998-09-29 22:01:21 +02:00
|
|
|
if (is_comment && makeold){ /* copy comments */
|
1998-02-16 20:08:18 +01:00
|
|
|
if (fprintf(oldfp, "%s\n", line) < 0)
|
1998-02-15 23:31:40 +01:00
|
|
|
error("write old");
|
1998-02-19 09:12:11 +01:00
|
|
|
} else if (makeold) {
|
1996-10-22 05:18:11 +02:00
|
|
|
char uidstr[20];
|
|
|
|
char gidstr[20];
|
|
|
|
|
2000-03-09 19:11:16 +01:00
|
|
|
snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
|
|
|
|
snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
|
1996-10-22 05:18:11 +02:00
|
|
|
|
1998-02-15 23:31:40 +01:00
|
|
|
if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
|
1996-10-22 05:18:11 +02:00
|
|
|
pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
|
|
|
|
pwd.pw_fields & _PWF_GID ? gidstr : "",
|
1998-02-16 20:08:18 +01:00
|
|
|
pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
|
1998-02-15 23:31:40 +01:00
|
|
|
error("write old");
|
1996-10-22 05:18:11 +02:00
|
|
|
}
|
1994-05-26 07:23:31 +02:00
|
|
|
}
|
1994-09-20 03:15:08 +02:00
|
|
|
/* If YP enabled, set flag. */
|
1996-07-12 10:20:28 +02:00
|
|
|
if (yp_enabled) {
|
1994-09-20 23:40:12 +02:00
|
|
|
buf[0] = yp_enabled + 2;
|
|
|
|
data.size = 1;
|
1994-09-20 03:15:08 +02:00
|
|
|
tbuf[0] = _PW_KEYYPENABLED;
|
|
|
|
key.size = 1;
|
1996-07-01 21:38:50 +02:00
|
|
|
if ((dp->put)(dp, &key, &data, method) == -1)
|
|
|
|
error("put");
|
|
|
|
if ((sdp->put)(sdp, &key, &data, method) == -1)
|
1994-09-20 03:15:08 +02:00
|
|
|
error("put");
|
|
|
|
}
|
|
|
|
|
1997-08-18 22:48:35 +02:00
|
|
|
if ((dp->close)(dp) == -1)
|
|
|
|
error("close");
|
|
|
|
if ((sdp->close)(sdp) == -1)
|
|
|
|
error("close");
|
1994-05-26 07:23:31 +02:00
|
|
|
if (makeold) {
|
|
|
|
(void)fflush(oldfp);
|
1998-02-15 23:31:40 +01:00
|
|
|
if (fclose(oldfp) == EOF)
|
|
|
|
error("close old");
|
1994-05-26 07:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set master.passwd permissions, in case caller forgot. */
|
|
|
|
(void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
|
|
|
|
|
|
|
|
/* Install as the real password files. */
|
1994-09-08 00:54:00 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
|
|
|
|
(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
|
|
|
|
mv(buf, buf2);
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
|
|
|
|
(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
|
|
|
|
mv(buf, buf2);
|
1994-05-26 07:23:31 +02:00
|
|
|
if (makeold) {
|
1994-09-08 00:54:00 +02:00
|
|
|
(void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
|
1994-05-26 07:23:31 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
|
1995-05-30 05:57:47 +02:00
|
|
|
mv(buf, buf2);
|
1994-05-26 07:23:31 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
|
|
|
|
* all use flock(2) on it to block other incarnations of themselves.
|
|
|
|
* The rename means that everything is unlocked, as the original file
|
|
|
|
* can no longer be accessed.
|
|
|
|
*/
|
1994-09-08 00:54:00 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
|
1995-05-30 05:57:47 +02:00
|
|
|
mv(pname, buf);
|
1998-12-13 02:53:50 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Close locked password file after rename()
|
|
|
|
*/
|
|
|
|
if (fclose(fp) == EOF)
|
|
|
|
error("close fp");
|
|
|
|
|
1994-05-26 07:23:31 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
scan(fp, pw)
|
|
|
|
FILE *fp;
|
|
|
|
struct passwd *pw;
|
|
|
|
{
|
|
|
|
static int lcnt;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!fgets(line, sizeof(line), fp))
|
|
|
|
return (0);
|
|
|
|
++lcnt;
|
|
|
|
/*
|
|
|
|
* ``... if I swallow anything evil, put your fingers down my
|
|
|
|
* throat...''
|
|
|
|
* -- The Who
|
|
|
|
*/
|
|
|
|
if (!(p = strchr(line, '\n'))) {
|
|
|
|
warnx("line too long");
|
|
|
|
goto fmt;
|
|
|
|
|
|
|
|
}
|
|
|
|
*p = '\0';
|
1997-03-08 15:09:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore comments: ^[ \t]*#
|
|
|
|
*/
|
|
|
|
for (p = line; *p != '\0'; p++)
|
|
|
|
if (*p != ' ' && *p != '\t')
|
|
|
|
break;
|
|
|
|
if (*p == '#' || *p == '\0') {
|
1998-09-29 22:01:21 +02:00
|
|
|
is_comment = 1;
|
1997-03-08 15:09:24 +01:00
|
|
|
return(1);
|
|
|
|
} else
|
1998-09-29 22:01:21 +02:00
|
|
|
is_comment = 0;
|
1997-03-08 15:09:24 +01:00
|
|
|
|
Add nsswitch support. By creating an /etc/nsswitch.conf file, you can
configure FreeBSD so that various databases such as passwd and group can be
looked up using flat files, NIS, or Hesiod.
= Hesiod has been added to libc (see hesiod(3)).
= A library routine for parsing nsswitch.conf and invoking callback
functions as specified has been added to libc (see nsdispatch(3)).
= The following C library functions have been modified to use nsdispatch:
. getgrent, getgrnam, getgrgid
. getpwent, getpwnam, getpwuid
. getusershell
. getaddrinfo
. gethostbyname, gethostbyname2, gethostbyaddr
. getnetbyname, getnetbyaddr
. getipnodebyname, getipnodebyaddr, getnodebyname, getnodebyaddr
= host.conf has been removed from src/etc. rc.network has been modified
to warn that host.conf is no longer used at boot time. In addition, if
there is a host.conf but no nsswitch.conf, the latter is created at boot
time from the former.
Obtained from: NetBSD
2000-09-06 20:16:48 +02:00
|
|
|
if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
|
1994-05-26 07:23:31 +02:00
|
|
|
warnx("at line #%d", lcnt);
|
|
|
|
fmt: errno = EFTYPE; /* XXX */
|
|
|
|
error(pname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1996-07-01 21:38:50 +02:00
|
|
|
void
|
|
|
|
cp(from, to, mode)
|
|
|
|
char *from, *to;
|
|
|
|
mode_t mode;
|
|
|
|
{
|
|
|
|
static char buf[MAXBSIZE];
|
|
|
|
int from_fd, rcount, to_fd, wcount;
|
|
|
|
|
|
|
|
if ((from_fd = open(from, O_RDONLY, 0)) < 0)
|
|
|
|
error(from);
|
|
|
|
if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
|
|
|
|
error(to);
|
|
|
|
while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
|
|
|
|
wcount = write(to_fd, buf, rcount);
|
|
|
|
if (rcount != wcount || wcount == -1) {
|
|
|
|
int sverrno = errno;
|
|
|
|
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
|
|
|
|
errno = sverrno;
|
|
|
|
error(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rcount < 0) {
|
|
|
|
int sverrno = errno;
|
|
|
|
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
|
|
|
|
errno = sverrno;
|
|
|
|
error(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-26 07:23:31 +02:00
|
|
|
void
|
|
|
|
mv(from, to)
|
|
|
|
char *from, *to;
|
|
|
|
{
|
|
|
|
char buf[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (rename(from, to)) {
|
|
|
|
int sverrno = errno;
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
|
|
|
|
errno = sverrno;
|
|
|
|
error(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
error(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
|
1997-10-10 08:27:07 +02:00
|
|
|
warn("%s", name);
|
1994-05-26 07:23:31 +02:00
|
|
|
cleanup();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup()
|
|
|
|
{
|
|
|
|
char buf[MAXPATHLEN];
|
|
|
|
|
|
|
|
switch(clean) {
|
|
|
|
case FILE_ORIG:
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s.orig", pname);
|
|
|
|
(void)unlink(buf);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FILE_SECURE:
|
1994-09-08 00:54:00 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
|
1994-05-26 07:23:31 +02:00
|
|
|
(void)unlink(buf);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FILE_INSECURE:
|
1994-09-08 00:54:00 +02:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
|
1994-05-26 07:23:31 +02:00
|
|
|
(void)unlink(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-10 08:27:07 +02:00
|
|
|
static void
|
1994-05-26 07:23:31 +02:00
|
|
|
usage()
|
|
|
|
{
|
|
|
|
|
1997-10-10 08:27:07 +02:00
|
|
|
(void)fprintf(stderr,
|
1999-01-01 21:39:37 +01:00
|
|
|
"usage: pwd_mkdb [-C] [-N] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n");
|
1994-05-26 07:23:31 +02:00
|
|
|
exit(1);
|
|
|
|
}
|