1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if 0 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1991, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94"; 41 #endif /* not lint */ 42 #endif 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/endian.h> 49 #include <sys/stat.h> 50 #include <arpa/inet.h> 51 52 #include <db.h> 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <libgen.h> 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> 64 65 #include "pw_scan.h" 66 67 #define INSECURE 1 68 #define SECURE 2 69 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 70 #define PERM_SECURE (S_IRUSR|S_IWUSR) 71 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3) 72 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4) 73 74 static HASHINFO openinfo = { 75 4096, /* bsize */ 76 32, /* ffactor */ 77 256, /* nelem */ 78 2048 * 1024, /* cachesize */ 79 NULL, /* hash() */ 80 BYTE_ORDER /* lorder */ 81 }; 82 83 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean; 84 static struct passwd pwd; /* password structure */ 85 static char *pname; /* password file name */ 86 static char prefix[MAXPATHLEN]; 87 88 static int is_comment; /* flag for comments */ 89 static char line[LINE_MAX]; 90 91 void cleanup(void); 92 void error(const char *); 93 void cp(char *, char *, mode_t mode); 94 void mv(char *, char *); 95 int scan(FILE *, struct passwd *); 96 static void usage(void); 97 98 int 99 main(int argc, char *argv[]) 100 { 101 static char verskey[] = _PWD_VERSION_KEY; 102 char version = _PWD_CURRENT_VERSION; 103 DB *dp, *sdp, *pw_db; 104 DBT data, sdata, key; 105 FILE *fp, *oldfp; 106 sigset_t set; 107 int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0; 108 unsigned int len; 109 uint32_t store; 110 const char *t; 111 char *p; 112 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024]; 113 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)]; 114 char buf2[MAXPATHLEN]; 115 char sbuf2[MAXPATHLEN]; 116 char *username; 117 u_int method, methoduid; 118 int Cflag, dflag, iflag, lflag; 119 int nblock = 0; 120 121 iflag = dflag = Cflag = lflag = 0; 122 strcpy(prefix, _PATH_PWD); 123 makeold = 0; 124 username = NULL; 125 oldfp = NULL; 126 while ((ch = getopt(argc, argv, "BCLlNd:ips:u:v")) != -1) 127 switch(ch) { 128 case 'B': /* big-endian output */ 129 openinfo.lorder = BIG_ENDIAN; 130 break; 131 case 'C': /* verify only */ 132 Cflag = 1; 133 break; 134 case 'l': /* generate legacy entries */ 135 lflag = 1; 136 break; 137 case 'L': /* little-endian output */ 138 openinfo.lorder = LITTLE_ENDIAN; 139 break; 140 case 'N': /* do not wait for lock */ 141 nblock = LOCK_NB; /* will fail if locked */ 142 break; 143 case 'd': 144 dflag++; 145 strlcpy(prefix, optarg, sizeof(prefix)); 146 break; 147 case 'i': 148 iflag++; 149 break; 150 case 'p': /* create V7 "file.orig" */ 151 makeold = 1; 152 break; 153 case 's': /* change default cachesize */ 154 openinfo.cachesize = atoi(optarg) * 1024 * 1024; 155 break; 156 case 'u': /* only update this record */ 157 username = optarg; 158 break; 159 case 'v': /* backward compatible */ 160 break; 161 default: 162 usage(); 163 } 164 argc -= optind; 165 argv += optind; 166 167 if (argc != 1 || (username && (*username == '+' || *username == '-'))) 168 usage(); 169 if (lflag) 170 warnx("legacy (v3) database format support is deprecated"); 171 172 /* 173 * This could be changed to allow the user to interrupt. 174 * Probably not worth the effort. 175 */ 176 sigemptyset(&set); 177 sigaddset(&set, SIGTSTP); 178 sigaddset(&set, SIGHUP); 179 sigaddset(&set, SIGINT); 180 sigaddset(&set, SIGQUIT); 181 sigaddset(&set, SIGTERM); 182 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL); 183 184 /* We don't care what the user wants. */ 185 (void)umask(0); 186 187 pname = *argv; 188 189 /* 190 * Open and lock the original password file. We have to check 191 * the hardlink count after we get the lock to handle any potential 192 * unlink/rename race. 193 * 194 * This lock is necessary when someone runs pwd_mkdb manually, directly 195 * on master.passwd, to handle the case where a user might try to 196 * change his password while pwd_mkdb is running. 197 */ 198 for (;;) { 199 struct stat st; 200 201 if (!(fp = fopen(pname, "r"))) 202 error(pname); 203 if (flock(fileno(fp), LOCK_EX|nblock) < 0 && !(dflag && iflag)) 204 error("flock"); 205 if (fstat(fileno(fp), &st) < 0) 206 error(pname); 207 if (st.st_nlink != 0) 208 break; 209 fclose(fp); 210 fp = NULL; 211 } 212 213 /* check only if password database is valid */ 214 if (Cflag) { 215 while (scan(fp, &pwd)) 216 if (!is_comment && strlen(pwd.pw_name) >= MAXLOGNAME) { 217 warnx("%s: username too long", pwd.pw_name); 218 exit(1); 219 } 220 exit(0); 221 } 222 223 /* Open the temporary insecure password database. */ 224 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB); 225 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB); 226 if (username) { 227 int use_version; 228 229 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB); 230 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB); 231 232 clean = FILE_INSECURE; 233 cp(buf2, buf, PERM_INSECURE); 234 dp = dbopen(buf, 235 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo); 236 if (dp == NULL) 237 error(buf); 238 239 clean = FILE_SECURE; 240 cp(sbuf2, sbuf, PERM_SECURE); 241 sdp = dbopen(sbuf, 242 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo); 243 if (sdp == NULL) 244 error(sbuf); 245 246 /* 247 * Do some trouble to check if we should store this users 248 * uid. Don't use getpwnam/getpwuid as that interferes 249 * with NIS. 250 */ 251 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL); 252 if (!pw_db) 253 error(_MP_DB); 254 255 key.data = verskey; 256 key.size = sizeof(verskey)-1; 257 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) 258 use_version = *(unsigned char *)data.data; 259 else 260 use_version = 3; 261 buf[0] = _PW_VERSIONED(_PW_KEYBYNAME, use_version); 262 len = strlen(username); 263 264 /* Only check that username fits in buffer */ 265 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1)); 266 key.data = (u_char *)buf; 267 key.size = len + 1; 268 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) { 269 p = (char *)data.data; 270 271 /* jump over pw_name and pw_passwd, to get to pw_uid */ 272 while (*p++) 273 ; 274 while (*p++) 275 ; 276 277 buf[0] = _PW_VERSIONED(_PW_KEYBYUID, use_version); 278 memmove(buf + 1, p, sizeof(store)); 279 key.data = (u_char *)buf; 280 key.size = sizeof(store) + 1; 281 282 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) { 283 /* First field of data.data holds pw_pwname */ 284 if (!strcmp(data.data, username)) 285 methoduid = 0; 286 else 287 methoduid = R_NOOVERWRITE; 288 } else { 289 methoduid = R_NOOVERWRITE; 290 } 291 } else { 292 methoduid = R_NOOVERWRITE; 293 } 294 if ((pw_db->close)(pw_db)) 295 error("close pw_db"); 296 method = 0; 297 } else { 298 dp = dbopen(buf, 299 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo); 300 if (dp == NULL) 301 error(buf); 302 clean = FILE_INSECURE; 303 304 sdp = dbopen(sbuf, 305 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo); 306 if (sdp == NULL) 307 error(sbuf); 308 clean = FILE_SECURE; 309 310 method = R_NOOVERWRITE; 311 methoduid = R_NOOVERWRITE; 312 } 313 314 /* 315 * Open file for old password file. Minor trickiness -- don't want to 316 * chance the file already existing, since someone (stupidly) might 317 * still be using this for permission checking. So, open it first and 318 * fdopen the resulting fd. The resulting file should be readable by 319 * everyone. 320 */ 321 if (makeold) { 322 (void)snprintf(buf, sizeof(buf), "%s.orig", pname); 323 if ((tfd = open(buf, 324 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0) 325 error(buf); 326 if ((oldfp = fdopen(tfd, "w")) == NULL) 327 error(buf); 328 clean = FILE_ORIG; 329 } 330 331 /* 332 * The databases actually contain three copies of the original data. 333 * Each password file entry is converted into a rough approximation 334 * of a ``struct passwd'', with the strings placed inline. This 335 * object is then stored as the data for three separate keys. The 336 * first key * is the pw_name field prepended by the _PW_KEYBYNAME 337 * character. The second key is the pw_uid field prepended by the 338 * _PW_KEYBYUID character. The third key is the line number in the 339 * original file prepended by the _PW_KEYBYNUM character. (The special 340 * characters are prepended to ensure that the keys do not collide.) 341 */ 342 /* In order to transition this file into a machine-independent 343 * form, we have to change the format of entries. However, since 344 * older binaries will still expect the old MD format entries, we 345 * create those as usual and use versioned tags for the new entries. 346 */ 347 if (username == NULL) { 348 /* Do not add the VERSION tag when updating a single 349 * user. When operating on `old format' databases, this 350 * would result in applications `seeing' only the updated 351 * entries. 352 */ 353 key.data = verskey; 354 key.size = sizeof(verskey)-1; 355 data.data = &version; 356 data.size = 1; 357 if ((dp->put)(dp, &key, &data, 0) == -1) 358 error("put"); 359 if ((sdp->put)(sdp, &key, &data, 0) == -1) 360 error("put"); 361 } 362 ypcnt = 0; 363 data.data = (u_char *)buf; 364 sdata.data = (u_char *)sbuf; 365 key.data = (u_char *)tbuf; 366 for (cnt = 1; scan(fp, &pwd); ++cnt) { 367 if (!is_comment && 368 (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')) { 369 yp_enabled = 1; 370 ypcnt++; 371 } 372 if (is_comment) 373 --cnt; 374 #define COMPACT(e) t = e; while ((*p++ = *t++)); 375 #define SCALAR(e) store = htonl((uint32_t)(e)); \ 376 memmove(p, &store, sizeof(store)); \ 377 p += sizeof(store); 378 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \ 379 memmove(p, &store, sizeof(store)); \ 380 p += sizeof(store); 381 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \ 382 (uint32_t)(e) : \ 383 bswap32((uint32_t)(e))) 384 if (!is_comment && 385 (!username || (strcmp(username, pwd.pw_name) == 0))) { 386 /* Create insecure data. */ 387 p = buf; 388 COMPACT(pwd.pw_name); 389 COMPACT("*"); 390 SCALAR(pwd.pw_uid); 391 SCALAR(pwd.pw_gid); 392 SCALAR(pwd.pw_change); 393 COMPACT(pwd.pw_class); 394 COMPACT(pwd.pw_gecos); 395 COMPACT(pwd.pw_dir); 396 COMPACT(pwd.pw_shell); 397 SCALAR(pwd.pw_expire); 398 SCALAR(pwd.pw_fields); 399 data.size = p - buf; 400 401 /* Create secure data. */ 402 p = sbuf; 403 COMPACT(pwd.pw_name); 404 COMPACT(pwd.pw_passwd); 405 SCALAR(pwd.pw_uid); 406 SCALAR(pwd.pw_gid); 407 SCALAR(pwd.pw_change); 408 COMPACT(pwd.pw_class); 409 COMPACT(pwd.pw_gecos); 410 COMPACT(pwd.pw_dir); 411 COMPACT(pwd.pw_shell); 412 SCALAR(pwd.pw_expire); 413 SCALAR(pwd.pw_fields); 414 sdata.size = p - sbuf; 415 416 /* Store insecure by name. */ 417 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME); 418 len = strlen(pwd.pw_name); 419 memmove(tbuf + 1, pwd.pw_name, len); 420 key.size = len + 1; 421 if ((dp->put)(dp, &key, &data, method) == -1) 422 error("put"); 423 424 /* Store insecure by number. */ 425 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM); 426 store = htonl(cnt); 427 memmove(tbuf + 1, &store, sizeof(store)); 428 key.size = sizeof(store) + 1; 429 if ((dp->put)(dp, &key, &data, method) == -1) 430 error("put"); 431 432 /* Store insecure by uid. */ 433 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID); 434 store = htonl(pwd.pw_uid); 435 memmove(tbuf + 1, &store, sizeof(store)); 436 key.size = sizeof(store) + 1; 437 if ((dp->put)(dp, &key, &data, methoduid) == -1) 438 error("put"); 439 440 /* Store secure by name. */ 441 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNAME); 442 len = strlen(pwd.pw_name); 443 memmove(tbuf + 1, pwd.pw_name, len); 444 key.size = len + 1; 445 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 446 error("put"); 447 448 /* Store secure by number. */ 449 tbuf[0] = CURRENT_VERSION(_PW_KEYBYNUM); 450 store = htonl(cnt); 451 memmove(tbuf + 1, &store, sizeof(store)); 452 key.size = sizeof(store) + 1; 453 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 454 error("put"); 455 456 /* Store secure by uid. */ 457 tbuf[0] = CURRENT_VERSION(_PW_KEYBYUID); 458 store = htonl(pwd.pw_uid); 459 memmove(tbuf + 1, &store, sizeof(store)); 460 key.size = sizeof(store) + 1; 461 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1) 462 error("put"); 463 464 /* Store insecure and secure special plus and special minus */ 465 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') { 466 tbuf[0] = CURRENT_VERSION(_PW_KEYYPBYNUM); 467 store = htonl(ypcnt); 468 memmove(tbuf + 1, &store, sizeof(store)); 469 key.size = sizeof(store) + 1; 470 if ((dp->put)(dp, &key, &data, method) == -1) 471 error("put"); 472 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 473 error("put"); 474 } 475 476 if (lflag) { 477 /* Create insecure data. (legacy version) */ 478 p = buf; 479 COMPACT(pwd.pw_name); 480 COMPACT("*"); 481 LSCALAR(pwd.pw_uid); 482 LSCALAR(pwd.pw_gid); 483 LSCALAR(pwd.pw_change); 484 COMPACT(pwd.pw_class); 485 COMPACT(pwd.pw_gecos); 486 COMPACT(pwd.pw_dir); 487 COMPACT(pwd.pw_shell); 488 LSCALAR(pwd.pw_expire); 489 LSCALAR(pwd.pw_fields); 490 data.size = p - buf; 491 492 /* Create secure data. (legacy version) */ 493 p = sbuf; 494 COMPACT(pwd.pw_name); 495 COMPACT(pwd.pw_passwd); 496 LSCALAR(pwd.pw_uid); 497 LSCALAR(pwd.pw_gid); 498 LSCALAR(pwd.pw_change); 499 COMPACT(pwd.pw_class); 500 COMPACT(pwd.pw_gecos); 501 COMPACT(pwd.pw_dir); 502 COMPACT(pwd.pw_shell); 503 LSCALAR(pwd.pw_expire); 504 LSCALAR(pwd.pw_fields); 505 sdata.size = p - sbuf; 506 507 /* Store insecure by name. */ 508 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME); 509 len = strlen(pwd.pw_name); 510 memmove(tbuf + 1, pwd.pw_name, len); 511 key.size = len + 1; 512 if ((dp->put)(dp, &key, &data, method) == -1) 513 error("put"); 514 515 /* Store insecure by number. */ 516 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM); 517 store = HTOL(cnt); 518 memmove(tbuf + 1, &store, sizeof(store)); 519 key.size = sizeof(store) + 1; 520 if ((dp->put)(dp, &key, &data, method) == -1) 521 error("put"); 522 523 /* Store insecure by uid. */ 524 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID); 525 store = HTOL(pwd.pw_uid); 526 memmove(tbuf + 1, &store, sizeof(store)); 527 key.size = sizeof(store) + 1; 528 if ((dp->put)(dp, &key, &data, methoduid) == -1) 529 error("put"); 530 531 /* Store secure by name. */ 532 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNAME); 533 len = strlen(pwd.pw_name); 534 memmove(tbuf + 1, pwd.pw_name, len); 535 key.size = len + 1; 536 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 537 error("put"); 538 539 /* Store secure by number. */ 540 tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM); 541 store = HTOL(cnt); 542 memmove(tbuf + 1, &store, sizeof(store)); 543 key.size = sizeof(store) + 1; 544 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 545 error("put"); 546 547 /* Store secure by uid. */ 548 tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID); 549 store = HTOL(pwd.pw_uid); 550 memmove(tbuf + 1, &store, sizeof(store)); 551 key.size = sizeof(store) + 1; 552 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1) 553 error("put"); 554 555 /* Store insecure and secure special plus and special minus */ 556 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') { 557 tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM); 558 store = HTOL(ypcnt); 559 memmove(tbuf + 1, &store, sizeof(store)); 560 key.size = sizeof(store) + 1; 561 if ((dp->put)(dp, &key, &data, method) == -1) 562 error("put"); 563 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 564 error("put"); 565 } 566 } 567 } 568 /* Create original format password file entry */ 569 if (is_comment && makeold){ /* copy comments */ 570 if (fprintf(oldfp, "%s\n", line) < 0) 571 error("write old"); 572 } else if (makeold) { 573 char uidstr[20]; 574 char gidstr[20]; 575 576 snprintf(uidstr, sizeof(uidstr), "%u", pwd.pw_uid); 577 snprintf(gidstr, sizeof(gidstr), "%u", pwd.pw_gid); 578 579 if (fprintf(oldfp, "%s:*:%s:%s:%s:%s:%s\n", 580 pwd.pw_name, pwd.pw_fields & _PWF_UID ? uidstr : "", 581 pwd.pw_fields & _PWF_GID ? gidstr : "", 582 pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell) < 0) 583 error("write old"); 584 } 585 } 586 /* If YP enabled, set flag. */ 587 if (yp_enabled) { 588 buf[0] = yp_enabled + 2; 589 data.size = 1; 590 key.size = 1; 591 tbuf[0] = CURRENT_VERSION(_PW_KEYYPENABLED); 592 if ((dp->put)(dp, &key, &data, method) == -1) 593 error("put"); 594 if ((sdp->put)(sdp, &key, &data, method) == -1) 595 error("put"); 596 if (lflag) { 597 tbuf[0] = LEGACY_VERSION(_PW_KEYYPENABLED); 598 key.size = 1; 599 if ((dp->put)(dp, &key, &data, method) == -1) 600 error("put"); 601 if ((sdp->put)(sdp, &key, &data, method) == -1) 602 error("put"); 603 } 604 } 605 606 if ((dp->close)(dp) == -1) 607 error("close"); 608 if ((sdp->close)(sdp) == -1) 609 error("close"); 610 if (makeold) { 611 (void)fflush(oldfp); 612 if (fclose(oldfp) == EOF) 613 error("close old"); 614 } 615 616 /* Set master.passwd permissions, in case caller forgot. */ 617 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR); 618 619 /* Install as the real password files. */ 620 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB); 621 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB); 622 mv(buf, buf2); 623 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB); 624 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB); 625 mv(buf, buf2); 626 if (makeold) { 627 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD); 628 (void)snprintf(buf, sizeof(buf), "%s.orig", pname); 629 mv(buf, buf2); 630 } 631 /* 632 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8) 633 * all use flock(2) on it to block other incarnations of themselves. 634 * The rename means that everything is unlocked, as the original file 635 * can no longer be accessed. 636 */ 637 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD); 638 mv(pname, buf); 639 640 /* 641 * Close locked password file after rename() 642 */ 643 if (fclose(fp) == EOF) 644 error("close fp"); 645 646 exit(0); 647 } 648 649 int 650 scan(FILE *fp, struct passwd *pw) 651 { 652 static int lcnt; 653 size_t len; 654 char *p; 655 656 p = fgetln(fp, &len); 657 if (p == NULL) 658 return (0); 659 ++lcnt; 660 /* 661 * ``... if I swallow anything evil, put your fingers down my 662 * throat...'' 663 * -- The Who 664 */ 665 if (len > 0 && p[len - 1] == '\n') 666 len--; 667 if (len >= sizeof(line) - 1) { 668 warnx("line #%d too long", lcnt); 669 goto fmt; 670 } 671 memcpy(line, p, len); 672 line[len] = '\0'; 673 674 /* 675 * Ignore comments: ^[ \t]*# 676 */ 677 for (p = line; *p != '\0'; p++) 678 if (*p != ' ' && *p != '\t') 679 break; 680 if (*p == '#' || *p == '\0') { 681 is_comment = 1; 682 return(1); 683 } else 684 is_comment = 0; 685 686 if (!__pw_scan(line, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) { 687 warnx("at line #%d", lcnt); 688 fmt: errno = EFTYPE; /* XXX */ 689 error(pname); 690 } 691 692 return (1); 693 } 694 695 void 696 cp(char *from, char *to, mode_t mode) 697 { 698 static char buf[MAXBSIZE]; 699 int from_fd, rcount, to_fd, wcount; 700 701 if ((from_fd = open(from, O_RDONLY, 0)) < 0) 702 error(from); 703 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) 704 error(to); 705 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { 706 wcount = write(to_fd, buf, rcount); 707 if (rcount != wcount || wcount == -1) { 708 int sverrno = errno; 709 710 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); 711 errno = sverrno; 712 error(buf); 713 } 714 } 715 if (rcount < 0) { 716 int sverrno = errno; 717 718 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); 719 errno = sverrno; 720 error(buf); 721 } 722 } 723 724 725 void 726 mv(char *from, char *to) 727 { 728 char buf[MAXPATHLEN]; 729 char *to_dir; 730 int to_dir_fd = -1; 731 732 /* 733 * Make sure file is safe on disk. To improve performance we will call 734 * fsync() to the directory where file lies 735 */ 736 if (rename(from, to) != 0 || 737 (to_dir = dirname(to)) == NULL || 738 (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 || 739 fsync(to_dir_fd) != 0) { 740 int sverrno = errno; 741 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); 742 errno = sverrno; 743 if (to_dir_fd != -1) 744 close(to_dir_fd); 745 error(buf); 746 } 747 748 if (to_dir_fd != -1) 749 close(to_dir_fd); 750 } 751 752 void 753 error(const char *name) 754 { 755 756 warn("%s", name); 757 cleanup(); 758 exit(1); 759 } 760 761 void 762 cleanup(void) 763 { 764 char buf[MAXPATHLEN]; 765 766 switch(clean) { 767 case FILE_ORIG: 768 (void)snprintf(buf, sizeof(buf), "%s.orig", pname); 769 (void)unlink(buf); 770 /* FALLTHROUGH */ 771 case FILE_SECURE: 772 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB); 773 (void)unlink(buf); 774 /* FALLTHROUGH */ 775 case FILE_INSECURE: 776 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB); 777 (void)unlink(buf); 778 } 779 } 780 781 static void 782 usage(void) 783 { 784 785 (void)fprintf(stderr, 786 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n"); 787 exit(1); 788 } 789