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 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/endian.h>
50 #include <arpa/inet.h>
69 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
70 #define PERM_SECURE (S_IRUSR|S_IWUSR)
71 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
72 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
74 static HASHINFO openinfo = {
78 2048 * 1024, /* cachesize */
80 BYTE_ORDER /* lorder */
83 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
84 static struct passwd pwd; /* password structure */
85 static char *pname; /* password file name */
86 static char prefix[MAXPATHLEN];
88 static int is_comment; /* flag for comments */
89 static char line[LINE_MAX];
92 void error(const char *);
93 void cp(char *, char *, mode_t mode);
94 void mv(char *, char *);
95 int scan(FILE *, struct passwd *);
96 static void usage(void);
99 main(int argc, char *argv[])
101 static char verskey[] = _PWD_VERSION_KEY;
102 char version = _PWD_CURRENT_VERSION;
103 DB *dp, *sdp, *pw_db;
104 DBT data, sdata, key;
107 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
112 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
113 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
114 char buf2[MAXPATHLEN];
115 char sbuf2[MAXPATHLEN];
117 u_int method, methoduid;
118 int Cflag, dflag, iflag, lflag;
121 iflag = dflag = Cflag = lflag = 0;
122 strcpy(prefix, _PATH_PWD);
126 while ((ch = getopt(argc, argv, "BCLlNd:ips:u:v")) != -1)
128 case 'B': /* big-endian output */
129 openinfo.lorder = BIG_ENDIAN;
131 case 'C': /* verify only */
134 case 'l': /* generate legacy entries */
137 case 'L': /* little-endian output */
138 openinfo.lorder = LITTLE_ENDIAN;
140 case 'N': /* do not wait for lock */
141 nblock = LOCK_NB; /* will fail if locked */
145 strlcpy(prefix, optarg, sizeof(prefix));
150 case 'p': /* create V7 "file.orig" */
153 case 's': /* change default cachesize */
154 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
156 case 'u': /* only update this record */
159 case 'v': /* backward compatible */
167 if (argc != 1 || (username && (*username == '+' || *username == '-')))
170 warnx("legacy (v3) database format support is deprecated");
173 * This could be changed to allow the user to interrupt.
174 * Probably not worth the effort.
177 sigaddset(&set, SIGTSTP);
178 sigaddset(&set, SIGHUP);
179 sigaddset(&set, SIGINT);
180 sigaddset(&set, SIGQUIT);
181 sigaddset(&set, SIGTERM);
182 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
184 /* We don't care what the user wants. */
190 * Open and lock the original password file. We have to check
191 * the hardlink count after we get the lock to handle any potential
192 * unlink/rename race.
194 * This lock is necessary when someone runs pwd_mkdb manually, directly
195 * on master.passwd, to handle the case where a user might try to
196 * change his password while pwd_mkdb is running.
201 if (!(fp = fopen(pname, "r")))
203 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
205 if (fstat(fileno(fp), &st) < 0)
207 if (st.st_nlink != 0)
213 /* check only if password database is valid */
215 while (scan(fp, &pwd))
216 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
217 warnx("%s: username too long", pwd.pw_name);
223 /* Open the temporary insecure password database. */
224 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
225 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
229 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
230 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
232 clean = FILE_INSECURE;
233 cp(buf2, buf, PERM_INSECURE);
235 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
240 cp(sbuf2, sbuf, PERM_SECURE);
242 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
247 * Do some trouble to check if we should store this users
248 * uid. Don't use getpwnam/getpwuid as that interferes
251 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
256 key.size = sizeof(verskey)-1;
257 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
258 use_version = *(unsigned char *)data.data;
261 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
262 len = strlen(username);
264 /* Only check that username fits in buffer */
265 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
266 key.data = (u_char *)buf;
268 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
269 p = (char *)data.data;
271 /* jump over pw_name and pw_passwd, to get to pw_uid */
277 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
278 memmove(buf + 1, p, sizeof(store));
279 key.data = (u_char *)buf;
280 key.size = sizeof(store) + 1;
282 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
283 /* First field of data.data holds pw_pwname */
284 if (!strcmp(data.data, username))
287 methoduid = R_NOOVERWRITE;
289 methoduid = R_NOOVERWRITE;
292 methoduid = R_NOOVERWRITE;
294 if ((pw_db->close)(pw_db))
295 error("close pw_db");
299 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
302 clean = FILE_INSECURE;
305 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
310 method = R_NOOVERWRITE;
311 methoduid = R_NOOVERWRITE;
315 * Open file for old password file. Minor trickiness -- don't want to
316 * chance the file already existing, since someone (stupidly) might
317 * still be using this for permission checking. So, open it first and
318 * fdopen the resulting fd. The resulting file should be readable by
322 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
324 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
326 if ((oldfp = fdopen(tfd, "w")) == NULL)
332 * The databases actually contain three copies of the original data.
333 * Each password file entry is converted into a rough approximation
334 * of a ``struct passwd'', with the strings placed inline. This
335 * object is then stored as the data for three separate keys. The
336 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
337 * character. The second key is the pw_uid field prepended by the
338 * _PW_KEYBYUID character. The third key is the line number in the
339 * original file prepended by the _PW_KEYBYNUM character. (The special
340 * characters are prepended to ensure that the keys do not collide.)
342 /* In order to transition this file into a machine-independent
343 * form, we have to change the format of entries. However, since
344 * older binaries will still expect the old MD format entries, we
345 * create those as usual and use versioned tags for the new entries.
347 if (username == NULL) {
348 /* Do not add the VERSION tag when updating a single
349 * user. When operating on `old format' databases, this
350 * would result in applications `seeing' only the updated
354 key.size = sizeof(verskey)-1;
355 data.data = &version;
357 if ((dp->put)(dp, &key, &data, 0) == -1)
359 if ((sdp->put)(sdp, &key, &data, 0) == -1)
363 data.data = (u_char *)buf;
364 sdata.data = (u_char *)sbuf;
365 key.data = (u_char *)tbuf;
366 for (cnt = 1; scan(fp, &pwd); ++cnt) {
368 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
374 #define COMPACT(e) t = e; while ((*p++ = *t++));
375 #define SCALAR(e) store = htonl((uint32_t)(e)); \
376 memmove(p, &store, sizeof(store)); \
378 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
379 memmove(p, &store, sizeof(store)); \
381 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
383 bswap32((uint32_t)(e)))
385 (!username || (strcmp(username, pwd.pw_name) == 0))) {
386 /* Create insecure data. */
388 COMPACT(pwd.pw_name);
392 SCALAR(pwd.pw_change);
393 COMPACT(pwd.pw_class);
394 COMPACT(pwd.pw_gecos);
396 COMPACT(pwd.pw_shell);
397 SCALAR(pwd.pw_expire);
398 SCALAR(pwd.pw_fields);
401 /* Create secure data. */
403 COMPACT(pwd.pw_name);
404 COMPACT(pwd.pw_passwd);
407 SCALAR(pwd.pw_change);
408 COMPACT(pwd.pw_class);
409 COMPACT(pwd.pw_gecos);
411 COMPACT(pwd.pw_shell);
412 SCALAR(pwd.pw_expire);
413 SCALAR(pwd.pw_fields);
414 sdata.size = p - sbuf;
416 /* Store insecure by name. */
417 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
418 len = strlen(pwd.pw_name);
419 memmove(tbuf + 1, pwd.pw_name, len);
421 if ((dp->put)(dp, &key, &data, method) == -1)
424 /* Store insecure by number. */
425 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
427 memmove(tbuf + 1, &store, sizeof(store));
428 key.size = sizeof(store) + 1;
429 if ((dp->put)(dp, &key, &data, method) == -1)
432 /* Store insecure by uid. */
433 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
434 store = htonl(pwd.pw_uid);
435 memmove(tbuf + 1, &store, sizeof(store));
436 key.size = sizeof(store) + 1;
437 if ((dp->put)(dp, &key, &data, methoduid) == -1)
440 /* Store secure by name. */
441 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
442 len = strlen(pwd.pw_name);
443 memmove(tbuf + 1, pwd.pw_name, len);
445 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
448 /* Store secure by number. */
449 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
451 memmove(tbuf + 1, &store, sizeof(store));
452 key.size = sizeof(store) + 1;
453 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
456 /* Store secure by uid. */
457 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
458 store = htonl(pwd.pw_uid);
459 memmove(tbuf + 1, &store, sizeof(store));
460 key.size = sizeof(store) + 1;
461 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
464 /* Store insecure and secure special plus and special minus */
465 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
466 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
467 store = htonl(ypcnt);
468 memmove(tbuf + 1, &store, sizeof(store));
469 key.size = sizeof(store) + 1;
470 if ((dp->put)(dp, &key, &data, method) == -1)
472 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
477 /* Create insecure data. (legacy version) */
479 COMPACT(pwd.pw_name);
483 LSCALAR(pwd.pw_change);
484 COMPACT(pwd.pw_class);
485 COMPACT(pwd.pw_gecos);
487 COMPACT(pwd.pw_shell);
488 LSCALAR(pwd.pw_expire);
489 LSCALAR(pwd.pw_fields);
492 /* Create secure data. (legacy version) */
494 COMPACT(pwd.pw_name);
495 COMPACT(pwd.pw_passwd);
498 LSCALAR(pwd.pw_change);
499 COMPACT(pwd.pw_class);
500 COMPACT(pwd.pw_gecos);
502 COMPACT(pwd.pw_shell);
503 LSCALAR(pwd.pw_expire);
504 LSCALAR(pwd.pw_fields);
505 sdata.size = p - sbuf;
507 /* Store insecure by name. */
508 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
509 len = strlen(pwd.pw_name);
510 memmove(tbuf + 1, pwd.pw_name, len);
512 if ((dp->put)(dp, &key, &data, method) == -1)
515 /* Store insecure by number. */
516 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
518 memmove(tbuf + 1, &store, sizeof(store));
519 key.size = sizeof(store) + 1;
520 if ((dp->put)(dp, &key, &data, method) == -1)
523 /* Store insecure by uid. */
524 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
525 store = HTOL(pwd.pw_uid);
526 memmove(tbuf + 1, &store, sizeof(store));
527 key.size = sizeof(store) + 1;
528 if ((dp->put)(dp, &key, &data, methoduid) == -1)
531 /* Store secure by name. */
532 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
533 len = strlen(pwd.pw_name);
534 memmove(tbuf + 1, pwd.pw_name, len);
536 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
539 /* Store secure by number. */
540 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
542 memmove(tbuf + 1, &store, sizeof(store));
543 key.size = sizeof(store) + 1;
544 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
547 /* Store secure by uid. */
548 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
549 store = HTOL(pwd.pw_uid);
550 memmove(tbuf + 1, &store, sizeof(store));
551 key.size = sizeof(store) + 1;
552 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
555 /* Store insecure and secure special plus and special minus */
556 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
557 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
559 memmove(tbuf + 1, &store, sizeof(store));
560 key.size = sizeof(store) + 1;
561 if ((dp->put)(dp, &key, &data, method) == -1)
563 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
568 /* Create original format password file entry */
569 if (is_comment && makeold){ /* copy comments */
570 if (fprintf(oldfp, "%s\n", line) < 0)
572 } else if (makeold) {
576 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
577 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
579 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
580 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
581 pwd.pw_fields & _PWF_GID ? gidstr : "",
582 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
586 /* If YP enabled, set flag. */
588 buf[0] = yp_enabled + 2;
591 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
592 if ((dp->put)(dp, &key, &data, method) == -1)
594 if ((sdp->put)(sdp, &key, &data, method) == -1)
597 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
599 if ((dp->put)(dp, &key, &data, method) == -1)
601 if ((sdp->put)(sdp, &key, &data, method) == -1)
606 if ((dp->close)(dp) == -1)
608 if ((sdp->close)(sdp) == -1)
612 if (fclose(oldfp) == EOF)
616 /* Set master.passwd permissions, in case caller forgot. */
617 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
619 /* Install as the real password files. */
620 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
621 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
623 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
624 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
627 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
628 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
632 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
633 * all use flock(2) on it to block other incarnations of themselves.
634 * The rename means that everything is unlocked, as the original file
635 * can no longer be accessed.
637 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
641 * Close locked password file after rename()
643 if (fclose(fp) == EOF)
650 scan(FILE *fp, struct passwd *pw)
656 p = fgetln(fp, &len);
661 * ``... if I swallow anything evil, put your fingers down my
665 if (len > 0 && p[len - 1] == '\n')
667 if (len >= sizeof(line) - 1) {
668 warnx("line #%d too long", lcnt);
671 memcpy(line, p, len);
675 * Ignore comments: ^[ \t]*#
677 for (p = line; *p != '\0'; p++)
678 if (*p != ' ' && *p != '\t')
680 if (*p == '#' || *p == '\0') {
686 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
687 warnx("at line #%d", lcnt);
688 fmt: errno = EFTYPE; /* XXX */
696 cp(char *from, char *to, mode_t mode)
698 static char buf[MAXBSIZE];
699 int from_fd, rcount, to_fd, wcount;
701 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
703 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
705 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
706 wcount = write(to_fd, buf, rcount);
707 if (rcount != wcount || wcount == -1) {
710 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
718 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
726 mv(char *from, char *to)
728 char buf[MAXPATHLEN];
733 * Make sure file is safe on disk. To improve performance we will call
734 * fsync() to the directory where file lies
736 if (rename(from, to) != 0 ||
737 (to_dir = dirname(to)) == NULL ||
738 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
739 fsync(to_dir_fd) != 0) {
741 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
753 error(const char *name)
764 char buf[MAXPATHLEN];
768 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
772 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
776 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
785 (void)fprintf(stderr,
786 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");