2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/endian.h>
35 #include <arpa/inet.h>
54 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
55 #define PERM_SECURE (S_IRUSR|S_IWUSR)
56 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
57 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
59 static HASHINFO openinfo = {
63 2048 * 1024, /* cachesize */
65 BIG_ENDIAN /* lorder */
68 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
69 static struct passwd pwd; /* password structure */
70 static char *pname; /* password file name */
71 static char prefix[MAXPATHLEN];
73 static int is_comment; /* flag for comments */
74 static char line[LINE_MAX];
77 void error(const char *);
78 void cp(char *, char *, mode_t mode);
79 void mv(char *, char *);
80 int scan(FILE *, struct passwd *);
81 static void usage(void);
84 main(int argc, char *argv[])
86 static char verskey[] = _PWD_VERSION_KEY;
87 char version = _PWD_CURRENT_VERSION;
92 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
97 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
98 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
99 char buf2[MAXPATHLEN];
100 char sbuf2[MAXPATHLEN];
102 u_int method, methoduid;
103 int Cflag, dflag, iflag;
106 iflag = dflag = Cflag = 0;
107 strcpy(prefix, _PATH_PWD);
111 while ((ch = getopt(argc, argv, "Cd:iNps:u:v")) != -1)
113 case 'C': /* verify only */
118 strlcpy(prefix, optarg, sizeof(prefix));
123 case 'N': /* do not wait for lock */
124 nblock = LOCK_NB; /* will fail if locked */
126 case 'p': /* create V7 "file.orig" */
129 case 's': /* change default cachesize */
130 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
132 case 'u': /* only update this record */
135 case 'v': /* backward compatible */
143 if (argc != 1 || (username && (*username == '+' || *username == '-')))
147 * This could be changed to allow the user to interrupt.
148 * Probably not worth the effort.
151 sigaddset(&set, SIGTSTP);
152 sigaddset(&set, SIGHUP);
153 sigaddset(&set, SIGINT);
154 sigaddset(&set, SIGQUIT);
155 sigaddset(&set, SIGTERM);
156 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
158 /* We don't care what the user wants. */
164 * Open and lock the original password file. We have to check
165 * the hardlink count after we get the lock to handle any potential
166 * unlink/rename race.
168 * This lock is necessary when someone runs pwd_mkdb manually, directly
169 * on master.passwd, to handle the case where a user might try to
170 * change his password while pwd_mkdb is running.
175 if (!(fp = fopen(pname, "r")))
177 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
179 if (fstat(fileno(fp), &st) < 0)
181 if (st.st_nlink != 0)
187 /* check only if password database is valid */
189 while (scan(fp, &pwd))
190 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
191 warnx("%s: username too long", pwd.pw_name);
197 /* Open the temporary insecure password database. */
198 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
199 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
203 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
204 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
206 clean = FILE_INSECURE;
207 cp(buf2, buf, PERM_INSECURE);
209 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
214 cp(sbuf2, sbuf, PERM_SECURE);
216 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
221 * Do some trouble to check if we should store this users
222 * uid. Don't use getpwnam/getpwuid as that interferes
225 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
230 key.size = sizeof(verskey)-1;
231 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
232 use_version = *(unsigned char *)data.data;
235 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
236 len = strlen(username);
238 /* Only check that username fits in buffer */
239 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
240 key.data = (u_char *)buf;
242 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
243 p = (char *)data.data;
245 /* jump over pw_name and pw_passwd, to get to pw_uid */
251 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
252 memmove(buf + 1, p, sizeof(store));
253 key.data = (u_char *)buf;
254 key.size = sizeof(store) + 1;
256 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
257 /* First field of data.data holds pw_pwname */
258 if (!strcmp(data.data, username))
261 methoduid = R_NOOVERWRITE;
263 methoduid = R_NOOVERWRITE;
266 methoduid = R_NOOVERWRITE;
268 if ((pw_db->close)(pw_db))
269 error("close pw_db");
273 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
276 clean = FILE_INSECURE;
279 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
284 method = R_NOOVERWRITE;
285 methoduid = R_NOOVERWRITE;
289 * Open file for old password file. Minor trickiness -- don't want to
290 * chance the file already existing, since someone (stupidly) might
291 * still be using this for permission checking. So, open it first and
292 * fdopen the resulting fd. The resulting file should be readable by
296 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
298 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
300 if ((oldfp = fdopen(tfd, "w")) == NULL)
306 * The databases actually contain three copies of the original data.
307 * Each password file entry is converted into a rough approximation
308 * of a ``struct passwd'', with the strings placed inline. This
309 * object is then stored as the data for three separate keys. The
310 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
311 * character. The second key is the pw_uid field prepended by the
312 * _PW_KEYBYUID character. The third key is the line number in the
313 * original file prepended by the _PW_KEYBYNUM character. (The special
314 * characters are prepended to ensure that the keys do not collide.)
316 /* In order to transition this file into a machine-independent
317 * form, we have to change the format of entries. However, since
318 * older binaries will still expect the old MD format entries, we
319 * create those as usual and use versioned tags for the new entries.
321 if (username == NULL) {
322 /* Do not add the VERSION tag when updating a single
323 * user. When operating on `old format' databases, this
324 * would result in applications `seeing' only the updated
328 key.size = sizeof(verskey)-1;
329 data.data = &version;
331 if ((dp->put)(dp, &key, &data, 0) == -1)
333 if ((sdp->put)(sdp, &key, &data, 0) == -1)
337 data.data = (u_char *)buf;
338 sdata.data = (u_char *)sbuf;
339 key.data = (u_char *)tbuf;
340 for (cnt = 1; scan(fp, &pwd); ++cnt) {
342 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
348 #define COMPACT(e) t = e; while ((*p++ = *t++));
349 #define SCALAR(e) store = htonl((uint32_t)(e)); \
350 memmove(p, &store, sizeof(store)); \
352 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
353 memmove(p, &store, sizeof(store)); \
355 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
357 bswap32((uint32_t)(e)))
359 (!username || (strcmp(username, pwd.pw_name) == 0))) {
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));
443 key.size = sizeof(store) + 1;
444 if ((dp->put)(dp, &key, &data, method) == -1)
446 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
451 * Create original style password file entry.
453 * Don't copy comments since this could reveal encrypted
454 * passwords if entries have been simply commented out
457 if (makeold && !is_comment) {
461 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
462 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
464 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
465 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
466 pwd.pw_fields & _PWF_GID ? gidstr : "",
467 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
471 /* If YP enabled, set flag. */
473 buf[0] = yp_enabled + 2;
476 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
477 if ((dp->put)(dp, &key, &data, method) == -1)
479 if ((sdp->put)(sdp, &key, &data, method) == -1)
483 if ((dp->close)(dp) == -1)
485 if ((sdp->close)(sdp) == -1)
489 if (fclose(oldfp) == EOF)
493 /* Set master.passwd permissions, in case caller forgot. */
494 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
496 /* Install as the real password files. */
497 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
498 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
500 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
501 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
504 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
505 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
509 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
510 * all use flock(2) on it to block other incarnations of themselves.
511 * The rename means that everything is unlocked, as the original file
512 * can no longer be accessed.
514 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
518 * Close locked password file after rename()
520 if (fclose(fp) == EOF)
527 scan(FILE *fp, struct passwd *pw)
533 p = fgetln(fp, &len);
538 * ``... if I swallow anything evil, put your fingers down my
542 if (len > 0 && p[len - 1] == '\n')
544 if (len >= sizeof(line) - 1) {
545 warnx("line #%d too long", lcnt);
548 memcpy(line, p, len);
552 * Ignore comments: ^[ \t]*#
554 for (p = line; *p != '\0'; p++)
555 if (*p != ' ' && *p != '\t')
557 if (*p == '#' || *p == '\0') {
563 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
564 warnx("at line #%d", lcnt);
565 fmt: errno = EFTYPE; /* XXX */
573 cp(char *from, char *to, mode_t mode)
575 static char buf[MAXBSIZE];
576 int from_fd, rcount, to_fd, wcount;
578 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
580 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
582 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
583 wcount = write(to_fd, buf, rcount);
584 if (rcount != wcount || wcount == -1) {
587 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
595 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
603 mv(char *from, char *to)
605 char buf[MAXPATHLEN];
610 * Make sure file is safe on disk. To improve performance we will call
611 * fsync() to the directory where file lies
613 if (rename(from, to) != 0 ||
614 (to_dir = dirname(to)) == NULL ||
615 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
616 fsync(to_dir_fd) != 0) {
618 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
630 error(const char *name)
641 char buf[MAXPATHLEN];
645 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
649 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
653 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
662 (void)fprintf(stderr,
663 "usage: pwd_mkdb [-CiNp] [-d directory] [-s cachesize] [-u username] file\n");