]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pwd_mkdb/pwd_mkdb.c
zfs: merge openzfs/zfs@dbda45160
[FreeBSD/FreeBSD.git] / usr.sbin / pwd_mkdb / pwd_mkdb.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/stat.h>
35 #include <arpa/inet.h>
36
37 #include <db.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <libgen.h>
42 #include <limits.h>
43 #include <pwd.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include "pw_scan.h"
51
52 #define INSECURE        1
53 #define SECURE          2
54 #define PERM_INSECURE   (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
55 #define PERM_SECURE     (S_IRUSR|S_IWUSR)
56 #define LEGACY_VERSION(x)  _PW_VERSIONED(x, 3)
57 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
58
59 static HASHINFO openinfo = {
60         4096,           /* bsize */
61         32,             /* ffactor */
62         256,            /* nelem */
63         2048 * 1024,    /* cachesize */
64         NULL,           /* hash() */
65         BIG_ENDIAN      /* lorder */
66 };
67
68 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
69 static struct passwd pwd;                       /* password structure */
70 static char *pname;                             /* password file name */
71 static char prefix[MAXPATHLEN];
72
73 static int is_comment;  /* flag for comments */
74 static char line[LINE_MAX];
75
76 void    cleanup(void);
77 void    error(const char *);
78 void    cp(char *, char *, mode_t mode);
79 void    mv(char *, char *);
80 int     scan(FILE *, struct passwd *);
81 static void     usage(void);
82
83 int
84 main(int argc, char *argv[])
85 {
86         static char verskey[] = _PWD_VERSION_KEY;
87         char version = _PWD_CURRENT_VERSION;
88         DB *dp, *sdp, *pw_db;
89         DBT data, sdata, key;
90         FILE *fp, *oldfp;
91         sigset_t set;
92         int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
93         unsigned int len;
94         uint32_t store;
95         const char *t;
96         char *p;
97         char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
98         char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)];
99         char buf2[MAXPATHLEN];
100         char sbuf2[MAXPATHLEN];
101         char *username;
102         u_int method, methoduid;
103         int Cflag, dflag, iflag;
104         int nblock = 0;
105
106         iflag = dflag = Cflag = 0;
107         strcpy(prefix, _PATH_PWD);
108         makeold = 0;
109         username = NULL;
110         oldfp = NULL;
111         while ((ch = getopt(argc, argv, "Cd:iNps:u:v")) != -1)
112                 switch(ch) {
113                 case 'C':                       /* verify only */
114                         Cflag = 1;
115                         break;
116                 case 'd':
117                         dflag++;
118                         strlcpy(prefix, optarg, sizeof(prefix));
119                         break;
120                 case 'i':
121                         iflag++;
122                         break;
123                 case 'N':                       /* do not wait for lock */
124                         nblock = LOCK_NB;       /* will fail if locked */
125                         break;
126                 case 'p':                       /* create V7 "file.orig" */
127                         makeold = 1;
128                         break;
129                 case 's':                       /* change default cachesize */
130                         openinfo.cachesize = atoi(optarg) * 1024 * 1024;
131                         break;
132                 case 'u':                       /* only update this record */
133                         username = optarg;
134                         break;
135                 case 'v':                       /* backward compatible */
136                         break;
137                 default:
138                         usage();
139                 }
140         argc -= optind;
141         argv += optind;
142
143         if (argc != 1 || (username && (*username == '+' || *username == '-')))
144                 usage();
145
146         /*
147          * This could be changed to allow the user to interrupt.
148          * Probably not worth the effort.
149          */
150         sigemptyset(&set);
151         sigaddset(&set, SIGTSTP);
152         sigaddset(&set, SIGHUP);
153         sigaddset(&set, SIGINT);
154         sigaddset(&set, SIGQUIT);
155         sigaddset(&set, SIGTERM);
156         (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
157
158         /* We don't care what the user wants. */
159         (void)umask(0);
160
161         pname = *argv;
162
163         /*
164          * Open and lock the original password file.  We have to check
165          * the hardlink count after we get the lock to handle any potential
166          * unlink/rename race.
167          *
168          * This lock is necessary when someone runs pwd_mkdb manually, directly
169          * on master.passwd, to handle the case where a user might try to
170          * change his password while pwd_mkdb is running. 
171          */
172         for (;;) {
173                 struct stat st;
174
175                 if (!(fp = fopen(pname, "r")))
176                         error(pname);
177                 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag))
178                         error("flock");
179                 if (fstat(fileno(fp), &st) < 0)
180                         error(pname);
181                 if (st.st_nlink != 0)
182                         break;
183                 fclose(fp);
184                 fp = NULL;
185         }
186
187         /* check only if password database is valid */
188         if (Cflag) {
189                 while (scan(fp, &pwd))
190                         if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) {
191                                 warnx("%s: username too long", pwd.pw_name);
192                                 exit(1);
193                         }
194                 exit(0);
195         }
196
197         /* Open the temporary insecure password database. */
198         (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
199         (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB);
200         if (username) {
201                 int use_version;
202
203                 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
204                 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB);
205
206                 clean = FILE_INSECURE;
207                 cp(buf2, buf, PERM_INSECURE);
208                 dp = dbopen(buf,
209                     O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
210                 if (dp == NULL)
211                         error(buf);
212
213                 clean = FILE_SECURE;
214                 cp(sbuf2, sbuf, PERM_SECURE);
215                 sdp = dbopen(sbuf,
216                     O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
217                 if (sdp == NULL)
218                         error(sbuf);
219
220                 /*
221                  * Do some trouble to check if we should store this users 
222                  * uid. Don't use getpwnam/getpwuid as that interferes 
223                  * with NIS.
224                  */
225                 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL);
226                 if (!pw_db)
227                         error(_MP_DB);
228
229                 key.data = verskey;
230                 key.size = sizeof(verskey)-1;
231                 if ((pw_db->get)(pw_db, &key, &data, 0) == 0)
232                         use_version = *(unsigned char *)data.data;
233                 else
234                         use_version = 3;
235                 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version);
236                 len = strlen(username);
237
238                 /* Only check that username fits in buffer */
239                 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1));
240                 key.data = (u_char *)buf;
241                 key.size = len + 1;
242                 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
243                         p = (char *)data.data;
244
245                         /* jump over pw_name and pw_passwd, to get to pw_uid */
246                         while (*p++)
247                                 ;
248                         while (*p++)
249                                 ;
250
251                         buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version);
252                         memmove(buf + 1, p, sizeof(store));
253                         key.data = (u_char *)buf;
254                         key.size = sizeof(store) + 1;
255
256                         if ((pw_db->get)(pw_db, &key, &data, 0) == 0) {
257                                 /* First field of data.data holds pw_pwname */
258                                 if (!strcmp(data.data, username))
259                                         methoduid = 0;
260                                 else
261                                         methoduid = R_NOOVERWRITE;
262                         } else {
263                                 methoduid = R_NOOVERWRITE;
264                         }
265                 } else {
266                         methoduid = R_NOOVERWRITE;
267                 }
268                 if ((pw_db->close)(pw_db))
269                         error("close pw_db");
270                 method = 0;
271         } else {
272                 dp = dbopen(buf,
273                     O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
274                 if (dp == NULL)
275                         error(buf);
276                 clean = FILE_INSECURE;
277
278                 sdp = dbopen(sbuf,
279                     O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
280                 if (sdp == NULL)
281                         error(sbuf);
282                 clean = FILE_SECURE;
283
284                 method = R_NOOVERWRITE;
285                 methoduid = R_NOOVERWRITE;
286         }
287
288         /*
289          * Open file for old password file.  Minor trickiness -- don't want to
290          * chance the file already existing, since someone (stupidly) might
291          * still be using this for permission checking.  So, open it first and
292          * fdopen the resulting fd.  The resulting file should be readable by
293          * everyone.
294          */
295         if (makeold) {
296                 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
297                 if ((tfd = open(buf,
298                     O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
299                         error(buf);
300                 if ((oldfp = fdopen(tfd, "w")) == NULL)
301                         error(buf);
302                 clean = FILE_ORIG;
303         }
304
305         /*
306          * The databases actually contain three copies of the original data.
307          * Each password file entry is converted into a rough approximation
308          * of a ``struct passwd'', with the strings placed inline.  This
309          * object is then stored as the data for three separate keys.  The
310          * first key * is the pw_name field prepended by the _PW_KEYBYNAME
311          * character.  The second key is the pw_uid field prepended by the
312          * _PW_KEYBYUID character.  The third key is the line number in the
313          * original file prepended by the _PW_KEYBYNUM character.  (The special
314          * characters are prepended to ensure that the keys do not collide.)
315          */
316         /* In order to transition this file into a machine-independent
317          * form, we have to change the format of entries.  However, since
318          * older binaries will still expect the old MD format entries, we 
319          * create those as usual and use versioned tags for the new entries.
320          */
321         if (username == NULL) {
322                 /* Do not add the VERSION tag when updating a single
323                  * user.  When operating on `old format' databases, this
324                  * would result in applications `seeing' only the updated
325                  * entries.
326                  */
327                 key.data = verskey;
328                 key.size = sizeof(verskey)-1;
329                 data.data = &version;
330                 data.size = 1;
331                 if ((dp->put)(dp, &key, &data, 0) == -1)
332                         error("put");
333                 if ((sdp->put)(sdp, &key, &data, 0) == -1)
334                         error("put");
335         }
336         ypcnt = 0;
337         data.data = (u_char *)buf;
338         sdata.data = (u_char *)sbuf;
339         key.data = (u_char *)tbuf;
340         for (cnt = 1; scan(fp, &pwd); ++cnt) {
341                 if (!is_comment && 
342                     (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) {
343                         yp_enabled = 1;
344                         ypcnt++;
345                 }
346                 if (is_comment)
347                         --cnt;
348 #define COMPACT(e)      t = e; while ((*p++ = *t++));
349 #define SCALAR(e)       store = htonl((uint32_t)(e));      \
350                         memmove(p, &store, sizeof(store)); \
351                         p += sizeof(store);
352 #define LSCALAR(e)      store = HTOL((uint32_t)(e));       \
353                         memmove(p, &store, sizeof(store)); \
354                         p += sizeof(store);
355 #define HTOL(e)         (openinfo.lorder == BYTE_ORDER ? \
356                         (uint32_t)(e) : \
357                         bswap32((uint32_t)(e)))
358                 if (!is_comment && 
359                     (!username || (strcmp(username, pwd.pw_name) == 0))) {
360                         /* Create insecure data. */
361                         p = buf;
362                         COMPACT(pwd.pw_name);
363                         COMPACT("*");
364                         SCALAR(pwd.pw_uid);
365                         SCALAR(pwd.pw_gid);
366                         SCALAR(pwd.pw_change);
367                         COMPACT(pwd.pw_class);
368                         COMPACT(pwd.pw_gecos);
369                         COMPACT(pwd.pw_dir);
370                         COMPACT(pwd.pw_shell);
371                         SCALAR(pwd.pw_expire);
372                         SCALAR(pwd.pw_fields);
373                         data.size = p - buf;
374
375                         /* Create secure data. */
376                         p = sbuf;
377                         COMPACT(pwd.pw_name);
378                         COMPACT(pwd.pw_passwd);
379                         SCALAR(pwd.pw_uid);
380                         SCALAR(pwd.pw_gid);
381                         SCALAR(pwd.pw_change);
382                         COMPACT(pwd.pw_class);
383                         COMPACT(pwd.pw_gecos);
384                         COMPACT(pwd.pw_dir);
385                         COMPACT(pwd.pw_shell);
386                         SCALAR(pwd.pw_expire);
387                         SCALAR(pwd.pw_fields);
388                         sdata.size = p - sbuf;
389
390                         /* Store insecure by name. */
391                         tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
392                         len = strlen(pwd.pw_name);
393                         memmove(tbuf + 1, pwd.pw_name, len);
394                         key.size = len + 1;
395                         if ((dp->put)(dp, &key, &data, method) == -1)
396                                 error("put");
397
398                         /* Store insecure by number. */
399                         tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
400                         store = htonl(cnt);
401                         memmove(tbuf + 1, &store, sizeof(store));
402                         key.size = sizeof(store) + 1;
403                         if ((dp->put)(dp, &key, &data, method) == -1)
404                                 error("put");
405
406                         /* Store insecure by uid. */
407                         tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
408                         store = htonl(pwd.pw_uid);
409                         memmove(tbuf + 1, &store, sizeof(store));
410                         key.size = sizeof(store) + 1;
411                         if ((dp->put)(dp, &key, &data, methoduid) == -1)
412                                 error("put");
413
414                         /* Store secure by name. */
415                         tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME);
416                         len = strlen(pwd.pw_name);
417                         memmove(tbuf + 1, pwd.pw_name, len);
418                         key.size = len + 1;
419                         if ((sdp->put)(sdp, &key, &sdata, method) == -1)
420                                 error("put");
421
422                         /* Store secure by number. */
423                         tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM);
424                         store = htonl(cnt);
425                         memmove(tbuf + 1, &store, sizeof(store));
426                         key.size = sizeof(store) + 1;
427                         if ((sdp->put)(sdp, &key, &sdata, method) == -1)
428                                 error("put");
429
430                         /* Store secure by uid. */
431                         tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID);
432                         store = htonl(pwd.pw_uid);
433                         memmove(tbuf + 1, &store, sizeof(store));
434                         key.size = sizeof(store) + 1;
435                         if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
436                                 error("put");
437
438                         /* Store insecure and secure special plus and special minus */
439                         if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
440                                 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM);
441                                 store = htonl(ypcnt);
442                                 memmove(tbuf + 1, &store, sizeof(store));
443                                 key.size = sizeof(store) + 1;
444                                 if ((dp->put)(dp, &key, &data, method) == -1)
445                                         error("put");
446                                 if ((sdp->put)(sdp, &key, &sdata, method) == -1)
447                                         error("put");
448                         }
449                 }
450                 /*
451                  * Create original style password file entry.
452                  *
453                  * Don't copy comments since this could reveal encrypted
454                  * passwords if entries have been simply commented out
455                  * in master.passwd.
456                  */
457                 if (makeold && !is_comment) {
458                         char uidstr[20];
459                         char gidstr[20];
460
461                         snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid);
462                         snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid);
463
464                         if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n",
465                             pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "",
466                             pwd.pw_fields & _PWF_GID ? gidstr : "",
467                             pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0)
468                                 error("write old");
469                 }
470         }
471         /* If YP enabled, set flag. */
472         if (yp_enabled) {
473                 buf[0] = yp_enabled + 2;
474                 data.size = 1;
475                 key.size = 1;
476                 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED);
477                 if ((dp->put)(dp, &key, &data, method) == -1)
478                         error("put");
479                 if ((sdp->put)(sdp, &key, &data, method) == -1)
480                         error("put");
481         }
482
483         if ((dp->close)(dp) == -1)
484                 error("close");
485         if ((sdp->close)(sdp) == -1)
486                 error("close");
487         if (makeold) {
488                 (void)fflush(oldfp);
489                 if (fclose(oldfp) == EOF)
490                         error("close old");
491         }
492
493         /* Set master.passwd permissions, in case caller forgot. */
494         (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
495
496         /* Install as the real password files. */
497         (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
498         (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB);
499         mv(buf, buf2);
500         (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
501         (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB);
502         mv(buf, buf2);
503         if (makeold) {
504                 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD);
505                 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
506                 mv(buf, buf2);
507         }
508         /*
509          * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
510          * all use flock(2) on it to block other incarnations of themselves.
511          * The rename means that everything is unlocked, as the original file
512          * can no longer be accessed.
513          */
514         (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD);
515         mv(pname, buf);
516
517         /*
518          * Close locked password file after rename()
519          */
520         if (fclose(fp) == EOF)
521                 error("close fp");
522
523         exit(0);
524 }
525
526 int
527 scan(FILE *fp, struct passwd *pw)
528 {
529         static int lcnt;
530         size_t len;
531         char *p;
532
533         p = fgetln(fp, &len);
534         if (p == NULL)
535                 return (0);
536         ++lcnt;
537         /*
538          * ``... if I swallow anything evil, put your fingers down my
539          * throat...''
540          *      -- The Who
541          */
542         if (len > 0 && p[len - 1] == '\n')
543                 len--;
544         if (len >= sizeof(line) - 1) {
545                 warnx("line #%d too long", lcnt);
546                 goto fmt;
547         }
548         memcpy(line, p, len);
549         line[len] = '\0';
550
551         /* 
552          * Ignore comments: ^[ \t]*#
553          */
554         for (p = line; *p != '\0'; p++)
555                 if (*p != ' ' && *p != '\t')
556                         break;
557         if (*p == '#' || *p == '\0') {
558                 is_comment = 1;
559                 return(1);
560         } else
561                 is_comment = 0;
562
563         if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) {
564                 warnx("at line #%d", lcnt);
565 fmt:            errno = EFTYPE; /* XXX */
566                 error(pname);
567         }
568
569         return (1);
570 }
571
572 void                    
573 cp(char *from, char *to, mode_t mode)              
574 {               
575         static char buf[MAXBSIZE];
576         int from_fd, rcount, to_fd, wcount;
577
578         if ((from_fd = open(from, O_RDONLY, 0)) < 0)
579                 error(from);
580         if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0)
581                 error(to);
582         while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
583                 wcount = write(to_fd, buf, rcount);
584                 if (rcount != wcount || wcount == -1) {
585                         int sverrno = errno;
586
587                         (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
588                         errno = sverrno;
589                         error(buf);
590                 }
591         }
592         if (rcount < 0) {
593                 int sverrno = errno;
594
595                 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
596                 errno = sverrno;
597                 error(buf);
598         }
599 }
600
601
602 void
603 mv(char *from, char *to)
604 {
605         char buf[MAXPATHLEN];
606         char *to_dir;
607         int to_dir_fd = -1;
608
609         /*
610          * Make sure file is safe on disk. To improve performance we will call
611          * fsync() to the directory where file lies
612          */
613         if (rename(from, to) != 0 ||
614             (to_dir = dirname(to)) == NULL ||
615             (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
616             fsync(to_dir_fd) != 0) {
617                 int sverrno = errno;
618                 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
619                 errno = sverrno;
620                 if (to_dir_fd != -1)
621                         close(to_dir_fd);
622                 error(buf);
623         }
624
625         if (to_dir_fd != -1)
626                 close(to_dir_fd);
627 }
628
629 void
630 error(const char *name)
631 {
632
633         warn("%s", name);
634         cleanup();
635         exit(1);
636 }
637
638 void
639 cleanup(void)
640 {
641         char buf[MAXPATHLEN];
642
643         switch(clean) {
644         case FILE_ORIG:
645                 (void)snprintf(buf, sizeof(buf), "%s.orig", pname);
646                 (void)unlink(buf);
647                 /* FALLTHROUGH */
648         case FILE_SECURE:
649                 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB);
650                 (void)unlink(buf);
651                 /* FALLTHROUGH */
652         case FILE_INSECURE:
653                 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB);
654                 (void)unlink(buf);
655         }
656 }
657
658 static void
659 usage(void)
660 {
661
662         (void)fprintf(stderr,
663 "usage: pwd_mkdb [-CiNp] [-d directory] [-s cachesize] [-u username] file\n");
664         exit(1);
665 }