pwd_mkdb.c revision 1.5 1 1.1 cgd /*-
2 1.5 mycroft * Copyright (c) 1991, 1993, 1994
3 1.5 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.5 mycroft static char copyright[] =
36 1.5 mycroft "@(#) Copyright (c) 1991, 1993, 1994\n\
37 1.5 mycroft The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.5 mycroft /*static char sccsid[] = "from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";*/
42 1.5 mycroft static char *rcsid = "$Id: pwd_mkdb.c,v 1.5 1994/08/28 23:32:54 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/stat.h>
47 1.5 mycroft
48 1.1 cgd #include <db.h>
49 1.5 mycroft #include <err.h>
50 1.1 cgd #include <errno.h>
51 1.5 mycroft #include <fcntl.h>
52 1.1 cgd #include <limits.h>
53 1.5 mycroft #include <pwd.h>
54 1.5 mycroft #include <signal.h>
55 1.1 cgd #include <stdio.h>
56 1.5 mycroft #include <stdlib.h>
57 1.1 cgd #include <string.h>
58 1.5 mycroft #include <unistd.h>
59 1.5 mycroft
60 1.5 mycroft #include "pw_scan.h"
61 1.1 cgd
62 1.1 cgd #define INSECURE 1
63 1.1 cgd #define SECURE 2
64 1.1 cgd #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
65 1.1 cgd #define PERM_SECURE (S_IRUSR|S_IWUSR)
66 1.1 cgd
67 1.5 mycroft HASHINFO openinfo = {
68 1.5 mycroft 4096, /* bsize */
69 1.5 mycroft 32, /* ffactor */
70 1.5 mycroft 256, /* nelem */
71 1.5 mycroft 2048 * 1024, /* cachesize */
72 1.5 mycroft NULL, /* hash() */
73 1.5 mycroft 0 /* lorder */
74 1.5 mycroft };
75 1.1 cgd
76 1.1 cgd static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
77 1.1 cgd static struct passwd pwd; /* password structure */
78 1.1 cgd static char *pname; /* password file name */
79 1.1 cgd
80 1.5 mycroft void cleanup __P((void));
81 1.5 mycroft void error __P((char *));
82 1.5 mycroft void mv __P((char *, char *));
83 1.5 mycroft int scan __P((FILE *, struct passwd *));
84 1.5 mycroft void usage __P((void));
85 1.5 mycroft
86 1.5 mycroft int
87 1.1 cgd main(argc, argv)
88 1.1 cgd int argc;
89 1.5 mycroft char *argv[];
90 1.1 cgd {
91 1.5 mycroft DB *dp, *edp;
92 1.5 mycroft DBT data, key;
93 1.1 cgd FILE *fp, *oldfp;
94 1.1 cgd sigset_t set;
95 1.5 mycroft int ch, cnt, len, makeold, tfd;
96 1.5 mycroft char *p, *t;
97 1.1 cgd char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
98 1.1 cgd
99 1.1 cgd makeold = 0;
100 1.1 cgd while ((ch = getopt(argc, argv, "pv")) != EOF)
101 1.1 cgd switch(ch) {
102 1.1 cgd case 'p': /* create V7 "file.orig" */
103 1.1 cgd makeold = 1;
104 1.1 cgd break;
105 1.1 cgd case 'v': /* backward compatible */
106 1.1 cgd break;
107 1.1 cgd case '?':
108 1.1 cgd default:
109 1.1 cgd usage();
110 1.1 cgd }
111 1.1 cgd argc -= optind;
112 1.1 cgd argv += optind;
113 1.1 cgd
114 1.1 cgd if (argc != 1)
115 1.1 cgd usage();
116 1.4 cgd
117 1.1 cgd /*
118 1.5 mycroft * This could be changed to allow the user to interrupt.
119 1.5 mycroft * Probably not worth the effort.
120 1.1 cgd */
121 1.1 cgd sigemptyset(&set);
122 1.1 cgd sigaddset(&set, SIGTSTP);
123 1.1 cgd sigaddset(&set, SIGHUP);
124 1.1 cgd sigaddset(&set, SIGINT);
125 1.1 cgd sigaddset(&set, SIGQUIT);
126 1.1 cgd sigaddset(&set, SIGTERM);
127 1.1 cgd (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
128 1.1 cgd
129 1.5 mycroft /* We don't care what the user wants. */
130 1.5 mycroft (void)umask(0);
131 1.5 mycroft
132 1.1 cgd pname = *argv;
133 1.1 cgd /* Open the original password file */
134 1.1 cgd if (!(fp = fopen(pname, "r")))
135 1.1 cgd error(pname);
136 1.1 cgd
137 1.1 cgd /* Open the temporary insecure password database. */
138 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.tmp", _PATH_MP_DB);
139 1.5 mycroft dp = dbopen(buf,
140 1.5 mycroft O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
141 1.5 mycroft if (dp == NULL)
142 1.1 cgd error(buf);
143 1.1 cgd clean = FILE_INSECURE;
144 1.1 cgd
145 1.1 cgd /*
146 1.1 cgd * Open file for old password file. Minor trickiness -- don't want to
147 1.1 cgd * chance the file already existing, since someone (stupidly) might
148 1.1 cgd * still be using this for permission checking. So, open it first and
149 1.5 mycroft * fdopen the resulting fd. The resulting file should be readable by
150 1.5 mycroft * everyone.
151 1.1 cgd */
152 1.1 cgd if (makeold) {
153 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
154 1.1 cgd if ((tfd = open(buf,
155 1.1 cgd O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
156 1.1 cgd error(buf);
157 1.5 mycroft if ((oldfp = fdopen(tfd, "w")) == NULL)
158 1.1 cgd error(buf);
159 1.1 cgd clean = FILE_ORIG;
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd /*
163 1.1 cgd * The databases actually contain three copies of the original data.
164 1.1 cgd * Each password file entry is converted into a rough approximation
165 1.1 cgd * of a ``struct passwd'', with the strings placed inline. This
166 1.1 cgd * object is then stored as the data for three separate keys. The
167 1.1 cgd * first key * is the pw_name field prepended by the _PW_KEYBYNAME
168 1.1 cgd * character. The second key is the pw_uid field prepended by the
169 1.1 cgd * _PW_KEYBYUID character. The third key is the line number in the
170 1.1 cgd * original file prepended by the _PW_KEYBYNUM character. (The special
171 1.1 cgd * characters are prepended to ensure that the keys do not collide.)
172 1.1 cgd */
173 1.1 cgd data.data = (u_char *)buf;
174 1.1 cgd key.data = (u_char *)tbuf;
175 1.1 cgd for (cnt = 1; scan(fp, &pwd); ++cnt) {
176 1.1 cgd #define COMPACT(e) t = e; while (*p++ = *t++);
177 1.1 cgd /* Create insecure data. */
178 1.1 cgd p = buf;
179 1.1 cgd COMPACT(pwd.pw_name);
180 1.1 cgd COMPACT("*");
181 1.5 mycroft memmove(p, &pwd.pw_uid, sizeof(int));
182 1.1 cgd p += sizeof(int);
183 1.5 mycroft memmove(p, &pwd.pw_gid, sizeof(int));
184 1.1 cgd p += sizeof(int);
185 1.5 mycroft memmove(p, &pwd.pw_change, sizeof(time_t));
186 1.1 cgd p += sizeof(time_t);
187 1.1 cgd COMPACT(pwd.pw_class);
188 1.1 cgd COMPACT(pwd.pw_gecos);
189 1.1 cgd COMPACT(pwd.pw_dir);
190 1.1 cgd COMPACT(pwd.pw_shell);
191 1.5 mycroft memmove(p, &pwd.pw_expire, sizeof(time_t));
192 1.1 cgd p += sizeof(time_t);
193 1.1 cgd data.size = p - buf;
194 1.1 cgd
195 1.1 cgd /* Store insecure by name. */
196 1.1 cgd tbuf[0] = _PW_KEYBYNAME;
197 1.1 cgd len = strlen(pwd.pw_name);
198 1.5 mycroft memmove(tbuf + 1, pwd.pw_name, len);
199 1.1 cgd key.size = len + 1;
200 1.1 cgd if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
201 1.1 cgd error("put");
202 1.1 cgd
203 1.1 cgd /* Store insecure by number. */
204 1.1 cgd tbuf[0] = _PW_KEYBYNUM;
205 1.5 mycroft memmove(tbuf + 1, &cnt, sizeof(cnt));
206 1.1 cgd key.size = sizeof(cnt) + 1;
207 1.1 cgd if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
208 1.1 cgd error("put");
209 1.1 cgd
210 1.1 cgd /* Store insecure by uid. */
211 1.1 cgd tbuf[0] = _PW_KEYBYUID;
212 1.5 mycroft memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
213 1.1 cgd key.size = sizeof(pwd.pw_uid) + 1;
214 1.1 cgd if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
215 1.1 cgd error("put");
216 1.1 cgd
217 1.5 mycroft /* Create original format password file entry */
218 1.5 mycroft if (makeold)
219 1.5 mycroft (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
220 1.5 mycroft pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
221 1.5 mycroft pwd.pw_dir, pwd.pw_shell);
222 1.5 mycroft }
223 1.5 mycroft (void)(dp->close)(dp);
224 1.5 mycroft if (makeold) {
225 1.5 mycroft (void)fflush(oldfp);
226 1.5 mycroft (void)fclose(oldfp);
227 1.5 mycroft }
228 1.5 mycroft
229 1.5 mycroft /* Open the temporary encrypted password database. */
230 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.tmp", _PATH_SMP_DB);
231 1.5 mycroft edp = dbopen(buf,
232 1.5 mycroft O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
233 1.5 mycroft if (!edp)
234 1.5 mycroft error(buf);
235 1.5 mycroft clean = FILE_SECURE;
236 1.5 mycroft
237 1.5 mycroft rewind(fp);
238 1.5 mycroft for (cnt = 1; scan(fp, &pwd); ++cnt) {
239 1.5 mycroft
240 1.1 cgd /* Create secure data. */
241 1.1 cgd p = buf;
242 1.1 cgd COMPACT(pwd.pw_name);
243 1.1 cgd COMPACT(pwd.pw_passwd);
244 1.5 mycroft memmove(p, &pwd.pw_uid, sizeof(int));
245 1.1 cgd p += sizeof(int);
246 1.5 mycroft memmove(p, &pwd.pw_gid, sizeof(int));
247 1.1 cgd p += sizeof(int);
248 1.5 mycroft memmove(p, &pwd.pw_change, sizeof(time_t));
249 1.1 cgd p += sizeof(time_t);
250 1.1 cgd COMPACT(pwd.pw_class);
251 1.1 cgd COMPACT(pwd.pw_gecos);
252 1.1 cgd COMPACT(pwd.pw_dir);
253 1.1 cgd COMPACT(pwd.pw_shell);
254 1.5 mycroft memmove(p, &pwd.pw_expire, sizeof(time_t));
255 1.1 cgd p += sizeof(time_t);
256 1.1 cgd data.size = p - buf;
257 1.1 cgd
258 1.1 cgd /* Store secure by name. */
259 1.1 cgd tbuf[0] = _PW_KEYBYNAME;
260 1.1 cgd len = strlen(pwd.pw_name);
261 1.5 mycroft memmove(tbuf + 1, pwd.pw_name, len);
262 1.1 cgd key.size = len + 1;
263 1.1 cgd if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
264 1.1 cgd error("put");
265 1.1 cgd
266 1.1 cgd /* Store secure by number. */
267 1.1 cgd tbuf[0] = _PW_KEYBYNUM;
268 1.5 mycroft memmove(tbuf + 1, &cnt, sizeof(cnt));
269 1.1 cgd key.size = sizeof(cnt) + 1;
270 1.1 cgd if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
271 1.1 cgd error("put");
272 1.1 cgd
273 1.1 cgd /* Store secure by uid. */
274 1.1 cgd tbuf[0] = _PW_KEYBYUID;
275 1.5 mycroft memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
276 1.1 cgd key.size = sizeof(pwd.pw_uid) + 1;
277 1.1 cgd if ((dp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
278 1.1 cgd error("put");
279 1.5 mycroft }
280 1.1 cgd
281 1.1 cgd (void)(edp->close)(edp);
282 1.1 cgd
283 1.1 cgd /* Set master.passwd permissions, in case caller forgot. */
284 1.1 cgd (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
285 1.1 cgd (void)fclose(fp);
286 1.1 cgd
287 1.1 cgd /* Install as the real password files. */
288 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.tmp", _PATH_MP_DB);
289 1.1 cgd mv(buf, _PATH_MP_DB);
290 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.tmp", _PATH_SMP_DB);
291 1.1 cgd mv(buf, _PATH_SMP_DB);
292 1.1 cgd if (makeold) {
293 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
294 1.1 cgd mv(buf, _PATH_PASSWD);
295 1.1 cgd }
296 1.1 cgd /*
297 1.1 cgd * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
298 1.1 cgd * all use flock(2) on it to block other incarnations of themselves.
299 1.1 cgd * The rename means that everything is unlocked, as the original file
300 1.1 cgd * can no longer be accessed.
301 1.1 cgd */
302 1.1 cgd mv(pname, _PATH_MASTERPASSWD);
303 1.1 cgd exit(0);
304 1.1 cgd }
305 1.1 cgd
306 1.5 mycroft int
307 1.1 cgd scan(fp, pw)
308 1.1 cgd FILE *fp;
309 1.1 cgd struct passwd *pw;
310 1.1 cgd {
311 1.1 cgd static int lcnt;
312 1.1 cgd static char line[LINE_MAX];
313 1.1 cgd char *p;
314 1.1 cgd
315 1.1 cgd if (!fgets(line, sizeof(line), fp))
316 1.5 mycroft return (0);
317 1.1 cgd ++lcnt;
318 1.1 cgd /*
319 1.1 cgd * ``... if I swallow anything evil, put your fingers down my
320 1.1 cgd * throat...''
321 1.1 cgd * -- The Who
322 1.1 cgd */
323 1.5 mycroft if (!(p = strchr(line, '\n'))) {
324 1.5 mycroft warnx("line too long");
325 1.1 cgd goto fmt;
326 1.1 cgd
327 1.1 cgd }
328 1.1 cgd *p = '\0';
329 1.1 cgd if (!pw_scan(line, pw)) {
330 1.5 mycroft warnx("at line #%d", lcnt);
331 1.5 mycroft fmt: errno = EFTYPE; /* XXX */
332 1.1 cgd error(pname);
333 1.1 cgd }
334 1.5 mycroft
335 1.5 mycroft return (1);
336 1.1 cgd }
337 1.1 cgd
338 1.5 mycroft void
339 1.1 cgd mv(from, to)
340 1.1 cgd char *from, *to;
341 1.1 cgd {
342 1.1 cgd char buf[MAXPATHLEN];
343 1.1 cgd
344 1.1 cgd if (rename(from, to)) {
345 1.5 mycroft int sverrno = errno;
346 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
347 1.1 cgd errno = sverrno;
348 1.1 cgd error(buf);
349 1.1 cgd }
350 1.1 cgd }
351 1.1 cgd
352 1.5 mycroft void
353 1.1 cgd error(name)
354 1.1 cgd char *name;
355 1.1 cgd {
356 1.5 mycroft
357 1.5 mycroft warn(name);
358 1.1 cgd cleanup();
359 1.1 cgd exit(1);
360 1.1 cgd }
361 1.1 cgd
362 1.5 mycroft void
363 1.1 cgd cleanup()
364 1.1 cgd {
365 1.1 cgd char buf[MAXPATHLEN];
366 1.1 cgd
367 1.1 cgd switch(clean) {
368 1.1 cgd case FILE_ORIG:
369 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
370 1.1 cgd (void)unlink(buf);
371 1.1 cgd /* FALLTHROUGH */
372 1.1 cgd case FILE_SECURE:
373 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.tmp", _PATH_SMP_DB);
374 1.1 cgd (void)unlink(buf);
375 1.1 cgd /* FALLTHROUGH */
376 1.1 cgd case FILE_INSECURE:
377 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s.tmp", _PATH_MP_DB);
378 1.1 cgd (void)unlink(buf);
379 1.1 cgd }
380 1.1 cgd }
381 1.1 cgd
382 1.5 mycroft void
383 1.1 cgd usage()
384 1.1 cgd {
385 1.5 mycroft
386 1.1 cgd (void)fprintf(stderr, "usage: pwd_mkdb [-p] file\n");
387 1.1 cgd exit(1);
388 1.1 cgd }
389