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 RG |
55 | |
56 | #define INSECURE 1 | |
57 | #define SECURE 2 | |
58 | #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) | |
59 | #define PERM_SECURE (S_IRUSR|S_IWUSR) | |
60 | ||
61 | char *progname = "pwd_mkdb"; | |
62 | ||
63 | static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean; | |
64 | static struct passwd pwd; /* password structure */ | |
65 | static char *pname; /* password file name */ | |
0e7b8dd7 | 66 | static char prefix[MAXPATHLEN]; |
15637ed4 RG |
67 | |
68 | main(argc, argv) | |
69 | int argc; | |
70 | char **argv; | |
71 | { | |
72 | extern int optind; | |
73 | register int len, makeold; | |
74 | register char *p, *t; | |
75 | FILE *fp, *oldfp; | |
76 | DB *dp, *edp; | |
77 | sigset_t set; | |
78 | DBT data, key; | |
79 | int ch, cnt, tfd; | |
80 | char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024]; | |
0e7b8dd7 | 81 | char buf2[MAXPATHLEN]; |
15637ed4 | 82 | |
0e7b8dd7 | 83 | strcpy(prefix, _PATH_PWD); |
15637ed4 | 84 | makeold = 0; |
0e7b8dd7 | 85 | while ((ch = getopt(argc, argv, "d:pv")) != EOF) |
15637ed4 | 86 | switch(ch) { |
0e7b8dd7 SW |
87 | case 'd': |
88 | strcpy(prefix, optarg); | |
89 | break; | |
15637ed4 RG |
90 | case 'p': /* create V7 "file.orig" */ |
91 | makeold = 1; | |
92 | break; | |
0e7b8dd7 | 93 | case 'v': /* backward compatible */ |
15637ed4 RG |
94 | break; |
95 | case '?': | |
96 | default: | |
97 | usage(); | |
98 | } | |
99 | argc -= optind; | |
100 | argv += optind; | |
101 | ||
102 | if (argc != 1) | |
103 | usage(); | |
104 | ||
105 | /* | |
106 | * This could be done to allow the user to interrupt. Probably | |
107 | * not worth the effort. | |
108 | */ | |
109 | sigemptyset(&set); | |
110 | sigaddset(&set, SIGTSTP); | |
111 | sigaddset(&set, SIGHUP); | |
112 | sigaddset(&set, SIGINT); | |
113 | sigaddset(&set, SIGQUIT); | |
114 | sigaddset(&set, SIGTERM); | |
115 | (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL); | |
116 | ||
117 | pname = *argv; | |
118 | /* Open the original password file */ | |
119 | if (!(fp = fopen(pname, "r"))) | |
120 | error(pname); | |
121 | ||
122 | /* Open the temporary insecure password database. */ | |
0e7b8dd7 | 123 | (void)sprintf(buf, "%s/%s.tmp", prefix, _MP_DB); |
28e48405 | 124 | dp = dbopen(buf, O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, NULL); |
15637ed4 RG |
125 | if (!dp) |
126 | error(buf); | |
127 | clean = FILE_INSECURE; | |
128 | ||
129 | /* Open the temporary encrypted password database. */ | |
0e7b8dd7 | 130 | (void)sprintf(buf, "%s/%s.tmp", prefix, _SMP_DB); |
28e48405 | 131 | edp = dbopen(buf, O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, NULL); |
15637ed4 RG |
132 | if (!edp) |
133 | error(buf); | |
134 | clean = FILE_SECURE; | |
135 | ||
136 | /* | |
137 | * Open file for old password file. Minor trickiness -- don't want to | |
138 | * chance the file already existing, since someone (stupidly) might | |
139 | * still be using this for permission checking. So, open it first and | |
140 | * fdopen the resulting fd. Don't really care who reads it. | |
141 | */ | |
142 | if (makeold) { | |
143 | (void)sprintf(buf, "%s.orig", pname); | |
144 | if ((tfd = open(buf, | |
145 | O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0) | |
146 | error(buf); | |
147 | if (!(oldfp = fdopen(tfd, "w"))) | |
148 | error(buf); | |
149 | clean = FILE_ORIG; | |
150 | } | |
151 | ||
152 | /* | |
153 | * The databases actually contain three copies of the original data. | |
154 | * Each password file entry is converted into a rough approximation | |
155 | * of a ``struct passwd'', with the strings placed inline. This | |
156 | * object is then stored as the data for three separate keys. The | |
157 | * first key * is the pw_name field prepended by the _PW_KEYBYNAME | |
158 | * character. The second key is the pw_uid field prepended by the | |
159 | * _PW_KEYBYUID character. The third key is the line number in the | |
160 | * original file prepended by the _PW_KEYBYNUM character. (The special | |
161 | * characters are prepended to ensure that the keys do not collide.) | |
162 | */ | |
163 | data.data = (u_char *)buf; | |
164 | key.data = (u_char *)tbuf; | |
165 | for (cnt = 1; scan(fp, &pwd); ++cnt) { | |
166 | #define COMPACT(e) t = e; while (*p++ = *t++); | |
167 | /* Create insecure data. */ | |
168 | p = buf; | |
169 | COMPACT(pwd.pw_name); | |
170 | COMPACT("*"); | |
171 | bcopy((char *)&pwd.pw_uid, p, sizeof(int)); | |
172 | p += sizeof(int); | |
173 | bcopy((char *)&pwd.pw_gid, p, sizeof(int)); | |
174 | p += sizeof(int); | |
175 | bcopy((char *)&pwd.pw_change, p, sizeof(time_t)); | |
176 | p += sizeof(time_t); | |
177 | COMPACT(pwd.pw_class); | |
178 | COMPACT(pwd.pw_gecos); | |
179 | COMPACT(pwd.pw_dir); | |
180 | COMPACT(pwd.pw_shell); | |
181 | bcopy((char *)&pwd.pw_expire, p, sizeof(time_t)); | |
182 | p += sizeof(time_t); | |
183 | data.size = p - buf; | |
184 | ||
185 | /* Store insecure by name. */ | |
186 | tbuf[0] = _PW_KEYBYNAME; | |
187 | len = strlen(pwd.pw_name); | |
188 | bcopy(pwd.pw_name, tbuf + 1, len); | |
189 | key.size = len + 1; | |
190 | if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1) | |
191 | error("put"); | |
192 | ||
193 | /* Store insecure by number. */ | |
194 | tbuf[0] = _PW_KEYBYNUM; | |
195 | bcopy((char *)&cnt, tbuf + 1, sizeof(cnt)); | |
196 | key.size = sizeof(cnt) + 1; | |
197 | if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1) | |
198 | error("put"); | |
199 | ||
200 | /* Store insecure by uid. */ | |
201 | tbuf[0] = _PW_KEYBYUID; | |
202 | bcopy((char *)&pwd.pw_uid, tbuf + 1, sizeof(pwd.pw_uid)); | |
203 | key.size = sizeof(pwd.pw_uid) + 1; | |
204 | if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1) | |
205 | error("put"); | |
206 | ||
207 | /* Create secure data. */ | |
208 | p = buf; | |
209 | COMPACT(pwd.pw_name); | |
210 | COMPACT(pwd.pw_passwd); | |
211 | bcopy((char *)&pwd.pw_uid, p, sizeof(int)); | |
212 | p += sizeof(int); | |
213 | bcopy((char *)&pwd.pw_gid, p, sizeof(int)); | |
214 | p += sizeof(int); | |
215 | bcopy((char *)&pwd.pw_change, p, sizeof(time_t)); | |
216 | p += sizeof(time_t); | |
217 | COMPACT(pwd.pw_class); | |
218 | COMPACT(pwd.pw_gecos); | |
219 | COMPACT(pwd.pw_dir); | |
220 | COMPACT(pwd.pw_shell); | |
221 | bcopy((char *)&pwd.pw_expire, p, sizeof(time_t)); | |
222 | p += sizeof(time_t); | |
223 | data.size = p - buf; | |
224 | ||
225 | /* Store secure by name. */ | |
226 | tbuf[0] = _PW_KEYBYNAME; | |
227 | len = strlen(pwd.pw_name); | |
228 | bcopy(pwd.pw_name, tbuf + 1, len); | |
229 | key.size = len + 1; | |
230 | if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1) | |
231 | error("put"); | |
232 | ||
233 | /* Store secure by number. */ | |
234 | tbuf[0] = _PW_KEYBYNUM; | |
235 | bcopy((char *)&cnt, tbuf + 1, sizeof(cnt)); | |
236 | key.size = sizeof(cnt) + 1; | |
237 | if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1) | |
238 | error("put"); | |
239 | ||
240 | /* Store secure by uid. */ | |
241 | tbuf[0] = _PW_KEYBYUID; | |
242 | bcopy((char *)&pwd.pw_uid, tbuf + 1, sizeof(pwd.pw_uid)); | |
243 | key.size = sizeof(pwd.pw_uid) + 1; | |
244 | if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1) | |
245 | error("put"); | |
246 | ||
247 | /* Create original format password file entry */ | |
248 | if (makeold) | |
249 | (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n", | |
250 | pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos, | |
251 | pwd.pw_dir, pwd.pw_shell); | |
252 | } | |
253 | (void)(dp->close)(dp); | |
254 | (void)(edp->close)(edp); | |
255 | if (makeold) { | |
256 | (void)fsync(oldfp); | |
257 | (void)fclose(oldfp); | |
258 | } | |
259 | ||
260 | /* Set master.passwd permissions, in case caller forgot. */ | |
261 | (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR); | |
262 | (void)fclose(fp); | |
263 | ||
264 | /* Install as the real password files. */ | |
0e7b8dd7 SW |
265 | (void)sprintf(buf, "%s/%s.tmp", prefix, _MP_DB); |
266 | (void)sprintf(buf2, "%s/%s", prefix, _MP_DB); | |
267 | mv(buf, buf2); | |
268 | (void)sprintf(buf, "%s/%s.tmp", prefix, _SMP_DB); | |
269 | (void)sprintf(buf2, "%s/%s", prefix, _SMP_DB); | |
270 | mv(buf, buf2); | |
15637ed4 | 271 | if (makeold) { |
0e7b8dd7 | 272 | (void)sprintf(buf2, "%s/%s", prefix, _PASSWD); |
15637ed4 | 273 | (void)sprintf(buf, "%s.orig", pname); |
0e7b8dd7 | 274 | mv(buf, buf2); |
15637ed4 RG |
275 | } |
276 | /* | |
277 | * Move the master password LAST -- chpass(1), passwd(1) and vipw(8) | |
278 | * all use flock(2) on it to block other incarnations of themselves. | |
279 | * The rename means that everything is unlocked, as the original file | |
280 | * can no longer be accessed. | |
281 | */ | |
0e7b8dd7 SW |
282 | (void)sprintf(buf, "%s/%s", prefix, _MASTERPASSWD); |
283 | mv(pname, buf); | |
15637ed4 RG |
284 | exit(0); |
285 | } | |
286 | ||
287 | scan(fp, pw) | |
288 | FILE *fp; | |
289 | struct passwd *pw; | |
290 | { | |
291 | static int lcnt; | |
292 | static char line[LINE_MAX]; | |
293 | char *p; | |
294 | ||
295 | if (!fgets(line, sizeof(line), fp)) | |
296 | return(0); | |
297 | ++lcnt; | |
298 | /* | |
299 | * ``... if I swallow anything evil, put your fingers down my | |
300 | * throat...'' | |
301 | * -- The Who | |
302 | */ | |
303 | if (!(p = index(line, '\n'))) { | |
304 | (void)fprintf(stderr, "pwd_mkdb: line too long\n"); | |
305 | goto fmt; | |
306 | ||
307 | } | |
308 | *p = '\0'; | |
309 | if (!pw_scan(line, pw)) { | |
310 | (void)fprintf(stderr, "pwd_mkdb: at line #%d.\n", lcnt); | |
311 | fmt: errno = EFTYPE; | |
312 | error(pname); | |
313 | exit(1); | |
314 | } | |
315 | } | |
316 | ||
317 | mv(from, to) | |
318 | char *from, *to; | |
319 | { | |
320 | int sverrno; | |
321 | char buf[MAXPATHLEN]; | |
322 | ||
323 | if (rename(from, to)) { | |
324 | sverrno = errno; | |
325 | (void)sprintf(buf, "%s to %s", from, to); | |
326 | errno = sverrno; | |
327 | error(buf); | |
328 | } | |
329 | } | |
330 | ||
331 | error(name) | |
332 | char *name; | |
333 | { | |
334 | (void)fprintf(stderr, "pwd_mkdb: %s: %s\n", name, strerror(errno)); | |
335 | cleanup(); | |
336 | exit(1); | |
337 | } | |
338 | ||
339 | cleanup() | |
340 | { | |
341 | char buf[MAXPATHLEN]; | |
342 | ||
343 | switch(clean) { | |
344 | case FILE_ORIG: | |
345 | (void)sprintf(buf, "%s.orig", pname); | |
346 | (void)unlink(buf); | |
347 | /* FALLTHROUGH */ | |
348 | case FILE_SECURE: | |
0e7b8dd7 | 349 | (void)sprintf(buf, "%s/%s.tmp", prefix, _SMP_DB); |
15637ed4 RG |
350 | (void)unlink(buf); |
351 | /* FALLTHROUGH */ | |
352 | case FILE_INSECURE: | |
0e7b8dd7 | 353 | (void)sprintf(buf, "%s/%s.tmp", prefix, _MP_DB); |
15637ed4 RG |
354 | (void)unlink(buf); |
355 | } | |
356 | } | |
357 | ||
358 | usage() | |
359 | { | |
0e7b8dd7 | 360 | (void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d <dest dir>] file\n"); |
15637ed4 RG |
361 | exit(1); |
362 | } |