2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1991, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
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 BYTE_ORDER /* 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, "BCLNd:ips:u:v")) != -1)
126 case 'B': /* big-endian output */
127 openinfo.lorder = BIG_ENDIAN;
129 case 'C': /* verify only */
132 case 'L': /* little-endian output */
133 openinfo.lorder = LITTLE_ENDIAN;
135 case 'N': /* do not wait for lock */
136 nblock = LOCK_NB; /* will fail if locked */
140 strlcpy(prefix, optarg, sizeof(prefix));
145 case 'p': /* create V7 "file.orig" */
148 case 's': /* change default cachesize */
149 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
151 case 'u': /* only update this record */
154 case 'v': /* backward compatible */
162 if (argc != 1 || (username && (*username == '+' || *username == '-')))
166 * This could be changed to allow the user to interrupt.
167 * Probably not worth the effort.
170 sigaddset(&set, SIGTSTP);
171 sigaddset(&set, SIGHUP);
172 sigaddset(&set, SIGINT);
173 sigaddset(&set, SIGQUIT);
174 sigaddset(&set, SIGTERM);
175 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
177 /* We don't care what the user wants. */
183 * Open and lock the original password file. We have to check
184 * the hardlink count after we get the lock to handle any potential
185 * unlink/rename race.
187 * This lock is necessary when someone runs pwd_mkdb manually, directly
188 * on master.passwd, to handle the case where a user might try to
189 * change his password while pwd_mkdb is running.
194 if (!(fp = fopen(pname, "r")))
196 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
198 if (fstat(fileno(fp), &st) < 0)
200 if (st.st_nlink != 0)
206 /* check only if password database is valid */
208 while (scan(fp, &pwd))
209 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
210 warnx("%s: username too long", pwd.pw_name);
216 /* Open the temporary insecure password database. */
217 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
218 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
222 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
223 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
225 clean = FILE_INSECURE;
226 cp(buf2, buf, PERM_INSECURE);
228 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
233 cp(sbuf2, sbuf, PERM_SECURE);
235 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
240 * Do some trouble to check if we should store this users
241 * uid. Don't use getpwnam/getpwuid as that interferes
244 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
249 key.size = sizeof(verskey)-1;
250 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
251 use_version = *(unsigned char *)data.data;
254 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
255 len = strlen(username);
257 /* Only check that username fits in buffer */
258 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
259 key.data = (u_char *)buf;
261 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
262 p = (char *)data.data;
264 /* jump over pw_name and pw_passwd, to get to pw_uid */
270 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
271 memmove(buf + 1, p, sizeof(store));
272 key.data = (u_char *)buf;
273 key.size = sizeof(store) + 1;
275 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
276 /* First field of data.data holds pw_pwname */
277 if (!strcmp(data.data, username))
280 methoduid = R_NOOVERWRITE;
282 methoduid = R_NOOVERWRITE;
285 methoduid = R_NOOVERWRITE;
287 if ((pw_db->close)(pw_db))
288 error("close pw_db");
292 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
295 clean = FILE_INSECURE;
298 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
303 method = R_NOOVERWRITE;
304 methoduid = R_NOOVERWRITE;
308 * Open file for old password file. Minor trickiness -- don't want to
309 * chance the file already existing, since someone (stupidly) might
310 * still be using this for permission checking. So, open it first and
311 * fdopen the resulting fd. The resulting file should be readable by
315 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
317 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
319 if ((oldfp = fdopen(tfd, "w")) == NULL)
325 * The databases actually contain three copies of the original data.
326 * Each password file entry is converted into a rough approximation
327 * of a ``struct passwd'', with the strings placed inline. This
328 * object is then stored as the data for three separate keys. The
329 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
330 * character. The second key is the pw_uid field prepended by the
331 * _PW_KEYBYUID character. The third key is the line number in the
332 * original file prepended by the _PW_KEYBYNUM character. (The special
333 * characters are prepended to ensure that the keys do not collide.)
335 /* In order to transition this file into a machine-independent
336 * form, we have to change the format of entries. However, since
337 * older binaries will still expect the old MD format entries, we
338 * create those as usual and use versioned tags for the new entries.
340 if (username == NULL) {
341 /* Do not add the VERSION tag when updating a single
342 * user. When operating on `old format' databases, this
343 * would result in applications `seeing' only the updated
347 key.size = sizeof(verskey)-1;
348 data.data = &version;
350 if ((dp->put)(dp, &key, &data, 0) == -1)
352 if ((dp->put)(sdp, &key, &data, 0) == -1)
356 data.data = (u_char *)buf;
357 sdata.data = (u_char *)sbuf;
358 key.data = (u_char *)tbuf;
359 for (cnt = 1; scan(fp, &pwd); ++cnt) {
361 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
367 #define COMPACT(e) t = e; while ((*p++ = *t++));
368 #define SCALAR(e) store = htonl((uint32_t)(e)); \
369 memmove(p, &store, sizeof(store)); \
371 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
372 memmove(p, &store, sizeof(store)); \
374 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
376 bswap32((uint32_t)(e)))
378 (!username || (strcmp(username, pwd.pw_name) == 0))) {
379 /* Create insecure data. */
381 COMPACT(pwd.pw_name);
385 SCALAR(pwd.pw_change);
386 COMPACT(pwd.pw_class);
387 COMPACT(pwd.pw_gecos);
389 COMPACT(pwd.pw_shell);
390 SCALAR(pwd.pw_expire);
391 SCALAR(pwd.pw_fields);
394 /* Create secure data. */
396 COMPACT(pwd.pw_name);
397 COMPACT(pwd.pw_passwd);
400 SCALAR(pwd.pw_change);
401 COMPACT(pwd.pw_class);
402 COMPACT(pwd.pw_gecos);
404 COMPACT(pwd.pw_shell);
405 SCALAR(pwd.pw_expire);
406 SCALAR(pwd.pw_fields);
407 sdata.size = p - sbuf;
409 /* Store insecure by name. */
410 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
411 len = strlen(pwd.pw_name);
412 memmove(tbuf + 1, pwd.pw_name, len);
414 if ((dp->put)(dp, &key, &data, method) == -1)
417 /* Store insecure by number. */
418 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
420 memmove(tbuf + 1, &store, sizeof(store));
421 key.size = sizeof(store) + 1;
422 if ((dp->put)(dp, &key, &data, method) == -1)
425 /* Store insecure by uid. */
426 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
427 store = htonl(pwd.pw_uid);
428 memmove(tbuf + 1, &store, sizeof(store));
429 key.size = sizeof(store) + 1;
430 if ((dp->put)(dp, &key, &data, methoduid) == -1)
433 /* Store secure by name. */
434 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
435 len = strlen(pwd.pw_name);
436 memmove(tbuf + 1, pwd.pw_name, len);
438 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
441 /* Store secure by number. */
442 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
444 memmove(tbuf + 1, &store, sizeof(store));
445 key.size = sizeof(store) + 1;
446 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
449 /* Store secure by uid. */
450 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
451 store = htonl(pwd.pw_uid);
452 memmove(tbuf + 1, &store, sizeof(store));
453 key.size = sizeof(store) + 1;
454 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
457 /* Store insecure and secure special plus and special minus */
458 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
459 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
460 store = htonl(ypcnt);
461 memmove(tbuf + 1, &store, sizeof(store));
462 key.size = sizeof(store) + 1;
463 if ((dp->put)(dp, &key, &data, method) == -1)
465 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
469 /* Create insecure data. (legacy version) */
471 COMPACT(pwd.pw_name);
475 LSCALAR(pwd.pw_change);
476 COMPACT(pwd.pw_class);
477 COMPACT(pwd.pw_gecos);
479 COMPACT(pwd.pw_shell);
480 LSCALAR(pwd.pw_expire);
481 LSCALAR(pwd.pw_fields);
484 /* Create secure data. (legacy version) */
486 COMPACT(pwd.pw_name);
487 COMPACT(pwd.pw_passwd);
490 LSCALAR(pwd.pw_change);
491 COMPACT(pwd.pw_class);
492 COMPACT(pwd.pw_gecos);
494 COMPACT(pwd.pw_shell);
495 LSCALAR(pwd.pw_expire);
496 LSCALAR(pwd.pw_fields);
497 sdata.size = p - sbuf;
499 /* Store insecure by name. */
500 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
501 len = strlen(pwd.pw_name);
502 memmove(tbuf + 1, pwd.pw_name, len);
504 if ((dp->put)(dp, &key, &data, method) == -1)
507 /* Store insecure by number. */
508 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
510 memmove(tbuf + 1, &store, sizeof(store));
511 key.size = sizeof(store) + 1;
512 if ((dp->put)(dp, &key, &data, method) == -1)
515 /* Store insecure by uid. */
516 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
517 store = HTOL(pwd.pw_uid);
518 memmove(tbuf + 1, &store, sizeof(store));
519 key.size = sizeof(store) + 1;
520 if ((dp->put)(dp, &key, &data, methoduid) == -1)
523 /* Store secure by name. */
524 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
525 len = strlen(pwd.pw_name);
526 memmove(tbuf + 1, pwd.pw_name, len);
528 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
531 /* Store secure by number. */
532 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
534 memmove(tbuf + 1, &store, sizeof(store));
535 key.size = sizeof(store) + 1;
536 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
539 /* Store secure by uid. */
540 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
541 store = HTOL(pwd.pw_uid);
542 memmove(tbuf + 1, &store, sizeof(store));
543 key.size = sizeof(store) + 1;
544 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
547 /* Store insecure and secure special plus and special minus */
548 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
549 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
551 memmove(tbuf + 1, &store, sizeof(store));
552 key.size = sizeof(store) + 1;
553 if ((dp->put)(dp, &key, &data, method) == -1)
555 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
559 /* Create original format password file entry */
560 if (is_comment && makeold){ /* copy comments */
561 if (fprintf(oldfp, "%s\n", line) < 0)
563 } else if (makeold) {
567 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
568 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
570 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
571 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
572 pwd.pw_fields & _PWF_GID ? gidstr : "",
573 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
577 /* If YP enabled, set flag. */
579 buf[0] = yp_enabled + 2;
582 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
583 if ((dp->put)(dp, &key, &data, method) == -1)
585 if ((sdp->put)(sdp, &key, &data, method) == -1)
587 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
589 if ((dp->put)(dp, &key, &data, method) == -1)
591 if ((sdp->put)(sdp, &key, &data, method) == -1)
595 if ((dp->close)(dp) == -1)
597 if ((sdp->close)(sdp) == -1)
601 if (fclose(oldfp) == EOF)
605 /* Set master.passwd permissions, in case caller forgot. */
606 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
608 /* Install as the real password files. */
609 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
610 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
612 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
613 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
616 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
617 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
621 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
622 * all use flock(2) on it to block other incarnations of themselves.
623 * The rename means that everything is unlocked, as the original file
624 * can no longer be accessed.
626 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
630 * Close locked password file after rename()
632 if (fclose(fp) == EOF)
639 scan(FILE *fp, struct passwd *pw)
645 p = fgetln(fp, &len);
650 * ``... if I swallow anything evil, put your fingers down my
654 if (len > 0 && p[len - 1] == '\n')
656 if (len >= sizeof(line) - 1) {
657 warnx("line #%d too long", lcnt);
660 memcpy(line, p, len);
664 * Ignore comments: ^[ \t]*#
666 for (p = line; *p != '\0'; p++)
667 if (*p != ' ' && *p != '\t')
669 if (*p == '#' || *p == '\0') {
675 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
676 warnx("at line #%d", lcnt);
677 fmt: errno = EFTYPE; /* XXX */
685 cp(char *from, char *to, mode_t mode)
687 static char buf[MAXBSIZE];
688 int from_fd, rcount, to_fd, wcount;
690 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
692 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
694 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
695 wcount = write(to_fd, buf, rcount);
696 if (rcount != wcount || wcount == -1) {
699 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
707 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
715 mv(char *from, char *to)
717 char buf[MAXPATHLEN];
722 * Make sure file is safe on disk. To improve performance we will call
723 * fsync() to the directory where file lies
725 if (rename(from, to) != 0 ||
726 (to_dir = dirname(to)) == NULL ||
727 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
728 fsync(to_dir_fd) != 0) {
730 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
742 error(const char *name)
753 char buf[MAXPATHLEN];
757 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
761 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
765 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
774 (void)fprintf(stderr,
775 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");