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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
44 static const char rcsid[] =
48 #include <sys/param.h>
67 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
68 #define PERM_SECURE (S_IRUSR|S_IWUSR)
74 2048 * 1024, /* cachesize */
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];
87 void cleanup __P((void));
88 void error __P((char *));
89 void cp __P((char *, char *, mode_t mode));
90 void mv __P((char *, char *));
91 int scan __P((FILE *, struct passwd *));
92 static void usage __P((void));
100 DBT data, sdata, key;
103 int ch, cnt, ypcnt, len, makeold, tfd, yp_enabled = 0;
104 int32_t pw_change, pw_expire;
106 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
107 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
108 char buf2[MAXPATHLEN];
109 char sbuf2[MAXPATHLEN];
111 u_int method, methoduid;
116 strcpy(prefix, _PATH_PWD);
119 while ((ch = getopt(argc, argv, "Cd:ps:u:vN")) != -1)
121 case 'C': /* verify only */
125 strncpy(prefix, optarg, sizeof prefix - 1);
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 */
138 case 'N': /* do not wait for lock */
147 if (argc != 1 || (username && (*username == '+' || *username == '-')))
151 * This could be changed to allow the user to interrupt.
152 * Probably not worth the effort.
155 sigaddset(&set, SIGTSTP);
156 sigaddset(&set, SIGHUP);
157 sigaddset(&set, SIGINT);
158 sigaddset(&set, SIGQUIT);
159 sigaddset(&set, SIGTERM);
160 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
162 /* We don't care what the user wants. */
168 * Open and lock the original password file. We have to check
169 * the hardlink count after we get the lock to handle any potential
170 * unlink/rename race.
172 * This lock is necessary when someone runs pwd_mkdb manually, directly
173 * on master.passwd, to handle the case where a user might try to
174 * change his password while pwd_mkdb is running.
179 if (!(fp = fopen(pname, "r")))
181 if (flock(fileno(fp), LOCK_EX|nblock) < 0)
183 if (fstat(fileno(fp), &st) < 0)
185 if (st.st_nlink != 0)
191 /* check only if password database is valid */
193 for (cnt = 1; scan(fp, &pwd); ++cnt);
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);
201 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
202 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
204 clean = FILE_INSECURE;
205 cp(buf2, buf, PERM_INSECURE);
207 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
212 cp(sbuf2, sbuf, PERM_SECURE);
214 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
219 * Do some trouble to check if we should store this users
220 * uid. Don't use getpwnam/getpwuid as that interferes
223 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
226 buf[0] = _PW_KEYBYNAME;
227 len = strlen(username);
229 /* Only check that username fits in buffer */
230 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
231 key.data = (u_char *)buf;
233 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
234 p = (char *)data.data;
236 /* jump over pw_name and pw_passwd, to get to pw_uid */
242 buf[0] = _PW_KEYBYUID;
243 memmove(buf + 1, p, sizeof(int));
244 key.data = (u_char *)buf;
245 key.size = sizeof(int) + 1;
247 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
248 /* First field of data.data holds pw_pwname */
249 if (!strcmp(data.data, username))
252 methoduid = R_NOOVERWRITE;
254 methoduid = R_NOOVERWRITE;
257 methoduid = R_NOOVERWRITE;
259 if ((pw_db->close)(pw_db))
260 error("close pw_db");
264 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
267 clean = FILE_INSECURE;
270 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
275 method = R_NOOVERWRITE;
276 methoduid = R_NOOVERWRITE;
280 * Open file for old password file. Minor trickiness -- don't want to
281 * chance the file already existing, since someone (stupidly) might
282 * still be using this for permission checking. So, open it first and
283 * fdopen the resulting fd. The resulting file should be readable by
287 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
289 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
291 if ((oldfp = fdopen(tfd, "w")) == NULL)
297 * The databases actually contain three copies of the original data.
298 * Each password file entry is converted into a rough approximation
299 * of a ``struct passwd'', with the strings placed inline. This
300 * object is then stored as the data for three separate keys. The
301 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
302 * character. The second key is the pw_uid field prepended by the
303 * _PW_KEYBYUID character. The third key is the line number in the
304 * original file prepended by the _PW_KEYBYNUM character. (The special
305 * characters are prepended to ensure that the keys do not collide.)
308 data.data = (u_char *)buf;
309 sdata.data = (u_char *)sbuf;
310 key.data = (u_char *)tbuf;
311 for (cnt = 1; scan(fp, &pwd); ++cnt) {
313 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
317 #define COMPACT(e) t = e; while ((*p++ = *t++));
319 (!username || (strcmp(username, pwd.pw_name) == 0))) {
320 pw_change = pwd.pw_change;
321 pw_expire = pwd.pw_expire;
322 /* Create insecure data. */
324 COMPACT(pwd.pw_name);
326 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
328 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
330 memmove(p, &pw_change, sizeof(pw_change));
331 p += sizeof(pw_change);
332 COMPACT(pwd.pw_class);
333 COMPACT(pwd.pw_gecos);
335 COMPACT(pwd.pw_shell);
336 memmove(p, &pw_expire, sizeof(pw_expire));
337 p += sizeof(pw_expire);
338 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
339 p += sizeof pwd.pw_fields;
342 /* Create secure data. */
344 COMPACT(pwd.pw_name);
345 COMPACT(pwd.pw_passwd);
346 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
348 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
350 memmove(p, &pw_change, sizeof(pw_change));
351 p += sizeof(pw_change);
352 COMPACT(pwd.pw_class);
353 COMPACT(pwd.pw_gecos);
355 COMPACT(pwd.pw_shell);
356 memmove(p, &pw_expire, sizeof(pw_expire));
357 p += sizeof(pw_expire);
358 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
359 p += sizeof pwd.pw_fields;
360 sdata.size = p - sbuf;
362 /* Store insecure by name. */
363 tbuf[0] = _PW_KEYBYNAME;
364 len = strlen(pwd.pw_name);
365 memmove(tbuf + 1, pwd.pw_name, len);
367 if ((dp->put)(dp, &key, &data, method) == -1)
370 /* Store insecure by number. */
371 tbuf[0] = _PW_KEYBYNUM;
372 memmove(tbuf + 1, &cnt, sizeof(cnt));
373 key.size = sizeof(cnt) + 1;
374 if ((dp->put)(dp, &key, &data, method) == -1)
377 /* Store insecure by uid. */
378 tbuf[0] = _PW_KEYBYUID;
379 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
380 key.size = sizeof(pwd.pw_uid) + 1;
381 if ((dp->put)(dp, &key, &data, methoduid) == -1)
384 /* Store secure by name. */
385 tbuf[0] = _PW_KEYBYNAME;
386 len = strlen(pwd.pw_name);
387 memmove(tbuf + 1, pwd.pw_name, len);
389 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
392 /* Store secure by number. */
393 tbuf[0] = _PW_KEYBYNUM;
394 memmove(tbuf + 1, &cnt, sizeof(cnt));
395 key.size = sizeof(cnt) + 1;
396 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
399 /* Store secure by uid. */
400 tbuf[0] = _PW_KEYBYUID;
401 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
402 key.size = sizeof(pwd.pw_uid) + 1;
403 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
406 /* Store insecure and secure special plus and special minus */
407 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
408 tbuf[0] = _PW_KEYYPBYNUM;
409 memmove(tbuf + 1, &ypcnt, sizeof(cnt));
411 key.size = sizeof(cnt) + 1;
412 if ((dp->put)(dp, &key, &data, method) == -1)
414 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
418 /* Create original format password file entry */
419 if (is_comment && makeold){ /* copy comments */
420 if (fprintf(oldfp, "%s\n", line) < 0)
422 } else if (makeold) {
426 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
427 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
429 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
430 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
431 pwd.pw_fields & _PWF_GID ? gidstr : "",
432 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
436 /* If YP enabled, set flag. */
438 buf[0] = yp_enabled + 2;
440 tbuf[0] = _PW_KEYYPENABLED;
442 if ((dp->put)(dp, &key, &data, method) == -1)
444 if ((sdp->put)(sdp, &key, &data, method) == -1)
448 if ((dp->close)(dp) == -1)
450 if ((sdp->close)(sdp) == -1)
454 if (fclose(oldfp) == EOF)
458 /* Set master.passwd permissions, in case caller forgot. */
459 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
461 /* Install as the real password files. */
462 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
463 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
465 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
466 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
469 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
470 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
474 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
475 * all use flock(2) on it to block other incarnations of themselves.
476 * The rename means that everything is unlocked, as the original file
477 * can no longer be accessed.
479 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
483 * Close locked password file after rename()
485 if (fclose(fp) == EOF)
499 if (!fgets(line, sizeof(line), fp))
503 * ``... if I swallow anything evil, put your fingers down my
507 if (!(p = strchr(line, '\n'))) {
509 * XXX: This may also happen if the last line in a
510 * file does not have a trailing newline.
512 warnx("line #%d too long", lcnt);
519 * Ignore comments: ^[ \t]*#
521 for (p = line; *p != '\0'; p++)
522 if (*p != ' ' && *p != '\t')
524 if (*p == '#' || *p == '\0') {
530 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
531 warnx("at line #%d", lcnt);
532 fmt: errno = EFTYPE; /* XXX */
544 static char buf[MAXBSIZE];
545 int from_fd, rcount, to_fd, wcount;
547 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
549 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
551 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
552 wcount = write(to_fd, buf, rcount);
553 if (rcount != wcount || wcount == -1) {
556 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
564 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
575 char buf[MAXPATHLEN];
577 if (rename(from, to)) {
579 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
598 char buf[MAXPATHLEN];
602 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
606 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
610 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
619 (void)fprintf(stderr,
620 "usage: pwd_mkdb [-C] [-N] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n");