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[] =
45 "$FreeBSD: src/usr.sbin/pwd_mkdb/pwd_mkdb.c,v 1.35 2000/03/09 18:11:16 paul Exp $";
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;
105 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
106 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
107 char buf2[MAXPATHLEN];
108 char sbuf2[MAXPATHLEN];
110 u_int method, methoduid;
115 strcpy(prefix, _PATH_PWD);
118 while ((ch = getopt(argc, argv, "Cd:ps:u:vN")) != -1)
120 case 'C': /* verify only */
124 strncpy(prefix, optarg, sizeof prefix - 1);
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 */
137 case 'N': /* do not wait for lock */
146 if (argc != 1 || (username && (*username == '+' || *username == '-')))
150 * This could be changed to allow the user to interrupt.
151 * Probably not worth the effort.
154 sigaddset(&set, SIGTSTP);
155 sigaddset(&set, SIGHUP);
156 sigaddset(&set, SIGINT);
157 sigaddset(&set, SIGQUIT);
158 sigaddset(&set, SIGTERM);
159 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
161 /* We don't care what the user wants. */
167 * Open and lock the original password file. We have to check
168 * the hardlink count after we get the lock to handle any potential
169 * unlink/rename race.
171 * This lock is necessary when someone runs pwd_mkdb manually, directly
172 * on master.passwd, to handle the case where a user might try to
173 * change his password while pwd_mkdb is running.
178 if (!(fp = fopen(pname, "r")))
180 if (flock(fileno(fp), LOCK_EX|nblock) < 0)
182 if (fstat(fileno(fp), &st) < 0)
184 if (st.st_nlink != 0)
190 /* check only if password database is valid */
192 for (cnt = 1; scan(fp, &pwd); ++cnt);
196 /* Open the temporary insecure password database. */
197 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
198 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
200 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
201 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
203 clean = FILE_INSECURE;
204 cp(buf2, buf, PERM_INSECURE);
206 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
211 cp(sbuf2, sbuf, PERM_SECURE);
213 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
218 * Do some trouble to check if we should store this users
219 * uid. Don't use getpwnam/getpwuid as that interferes
222 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
225 buf[0] = _PW_KEYBYNAME;
226 len = strlen(username);
228 /* Only check that username fits in buffer */
229 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
230 key.data = (u_char *)buf;
232 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
233 p = (char *)data.data;
235 /* jump over pw_name and pw_passwd, to get to pw_uid */
241 buf[0] = _PW_KEYBYUID;
242 memmove(buf + 1, p, sizeof(int));
243 key.data = (u_char *)buf;
244 key.size = sizeof(int) + 1;
246 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
247 /* First field of data.data holds pw_pwname */
248 if (!strcmp(data.data, username))
251 methoduid = R_NOOVERWRITE;
253 methoduid = R_NOOVERWRITE;
256 methoduid = R_NOOVERWRITE;
258 if ((pw_db->close)(pw_db))
259 error("close pw_db");
263 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
266 clean = FILE_INSECURE;
269 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
274 method = R_NOOVERWRITE;
275 methoduid = R_NOOVERWRITE;
279 * Open file for old password file. Minor trickiness -- don't want to
280 * chance the file already existing, since someone (stupidly) might
281 * still be using this for permission checking. So, open it first and
282 * fdopen the resulting fd. The resulting file should be readable by
286 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
288 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
290 if ((oldfp = fdopen(tfd, "w")) == NULL)
296 * The databases actually contain three copies of the original data.
297 * Each password file entry is converted into a rough approximation
298 * of a ``struct passwd'', with the strings placed inline. This
299 * object is then stored as the data for three separate keys. The
300 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
301 * character. The second key is the pw_uid field prepended by the
302 * _PW_KEYBYUID character. The third key is the line number in the
303 * original file prepended by the _PW_KEYBYNUM character. (The special
304 * characters are prepended to ensure that the keys do not collide.)
307 data.data = (u_char *)buf;
308 sdata.data = (u_char *)sbuf;
309 key.data = (u_char *)tbuf;
310 for (cnt = 1; scan(fp, &pwd); ++cnt) {
312 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
316 #define COMPACT(e) t = e; while ((*p++ = *t++));
318 (!username || (strcmp(username, pwd.pw_name) == 0))) {
319 /* Create insecure data. */
321 COMPACT(pwd.pw_name);
323 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
325 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
327 memmove(p, &pwd.pw_change, sizeof(time_t));
329 COMPACT(pwd.pw_class);
330 COMPACT(pwd.pw_gecos);
332 COMPACT(pwd.pw_shell);
333 memmove(p, &pwd.pw_expire, sizeof(time_t));
335 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
336 p += sizeof pwd.pw_fields;
339 /* Create secure data. */
341 COMPACT(pwd.pw_name);
342 COMPACT(pwd.pw_passwd);
343 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
345 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
347 memmove(p, &pwd.pw_change, sizeof(time_t));
349 COMPACT(pwd.pw_class);
350 COMPACT(pwd.pw_gecos);
352 COMPACT(pwd.pw_shell);
353 memmove(p, &pwd.pw_expire, sizeof(time_t));
355 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
356 p += sizeof pwd.pw_fields;
357 sdata.size = p - sbuf;
359 /* Store insecure by name. */
360 tbuf[0] = _PW_KEYBYNAME;
361 len = strlen(pwd.pw_name);
362 memmove(tbuf + 1, pwd.pw_name, len);
364 if ((dp->put)(dp, &key, &data, method) == -1)
367 /* Store insecure by number. */
368 tbuf[0] = _PW_KEYBYNUM;
369 memmove(tbuf + 1, &cnt, sizeof(cnt));
370 key.size = sizeof(cnt) + 1;
371 if ((dp->put)(dp, &key, &data, method) == -1)
374 /* Store insecure by uid. */
375 tbuf[0] = _PW_KEYBYUID;
376 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
377 key.size = sizeof(pwd.pw_uid) + 1;
378 if ((dp->put)(dp, &key, &data, methoduid) == -1)
381 /* Store secure by name. */
382 tbuf[0] = _PW_KEYBYNAME;
383 len = strlen(pwd.pw_name);
384 memmove(tbuf + 1, pwd.pw_name, len);
386 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
389 /* Store secure by number. */
390 tbuf[0] = _PW_KEYBYNUM;
391 memmove(tbuf + 1, &cnt, sizeof(cnt));
392 key.size = sizeof(cnt) + 1;
393 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
396 /* Store secure by uid. */
397 tbuf[0] = _PW_KEYBYUID;
398 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
399 key.size = sizeof(pwd.pw_uid) + 1;
400 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
403 /* Store insecure and secure special plus and special minus */
404 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
405 tbuf[0] = _PW_KEYYPBYNUM;
406 memmove(tbuf + 1, &ypcnt, sizeof(cnt));
408 key.size = sizeof(cnt) + 1;
409 if ((dp->put)(dp, &key, &data, method) == -1)
411 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
415 /* Create original format password file entry */
416 if (is_comment && makeold){ /* copy comments */
417 if (fprintf(oldfp, "%s\n", line) < 0)
419 } else if (makeold) {
423 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
424 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
426 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
427 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
428 pwd.pw_fields & _PWF_GID ? gidstr : "",
429 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
433 /* If YP enabled, set flag. */
435 buf[0] = yp_enabled + 2;
437 tbuf[0] = _PW_KEYYPENABLED;
439 if ((dp->put)(dp, &key, &data, method) == -1)
441 if ((sdp->put)(sdp, &key, &data, method) == -1)
445 if ((dp->close)(dp) == -1)
447 if ((sdp->close)(sdp) == -1)
451 if (fclose(oldfp) == EOF)
455 /* Set master.passwd permissions, in case caller forgot. */
456 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
458 /* Install as the real password files. */
459 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
460 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
462 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
463 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
466 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
467 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
471 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
472 * all use flock(2) on it to block other incarnations of themselves.
473 * The rename means that everything is unlocked, as the original file
474 * can no longer be accessed.
476 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
480 * Close locked password file after rename()
482 if (fclose(fp) == EOF)
496 if (!fgets(line, sizeof(line), fp))
500 * ``... if I swallow anything evil, put your fingers down my
504 if (!(p = strchr(line, '\n'))) {
505 warnx("line too long");
512 * Ignore comments: ^[ \t]*#
514 for (p = line; *p != '\0'; p++)
515 if (*p != ' ' && *p != '\t')
517 if (*p == '#' || *p == '\0') {
523 if (!pw_scan(line, pw)) {
524 warnx("at line #%d", lcnt);
525 fmt: errno = EFTYPE; /* XXX */
537 static char buf[MAXBSIZE];
538 int from_fd, rcount, to_fd, wcount;
540 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
542 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
544 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
545 wcount = write(to_fd, buf, rcount);
546 if (rcount != wcount || wcount == -1) {
549 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
557 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
568 char buf[MAXPATHLEN];
570 if (rename(from, to)) {
572 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
591 char buf[MAXPATHLEN];
595 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
599 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
603 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
612 (void)fprintf(stderr,
613 "usage: pwd_mkdb [-C] [-N] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n");