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)];
98 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
99 char buf2[MAXPATHLEN];
100 char sbuf2[MAXPATHLEN];
103 u_int method, methoduid;
104 int Cflag, dflag, iflag;
107 iflag = dflag = Cflag = 0;
108 strcpy(prefix, _PATH_PWD);
112 while ((ch = getopt(argc, argv, "Cd:iNps:u:v")) != -1)
114 case 'C': /* verify only */
119 strlcpy(prefix, optarg, sizeof(prefix));
124 case 'N': /* do not wait for lock */
125 nblock = LOCK_NB; /* will fail if locked */
127 case 'p': /* create V7 "file.orig" */
130 case 's': /* change default cachesize */
131 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
133 case 'u': /* only update this record */
136 case 'v': /* backward compatible */
144 if (argc != 1 || (username && (*username == '+' || *username == '-')))
148 * This could be changed to allow the user to interrupt.
149 * Probably not worth the effort.
152 sigaddset(&set, SIGTSTP);
153 sigaddset(&set, SIGHUP);
154 sigaddset(&set, SIGINT);
155 sigaddset(&set, SIGQUIT);
156 sigaddset(&set, SIGTERM);
157 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
159 /* We don't care what the user wants. */
165 * Open and lock the original password file. We have to check
166 * the hardlink count after we get the lock to handle any potential
167 * unlink/rename race.
169 * This lock is necessary when someone runs pwd_mkdb manually, directly
170 * on master.passwd, to handle the case where a user might try to
171 * change his password while pwd_mkdb is running.
176 if (!(fp = fopen(pname, "r")))
178 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
180 if (fstat(fileno(fp), &st) < 0)
182 if (st.st_nlink != 0)
188 /* check only if password database is valid */
190 while (scan(fp, &pwd))
191 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
192 warnx("%s: username too long", pwd.pw_name);
198 /* Open the temporary insecure password database. */
199 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
200 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
204 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
205 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
207 clean = FILE_INSECURE;
208 cp(buf2, buf, PERM_INSECURE);
210 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
215 cp(sbuf2, sbuf, PERM_SECURE);
217 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
222 * Do some trouble to check if we should store this users
223 * uid. Don't use getpwnam/getpwuid as that interferes
226 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
231 key.size = sizeof(verskey)-1;
232 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
233 use_version = *(unsigned char *)data.data;
236 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
237 len = strlen(username);
239 /* Only check that username fits in buffer */
240 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
241 key.data = (u_char *)buf;
243 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
244 p = (char *)data.data;
246 /* jump over pw_name and pw_passwd, to get to pw_uid */
252 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
253 memmove(buf + 1, p, sizeof(store));
254 key.data = (u_char *)buf;
255 key.size = sizeof(store) + 1;
257 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
258 /* First field of data.data holds pw_pwname */
259 if (!strcmp(data.data, username))
262 methoduid = R_NOOVERWRITE;
264 methoduid = R_NOOVERWRITE;
267 methoduid = R_NOOVERWRITE;
269 if ((pw_db->close)(pw_db))
270 error("close pw_db");
274 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
277 clean = FILE_INSECURE;
280 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
285 method = R_NOOVERWRITE;
286 methoduid = R_NOOVERWRITE;
290 * Open file for old password file. Minor trickiness -- don't want to
291 * chance the file already existing, since someone (stupidly) might
292 * still be using this for permission checking. So, open it first and
293 * fdopen the resulting fd. The resulting file should be readable by
297 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
299 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
301 if ((oldfp = fdopen(tfd, "w")) == NULL)
307 * The databases actually contain three copies of the original data.
308 * Each password file entry is converted into a rough approximation
309 * of a ``struct passwd'', with the strings placed inline. This
310 * object is then stored as the data for three separate keys. The
311 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
312 * character. The second key is the pw_uid field prepended by the
313 * _PW_KEYBYUID character. The third key is the line number in the
314 * original file prepended by the _PW_KEYBYNUM character. (The special
315 * characters are prepended to ensure that the keys do not collide.)
317 /* In order to transition this file into a machine-independent
318 * form, we have to change the format of entries. However, since
319 * older binaries will still expect the old MD format entries, we
320 * create those as usual and use versioned tags for the new entries.
322 if (username == NULL) {
323 /* Do not add the VERSION tag when updating a single
324 * user. When operating on `old format' databases, this
325 * would result in applications `seeing' only the updated
329 key.size = sizeof(verskey)-1;
330 data.data = &version;
332 if ((dp->put)(dp, &key, &data, 0) == -1)
334 if ((sdp->put)(sdp, &key, &data, 0) == -1)
338 data.data = (u_char *)buf;
339 sdata.data = (u_char *)sbuf;
340 key.data = (u_char *)tbuf;
341 for (cnt = 1; scan(fp, &pwd); ++cnt) {
343 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
349 #define COMPACT(e) t = e; while ((*p++ = *t++));
350 #define SCALAR(e) store = htonl((uint32_t)(e)); \
351 memmove(p, &store, sizeof(store)); \
353 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
354 memmove(p, &store, sizeof(store)); \
356 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
358 bswap32((uint32_t)(e)))
360 (!username || (strcmp(username, pwd.pw_name) == 0))) {
361 /* Create insecure data. */
363 COMPACT(pwd.pw_name);
367 SCALAR(pwd.pw_change);
368 COMPACT(pwd.pw_class);
369 COMPACT(pwd.pw_gecos);
371 COMPACT(pwd.pw_shell);
372 SCALAR(pwd.pw_expire);
373 SCALAR(pwd.pw_fields);
376 /* Create secure data. */
378 COMPACT(pwd.pw_name);
379 COMPACT(pwd.pw_passwd);
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);
389 sdata.size = p - sbuf;
391 /* Store insecure by name. */
392 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
393 len = strlen(pwd.pw_name);
394 memmove(tbuf + 1, pwd.pw_name, len);
396 if ((dp->put)(dp, &key, &data, method) == -1)
399 /* Store insecure by number. */
400 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
402 memmove(tbuf + 1, &store, sizeof(store));
403 key.size = sizeof(store) + 1;
404 if ((dp->put)(dp, &key, &data, method) == -1)
407 /* Store insecure by uid. */
408 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
409 store = htonl(pwd.pw_uid);
410 memmove(tbuf + 1, &store, sizeof(store));
411 key.size = sizeof(store) + 1;
412 if ((dp->put)(dp, &key, &data, methoduid) == -1)
415 /* Store secure by name. */
416 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
417 len = strlen(pwd.pw_name);
418 memmove(tbuf + 1, pwd.pw_name, len);
420 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
423 /* Store secure by number. */
424 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
426 memmove(tbuf + 1, &store, sizeof(store));
427 key.size = sizeof(store) + 1;
428 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
431 /* Store secure by uid. */
432 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
433 store = htonl(pwd.pw_uid);
434 memmove(tbuf + 1, &store, sizeof(store));
435 key.size = sizeof(store) + 1;
436 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
439 /* Store insecure and secure special plus and special minus */
440 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
441 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
442 store = htonl(ypcnt);
443 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)
452 * Create original style password file entry.
454 * Don't copy comments since this could reveal encrypted
455 * passwords if entries have been simply commented out
458 if (makeold && !is_comment) {
462 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
463 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
465 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
466 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
467 pwd.pw_fields & _PWF_GID ? gidstr : "",
468 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
472 /* If YP enabled, set flag. */
474 buf[0] = yp_enabled + 2;
477 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
478 if ((dp->put)(dp, &key, &data, method) == -1)
480 if ((sdp->put)(sdp, &key, &data, method) == -1)
484 if ((dp->close)(dp) == -1)
486 if ((sdp->close)(sdp) == -1)
490 if (fclose(oldfp) == EOF)
494 /* Set master.passwd permissions, in case caller forgot. */
495 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
497 /* Install as the real password files. */
498 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
499 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
501 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
502 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
505 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
506 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
510 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
511 * all use flock(2) on it to block other incarnations of themselves.
512 * The rename means that everything is unlocked, as the original file
513 * can no longer be accessed.
515 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
519 * Close locked password file after rename()
521 if (fclose(fp) == EOF)
528 scan(FILE *fp, struct passwd *pw)
534 p = fgetln(fp, &len);
539 * ``... if I swallow anything evil, put your fingers down my
543 if (len > 0 && p[len - 1] == '\n')
545 if (len >= sizeof(line) - 1) {
546 warnx("line #%d too long", lcnt);
549 memcpy(line, p, len);
553 * Ignore comments: ^[ \t]*#
555 for (p = line; *p != '\0'; p++)
556 if (*p != ' ' && *p != '\t')
558 if (*p == '#' || *p == '\0') {
564 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
565 warnx("at line #%d", lcnt);
566 fmt: errno = EFTYPE; /* XXX */
574 cp(char *from, char *to, mode_t mode)
576 static char buf[MAXBSIZE];
577 int from_fd, rcount, to_fd, wcount;
579 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
581 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
583 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
584 wcount = write(to_fd, buf, rcount);
585 if (rcount != wcount || wcount == -1) {
588 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
596 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
604 mv(char *from, char *to)
606 char buf[MAXPATHLEN];
611 * Make sure file is safe on disk. To improve performance we will call
612 * fsync() to the directory where file lies
614 if (rename(from, to) != 0 ||
615 (to_dir = dirname(to)) == NULL ||
616 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
617 fsync(to_dir_fd) != 0) {
619 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
631 error(const char *name)
642 char buf[MAXPATHLEN];
646 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
650 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
654 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
663 (void)fprintf(stderr,
664 "usage: pwd_mkdb [-CiNp] [-d directory] [-s cachesize] [-u username] file\n");