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";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/endian.h>
47 #include <arpa/inet.h>
65 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
66 #define PERM_SECURE (S_IRUSR|S_IWUSR)
67 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
68 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
74 2048 * 1024, /* cachesize */
76 BYTE_ORDER /* lorder */
79 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
80 static struct passwd pwd; /* password structure */
81 static char *pname; /* password file name */
82 static char prefix[MAXPATHLEN];
84 static int is_comment; /* flag for comments */
85 static char line[LINE_MAX];
88 void error(const char *);
89 void cp(char *, char *, mode_t mode);
90 void mv(char *, char *);
91 int scan(FILE *, struct passwd *);
92 static void usage(void);
95 main(int argc, char *argv[])
97 static char verskey[] = _PWD_VERSION_KEY;
98 char version = _PWD_CURRENT_VERSION;
100 DBT data, sdata, key;
103 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
108 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
109 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
110 char buf2[MAXPATHLEN];
111 char sbuf2[MAXPATHLEN];
113 u_int method, methoduid;
114 int Cflag, dflag, iflag;
117 iflag = dflag = Cflag = 0;
118 strcpy(prefix, _PATH_PWD);
121 while ((ch = getopt(argc, argv, "BCLNd:ips:u:v")) != -1)
123 case 'B': /* big-endian output */
124 openinfo.lorder = BIG_ENDIAN;
126 case 'C': /* verify only */
129 case 'L': /* little-endian output */
130 openinfo.lorder = LITTLE_ENDIAN;
132 case 'N': /* do not wait for lock */
133 nblock = LOCK_NB; /* will fail if locked */
137 strlcpy(prefix, optarg, sizeof(prefix));
142 case 'p': /* create V7 "file.orig" */
145 case 's': /* change default cachesize */
146 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
148 case 'u': /* only update this record */
151 case 'v': /* backward compatible */
159 if (argc != 1 || (username && (*username == '+' || *username == '-')))
163 * This could be changed to allow the user to interrupt.
164 * Probably not worth the effort.
167 sigaddset(&set, SIGTSTP);
168 sigaddset(&set, SIGHUP);
169 sigaddset(&set, SIGINT);
170 sigaddset(&set, SIGQUIT);
171 sigaddset(&set, SIGTERM);
172 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
174 /* We don't care what the user wants. */
180 * Open and lock the original password file. We have to check
181 * the hardlink count after we get the lock to handle any potential
182 * unlink/rename race.
184 * This lock is necessary when someone runs pwd_mkdb manually, directly
185 * on master.passwd, to handle the case where a user might try to
186 * change his password while pwd_mkdb is running.
191 if (!(fp = fopen(pname, "r")))
193 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
195 if (fstat(fileno(fp), &st) < 0)
197 if (st.st_nlink != 0)
203 /* check only if password database is valid */
205 for (cnt = 1; scan(fp, &pwd); ++cnt);
209 /* Open the temporary insecure password database. */
210 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
211 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
215 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
216 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
218 clean = FILE_INSECURE;
219 cp(buf2, buf, PERM_INSECURE);
221 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
226 cp(sbuf2, sbuf, PERM_SECURE);
228 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
233 * Do some trouble to check if we should store this users
234 * uid. Don't use getpwnam/getpwuid as that interferes
237 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
242 key.size = sizeof(verskey)-1;
243 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
244 use_version = *(unsigned char *)data.data;
247 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
248 len = strlen(username);
250 /* Only check that username fits in buffer */
251 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
252 key.data = (u_char *)buf;
254 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
255 p = (char *)data.data;
257 /* jump over pw_name and pw_passwd, to get to pw_uid */
263 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
264 memmove(buf + 1, p, sizeof(store));
265 key.data = (u_char *)buf;
266 key.size = sizeof(store) + 1;
268 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
269 /* First field of data.data holds pw_pwname */
270 if (!strcmp(data.data, username))
273 methoduid = R_NOOVERWRITE;
275 methoduid = R_NOOVERWRITE;
278 methoduid = R_NOOVERWRITE;
280 if ((pw_db->close)(pw_db))
281 error("close pw_db");
285 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
288 clean = FILE_INSECURE;
291 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
296 method = R_NOOVERWRITE;
297 methoduid = R_NOOVERWRITE;
301 * Open file for old password file. Minor trickiness -- don't want to
302 * chance the file already existing, since someone (stupidly) might
303 * still be using this for permission checking. So, open it first and
304 * fdopen the resulting fd. The resulting file should be readable by
308 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
310 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
312 if ((oldfp = fdopen(tfd, "w")) == NULL)
318 * The databases actually contain three copies of the original data.
319 * Each password file entry is converted into a rough approximation
320 * of a ``struct passwd'', with the strings placed inline. This
321 * object is then stored as the data for three separate keys. The
322 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
323 * character. The second key is the pw_uid field prepended by the
324 * _PW_KEYBYUID character. The third key is the line number in the
325 * original file prepended by the _PW_KEYBYNUM character. (The special
326 * characters are prepended to ensure that the keys do not collide.)
328 /* In order to transition this file into a machine-independent
329 * form, we have to change the format of entries. However, since
330 * older binaries will still expect the old MD format entries, we
331 * create those as usual and use versioned tags for the new entries.
333 if (username == NULL) {
334 /* Do not add the VERSION tag when updating a single
335 * user. When operating on `old format' databases, this
336 * would result in applications `seeing' only the updated
340 key.size = sizeof(verskey)-1;
341 data.data = &version;
343 if ((dp->put)(dp, &key, &data, 0) == -1)
345 if ((dp->put)(sdp, &key, &data, 0) == -1)
349 data.data = (u_char *)buf;
350 sdata.data = (u_char *)sbuf;
351 key.data = (u_char *)tbuf;
352 for (cnt = 1; scan(fp, &pwd); ++cnt) {
354 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
358 #define COMPACT(e) t = e; while ((*p++ = *t++));
359 #define SCALAR(e) store = htonl((uint32_t)(e)); \
360 memmove(p, &store, sizeof(store)); \
362 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
363 memmove(p, &store, sizeof(store)); \
365 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
367 bswap32((uint32_t)(e)))
369 (!username || (strcmp(username, pwd.pw_name) == 0))) {
370 /* Create insecure data. */
372 COMPACT(pwd.pw_name);
376 SCALAR(pwd.pw_change);
377 COMPACT(pwd.pw_class);
378 COMPACT(pwd.pw_gecos);
380 COMPACT(pwd.pw_shell);
381 SCALAR(pwd.pw_expire);
382 SCALAR(pwd.pw_fields);
385 /* Create secure data. */
387 COMPACT(pwd.pw_name);
388 COMPACT(pwd.pw_passwd);
391 SCALAR(pwd.pw_change);
392 COMPACT(pwd.pw_class);
393 COMPACT(pwd.pw_gecos);
395 COMPACT(pwd.pw_shell);
396 SCALAR(pwd.pw_expire);
397 SCALAR(pwd.pw_fields);
398 sdata.size = p - sbuf;
400 /* Store insecure by name. */
401 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
402 len = strlen(pwd.pw_name);
403 memmove(tbuf + 1, pwd.pw_name, len);
405 if ((dp->put)(dp, &key, &data, method) == -1)
408 /* Store insecure by number. */
409 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
411 memmove(tbuf + 1, &store, sizeof(store));
412 key.size = sizeof(store) + 1;
413 if ((dp->put)(dp, &key, &data, method) == -1)
416 /* Store insecure by uid. */
417 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
418 store = htonl(pwd.pw_uid);
419 memmove(tbuf + 1, &store, sizeof(store));
420 key.size = sizeof(store) + 1;
421 if ((dp->put)(dp, &key, &data, methoduid) == -1)
424 /* Store secure by name. */
425 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
426 len = strlen(pwd.pw_name);
427 memmove(tbuf + 1, pwd.pw_name, len);
429 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
432 /* Store secure by number. */
433 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
435 memmove(tbuf + 1, &store, sizeof(store));
436 key.size = sizeof(store) + 1;
437 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
440 /* Store secure by uid. */
441 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
442 store = htonl(pwd.pw_uid);
443 memmove(tbuf + 1, &store, sizeof(store));
444 key.size = sizeof(store) + 1;
445 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
448 /* Store insecure and secure special plus and special minus */
449 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
450 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
451 store = htonl(ypcnt);
452 memmove(tbuf + 1, &store, sizeof(store));
454 key.size = sizeof(store) + 1;
455 if ((dp->put)(dp, &key, &data, method) == -1)
457 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
461 /* Create insecure data. (legacy version) */
463 COMPACT(pwd.pw_name);
467 LSCALAR(pwd.pw_change);
468 COMPACT(pwd.pw_class);
469 COMPACT(pwd.pw_gecos);
471 COMPACT(pwd.pw_shell);
472 LSCALAR(pwd.pw_expire);
473 LSCALAR(pwd.pw_fields);
476 /* Create secure data. (legacy version) */
478 COMPACT(pwd.pw_name);
479 COMPACT(pwd.pw_passwd);
482 LSCALAR(pwd.pw_change);
483 COMPACT(pwd.pw_class);
484 COMPACT(pwd.pw_gecos);
486 COMPACT(pwd.pw_shell);
487 LSCALAR(pwd.pw_expire);
488 LSCALAR(pwd.pw_fields);
489 sdata.size = p - sbuf;
491 /* Store insecure by name. */
492 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
493 len = strlen(pwd.pw_name);
494 memmove(tbuf + 1, pwd.pw_name, len);
496 if ((dp->put)(dp, &key, &data, method) == -1)
499 /* Store insecure by number. */
500 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
502 memmove(tbuf + 1, &store, sizeof(store));
503 key.size = sizeof(store) + 1;
504 if ((dp->put)(dp, &key, &data, method) == -1)
507 /* Store insecure by uid. */
508 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
509 store = HTOL(pwd.pw_uid);
510 memmove(tbuf + 1, &store, sizeof(store));
511 key.size = sizeof(store) + 1;
512 if ((dp->put)(dp, &key, &data, methoduid) == -1)
515 /* Store secure by name. */
516 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
517 len = strlen(pwd.pw_name);
518 memmove(tbuf + 1, pwd.pw_name, len);
520 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
523 /* Store secure by number. */
524 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
526 memmove(tbuf + 1, &store, sizeof(store));
527 key.size = sizeof(store) + 1;
528 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
531 /* Store secure by uid. */
532 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
533 store = HTOL(pwd.pw_uid);
534 memmove(tbuf + 1, &store, sizeof(store));
535 key.size = sizeof(store) + 1;
536 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
539 /* Store insecure and secure special plus and special minus */
540 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
541 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
543 memmove(tbuf + 1, &store, sizeof(store));
545 key.size = sizeof(store) + 1;
546 if ((dp->put)(dp, &key, &data, method) == -1)
548 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
552 /* Create original format password file entry */
553 if (is_comment && makeold){ /* copy comments */
554 if (fprintf(oldfp, "%s\n", line) < 0)
556 } else if (makeold) {
560 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
561 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
563 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
564 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
565 pwd.pw_fields & _PWF_GID ? gidstr : "",
566 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
570 /* If YP enabled, set flag. */
572 buf[0] = yp_enabled + 2;
575 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
576 if ((dp->put)(dp, &key, &data, method) == -1)
578 if ((sdp->put)(sdp, &key, &data, method) == -1)
580 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
582 if ((dp->put)(dp, &key, &data, method) == -1)
584 if ((sdp->put)(sdp, &key, &data, method) == -1)
588 if ((dp->close)(dp) == -1)
590 if ((sdp->close)(sdp) == -1)
594 if (fclose(oldfp) == EOF)
598 /* Set master.passwd permissions, in case caller forgot. */
599 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
601 /* Install as the real password files. */
602 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
603 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
605 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
606 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
609 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
610 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
614 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
615 * all use flock(2) on it to block other incarnations of themselves.
616 * The rename means that everything is unlocked, as the original file
617 * can no longer be accessed.
619 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
623 * Close locked password file after rename()
625 if (fclose(fp) == EOF)
632 scan(FILE * fp, struct passwd *pw)
637 if (!fgets(line, sizeof(line), fp))
641 * ``... if I swallow anything evil, put your fingers down my
645 if (!(p = strchr(line, '\n'))) {
647 * XXX: This may also happen if the last line in a
648 * file does not have a trailing newline.
650 warnx("line #%d too long", lcnt);
657 * Ignore comments: ^[ \t]*#
659 for (p = line; *p != '\0'; p++)
660 if (*p != ' ' && *p != '\t')
662 if (*p == '#' || *p == '\0') {
668 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
669 warnx("at line #%d", lcnt);
670 fmt: errno = EFTYPE; /* XXX */
678 cp(char *from, char *to, mode_t mode)
680 static char buf[MAXBSIZE];
681 int from_fd, rcount, to_fd, wcount;
683 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
685 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
687 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
688 wcount = write(to_fd, buf, rcount);
689 if (rcount != wcount || wcount == -1) {
692 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
700 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
708 mv(char *from, char *to)
710 char buf[MAXPATHLEN];
712 if (rename(from, to)) {
714 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
721 error(const char *name)
732 char buf[MAXPATHLEN];
736 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
740 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
744 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
753 (void)fprintf(stderr,
754 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");