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 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, 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 == '-')))
168 warnx("legacy (v3) database format support is deprecated");
171 * This could be changed to allow the user to interrupt.
172 * Probably not worth the effort.
175 sigaddset(&set, SIGTSTP);
176 sigaddset(&set, SIGHUP);
177 sigaddset(&set, SIGINT);
178 sigaddset(&set, SIGQUIT);
179 sigaddset(&set, SIGTERM);
180 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
182 /* We don't care what the user wants. */
188 * Open and lock the original password file. We have to check
189 * the hardlink count after we get the lock to handle any potential
190 * unlink/rename race.
192 * This lock is necessary when someone runs pwd_mkdb manually, directly
193 * on master.passwd, to handle the case where a user might try to
194 * change his password while pwd_mkdb is running.
199 if (!(fp = fopen(pname, "r")))
201 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
203 if (fstat(fileno(fp), &st) < 0)
205 if (st.st_nlink != 0)
211 /* check only if password database is valid */
213 while (scan(fp, &pwd))
214 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
215 warnx("%s: username too long", pwd.pw_name);
221 /* Open the temporary insecure password database. */
222 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
223 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
227 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
228 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
230 clean = FILE_INSECURE;
231 cp(buf2, buf, PERM_INSECURE);
233 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
238 cp(sbuf2, sbuf, PERM_SECURE);
240 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
245 * Do some trouble to check if we should store this users
246 * uid. Don't use getpwnam/getpwuid as that interferes
249 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
254 key.size = sizeof(verskey)-1;
255 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
256 use_version = *(unsigned char *)data.data;
259 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
260 len = strlen(username);
262 /* Only check that username fits in buffer */
263 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
264 key.data = (u_char *)buf;
266 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
267 p = (char *)data.data;
269 /* jump over pw_name and pw_passwd, to get to pw_uid */
275 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
276 memmove(buf + 1, p, sizeof(store));
277 key.data = (u_char *)buf;
278 key.size = sizeof(store) + 1;
280 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
281 /* First field of data.data holds pw_pwname */
282 if (!strcmp(data.data, username))
285 methoduid = R_NOOVERWRITE;
287 methoduid = R_NOOVERWRITE;
290 methoduid = R_NOOVERWRITE;
292 if ((pw_db->close)(pw_db))
293 error("close pw_db");
297 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
300 clean = FILE_INSECURE;
303 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
308 method = R_NOOVERWRITE;
309 methoduid = R_NOOVERWRITE;
313 * Open file for old password file. Minor trickiness -- don't want to
314 * chance the file already existing, since someone (stupidly) might
315 * still be using this for permission checking. So, open it first and
316 * fdopen the resulting fd. The resulting file should be readable by
320 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
322 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
324 if ((oldfp = fdopen(tfd, "w")) == NULL)
330 * The databases actually contain three copies of the original data.
331 * Each password file entry is converted into a rough approximation
332 * of a ``struct passwd'', with the strings placed inline. This
333 * object is then stored as the data for three separate keys. The
334 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
335 * character. The second key is the pw_uid field prepended by the
336 * _PW_KEYBYUID character. The third key is the line number in the
337 * original file prepended by the _PW_KEYBYNUM character. (The special
338 * characters are prepended to ensure that the keys do not collide.)
340 /* In order to transition this file into a machine-independent
341 * form, we have to change the format of entries. However, since
342 * older binaries will still expect the old MD format entries, we
343 * create those as usual and use versioned tags for the new entries.
345 if (username == NULL) {
346 /* Do not add the VERSION tag when updating a single
347 * user. When operating on `old format' databases, this
348 * would result in applications `seeing' only the updated
352 key.size = sizeof(verskey)-1;
353 data.data = &version;
355 if ((dp->put)(dp, &key, &data, 0) == -1)
357 if ((sdp->put)(sdp, &key, &data, 0) == -1)
361 data.data = (u_char *)buf;
362 sdata.data = (u_char *)sbuf;
363 key.data = (u_char *)tbuf;
364 for (cnt = 1; scan(fp, &pwd); ++cnt) {
366 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
372 #define COMPACT(e) t = e; while ((*p++ = *t++));
373 #define SCALAR(e) store = htonl((uint32_t)(e)); \
374 memmove(p, &store, sizeof(store)); \
376 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
377 memmove(p, &store, sizeof(store)); \
379 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
381 bswap32((uint32_t)(e)))
383 (!username || (strcmp(username, pwd.pw_name) == 0))) {
384 /* Create insecure data. */
386 COMPACT(pwd.pw_name);
390 SCALAR(pwd.pw_change);
391 COMPACT(pwd.pw_class);
392 COMPACT(pwd.pw_gecos);
394 COMPACT(pwd.pw_shell);
395 SCALAR(pwd.pw_expire);
396 SCALAR(pwd.pw_fields);
399 /* Create secure data. */
401 COMPACT(pwd.pw_name);
402 COMPACT(pwd.pw_passwd);
405 SCALAR(pwd.pw_change);
406 COMPACT(pwd.pw_class);
407 COMPACT(pwd.pw_gecos);
409 COMPACT(pwd.pw_shell);
410 SCALAR(pwd.pw_expire);
411 SCALAR(pwd.pw_fields);
412 sdata.size = p - sbuf;
414 /* Store insecure by name. */
415 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
416 len = strlen(pwd.pw_name);
417 memmove(tbuf + 1, pwd.pw_name, len);
419 if ((dp->put)(dp, &key, &data, method) == -1)
422 /* Store insecure by number. */
423 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
425 memmove(tbuf + 1, &store, sizeof(store));
426 key.size = sizeof(store) + 1;
427 if ((dp->put)(dp, &key, &data, method) == -1)
430 /* Store insecure by uid. */
431 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
432 store = htonl(pwd.pw_uid);
433 memmove(tbuf + 1, &store, sizeof(store));
434 key.size = sizeof(store) + 1;
435 if ((dp->put)(dp, &key, &data, methoduid) == -1)
438 /* Store secure by name. */
439 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
440 len = strlen(pwd.pw_name);
441 memmove(tbuf + 1, pwd.pw_name, len);
443 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
446 /* Store secure by number. */
447 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
449 memmove(tbuf + 1, &store, sizeof(store));
450 key.size = sizeof(store) + 1;
451 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
454 /* Store secure by uid. */
455 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
456 store = htonl(pwd.pw_uid);
457 memmove(tbuf + 1, &store, sizeof(store));
458 key.size = sizeof(store) + 1;
459 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
462 /* Store insecure and secure special plus and special minus */
463 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
464 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
465 store = htonl(ypcnt);
466 memmove(tbuf + 1, &store, sizeof(store));
467 key.size = sizeof(store) + 1;
468 if ((dp->put)(dp, &key, &data, method) == -1)
470 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
475 /* Create insecure data. (legacy version) */
477 COMPACT(pwd.pw_name);
481 LSCALAR(pwd.pw_change);
482 COMPACT(pwd.pw_class);
483 COMPACT(pwd.pw_gecos);
485 COMPACT(pwd.pw_shell);
486 LSCALAR(pwd.pw_expire);
487 LSCALAR(pwd.pw_fields);
490 /* Create secure data. (legacy version) */
492 COMPACT(pwd.pw_name);
493 COMPACT(pwd.pw_passwd);
496 LSCALAR(pwd.pw_change);
497 COMPACT(pwd.pw_class);
498 COMPACT(pwd.pw_gecos);
500 COMPACT(pwd.pw_shell);
501 LSCALAR(pwd.pw_expire);
502 LSCALAR(pwd.pw_fields);
503 sdata.size = p - sbuf;
505 /* Store insecure by name. */
506 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
507 len = strlen(pwd.pw_name);
508 memmove(tbuf + 1, pwd.pw_name, len);
510 if ((dp->put)(dp, &key, &data, method) == -1)
513 /* Store insecure by number. */
514 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
516 memmove(tbuf + 1, &store, sizeof(store));
517 key.size = sizeof(store) + 1;
518 if ((dp->put)(dp, &key, &data, method) == -1)
521 /* Store insecure by uid. */
522 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
523 store = HTOL(pwd.pw_uid);
524 memmove(tbuf + 1, &store, sizeof(store));
525 key.size = sizeof(store) + 1;
526 if ((dp->put)(dp, &key, &data, methoduid) == -1)
529 /* Store secure by name. */
530 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
531 len = strlen(pwd.pw_name);
532 memmove(tbuf + 1, pwd.pw_name, len);
534 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
537 /* Store secure by number. */
538 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
540 memmove(tbuf + 1, &store, sizeof(store));
541 key.size = sizeof(store) + 1;
542 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
545 /* Store secure by uid. */
546 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
547 store = HTOL(pwd.pw_uid);
548 memmove(tbuf + 1, &store, sizeof(store));
549 key.size = sizeof(store) + 1;
550 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
553 /* Store insecure and secure special plus and special minus */
554 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
555 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
557 memmove(tbuf + 1, &store, sizeof(store));
558 key.size = sizeof(store) + 1;
559 if ((dp->put)(dp, &key, &data, method) == -1)
561 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
566 /* Create original format password file entry */
567 if (is_comment && makeold){ /* copy comments */
568 if (fprintf(oldfp, "%s\n", line) < 0)
570 } else if (makeold) {
574 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
575 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
577 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
578 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
579 pwd.pw_fields & _PWF_GID ? gidstr : "",
580 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
584 /* If YP enabled, set flag. */
586 buf[0] = yp_enabled + 2;
589 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
590 if ((dp->put)(dp, &key, &data, method) == -1)
592 if ((sdp->put)(sdp, &key, &data, method) == -1)
595 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
597 if ((dp->put)(dp, &key, &data, method) == -1)
599 if ((sdp->put)(sdp, &key, &data, method) == -1)
604 if ((dp->close)(dp) == -1)
606 if ((sdp->close)(sdp) == -1)
610 if (fclose(oldfp) == EOF)
614 /* Set master.passwd permissions, in case caller forgot. */
615 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
617 /* Install as the real password files. */
618 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
619 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
621 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
622 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
625 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
626 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
630 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
631 * all use flock(2) on it to block other incarnations of themselves.
632 * The rename means that everything is unlocked, as the original file
633 * can no longer be accessed.
635 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
639 * Close locked password file after rename()
641 if (fclose(fp) == EOF)
648 scan(FILE *fp, struct passwd *pw)
654 p = fgetln(fp, &len);
659 * ``... if I swallow anything evil, put your fingers down my
663 if (len > 0 && p[len - 1] == '\n')
665 if (len >= sizeof(line) - 1) {
666 warnx("line #%d too long", lcnt);
669 memcpy(line, p, len);
673 * Ignore comments: ^[ \t]*#
675 for (p = line; *p != '\0'; p++)
676 if (*p != ' ' && *p != '\t')
678 if (*p == '#' || *p == '\0') {
684 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
685 warnx("at line #%d", lcnt);
686 fmt: errno = EFTYPE; /* XXX */
694 cp(char *from, char *to, mode_t mode)
696 static char buf[MAXBSIZE];
697 int from_fd, rcount, to_fd, wcount;
699 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
701 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
703 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
704 wcount = write(to_fd, buf, rcount);
705 if (rcount != wcount || wcount == -1) {
708 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
716 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
724 mv(char *from, char *to)
726 char buf[MAXPATHLEN];
731 * Make sure file is safe on disk. To improve performance we will call
732 * fsync() to the directory where file lies
734 if (rename(from, to) != 0 ||
735 (to_dir = dirname(to)) == NULL ||
736 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
737 fsync(to_dir_fd) != 0) {
739 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
751 error(const char *name)
762 char buf[MAXPATHLEN];
766 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
770 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
774 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
783 (void)fprintf(stderr,
784 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");