1dea673e9SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4dea673e9SRodney W. Grimes * Copyright (c) 1991, 1993, 1994
5dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved.
6dea673e9SRodney W. Grimes *
7dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
8dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions
9dea673e9SRodney W. Grimes * are met:
10dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
11dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
12dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
13dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
14dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
16dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software
17dea673e9SRodney W. Grimes * without specific prior written permission.
18dea673e9SRodney W. Grimes *
19dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29dea673e9SRodney W. Grimes * SUCH DAMAGE.
30dea673e9SRodney W. Grimes */
31dea673e9SRodney W. Grimes
32dea673e9SRodney W. Grimes #include <sys/param.h>
33e2db8d37SRuslan Ermilov #include <sys/endian.h>
34dea673e9SRodney W. Grimes #include <sys/stat.h>
3505f98035SJacques Vidrine #include <arpa/inet.h>
36dea673e9SRodney W. Grimes
37dea673e9SRodney W. Grimes #include <db.h>
38dea673e9SRodney W. Grimes #include <err.h>
39dea673e9SRodney W. Grimes #include <errno.h>
40dea673e9SRodney W. Grimes #include <fcntl.h>
41d32a66b2SRenato Botelho #include <libgen.h>
42dea673e9SRodney W. Grimes #include <limits.h>
43dea673e9SRodney W. Grimes #include <pwd.h>
44dea673e9SRodney W. Grimes #include <signal.h>
45dea673e9SRodney W. Grimes #include <stdio.h>
46dea673e9SRodney W. Grimes #include <stdlib.h>
47dea673e9SRodney W. Grimes #include <string.h>
48dea673e9SRodney W. Grimes #include <unistd.h>
49dea673e9SRodney W. Grimes
50dea673e9SRodney W. Grimes #include "pw_scan.h"
51dea673e9SRodney W. Grimes
52dea673e9SRodney W. Grimes #define INSECURE 1
53dea673e9SRodney W. Grimes #define SECURE 2
54dea673e9SRodney W. Grimes #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
55dea673e9SRodney W. Grimes #define PERM_SECURE (S_IRUSR|S_IWUSR)
56b4603f3dSJacques Vidrine #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
57b4603f3dSJacques Vidrine #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
58dea673e9SRodney W. Grimes
594da4d001SEd Schouten static HASHINFO openinfo = {
60dea673e9SRodney W. Grimes 4096, /* bsize */
61dea673e9SRodney W. Grimes 32, /* ffactor */
62dea673e9SRodney W. Grimes 256, /* nelem */
63dea673e9SRodney W. Grimes 2048 * 1024, /* cachesize */
64dea673e9SRodney W. Grimes NULL, /* hash() */
65e936bb9fSEd Maste BIG_ENDIAN /* lorder */
66dea673e9SRodney W. Grimes };
67dea673e9SRodney W. Grimes
68dea673e9SRodney W. Grimes static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
69dea673e9SRodney W. Grimes static struct passwd pwd; /* password structure */
70dea673e9SRodney W. Grimes static char *pname; /* password file name */
71fb0e6accSGary Palmer static char prefix[MAXPATHLEN];
72dea673e9SRodney W. Grimes
7350819461SDmitrij Tejblum static int is_comment; /* flag for comments */
74eb0abfccSWolfram Schneider static char line[LINE_MAX];
75eb0abfccSWolfram Schneider
762f9af241SAlfred Perlstein void cleanup(void);
772f9af241SAlfred Perlstein void error(const char *);
782f9af241SAlfred Perlstein void cp(char *, char *, mode_t mode);
792f9af241SAlfred Perlstein void mv(char *, char *);
802f9af241SAlfred Perlstein int scan(FILE *, struct passwd *);
812f9af241SAlfred Perlstein static void usage(void);
82dea673e9SRodney W. Grimes
83dea673e9SRodney W. Grimes int
main(int argc,char * argv[])842f9af241SAlfred Perlstein main(int argc, char *argv[])
85dea673e9SRodney W. Grimes {
8605f98035SJacques Vidrine static char verskey[] = _PWD_VERSION_KEY;
8705f98035SJacques Vidrine char version = _PWD_CURRENT_VERSION;
8879a1b8d9SGuido van Rooij DB *dp, *sdp, *pw_db;
8979a1b8d9SGuido van Rooij DBT data, sdata, key;
90dea673e9SRodney W. Grimes FILE *fp, *oldfp;
91dea673e9SRodney W. Grimes sigset_t set;
922f9af241SAlfred Perlstein int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
932f9af241SAlfred Perlstein unsigned int len;
9405f98035SJacques Vidrine uint32_t store;
952f9af241SAlfred Perlstein const char *t;
962f9af241SAlfred Perlstein char *p;
97*fcaae065SDag-Erling Smørgrav char buf[MAX(MAXPATHLEN, LINE_MAX * 2)];
9879a1b8d9SGuido van Rooij char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
99fb0e6accSGary Palmer char buf2[MAXPATHLEN];
10079a1b8d9SGuido van Rooij char sbuf2[MAXPATHLEN];
101*fcaae065SDag-Erling Smørgrav char tbuf[1024];
10279a1b8d9SGuido van Rooij char *username;
10379a1b8d9SGuido van Rooij u_int method, methoduid;
104c20dbeb6SEd Maste int Cflag, dflag, iflag;
105be648216SMatthew Dillon int nblock = 0;
106dea673e9SRodney W. Grimes
107c20dbeb6SEd Maste iflag = dflag = Cflag = 0;
108fb0e6accSGary Palmer strcpy(prefix, _PATH_PWD);
109dea673e9SRodney W. Grimes makeold = 0;
11079a1b8d9SGuido van Rooij username = NULL;
111ce2657c0SPhilippe Charnier oldfp = NULL;
112508aee96SDag-Erling Smørgrav while ((ch = getopt(argc, argv, "Cd:iNps:u:v")) != -1)
113dea673e9SRodney W. Grimes switch(ch) {
114d7f71209SWolfram Schneider case 'C': /* verify only */
115d7f71209SWolfram Schneider Cflag = 1;
116e2ab8bacSWolfram Schneider break;
117fb0e6accSGary Palmer case 'd':
1189906740eSWarner Losh dflag++;
11924412bd1SWarner Losh strlcpy(prefix, optarg, sizeof(prefix));
120fb0e6accSGary Palmer break;
1219906740eSWarner Losh case 'i':
1229906740eSWarner Losh iflag++;
1239906740eSWarner Losh break;
124508aee96SDag-Erling Smørgrav case 'N': /* do not wait for lock */
125508aee96SDag-Erling Smørgrav nblock = LOCK_NB; /* will fail if locked */
126508aee96SDag-Erling Smørgrav break;
127dea673e9SRodney W. Grimes case 'p': /* create V7 "file.orig" */
128dea673e9SRodney W. Grimes makeold = 1;
129dea673e9SRodney W. Grimes break;
130e6bb224eSPoul-Henning Kamp case 's': /* change default cachesize */
131e6bb224eSPoul-Henning Kamp openinfo.cachesize = atoi(optarg) * 1024 * 1024;
132e6bb224eSPoul-Henning Kamp break;
13379a1b8d9SGuido van Rooij case 'u': /* only update this record */
13479a1b8d9SGuido van Rooij username = optarg;
13579a1b8d9SGuido van Rooij break;
136dea673e9SRodney W. Grimes case 'v': /* backward compatible */
137dea673e9SRodney W. Grimes break;
138dea673e9SRodney W. Grimes default:
139dea673e9SRodney W. Grimes usage();
140dea673e9SRodney W. Grimes }
141dea673e9SRodney W. Grimes argc -= optind;
142dea673e9SRodney W. Grimes argv += optind;
143dea673e9SRodney W. Grimes
144ca7c8034SMartin Renters if (argc != 1 || (username && (*username == '+' || *username == '-')))
145dea673e9SRodney W. Grimes usage();
146dea673e9SRodney W. Grimes
147dea673e9SRodney W. Grimes /*
148dea673e9SRodney W. Grimes * This could be changed to allow the user to interrupt.
149dea673e9SRodney W. Grimes * Probably not worth the effort.
150dea673e9SRodney W. Grimes */
151dea673e9SRodney W. Grimes sigemptyset(&set);
152dea673e9SRodney W. Grimes sigaddset(&set, SIGTSTP);
153dea673e9SRodney W. Grimes sigaddset(&set, SIGHUP);
154dea673e9SRodney W. Grimes sigaddset(&set, SIGINT);
155dea673e9SRodney W. Grimes sigaddset(&set, SIGQUIT);
156dea673e9SRodney W. Grimes sigaddset(&set, SIGTERM);
157dea673e9SRodney W. Grimes (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
158dea673e9SRodney W. Grimes
159dea673e9SRodney W. Grimes /* We don't care what the user wants. */
160dea673e9SRodney W. Grimes (void)umask(0);
161dea673e9SRodney W. Grimes
162dea673e9SRodney W. Grimes pname = *argv;
163be648216SMatthew Dillon
164be648216SMatthew Dillon /*
165be648216SMatthew Dillon * Open and lock the original password file. We have to check
166be648216SMatthew Dillon * the hardlink count after we get the lock to handle any potential
167be648216SMatthew Dillon * unlink/rename race.
168be648216SMatthew Dillon *
169be648216SMatthew Dillon * This lock is necessary when someone runs pwd_mkdb manually, directly
170be648216SMatthew Dillon * on master.passwd, to handle the case where a user might try to
171be648216SMatthew Dillon * change his password while pwd_mkdb is running.
172be648216SMatthew Dillon */
173be648216SMatthew Dillon for (;;) {
174be648216SMatthew Dillon struct stat st;
175be648216SMatthew Dillon
176dea673e9SRodney W. Grimes if (!(fp = fopen(pname, "r")))
177dea673e9SRodney W. Grimes error(pname);
1789906740eSWarner Losh if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
179be648216SMatthew Dillon error("flock");
180be648216SMatthew Dillon if (fstat(fileno(fp), &st) < 0)
181be648216SMatthew Dillon error(pname);
182be648216SMatthew Dillon if (st.st_nlink != 0)
183be648216SMatthew Dillon break;
184be648216SMatthew Dillon fclose(fp);
185be648216SMatthew Dillon fp = NULL;
186be648216SMatthew Dillon }
187dea673e9SRodney W. Grimes
188e2ab8bacSWolfram Schneider /* check only if password database is valid */
189d7f71209SWolfram Schneider if (Cflag) {
19015344a56SBrian Somers while (scan(fp, &pwd))
19115344a56SBrian Somers if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
19215344a56SBrian Somers warnx("%s: username too long", pwd.pw_name);
19315344a56SBrian Somers exit(1);
19415344a56SBrian Somers }
195e2ab8bacSWolfram Schneider exit(0);
196e2ab8bacSWolfram Schneider }
197e2ab8bacSWolfram Schneider
198dea673e9SRodney W. Grimes /* Open the temporary insecure password database. */
199fb0e6accSGary Palmer (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
20079a1b8d9SGuido van Rooij (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
20179a1b8d9SGuido van Rooij if (username) {
202257ca75fSJacques Vidrine int use_version;
203257ca75fSJacques Vidrine
20479a1b8d9SGuido van Rooij (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
20579a1b8d9SGuido van Rooij (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
20679a1b8d9SGuido van Rooij
20779a1b8d9SGuido van Rooij clean = FILE_INSECURE;
20879a1b8d9SGuido van Rooij cp(buf2, buf, PERM_INSECURE);
20979a1b8d9SGuido van Rooij dp = dbopen(buf,
21095bcaf51SDavid Malone O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
21179a1b8d9SGuido van Rooij if (dp == NULL)
21279a1b8d9SGuido van Rooij error(buf);
21379a1b8d9SGuido van Rooij
21479a1b8d9SGuido van Rooij clean = FILE_SECURE;
21579a1b8d9SGuido van Rooij cp(sbuf2, sbuf, PERM_SECURE);
21679a1b8d9SGuido van Rooij sdp = dbopen(sbuf,
21795bcaf51SDavid Malone O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
21879a1b8d9SGuido van Rooij if (sdp == NULL)
21979a1b8d9SGuido van Rooij error(sbuf);
22079a1b8d9SGuido van Rooij
22179a1b8d9SGuido van Rooij /*
22279a1b8d9SGuido van Rooij * Do some trouble to check if we should store this users
22379a1b8d9SGuido van Rooij * uid. Don't use getpwnam/getpwuid as that interferes
22479a1b8d9SGuido van Rooij * with NIS.
22579a1b8d9SGuido van Rooij */
22679a1b8d9SGuido van Rooij pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
22779a1b8d9SGuido van Rooij if (!pw_db)
22879a1b8d9SGuido van Rooij error(_MP_DB);
229257ca75fSJacques Vidrine
230257ca75fSJacques Vidrine key.data = verskey;
231257ca75fSJacques Vidrine key.size = sizeof(verskey)-1;
232257ca75fSJacques Vidrine if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
233257ca75fSJacques Vidrine use_version = *(unsigned char *)data.data;
234257ca75fSJacques Vidrine else
235257ca75fSJacques Vidrine use_version = 3;
236257ca75fSJacques Vidrine buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
23779a1b8d9SGuido van Rooij len = strlen(username);
23879a1b8d9SGuido van Rooij
23979a1b8d9SGuido van Rooij /* Only check that username fits in buffer */
24079a1b8d9SGuido van Rooij memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
24179a1b8d9SGuido van Rooij key.data = (u_char *)buf;
24279a1b8d9SGuido van Rooij key.size = len + 1;
24379a1b8d9SGuido van Rooij if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
24479a1b8d9SGuido van Rooij p = (char *)data.data;
24579a1b8d9SGuido van Rooij
24679a1b8d9SGuido van Rooij /* jump over pw_name and pw_passwd, to get to pw_uid */
2476151b317SBruce Evans while (*p++)
2486151b317SBruce Evans ;
2496151b317SBruce Evans while (*p++)
2506151b317SBruce Evans ;
25179a1b8d9SGuido van Rooij
252257ca75fSJacques Vidrine buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
253257ca75fSJacques Vidrine memmove(buf + 1, p, sizeof(store));
25479a1b8d9SGuido van Rooij key.data = (u_char *)buf;
255257ca75fSJacques Vidrine key.size = sizeof(store) + 1;
25679a1b8d9SGuido van Rooij
25779a1b8d9SGuido van Rooij if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
25879a1b8d9SGuido van Rooij /* First field of data.data holds pw_pwname */
25979a1b8d9SGuido van Rooij if (!strcmp(data.data, username))
26079a1b8d9SGuido van Rooij methoduid = 0;
26179a1b8d9SGuido van Rooij else
26279a1b8d9SGuido van Rooij methoduid = R_NOOVERWRITE;
26379a1b8d9SGuido van Rooij } else {
26479a1b8d9SGuido van Rooij methoduid = R_NOOVERWRITE;
26579a1b8d9SGuido van Rooij }
26679a1b8d9SGuido van Rooij } else {
26779a1b8d9SGuido van Rooij methoduid = R_NOOVERWRITE;
26879a1b8d9SGuido van Rooij }
2691aa9837cSGuido van Rooij if ((pw_db->close)(pw_db))
2701aa9837cSGuido van Rooij error("close pw_db");
27179a1b8d9SGuido van Rooij method = 0;
27279a1b8d9SGuido van Rooij } else {
273dea673e9SRodney W. Grimes dp = dbopen(buf,
27495bcaf51SDavid Malone O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
275dea673e9SRodney W. Grimes if (dp == NULL)
276dea673e9SRodney W. Grimes error(buf);
277dea673e9SRodney W. Grimes clean = FILE_INSECURE;
278dea673e9SRodney W. Grimes
27979a1b8d9SGuido van Rooij sdp = dbopen(sbuf,
28095bcaf51SDavid Malone O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
28179a1b8d9SGuido van Rooij if (sdp == NULL)
28279a1b8d9SGuido van Rooij error(sbuf);
28379a1b8d9SGuido van Rooij clean = FILE_SECURE;
28479a1b8d9SGuido van Rooij
28579a1b8d9SGuido van Rooij method = R_NOOVERWRITE;
28679a1b8d9SGuido van Rooij methoduid = R_NOOVERWRITE;
28779a1b8d9SGuido van Rooij }
28879a1b8d9SGuido van Rooij
289dea673e9SRodney W. Grimes /*
290dea673e9SRodney W. Grimes * Open file for old password file. Minor trickiness -- don't want to
291dea673e9SRodney W. Grimes * chance the file already existing, since someone (stupidly) might
292dea673e9SRodney W. Grimes * still be using this for permission checking. So, open it first and
293dea673e9SRodney W. Grimes * fdopen the resulting fd. The resulting file should be readable by
294dea673e9SRodney W. Grimes * everyone.
295dea673e9SRodney W. Grimes */
296dea673e9SRodney W. Grimes if (makeold) {
297dea673e9SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
298dea673e9SRodney W. Grimes if ((tfd = open(buf,
299dea673e9SRodney W. Grimes O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
300dea673e9SRodney W. Grimes error(buf);
301dea673e9SRodney W. Grimes if ((oldfp = fdopen(tfd, "w")) == NULL)
302dea673e9SRodney W. Grimes error(buf);
303dea673e9SRodney W. Grimes clean = FILE_ORIG;
304dea673e9SRodney W. Grimes }
305dea673e9SRodney W. Grimes
306dea673e9SRodney W. Grimes /*
307dea673e9SRodney W. Grimes * The databases actually contain three copies of the original data.
308dea673e9SRodney W. Grimes * Each password file entry is converted into a rough approximation
309dea673e9SRodney W. Grimes * of a ``struct passwd'', with the strings placed inline. This
310dea673e9SRodney W. Grimes * object is then stored as the data for three separate keys. The
311dea673e9SRodney W. Grimes * first key * is the pw_name field prepended by the _PW_KEYBYNAME
312dea673e9SRodney W. Grimes * character. The second key is the pw_uid field prepended by the
313dea673e9SRodney W. Grimes * _PW_KEYBYUID character. The third key is the line number in the
314dea673e9SRodney W. Grimes * original file prepended by the _PW_KEYBYNUM character. (The special
315dea673e9SRodney W. Grimes * characters are prepended to ensure that the keys do not collide.)
316dea673e9SRodney W. Grimes */
31705f98035SJacques Vidrine /* In order to transition this file into a machine-independent
31805f98035SJacques Vidrine * form, we have to change the format of entries. However, since
31905f98035SJacques Vidrine * older binaries will still expect the old MD format entries, we
320257ca75fSJacques Vidrine * create those as usual and use versioned tags for the new entries.
321257ca75fSJacques Vidrine */
322257ca75fSJacques Vidrine if (username == NULL) {
323257ca75fSJacques Vidrine /* Do not add the VERSION tag when updating a single
324257ca75fSJacques Vidrine * user. When operating on `old format' databases, this
325257ca75fSJacques Vidrine * would result in applications `seeing' only the updated
326257ca75fSJacques Vidrine * entries.
32705f98035SJacques Vidrine */
32805f98035SJacques Vidrine key.data = verskey;
32905f98035SJacques Vidrine key.size = sizeof(verskey)-1;
33005f98035SJacques Vidrine data.data = &version;
33105f98035SJacques Vidrine data.size = 1;
33205f98035SJacques Vidrine if ((dp->put)(dp, &key, &data, 0) == -1)
33305f98035SJacques Vidrine error("put");
334daa3baebSMarcelo Araujo if ((sdp->put)(sdp, &key, &data, 0) == -1)
33505f98035SJacques Vidrine error("put");
336257ca75fSJacques Vidrine }
337c909243fSGordon Tetlow ypcnt = 0;
338dea673e9SRodney W. Grimes data.data = (u_char *)buf;
33979a1b8d9SGuido van Rooij sdata.data = (u_char *)sbuf;
340dea673e9SRodney W. Grimes key.data = (u_char *)tbuf;
341dea673e9SRodney W. Grimes for (cnt = 1; scan(fp, &pwd); ++cnt) {
34250819461SDmitrij Tejblum if (!is_comment &&
343c909243fSGordon Tetlow (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
344ab5f8d28SGarrett Wollman yp_enabled = 1;
345c909243fSGordon Tetlow ypcnt++;
346c909243fSGordon Tetlow }
34717b09acaSFoxfair Hu if (is_comment)
34817b09acaSFoxfair Hu --cnt;
3490c2b3c31SPeter Wemm #define COMPACT(e) t = e; while ((*p++ = *t++));
35005f98035SJacques Vidrine #define SCALAR(e) store = htonl((uint32_t)(e)); \
35105f98035SJacques Vidrine memmove(p, &store, sizeof(store)); \
35205f98035SJacques Vidrine p += sizeof(store);
353e2db8d37SRuslan Ermilov #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
354e2db8d37SRuslan Ermilov memmove(p, &store, sizeof(store)); \
355e2db8d37SRuslan Ermilov p += sizeof(store);
356e2db8d37SRuslan Ermilov #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
357e2db8d37SRuslan Ermilov (uint32_t)(e) : \
358e2db8d37SRuslan Ermilov bswap32((uint32_t)(e)))
35950819461SDmitrij Tejblum if (!is_comment &&
360eb0abfccSWolfram Schneider (!username || (strcmp(username, pwd.pw_name) == 0))) {
361dea673e9SRodney W. Grimes /* Create insecure data. */
362dea673e9SRodney W. Grimes p = buf;
363dea673e9SRodney W. Grimes COMPACT(pwd.pw_name);
364dea673e9SRodney W. Grimes COMPACT("*");
36505f98035SJacques Vidrine SCALAR(pwd.pw_uid);
36605f98035SJacques Vidrine SCALAR(pwd.pw_gid);
36705f98035SJacques Vidrine SCALAR(pwd.pw_change);
36805f98035SJacques Vidrine COMPACT(pwd.pw_class);
36905f98035SJacques Vidrine COMPACT(pwd.pw_gecos);
37005f98035SJacques Vidrine COMPACT(pwd.pw_dir);
37105f98035SJacques Vidrine COMPACT(pwd.pw_shell);
37205f98035SJacques Vidrine SCALAR(pwd.pw_expire);
37305f98035SJacques Vidrine SCALAR(pwd.pw_fields);
37405f98035SJacques Vidrine data.size = p - buf;
37505f98035SJacques Vidrine
37605f98035SJacques Vidrine /* Create secure data. */
37705f98035SJacques Vidrine p = sbuf;
37805f98035SJacques Vidrine COMPACT(pwd.pw_name);
37905f98035SJacques Vidrine COMPACT(pwd.pw_passwd);
38005f98035SJacques Vidrine SCALAR(pwd.pw_uid);
38105f98035SJacques Vidrine SCALAR(pwd.pw_gid);
38205f98035SJacques Vidrine SCALAR(pwd.pw_change);
38305f98035SJacques Vidrine COMPACT(pwd.pw_class);
38405f98035SJacques Vidrine COMPACT(pwd.pw_gecos);
38505f98035SJacques Vidrine COMPACT(pwd.pw_dir);
38605f98035SJacques Vidrine COMPACT(pwd.pw_shell);
38705f98035SJacques Vidrine SCALAR(pwd.pw_expire);
38805f98035SJacques Vidrine SCALAR(pwd.pw_fields);
38905f98035SJacques Vidrine sdata.size = p - sbuf;
39005f98035SJacques Vidrine
39105f98035SJacques Vidrine /* Store insecure by name. */
392b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
39305f98035SJacques Vidrine len = strlen(pwd.pw_name);
39405f98035SJacques Vidrine memmove(tbuf + 1, pwd.pw_name, len);
39505f98035SJacques Vidrine key.size = len + 1;
39605f98035SJacques Vidrine if ((dp->put)(dp, &key, &data, method) == -1)
39705f98035SJacques Vidrine error("put");
39805f98035SJacques Vidrine
39905f98035SJacques Vidrine /* Store insecure by number. */
400b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
40105f98035SJacques Vidrine store = htonl(cnt);
40205f98035SJacques Vidrine memmove(tbuf + 1, &store, sizeof(store));
40305f98035SJacques Vidrine key.size = sizeof(store) + 1;
40405f98035SJacques Vidrine if ((dp->put)(dp, &key, &data, method) == -1)
40505f98035SJacques Vidrine error("put");
40605f98035SJacques Vidrine
40705f98035SJacques Vidrine /* Store insecure by uid. */
408b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
40905f98035SJacques Vidrine store = htonl(pwd.pw_uid);
41005f98035SJacques Vidrine memmove(tbuf + 1, &store, sizeof(store));
41105f98035SJacques Vidrine key.size = sizeof(store) + 1;
41205f98035SJacques Vidrine if ((dp->put)(dp, &key, &data, methoduid) == -1)
41305f98035SJacques Vidrine error("put");
41405f98035SJacques Vidrine
41505f98035SJacques Vidrine /* Store secure by name. */
416b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
41705f98035SJacques Vidrine len = strlen(pwd.pw_name);
41805f98035SJacques Vidrine memmove(tbuf + 1, pwd.pw_name, len);
41905f98035SJacques Vidrine key.size = len + 1;
42005f98035SJacques Vidrine if ((sdp->put)(sdp, &key, &sdata, method) == -1)
42105f98035SJacques Vidrine error("put");
42205f98035SJacques Vidrine
42305f98035SJacques Vidrine /* Store secure by number. */
424b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
42505f98035SJacques Vidrine store = htonl(cnt);
42605f98035SJacques Vidrine memmove(tbuf + 1, &store, sizeof(store));
42705f98035SJacques Vidrine key.size = sizeof(store) + 1;
42805f98035SJacques Vidrine if ((sdp->put)(sdp, &key, &sdata, method) == -1)
42905f98035SJacques Vidrine error("put");
43005f98035SJacques Vidrine
43105f98035SJacques Vidrine /* Store secure by uid. */
432b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
43305f98035SJacques Vidrine store = htonl(pwd.pw_uid);
43405f98035SJacques Vidrine memmove(tbuf + 1, &store, sizeof(store));
43505f98035SJacques Vidrine key.size = sizeof(store) + 1;
43605f98035SJacques Vidrine if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
43705f98035SJacques Vidrine error("put");
43805f98035SJacques Vidrine
43905f98035SJacques Vidrine /* Store insecure and secure special plus and special minus */
44005f98035SJacques Vidrine if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
441b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
44205f98035SJacques Vidrine store = htonl(ypcnt);
44305f98035SJacques Vidrine memmove(tbuf + 1, &store, sizeof(store));
44405f98035SJacques Vidrine key.size = sizeof(store) + 1;
44505f98035SJacques Vidrine if ((dp->put)(dp, &key, &data, method) == -1)
44605f98035SJacques Vidrine error("put");
44705f98035SJacques Vidrine if ((sdp->put)(sdp, &key, &sdata, method) == -1)
44805f98035SJacques Vidrine error("put");
44905f98035SJacques Vidrine }
4503e08b16eSXin LI }
4510deb25bdSAndre Albsmeier /*
4520deb25bdSAndre Albsmeier * Create original style password file entry.
4530deb25bdSAndre Albsmeier *
4540deb25bdSAndre Albsmeier * Don't copy comments since this could reveal encrypted
4550deb25bdSAndre Albsmeier * passwords if entries have been simply commented out
4560deb25bdSAndre Albsmeier * in master.passwd.
4570deb25bdSAndre Albsmeier */
4580deb25bdSAndre Albsmeier if (makeold && !is_comment) {
4598839484bSBill Paul char uidstr[20];
4608839484bSBill Paul char gidstr[20];
4618839484bSBill Paul
462cd7b8d78SPaul Richards snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
463cd7b8d78SPaul Richards snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
4648839484bSBill Paul
4651aa9837cSGuido van Rooij if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
4668839484bSBill Paul pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
4678839484bSBill Paul pwd.pw_fields & _PWF_GID ? gidstr : "",
468cf564323SGuido van Rooij pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
4691aa9837cSGuido van Rooij error("write old");
4708839484bSBill Paul }
47179a1b8d9SGuido van Rooij }
47228ca3091SGarrett Wollman /* If YP enabled, set flag. */
47328ca3091SGarrett Wollman if (yp_enabled) {
474ab5f8d28SGarrett Wollman buf[0] = yp_enabled + 2;
475ab5f8d28SGarrett Wollman data.size = 1;
476b4603f3dSJacques Vidrine key.size = 1;
477b4603f3dSJacques Vidrine tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
478b4603f3dSJacques Vidrine if ((dp->put)(dp, &key, &data, method) == -1)
479b4603f3dSJacques Vidrine error("put");
480b4603f3dSJacques Vidrine if ((sdp->put)(sdp, &key, &data, method) == -1)
481b4603f3dSJacques Vidrine error("put");
4823e08b16eSXin LI }
4836ec733f4SBill Paul
4847256f6b3SJonathan Lemon if ((dp->close)(dp) == -1)
4857256f6b3SJonathan Lemon error("close");
4867256f6b3SJonathan Lemon if ((sdp->close)(sdp) == -1)
4877256f6b3SJonathan Lemon error("close");
48879a1b8d9SGuido van Rooij if (makeold) {
48979a1b8d9SGuido van Rooij (void)fflush(oldfp);
4901aa9837cSGuido van Rooij if (fclose(oldfp) == EOF)
4911aa9837cSGuido van Rooij error("close old");
49279a1b8d9SGuido van Rooij }
493dea673e9SRodney W. Grimes
494dea673e9SRodney W. Grimes /* Set master.passwd permissions, in case caller forgot. */
495dea673e9SRodney W. Grimes (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
496dea673e9SRodney W. Grimes
497dea673e9SRodney W. Grimes /* Install as the real password files. */
498fb0e6accSGary Palmer (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
499fb0e6accSGary Palmer (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
500fb0e6accSGary Palmer mv(buf, buf2);
501fb0e6accSGary Palmer (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
502fb0e6accSGary Palmer (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
503fb0e6accSGary Palmer mv(buf, buf2);
504dea673e9SRodney W. Grimes if (makeold) {
505fb0e6accSGary Palmer (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
506dea673e9SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
507fb0e6accSGary Palmer mv(buf, buf2);
508dea673e9SRodney W. Grimes }
509dea673e9SRodney W. Grimes /*
510dea673e9SRodney W. Grimes * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
511dea673e9SRodney W. Grimes * all use flock(2) on it to block other incarnations of themselves.
512dea673e9SRodney W. Grimes * The rename means that everything is unlocked, as the original file
513dea673e9SRodney W. Grimes * can no longer be accessed.
514dea673e9SRodney W. Grimes */
515fb0e6accSGary Palmer (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
516fb0e6accSGary Palmer mv(pname, buf);
517be648216SMatthew Dillon
518be648216SMatthew Dillon /*
519be648216SMatthew Dillon * Close locked password file after rename()
520be648216SMatthew Dillon */
521be648216SMatthew Dillon if (fclose(fp) == EOF)
522be648216SMatthew Dillon error("close fp");
523be648216SMatthew Dillon
524dea673e9SRodney W. Grimes exit(0);
525dea673e9SRodney W. Grimes }
526dea673e9SRodney W. Grimes
527dea673e9SRodney W. Grimes int
scan(FILE * fp,struct passwd * pw)528bf94610cSStefan Farfeleder scan(FILE *fp, struct passwd *pw)
529dea673e9SRodney W. Grimes {
530dea673e9SRodney W. Grimes static int lcnt;
531ff900dedSDima Dorfman size_t len;
532dea673e9SRodney W. Grimes char *p;
533dea673e9SRodney W. Grimes
534ff900dedSDima Dorfman p = fgetln(fp, &len);
535ff900dedSDima Dorfman if (p == NULL)
536dea673e9SRodney W. Grimes return (0);
537dea673e9SRodney W. Grimes ++lcnt;
538dea673e9SRodney W. Grimes /*
539dea673e9SRodney W. Grimes * ``... if I swallow anything evil, put your fingers down my
540dea673e9SRodney W. Grimes * throat...''
541dea673e9SRodney W. Grimes * -- The Who
542dea673e9SRodney W. Grimes */
543ff900dedSDima Dorfman if (len > 0 && p[len - 1] == '\n')
544ff900dedSDima Dorfman len--;
545ff900dedSDima Dorfman if (len >= sizeof(line) - 1) {
546524a875cSDima Dorfman warnx("line #%d too long", lcnt);
547dea673e9SRodney W. Grimes goto fmt;
548dea673e9SRodney W. Grimes }
549ff900dedSDima Dorfman memcpy(line, p, len);
550ff900dedSDima Dorfman line[len] = '\0';
551eb0abfccSWolfram Schneider
552eb0abfccSWolfram Schneider /*
553eb0abfccSWolfram Schneider * Ignore comments: ^[ \t]*#
554eb0abfccSWolfram Schneider */
555eb0abfccSWolfram Schneider for (p = line; *p != '\0'; p++)
556eb0abfccSWolfram Schneider if (*p != ' ' && *p != '\t')
557eb0abfccSWolfram Schneider break;
558eb0abfccSWolfram Schneider if (*p == '#' || *p == '\0') {
55950819461SDmitrij Tejblum is_comment = 1;
560eb0abfccSWolfram Schneider return(1);
561eb0abfccSWolfram Schneider } else
56250819461SDmitrij Tejblum is_comment = 0;
563eb0abfccSWolfram Schneider
564248aee62SJacques Vidrine if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
565dea673e9SRodney W. Grimes warnx("at line #%d", lcnt);
566dea673e9SRodney W. Grimes fmt: errno = EFTYPE; /* XXX */
567dea673e9SRodney W. Grimes error(pname);
568dea673e9SRodney W. Grimes }
569dea673e9SRodney W. Grimes
570dea673e9SRodney W. Grimes return (1);
571dea673e9SRodney W. Grimes }
572dea673e9SRodney W. Grimes
573dea673e9SRodney W. Grimes void
cp(char * from,char * to,mode_t mode)574bf94610cSStefan Farfeleder cp(char *from, char *to, mode_t mode)
57579a1b8d9SGuido van Rooij {
57679a1b8d9SGuido van Rooij static char buf[MAXBSIZE];
57779a1b8d9SGuido van Rooij int from_fd, rcount, to_fd, wcount;
57879a1b8d9SGuido van Rooij
57979a1b8d9SGuido van Rooij if ((from_fd = open(from, O_RDONLY, 0)) < 0)
58079a1b8d9SGuido van Rooij error(from);
58179a1b8d9SGuido van Rooij if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
58279a1b8d9SGuido van Rooij error(to);
58379a1b8d9SGuido van Rooij while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
58479a1b8d9SGuido van Rooij wcount = write(to_fd, buf, rcount);
58579a1b8d9SGuido van Rooij if (rcount != wcount || wcount == -1) {
58679a1b8d9SGuido van Rooij int sverrno = errno;
58779a1b8d9SGuido van Rooij
58879a1b8d9SGuido van Rooij (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
58979a1b8d9SGuido van Rooij errno = sverrno;
59079a1b8d9SGuido van Rooij error(buf);
59179a1b8d9SGuido van Rooij }
59279a1b8d9SGuido van Rooij }
59379a1b8d9SGuido van Rooij if (rcount < 0) {
59479a1b8d9SGuido van Rooij int sverrno = errno;
59579a1b8d9SGuido van Rooij
59679a1b8d9SGuido van Rooij (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
59779a1b8d9SGuido van Rooij errno = sverrno;
59879a1b8d9SGuido van Rooij error(buf);
59979a1b8d9SGuido van Rooij }
60079a1b8d9SGuido van Rooij }
60179a1b8d9SGuido van Rooij
60279a1b8d9SGuido van Rooij
60379a1b8d9SGuido van Rooij void
mv(char * from,char * to)604bf94610cSStefan Farfeleder mv(char *from, char *to)
605dea673e9SRodney W. Grimes {
606dea673e9SRodney W. Grimes char buf[MAXPATHLEN];
607d32a66b2SRenato Botelho char *to_dir;
608d32a66b2SRenato Botelho int to_dir_fd = -1;
609dea673e9SRodney W. Grimes
610d32a66b2SRenato Botelho /*
611d32a66b2SRenato Botelho * Make sure file is safe on disk. To improve performance we will call
612d32a66b2SRenato Botelho * fsync() to the directory where file lies
613d32a66b2SRenato Botelho */
614d32a66b2SRenato Botelho if (rename(from, to) != 0 ||
615d32a66b2SRenato Botelho (to_dir = dirname(to)) == NULL ||
616d32a66b2SRenato Botelho (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
617d32a66b2SRenato Botelho fsync(to_dir_fd) != 0) {
618dea673e9SRodney W. Grimes int sverrno = errno;
619dea673e9SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
620dea673e9SRodney W. Grimes errno = sverrno;
621d32a66b2SRenato Botelho if (to_dir_fd != -1)
622d32a66b2SRenato Botelho close(to_dir_fd);
623dea673e9SRodney W. Grimes error(buf);
624dea673e9SRodney W. Grimes }
625d32a66b2SRenato Botelho
626d32a66b2SRenato Botelho if (to_dir_fd != -1)
627d32a66b2SRenato Botelho close(to_dir_fd);
628dea673e9SRodney W. Grimes }
629dea673e9SRodney W. Grimes
630dea673e9SRodney W. Grimes void
error(const char * name)631bf94610cSStefan Farfeleder error(const char *name)
632dea673e9SRodney W. Grimes {
633dea673e9SRodney W. Grimes
6348b76e1d7SPhilippe Charnier warn("%s", name);
635dea673e9SRodney W. Grimes cleanup();
636dea673e9SRodney W. Grimes exit(1);
637dea673e9SRodney W. Grimes }
638dea673e9SRodney W. Grimes
639dea673e9SRodney W. Grimes void
cleanup(void)640bf94610cSStefan Farfeleder cleanup(void)
641dea673e9SRodney W. Grimes {
642dea673e9SRodney W. Grimes char buf[MAXPATHLEN];
643dea673e9SRodney W. Grimes
644dea673e9SRodney W. Grimes switch(clean) {
645dea673e9SRodney W. Grimes case FILE_ORIG:
646dea673e9SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
647dea673e9SRodney W. Grimes (void)unlink(buf);
648dea673e9SRodney W. Grimes /* FALLTHROUGH */
649dea673e9SRodney W. Grimes case FILE_SECURE:
650fb0e6accSGary Palmer (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
651dea673e9SRodney W. Grimes (void)unlink(buf);
652dea673e9SRodney W. Grimes /* FALLTHROUGH */
653dea673e9SRodney W. Grimes case FILE_INSECURE:
654fb0e6accSGary Palmer (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
655dea673e9SRodney W. Grimes (void)unlink(buf);
656dea673e9SRodney W. Grimes }
657dea673e9SRodney W. Grimes }
658dea673e9SRodney W. Grimes
6598b76e1d7SPhilippe Charnier static void
usage(void)660bf94610cSStefan Farfeleder usage(void)
661dea673e9SRodney W. Grimes {
662dea673e9SRodney W. Grimes
6638b76e1d7SPhilippe Charnier (void)fprintf(stderr,
664508aee96SDag-Erling Smørgrav "usage: pwd_mkdb [-CiNp] [-d directory] [-s cachesize] [-u username] file\n");
665dea673e9SRodney W. Grimes exit(1);
666dea673e9SRodney W. Grimes }
667