pwd_mkdb.c revision 1.8 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 #ifndef lint
36 static char copyright[] =
37 "@(#) 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 /*static char sccsid[] = "from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";*/
43 static char *rcsid = "$Id: pwd_mkdb.c,v 1.8 1996/11/22 05:37:30 lukem Exp $";
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48
49 #include <db.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <limits.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <util.h>
61
62 #define INSECURE 1
63 #define SECURE 2
64 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
65 #define PERM_SECURE (S_IRUSR|S_IWUSR)
66
67 /* pull this out of the C library. */
68 extern const char __yp_token[];
69
70 HASHINFO openinfo = {
71 4096, /* bsize */
72 32, /* ffactor */
73 256, /* nelem */
74 2048 * 1024, /* cachesize */
75 NULL, /* hash() */
76 0 /* lorder */
77 };
78
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];
83
84 void cleanup __P((void));
85 void error __P((char *));
86 void mv __P((char *, char *));
87 int scan __P((FILE *, struct passwd *, int *));
88 void usage __P((void));
89
90 int
91 main(argc, argv)
92 int argc;
93 char *argv[];
94 {
95 DB *dp, *edp;
96 DBT data, key;
97 FILE *fp, *oldfp;
98 sigset_t set;
99 int ch, cnt, len, makeold, tfd, flags;
100 char *p, *t;
101 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], buf2[MAXPATHLEN], tbuf[1024];
102 int hasyp = 0;
103 DBT ypdata, ypkey;
104
105 strcpy(prefix, "/");
106 makeold = 0;
107 while ((ch = getopt(argc, argv, "d:pv")) != EOF)
108 switch(ch) {
109 case 'd':
110 strncpy(prefix, optarg, sizeof(prefix));
111 prefix[sizeof(prefix)-1] = '\0';
112 break;
113 case 'p': /* create V7 "file.orig" */
114 makeold = 1;
115 break;
116 case 'v': /* backward compatible */
117 break;
118 case '?':
119 default:
120 usage();
121 }
122 argc -= optind;
123 argv += optind;
124
125 if (argc != 1)
126 usage();
127
128 /*
129 * This could be changed to allow the user to interrupt.
130 * Probably not worth the effort.
131 */
132 sigemptyset(&set);
133 sigaddset(&set, SIGTSTP);
134 sigaddset(&set, SIGHUP);
135 sigaddset(&set, SIGINT);
136 sigaddset(&set, SIGQUIT);
137 sigaddset(&set, SIGTERM);
138 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
139
140 /* We don't care what the user wants. */
141 (void)umask(0);
142
143 pname = *argv;
144 /* Open the original password file */
145 if (!(fp = fopen(pname, "r")))
146 error(pname);
147
148 /* Open the temporary insecure password database. */
149 (void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_MP_DB);
150 dp = dbopen(buf,
151 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
152 if (dp == NULL)
153 error(buf);
154 clean = FILE_INSECURE;
155
156 /*
157 * Open file for old password file. Minor trickiness -- don't want to
158 * chance the file already existing, since someone (stupidly) might
159 * still be using this for permission checking. So, open it first and
160 * fdopen the resulting fd. The resulting file should be readable by
161 * everyone.
162 */
163 if (makeold) {
164 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
165 if ((tfd = open(buf,
166 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
167 error(buf);
168 if ((oldfp = fdopen(tfd, "w")) == NULL)
169 error(buf);
170 clean = FILE_ORIG;
171 }
172
173 /*
174 * The databases actually contain three copies of the original data.
175 * Each password file entry is converted into a rough approximation
176 * of a ``struct passwd'', with the strings placed inline. This
177 * object is then stored as the data for three separate keys. The
178 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
179 * character. The second key is the pw_uid field prepended by the
180 * _PW_KEYBYUID character. The third key is the line number in the
181 * original file prepended by the _PW_KEYBYNUM character. (The special
182 * characters are prepended to ensure that the keys do not collide.)
183 *
184 * If we see something go by that looks like YP, we save a special
185 * pointer record, which if YP is enabled in the C lib, will speed
186 * things up.
187 */
188 data.data = (u_char *)buf;
189 key.data = (u_char *)tbuf;
190 for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
191 #define COMPACT(e) t = e; while ((*p++ = *t++));
192
193 /* look like YP? */
194 if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
195 hasyp++;
196
197 /* Create insecure data. */
198 p = buf;
199 COMPACT(pwd.pw_name);
200 COMPACT("*");
201 memmove(p, &pwd.pw_uid, sizeof(int));
202 p += sizeof(int);
203 memmove(p, &pwd.pw_gid, sizeof(int));
204 p += sizeof(int);
205 memmove(p, &pwd.pw_change, sizeof(time_t));
206 p += sizeof(time_t);
207 COMPACT(pwd.pw_class);
208 COMPACT(pwd.pw_gecos);
209 COMPACT(pwd.pw_dir);
210 COMPACT(pwd.pw_shell);
211 memmove(p, &pwd.pw_expire, sizeof(time_t));
212 p += sizeof(time_t);
213 memmove(p, &flags, sizeof(int));
214 p += sizeof(int);
215 data.size = p - buf;
216
217 /* Store insecure by name. */
218 tbuf[0] = _PW_KEYBYNAME;
219 len = strlen(pwd.pw_name);
220 memmove(tbuf + 1, pwd.pw_name, len);
221 key.size = len + 1;
222 if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
223 error("put");
224
225 /* Store insecure by number. */
226 tbuf[0] = _PW_KEYBYNUM;
227 memmove(tbuf + 1, &cnt, sizeof(cnt));
228 key.size = sizeof(cnt) + 1;
229 if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
230 error("put");
231
232 /* Store insecure by uid. */
233 tbuf[0] = _PW_KEYBYUID;
234 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
235 key.size = sizeof(pwd.pw_uid) + 1;
236 if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
237 error("put");
238
239 /* Create original format password file entry */
240 if (makeold)
241 (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
242 pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
243 pwd.pw_dir, pwd.pw_shell);
244 }
245
246 /* Store YP token, if needed. */
247 if(hasyp) {
248 ypkey.data = (u_char *)__yp_token;
249 ypkey.size = strlen(__yp_token);
250 ypdata.data = (u_char *)NULL;
251 ypdata.size = 0;
252
253 if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
254 error("put");
255 }
256
257 (void)(dp->close)(dp);
258 if (makeold) {
259 (void)fflush(oldfp);
260 (void)fclose(oldfp);
261 }
262
263 /* Open the temporary encrypted password database. */
264 (void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_SMP_DB);
265 edp = dbopen(buf,
266 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
267 if (!edp)
268 error(buf);
269 clean = FILE_SECURE;
270
271 rewind(fp);
272 for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
273
274 /* Create secure data. */
275 p = buf;
276 COMPACT(pwd.pw_name);
277 COMPACT(pwd.pw_passwd);
278 memmove(p, &pwd.pw_uid, sizeof(int));
279 p += sizeof(int);
280 memmove(p, &pwd.pw_gid, sizeof(int));
281 p += sizeof(int);
282 memmove(p, &pwd.pw_change, sizeof(time_t));
283 p += sizeof(time_t);
284 COMPACT(pwd.pw_class);
285 COMPACT(pwd.pw_gecos);
286 COMPACT(pwd.pw_dir);
287 COMPACT(pwd.pw_shell);
288 memmove(p, &pwd.pw_expire, sizeof(time_t));
289 p += sizeof(time_t);
290 memmove(p, &flags, sizeof(int));
291 p += sizeof(int);
292 data.size = p - buf;
293
294 /* Store secure by name. */
295 tbuf[0] = _PW_KEYBYNAME;
296 len = strlen(pwd.pw_name);
297 memmove(tbuf + 1, pwd.pw_name, len);
298 key.size = len + 1;
299 if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
300 error("put");
301
302 /* Store secure by number. */
303 tbuf[0] = _PW_KEYBYNUM;
304 memmove(tbuf + 1, &cnt, sizeof(cnt));
305 key.size = sizeof(cnt) + 1;
306 if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
307 error("put");
308
309 /* Store secure by uid. */
310 tbuf[0] = _PW_KEYBYUID;
311 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
312 key.size = sizeof(pwd.pw_uid) + 1;
313 if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
314 error("put");
315 }
316
317 /* Store YP token, if needed. */
318 if(hasyp) {
319 ypkey.data = (u_char *)__yp_token;
320 ypkey.size = strlen(__yp_token);
321 ypdata.data = (u_char *)NULL;
322 ypdata.size = 0;
323
324 if((dp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
325 error("put");
326 }
327
328 (void)(edp->close)(edp);
329
330 /* Set master.passwd permissions, in case caller forgot. */
331 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
332 (void)fclose(fp);
333
334 /* Install as the real password files. */
335 (void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_MP_DB);
336 (void)snprintf(buf2, sizeof(buf2), "%s%s", prefix, _PATH_MP_DB);
337 mv(buf, buf2);
338 (void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix, _PATH_SMP_DB);
339 (void)snprintf(buf2, sizeof(buf2), "%s%s", prefix, _PATH_SMP_DB);
340 mv(buf, buf2);
341 if (makeold) {
342 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
343 (void)snprintf(buf2, sizeof(buf2), "%s%s", prefix,
344 _PATH_PASSWD);
345 mv(buf, buf2);
346 }
347 /*
348 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
349 * all use flock(2) on it to block other incarnations of themselves.
350 * The rename means that everything is unlocked, as the original file
351 * can no longer be accessed.
352 */
353 (void)snprintf(buf, sizeof(buf), "%s%s", prefix, _PATH_MASTERPASSWD);
354 mv(pname, buf);
355 exit(0);
356 }
357
358 int
359 scan(fp, pw, flags)
360 FILE *fp;
361 struct passwd *pw;
362 int *flags;
363 {
364 static int lcnt;
365 static char line[LINE_MAX];
366 char *p;
367
368 if (!fgets(line, sizeof(line), fp))
369 return (0);
370 ++lcnt;
371 /*
372 * ``... if I swallow anything evil, put your fingers down my
373 * throat...''
374 * -- The Who
375 */
376 if (!(p = strchr(line, '\n'))) {
377 warnx("line too long");
378 goto fmt;
379
380 }
381 *p = '\0';
382 if (!pw_scan(line, pw, flags)) {
383 warnx("at line #%d", lcnt);
384 fmt: errno = EFTYPE; /* XXX */
385 error(pname);
386 }
387
388 return (1);
389 }
390
391 void
392 mv(from, to)
393 char *from, *to;
394 {
395 char buf[MAXPATHLEN];
396
397 if (rename(from, to)) {
398 int sverrno = errno;
399 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
400 errno = sverrno;
401 error(buf);
402 }
403 }
404
405 void
406 error(name)
407 char *name;
408 {
409
410 warn(name);
411 cleanup();
412 exit(1);
413 }
414
415 void
416 cleanup()
417 {
418 char buf[MAXPATHLEN];
419
420 switch(clean) {
421 case FILE_ORIG:
422 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
423 (void)unlink(buf);
424 /* FALLTHROUGH */
425 case FILE_SECURE:
426 (void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix,
427 _PATH_SMP_DB);
428 (void)unlink(buf);
429 /* FALLTHROUGH */
430 case FILE_INSECURE:
431 (void)snprintf(buf, sizeof(buf), "%s%s.tmp", prefix,
432 _PATH_MP_DB);
433 (void)unlink(buf);
434 }
435 }
436
437 void
438 usage()
439 {
440
441 (void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
442 exit(1);
443 }
444