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