]>
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 A |
387 | void |
388 | cp(from, to, mode) | |
389 | char *from, *to; | |
390 | mode_t mode; | |
391 | { | |
392 | static char buf[MAXBSIZE]; | |
393 | int from_fd, rcount, to_fd, wcount; | |
394 | ||
395 | if ((from_fd = open(from, O_RDONLY, 0)) < 0) | |
396 | error(from); | |
397 | if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) | |
398 | error(to); | |
399 | while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { | |
400 | wcount = write(to_fd, buf, rcount); | |
401 | if (rcount != wcount || wcount == -1) { | |
402 | int sverrno = errno; | |
403 | ||
404 | (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); | |
405 | errno = sverrno; | |
406 | error(buf); | |
407 | } | |
408 | } | |
409 | if (rcount < 0) { | |
410 | int sverrno = errno; | |
411 | ||
412 | (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); | |
413 | errno = sverrno; | |
414 | error(buf); | |
415 | } | |
416 | } | |
417 | ||
1815bff5 A |
418 | void |
419 | mv(from, to) | |
420 | char *from, *to; | |
421 | { | |
c3a08f59 | 422 | char buf[MAXPATHLEN * 2]; |
1815bff5 A |
423 | |
424 | if (rename(from, to)) { | |
425 | int sverrno = errno; | |
c3a08f59 | 426 | |
1815bff5 A |
427 | (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); |
428 | errno = sverrno; | |
429 | error(buf); | |
430 | } | |
431 | } | |
432 | ||
433 | void | |
434 | error(name) | |
435 | char *name; | |
436 | { | |
437 | ||
c3a08f59 A |
438 | warn("%s", name); |
439 | cleanup(); | |
440 | exit(1); | |
441 | } | |
442 | ||
443 | void | |
444 | errorx(name) | |
445 | char *name; | |
446 | { | |
447 | ||
448 | warnx("%s", name); | |
1815bff5 A |
449 | cleanup(); |
450 | exit(1); | |
451 | } | |
452 | ||
453 | void | |
454 | cleanup() | |
455 | { | |
456 | char buf[MAXPATHLEN]; | |
457 | ||
c3a08f59 | 458 | if (clean & FILE_ORIG) { |
1815bff5 A |
459 | (void)snprintf(buf, sizeof(buf), "%s.orig", pname); |
460 | (void)unlink(buf); | |
c3a08f59 A |
461 | } |
462 | if (clean & FILE_SECURE) { | |
463 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
464 | changedir(_PATH_SMP_DB, basedir)); | |
1815bff5 | 465 | (void)unlink(buf); |
c3a08f59 A |
466 | } |
467 | if (clean & FILE_INSECURE) { | |
468 | (void)snprintf(buf, sizeof(buf), "%s.tmp", | |
469 | changedir(_PATH_MP_DB, basedir)); | |
1815bff5 A |
470 | (void)unlink(buf); |
471 | } | |
472 | } | |
473 | ||
474 | void | |
c3a08f59 | 475 | usage(void) |
1815bff5 A |
476 | { |
477 | ||
c3a08f59 A |
478 | (void)fprintf(stderr, |
479 | "usage: pwd_mkdb [-c] [-p | -s] [-d basedir] [-u username] file\n"); | |
1815bff5 A |
480 | exit(1); |
481 | } | |
c3a08f59 A |
482 | |
483 | char * | |
484 | changedir(char *path, char *dir) | |
485 | { | |
486 | static char fixed[MAXPATHLEN]; | |
487 | char *p; | |
488 | ||
489 | if (!dir) | |
490 | return (path); | |
491 | ||
492 | if ((p = strrchr(path, '/')) != NULL) | |
493 | path = p + 1; | |
494 | snprintf(fixed, sizeof(fixed), "%s/%s", dir, path); | |
495 | return (fixed); | |
496 | } | |
497 | ||
498 | void | |
499 | db_store(FILE *fp, FILE *oldfp, DB *edp, DB *dp, struct passwd *pw, | |
500 | int keytype, char *username, uid_t olduid) | |
501 | { | |
502 | int flags = 0; | |
503 | int dbmode, found = 0; | |
504 | u_int cnt; | |
505 | char *p, *t, buf[LINE_MAX * 2], tbuf[1024]; | |
506 | DBT data, key; | |
507 | size_t len; | |
508 | static int firsttime = 1; | |
509 | ||
510 | /* If given a username just add that record to the existing db. */ | |
511 | dbmode = username ? 0 : R_NOOVERWRITE; | |
512 | ||
513 | rewind(fp); | |
514 | data.data = (u_char *)buf; | |
515 | key.data = (u_char *)tbuf; | |
516 | for (cnt = 1; scan(fp, pw, &flags); ++cnt) { | |
517 | ||
83f6dbe8 A |
518 | #ifdef __APPLE__ |
519 | if (pw->pw_name == NULL) | |
520 | continue; | |
521 | #endif | |
522 | ||
c3a08f59 A |
523 | if (firsttime) { |
524 | /* Look like YP? */ | |
525 | if ((pw->pw_name[0] == '+') || (pw->pw_name[0] == '-')) | |
526 | hasyp++; | |
527 | ||
528 | /* Warn about potentially unsafe uid/gid overrides. */ | |
529 | if (pw->pw_name[0] == '+') { | |
530 | if (!(flags & _PASSWORD_NOUID) && !pw->pw_uid) | |
531 | warnx("line %d: superuser override in " | |
532 | "YP inclusion", cnt); | |
533 | if (!(flags & _PASSWORD_NOGID) && !pw->pw_gid) | |
534 | warnx("line %d: wheel override in " | |
535 | "YP inclusion", cnt); | |
536 | } | |
537 | ||
538 | /* Create V7 format password file entry. */ | |
539 | if (oldfp != NULL) | |
540 | if (fprintf(oldfp, "%s:*:%u:%u:%s:%s:%s\n", | |
541 | pw->pw_name, pw->pw_uid, pw->pw_gid, | |
542 | pw->pw_gecos, pw->pw_dir, pw->pw_shell) | |
543 | == EOF) | |
544 | error("write old"); | |
545 | } | |
546 | ||
547 | /* Are we updating a specific record? */ | |
548 | if (username) { | |
549 | if (strcmp(username, pw->pw_name) != 0) | |
550 | continue; | |
551 | found = 1; | |
552 | /* If the uid changed, remove the old record by uid. */ | |
553 | if (olduid != UID_MAX && olduid != pw->pw_uid) { | |
554 | tbuf[0] = _PW_KEYBYUID; | |
555 | memcpy(tbuf + 1, &olduid, sizeof(olduid)); | |
556 | key.size = sizeof(olduid) + 1; | |
557 | (edp->del)(edp, &key, 0); | |
558 | if (dp) | |
559 | (dp->del)(dp, &key, 0); | |
560 | } | |
561 | /* XXX - should check to see if line number changed. */ | |
562 | } | |
563 | ||
564 | /* Build the key. */ | |
565 | tbuf[0] = keytype; | |
566 | switch (keytype) { | |
567 | case _PW_KEYBYNUM: | |
568 | memmove(tbuf + 1, &cnt, sizeof(cnt)); | |
569 | key.size = sizeof(cnt) + 1; | |
570 | break; | |
571 | ||
572 | case _PW_KEYBYNAME: | |
573 | len = strlen(pw->pw_name); | |
574 | memmove(tbuf + 1, pw->pw_name, len); | |
575 | key.size = len + 1; | |
576 | break; | |
577 | ||
578 | case _PW_KEYBYUID: | |
579 | memmove(tbuf + 1, &pw->pw_uid, sizeof(pw->pw_uid)); | |
580 | key.size = sizeof(pw->pw_uid) + 1; | |
581 | break; | |
582 | } | |
583 | ||
584 | #define COMPACT(e) t = e; while ((*p++ = *t++)); | |
585 | /* Create the secure record. */ | |
586 | p = buf; | |
587 | COMPACT(pw->pw_name); | |
588 | COMPACT(pw->pw_passwd); | |
589 | memmove(p, &pw->pw_uid, sizeof(uid_t)); | |
590 | p += sizeof(uid_t); | |
591 | memmove(p, &pw->pw_gid, sizeof(gid_t)); | |
592 | p += sizeof(gid_t); | |
593 | memmove(p, &pw->pw_change, sizeof(time_t)); | |
594 | p += sizeof(time_t); | |
595 | COMPACT(pw->pw_class); | |
596 | COMPACT(pw->pw_gecos); | |
597 | COMPACT(pw->pw_dir); | |
598 | COMPACT(pw->pw_shell); | |
599 | memmove(p, &pw->pw_expire, sizeof(time_t)); | |
600 | p += sizeof(time_t); | |
601 | memmove(p, &flags, sizeof(int)); | |
602 | p += sizeof(int); | |
603 | data.size = p - buf; | |
604 | ||
605 | /* Write the secure record. */ | |
606 | if ((edp->put)(edp, &key, &data, dbmode) == -1) | |
607 | error("put"); | |
608 | ||
609 | if (dp == NULL) | |
610 | continue; | |
611 | ||
612 | /* Star out password to make insecure record. */ | |
613 | p = buf + strlen(pw->pw_name) + 1; /* skip pw_name */ | |
614 | len = strlen(pw->pw_passwd); | |
615 | memset(p, 0, len); /* zero pw_passwd */ | |
616 | t = p + len + 1; /* skip pw_passwd */ | |
617 | if (len != 0) | |
618 | *p++ = '*'; | |
619 | *p++ = '\0'; | |
620 | memmove(p, t, data.size - (t - buf)); | |
621 | data.size -= len - 1; | |
622 | ||
623 | /* Write the insecure record. */ | |
624 | if ((dp->put)(dp, &key, &data, dbmode) == -1) | |
625 | error("put"); | |
626 | } | |
627 | if (firsttime) { | |
628 | firsttime = 0; | |
629 | if (username && !found && olduid != UID_MAX) | |
630 | errorx("can't find user in master.passwd"); | |
631 | } | |
632 | } |