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