]>
Commit | Line | Data |
---|---|---|
c3a08f59 A |
1 | /* $OpenBSD: pwd_mkdb.c,v 1.36 2003/06/08 21:14:55 millert Exp $ */ |
2 | ||
1815bff5 A |
3 | /*- |
4 | * Copyright (c) 1991, 1993, 1994 | |
5 | * The Regents of the University of California. All rights reserved. | |
c3a08f59 A |
6 | * Portions Copyright (c) 1994, Jason Downs. All rights reserved. |
7 | * Portions Copyright (c) 1998, Todd C. Miller. All rights reserved. | |
1815bff5 A |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
c3a08f59 | 17 | * 3. Neither the name of the University nor the names of its contributors |
1815bff5 A |
18 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34d340d7 | 34 | #include <sys/cdefs.h> |
1815bff5 | 35 | #ifndef lint |
34d340d7 | 36 | __unused static const char copyright[] = |
1815bff5 A |
37 | "@(#) Copyright (c) 1991, 1993, 1994\n\ |
38 | The Regents of the University of California. All rights reserved.\n"; | |
39 | #endif /* not lint */ | |
40 | ||
41 | #ifndef lint | |
c3a08f59 A |
42 | #if 0 |
43 | static const char sccsid[] = "from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94"; | |
44 | #else | |
34d340d7 | 45 | __unused static const char rcsid[] = "$OpenBSD: pwd_mkdb.c,v 1.36 2003/06/08 21:14:55 millert Exp $"; |
c3a08f59 | 46 | #endif |
1815bff5 A |
47 | #endif /* not lint */ |
48 | ||
49 | #include <sys/param.h> | |
50 | #include <sys/stat.h> | |
51 | ||
52 | #include <db.h> | |
53 | #include <err.h> | |
54 | #include <errno.h> | |
55 | #include <fcntl.h> | |
c3a08f59 | 56 | #include <grp.h> |
1815bff5 A |
57 | #include <limits.h> |
58 | #include <pwd.h> | |
59 | #include <signal.h> | |
60 | #include <stdio.h> | |
61 | #include <stdlib.h> | |
62 | #include <string.h> | |
63 | #include <unistd.h> | |
c3a08f59 A |
64 | #include <util.h> |
65 | #include <sys/param.h> | |
34d340d7 | 66 | #include "pw_scan.h" |
1815bff5 A |
67 | |
68 | #define INSECURE 1 | |
69 | #define SECURE 2 | |
70 | #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) | |
71 | #define PERM_SECURE (S_IRUSR|S_IWUSR) | |
72 | ||
c3a08f59 A |
73 | #define FILE_SECURE 0x01 |
74 | #define FILE_INSECURE 0x02 | |
75 | #define FILE_ORIG 0x04 | |
76 | ||
77 | #define SHADOW_GROUP "wheel" | |
78 | ||
1815bff5 A |
79 | HASHINFO openinfo = { |
80 | 4096, /* bsize */ | |
81 | 32, /* ffactor */ | |
82 | 256, /* nelem */ | |
83 | 2048 * 1024, /* cachesize */ | |
84 | NULL, /* hash() */ | |
85 | 0 /* lorder */ | |
86 | }; | |
87 | ||
1815bff5 | 88 | static char *pname; /* password file name */ |
c3a08f59 A |
89 | static char *basedir; /* dir holding master.passwd */ |
90 | static int clean; /* what to remove on cleanup */ | |
91 | static int hasyp; /* are we running YP? */ | |
92 | ||
93 | void cleanup(void); | |
94 | void error(char *); | |
95 | void errorx(char *); | |
96 | void cp(char *, char *, mode_t); | |
97 | void mv(char *, char *); | |
98 | int scan(FILE *, struct passwd *, int *); | |
99 | void usage(void); | |
100 | char *changedir(char *path, char *dir); | |
101 | void db_store(FILE *, FILE *, DB *, DB *,struct passwd *, int, char *, uid_t); | |
1815bff5 A |
102 | |
103 | int | |
c3a08f59 | 104 | main(int argc, char **argv) |
1815bff5 A |
105 | { |
106 | DB *dp, *edp; | |
107 | DBT data, key; | |
c3a08f59 A |
108 | FILE *fp, *oldfp = NULL; |
109 | struct stat st; | |
110 | struct passwd pwd; | |
111 | struct group *grp; | |
1815bff5 | 112 | sigset_t set; |
c3a08f59 A |
113 | uid_t olduid; |
114 | gid_t shadow; | |
115 | int ch, tfd, makeold, secureonly, flags, checkonly; | |
116 | char *username, buf[MAX(MAXPATHLEN, LINE_MAX * 2)]; | |
117 | ||
118 | flags = checkonly = makeold = secureonly = 0; | |
119 | username = NULL; | |
120 | while ((ch = getopt(argc, argv, "cd:psu:v")) != -1) | |
121 | switch (ch) { | |
122 | case 'c': /* verify only */ | |
123 | checkonly = 1; | |
124 | break; | |
125 | case 'd': | |
126 | basedir = optarg; | |
127 | if (strlen(basedir) > MAXPATHLEN - 40) | |
128 | errx(1, "basedir too long"); | |
129 | break; | |
1815bff5 A |
130 | case 'p': /* create V7 "file.orig" */ |
131 | makeold = 1; | |
132 | break; | |
c3a08f59 A |
133 | case 's': /* only update spwd.db */ |
134 | secureonly = 1; | |
135 | break; | |
136 | case 'u': /* only update this record */ | |
137 | username = optarg; | |
138 | if (strlen(username) > _PW_NAME_LEN) | |
139 | errx(1, "username too long"); | |
140 | break; | |
1815bff5 A |
141 | case 'v': /* backward compatible */ |
142 | break; | |
143 | case '?': | |
144 | default: | |
145 | usage(); | |
146 | } | |
147 | argc -= optind; | |
148 | argv += optind; | |
149 | ||
c3a08f59 A |
150 | if (argc != 1 || (makeold && secureonly) || |
151 | (username && (*username == '+' || *username == '-'))) | |
1815bff5 | 152 | usage(); |
c3a08f59 A |
153 | |
154 | if ((grp = getgrnam(SHADOW_GROUP)) == NULL) | |
155 | errx(1, "cannot find `%s' in the group database, aborting", | |
156 | SHADOW_GROUP); | |
157 | shadow = grp->gr_gid; | |
1815bff5 A |
158 | |
159 | /* | |
160 | * This could be changed to allow the user to interrupt. | |
161 | * Probably not worth the effort. | |
162 | */ | |
163 | sigemptyset(&set); | |
164 | sigaddset(&set, SIGTSTP); | |
165 | sigaddset(&set, SIGHUP); | |
166 | sigaddset(&set, SIGINT); | |
167 | sigaddset(&set, SIGQUIT); | |
168 | sigaddset(&set, SIGTERM); | |
169 | (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL); | |
170 | ||
171 | /* We don't care what the user wants. */ | |
172 | (void)umask(0); | |
173 | ||
c3a08f59 A |
174 | if (**argv != '/' && basedir == NULL) |
175 | errx(1, "%s must be specified as an absolute path", *argv); | |
176 | ||
177 | if ((pname = strdup(changedir(*argv, basedir))) == NULL) | |
178 | err(1, NULL); | |
1815bff5 A |
179 | /* Open the original password file */ |
180 | if (!(fp = fopen(pname, "r"))) | |
181 | error(pname); | |
182 | ||
c3a08f59 A |
183 | /* Check only if password database is valid */ |
184 | if (checkonly) { | |
185 | u_int cnt; | |
186 | ||
187 | for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) | |
188 | ; | |
189 | exit(0); | |
190 | } | |
191 | ||
192 | if (fstat(fileno(fp), &st) == -1) | |
193 | error(pname); | |
194 | ||
195 | /* Tweak openinfo values for large passwd files. */ | |
196 | if (st.st_size > (off_t)100*1024) | |
197 | openinfo.cachesize = MIN(st.st_size * 20, (off_t)12*1024*1024); | |
198 | if (st.st_size / 128 > openinfo.nelem) | |
199 | openinfo.nelem = st.st_size / 128; | |
200 | ||
201 | /* If only updating a single record, stash the old uid */ | |
202 | if (username) { | |
203 | dp = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL); | |
204 | if (dp == NULL) | |
205 | error(_PATH_MP_DB); | |
206 | buf[0] = _PW_KEYBYNAME; | |
207 | strlcpy(buf + 1, username, sizeof(buf) - 1); | |
208 | key.data = (u_char *)buf; | |
209 | key.size = strlen(buf + 1) + 1; | |
210 | if ((dp->get)(dp, &key, &data, 0) == 0) { | |
211 | char *p = (char *)data.data; | |
212 | /* Skip to uid field */ | |
213 | while (*p++ != '\0') | |
214 | ; | |
215 | while (*p++ != '\0') | |
216 | ; | |
217 | memcpy(&olduid, p, sizeof(olduid)); | |
218 | } else | |
219 | olduid = UID_MAX; | |
220 | (dp->close)(dp); | |
221 | } | |
222 | ||
223 | /* Open the temporary encrypted password database. */ | |
224 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
225 | changedir(_PATH_SMP_DB, basedir)); | |
226 | if (username) { | |
227 | cp(changedir(_PATH_SMP_DB, basedir), buf, PERM_SECURE); | |
228 | edp = dbopen(buf, | |
229 | O_RDWR, PERM_SECURE, DB_HASH, &openinfo); | |
230 | } else { | |
231 | edp = dbopen(buf, | |
232 | O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo); | |
233 | } | |
234 | if (!edp) | |
1815bff5 | 235 | error(buf); |
c3a08f59 A |
236 | if (fchown(edp->fd(edp), (uid_t)-1, shadow) != 0) |
237 | warn("%s: unable to set group to %s", _PATH_SMP_DB, | |
238 | SHADOW_GROUP); | |
239 | else if (fchmod(edp->fd(edp), PERM_SECURE|S_IRGRP) != 0) | |
240 | warn("%s: unable to make group readable", _PATH_SMP_DB); | |
241 | clean |= FILE_SECURE; | |
242 | ||
243 | /* Open the temporary insecure password database. */ | |
244 | if (!secureonly) { | |
245 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
246 | changedir(_PATH_MP_DB, basedir)); | |
247 | if (username) { | |
248 | cp(changedir(_PATH_MP_DB, basedir), buf, PERM_INSECURE); | |
249 | dp = dbopen(buf, O_RDWR, PERM_INSECURE, DB_HASH, | |
250 | &openinfo); | |
251 | } else { | |
252 | dp = dbopen(buf, O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, | |
253 | DB_HASH, &openinfo); | |
254 | } | |
255 | if (dp == NULL) | |
256 | error(buf); | |
257 | clean |= FILE_INSECURE; | |
258 | } else | |
259 | dp = NULL; | |
1815bff5 A |
260 | |
261 | /* | |
262 | * Open file for old password file. Minor trickiness -- don't want to | |
263 | * chance the file already existing, since someone (stupidly) might | |
264 | * still be using this for permission checking. So, open it first and | |
265 | * fdopen the resulting fd. The resulting file should be readable by | |
266 | * everyone. | |
267 | */ | |
268 | if (makeold) { | |
269 | (void)snprintf(buf, sizeof(buf), "%s.orig", pname); | |
270 | if ((tfd = open(buf, | |
271 | O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0) | |
272 | error(buf); | |
273 | if ((oldfp = fdopen(tfd, "w")) == NULL) | |
274 | error(buf); | |
c3a08f59 | 275 | clean |= FILE_ORIG; |
1815bff5 A |
276 | } |
277 | ||
278 | /* | |
279 | * The databases actually contain three copies of the original data. | |
280 | * Each password file entry is converted into a rough approximation | |
281 | * of a ``struct passwd'', with the strings placed inline. This | |
282 | * object is then stored as the data for three separate keys. The | |
283 | * first key * is the pw_name field prepended by the _PW_KEYBYNAME | |
284 | * character. The second key is the pw_uid field prepended by the | |
285 | * _PW_KEYBYUID character. The third key is the line number in the | |
286 | * original file prepended by the _PW_KEYBYNUM character. (The special | |
287 | * characters are prepended to ensure that the keys do not collide.) | |
c3a08f59 A |
288 | * |
289 | * If we see something go by that looks like YP, we save a special | |
290 | * pointer record, which if YP is enabled in the C lib, will speed | |
291 | * things up. | |
1815bff5 | 292 | */ |
1815bff5 | 293 | |
c3a08f59 A |
294 | /* |
295 | * Write the .db files. | |
296 | * We do this three times, one per key type (for getpw{nam,uid,ent}). | |
297 | * The first time through we also check for YP, issue warnings | |
298 | * and save the V7 format passwd file if necessary. | |
299 | */ | |
300 | db_store(fp, oldfp, edp, dp, &pwd, _PW_KEYBYNAME, username, olduid); | |
301 | db_store(fp, oldfp, edp, dp, &pwd, _PW_KEYBYUID, username, olduid); | |
302 | db_store(fp, oldfp, edp, dp, &pwd, _PW_KEYBYNUM, username, olduid); | |
1815bff5 | 303 | |
c3a08f59 A |
304 | /* Store YP token, if needed. */ |
305 | if (hasyp && !username) { | |
306 | key.data = (u_char *)_PW_YPTOKEN; | |
307 | key.size = strlen(_PW_YPTOKEN); | |
308 | data.data = (u_char *)NULL; | |
309 | data.size = 0; | |
1815bff5 | 310 | |
1815bff5 A |
311 | if ((edp->put)(edp, &key, &data, R_NOOVERWRITE) == -1) |
312 | error("put"); | |
313 | ||
c3a08f59 | 314 | if (dp && (dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1) |
1815bff5 A |
315 | error("put"); |
316 | } | |
317 | ||
c3a08f59 A |
318 | if ((edp->close)(edp)) |
319 | error("close edp"); | |
320 | if (dp && (dp->close)(dp)) | |
321 | error("close dp"); | |
322 | if (makeold) { | |
323 | if (fclose(oldfp) == EOF) | |
324 | error("close old"); | |
325 | } | |
1815bff5 A |
326 | |
327 | /* Set master.passwd permissions, in case caller forgot. */ | |
328 | (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR); | |
c3a08f59 A |
329 | if (fclose(fp) != 0) |
330 | error("fclose"); | |
1815bff5 A |
331 | |
332 | /* Install as the real password files. */ | |
c3a08f59 A |
333 | if (!secureonly) { |
334 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
335 | changedir(_PATH_MP_DB, basedir)); | |
336 | mv(buf, changedir(_PATH_MP_DB, basedir)); | |
337 | } | |
338 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
339 | changedir(_PATH_SMP_DB, basedir)); | |
340 | mv(buf, changedir(_PATH_SMP_DB, basedir)); | |
1815bff5 A |
341 | if (makeold) { |
342 | (void)snprintf(buf, sizeof(buf), "%s.orig", pname); | |
c3a08f59 | 343 | mv(buf, changedir(_PATH_PASSWD, basedir)); |
1815bff5 | 344 | } |
c3a08f59 | 345 | |
1815bff5 A |
346 | /* |
347 | * Move the master password LAST -- chpass(1), passwd(1) and vipw(8) | |
348 | * all use flock(2) on it to block other incarnations of themselves. | |
349 | * The rename means that everything is unlocked, as the original file | |
350 | * can no longer be accessed. | |
351 | */ | |
c3a08f59 | 352 | mv(pname, changedir(_PATH_MASTERPASSWD, basedir)); |
1815bff5 A |
353 | exit(0); |
354 | } | |
355 | ||
356 | int | |
c3a08f59 | 357 | scan(FILE *fp, struct passwd *pw, int *flags) |
1815bff5 A |
358 | { |
359 | static int lcnt; | |
360 | static char line[LINE_MAX]; | |
361 | char *p; | |
362 | ||
c3a08f59 | 363 | if (fgets(line, sizeof(line), fp) == NULL) |
1815bff5 | 364 | return (0); |
1815bff5 A |
365 | ++lcnt; |
366 | /* | |
367 | * ``... if I swallow anything evil, put your fingers down my | |
368 | * throat...'' | |
369 | * -- The Who | |
370 | */ | |
c3a08f59 A |
371 | p = line; |
372 | if (*p != '\0' && *(p += strlen(line) - 1) != '\n') { | |
1815bff5 A |
373 | warnx("line too long"); |
374 | goto fmt; | |
1815bff5 A |
375 | } |
376 | *p = '\0'; | |
c3a08f59 A |
377 | *flags = 0; |
378 | if (!pw_scan(line, pw, flags)) { | |
1815bff5 A |
379 | warnx("at line #%d", lcnt); |
380 | fmt: errno = EFTYPE; /* XXX */ | |
381 | error(pname); | |
382 | } | |
383 | ||
384 | return (1); | |
385 | } | |
386 | ||
c3a08f59 | 387 | void |
8459d725 | 388 | cp(char *from, char *to, mode_t mode) |
c3a08f59 A |
389 | { |
390 | static char buf[MAXBSIZE]; | |
391 | int from_fd, rcount, to_fd, wcount; | |
392 | ||
393 | if ((from_fd = open(from, O_RDONLY, 0)) < 0) | |
394 | error(from); | |
395 | if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) | |
396 | error(to); | |
397 | while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { | |
398 | wcount = write(to_fd, buf, rcount); | |
399 | if (rcount != wcount || wcount == -1) { | |
400 | int sverrno = errno; | |
401 | ||
402 | (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); | |
403 | errno = sverrno; | |
404 | error(buf); | |
405 | } | |
406 | } | |
407 | if (rcount < 0) { | |
408 | int sverrno = errno; | |
409 | ||
410 | (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); | |
411 | errno = sverrno; | |
412 | error(buf); | |
413 | } | |
414 | } | |
415 | ||
1815bff5 A |
416 | void |
417 | mv(from, to) | |
418 | char *from, *to; | |
419 | { | |
c3a08f59 | 420 | char buf[MAXPATHLEN * 2]; |
1815bff5 A |
421 | |
422 | if (rename(from, to)) { | |
423 | int sverrno = errno; | |
c3a08f59 | 424 | |
1815bff5 A |
425 | (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); |
426 | errno = sverrno; | |
427 | error(buf); | |
428 | } | |
429 | } | |
430 | ||
431 | void | |
432 | error(name) | |
433 | char *name; | |
434 | { | |
435 | ||
c3a08f59 A |
436 | warn("%s", name); |
437 | cleanup(); | |
438 | exit(1); | |
439 | } | |
440 | ||
441 | void | |
442 | errorx(name) | |
443 | char *name; | |
444 | { | |
445 | ||
446 | warnx("%s", name); | |
1815bff5 A |
447 | cleanup(); |
448 | exit(1); | |
449 | } | |
450 | ||
451 | void | |
452 | cleanup() | |
453 | { | |
454 | char buf[MAXPATHLEN]; | |
455 | ||
c3a08f59 | 456 | if (clean & FILE_ORIG) { |
1815bff5 A |
457 | (void)snprintf(buf, sizeof(buf), "%s.orig", pname); |
458 | (void)unlink(buf); | |
c3a08f59 A |
459 | } |
460 | if (clean & FILE_SECURE) { | |
461 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
462 | changedir(_PATH_SMP_DB, basedir)); | |
1815bff5 | 463 | (void)unlink(buf); |
c3a08f59 A |
464 | } |
465 | if (clean & FILE_INSECURE) { | |
466 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
467 | changedir(_PATH_MP_DB, basedir)); | |
1815bff5 A |
468 | (void)unlink(buf); |
469 | } | |
470 | } | |
471 | ||
472 | void | |
c3a08f59 | 473 | usage(void) |
1815bff5 A |
474 | { |
475 | ||
c3a08f59 A |
476 | (void)fprintf(stderr, |
477 | "usage: pwd_mkdb [-c] [-p | -s] [-d basedir] [-u username] file\n"); | |
1815bff5 A |
478 | exit(1); |
479 | } | |
c3a08f59 A |
480 | |
481 | char * | |
482 | changedir(char *path, char *dir) | |
483 | { | |
484 | static char fixed[MAXPATHLEN]; | |
485 | char *p; | |
486 | ||
487 | if (!dir) | |
488 | return (path); | |
489 | ||
490 | if ((p = strrchr(path, '/')) != NULL) | |
491 | path = p + 1; | |
492 | snprintf(fixed, sizeof(fixed), "%s/%s", dir, path); | |
493 | return (fixed); | |
494 | } | |
495 | ||
496 | void | |
497 | db_store(FILE *fp, FILE *oldfp, DB *edp, DB *dp, struct passwd *pw, | |
498 | int keytype, char *username, uid_t olduid) | |
499 | { | |
500 | int flags = 0; | |
501 | int dbmode, found = 0; | |
502 | u_int cnt; | |
503 | char *p, *t, buf[LINE_MAX * 2], tbuf[1024]; | |
504 | DBT data, key; | |
505 | size_t len; | |
506 | static int firsttime = 1; | |
507 | ||
508 | /* If given a username just add that record to the existing db. */ | |
509 | dbmode = username ? 0 : R_NOOVERWRITE; | |
510 | ||
511 | rewind(fp); | |
512 | data.data = (u_char *)buf; | |
513 | key.data = (u_char *)tbuf; | |
514 | for (cnt = 1; scan(fp, pw, &flags); ++cnt) { | |
515 | ||
83f6dbe8 A |
516 | #ifdef __APPLE__ |
517 | if (pw->pw_name == NULL) | |
518 | continue; | |
519 | #endif | |
520 | ||
c3a08f59 A |
521 | if (firsttime) { |
522 | /* Look like YP? */ | |
523 | if ((pw->pw_name[0] == '+') || (pw->pw_name[0] == '-')) | |
524 | hasyp++; | |
525 | ||
526 | /* Warn about potentially unsafe uid/gid overrides. */ | |
527 | if (pw->pw_name[0] == '+') { | |
528 | if (!(flags & _PASSWORD_NOUID) && !pw->pw_uid) | |
529 | warnx("line %d: superuser override in " | |
530 | "YP inclusion", cnt); | |
531 | if (!(flags & _PASSWORD_NOGID) && !pw->pw_gid) | |
532 | warnx("line %d: wheel override in " | |
533 | "YP inclusion", cnt); | |
534 | } | |
535 | ||
536 | /* Create V7 format password file entry. */ | |
537 | if (oldfp != NULL) | |
538 | if (fprintf(oldfp, "%s:*:%u:%u:%s:%s:%s\n", | |
539 | pw->pw_name, pw->pw_uid, pw->pw_gid, | |
540 | pw->pw_gecos, pw->pw_dir, pw->pw_shell) | |
541 | == EOF) | |
542 | error("write old"); | |
543 | } | |
544 | ||
545 | /* Are we updating a specific record? */ | |
546 | if (username) { | |
547 | if (strcmp(username, pw->pw_name) != 0) | |
548 | continue; | |
549 | found = 1; | |
550 | /* If the uid changed, remove the old record by uid. */ | |
551 | if (olduid != UID_MAX && olduid != pw->pw_uid) { | |
552 | tbuf[0] = _PW_KEYBYUID; | |
553 | memcpy(tbuf + 1, &olduid, sizeof(olduid)); | |
554 | key.size = sizeof(olduid) + 1; | |
555 | (edp->del)(edp, &key, 0); | |
556 | if (dp) | |
557 | (dp->del)(dp, &key, 0); | |
558 | } | |
559 | /* XXX - should check to see if line number changed. */ | |
560 | } | |
561 | ||
562 | /* Build the key. */ | |
563 | tbuf[0] = keytype; | |
564 | switch (keytype) { | |
565 | case _PW_KEYBYNUM: | |
566 | memmove(tbuf + 1, &cnt, sizeof(cnt)); | |
567 | key.size = sizeof(cnt) + 1; | |
568 | break; | |
569 | ||
570 | case _PW_KEYBYNAME: | |
571 | len = strlen(pw->pw_name); | |
572 | memmove(tbuf + 1, pw->pw_name, len); | |
573 | key.size = len + 1; | |
574 | break; | |
575 | ||
576 | case _PW_KEYBYUID: | |
577 | memmove(tbuf + 1, &pw->pw_uid, sizeof(pw->pw_uid)); | |
578 | key.size = sizeof(pw->pw_uid) + 1; | |
579 | break; | |
580 | } | |
581 | ||
582 | #define COMPACT(e) t = e; while ((*p++ = *t++)); | |
583 | /* Create the secure record. */ | |
584 | p = buf; | |
585 | COMPACT(pw->pw_name); | |
586 | COMPACT(pw->pw_passwd); | |
587 | memmove(p, &pw->pw_uid, sizeof(uid_t)); | |
588 | p += sizeof(uid_t); | |
589 | memmove(p, &pw->pw_gid, sizeof(gid_t)); | |
590 | p += sizeof(gid_t); | |
591 | memmove(p, &pw->pw_change, sizeof(time_t)); | |
592 | p += sizeof(time_t); | |
593 | COMPACT(pw->pw_class); | |
594 | COMPACT(pw->pw_gecos); | |
595 | COMPACT(pw->pw_dir); | |
596 | COMPACT(pw->pw_shell); | |
597 | memmove(p, &pw->pw_expire, sizeof(time_t)); | |
598 | p += sizeof(time_t); | |
599 | memmove(p, &flags, sizeof(int)); | |
600 | p += sizeof(int); | |
601 | data.size = p - buf; | |
602 | ||
603 | /* Write the secure record. */ | |
604 | if ((edp->put)(edp, &key, &data, dbmode) == -1) | |
605 | error("put"); | |
606 | ||
607 | if (dp == NULL) | |
608 | continue; | |
609 | ||
610 | /* Star out password to make insecure record. */ | |
611 | p = buf + strlen(pw->pw_name) + 1; /* skip pw_name */ | |
612 | len = strlen(pw->pw_passwd); | |
613 | memset(p, 0, len); /* zero pw_passwd */ | |
614 | t = p + len + 1; /* skip pw_passwd */ | |
615 | if (len != 0) | |
616 | *p++ = '*'; | |
617 | *p++ = '\0'; | |
618 | memmove(p, t, data.size - (t - buf)); | |
619 | data.size -= len - 1; | |
620 | ||
621 | /* Write the insecure record. */ | |
622 | if ((dp->put)(dp, &key, &data, dbmode) == -1) | |
623 | error("put"); | |
624 | } | |
625 | if (firsttime) { | |
626 | firsttime = 0; | |
627 | if (username && !found && olduid != UID_MAX) | |
628 | errorx("can't find user in master.passwd"); | |
629 | } | |
630 | } |