2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1991, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/endian.h>
48 #include <arpa/inet.h>
67 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
68 #define PERM_SECURE (S_IRUSR|S_IWUSR)
69 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
70 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
72 static HASHINFO openinfo = {
76 2048 * 1024, /* cachesize */
78 BIG_ENDIAN /* lorder */
81 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
82 static struct passwd pwd; /* password structure */
83 static char *pname; /* password file name */
84 static char prefix[MAXPATHLEN];
86 static int is_comment; /* flag for comments */
87 static char line[LINE_MAX];
90 void error(const char *);
91 void cp(char *, char *, mode_t mode);
92 void mv(char *, char *);
93 int scan(FILE *, struct passwd *);
94 static void usage(void);
97 main(int argc, char *argv[])
99 static char verskey[] = _PWD_VERSION_KEY;
100 char version = _PWD_CURRENT_VERSION;
101 DB *dp, *sdp, *pw_db;
102 DBT data, sdata, key;
105 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
110 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
111 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
112 char buf2[MAXPATHLEN];
113 char sbuf2[MAXPATHLEN];
115 u_int method, methoduid;
116 int Cflag, dflag, iflag;
119 iflag = dflag = Cflag = 0;
120 strcpy(prefix, _PATH_PWD);
124 while ((ch = getopt(argc, argv, "Cd:iNps:u:v")) != -1)
126 case 'C': /* verify only */
131 strlcpy(prefix, optarg, sizeof(prefix));
136 case 'N': /* do not wait for lock */
137 nblock = LOCK_NB; /* will fail if locked */
139 case 'p': /* create V7 "file.orig" */
142 case 's': /* change default cachesize */
143 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
145 case 'u': /* only update this record */
148 case 'v': /* backward compatible */
156 if (argc != 1 || (username && (*username == '+' || *username == '-')))
160 * This could be changed to allow the user to interrupt.
161 * Probably not worth the effort.
164 sigaddset(&set, SIGTSTP);
165 sigaddset(&set, SIGHUP);
166 sigaddset(&set, SIGINT);
167 sigaddset(&set, SIGQUIT);
168 sigaddset(&set, SIGTERM);
169 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
171 /* We don't care what the user wants. */
177 * Open and lock the original password file. We have to check
178 * the hardlink count after we get the lock to handle any potential
179 * unlink/rename race.
181 * This lock is necessary when someone runs pwd_mkdb manually, directly
182 * on master.passwd, to handle the case where a user might try to
183 * change his password while pwd_mkdb is running.
188 if (!(fp = fopen(pname, "r")))
190 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
192 if (fstat(fileno(fp), &st) < 0)
194 if (st.st_nlink != 0)
200 /* check only if password database is valid */
202 while (scan(fp, &pwd))
203 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
204 warnx("%s: username too long", pwd.pw_name);
210 /* Open the temporary insecure password database. */
211 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
212 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
216 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
217 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
219 clean = FILE_INSECURE;
220 cp(buf2, buf, PERM_INSECURE);
222 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
227 cp(sbuf2, sbuf, PERM_SECURE);
229 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
234 * Do some trouble to check if we should store this users
235 * uid. Don't use getpwnam/getpwuid as that interferes
238 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
243 key.size = sizeof(verskey)-1;
244 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
245 use_version = *(unsigned char *)data.data;
248 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
249 len = strlen(username);
251 /* Only check that username fits in buffer */
252 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
253 key.data = (u_char *)buf;
255 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
256 p = (char *)data.data;
258 /* jump over pw_name and pw_passwd, to get to pw_uid */
264 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
265 memmove(buf + 1, p, sizeof(store));
266 key.data = (u_char *)buf;
267 key.size = sizeof(store) + 1;
269 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
270 /* First field of data.data holds pw_pwname */
271 if (!strcmp(data.data, username))
274 methoduid = R_NOOVERWRITE;
276 methoduid = R_NOOVERWRITE;
279 methoduid = R_NOOVERWRITE;
281 if ((pw_db->close)(pw_db))
282 error("close pw_db");
286 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
289 clean = FILE_INSECURE;
292 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
297 method = R_NOOVERWRITE;
298 methoduid = R_NOOVERWRITE;
302 * Open file for old password file. Minor trickiness -- don't want to
303 * chance the file already existing, since someone (stupidly) might
304 * still be using this for permission checking. So, open it first and
305 * fdopen the resulting fd. The resulting file should be readable by
309 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
311 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
313 if ((oldfp = fdopen(tfd, "w")) == NULL)
319 * The databases actually contain three copies of the original data.
320 * Each password file entry is converted into a rough approximation
321 * of a ``struct passwd'', with the strings placed inline. This
322 * object is then stored as the data for three separate keys. The
323 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
324 * character. The second key is the pw_uid field prepended by the
325 * _PW_KEYBYUID character. The third key is the line number in the
326 * original file prepended by the _PW_KEYBYNUM character. (The special
327 * characters are prepended to ensure that the keys do not collide.)
329 /* In order to transition this file into a machine-independent
330 * form, we have to change the format of entries. However, since
331 * older binaries will still expect the old MD format entries, we
332 * create those as usual and use versioned tags for the new entries.
334 if (username == NULL) {
335 /* Do not add the VERSION tag when updating a single
336 * user. When operating on `old format' databases, this
337 * would result in applications `seeing' only the updated
341 key.size = sizeof(verskey)-1;
342 data.data = &version;
344 if ((dp->put)(dp, &key, &data, 0) == -1)
346 if ((sdp->put)(sdp, &key, &data, 0) == -1)
350 data.data = (u_char *)buf;
351 sdata.data = (u_char *)sbuf;
352 key.data = (u_char *)tbuf;
353 for (cnt = 1; scan(fp, &pwd); ++cnt) {
355 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
361 #define COMPACT(e) t = e; while ((*p++ = *t++));
362 #define SCALAR(e) store = htonl((uint32_t)(e)); \
363 memmove(p, &store, sizeof(store)); \
365 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
366 memmove(p, &store, sizeof(store)); \
368 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
370 bswap32((uint32_t)(e)))
372 (!username || (strcmp(username, pwd.pw_name) == 0))) {
373 /* Create insecure data. */
375 COMPACT(pwd.pw_name);
379 SCALAR(pwd.pw_change);
380 COMPACT(pwd.pw_class);
381 COMPACT(pwd.pw_gecos);
383 COMPACT(pwd.pw_shell);
384 SCALAR(pwd.pw_expire);
385 SCALAR(pwd.pw_fields);
388 /* Create secure data. */
390 COMPACT(pwd.pw_name);
391 COMPACT(pwd.pw_passwd);
394 SCALAR(pwd.pw_change);
395 COMPACT(pwd.pw_class);
396 COMPACT(pwd.pw_gecos);
398 COMPACT(pwd.pw_shell);
399 SCALAR(pwd.pw_expire);
400 SCALAR(pwd.pw_fields);
401 sdata.size = p - sbuf;
403 /* Store insecure by name. */
404 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
405 len = strlen(pwd.pw_name);
406 memmove(tbuf + 1, pwd.pw_name, len);
408 if ((dp->put)(dp, &key, &data, method) == -1)
411 /* Store insecure by number. */
412 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
414 memmove(tbuf + 1, &store, sizeof(store));
415 key.size = sizeof(store) + 1;
416 if ((dp->put)(dp, &key, &data, method) == -1)
419 /* Store insecure by uid. */
420 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
421 store = htonl(pwd.pw_uid);
422 memmove(tbuf + 1, &store, sizeof(store));
423 key.size = sizeof(store) + 1;
424 if ((dp->put)(dp, &key, &data, methoduid) == -1)
427 /* Store secure by name. */
428 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
429 len = strlen(pwd.pw_name);
430 memmove(tbuf + 1, pwd.pw_name, len);
432 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
435 /* Store secure by number. */
436 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
438 memmove(tbuf + 1, &store, sizeof(store));
439 key.size = sizeof(store) + 1;
440 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
443 /* Store secure by uid. */
444 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
445 store = htonl(pwd.pw_uid);
446 memmove(tbuf + 1, &store, sizeof(store));
447 key.size = sizeof(store) + 1;
448 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
451 /* Store insecure and secure special plus and special minus */
452 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
453 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
454 store = htonl(ypcnt);
455 memmove(tbuf + 1, &store, sizeof(store));
456 key.size = sizeof(store) + 1;
457 if ((dp->put)(dp, &key, &data, method) == -1)
459 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
464 * Create original style password file entry.
466 * Don't copy comments since this could reveal encrypted
467 * passwords if entries have been simply commented out
470 if (makeold && !is_comment) {
474 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
475 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
477 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
478 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
479 pwd.pw_fields & _PWF_GID ? gidstr : "",
480 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
484 /* If YP enabled, set flag. */
486 buf[0] = yp_enabled + 2;
489 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
490 if ((dp->put)(dp, &key, &data, method) == -1)
492 if ((sdp->put)(sdp, &key, &data, method) == -1)
496 if ((dp->close)(dp) == -1)
498 if ((sdp->close)(sdp) == -1)
502 if (fclose(oldfp) == EOF)
506 /* Set master.passwd permissions, in case caller forgot. */
507 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
509 /* Install as the real password files. */
510 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
511 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
513 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
514 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
517 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
518 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
522 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
523 * all use flock(2) on it to block other incarnations of themselves.
524 * The rename means that everything is unlocked, as the original file
525 * can no longer be accessed.
527 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
531 * Close locked password file after rename()
533 if (fclose(fp) == EOF)
540 scan(FILE *fp, struct passwd *pw)
546 p = fgetln(fp, &len);
551 * ``... if I swallow anything evil, put your fingers down my
555 if (len > 0 && p[len - 1] == '\n')
557 if (len >= sizeof(line) - 1) {
558 warnx("line #%d too long", lcnt);
561 memcpy(line, p, len);
565 * Ignore comments: ^[ \t]*#
567 for (p = line; *p != '\0'; p++)
568 if (*p != ' ' && *p != '\t')
570 if (*p == '#' || *p == '\0') {
576 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
577 warnx("at line #%d", lcnt);
578 fmt: errno = EFTYPE; /* XXX */
586 cp(char *from, char *to, mode_t mode)
588 static char buf[MAXBSIZE];
589 int from_fd, rcount, to_fd, wcount;
591 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
593 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
595 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
596 wcount = write(to_fd, buf, rcount);
597 if (rcount != wcount || wcount == -1) {
600 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
608 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
616 mv(char *from, char *to)
618 char buf[MAXPATHLEN];
623 * Make sure file is safe on disk. To improve performance we will call
624 * fsync() to the directory where file lies
626 if (rename(from, to) != 0 ||
627 (to_dir = dirname(to)) == NULL ||
628 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
629 fsync(to_dir_fd) != 0) {
631 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
643 error(const char *name)
654 char buf[MAXPATHLEN];
658 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
662 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
666 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
675 (void)fprintf(stderr,
676 "usage: pwd_mkdb [-CiNp] [-d directory] [-s cachesize] [-u username] file\n");