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>
46 #include <arpa/inet.h>
64 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
65 #define PERM_SECURE (S_IRUSR|S_IWUSR)
66 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
67 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
73 2048 * 1024, /* cachesize */
78 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
79 static struct passwd pwd; /* password structure */
80 static char *pname; /* password file name */
81 static char prefix[MAXPATHLEN];
83 static int is_comment; /* flag for comments */
84 static char line[LINE_MAX];
87 void error(const char *);
88 void cp(char *, char *, mode_t mode);
89 void mv(char *, char *);
90 int scan(FILE *, struct passwd *);
91 static void usage(void);
94 main(int argc, char *argv[])
96 static char verskey[] = _PWD_VERSION_KEY;
97 char version = _PWD_CURRENT_VERSION;
102 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
104 int32_t pw_change, pw_expire;
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, "CNd:ips:u:v")) != -1)
123 case 'C': /* verify only */
126 case 'N': /* do not wait for lock */
127 nblock = LOCK_NB; /* will fail if locked */
131 strlcpy(prefix, optarg, sizeof(prefix));
136 case 'p': /* create V7 "file.orig" */
139 case 's': /* change default cachesize */
140 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
142 case 'u': /* only update this record */
145 case 'v': /* backward compatible */
153 if (argc != 1 || (username && (*username == '+' || *username == '-')))
157 * This could be changed to allow the user to interrupt.
158 * Probably not worth the effort.
161 sigaddset(&set, SIGTSTP);
162 sigaddset(&set, SIGHUP);
163 sigaddset(&set, SIGINT);
164 sigaddset(&set, SIGQUIT);
165 sigaddset(&set, SIGTERM);
166 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
168 /* We don't care what the user wants. */
174 * Open and lock the original password file. We have to check
175 * the hardlink count after we get the lock to handle any potential
176 * unlink/rename race.
178 * This lock is necessary when someone runs pwd_mkdb manually, directly
179 * on master.passwd, to handle the case where a user might try to
180 * change his password while pwd_mkdb is running.
185 if (!(fp = fopen(pname, "r")))
187 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
189 if (fstat(fileno(fp), &st) < 0)
191 if (st.st_nlink != 0)
197 /* check only if password database is valid */
199 for (cnt = 1; scan(fp, &pwd); ++cnt);
203 /* Open the temporary insecure password database. */
204 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
205 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
209 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
210 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
212 clean = FILE_INSECURE;
213 cp(buf2, buf, PERM_INSECURE);
215 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
220 cp(sbuf2, sbuf, PERM_SECURE);
222 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
227 * Do some trouble to check if we should store this users
228 * uid. Don't use getpwnam/getpwuid as that interferes
231 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
236 key.size = sizeof(verskey)-1;
237 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
238 use_version = *(unsigned char *)data.data;
241 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
242 len = strlen(username);
244 /* Only check that username fits in buffer */
245 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
246 key.data = (u_char *)buf;
248 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
249 p = (char *)data.data;
251 /* jump over pw_name and pw_passwd, to get to pw_uid */
257 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
258 memmove(buf + 1, p, sizeof(store));
259 key.data = (u_char *)buf;
260 key.size = sizeof(store) + 1;
262 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
263 /* First field of data.data holds pw_pwname */
264 if (!strcmp(data.data, username))
267 methoduid = R_NOOVERWRITE;
269 methoduid = R_NOOVERWRITE;
272 methoduid = R_NOOVERWRITE;
274 if ((pw_db->close)(pw_db))
275 error("close pw_db");
279 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
282 clean = FILE_INSECURE;
285 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
290 method = R_NOOVERWRITE;
291 methoduid = R_NOOVERWRITE;
295 * Open file for old password file. Minor trickiness -- don't want to
296 * chance the file already existing, since someone (stupidly) might
297 * still be using this for permission checking. So, open it first and
298 * fdopen the resulting fd. The resulting file should be readable by
302 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
304 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
306 if ((oldfp = fdopen(tfd, "w")) == NULL)
312 * The databases actually contain three copies of the original data.
313 * Each password file entry is converted into a rough approximation
314 * of a ``struct passwd'', with the strings placed inline. This
315 * object is then stored as the data for three separate keys. The
316 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
317 * character. The second key is the pw_uid field prepended by the
318 * _PW_KEYBYUID character. The third key is the line number in the
319 * original file prepended by the _PW_KEYBYNUM character. (The special
320 * characters are prepended to ensure that the keys do not collide.)
322 /* In order to transition this file into a machine-independent
323 * form, we have to change the format of entries. However, since
324 * older binaries will still expect the old MD format entries, we
325 * create those as usual and use versioned tags for the new entries.
327 if (username == NULL) {
328 /* Do not add the VERSION tag when updating a single
329 * user. When operating on `old format' databases, this
330 * would result in applications `seeing' only the updated
334 key.size = sizeof(verskey)-1;
335 data.data = &version;
337 if ((dp->put)(dp, &key, &data, 0) == -1)
339 if ((dp->put)(sdp, &key, &data, 0) == -1)
343 data.data = (u_char *)buf;
344 sdata.data = (u_char *)sbuf;
345 key.data = (u_char *)tbuf;
346 for (cnt = 1; scan(fp, &pwd); ++cnt) {
348 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
352 #define COMPACT(e) t = e; while ((*p++ = *t++));
353 #define SCALAR(e) store = htonl((uint32_t)(e)); \
354 memmove(p, &store, sizeof(store)); \
357 (!username || (strcmp(username, pwd.pw_name) == 0))) {
358 pw_change = pwd.pw_change;
359 pw_expire = pwd.pw_expire;
360 /* Create insecure data. */
362 COMPACT(pwd.pw_name);
366 SCALAR(pwd.pw_change);
367 COMPACT(pwd.pw_class);
368 COMPACT(pwd.pw_gecos);
370 COMPACT(pwd.pw_shell);
371 SCALAR(pwd.pw_expire);
372 SCALAR(pwd.pw_fields);
375 /* Create secure data. */
377 COMPACT(pwd.pw_name);
378 COMPACT(pwd.pw_passwd);
381 SCALAR(pwd.pw_change);
382 COMPACT(pwd.pw_class);
383 COMPACT(pwd.pw_gecos);
385 COMPACT(pwd.pw_shell);
386 SCALAR(pwd.pw_expire);
387 SCALAR(pwd.pw_fields);
388 sdata.size = p - sbuf;
390 /* Store insecure by name. */
391 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
392 len = strlen(pwd.pw_name);
393 memmove(tbuf + 1, pwd.pw_name, len);
395 if ((dp->put)(dp, &key, &data, method) == -1)
398 /* Store insecure by number. */
399 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
401 memmove(tbuf + 1, &store, sizeof(store));
402 key.size = sizeof(store) + 1;
403 if ((dp->put)(dp, &key, &data, method) == -1)
406 /* Store insecure by uid. */
407 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
408 store = htonl(pwd.pw_uid);
409 memmove(tbuf + 1, &store, sizeof(store));
410 key.size = sizeof(store) + 1;
411 if ((dp->put)(dp, &key, &data, methoduid) == -1)
414 /* Store secure 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 ((sdp->put)(sdp, &key, &sdata, method) == -1)
422 /* Store secure by number. */
423 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
425 memmove(tbuf + 1, &store, sizeof(store));
426 key.size = sizeof(store) + 1;
427 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
430 /* Store secure 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 ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
438 /* Store insecure and secure special plus and special minus */
439 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
440 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
441 store = htonl(ypcnt);
442 memmove(tbuf + 1, &store, sizeof(store));
444 key.size = sizeof(store) + 1;
445 if ((dp->put)(dp, &key, &data, method) == -1)
447 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
451 /* Create insecure data. (legacy version) */
453 COMPACT(pwd.pw_name);
455 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
457 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
459 memmove(p, &pw_change, sizeof(pw_change));
460 p += sizeof(pw_change);
461 COMPACT(pwd.pw_class);
462 COMPACT(pwd.pw_gecos);
464 COMPACT(pwd.pw_shell);
465 memmove(p, &pw_expire, sizeof(pw_expire));
466 p += sizeof(pw_expire);
467 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
468 p += sizeof pwd.pw_fields;
471 /* Create secure data. (legacy version) */
473 COMPACT(pwd.pw_name);
474 COMPACT(pwd.pw_passwd);
475 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
477 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
479 memmove(p, &pw_change, sizeof(pw_change));
480 p += sizeof(pw_change);
481 COMPACT(pwd.pw_class);
482 COMPACT(pwd.pw_gecos);
484 COMPACT(pwd.pw_shell);
485 memmove(p, &pw_expire, sizeof(pw_expire));
486 p += sizeof(pw_expire);
487 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
488 p += sizeof 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);
501 memmove(tbuf + 1, &cnt, sizeof(cnt));
502 key.size = sizeof(cnt) + 1;
503 if ((dp->put)(dp, &key, &data, method) == -1)
506 /* Store insecure by uid. */
507 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
508 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
509 key.size = sizeof(pwd.pw_uid) + 1;
510 if ((dp->put)(dp, &key, &data, methoduid) == -1)
513 /* Store secure by name. */
514 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME);
515 len = strlen(pwd.pw_name);
516 memmove(tbuf + 1, pwd.pw_name, len);
518 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
521 /* Store secure by number. */
522 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
523 memmove(tbuf + 1, &cnt, sizeof(cnt));
524 key.size = sizeof(cnt) + 1;
525 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
528 /* Store secure by uid. */
529 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
530 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
531 key.size = sizeof(pwd.pw_uid) + 1;
532 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
535 /* Store insecure and secure special plus and special minus */
536 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
537 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
538 memmove(tbuf + 1, &ypcnt, sizeof(cnt));
540 key.size = sizeof(cnt) + 1;
541 if ((dp->put)(dp, &key, &data, method) == -1)
543 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
547 /* Create original format password file entry */
548 if (is_comment && makeold){ /* copy comments */
549 if (fprintf(oldfp, "%s\n", line) < 0)
551 } else if (makeold) {
555 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
556 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
558 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
559 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
560 pwd.pw_fields & _PWF_GID ? gidstr : "",
561 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
565 /* If YP enabled, set flag. */
567 buf[0] = yp_enabled + 2;
570 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
571 if ((dp->put)(dp, &key, &data, method) == -1)
573 if ((sdp->put)(sdp, &key, &data, method) == -1)
575 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED);
577 if ((dp->put)(dp, &key, &data, method) == -1)
579 if ((sdp->put)(sdp, &key, &data, method) == -1)
583 if ((dp->close)(dp) == -1)
585 if ((sdp->close)(sdp) == -1)
589 if (fclose(oldfp) == EOF)
593 /* Set master.passwd permissions, in case caller forgot. */
594 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
596 /* Install as the real password files. */
597 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
598 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
600 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
601 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
604 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
605 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
609 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
610 * all use flock(2) on it to block other incarnations of themselves.
611 * The rename means that everything is unlocked, as the original file
612 * can no longer be accessed.
614 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
618 * Close locked password file after rename()
620 if (fclose(fp) == EOF)
634 if (!fgets(line, sizeof(line), fp))
638 * ``... if I swallow anything evil, put your fingers down my
642 if (!(p = strchr(line, '\n'))) {
644 * XXX: This may also happen if the last line in a
645 * file does not have a trailing newline.
647 warnx("line #%d too long", lcnt);
654 * Ignore comments: ^[ \t]*#
656 for (p = line; *p != '\0'; p++)
657 if (*p != ' ' && *p != '\t')
659 if (*p == '#' || *p == '\0') {
665 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
666 warnx("at line #%d", lcnt);
667 fmt: errno = EFTYPE; /* XXX */
679 static char buf[MAXBSIZE];
680 int from_fd, rcount, to_fd, wcount;
682 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
684 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
686 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
687 wcount = write(to_fd, buf, rcount);
688 if (rcount != wcount || wcount == -1) {
691 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
699 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
710 char buf[MAXPATHLEN];
712 if (rename(from, to)) {
714 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
733 char buf[MAXPATHLEN];
737 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
741 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
745 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
754 (void)fprintf(stderr,
755 "usage: pwd_mkdb [-C] [-N] [-i] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n");