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>
66 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
67 #define PERM_SECURE (S_IRUSR|S_IWUSR)
68 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
69 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
75 2048 * 1024, /* cachesize */
77 BYTE_ORDER /* lorder */
80 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
81 static struct passwd pwd; /* password structure */
82 static char *pname; /* password file name */
83 static char prefix[MAXPATHLEN];
85 static int is_comment; /* flag for comments */
86 static char line[LINE_MAX];
89 void error(const char *);
90 void cp(char *, char *, mode_t mode);
91 void mv(char *, char *);
92 int scan(FILE *, struct passwd *);
93 static void usage(void);
96 main(int argc, char *argv[])
98 static char verskey[] = _PWD_VERSION_KEY;
99 char version = _PWD_CURRENT_VERSION;
100 DB *dp, *sdp, *pw_db;
101 DBT data, sdata, key;
104 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
109 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
110 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
111 char buf2[MAXPATHLEN];
112 char sbuf2[MAXPATHLEN];
114 u_int method, methoduid;
115 int Cflag, dflag, iflag;
118 iflag = dflag = Cflag = 0;
119 strcpy(prefix, _PATH_PWD);
123 while ((ch = getopt(argc, argv, "BCLNd:ips:u:v")) != -1)
125 case 'B': /* big-endian output */
126 openinfo.lorder = BIG_ENDIAN;
128 case 'C': /* verify only */
131 case 'L': /* little-endian output */
132 openinfo.lorder = LITTLE_ENDIAN;
134 case 'N': /* do not wait for lock */
135 nblock = LOCK_NB; /* will fail if locked */
139 strlcpy(prefix, optarg, sizeof(prefix));
144 case 'p': /* create V7 "file.orig" */
147 case 's': /* change default cachesize */
148 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
150 case 'u': /* only update this record */
153 case 'v': /* backward compatible */
161 if (argc != 1 || (username && (*username == '+' || *username == '-')))
165 * This could be changed to allow the user to interrupt.
166 * Probably not worth the effort.
169 sigaddset(&set, SIGTSTP);
170 sigaddset(&set, SIGHUP);
171 sigaddset(&set, SIGINT);
172 sigaddset(&set, SIGQUIT);
173 sigaddset(&set, SIGTERM);
174 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
176 /* We don't care what the user wants. */
182 * Open and lock the original password file. We have to check
183 * the hardlink count after we get the lock to handle any potential
184 * unlink/rename race.
186 * This lock is necessary when someone runs pwd_mkdb manually, directly
187 * on master.passwd, to handle the case where a user might try to
188 * change his password while pwd_mkdb is running.
193 if (!(fp = fopen(pname, "r")))
195 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
197 if (fstat(fileno(fp), &st) < 0)
199 if (st.st_nlink != 0)
205 /* check only if password database is valid */
207 while (scan(fp, &pwd))
208 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
209 warnx("%s: username too long", pwd.pw_name);
215 /* Open the temporary insecure password database. */
216 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
217 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
221 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
222 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
224 clean = FILE_INSECURE;
225 cp(buf2, buf, PERM_INSECURE);
227 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
232 cp(sbuf2, sbuf, PERM_SECURE);
234 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
239 * Do some trouble to check if we should store this users
240 * uid. Don't use getpwnam/getpwuid as that interferes
243 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
248 key.size = sizeof(verskey)-1;
249 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
250 use_version = *(unsigned char *)data.data;
253 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
254 len = strlen(username);
256 /* Only check that username fits in buffer */
257 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
258 key.data = (u_char *)buf;
260 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
261 p = (char *)data.data;
263 /* jump over pw_name and pw_passwd, to get to pw_uid */
269 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
270 memmove(buf + 1, p, sizeof(store));
271 key.data = (u_char *)buf;
272 key.size = sizeof(store) + 1;
274 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
275 /* First field of data.data holds pw_pwname */
276 if (!strcmp(data.data, username))
279 methoduid = R_NOOVERWRITE;
281 methoduid = R_NOOVERWRITE;
284 methoduid = R_NOOVERWRITE;
286 if ((pw_db->close)(pw_db))
287 error("close pw_db");
291 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
294 clean = FILE_INSECURE;
297 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
302 method = R_NOOVERWRITE;
303 methoduid = R_NOOVERWRITE;
307 * Open file for old password file. Minor trickiness -- don't want to
308 * chance the file already existing, since someone (stupidly) might
309 * still be using this for permission checking. So, open it first and
310 * fdopen the resulting fd. The resulting file should be readable by
314 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
316 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
318 if ((oldfp = fdopen(tfd, "w")) == NULL)
324 * The databases actually contain three copies of the original data.
325 * Each password file entry is converted into a rough approximation
326 * of a ``struct passwd'', with the strings placed inline. This
327 * object is then stored as the data for three separate keys. The
328 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
329 * character. The second key is the pw_uid field prepended by the
330 * _PW_KEYBYUID character. The third key is the line number in the
331 * original file prepended by the _PW_KEYBYNUM character. (The special
332 * characters are prepended to ensure that the keys do not collide.)
334 /* In order to transition this file into a machine-independent
335 * form, we have to change the format of entries. However, since
336 * older binaries will still expect the old MD format entries, we
337 * create those as usual and use versioned tags for the new entries.
339 if (username == NULL) {
340 /* Do not add the VERSION tag when updating a single
341 * user. When operating on `old format' databases, this
342 * would result in applications `seeing' only the updated
346 key.size = sizeof(verskey)-1;
347 data.data = &version;
349 if ((dp->put)(dp, &key, &data, 0) == -1)
351 if ((dp->put)(sdp, &key, &data, 0) == -1)
355 data.data = (u_char *)buf;
356 sdata.data = (u_char *)sbuf;
357 key.data = (u_char *)tbuf;
358 for (cnt = 1; scan(fp, &pwd); ++cnt) {
360 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
364 #define COMPACT(e) t = e; while ((*p++ = *t++));
365 #define SCALAR(e) store = htonl((uint32_t)(e)); \
366 memmove(p, &store, sizeof(store)); \
368 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
369 memmove(p, &store, sizeof(store)); \
371 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
373 bswap32((uint32_t)(e)))
375 (!username || (strcmp(username, pwd.pw_name) == 0))) {
376 /* Create insecure data. */
378 COMPACT(pwd.pw_name);
382 SCALAR(pwd.pw_change);
383 COMPACT(pwd.pw_class);
384 COMPACT(pwd.pw_gecos);
386 COMPACT(pwd.pw_shell);
387 SCALAR(pwd.pw_expire);
388 SCALAR(pwd.pw_fields);
391 /* Create secure data. */
393 COMPACT(pwd.pw_name);
394 COMPACT(pwd.pw_passwd);
397 SCALAR(pwd.pw_change);
398 COMPACT(pwd.pw_class);
399 COMPACT(pwd.pw_gecos);
401 COMPACT(pwd.pw_shell);
402 SCALAR(pwd.pw_expire);
403 SCALAR(pwd.pw_fields);
404 sdata.size = p - sbuf;
406 /* Store insecure by name. */
407 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
408 len = strlen(pwd.pw_name);
409 memmove(tbuf + 1, pwd.pw_name, len);
411 if ((dp->put)(dp, &key, &data, method) == -1)
414 /* Store insecure by number. */
415 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
417 memmove(tbuf + 1, &store, sizeof(store));
418 key.size = sizeof(store) + 1;
419 if ((dp->put)(dp, &key, &data, method) == -1)
422 /* Store insecure by uid. */
423 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
424 store = htonl(pwd.pw_uid);
425 memmove(tbuf + 1, &store, sizeof(store));
426 key.size = sizeof(store) + 1;
427 if ((dp->put)(dp, &key, &data, methoduid) == -1)
430 /* Store secure by name. */
431 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
432 len = strlen(pwd.pw_name);
433 memmove(tbuf + 1, pwd.pw_name, len);
435 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
438 /* Store secure by number. */
439 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
441 memmove(tbuf + 1, &store, sizeof(store));
442 key.size = sizeof(store) + 1;
443 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
446 /* Store secure by uid. */
447 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
448 store = htonl(pwd.pw_uid);
449 memmove(tbuf + 1, &store, sizeof(store));
450 key.size = sizeof(store) + 1;
451 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
454 /* Store insecure and secure special plus and special minus */
455 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
456 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
457 store = htonl(ypcnt);
458 memmove(tbuf + 1, &store, sizeof(store));
460 key.size = sizeof(store) + 1;
461 if ((dp->put)(dp, &key, &data, method) == -1)
463 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
467 /* Create insecure data. (legacy version) */
469 COMPACT(pwd.pw_name);
473 LSCALAR(pwd.pw_change);
474 COMPACT(pwd.pw_class);
475 COMPACT(pwd.pw_gecos);
477 COMPACT(pwd.pw_shell);
478 LSCALAR(pwd.pw_expire);
479 LSCALAR(pwd.pw_fields);
482 /* Create secure data. (legacy version) */
484 COMPACT(pwd.pw_name);
485 COMPACT(pwd.pw_passwd);
488 LSCALAR(pwd.pw_change);
489 COMPACT(pwd.pw_class);
490 COMPACT(pwd.pw_gecos);
492 COMPACT(pwd.pw_shell);
493 LSCALAR(pwd.pw_expire);
494 LSCALAR(pwd.pw_fields);
495 sdata.size = p - sbuf;
497 /* Store insecure by name. */
498 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
499 len = strlen(pwd.pw_name);
500 memmove(tbuf + 1, pwd.pw_name, len);
502 if ((dp->put)(dp, &key, &data, method) == -1)
505 /* Store insecure by number. */
506 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
508 memmove(tbuf + 1, &store, sizeof(store));
509 key.size = sizeof(store) + 1;
510 if ((dp->put)(dp, &key, &data, method) == -1)
513 /* Store insecure by uid. */
514 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
515 store = HTOL(pwd.pw_uid);
516 memmove(tbuf + 1, &store, sizeof(store));
517 key.size = sizeof(store) + 1;
518 if ((dp->put)(dp, &key, &data, methoduid) == -1)
521 /* Store secure by name. */
522 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
523 len = strlen(pwd.pw_name);
524 memmove(tbuf + 1, pwd.pw_name, len);
526 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
529 /* Store secure by number. */
530 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
532 memmove(tbuf + 1, &store, sizeof(store));
533 key.size = sizeof(store) + 1;
534 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
537 /* Store secure by uid. */
538 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
539 store = HTOL(pwd.pw_uid);
540 memmove(tbuf + 1, &store, sizeof(store));
541 key.size = sizeof(store) + 1;
542 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
545 /* Store insecure and secure special plus and special minus */
546 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
547 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
549 memmove(tbuf + 1, &store, sizeof(store));
551 key.size = sizeof(store) + 1;
552 if ((dp->put)(dp, &key, &data, method) == -1)
554 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
558 /* Create original format password file entry */
559 if (is_comment && makeold){ /* copy comments */
560 if (fprintf(oldfp, "%s\n", line) < 0)
562 } else if (makeold) {
566 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
567 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
569 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
570 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
571 pwd.pw_fields & _PWF_GID ? gidstr : "",
572 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
576 /* If YP enabled, set flag. */
578 buf[0] = yp_enabled + 2;
581 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
582 if ((dp->put)(dp, &key, &data, method) == -1)
584 if ((sdp->put)(sdp, &key, &data, method) == -1)
586 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
588 if ((dp->put)(dp, &key, &data, method) == -1)
590 if ((sdp->put)(sdp, &key, &data, method) == -1)
594 if ((dp->close)(dp) == -1)
596 if ((sdp->close)(sdp) == -1)
600 if (fclose(oldfp) == EOF)
604 /* Set master.passwd permissions, in case caller forgot. */
605 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
607 /* Install as the real password files. */
608 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
609 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
611 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
612 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
615 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
616 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
620 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
621 * all use flock(2) on it to block other incarnations of themselves.
622 * The rename means that everything is unlocked, as the original file
623 * can no longer be accessed.
625 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
629 * Close locked password file after rename()
631 if (fclose(fp) == EOF)
638 scan(FILE *fp, struct passwd *pw)
644 p = fgetln(fp, &len);
649 * ``... if I swallow anything evil, put your fingers down my
653 if (len > 0 && p[len - 1] == '\n')
655 if (len >= sizeof(line) - 1) {
656 warnx("line #%d too long", lcnt);
659 memcpy(line, p, len);
663 * Ignore comments: ^[ \t]*#
665 for (p = line; *p != '\0'; p++)
666 if (*p != ' ' && *p != '\t')
668 if (*p == '#' || *p == '\0') {
674 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
675 warnx("at line #%d", lcnt);
676 fmt: errno = EFTYPE; /* XXX */
684 cp(char *from, char *to, mode_t mode)
686 static char buf[MAXBSIZE];
687 int from_fd, rcount, to_fd, wcount;
689 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
691 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
693 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
694 wcount = write(to_fd, buf, rcount);
695 if (rcount != wcount || wcount == -1) {
698 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
706 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
714 mv(char *from, char *to)
716 char buf[MAXPATHLEN];
718 if (rename(from, to)) {
720 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
727 error(const char *name)
738 char buf[MAXPATHLEN];
742 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
746 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
750 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
759 (void)fprintf(stderr,
760 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");