pwd_mkdb.c revision 1.25 1 /* $NetBSD: pwd_mkdb.c,v 1.25 2003/05/17 19:18:59 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Portions Copyright(C) 1994, Jason Downs. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #if HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <sys/cdefs.h>
42 #if defined(__RCSID) && !defined(lint)
43 __COPYRIGHT("@(#) Copyright (c) 2000\n\
44 The NetBSD Foundation, Inc. All rights reserved.\n\
45 Copyright (c) 1991, 1993, 1994\n\
46 The Regents of the University of California. All rights reserved.\n");
47 __SCCSID("from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94");
48 __RCSID("$NetBSD: pwd_mkdb.c,v 1.25 2003/05/17 19:18:59 itojun Exp $");
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/stat.h>
53
54 #include <db.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <limits.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <util.h>
65
66 #if HAVE_CONFIG_H
67 #include "compat_pwd.h"
68 #else
69 #include <pwd.h>
70 #endif
71
72 #define MAX_CACHESIZE 8*1024*1024
73 #define MIN_CACHESIZE 2*1024*1024
74
75 #define PERM_INSECURE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
76 #define PERM_SECURE (S_IRUSR | S_IWUSR)
77
78 #if HAVE_CONFIG_H
79 static const char __yp_token[] = "__YP!";
80 #else
81 /* Pull this out of the C library. */
82 extern const char __yp_token[];
83 #endif
84
85 HASHINFO openinfo = {
86 4096, /* bsize */
87 32, /* ffactor */
88 256, /* nelem */
89 0, /* cachesize */
90 NULL, /* hash() */
91 0 /* lorder */
92 };
93
94 #define FILE_INSECURE 0x01
95 #define FILE_SECURE 0x02
96 #define FILE_ORIG 0x04
97
98 static char *pname; /* password file name */
99 static char prefix[MAXPATHLEN];
100 static char oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
101 static char pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
102 static char pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
103 static int lorder = BYTE_ORDER;
104 static int clean;
105
106 void bailout(void);
107 void cp(const char *, const char *, mode_t);
108 int deldbent(DB *, const char *, int, void *);
109 void error(const char *);
110 int getdbent(DB *, const char *, int, void *, struct passwd **);
111 void inconsistancy(void);
112 void install(const char *, const char *);
113 int main(int, char **);
114 void putdbents(DB *, struct passwd *, const char *, int, const char *, int,
115 int, int);
116 void putyptoken(DB *, const char *);
117 void rm(const char *);
118 int scan(FILE *, struct passwd *, int *, int *);
119 void usage(void);
120 void wr_error(const char *);
121
122 int
123 main(int argc, char *argv[])
124 {
125 int ch, makeold, tfd, lineno, found, rv, hasyp, secureonly;
126 struct passwd pwd, *tpwd;
127 char *username;
128 DB *dp, *edp;
129 FILE *fp, *oldfp;
130 sigset_t set;
131 int dbflg, uid_dbflg, newuser, olduid, flags;
132 char buf[MAXPATHLEN];
133 struct stat st;
134 u_int cachesize;
135
136 prefix[0] = '\0';
137 makeold = 0;
138 oldfp = NULL;
139 username = NULL;
140 hasyp = 0;
141 secureonly = 0;
142
143 while ((ch = getopt(argc, argv, "BLd:psu:v")) != -1)
144 switch (ch) {
145 case 'B': /* big-endian output */
146 lorder = BIG_ENDIAN;
147 break;
148 case 'L': /* little-endian output */
149 lorder = LITTLE_ENDIAN;
150 break;
151 case 'd': /* set prefix */
152 strlcpy(prefix, optarg, sizeof(prefix));
153 break;
154 case 'p': /* create V7 "file.orig" */
155 makeold = 1;
156 break;
157 case 's': /* modify secure db only */
158 secureonly = 1;
159 break;
160 case 'u': /* modify one user only */
161 username = optarg;
162 break;
163 case 'v': /* backward compatible */
164 break;
165 case '?':
166 default:
167 usage();
168 }
169 argc -= optind;
170 argv += optind;
171
172 if (argc != 1)
173 usage();
174 if (username != NULL)
175 if (username[0] == '+' || username[0] == '-')
176 usage();
177 if (secureonly)
178 makeold = 0;
179
180 /*
181 * This could be changed to allow the user to interrupt.
182 * Probably not worth the effort.
183 */
184 sigemptyset(&set);
185 sigaddset(&set, SIGTSTP);
186 sigaddset(&set, SIGHUP);
187 sigaddset(&set, SIGINT);
188 sigaddset(&set, SIGQUIT);
189 sigaddset(&set, SIGTERM);
190 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
191
192 /* We don't care what the user wants. */
193 (void)umask(0);
194
195 if (username == NULL)
196 flags = O_RDWR | O_CREAT | O_EXCL;
197 else
198 flags = O_RDWR;
199
200 pname = *argv;
201 /* Open the original password file */
202 if ((fp = fopen(pname, "r")) == NULL)
203 error(pname);
204
205 openinfo.lorder = lorder;
206
207 if (fstat(fileno(fp), &st) == -1)
208 error(pname);
209
210 /* Tweak openinfo values for large passwd files. */
211 cachesize = st.st_size * 20;
212 if (cachesize > MAX_CACHESIZE)
213 cachesize = MAX_CACHESIZE;
214 else if (cachesize < MIN_CACHESIZE)
215 cachesize = MIN_CACHESIZE;
216 openinfo.cachesize = cachesize;
217
218 /* Open the temporary insecure password database. */
219 if (!secureonly) {
220 (void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp",
221 prefix, _PATH_MP_DB);
222 if (username != NULL) {
223 snprintf(buf, sizeof(buf), "%s" _PATH_MP_DB, prefix);
224 cp(buf, pwd_db_tmp, PERM_INSECURE);
225 }
226 dp = dbopen(pwd_db_tmp, flags, PERM_INSECURE, DB_HASH,
227 &openinfo);
228 if (dp == NULL)
229 error(pwd_db_tmp);
230 clean |= FILE_INSECURE;
231 }
232
233 /* Open the temporary encrypted password database. */
234 (void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
235 _PATH_SMP_DB);
236 if (username != NULL) {
237 snprintf(buf, sizeof(buf), "%s" _PATH_SMP_DB, prefix);
238 cp(buf, pwd_Sdb_tmp, PERM_SECURE);
239 }
240 edp = dbopen(pwd_Sdb_tmp, flags, PERM_SECURE, DB_HASH, &openinfo);
241 if (!edp)
242 error(pwd_Sdb_tmp);
243 clean |= FILE_SECURE;
244
245 /*
246 * Open file for old password file. Minor trickiness -- don't want to
247 * chance the file already existing, since someone (stupidly) might
248 * still be using this for permission checking. So, open it first and
249 * fdopen the resulting fd. The resulting file should be readable by
250 * everyone.
251 */
252 if (makeold) {
253 (void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
254 pname);
255 if ((tfd = open(oldpwdfile, O_WRONLY | O_CREAT | O_EXCL,
256 PERM_INSECURE)) < 0)
257 error(oldpwdfile);
258 clean |= FILE_ORIG;
259 if ((oldfp = fdopen(tfd, "w")) == NULL)
260 error(oldpwdfile);
261 }
262
263 if (username != NULL) {
264 uid_dbflg = 0;
265 dbflg = 0;
266 found = 0;
267
268 /*
269 * Determine if this is a new entry.
270 */
271 if (getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYNAME, username, &tpwd))
272 newuser = 1;
273 else {
274 newuser = 0;
275 olduid = tpwd->pw_uid;
276 }
277
278 } else {
279 uid_dbflg = R_NOOVERWRITE;
280 dbflg = R_NOOVERWRITE;
281 }
282
283 /*
284 * If we see something go by that looks like YP, we save a special
285 * pointer record, which if YP is enabled in the C lib, will speed
286 * things up.
287 */
288 for (lineno = 0; scan(fp, &pwd, &flags, &lineno);) {
289 /*
290 * Create original format password file entry.
291 */
292 if (makeold) {
293 (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
294 pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
295 pwd.pw_dir, pwd.pw_shell);
296 if (ferror(oldfp))
297 wr_error(oldpwdfile);
298 }
299
300 if (username == NULL) {
301 /* Look like YP? */
302 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')
303 hasyp++;
304
305 /* Warn about potentially unsafe uid/gid overrides. */
306 if (pwd.pw_name[0] == '+') {
307 if ((flags & _PASSWORD_NOUID) == 0 &&
308 pwd.pw_uid == 0)
309 warnx("line %d: superuser override "
310 "in YP inclusion", lineno);
311 if ((flags & _PASSWORD_NOGID) == 0 &&
312 pwd.pw_gid == 0)
313 warnx("line %d: wheel override "
314 "in YP inclusion", lineno);
315 }
316
317 /* Write the database entry out. */
318 if (!secureonly)
319 putdbents(dp, &pwd, "*", flags, pwd_db_tmp,
320 lineno, dbflg, uid_dbflg);
321 continue;
322 } else if (strcmp(username, pwd.pw_name) != 0)
323 continue;
324
325 if (found) {
326 warnx("user `%s' listed twice in password file",
327 username);
328 bailout();
329 }
330
331 /*
332 * Ensure that the text file and database agree on
333 * which line the record is from.
334 */
335 rv = getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYNUM, &lineno, &tpwd);
336 if (newuser) {
337 if (rv == 0)
338 inconsistancy();
339 } else if (rv == -1 ||
340 strcmp(username, tpwd->pw_name) != 0)
341 inconsistancy();
342 else if (olduid != pwd.pw_uid) {
343 /*
344 * If we're changing UID, remove the BYUID
345 * record for the old UID only if it has the
346 * same username.
347 */
348 if (!getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYUID, &olduid,
349 &tpwd)) {
350 if (strcmp(username, tpwd->pw_name) == 0) {
351 if (!secureonly)
352 deldbent(dp, pwd_db_tmp,
353 _PW_KEYBYUID, &olduid);
354 deldbent(edp, pwd_Sdb_tmp,
355 _PW_KEYBYUID, &olduid);
356 }
357 } else
358 inconsistancy();
359 }
360
361 /*
362 * If there's an existing BYUID record for the new UID and
363 * the username doesn't match then be sure not to overwrite
364 * it.
365 */
366 if (!getdbent(edp, pwd_Sdb_tmp, _PW_KEYBYUID, &pwd.pw_uid,
367 &tpwd))
368 if (strcmp(username, tpwd->pw_name) != 0)
369 uid_dbflg = R_NOOVERWRITE;
370
371 /* Write the database entries out */
372 if (!secureonly)
373 putdbents(dp, &pwd, "*", flags, pwd_db_tmp, lineno,
374 dbflg, uid_dbflg);
375 putdbents(edp, &pwd, pwd.pw_passwd, flags, pwd_Sdb_tmp,
376 lineno, dbflg, uid_dbflg);
377
378 found = 1;
379 if (!makeold)
380 break;
381 }
382
383 if (!secureonly) {
384 /* Store YP token if needed. */
385 if (hasyp)
386 putyptoken(dp, pwd_db_tmp);
387
388 /* Close the insecure database. */
389 if ((*dp->close)(dp) < 0)
390 wr_error(pwd_db_tmp);
391 }
392
393 /*
394 * If rebuilding the databases, we re-parse the text file and write
395 * the secure entries out in a separate pass.
396 */
397 if (username == NULL) {
398 rewind(fp);
399 for (lineno = 0; scan(fp, &pwd, &flags, &lineno);)
400 putdbents(edp, &pwd, pwd.pw_passwd, flags, pwd_Sdb_tmp,
401 lineno, dbflg, uid_dbflg);
402
403 /* Store YP token if needed. */
404 if (hasyp)
405 putyptoken(edp, pwd_Sdb_tmp);
406 } else if (!found) {
407 warnx("user `%s' not found in password file", username);
408 bailout();
409 }
410
411 /* Close the secure database. */
412 if ((*edp->close)(edp) < 0)
413 wr_error(pwd_Sdb_tmp);
414
415 /* Install as the real password files. */
416 if (!secureonly)
417 install(pwd_db_tmp, _PATH_MP_DB);
418 install(pwd_Sdb_tmp, _PATH_SMP_DB);
419
420 /* Install the V7 password file. */
421 if (makeold) {
422 if (fflush(oldfp) == EOF)
423 wr_error(oldpwdfile);
424 if (fclose(oldfp) == EOF)
425 wr_error(oldpwdfile);
426 install(oldpwdfile, _PATH_PASSWD);
427 }
428
429 /* Set master.passwd permissions, in case caller forgot. */
430 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR);
431 if (fclose(fp) == EOF)
432 wr_error(pname);
433
434 /*
435 * Move the temporary master password file LAST -- chpass(1),
436 * passwd(1), vipw(8) and friends all use its existance to block
437 * other incarnations of themselves. The rename means that
438 * everything is unlocked, as the original file can no longer be
439 * accessed.
440 */
441 install(pname, _PATH_MASTERPASSWD);
442 exit(EXIT_SUCCESS);
443 /* NOTREACHED */
444 }
445
446 int
447 scan(FILE *fp, struct passwd *pw, int *flags, int *lineno)
448 {
449 static char line[LINE_MAX];
450 char *p;
451 int oflags;
452
453 if (fgets(line, sizeof(line), fp) == NULL)
454 return (0);
455 (*lineno)++;
456
457 /*
458 * ``... if I swallow anything evil, put your fingers down my
459 * throat...''
460 * -- The Who
461 */
462 if ((p = strchr(line, '\n')) == NULL) {
463 warnx("line too long");
464 errno = EFTYPE; /* XXX */
465 error(pname);
466 }
467 *p = '\0';
468 if (strcmp(line, "+") == 0)
469 strcpy(line, "+:::::::::"); /* pw_scan() can't handle "+" */
470 oflags = 0;
471 if (!pw_scan(line, pw, &oflags)) {
472 warnx("at line #%d", *lineno);
473 errno = EFTYPE; /* XXX */
474 error(pname);
475 }
476 *flags = oflags;
477
478 return (1);
479 }
480
481 void
482 install(const char *from, const char *to)
483 {
484 char buf[MAXPATHLEN];
485 int sverrno;
486
487 snprintf(buf, sizeof(buf), "%s%s", prefix, to);
488 if (rename(from, buf)) {
489 sverrno = errno;
490 (void)snprintf(buf, sizeof(buf), "%s to %s", from, buf);
491 errno = sverrno;
492 error(buf);
493 }
494 }
495
496 void
497 rm(const char *victim)
498 {
499
500 if (unlink(victim) < 0)
501 warn("unlink(%s)", victim);
502 }
503
504 void
505 cp(const char *from, const char *to, mode_t mode)
506 {
507 static char buf[MAXBSIZE];
508 int from_fd, rcount, to_fd, wcount, sverrno;
509
510 if ((from_fd = open(from, O_RDONLY, 0)) < 0)
511 error(from);
512 if ((to_fd = open(to, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0)
513 error(to);
514 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
515 wcount = write(to_fd, buf, rcount);
516 if (rcount != wcount || wcount == -1) {
517 sverrno = errno;
518 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
519 errno = sverrno;
520 error(buf);
521 }
522 }
523
524 if (rcount < 0) {
525 sverrno = errno;
526 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
527 errno = sverrno;
528 error(buf);
529 }
530 }
531
532 void
533 wr_error(const char *str)
534 {
535 char errbuf[BUFSIZ];
536 int sverrno;
537
538 sverrno = errno;
539
540 (void)snprintf(errbuf, sizeof(errbuf),
541 "attempt to write %s failed", str);
542
543 errno = sverrno;
544 error(errbuf);
545 }
546
547 void
548 error(const char *str)
549 {
550
551 warn("%s", str);
552 bailout();
553 }
554
555 void
556 inconsistancy(void)
557 {
558
559 warnx("text files and databases are inconsistent");
560 warnx("re-build the databases without -u");
561 bailout();
562 }
563
564 void
565 bailout(void)
566 {
567
568 if ((clean & FILE_ORIG) != 0)
569 rm(oldpwdfile);
570 if ((clean & FILE_SECURE) != 0)
571 rm(pwd_Sdb_tmp);
572 if ((clean & FILE_INSECURE) != 0)
573 rm(pwd_db_tmp);
574
575 exit(EXIT_FAILURE);
576 }
577
578 /*
579 * Write entries to a database for a single user.
580 *
581 * The databases actually contain three copies of the original data. Each
582 * password file entry is converted into a rough approximation of a ``struct
583 * passwd'', with the strings placed inline. This object is then stored as
584 * the data for three separate keys. The first key * is the pw_name field
585 * prepended by the _PW_KEYBYNAME character. The second key is the pw_uid
586 * field prepended by the _PW_KEYBYUID character. The third key is the line
587 * number in the original file prepended by the _PW_KEYBYNUM character.
588 * (The special characters are prepended to ensure that the keys do not
589 * collide.)
590 */
591 #define COMPACT(e) for (t = e; (*p++ = *t++) != '\0';)
592
593 void
594 putdbents(DB *dp, struct passwd *pw, const char *passwd, int flags,
595 const char *fn, int lineno, int dbflg, int uid_dbflg)
596 {
597 struct passwd pwd;
598 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024], *p;
599 DBT data, key;
600 const char *t;
601 u_int32_t x;
602 int len;
603
604 memcpy(&pwd, pw, sizeof(pwd));
605 data.data = (u_char *)buf;
606 key.data = (u_char *)tbuf;
607
608 if (lorder != BYTE_ORDER) {
609 M_32_SWAP(pwd.pw_uid);
610 M_32_SWAP(pwd.pw_gid);
611 M_32_SWAP(pwd.pw_change);
612 M_32_SWAP(pwd.pw_expire);
613 }
614
615 /* Create insecure data. */
616 p = buf;
617 COMPACT(pwd.pw_name);
618 COMPACT(passwd);
619 memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
620 p += sizeof(pwd.pw_uid);
621 memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
622 p += sizeof(pwd.pw_gid);
623 memmove(p, &pwd.pw_change, sizeof(pwd.pw_change));
624 p += sizeof(pwd.pw_change);
625 COMPACT(pwd.pw_class);
626 COMPACT(pwd.pw_gecos);
627 COMPACT(pwd.pw_dir);
628 COMPACT(pwd.pw_shell);
629 memmove(p, &pwd.pw_expire, sizeof(pwd.pw_expire));
630 p += sizeof(pwd.pw_expire);
631 x = flags;
632 if (lorder != BYTE_ORDER)
633 M_32_SWAP(x);
634 memmove(p, &x, sizeof(x));
635 p += sizeof(flags);
636 data.size = p - buf;
637
638 /* Store insecure by name. */
639 tbuf[0] = _PW_KEYBYNAME;
640 len = strlen(pwd.pw_name);
641 memmove(tbuf + 1, pwd.pw_name, len);
642 key.size = len + 1;
643 if ((*dp->put)(dp, &key, &data, dbflg) == -1)
644 wr_error(fn);
645
646 /* Store insecure by number. */
647 tbuf[0] = _PW_KEYBYNUM;
648 x = lineno;
649 if (lorder != BYTE_ORDER)
650 M_32_SWAP(x);
651 memmove(tbuf + 1, &x, sizeof(x));
652 key.size = sizeof(x) + 1;
653 if ((*dp->put)(dp, &key, &data, dbflg) == -1)
654 wr_error(fn);
655
656 /* Store insecure by uid. */
657 tbuf[0] = _PW_KEYBYUID;
658 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
659 key.size = sizeof(pwd.pw_uid) + 1;
660 if ((*dp->put)(dp, &key, &data, uid_dbflg) == -1)
661 wr_error(fn);
662 }
663
664 int
665 deldbent(DB *dp, const char *fn, int type, void *keyp)
666 {
667 char tbuf[1024];
668 DBT key;
669 u_int32_t x;
670 int len, rv;
671
672 key.data = (u_char *)tbuf;
673
674 switch (tbuf[0] = type) {
675 case _PW_KEYBYNAME:
676 len = strlen((char *)keyp);
677 memcpy(tbuf + 1, keyp, len);
678 key.size = len + 1;
679 break;
680
681 case _PW_KEYBYNUM:
682 case _PW_KEYBYUID:
683 x = *(int *)keyp;
684 if (lorder != BYTE_ORDER)
685 M_32_SWAP(x);
686 memmove(tbuf + 1, &x, sizeof(x));
687 key.size = sizeof(x) + 1;
688 break;
689 }
690
691 if ((rv = (*dp->del)(dp, &key, 0)) == -1)
692 wr_error(fn);
693 return (rv);
694 }
695
696 int
697 getdbent(DB *dp, const char *fn, int type, void *keyp, struct passwd **tpwd)
698 {
699 static char buf[MAX(MAXPATHLEN, LINE_MAX * 2)];
700 static struct passwd pwd;
701 char tbuf[1024], *p;
702 DBT key, data;
703 u_int32_t x;
704 int len, rv;
705
706 data.data = (u_char *)buf;
707 data.size = sizeof(buf);
708 key.data = (u_char *)tbuf;
709
710 switch (tbuf[0] = type) {
711 case _PW_KEYBYNAME:
712 len = strlen((char *)keyp);
713 memcpy(tbuf + 1, keyp, len);
714 key.size = len + 1;
715 break;
716
717 case _PW_KEYBYNUM:
718 case _PW_KEYBYUID:
719 x = *(int *)keyp;
720 if (lorder != BYTE_ORDER)
721 M_32_SWAP(x);
722 memmove(tbuf + 1, &x, sizeof(x));
723 key.size = sizeof(x) + 1;
724 break;
725 }
726
727 if ((rv = (*dp->get)(dp, &key, &data, 0)) == 1)
728 return (rv);
729 if (rv == -1)
730 error(pwd_Sdb_tmp);
731
732 p = (char *)data.data;
733
734 pwd.pw_name = p;
735 while (*p++ != '\0')
736 ;
737 pwd.pw_passwd = p;
738 while (*p++ != '\0')
739 ;
740
741 memcpy(&pwd.pw_uid, p, sizeof(pwd.pw_uid));
742 p += sizeof(pwd.pw_uid);
743 memcpy(&pwd.pw_gid, p, sizeof(pwd.pw_gid));
744 p += sizeof(pwd.pw_gid);
745 memcpy(&pwd.pw_change, p, sizeof(pwd.pw_change));
746 p += sizeof(pwd.pw_change);
747
748 pwd.pw_class = p;
749 while (*p++ != '\0')
750 ;
751 pwd.pw_gecos = p;
752 while (*p++ != '\0')
753 ;
754 pwd.pw_dir = p;
755 while (*p++ != '\0')
756 ;
757 pwd.pw_shell = p;
758 while (*p++ != '\0')
759 ;
760
761 memcpy(&pwd.pw_expire, p, sizeof(pwd.pw_expire));
762 p += sizeof(pwd.pw_expire);
763
764 if (lorder != BYTE_ORDER) {
765 M_32_SWAP(pwd.pw_uid);
766 M_32_SWAP(pwd.pw_gid);
767 M_32_SWAP(pwd.pw_change);
768 M_32_SWAP(pwd.pw_expire);
769 }
770
771 *tpwd = &pwd;
772 return (0);
773 }
774
775 void
776 putyptoken(DB *dp, const char *fn)
777 {
778 DBT data, key;
779
780 key.data = (u_char *)__yp_token;
781 key.size = strlen(__yp_token);
782 data.data = (u_char *)NULL;
783 data.size = 0;
784
785 if ((*dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
786 wr_error(fn);
787 }
788
789 void
790 usage(void)
791 {
792
793 (void)fprintf(stderr,
794 "usage: pwd_mkdb [-BLps] [-d directory] [-u user] file\n");
795 exit(EXIT_FAILURE);
796 }
797