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.4 2004/12/18 22:48:04 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);
94 int ch, cnt, ypcnt, len, makeold, tfd, yp_enabled = 0;
96 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
97 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
98 char buf2[MAXPATHLEN];
99 char sbuf2[MAXPATHLEN];
101 u_int method, methoduid;
106 strcpy(prefix, _PATH_PWD);
109 while ((ch = getopt(argc, argv, "Cd:ps:u:vN")) != -1)
111 case 'C': /* verify only */
115 strncpy(prefix, optarg, sizeof prefix - 1);
117 case 'p': /* create V7 "file.orig" */
120 case 's': /* change default cachesize */
121 openinfo.cachesize = atoi(optarg) * 1024 * 1024;
123 case 'u': /* only update this record */
126 case 'v': /* backward compatible */
128 case 'N': /* do not wait for lock */
137 if (argc != 1 || (username && (*username == '+' || *username == '-')))
141 * This could be changed to allow the user to interrupt.
142 * Probably not worth the effort.
145 sigaddset(&set, SIGTSTP);
146 sigaddset(&set, SIGHUP);
147 sigaddset(&set, SIGINT);
148 sigaddset(&set, SIGQUIT);
149 sigaddset(&set, SIGTERM);
150 sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
152 /* We don't care what the user wants. */
158 * Open and lock the original password file. We have to check
159 * the hardlink count after we get the lock to handle any potential
160 * unlink/rename race.
162 * This lock is necessary when someone runs pwd_mkdb manually, directly
163 * on master.passwd, to handle the case where a user might try to
164 * change his password while pwd_mkdb is running.
169 if (!(fp = fopen(pname, "r")))
171 if (flock(fileno(fp), LOCK_EX|nblock) < 0)
173 if (fstat(fileno(fp), &st) < 0)
175 if (st.st_nlink != 0)
181 /* check only if password database is valid */
183 for (cnt = 1; scan(fp, &pwd); ++cnt);
187 /* Open the temporary insecure password database. */
188 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
189 snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
191 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
192 snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
194 clean = FILE_INSECURE;
195 cp(buf2, buf, PERM_INSECURE);
197 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
202 cp(sbuf2, sbuf, PERM_SECURE);
204 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
209 * Do some trouble to check if we should store this users
210 * uid. Don't use getpwnam/getpwuid as that interferes
213 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
216 buf[0] = _PW_KEYBYNAME;
217 len = strlen(username);
219 /* Only check that username fits in buffer */
220 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
221 key.data = (u_char *)buf;
223 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
224 p = (char *)data.data;
226 /* jump over pw_name and pw_passwd, to get to pw_uid */
232 buf[0] = _PW_KEYBYUID;
233 memmove(buf + 1, p, sizeof(int));
234 key.data = (u_char *)buf;
235 key.size = sizeof(int) + 1;
237 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
238 /* First field of data.data holds pw_pwname */
239 if (!strcmp(data.data, username))
242 methoduid = R_NOOVERWRITE;
244 methoduid = R_NOOVERWRITE;
247 methoduid = R_NOOVERWRITE;
249 if ((pw_db->close)(pw_db))
250 error("close pw_db");
254 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
257 clean = FILE_INSECURE;
260 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
265 method = R_NOOVERWRITE;
266 methoduid = R_NOOVERWRITE;
270 * Open file for old password file. Minor trickiness -- don't want to
271 * chance the file already existing, since someone (stupidly) might
272 * still be using this for permission checking. So, open it first and
273 * fdopen the resulting fd. The resulting file should be readable by
277 snprintf(buf, sizeof(buf), "%s.orig", pname);
279 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
281 if ((oldfp = fdopen(tfd, "w")) == NULL)
287 * The databases actually contain three copies of the original data.
288 * Each password file entry is converted into a rough approximation
289 * of a ``struct passwd'', with the strings placed inline. This
290 * object is then stored as the data for three separate keys. The
291 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
292 * character. The second key is the pw_uid field prepended by the
293 * _PW_KEYBYUID character. The third key is the line number in the
294 * original file prepended by the _PW_KEYBYNUM character. (The special
295 * characters are prepended to ensure that the keys do not collide.)
298 data.data = (u_char *)buf;
299 sdata.data = (u_char *)sbuf;
300 key.data = (u_char *)tbuf;
301 for (cnt = 1; scan(fp, &pwd); ++cnt) {
303 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-'))
307 #define COMPACT(e) t = e; while ((*p++ = *t++));
309 (!username || (strcmp(username, pwd.pw_name) == 0))) {
310 /* Create insecure data. */
312 COMPACT(pwd.pw_name);
314 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
316 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
318 memmove(p, &pwd.pw_change, sizeof(time_t));
320 COMPACT(pwd.pw_class);
321 COMPACT(pwd.pw_gecos);
323 COMPACT(pwd.pw_shell);
324 memmove(p, &pwd.pw_expire, sizeof(time_t));
326 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
327 p += sizeof pwd.pw_fields;
330 /* Create secure data. */
332 COMPACT(pwd.pw_name);
333 COMPACT(pwd.pw_passwd);
334 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
336 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
338 memmove(p, &pwd.pw_change, sizeof(time_t));
340 COMPACT(pwd.pw_class);
341 COMPACT(pwd.pw_gecos);
343 COMPACT(pwd.pw_shell);
344 memmove(p, &pwd.pw_expire, sizeof(time_t));
346 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
347 p += sizeof pwd.pw_fields;
348 sdata.size = p - sbuf;
350 /* Store insecure by name. */
351 tbuf[0] = _PW_KEYBYNAME;
352 len = strlen(pwd.pw_name);
353 memmove(tbuf + 1, pwd.pw_name, len);
355 if ((dp->put)(dp, &key, &data, method) == -1)
358 /* Store insecure by number. */
359 tbuf[0] = _PW_KEYBYNUM;
360 memmove(tbuf + 1, &cnt, sizeof(cnt));
361 key.size = sizeof(cnt) + 1;
362 if ((dp->put)(dp, &key, &data, method) == -1)
365 /* Store insecure by uid. */
366 tbuf[0] = _PW_KEYBYUID;
367 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
368 key.size = sizeof(pwd.pw_uid) + 1;
369 if ((dp->put)(dp, &key, &data, methoduid) == -1)
372 /* Store secure by name. */
373 tbuf[0] = _PW_KEYBYNAME;
374 len = strlen(pwd.pw_name);
375 memmove(tbuf + 1, pwd.pw_name, len);
377 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
380 /* Store secure by number. */
381 tbuf[0] = _PW_KEYBYNUM;
382 memmove(tbuf + 1, &cnt, sizeof(cnt));
383 key.size = sizeof(cnt) + 1;
384 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
387 /* Store secure by uid. */
388 tbuf[0] = _PW_KEYBYUID;
389 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
390 key.size = sizeof(pwd.pw_uid) + 1;
391 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
394 /* Store insecure and secure special plus and special minus */
395 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
396 tbuf[0] = _PW_KEYYPBYNUM;
397 memmove(tbuf + 1, &ypcnt, sizeof(cnt));
399 key.size = sizeof(cnt) + 1;
400 if ((dp->put)(dp, &key, &data, method) == -1)
402 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
406 /* Create original format password file entry */
407 if (is_comment && makeold){ /* copy comments */
408 if (fprintf(oldfp, "%s\n", line) < 0)
410 } else if (makeold) {
414 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
415 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
417 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
418 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
419 pwd.pw_fields & _PWF_GID ? gidstr : "",
420 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
424 /* If YP enabled, set flag. */
426 buf[0] = yp_enabled + 2;
428 tbuf[0] = _PW_KEYYPENABLED;
430 if ((dp->put)(dp, &key, &data, method) == -1)
432 if ((sdp->put)(sdp, &key, &data, method) == -1)
436 if ((dp->close)(dp) == -1)
438 if ((sdp->close)(sdp) == -1)
442 if (fclose(oldfp) == EOF)
446 /* Set master.passwd permissions, in case caller forgot. */
447 fchmod(fileno(fp), S_IRUSR|S_IWUSR);
449 /* Install as the real password files. */
450 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
451 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
453 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
454 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
457 snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
458 snprintf(buf, sizeof(buf), "%s.orig", pname);
462 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
463 * all use flock(2) on it to block other incarnations of themselves.
464 * The rename means that everything is unlocked, as the original file
465 * can no longer be accessed.
467 snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
471 * Close locked password file after rename()
473 if (fclose(fp) == EOF)
487 if (!fgets(line, sizeof(line), fp))
491 * ``... if I swallow anything evil, put your fingers down my
495 if (!(p = strchr(line, '\n'))) {
496 warnx("line too long");
503 * Ignore comments: ^[ \t]*#
505 for (p = line; *p != '\0'; p++)
506 if (*p != ' ' && *p != '\t')
508 if (*p == '#' || *p == '\0') {
514 if (!pw_scan(line, pw)) {
515 warnx("at line #%d", lcnt);
516 fmt: errno = EFTYPE; /* XXX */
528 static char buf[MAXBSIZE];
529 int from_fd, rcount, to_fd, wcount;
531 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
533 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
535 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
536 wcount = write(to_fd, buf, rcount);
537 if (rcount != wcount || wcount == -1) {
540 snprintf(buf, sizeof(buf), "%s to %s", from, to);
548 snprintf(buf, sizeof(buf), "%s to %s", from, to);
559 char buf[MAXPATHLEN];
561 if (rename(from, to)) {
563 snprintf(buf, sizeof(buf), "%s to %s", from, to);
582 char buf[MAXPATHLEN];
586 snprintf(buf, sizeof(buf), "%s.orig", pname);
590 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
594 snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
604 "usage: pwd_mkdb [-C] [-N] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n");