1 /*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 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 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1991, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 47 #include <db.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <limits.h> 52 #include <pwd.h> 53 #include <signal.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "pw_scan.h" 60 61 #define INSECURE 1 62 #define SECURE 2 63 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 64 #define PERM_SECURE (S_IRUSR|S_IWUSR) 65 66 HASHINFO openinfo = { 67 4096, /* bsize */ 68 32, /* ffactor */ 69 256, /* nelem */ 70 2048 * 1024, /* cachesize */ 71 NULL, /* hash() */ 72 0 /* lorder */ 73 }; 74 75 static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean; 76 static struct passwd pwd; /* password structure */ 77 static char *pname; /* password file name */ 78 static char prefix[MAXPATHLEN]; 79 80 void cleanup __P((void)); 81 void error __P((char *)); 82 void cp __P((char *, char *, mode_t mode)); 83 void mv __P((char *, char *)); 84 int scan __P((FILE *, struct passwd *)); 85 void usage __P((void)); 86 87 int 88 main(argc, argv) 89 int argc; 90 char *argv[]; 91 { 92 DB *dp, *sdp, *pw_db; 93 DBT data, sdata, key; 94 FILE *fp, *oldfp; 95 sigset_t set; 96 int ch, cnt, ypcnt, len, makeold, tfd, yp_enabled = 0; 97 char *p, *t; 98 char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024]; 99 char sbuf[MAX(MAXPATHLEN, LINE_MAX * 2)]; 100 char buf2[MAXPATHLEN]; 101 char sbuf2[MAXPATHLEN]; 102 char *username; 103 u_int method, methoduid; 104 int cflag; 105 106 cflag = 0; 107 strcpy(prefix, _PATH_PWD); 108 makeold = 0; 109 username = NULL; 110 while ((ch = getopt(argc, argv, "cd:pu:v")) != EOF) 111 switch(ch) { 112 case 'c': /* verify only */ 113 cflag = 1; 114 break; 115 case 'd': 116 strcpy(prefix, optarg); 117 break; 118 case 'p': /* create V7 "file.orig" */ 119 makeold = 1; 120 break; 121 case 'u': /* only update this record */ 122 username = optarg; 123 break; 124 case 'v': /* backward compatible */ 125 break; 126 default: 127 usage(); 128 } 129 argc -= optind; 130 argv += optind; 131 132 if (argc != 1 || (username && (*username == '+' || *username == '-'))) 133 usage(); 134 135 /* 136 * This could be changed to allow the user to interrupt. 137 * Probably not worth the effort. 138 */ 139 sigemptyset(&set); 140 sigaddset(&set, SIGTSTP); 141 sigaddset(&set, SIGHUP); 142 sigaddset(&set, SIGINT); 143 sigaddset(&set, SIGQUIT); 144 sigaddset(&set, SIGTERM); 145 (void)sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL); 146 147 /* We don't care what the user wants. */ 148 (void)umask(0); 149 150 pname = *argv; 151 /* Open the original password file */ 152 if (!(fp = fopen(pname, "r"))) 153 error(pname); 154 155 /* check only if password database is valid */ 156 if (cflag) { 157 for (cnt = 1; scan(fp, &pwd); ++cnt); 158 exit(0); 159 } 160 161 /* Open the temporary insecure password database. */ 162 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB); 163 (void)snprintf(sbuf, sizeof(sbuf), "%s/%s.tmp", prefix, _SMP_DB); 164 if (username) { 165 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB); 166 (void)snprintf(sbuf2, sizeof(sbuf2), "%s/%s", prefix, _SMP_DB); 167 168 clean = FILE_INSECURE; 169 cp(buf2, buf, PERM_INSECURE); 170 dp = dbopen(buf, 171 O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo); 172 if (dp == NULL) 173 error(buf); 174 175 clean = FILE_SECURE; 176 cp(sbuf2, sbuf, PERM_SECURE); 177 sdp = dbopen(sbuf, 178 O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo); 179 if (sdp == NULL) 180 error(sbuf); 181 182 /* 183 * Do some trouble to check if we should store this users 184 * uid. Don't use getpwnam/getpwuid as that interferes 185 * with NIS. 186 */ 187 pw_db = dbopen(_PATH_MP_DB, O_RDONLY, 0, DB_HASH, NULL); 188 if (!pw_db) 189 error(_MP_DB); 190 buf[0] = _PW_KEYBYNAME; 191 len = strlen(username); 192 193 /* Only check that username fits in buffer */ 194 memmove(buf + 1, username, MIN(len, sizeof(buf) - 1)); 195 key.data = (u_char *)buf; 196 key.size = len + 1; 197 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) { 198 p = (char *)data.data; 199 200 /* jump over pw_name and pw_passwd, to get to pw_uid */ 201 while (*p++) 202 ; 203 while (*p++) 204 ; 205 206 buf[0] = _PW_KEYBYUID; 207 memmove(buf + 1, p, sizeof(int)); 208 key.data = (u_char *)buf; 209 key.size = sizeof(int) + 1; 210 211 if ((pw_db->get)(pw_db, &key, &data, 0) == 0) { 212 /* First field of data.data holds pw_pwname */ 213 if (!strcmp(data.data, username)) 214 methoduid = 0; 215 else 216 methoduid = R_NOOVERWRITE; 217 } else { 218 methoduid = R_NOOVERWRITE; 219 } 220 } else { 221 methoduid = R_NOOVERWRITE; 222 } 223 (void)(pw_db->close)(pw_db); 224 method = 0; 225 } else { 226 dp = dbopen(buf, 227 O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo); 228 if (dp == NULL) 229 error(buf); 230 clean = FILE_INSECURE; 231 232 sdp = dbopen(sbuf, 233 O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo); 234 if (sdp == NULL) 235 error(sbuf); 236 clean = FILE_SECURE; 237 238 method = R_NOOVERWRITE; 239 methoduid = R_NOOVERWRITE; 240 } 241 242 /* 243 * Open file for old password file. Minor trickiness -- don't want to 244 * chance the file already existing, since someone (stupidly) might 245 * still be using this for permission checking. So, open it first and 246 * fdopen the resulting fd. The resulting file should be readable by 247 * everyone. 248 */ 249 if (makeold) { 250 (void)snprintf(buf, sizeof(buf), "%s.orig", pname); 251 if ((tfd = open(buf, 252 O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0) 253 error(buf); 254 if ((oldfp = fdopen(tfd, "w")) == NULL) 255 error(buf); 256 clean = FILE_ORIG; 257 } 258 259 /* 260 * The databases actually contain three copies of the original data. 261 * Each password file entry is converted into a rough approximation 262 * of a ``struct passwd'', with the strings placed inline. This 263 * object is then stored as the data for three separate keys. The 264 * first key * is the pw_name field prepended by the _PW_KEYBYNAME 265 * character. The second key is the pw_uid field prepended by the 266 * _PW_KEYBYUID character. The third key is the line number in the 267 * original file prepended by the _PW_KEYBYNUM character. (The special 268 * characters are prepended to ensure that the keys do not collide.) 269 */ 270 ypcnt = 1; 271 data.data = (u_char *)buf; 272 sdata.data = (u_char *)sbuf; 273 key.data = (u_char *)tbuf; 274 for (cnt = 1; scan(fp, &pwd); ++cnt) { 275 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') 276 yp_enabled = 1; 277 #define COMPACT(e) t = e; while (*p++ = *t++); 278 if (!username || (strcmp(username, pwd.pw_name) == 0)) { 279 /* Create insecure data. */ 280 p = buf; 281 COMPACT(pwd.pw_name); 282 COMPACT("*"); 283 memmove(p, &pwd.pw_uid, sizeof(int)); 284 p += sizeof(int); 285 memmove(p, &pwd.pw_gid, sizeof(int)); 286 p += sizeof(int); 287 memmove(p, &pwd.pw_change, sizeof(time_t)); 288 p += sizeof(time_t); 289 COMPACT(pwd.pw_class); 290 COMPACT(pwd.pw_gecos); 291 COMPACT(pwd.pw_dir); 292 COMPACT(pwd.pw_shell); 293 memmove(p, &pwd.pw_expire, sizeof(time_t)); 294 p += sizeof(time_t); 295 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields); 296 p += sizeof pwd.pw_fields; 297 data.size = p - buf; 298 299 /* Create secure data. */ 300 p = sbuf; 301 COMPACT(pwd.pw_name); 302 COMPACT(pwd.pw_passwd); 303 memmove(p, &pwd.pw_uid, sizeof(int)); 304 p += sizeof(int); 305 memmove(p, &pwd.pw_gid, sizeof(int)); 306 p += sizeof(int); 307 memmove(p, &pwd.pw_change, sizeof(time_t)); 308 p += sizeof(time_t); 309 COMPACT(pwd.pw_class); 310 COMPACT(pwd.pw_gecos); 311 COMPACT(pwd.pw_dir); 312 COMPACT(pwd.pw_shell); 313 memmove(p, &pwd.pw_expire, sizeof(time_t)); 314 p += sizeof(time_t); 315 memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields); 316 p += sizeof pwd.pw_fields; 317 sdata.size = p - sbuf; 318 319 /* Store insecure by name. */ 320 tbuf[0] = _PW_KEYBYNAME; 321 len = strlen(pwd.pw_name); 322 memmove(tbuf + 1, pwd.pw_name, len); 323 key.size = len + 1; 324 if ((dp->put)(dp, &key, &data, method) == -1) 325 error("put"); 326 327 /* Store insecure by number. */ 328 tbuf[0] = _PW_KEYBYNUM; 329 memmove(tbuf + 1, &cnt, sizeof(cnt)); 330 key.size = sizeof(cnt) + 1; 331 if ((dp->put)(dp, &key, &data, method) == -1) 332 error("put"); 333 334 /* Store insecure by uid. */ 335 tbuf[0] = _PW_KEYBYUID; 336 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid)); 337 key.size = sizeof(pwd.pw_uid) + 1; 338 if ((dp->put)(dp, &key, &data, methoduid) == -1) 339 error("put"); 340 341 /* Store secure by name. */ 342 tbuf[0] = _PW_KEYBYNAME; 343 len = strlen(pwd.pw_name); 344 memmove(tbuf + 1, pwd.pw_name, len); 345 key.size = len + 1; 346 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 347 error("put"); 348 349 /* Store secure by number. */ 350 tbuf[0] = _PW_KEYBYNUM; 351 memmove(tbuf + 1, &cnt, sizeof(cnt)); 352 key.size = sizeof(cnt) + 1; 353 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 354 error("put"); 355 356 /* Store secure by uid. */ 357 tbuf[0] = _PW_KEYBYUID; 358 memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid)); 359 key.size = sizeof(pwd.pw_uid) + 1; 360 if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1) 361 error("put"); 362 363 /* Store insecure and secure special plus and special minus */ 364 if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') { 365 tbuf[0] = _PW_KEYYPBYNUM; 366 memmove(tbuf + 1, &ypcnt, sizeof(cnt)); 367 ypcnt++; 368 key.size = sizeof(cnt) + 1; 369 if ((dp->put)(dp, &key, &data, method) == -1) 370 error("put"); 371 if ((sdp->put)(sdp, &key, &sdata, method) == -1) 372 error("put"); 373 } 374 } 375 /* Create original format password file entry */ 376 if (makeold) 377 (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n", 378 pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos, 379 pwd.pw_dir, pwd.pw_shell); 380 } 381 /* If YP enabled, set flag. */ 382 if (yp_enabled) { 383 buf[0] = yp_enabled + 2; 384 data.size = 1; 385 tbuf[0] = _PW_KEYYPENABLED; 386 key.size = 1; 387 if ((dp->put)(dp, &key, &data, method) == -1) 388 error("put"); 389 if ((sdp->put)(sdp, &key, &data, method) == -1) 390 error("put"); 391 } 392 393 (void)(dp->close)(dp); 394 (void)(sdp->close)(sdp); 395 if (makeold) { 396 (void)fflush(oldfp); 397 (void)fclose(oldfp); 398 } 399 400 /* Set master.passwd permissions, in case caller forgot. */ 401 (void)fchmod(fileno(fp), S_IRUSR|S_IWUSR); 402 (void)fclose(fp); 403 404 /* Install as the real password files. */ 405 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB); 406 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _MP_DB); 407 mv(buf, buf2); 408 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB); 409 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _SMP_DB); 410 mv(buf, buf2); 411 if (makeold) { 412 (void)snprintf(buf2, sizeof(buf2), "%s/%s", prefix, _PASSWD); 413 (void)snprintf(buf, sizeof(buf), "%s.orig", pname); 414 mv(buf, buf2); 415 } 416 /* 417 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8) 418 * all use flock(2) on it to block other incarnations of themselves. 419 * The rename means that everything is unlocked, as the original file 420 * can no longer be accessed. 421 */ 422 (void)snprintf(buf, sizeof(buf), "%s/%s", prefix, _MASTERPASSWD); 423 mv(pname, buf); 424 exit(0); 425 } 426 427 int 428 scan(fp, pw) 429 FILE *fp; 430 struct passwd *pw; 431 { 432 static int lcnt; 433 static char line[LINE_MAX]; 434 char *p; 435 436 if (!fgets(line, sizeof(line), fp)) 437 return (0); 438 ++lcnt; 439 /* 440 * ``... if I swallow anything evil, put your fingers down my 441 * throat...'' 442 * -- The Who 443 */ 444 if (!(p = strchr(line, '\n'))) { 445 warnx("line too long"); 446 goto fmt; 447 448 } 449 *p = '\0'; 450 if (!pw_scan(line, pw)) { 451 warnx("at line #%d", lcnt); 452 fmt: errno = EFTYPE; /* XXX */ 453 error(pname); 454 } 455 456 return (1); 457 } 458 459 void 460 cp(from, to, mode) 461 char *from, *to; 462 mode_t mode; 463 { 464 static char buf[MAXBSIZE]; 465 int from_fd, rcount, to_fd, wcount; 466 467 if ((from_fd = open(from, O_RDONLY, 0)) < 0) 468 error(from); 469 if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) 470 error(to); 471 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { 472 wcount = write(to_fd, buf, rcount); 473 if (rcount != wcount || wcount == -1) { 474 int sverrno = errno; 475 476 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); 477 errno = sverrno; 478 error(buf); 479 } 480 } 481 if (rcount < 0) { 482 int sverrno = errno; 483 484 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); 485 errno = sverrno; 486 error(buf); 487 } 488 } 489 490 491 void 492 mv(from, to) 493 char *from, *to; 494 { 495 char buf[MAXPATHLEN]; 496 497 if (rename(from, to)) { 498 int sverrno = errno; 499 (void)snprintf(buf, sizeof(buf), "%s to %s", from, to); 500 errno = sverrno; 501 error(buf); 502 } 503 } 504 505 void 506 error(name) 507 char *name; 508 { 509 510 warn(name); 511 cleanup(); 512 exit(1); 513 } 514 515 void 516 cleanup() 517 { 518 char buf[MAXPATHLEN]; 519 520 switch(clean) { 521 case FILE_ORIG: 522 (void)snprintf(buf, sizeof(buf), "%s.orig", pname); 523 (void)unlink(buf); 524 /* FALLTHROUGH */ 525 case FILE_SECURE: 526 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _SMP_DB); 527 (void)unlink(buf); 528 /* FALLTHROUGH */ 529 case FILE_INSECURE: 530 (void)snprintf(buf, sizeof(buf), "%s/%s.tmp", prefix, _MP_DB); 531 (void)unlink(buf); 532 } 533 } 534 535 void 536 usage() 537 { 538 539 (void)fprintf(stderr, "usage: pwd_mkdb [-c] [-p] [-d <dest dir>] [-u <local username>] file\n"); 540 exit(1); 541 } 542