pwd_mkdb.c revision 1.16 1 1.11 fair /*
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.6 phil * Portions Copyright(C) 1994, Jason Downs. All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.1 cgd * 3. All advertising materials mentioning features or use of this software
15 1.1 cgd * must display the following acknowledgement:
16 1.1 cgd * This product includes software developed by the University of
17 1.1 cgd * California, Berkeley and its contributors.
18 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.10 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.10 lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
38 1.10 lukem The Regents of the University of California. All rights reserved.\n");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.10 lukem #if 0
43 1.10 lukem static char sccsid[] = "from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
44 1.10 lukem #else
45 1.16 mycroft __RCSID("$NetBSD: pwd_mkdb.c,v 1.16 1998/07/27 00:52:02 mycroft Exp $");
46 1.10 lukem #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd #include <sys/param.h>
50 1.1 cgd #include <sys/stat.h>
51 1.5 mycroft
52 1.1 cgd #include <db.h>
53 1.5 mycroft #include <err.h>
54 1.1 cgd #include <errno.h>
55 1.5 mycroft #include <fcntl.h>
56 1.1 cgd #include <limits.h>
57 1.5 mycroft #include <pwd.h>
58 1.5 mycroft #include <signal.h>
59 1.1 cgd #include <stdio.h>
60 1.5 mycroft #include <stdlib.h>
61 1.1 cgd #include <string.h>
62 1.5 mycroft #include <unistd.h>
63 1.7 jtc #include <util.h>
64 1.1 cgd
65 1.1 cgd #define INSECURE 1
66 1.1 cgd #define SECURE 2
67 1.1 cgd #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
68 1.1 cgd #define PERM_SECURE (S_IRUSR|S_IWUSR)
69 1.1 cgd
70 1.6 phil /* pull this out of the C library. */
71 1.6 phil extern const char __yp_token[];
72 1.6 phil
73 1.5 mycroft HASHINFO openinfo = {
74 1.5 mycroft 4096, /* bsize */
75 1.5 mycroft 32, /* ffactor */
76 1.5 mycroft 256, /* nelem */
77 1.5 mycroft 2048 * 1024, /* cachesize */
78 1.5 mycroft NULL, /* hash() */
79 1.5 mycroft 0 /* lorder */
80 1.5 mycroft };
81 1.1 cgd
82 1.1 cgd static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
83 1.1 cgd static struct passwd pwd; /* password structure */
84 1.1 cgd static char *pname; /* password file name */
85 1.8 lukem static char prefix[MAXPATHLEN];
86 1.11 fair static char oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
87 1.11 fair static char pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
88 1.11 fair static char pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
89 1.1 cgd
90 1.5 mycroft void cleanup __P((void));
91 1.5 mycroft void error __P((char *));
92 1.11 fair void wr_error __P((char *));
93 1.10 lukem int main __P((int, char **));
94 1.5 mycroft void mv __P((char *, char *));
95 1.11 fair void rm __P((char *));
96 1.6 phil int scan __P((FILE *, struct passwd *, int *));
97 1.5 mycroft void usage __P((void));
98 1.5 mycroft
99 1.5 mycroft int
100 1.1 cgd main(argc, argv)
101 1.1 cgd int argc;
102 1.5 mycroft char *argv[];
103 1.1 cgd {
104 1.5 mycroft DB *dp, *edp;
105 1.5 mycroft DBT data, key;
106 1.1 cgd FILE *fp, *oldfp;
107 1.1 cgd sigset_t set;
108 1.6 phil int ch, cnt, len, makeold, tfd, flags;
109 1.16 mycroft char *p;
110 1.16 mycroft const char *t;
111 1.11 fair char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
112 1.6 phil int hasyp = 0;
113 1.6 phil DBT ypdata, ypkey;
114 1.1 cgd
115 1.10 lukem oldfp = NULL;
116 1.8 lukem strcpy(prefix, "/");
117 1.1 cgd makeold = 0;
118 1.10 lukem while ((ch = getopt(argc, argv, "d:pv")) != -1)
119 1.1 cgd switch(ch) {
120 1.8 lukem case 'd':
121 1.8 lukem strncpy(prefix, optarg, sizeof(prefix));
122 1.8 lukem prefix[sizeof(prefix)-1] = '\0';
123 1.8 lukem break;
124 1.1 cgd case 'p': /* create V7 "file.orig" */
125 1.1 cgd makeold = 1;
126 1.1 cgd break;
127 1.1 cgd case 'v': /* backward compatible */
128 1.1 cgd break;
129 1.1 cgd case '?':
130 1.1 cgd default:
131 1.1 cgd usage();
132 1.1 cgd }
133 1.1 cgd argc -= optind;
134 1.1 cgd argv += optind;
135 1.1 cgd
136 1.1 cgd if (argc != 1)
137 1.1 cgd usage();
138 1.4 cgd
139 1.1 cgd /*
140 1.5 mycroft * This could be changed to allow the user to interrupt.
141 1.5 mycroft * Probably not worth the effort.
142 1.1 cgd */
143 1.1 cgd sigemptyset(&set);
144 1.1 cgd sigaddset(&set, SIGTSTP);
145 1.1 cgd sigaddset(&set, SIGHUP);
146 1.1 cgd sigaddset(&set, SIGINT);
147 1.1 cgd sigaddset(&set, SIGQUIT);
148 1.1 cgd sigaddset(&set, SIGTERM);
149 1.1 cgd (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
150 1.1 cgd
151 1.5 mycroft /* We don't care what the user wants. */
152 1.5 mycroft (void)umask(0);
153 1.5 mycroft
154 1.1 cgd pname = *argv;
155 1.1 cgd /* Open the original password file */
156 1.1 cgd if (!(fp = fopen(pname, "r")))
157 1.1 cgd error(pname);
158 1.1 cgd
159 1.1 cgd /* Open the temporary insecure password database. */
160 1.11 fair (void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp", prefix,
161 1.11 fair _PATH_MP_DB);
162 1.11 fair dp = dbopen(pwd_db_tmp,
163 1.5 mycroft O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
164 1.5 mycroft if (dp == NULL)
165 1.11 fair error(pwd_db_tmp);
166 1.1 cgd clean = FILE_INSECURE;
167 1.1 cgd
168 1.1 cgd /*
169 1.1 cgd * Open file for old password file. Minor trickiness -- don't want to
170 1.1 cgd * chance the file already existing, since someone (stupidly) might
171 1.1 cgd * still be using this for permission checking. So, open it first and
172 1.5 mycroft * fdopen the resulting fd. The resulting file should be readable by
173 1.5 mycroft * everyone.
174 1.1 cgd */
175 1.1 cgd if (makeold) {
176 1.11 fair (void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
177 1.11 fair pname);
178 1.11 fair if ((tfd = open(oldpwdfile,
179 1.1 cgd O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
180 1.11 fair error(oldpwdfile);
181 1.5 mycroft if ((oldfp = fdopen(tfd, "w")) == NULL)
182 1.11 fair error(oldpwdfile);
183 1.1 cgd clean = FILE_ORIG;
184 1.1 cgd }
185 1.1 cgd
186 1.1 cgd /*
187 1.1 cgd * The databases actually contain three copies of the original data.
188 1.1 cgd * Each password file entry is converted into a rough approximation
189 1.1 cgd * of a ``struct passwd'', with the strings placed inline. This
190 1.1 cgd * object is then stored as the data for three separate keys. The
191 1.1 cgd * first key * is the pw_name field prepended by the _PW_KEYBYNAME
192 1.1 cgd * character. The second key is the pw_uid field prepended by the
193 1.1 cgd * _PW_KEYBYUID character. The third key is the line number in the
194 1.1 cgd * original file prepended by the _PW_KEYBYNUM character. (The special
195 1.1 cgd * characters are prepended to ensure that the keys do not collide.)
196 1.6 phil *
197 1.6 phil * If we see something go by that looks like YP, we save a special
198 1.6 phil * pointer record, which if YP is enabled in the C lib, will speed
199 1.6 phil * things up.
200 1.1 cgd */
201 1.1 cgd data.data = (u_char *)buf;
202 1.1 cgd key.data = (u_char *)tbuf;
203 1.6 phil for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
204 1.8 lukem #define COMPACT(e) t = e; while ((*p++ = *t++));
205 1.6 phil
206 1.6 phil /* look like YP? */
207 1.6 phil if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
208 1.6 phil hasyp++;
209 1.9 thorpej
210 1.9 thorpej /*
211 1.9 thorpej * Warn about potentially unsafe uid/gid overrides.
212 1.9 thorpej */
213 1.9 thorpej if (pwd.pw_name[0] == '+') {
214 1.9 thorpej if ((flags & _PASSWORD_NOUID) == 0 && pwd.pw_uid == 0)
215 1.15 lukem warnx(
216 1.15 lukem "line %d: superuser override in YP inclusion",
217 1.15 lukem cnt);
218 1.9 thorpej if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
219 1.15 lukem warnx("line %d: wheel override in YP inclusion",
220 1.15 lukem cnt);
221 1.9 thorpej }
222 1.6 phil
223 1.1 cgd /* Create insecure data. */
224 1.1 cgd p = buf;
225 1.1 cgd COMPACT(pwd.pw_name);
226 1.1 cgd COMPACT("*");
227 1.5 mycroft memmove(p, &pwd.pw_uid, sizeof(int));
228 1.1 cgd p += sizeof(int);
229 1.5 mycroft memmove(p, &pwd.pw_gid, sizeof(int));
230 1.1 cgd p += sizeof(int);
231 1.5 mycroft memmove(p, &pwd.pw_change, sizeof(time_t));
232 1.1 cgd p += sizeof(time_t);
233 1.1 cgd COMPACT(pwd.pw_class);
234 1.1 cgd COMPACT(pwd.pw_gecos);
235 1.1 cgd COMPACT(pwd.pw_dir);
236 1.1 cgd COMPACT(pwd.pw_shell);
237 1.5 mycroft memmove(p, &pwd.pw_expire, sizeof(time_t));
238 1.1 cgd p += sizeof(time_t);
239 1.6 phil memmove(p, &flags, sizeof(int));
240 1.6 phil p += sizeof(int);
241 1.1 cgd data.size = p - buf;
242 1.1 cgd
243 1.1 cgd /* Store insecure by name. */
244 1.1 cgd tbuf[0] = _PW_KEYBYNAME;
245 1.1 cgd len = strlen(pwd.pw_name);
246 1.5 mycroft memmove(tbuf + 1, pwd.pw_name, len);
247 1.1 cgd key.size = len + 1;
248 1.1 cgd if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
249 1.11 fair wr_error(pwd_db_tmp);
250 1.1 cgd
251 1.1 cgd /* Store insecure by number. */
252 1.1 cgd tbuf[0] = _PW_KEYBYNUM;
253 1.5 mycroft memmove(tbuf + 1, &cnt, sizeof(cnt));
254 1.1 cgd key.size = sizeof(cnt) + 1;
255 1.1 cgd if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
256 1.11 fair wr_error(pwd_db_tmp);
257 1.1 cgd
258 1.1 cgd /* Store insecure by uid. */
259 1.1 cgd tbuf[0] = _PW_KEYBYUID;
260 1.5 mycroft memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
261 1.1 cgd key.size = sizeof(pwd.pw_uid) + 1;
262 1.1 cgd if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
263 1.11 fair wr_error(pwd_db_tmp);
264 1.1 cgd
265 1.5 mycroft /* Create original format password file entry */
266 1.11 fair if (makeold) {
267 1.5 mycroft (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
268 1.5 mycroft pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
269 1.5 mycroft pwd.pw_dir, pwd.pw_shell);
270 1.11 fair if (ferror(oldfp)) {
271 1.11 fair wr_error(oldpwdfile);
272 1.11 fair }
273 1.11 fair }
274 1.5 mycroft }
275 1.6 phil
276 1.6 phil /* Store YP token, if needed. */
277 1.6 phil if(hasyp) {
278 1.6 phil ypkey.data = (u_char *)__yp_token;
279 1.6 phil ypkey.size = strlen(__yp_token);
280 1.6 phil ypdata.data = (u_char *)NULL;
281 1.6 phil ypdata.size = 0;
282 1.6 phil
283 1.6 phil if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
284 1.11 fair wr_error(pwd_db_tmp);
285 1.6 phil }
286 1.6 phil
287 1.11 fair if ((dp->close)(dp) < 0) {
288 1.11 fair wr_error(pwd_db_tmp);
289 1.11 fair }
290 1.5 mycroft if (makeold) {
291 1.11 fair if (fflush(oldfp) == EOF) {
292 1.11 fair wr_error(oldpwdfile);
293 1.11 fair }
294 1.11 fair if (fclose(oldfp) == EOF) {
295 1.11 fair wr_error(oldpwdfile);
296 1.11 fair }
297 1.5 mycroft }
298 1.5 mycroft
299 1.5 mycroft /* Open the temporary encrypted password database. */
300 1.11 fair (void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
301 1.11 fair _PATH_SMP_DB);
302 1.11 fair edp = dbopen(pwd_Sdb_tmp,
303 1.5 mycroft O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
304 1.5 mycroft if (!edp)
305 1.11 fair error(pwd_Sdb_tmp);
306 1.5 mycroft clean = FILE_SECURE;
307 1.5 mycroft
308 1.5 mycroft rewind(fp);
309 1.6 phil for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
310 1.5 mycroft
311 1.1 cgd /* Create secure data. */
312 1.1 cgd p = buf;
313 1.1 cgd COMPACT(pwd.pw_name);
314 1.1 cgd COMPACT(pwd.pw_passwd);
315 1.5 mycroft memmove(p, &pwd.pw_uid, sizeof(int));
316 1.1 cgd p += sizeof(int);
317 1.5 mycroft memmove(p, &pwd.pw_gid, sizeof(int));
318 1.1 cgd p += sizeof(int);
319 1.5 mycroft memmove(p, &pwd.pw_change, sizeof(time_t));
320 1.1 cgd p += sizeof(time_t);
321 1.1 cgd COMPACT(pwd.pw_class);
322 1.1 cgd COMPACT(pwd.pw_gecos);
323 1.1 cgd COMPACT(pwd.pw_dir);
324 1.1 cgd COMPACT(pwd.pw_shell);
325 1.5 mycroft memmove(p, &pwd.pw_expire, sizeof(time_t));
326 1.1 cgd p += sizeof(time_t);
327 1.6 phil memmove(p, &flags, sizeof(int));
328 1.6 phil p += sizeof(int);
329 1.1 cgd data.size = p - buf;
330 1.1 cgd
331 1.1 cgd /* Store secure by name. */
332 1.1 cgd tbuf[0] = _PW_KEYBYNAME;
333 1.1 cgd len = strlen(pwd.pw_name);
334 1.5 mycroft memmove(tbuf + 1, pwd.pw_name, len);
335 1.1 cgd key.size = len + 1;
336 1.11 fair if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
337 1.12 fair wr_error(pwd_Sdb_tmp);
338 1.1 cgd
339 1.1 cgd /* Store secure by number. */
340 1.1 cgd tbuf[0] = _PW_KEYBYNUM;
341 1.5 mycroft memmove(tbuf + 1, &cnt, sizeof(cnt));
342 1.1 cgd key.size = sizeof(cnt) + 1;
343 1.11 fair if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
344 1.12 fair wr_error(pwd_Sdb_tmp);
345 1.1 cgd
346 1.1 cgd /* Store secure by uid. */
347 1.1 cgd tbuf[0] = _PW_KEYBYUID;
348 1.5 mycroft memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
349 1.1 cgd key.size = sizeof(pwd.pw_uid) + 1;
350 1.11 fair if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1)
351 1.12 fair wr_error(pwd_Sdb_tmp);
352 1.5 mycroft }
353 1.1 cgd
354 1.6 phil /* Store YP token, if needed. */
355 1.6 phil if(hasyp) {
356 1.6 phil ypkey.data = (u_char *)__yp_token;
357 1.6 phil ypkey.size = strlen(__yp_token);
358 1.6 phil ypdata.data = (u_char *)NULL;
359 1.6 phil ypdata.size = 0;
360 1.6 phil
361 1.11 fair if((edp->put)(edp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
362 1.12 fair wr_error(pwd_Sdb_tmp);
363 1.6 phil }
364 1.6 phil
365 1.11 fair if ((edp->close)(edp) < 0) {
366 1.11 fair wr_error(_PATH_SMP_DB);
367 1.11 fair }
368 1.1 cgd
369 1.1 cgd /* Set master.passwd permissions, in case caller forgot. */
370 1.1 cgd (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
371 1.11 fair if (fclose(fp) == EOF) {
372 1.11 fair wr_error(pname);
373 1.11 fair }
374 1.1 cgd
375 1.1 cgd /* Install as the real password files. */
376 1.11 fair {
377 1.11 fair char destination[MAXPATHLEN];
378 1.11 fair
379 1.11 fair (void)snprintf(destination, sizeof(destination),
380 1.11 fair "%s%s", prefix, _PATH_MP_DB);
381 1.11 fair mv(pwd_db_tmp, destination);
382 1.11 fair
383 1.11 fair (void)snprintf(destination, sizeof(destination),
384 1.11 fair "%s%s", prefix, _PATH_SMP_DB);
385 1.11 fair mv(pwd_Sdb_tmp, destination);
386 1.11 fair
387 1.11 fair if (makeold) {
388 1.11 fair (void)snprintf(destination, sizeof(destination),
389 1.11 fair "%s%s", prefix, _PATH_PASSWD);
390 1.11 fair mv(oldpwdfile, destination);
391 1.11 fair }
392 1.11 fair
393 1.11 fair /*
394 1.11 fair * Move the master password LAST -- chpass(1),
395 1.11 fair * passwd(1) and vipw(8) all use flock(2) on it to
396 1.11 fair * block other incarnations of themselves. The rename
397 1.11 fair * means that everything is unlocked, as the original
398 1.11 fair * file can no longer be accessed.
399 1.11 fair */
400 1.11 fair (void)snprintf(destination, sizeof(destination),
401 1.11 fair "%s%s", prefix, _PATH_MASTERPASSWD);
402 1.11 fair mv(pname, destination);
403 1.1 cgd }
404 1.1 cgd exit(0);
405 1.1 cgd }
406 1.1 cgd
407 1.5 mycroft int
408 1.6 phil scan(fp, pw, flags)
409 1.1 cgd FILE *fp;
410 1.1 cgd struct passwd *pw;
411 1.6 phil int *flags;
412 1.1 cgd {
413 1.1 cgd static int lcnt;
414 1.1 cgd static char line[LINE_MAX];
415 1.1 cgd char *p;
416 1.1 cgd
417 1.1 cgd if (!fgets(line, sizeof(line), fp))
418 1.5 mycroft return (0);
419 1.1 cgd ++lcnt;
420 1.1 cgd /*
421 1.1 cgd * ``... if I swallow anything evil, put your fingers down my
422 1.1 cgd * throat...''
423 1.1 cgd * -- The Who
424 1.1 cgd */
425 1.5 mycroft if (!(p = strchr(line, '\n'))) {
426 1.5 mycroft warnx("line too long");
427 1.1 cgd goto fmt;
428 1.1 cgd
429 1.1 cgd }
430 1.1 cgd *p = '\0';
431 1.15 lukem if (strcmp(line, "+") == 0)
432 1.15 lukem strcpy(line, "+:::::::::"); /* pw_scan() can't handle "+" */
433 1.14 lukem *flags = 0;
434 1.6 phil if (!pw_scan(line, pw, flags)) {
435 1.5 mycroft warnx("at line #%d", lcnt);
436 1.5 mycroft fmt: errno = EFTYPE; /* XXX */
437 1.1 cgd error(pname);
438 1.1 cgd }
439 1.5 mycroft
440 1.5 mycroft return (1);
441 1.1 cgd }
442 1.1 cgd
443 1.5 mycroft void
444 1.1 cgd mv(from, to)
445 1.1 cgd char *from, *to;
446 1.1 cgd {
447 1.1 cgd char buf[MAXPATHLEN];
448 1.1 cgd
449 1.1 cgd if (rename(from, to)) {
450 1.5 mycroft int sverrno = errno;
451 1.5 mycroft (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
452 1.1 cgd errno = sverrno;
453 1.1 cgd error(buf);
454 1.1 cgd }
455 1.1 cgd }
456 1.1 cgd
457 1.5 mycroft void
458 1.11 fair wr_error(name)
459 1.11 fair char *name;
460 1.11 fair {
461 1.11 fair char errbuf[BUFSIZ];
462 1.11 fair int sverrno = errno;
463 1.11 fair
464 1.11 fair (void)snprintf(errbuf, sizeof(errbuf),
465 1.11 fair "attempt to write %s failed", name);
466 1.11 fair
467 1.11 fair errno = sverrno;
468 1.11 fair error(errbuf);
469 1.11 fair }
470 1.11 fair
471 1.11 fair void
472 1.1 cgd error(name)
473 1.1 cgd char *name;
474 1.1 cgd {
475 1.5 mycroft
476 1.5 mycroft warn(name);
477 1.1 cgd cleanup();
478 1.11 fair #ifdef think_about_this_a_while_longer
479 1.11 fair fputs("NOTE: possible inconsistencies between text files and databases\n", stderr);
480 1.11 fair fputs("re-run pwd_mkdb when you have fixed the problem.\n", stderr);
481 1.11 fair #endif
482 1.1 cgd exit(1);
483 1.1 cgd }
484 1.1 cgd
485 1.5 mycroft void
486 1.11 fair rm(victim)
487 1.11 fair char *victim;
488 1.11 fair {
489 1.11 fair if (unlink(victim) < 0) {
490 1.11 fair warn("unlink(%s)", victim);
491 1.11 fair }
492 1.11 fair }
493 1.11 fair
494 1.11 fair void
495 1.1 cgd cleanup()
496 1.1 cgd {
497 1.1 cgd switch(clean) {
498 1.1 cgd case FILE_ORIG:
499 1.11 fair rm(oldpwdfile);
500 1.1 cgd /* FALLTHROUGH */
501 1.1 cgd case FILE_SECURE:
502 1.11 fair rm(pwd_Sdb_tmp);
503 1.1 cgd /* FALLTHROUGH */
504 1.1 cgd case FILE_INSECURE:
505 1.11 fair rm(pwd_db_tmp);
506 1.1 cgd }
507 1.1 cgd }
508 1.1 cgd
509 1.5 mycroft void
510 1.1 cgd usage()
511 1.1 cgd {
512 1.5 mycroft
513 1.8 lukem (void)fprintf(stderr, "usage: pwd_mkdb [-p] [-d directory] file\n");
514 1.1 cgd exit(1);
515 1.1 cgd }
516