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