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
33 * @(#) Copyright (c) 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94
35 * $FreeBSD: src/usr.sbin/pwd_mkdb/pwd_mkdb.c,v 1.35 2000/03/09 18:11:16 paul Exp $
36 * $DragonFly: src/usr.sbin/pwd_mkdb/pwd_mkdb.c,v 1.5 2005/12/05 02:40:27 swildner Exp $
39 #include <sys/param.h>
58 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
59 #define PERM_SECURE (S_IRUSR|S_IWUSR)
65 2048 * 1024, /* cachesize */
70 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
71 static struct passwd pwd; /* password structure */
72 static char *pname; /* password file name */
73 static char prefix[MAXPATHLEN];
75 static int is_comment; /* flag for comments */
76 static char line[LINE_MAX];
80 void cp(char *, char *, mode_t mode);
81 void mv(char *, char *);
82 int scan(FILE *, struct passwd *);
83 static void usage(void);
86 main(int argc, char *argv[])
92 int ch, cnt, ypcnt, len, makeold, tfd, yp_enabled = 0;
94 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
95 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
96 char buf2[MAXPATHLEN];
97 char sbuf2[MAXPATHLEN];
99 u_int method, methoduid;
104 strcpy(prefix, _PATH_PWD);
107 while ((ch = getopt(argc, argv, "Cd:ps:u:vN")) != -1)
109 case 'C': /* verify only */
113 strncpy(prefix, optarg, sizeof prefix - 1);
115 case 'p': /* create V7 "file.orig" */
118 case 's': /* change default cachesize */
119 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
121 case 'u': /* only update this record */
124 case 'v': /* backward compatible */
126 case 'N': /* do not wait for lock */
135 if (argc != 1 || (username && (*username == '+' || *username == '-')))
139 * This could be changed to allow the user to interrupt.
140 * Probably not worth the effort.
143 sigaddset(&set, SIGTSTP);
144 sigaddset(&set, SIGHUP);
145 sigaddset(&set, SIGINT);
146 sigaddset(&set, SIGQUIT);
147 sigaddset(&set, SIGTERM);
148 sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
150 /* We don't care what the user wants. */
156 * Open and lock the original password file. We have to check
157 * the hardlink count after we get the lock to handle any potential
158 * unlink/rename race.
160 * This lock is necessary when someone runs pwd_mkdb manually, directly
161 * on master.passwd, to handle the case where a user might try to
162 * change his password while pwd_mkdb is running.
167 if (!(fp = fopen(pname, "r")))
169 if (flock(fileno(fp), LOCK_EX|nblock) < 0)
171 if (fstat(fileno(fp), &st) < 0)
173 if (st.st_nlink != 0)
179 /* check only if password database is valid */
181 for (cnt = 1; scan(fp, &pwd); ++cnt);
185 /* Open the temporary insecure password database. */
186 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
187 snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
189 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
190 snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
192 clean = FILE_INSECURE;
193 cp(buf2, buf, PERM_INSECURE);
195 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
200 cp(sbuf2, sbuf, PERM_SECURE);
202 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
207 * Do some trouble to check if we should store this users
208 * uid. Don't use getpwnam/getpwuid as that interferes
211 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
214 buf[0] = _PW_KEYBYNAME;
215 len = strlen(username);
217 /* Only check that username fits in buffer */
218 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
219 key.data = (u_char *)buf;
221 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
222 p = (char *)data.data;
224 /* jump over pw_name and pw_passwd, to get to pw_uid */
230 buf[0] = _PW_KEYBYUID;
231 memmove(buf + 1, p, sizeof(int));
232 key.data = (u_char *)buf;
233 key.size = sizeof(int) + 1;
235 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
236 /* First field of data.data holds pw_pwname */
237 if (!strcmp(data.data, username))
240 methoduid = R_NOOVERWRITE;
242 methoduid = R_NOOVERWRITE;
245 methoduid = R_NOOVERWRITE;
247 if ((pw_db->close)(pw_db))
248 error("close pw_db");
252 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
255 clean = FILE_INSECURE;
258 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
263 method = R_NOOVERWRITE;
264 methoduid = R_NOOVERWRITE;
268 * Open file for old password file. Minor trickiness -- don't want to
269 * chance the file already existing, since someone (stupidly) might
270 * still be using this for permission checking. So, open it first and
271 * fdopen the resulting fd. The resulting file should be readable by
275 snprintf(buf, sizeof(buf), "%s.orig", pname);
277 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
279 if ((oldfp = fdopen(tfd, "w")) == NULL)
285 * The databases actually contain three copies of the original data.
286 * Each password file entry is converted into a rough approximation
287 * of a ``struct passwd'', with the strings placed inline. This
288 * object is then stored as the data for three separate keys. The
289 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
290 * character. The second key is the pw_uid field prepended by the
291 * _PW_KEYBYUID character. The third key is the line number in the
292 * original file prepended by the _PW_KEYBYNUM character. (The special
293 * characters are prepended to ensure that the keys do not collide.)
296 data.data = (u_char *)buf;
297 sdata.data = (u_char *)sbuf;
298 key.data = (u_char *)tbuf;
299 for (cnt = 1; scan(fp, &pwd); ++cnt) {
301 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
305 #define COMPACT(e) t = e; while ((*p++ = *t++));
307 (!username || (strcmp(username, pwd.pw_name) == 0))) {
308 /* Create insecure data. */
310 COMPACT(pwd.pw_name);
312 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
314 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
316 memmove(p, &pwd.pw_change, sizeof(time_t));
318 COMPACT(pwd.pw_class);
319 COMPACT(pwd.pw_gecos);
321 COMPACT(pwd.pw_shell);
322 memmove(p, &pwd.pw_expire, sizeof(time_t));
324 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
325 p += sizeof pwd.pw_fields;
328 /* Create secure data. */
330 COMPACT(pwd.pw_name);
331 COMPACT(pwd.pw_passwd);
332 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
334 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
336 memmove(p, &pwd.pw_change, sizeof(time_t));
338 COMPACT(pwd.pw_class);
339 COMPACT(pwd.pw_gecos);
341 COMPACT(pwd.pw_shell);
342 memmove(p, &pwd.pw_expire, sizeof(time_t));
344 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
345 p += sizeof pwd.pw_fields;
346 sdata.size = p - sbuf;
348 /* Store insecure by name. */
349 tbuf[0] = _PW_KEYBYNAME;
350 len = strlen(pwd.pw_name);
351 memmove(tbuf + 1, pwd.pw_name, len);
353 if ((dp->put)(dp, &key, &data, method) == -1)
356 /* Store insecure by number. */
357 tbuf[0] = _PW_KEYBYNUM;
358 memmove(tbuf + 1, &cnt, sizeof(cnt));
359 key.size = sizeof(cnt) + 1;
360 if ((dp->put)(dp, &key, &data, method) == -1)
363 /* Store insecure by uid. */
364 tbuf[0] = _PW_KEYBYUID;
365 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
366 key.size = sizeof(pwd.pw_uid) + 1;
367 if ((dp->put)(dp, &key, &data, methoduid) == -1)
370 /* Store secure by name. */
371 tbuf[0] = _PW_KEYBYNAME;
372 len = strlen(pwd.pw_name);
373 memmove(tbuf + 1, pwd.pw_name, len);
375 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
378 /* Store secure by number. */
379 tbuf[0] = _PW_KEYBYNUM;
380 memmove(tbuf + 1, &cnt, sizeof(cnt));
381 key.size = sizeof(cnt) + 1;
382 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
385 /* Store secure by uid. */
386 tbuf[0] = _PW_KEYBYUID;
387 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
388 key.size = sizeof(pwd.pw_uid) + 1;
389 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
392 /* Store insecure and secure special plus and special minus */
393 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
394 tbuf[0] = _PW_KEYYPBYNUM;
395 memmove(tbuf + 1, &ypcnt, sizeof(cnt));
397 key.size = sizeof(cnt) + 1;
398 if ((dp->put)(dp, &key, &data, method) == -1)
400 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
404 /* Create original format password file entry */
405 if (is_comment && makeold){ /* copy comments */
406 if (fprintf(oldfp, "%s\n", line) < 0)
408 } else if (makeold) {
412 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
413 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
415 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
416 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
417 pwd.pw_fields & _PWF_GID ? gidstr : "",
418 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
422 /* If YP enabled, set flag. */
424 buf[0] = yp_enabled + 2;
426 tbuf[0] = _PW_KEYYPENABLED;
428 if ((dp->put)(dp, &key, &data, method) == -1)
430 if ((sdp->put)(sdp, &key, &data, method) == -1)
434 if ((dp->close)(dp) == -1)
436 if ((sdp->close)(sdp) == -1)
440 if (fclose(oldfp) == EOF)
444 /* Set master.passwd permissions, in case caller forgot. */
445 fchmod(fileno(fp), S_IRUSR|S_IWUSR);
447 /* Install as the real password files. */
448 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
449 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
451 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
452 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
455 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
456 snprintf(buf, sizeof(buf), "%s.orig", pname);
460 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
461 * all use flock(2) on it to block other incarnations of themselves.
462 * The rename means that everything is unlocked, as the original file
463 * can no longer be accessed.
465 snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
469 * Close locked password file after rename()
471 if (fclose(fp) == EOF)
478 scan(FILE *fp, struct passwd *pw)
483 if (!fgets(line, sizeof(line), fp))
487 * ``... if I swallow anything evil, put your fingers down my
491 if (!(p = strchr(line, '\n'))) {
492 warnx("line too long");
499 * Ignore comments: ^[ \t]*#
501 for (p = line; *p != '\0'; p++)
502 if (*p != ' ' && *p != '\t')
504 if (*p == '#' || *p == '\0') {
510 if (!pw_scan(line, pw)) {
511 warnx("at line #%d", lcnt);
512 fmt: errno = EFTYPE; /* XXX */
520 cp(char *from, char *to, mode_t mode)
522 static char buf[MAXBSIZE];
523 int from_fd, rcount, to_fd, wcount;
525 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
527 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
529 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
530 wcount = write(to_fd, buf, rcount);
531 if (rcount != wcount || wcount == -1) {
534 snprintf(buf, sizeof(buf), "%s to %s", from, to);
542 snprintf(buf, sizeof(buf), "%s to %s", from, to);
550 mv(char *from, char *to)
552 char buf[MAXPATHLEN];
554 if (rename(from, to)) {
556 snprintf(buf, sizeof(buf), "%s to %s", from, to);
573 char buf[MAXPATHLEN];
577 snprintf(buf, sizeof(buf), "%s.orig", pname);
581 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
585 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
595 "usage: pwd_mkdb [-C] [-N] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n");