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