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