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 * 3. 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, lflag;
119 iflag = dflag = Cflag = lflag = 0;
120 strcpy(prefix, _PATH_PWD);
124 while ((ch = getopt(argc, argv, "BCLlNd:ips:u:v")) != -1)
126 case 'B': /* big-endian output */
127 openinfo.lorder = BIG_ENDIAN;
129 case 'C': /* verify only */
132 case 'l': /* generate legacy entries */
135 case 'L': /* little-endian output */
136 openinfo.lorder = LITTLE_ENDIAN;
138 case 'N': /* do not wait for lock */
139 nblock = LOCK_NB; /* will fail if locked */
143 strlcpy(prefix, optarg, sizeof(prefix));
148 case 'p': /* create V7 "file.orig" */
151 case 's': /* change default cachesize */
152 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
154 case 'u': /* only update this record */
157 case 'v': /* backward compatible */
165 if (argc != 1 || (username && (*username == '+' || *username == '-')))
169 * This could be changed to allow the user to interrupt.
170 * Probably not worth the effort.
173 sigaddset(&set, SIGTSTP);
174 sigaddset(&set, SIGHUP);
175 sigaddset(&set, SIGINT);
176 sigaddset(&set, SIGQUIT);
177 sigaddset(&set, SIGTERM);
178 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
180 /* We don't care what the user wants. */
186 * Open and lock the original password file. We have to check
187 * the hardlink count after we get the lock to handle any potential
188 * unlink/rename race.
190 * This lock is necessary when someone runs pwd_mkdb manually, directly
191 * on master.passwd, to handle the case where a user might try to
192 * change his password while pwd_mkdb is running.
197 if (!(fp = fopen(pname, "r")))
199 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
201 if (fstat(fileno(fp), &st) < 0)
203 if (st.st_nlink != 0)
209 /* check only if password database is valid */
211 while (scan(fp, &pwd))
212 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
213 warnx("%s: username too long", pwd.pw_name);
219 /* Open the temporary insecure password database. */
220 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
221 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
225 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
226 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
228 clean = FILE_INSECURE;
229 cp(buf2, buf, PERM_INSECURE);
231 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
236 cp(sbuf2, sbuf, PERM_SECURE);
238 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
243 * Do some trouble to check if we should store this users
244 * uid. Don't use getpwnam/getpwuid as that interferes
247 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
252 key.size = sizeof(verskey)-1;
253 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
254 use_version = *(unsigned char *)data.data;
257 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
258 len = strlen(username);
260 /* Only check that username fits in buffer */
261 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
262 key.data = (u_char *)buf;
264 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
265 p = (char *)data.data;
267 /* jump over pw_name and pw_passwd, to get to pw_uid */
273 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
274 memmove(buf + 1, p, sizeof(store));
275 key.data = (u_char *)buf;
276 key.size = sizeof(store) + 1;
278 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
279 /* First field of data.data holds pw_pwname */
280 if (!strcmp(data.data, username))
283 methoduid = R_NOOVERWRITE;
285 methoduid = R_NOOVERWRITE;
288 methoduid = R_NOOVERWRITE;
290 if ((pw_db->close)(pw_db))
291 error("close pw_db");
295 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
298 clean = FILE_INSECURE;
301 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
306 method = R_NOOVERWRITE;
307 methoduid = R_NOOVERWRITE;
311 * Open file for old password file. Minor trickiness -- don't want to
312 * chance the file already existing, since someone (stupidly) might
313 * still be using this for permission checking. So, open it first and
314 * fdopen the resulting fd. The resulting file should be readable by
318 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
320 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
322 if ((oldfp = fdopen(tfd, "w")) == NULL)
328 * The databases actually contain three copies of the original data.
329 * Each password file entry is converted into a rough approximation
330 * of a ``struct passwd'', with the strings placed inline. This
331 * object is then stored as the data for three separate keys. The
332 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
333 * character. The second key is the pw_uid field prepended by the
334 * _PW_KEYBYUID character. The third key is the line number in the
335 * original file prepended by the _PW_KEYBYNUM character. (The special
336 * characters are prepended to ensure that the keys do not collide.)
338 /* In order to transition this file into a machine-independent
339 * form, we have to change the format of entries. However, since
340 * older binaries will still expect the old MD format entries, we
341 * create those as usual and use versioned tags for the new entries.
343 if (username == NULL) {
344 /* Do not add the VERSION tag when updating a single
345 * user. When operating on `old format' databases, this
346 * would result in applications `seeing' only the updated
350 key.size = sizeof(verskey)-1;
351 data.data = &version;
353 if ((dp->put)(dp, &key, &data, 0) == -1)
355 if ((sdp->put)(sdp, &key, &data, 0) == -1)
359 data.data = (u_char *)buf;
360 sdata.data = (u_char *)sbuf;
361 key.data = (u_char *)tbuf;
362 for (cnt = 1; scan(fp, &pwd); ++cnt) {
364 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
370 #define COMPACT(e) t = e; while ((*p++ = *t++));
371 #define SCALAR(e) store = htonl((uint32_t)(e)); \
372 memmove(p, &store, sizeof(store)); \
374 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
375 memmove(p, &store, sizeof(store)); \
377 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
379 bswap32((uint32_t)(e)))
381 (!username || (strcmp(username, pwd.pw_name) == 0))) {
382 /* Create insecure data. */
384 COMPACT(pwd.pw_name);
388 SCALAR(pwd.pw_change);
389 COMPACT(pwd.pw_class);
390 COMPACT(pwd.pw_gecos);
392 COMPACT(pwd.pw_shell);
393 SCALAR(pwd.pw_expire);
394 SCALAR(pwd.pw_fields);
397 /* Create secure data. */
399 COMPACT(pwd.pw_name);
400 COMPACT(pwd.pw_passwd);
403 SCALAR(pwd.pw_change);
404 COMPACT(pwd.pw_class);
405 COMPACT(pwd.pw_gecos);
407 COMPACT(pwd.pw_shell);
408 SCALAR(pwd.pw_expire);
409 SCALAR(pwd.pw_fields);
410 sdata.size = p - sbuf;
412 /* Store insecure by name. */
413 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
414 len = strlen(pwd.pw_name);
415 memmove(tbuf + 1, pwd.pw_name, len);
417 if ((dp->put)(dp, &key, &data, method) == -1)
420 /* Store insecure by number. */
421 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
423 memmove(tbuf + 1, &store, sizeof(store));
424 key.size = sizeof(store) + 1;
425 if ((dp->put)(dp, &key, &data, method) == -1)
428 /* Store insecure by uid. */
429 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
430 store = htonl(pwd.pw_uid);
431 memmove(tbuf + 1, &store, sizeof(store));
432 key.size = sizeof(store) + 1;
433 if ((dp->put)(dp, &key, &data, methoduid) == -1)
436 /* Store secure by name. */
437 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
438 len = strlen(pwd.pw_name);
439 memmove(tbuf + 1, pwd.pw_name, len);
441 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
444 /* Store secure by number. */
445 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
447 memmove(tbuf + 1, &store, sizeof(store));
448 key.size = sizeof(store) + 1;
449 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
452 /* Store secure by uid. */
453 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
454 store = htonl(pwd.pw_uid);
455 memmove(tbuf + 1, &store, sizeof(store));
456 key.size = sizeof(store) + 1;
457 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
460 /* Store insecure and secure special plus and special minus */
461 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
462 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
463 store = htonl(ypcnt);
464 memmove(tbuf + 1, &store, sizeof(store));
465 key.size = sizeof(store) + 1;
466 if ((dp->put)(dp, &key, &data, method) == -1)
468 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
473 /* Create insecure data. (legacy version) */
475 COMPACT(pwd.pw_name);
479 LSCALAR(pwd.pw_change);
480 COMPACT(pwd.pw_class);
481 COMPACT(pwd.pw_gecos);
483 COMPACT(pwd.pw_shell);
484 LSCALAR(pwd.pw_expire);
485 LSCALAR(pwd.pw_fields);
488 /* Create secure data. (legacy version) */
490 COMPACT(pwd.pw_name);
491 COMPACT(pwd.pw_passwd);
494 LSCALAR(pwd.pw_change);
495 COMPACT(pwd.pw_class);
496 COMPACT(pwd.pw_gecos);
498 COMPACT(pwd.pw_shell);
499 LSCALAR(pwd.pw_expire);
500 LSCALAR(pwd.pw_fields);
501 sdata.size = p - sbuf;
503 /* Store insecure by name. */
504 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
505 len = strlen(pwd.pw_name);
506 memmove(tbuf + 1, pwd.pw_name, len);
508 if ((dp->put)(dp, &key, &data, method) == -1)
511 /* Store insecure by number. */
512 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
514 memmove(tbuf + 1, &store, sizeof(store));
515 key.size = sizeof(store) + 1;
516 if ((dp->put)(dp, &key, &data, method) == -1)
519 /* Store insecure by uid. */
520 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
521 store = HTOL(pwd.pw_uid);
522 memmove(tbuf + 1, &store, sizeof(store));
523 key.size = sizeof(store) + 1;
524 if ((dp->put)(dp, &key, &data, methoduid) == -1)
527 /* Store secure by name. */
528 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
529 len = strlen(pwd.pw_name);
530 memmove(tbuf + 1, pwd.pw_name, len);
532 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
535 /* Store secure by number. */
536 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
538 memmove(tbuf + 1, &store, sizeof(store));
539 key.size = sizeof(store) + 1;
540 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
543 /* Store secure by uid. */
544 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
545 store = HTOL(pwd.pw_uid);
546 memmove(tbuf + 1, &store, sizeof(store));
547 key.size = sizeof(store) + 1;
548 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
551 /* Store insecure and secure special plus and special minus */
552 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
553 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
555 memmove(tbuf + 1, &store, sizeof(store));
556 key.size = sizeof(store) + 1;
557 if ((dp->put)(dp, &key, &data, method) == -1)
559 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
564 /* Create original format password file entry */
565 if (is_comment && makeold){ /* copy comments */
566 if (fprintf(oldfp, "%s\n", line) < 0)
568 } else if (makeold) {
572 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
573 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
575 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
576 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
577 pwd.pw_fields & _PWF_GID ? gidstr : "",
578 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
582 /* If YP enabled, set flag. */
584 buf[0] = yp_enabled + 2;
587 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
588 if ((dp->put)(dp, &key, &data, method) == -1)
590 if ((sdp->put)(sdp, &key, &data, method) == -1)
593 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
595 if ((dp->put)(dp, &key, &data, method) == -1)
597 if ((sdp->put)(sdp, &key, &data, method) == -1)
602 if ((dp->close)(dp) == -1)
604 if ((sdp->close)(sdp) == -1)
608 if (fclose(oldfp) == EOF)
612 /* Set master.passwd permissions, in case caller forgot. */
613 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
615 /* Install as the real password files. */
616 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
617 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
619 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
620 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
623 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
624 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
628 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
629 * all use flock(2) on it to block other incarnations of themselves.
630 * The rename means that everything is unlocked, as the original file
631 * can no longer be accessed.
633 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
637 * Close locked password file after rename()
639 if (fclose(fp) == EOF)
646 scan(FILE *fp, struct passwd *pw)
652 p = fgetln(fp, &len);
657 * ``... if I swallow anything evil, put your fingers down my
661 if (len > 0 && p[len - 1] == '\n')
663 if (len >= sizeof(line) - 1) {
664 warnx("line #%d too long", lcnt);
667 memcpy(line, p, len);
671 * Ignore comments: ^[ \t]*#
673 for (p = line; *p != '\0'; p++)
674 if (*p != ' ' && *p != '\t')
676 if (*p == '#' || *p == '\0') {
682 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
683 warnx("at line #%d", lcnt);
684 fmt: errno = EFTYPE; /* XXX */
692 cp(char *from, char *to, mode_t mode)
694 static char buf[MAXBSIZE];
695 int from_fd, rcount, to_fd, wcount;
697 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
699 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
701 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
702 wcount = write(to_fd, buf, rcount);
703 if (rcount != wcount || wcount == -1) {
706 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
714 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
722 mv(char *from, char *to)
724 char buf[MAXPATHLEN];
729 * Make sure file is safe on disk. To improve performance we will call
730 * fsync() to the directory where file lies
732 if (rename(from, to) != 0 ||
733 (to_dir = dirname(to)) == NULL ||
734 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
735 fsync(to_dir_fd) != 0) {
737 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
749 error(const char *name)
760 char buf[MAXPATHLEN];
764 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
768 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
772 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
781 (void)fprintf(stderr,
782 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");