pwd_mkdb.c revision 1.15 1 /*
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Portions Copyright(C) 1994, Jason Downs. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
44 #else
45 __RCSID("$NetBSD: pwd_mkdb.c,v 1.15 1998/06/08 03:23:07 lukem Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/stat.h>
51
52 #include <db.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <limits.h>
57 #include <pwd.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <util.h>
64
65 #define INSECURE 1
66 #define SECURE 2
67 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
68 #define PERM_SECURE (S_IRUSR|S_IWUSR)
69
70 /* pull this out of the C library. */
71 extern const char __yp_token[];
72
73 HASHINFO openinfo = {
74 4096, /* bsize */
75 32, /* ffactor */
76 256, /* nelem */
77 2048 * 1024, /* cachesize */
78 NULL, /* hash() */
79 0 /* lorder */
80 };
81
82 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
83 static struct passwd pwd; /* password structure */
84 static char *pname; /* password file name */
85 static char prefix[MAXPATHLEN];
86 static char oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
87 static char pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
88 static char pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
89
90 void cleanup __P((void));
91 void error __P((char *));
92 void wr_error __P((char *));
93 int main __P((int, char **));
94 void mv __P((char *, char *));
95 void rm __P((char *));
96 int scan __P((FILE *, struct passwd *, int *));
97 void usage __P((void));
98
99 int
100 main(argc, argv)
101 int argc;
102 char *argv[];
103 {
104 DB *dp, *edp;
105 DBT data, key;
106 FILE *fp, *oldfp;
107 sigset_t set;
108 int ch, cnt, len, makeold, tfd, flags;
109 char *p, *t;
110 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
111 int hasyp = 0;
112 DBT ypdata, ypkey;
113
114 oldfp = NULL;
115 strcpy(prefix, "/");
116 makeold = 0;
117 while ((ch = getopt(argc, argv, "d:pv")) != -1)
118 switch(ch) {
119 case 'd':
120 strncpy(prefix, optarg, sizeof(prefix));
121 prefix[sizeof(prefix)-1] = '\0';
122 break;
123 case 'p': /* create V7 "file.orig" */
124 makeold = 1;
125 break;
126 case 'v': /* backward compatible */
127 break;
128 case '?':
129 default:
130 usage();
131 }
132 argc -= optind;
133 argv += optind;
134
135 if (argc != 1)
136 usage();
137
138 /*
139 * This could be changed to allow the user to interrupt.
140 * Probably not worth the effort.
141 */
142 sigemptyset(&set);
143 sigaddset(&set, SIGTSTP);
144 sigaddset(&set, SIGHUP);
145 sigaddset(&set, SIGINT);
146 sigaddset(&set, SIGQUIT);
147 sigaddset(&set, SIGTERM);
148 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
149
150 /* We don't care what the user wants. */
151 (void)umask(0);
152
153 pname = *argv;
154 /* Open the original password file */
155 if (!(fp = fopen(pname, "r")))
156 error(pname);
157
158 /* Open the temporary insecure password database. */
159 (void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp", prefix,
160 _PATH_MP_DB);
161 dp = dbopen(pwd_db_tmp,
162 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
163 if (dp == NULL)
164 error(pwd_db_tmp);
165 clean = FILE_INSECURE;
166
167 /*
168 * Open file for old password file. Minor trickiness -- don't want to
169 * chance the file already existing, since someone (stupidly) might
170 * still be using this for permission checking. So, open it first and
171 * fdopen the resulting fd. The resulting file should be readable by
172 * everyone.
173 */
174 if (makeold) {
175 (void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
176 pname);
177 if ((tfd = open(oldpwdfile,
178 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
179 error(oldpwdfile);
180 if ((oldfp = fdopen(tfd, "w")) == NULL)
181 error(oldpwdfile);
182 clean = FILE_ORIG;
183 }
184
185 /*
186 * The databases actually contain three copies of the original data.
187 * Each password file entry is converted into a rough approximation
188 * of a ``struct passwd'', with the strings placed inline. This
189 * object is then stored as the data for three separate keys. The
190 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
191 * character. The second key is the pw_uid field prepended by the
192 * _PW_KEYBYUID character. The third key is the line number in the
193 * original file prepended by the _PW_KEYBYNUM character. (The special
194 * characters are prepended to ensure that the keys do not collide.)
195 *
196 * If we see something go by that looks like YP, we save a special
197 * pointer record, which if YP is enabled in the C lib, will speed
198 * things up.
199 */
200 data.data = (u_char *)buf;
201 key.data = (u_char *)tbuf;
202 for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
203 #define COMPACT(e) t = e; while ((*p++ = *t++));
204
205 /* look like YP? */
206 if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
207 hasyp++;
208
209 /*
210 * Warn about potentially unsafe uid/gid overrides.
211 */
212 if (pwd.pw_name[0] == '+') {
213 if ((flags & _PASSWORD_NOUID) == 0 && pwd.pw_uid == 0)
214 warnx(
215 "line %d: superuser override in YP inclusion",
216 cnt);
217 if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
218 warnx("line %d: wheel override in YP inclusion",
219 cnt);
220 }
221
222 /* Create insecure data. */
223 p = buf;
224 COMPACT(pwd.pw_name);
225 COMPACT("*");
226 memmove(p, &pwd.pw_uid, sizeof(int));
227 p += sizeof(int);
228 memmove(p, &pwd.pw_gid, sizeof(int));
229 p += sizeof(int);
230 memmove(p, &pwd.pw_change, sizeof(time_t));
231 p += sizeof(time_t);
232 COMPACT(pwd.pw_class);
233 COMPACT(pwd.pw_gecos);
234 COMPACT(pwd.pw_dir);
235 COMPACT(pwd.pw_shell);
236 memmove(p, &pwd.pw_expire, sizeof(time_t));
237 p += sizeof(time_t);
238 memmove(p, &flags, sizeof(int));
239 p += sizeof(int);
240 data.size = p - buf;
241
242 /* Store insecure by name. */
243 tbuf[0] = _PW_KEYBYNAME;
244 len = strlen(pwd.pw_name);
245 memmove(tbuf + 1, pwd.pw_name, len);
246 key.size = len + 1;
247 if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
248 wr_error(pwd_db_tmp);
249
250 /* Store insecure by number. */
251 tbuf[0] = _PW_KEYBYNUM;
252 memmove(tbuf + 1, &cnt, sizeof(cnt));
253 key.size = sizeof(cnt) + 1;
254 if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
255 wr_error(pwd_db_tmp);
256
257 /* Store insecure by uid. */
258 tbuf[0] = _PW_KEYBYUID;
259 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
260 key.size = sizeof(pwd.pw_uid) + 1;
261 if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
262 wr_error(pwd_db_tmp);
263
264 /* Create original format password file entry */
265 if (makeold) {
266 (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
267 pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
268 pwd.pw_dir, pwd.pw_shell);
269 if (ferror(oldfp)) {
270 wr_error(oldpwdfile);
271 }
272 }
273 }
274
275 /* Store YP token, if needed. */
276 if(hasyp) {
277 ypkey.data = (u_char *)__yp_token;
278 ypkey.size = strlen(__yp_token);
279 ypdata.data = (u_char *)NULL;
280 ypdata.size = 0;
281
282 if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
283 wr_error(pwd_db_tmp);
284 }
285
286 if ((dp->close)(dp) < 0) {
287 wr_error(pwd_db_tmp);
288 }
289 if (makeold) {
290 if (fflush(oldfp) == EOF) {
291 wr_error(oldpwdfile);
292 }
293 if (fclose(oldfp) == EOF) {
294 wr_error(oldpwdfile);
295 }
296 }
297
298 /* Open the temporary encrypted password database. */
299 (void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
300 _PATH_SMP_DB);
301 edp = dbopen(pwd_Sdb_tmp,
302 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
303 if (!edp)
304 error(pwd_Sdb_tmp);
305 clean = FILE_SECURE;
306
307 rewind(fp);
308 for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
309
310 /* Create secure data. */
311 p = buf;
312 COMPACT(pwd.pw_name);
313 COMPACT(pwd.pw_passwd);
314 memmove(p, &pwd.pw_uid, sizeof(int));
315 p += sizeof(int);
316 memmove(p, &pwd.pw_gid, sizeof(int));
317 p += sizeof(int);
318 memmove(p, &pwd.pw_change, sizeof(time_t));
319 p += sizeof(time_t);
320 COMPACT(pwd.pw_class);
321 COMPACT(pwd.pw_gecos);
322 COMPACT(pwd.pw_dir);
323 COMPACT(pwd.pw_shell);
324 memmove(p, &pwd.pw_expire, sizeof(time_t));
325 p += sizeof(time_t);
326 memmove(p, &flags, sizeof(int));
327 p += sizeof(int);
328 data.size = p - buf;
329
330 /* Store secure by name. */
331 tbuf[0] = _PW_KEYBYNAME;
332 len = strlen(pwd.pw_name);
333 memmove(tbuf + 1, pwd.pw_name, len);
334 key.size = len + 1;
335 if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
336 wr_error(pwd_Sdb_tmp);
337
338 /* Store secure by number. */
339 tbuf[0] = _PW_KEYBYNUM;
340 memmove(tbuf + 1, &cnt, sizeof(cnt));
341 key.size = sizeof(cnt) + 1;
342 if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
343 wr_error(pwd_Sdb_tmp);
344
345 /* Store secure by uid. */
346 tbuf[0] = _PW_KEYBYUID;
347 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
348 key.size = sizeof(pwd.pw_uid) + 1;
349 if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
350 wr_error(pwd_Sdb_tmp);
351 }
352
353 /* Store YP token, if needed. */
354 if(hasyp) {
355 ypkey.data = (u_char *)__yp_token;
356 ypkey.size = strlen(__yp_token);
357 ypdata.data = (u_char *)NULL;
358 ypdata.size = 0;
359
360 if((edp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
361 wr_error(pwd_Sdb_tmp);
362 }
363
364 if ((edp->close)(edp) < 0) {
365 wr_error(_PATH_SMP_DB);
366 }
367
368 /* Set master.passwd permissions, in case caller forgot. */
369 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
370 if (fclose(fp) == EOF) {
371 wr_error(pname);
372 }
373
374 /* Install as the real password files. */
375 {
376 char destination[MAXPATHLEN];
377
378 (void)snprintf(destination, sizeof(destination),
379 "%s%s", prefix, _PATH_MP_DB);
380 mv(pwd_db_tmp, destination);
381
382 (void)snprintf(destination, sizeof(destination),
383 "%s%s", prefix, _PATH_SMP_DB);
384 mv(pwd_Sdb_tmp, destination);
385
386 if (makeold) {
387 (void)snprintf(destination, sizeof(destination),
388 "%s%s", prefix, _PATH_PASSWD);
389 mv(oldpwdfile, destination);
390 }
391
392 /*
393 * Move the master password LAST -- chpass(1),
394 * passwd(1) and vipw(8) all use flock(2) on it to
395 * block other incarnations of themselves. The rename
396 * means that everything is unlocked, as the original
397 * file can no longer be accessed.
398 */
399 (void)snprintf(destination, sizeof(destination),
400 "%s%s", prefix, _PATH_MASTERPASSWD);
401 mv(pname, destination);
402 }
403 exit(0);
404 }
405
406 int
407 scan(fp, pw, flags)
408 FILE *fp;
409 struct passwd *pw;
410 int *flags;
411 {
412 static int lcnt;
413 static char line[LINE_MAX];
414 char *p;
415
416 if (!fgets(line, sizeof(line), fp))
417 return (0);
418 ++lcnt;
419 /*
420 * ``... if I swallow anything evil, put your fingers down my
421 * throat...''
422 * -- The Who
423 */
424 if (!(p = strchr(line, '\n'))) {
425 warnx("line too long");
426 goto fmt;
427
428 }
429 *p = '\0';
430 if (strcmp(line, "+") == 0)
431 strcpy(line, "+:::::::::"); /* pw_scan() can't handle "+" */
432 *flags = 0;
433 if (!pw_scan(line, pw, flags)) {
434 warnx("at line #%d", lcnt);
435 fmt: errno = EFTYPE; /* XXX */
436 error(pname);
437 }
438
439 return (1);
440 }
441
442 void
443 mv(from, to)
444 char *from, *to;
445 {
446 char buf[MAXPATHLEN];
447
448 if (rename(from, to)) {
449 int sverrno = errno;
450 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
451 errno = sverrno;
452 error(buf);
453 }
454 }
455
456 void
457 wr_error(name)
458 char *name;
459 {
460 char errbuf[BUFSIZ];
461 int sverrno = errno;
462
463 (void)snprintf(errbuf, sizeof(errbuf),
464 "attempt to write %s failed", name);
465
466 errno = sverrno;
467 error(errbuf);
468 }
469
470 void
471 error(name)
472 char *name;
473 {
474
475 warn(name);
476 cleanup();
477 #ifdef think_about_this_a_while_longer
478 fputs("NOTE: possible inconsistencies between text files and databases\n", stderr);
479 fputs("re-run pwd_mkdb when you have fixed the problem.\n", stderr);
480 #endif
481 exit(1);
482 }
483
484 void
485 rm(victim)
486 char *victim;
487 {
488 if (unlink(victim) < 0) {
489 warn("unlink(%s)", victim);
490 }
491 }
492
493 void
494 cleanup()
495 {
496 switch(clean) {
497 case FILE_ORIG:
498 rm(oldpwdfile);
499 /* FALLTHROUGH */
500 case FILE_SECURE:
501 rm(pwd_Sdb_tmp);
502 /* FALLTHROUGH */
503 case FILE_INSECURE:
504 rm(pwd_db_tmp);
505 }
506 }
507
508 void
509 usage()
510 {
511
512 (void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
513 exit(1);
514 }
515