1 /* 2 * Copyright (c) 1995, 1996 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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 Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/fcntl.h> 38 #include <sys/socket.h> 39 #include <sys/stat.h> 40 #include <sys/wait.h> 41 42 #include <arpa/inet.h> 43 #include <netinet/in.h> 44 45 #include <ctype.h> 46 #include <db.h> 47 #include <dirent.h> 48 #include <errno.h> 49 #include <limits.h> 50 #include <pwd.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include <libgen.h> 58 #include <libutil.h> 59 60 #include <rpc/rpc.h> 61 #include <rpcsvc/yp.h> 62 struct dom_binding; 63 #include <rpcsvc/ypclnt.h> 64 #include "yppasswdd_extern.h" 65 #include "yppasswd.h" 66 #include "yppasswd_private.h" 67 #include "ypxfr_extern.h" 68 #include "yp_extern.h" 69 70 static struct passwd yp_password; 71 72 static void 73 xlate_passwd(struct x_master_passwd *xpwd, struct passwd *pwd) 74 { 75 pwd->pw_name = xpwd->pw_name; 76 pwd->pw_passwd = xpwd->pw_passwd; 77 pwd->pw_uid = xpwd->pw_uid; 78 pwd->pw_gid = xpwd->pw_gid; 79 pwd->pw_change = xpwd->pw_change; 80 pwd->pw_class = xpwd->pw_class; 81 pwd->pw_gecos = xpwd->pw_gecos; 82 pwd->pw_dir = xpwd->pw_dir; 83 pwd->pw_shell = xpwd->pw_shell; 84 pwd->pw_expire = xpwd->pw_expire; 85 pwd->pw_fields = xpwd->pw_fields; 86 } 87 88 static void 89 copy_yp_pass(char *p, int x, int m) 90 { 91 char *t, *s = p; 92 static char *buf; 93 94 yp_password.pw_fields = 0; 95 96 buf = realloc(buf, m + 10); 97 bzero(buf, m + 10); 98 99 /* Turn all colons into NULLs */ 100 while (strchr(s, ':')) { 101 s = (strchr(s, ':') + 1); 102 *(s - 1)= '\0'; 103 } 104 105 t = buf; 106 #define EXPAND(e) do { \ 107 e = t; \ 108 while ((*t++ = *p++)); \ 109 } while (0) 110 EXPAND(yp_password.pw_name); 111 yp_password.pw_fields |= _PWF_NAME; 112 EXPAND(yp_password.pw_passwd); 113 yp_password.pw_fields |= _PWF_PASSWD; 114 yp_password.pw_uid = atoi(p); 115 p += (strlen(p) + 1); 116 yp_password.pw_fields |= _PWF_UID; 117 yp_password.pw_gid = atoi(p); 118 p += (strlen(p) + 1); 119 yp_password.pw_fields |= _PWF_GID; 120 if (x) { 121 EXPAND(yp_password.pw_class); 122 yp_password.pw_fields |= _PWF_CLASS; 123 yp_password.pw_change = atol(p); 124 p += (strlen(p) + 1); 125 yp_password.pw_fields |= _PWF_CHANGE; 126 yp_password.pw_expire = atol(p); 127 p += (strlen(p) + 1); 128 yp_password.pw_fields |= _PWF_EXPIRE; 129 } 130 EXPAND(yp_password.pw_gecos); 131 yp_password.pw_fields |= _PWF_GECOS; 132 EXPAND(yp_password.pw_dir); 133 yp_password.pw_fields |= _PWF_DIR; 134 EXPAND(yp_password.pw_shell); 135 yp_password.pw_fields |= _PWF_SHELL; 136 137 return; 138 } 139 140 static int 141 validchars(char *arg) 142 { 143 size_t i; 144 145 for (i = 0; i < strlen(arg); i++) { 146 if (iscntrl(arg[i])) { 147 yp_error("string contains a control character"); 148 return(1); 149 } 150 if (arg[i] == ':') { 151 yp_error("string contains a colon"); 152 return(1); 153 } 154 /* Be evil: truncate strings with \n in them silently. */ 155 if (arg[i] == '\n') { 156 arg[i] = '\0'; 157 return(0); 158 } 159 } 160 return(0); 161 } 162 163 static int 164 validate_master(struct passwd *opw __unused, struct x_master_passwd *npw) 165 { 166 167 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') { 168 yp_error("client tried to modify an NIS entry"); 169 return(1); 170 } 171 172 if (validchars(npw->pw_shell)) { 173 yp_error("specified shell contains invalid characters"); 174 return(1); 175 } 176 177 if (validchars(npw->pw_gecos)) { 178 yp_error("specified gecos field contains invalid characters"); 179 return(1); 180 } 181 182 if (validchars(npw->pw_passwd)) { 183 yp_error("specified password contains invalid characters"); 184 return(1); 185 } 186 return(0); 187 } 188 189 static int 190 validate(struct passwd *opw, struct x_passwd *npw) 191 { 192 193 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') { 194 yp_error("client tried to modify an NIS entry"); 195 return(1); 196 } 197 198 if ((uid_t)npw->pw_uid != opw->pw_uid) { 199 yp_error("UID mismatch: client says user %s has UID %d", 200 npw->pw_name, npw->pw_uid); 201 yp_error("database says user %s has UID %d", opw->pw_name, 202 opw->pw_uid); 203 return(1); 204 } 205 206 if ((gid_t)npw->pw_gid != opw->pw_gid) { 207 yp_error("GID mismatch: client says user %s has GID %d", 208 npw->pw_name, npw->pw_gid); 209 yp_error("database says user %s has GID %d", opw->pw_name, 210 opw->pw_gid); 211 return(1); 212 } 213 214 /* 215 * Don't allow the user to shoot himself in the foot, 216 * even on purpose. 217 */ 218 if (!no_chsh && !ok_shell(npw->pw_shell)) { 219 yp_error("%s is not a valid shell", npw->pw_shell); 220 return(1); 221 } 222 223 if (!no_chsh && validchars(npw->pw_shell)) { 224 yp_error("specified shell contains invalid characters"); 225 return(1); 226 } 227 228 if (validchars(npw->pw_gecos)) { 229 yp_error("specified gecos field contains invalid characters"); 230 return(1); 231 } 232 233 if (validchars(npw->pw_passwd)) { 234 yp_error("specified password contains invalid characters"); 235 return(1); 236 } 237 return(0); 238 } 239 240 /* 241 * Kludge alert: 242 * In order to have one rpc.yppasswdd support multiple domains, 243 * we have to cheat: we search each directory under /var/yp 244 * and try to match the user in each master.passwd.byname 245 * map that we find. If the user matches (username, uid and gid 246 * all agree), then we use that domain. If we match the user in 247 * more than one database, we must abort. 248 */ 249 static char * 250 find_domain(struct x_passwd *pw) 251 { 252 struct stat statbuf; 253 struct dirent *dirp; 254 DIR *dird; 255 char yp_mapdir[MAXPATHLEN + 2]; 256 static char domain[YPMAXDOMAIN]; 257 char *tmp = NULL; 258 DBT key, data; 259 int hit = 0; 260 261 yp_error("performing multidomain lookup"); 262 263 if ((dird = opendir(yp_dir)) == NULL) { 264 yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno)); 265 return(NULL); 266 } 267 268 while ((dirp = readdir(dird)) != NULL) { 269 snprintf(yp_mapdir, sizeof yp_mapdir, "%s/%s", 270 yp_dir, dirp->d_name); 271 if (stat(yp_mapdir, &statbuf) < 0) { 272 yp_error("stat(%s) failed: %s", yp_mapdir, 273 strerror(errno)); 274 closedir(dird); 275 return(NULL); 276 } 277 if (S_ISDIR(statbuf.st_mode)) { 278 tmp = (char *)dirp->d_name; 279 key.data = pw->pw_name; 280 key.size = strlen(pw->pw_name); 281 282 if (yp_get_record(tmp,"master.passwd.byname", 283 &key, &data, 0) != YP_TRUE) { 284 continue; 285 } 286 *((char *)data.data + data.size) = '\0'; 287 copy_yp_pass(data.data, 1, data.size); 288 if (yp_password.pw_uid == (uid_t)pw->pw_uid && 289 yp_password.pw_gid == (gid_t)pw->pw_gid) { 290 hit++; 291 snprintf(domain, YPMAXDOMAIN, "%s", tmp); 292 } 293 } 294 } 295 296 closedir(dird); 297 if (hit > 1) { 298 yp_error("found same user in two different domains"); 299 return(NULL); 300 } else 301 return((char *)&domain); 302 } 303 304 static const char *maps[] = { 305 "master.passwd.byname", 306 "master.passwd.byuid", 307 "passwd.byname", 308 "passwd.byuid" 309 }; 310 311 static const char *formats[] = { 312 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 313 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 314 "%s:%s:%d:%d:%s:%s:%s", 315 "%s:%s:%d:%d:%s:%s:%s" 316 }; 317 318 static int 319 update_inplace(struct passwd *pw, char *domain) 320 { 321 DB *dbp = NULL; 322 DBT key = { NULL, 0 }; 323 DBT data = { NULL, 0 }; 324 char pwbuf[YPMAXRECORD]; 325 char keybuf[20]; 326 int i; 327 char *ptr = NULL; 328 static char yp_last[] = "YP_LAST_MODIFIED"; 329 char yplastbuf[YPMAXRECORD]; 330 331 snprintf(yplastbuf, sizeof yplastbuf, "%llu", 332 (unsigned long long)time(NULL)); 333 334 for (i = 0; i < 4; i++) { 335 336 if (i % 2) { 337 snprintf(keybuf, sizeof keybuf, 338 "%llu", (unsigned long long)pw->pw_uid); 339 key.data = &keybuf; 340 key.size = strlen(keybuf); 341 } else { 342 key.data = pw->pw_name; 343 key.size = strlen(pw->pw_name); 344 } 345 346 /* 347 * XXX The passwd.byname and passwd.byuid maps come in 348 * two flavors: secure and insecure. The secure version 349 * has a '*' in the password field whereas the insecure one 350 * has a real crypted password. The maps will be insecure 351 * if they were built with 'unsecure = TRUE' enabled in 352 * /var/yp/Makefile, but we'd have no way of knowing if 353 * this has been done unless we were to try parsing the 354 * Makefile, which is a disgusting thought. Instead, we 355 * read the records from the maps, skip to the first ':' 356 * in them, and then look at the character immediately 357 * following it. If it's an '*' then the map is 'secure' 358 * and we must not insert a real password into the pw_passwd 359 * field. If it's not an '*', then we put the real crypted 360 * password in. 361 */ 362 if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) { 363 yp_error("couldn't read %s/%s: %s", domain, 364 maps[i], strerror(errno)); 365 return(1); 366 } 367 368 if ((ptr = strchr(data.data, ':')) == NULL) { 369 yp_error("no colon in passwd record?!"); 370 return(1); 371 } 372 373 /* 374 * XXX Supposing we have more than one user with the same 375 * UID? (Or more than one user with the same name?) We could 376 * end up modifying the wrong record if were not careful. 377 */ 378 if (i % 2) { 379 if (strncmp(data.data, pw->pw_name, 380 strlen(pw->pw_name))) { 381 yp_error("warning: found entry for UID %d \ 382 in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain, 383 (int)(ptr - (char *)data.data), 384 (char *)data.data); 385 yp_error("there may be more than one user \ 386 with the same UID - continuing"); 387 continue; 388 } 389 } else { 390 /* 391 * We're really being ultra-paranoid here. 392 * This is generally a 'can't happen' condition. 393 */ 394 snprintf(pwbuf, sizeof pwbuf, ":%d:%d:", pw->pw_uid, 395 pw->pw_gid); 396 if (!strstr(data.data, pwbuf)) { 397 yp_error("warning: found entry for user %s \ 398 in map %s@%s with wrong UID", pw->pw_name, maps[i], domain); 399 yp_error("there may be more than one user \ 400 with the same name - continuing"); 401 continue; 402 } 403 } 404 405 if (i < 2) { 406 snprintf(pwbuf, sizeof pwbuf, formats[i], 407 pw->pw_name, pw->pw_passwd, pw->pw_uid, 408 pw->pw_gid, pw->pw_class, pw->pw_change, 409 pw->pw_expire, pw->pw_gecos, pw->pw_dir, 410 pw->pw_shell); 411 } else { 412 snprintf(pwbuf, sizeof pwbuf, formats[i], 413 pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd, 414 pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir, 415 pw->pw_shell); 416 } 417 418 #define FLAGS O_RDWR|O_CREAT 419 420 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) { 421 yp_error("couldn't open %s/%s r/w: %s",domain, 422 maps[i],strerror(errno)); 423 return(1); 424 } 425 426 data.data = pwbuf; 427 data.size = strlen(pwbuf); 428 429 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) { 430 yp_error("failed to update record in %s/%s", domain, 431 maps[i]); 432 (void)(dbp->close)(dbp); 433 return(1); 434 } 435 436 key.data = yp_last; 437 key.size = strlen(yp_last); 438 data.data = (char *)&yplastbuf; 439 data.size = strlen(yplastbuf); 440 441 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) { 442 yp_error("failed to update timestamp in %s/%s", domain, 443 maps[i]); 444 (void)(dbp->close)(dbp); 445 return(1); 446 } 447 448 (void)(dbp->close)(dbp); 449 } 450 451 return(0); 452 } 453 454 int * 455 yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) 456 { 457 static int result; 458 struct sockaddr_in *rqhost; 459 DBT key, data; 460 int rval = 0; 461 int pfd, tfd; 462 int pid; 463 int passwd_changed = 0; 464 int shell_changed = 0; 465 int gecos_changed = 0; 466 char *cryptpw; 467 char *oldshell = NULL; 468 char *oldgecos = NULL; 469 char *passfile_hold; 470 char passfile_buf[MAXPATHLEN + 2]; 471 char passfile_hold_buf[MAXPATHLEN + 2]; 472 char *domain = yppasswd_domain; 473 static struct sockaddr_in clntaddr; 474 static struct timeval t_saved, t_test; 475 476 /* 477 * Normal user updates always use the 'default' master.passwd file. 478 */ 479 480 passfile = passfile_default; 481 result = 1; 482 483 rqhost = svc_getcaller(rqstp->rq_xprt); 484 485 gettimeofday(&t_test, NULL); 486 if (!bcmp(rqhost, &clntaddr, sizeof *rqhost) && 487 t_test.tv_sec > t_saved.tv_sec && 488 t_test.tv_sec - t_saved.tv_sec < 300) { 489 490 bzero(&clntaddr, sizeof clntaddr); 491 bzero(&t_saved, sizeof t_saved); 492 return(NULL); 493 } 494 495 bcopy(rqhost, &clntaddr, sizeof clntaddr); 496 gettimeofday(&t_saved, NULL); 497 498 if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) { 499 yp_error("rejected update request from unauthorized host"); 500 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED); 501 return(&result); 502 } 503 504 /* 505 * Step one: find the user. (It's kinda pointless to 506 * proceed if the user doesn't exist.) We look for the 507 * user in the master.passwd.byname database, _NOT_ by 508 * using getpwent() and friends! We can't use getpwent() 509 * since the NIS master server is not guaranteed to be 510 * configured as an NIS client. 511 */ 512 513 if (multidomain) { 514 if ((domain = find_domain(&argp->newpw)) == NULL) { 515 yp_error("multidomain lookup failed - aborting update"); 516 return(&result); 517 } else 518 yp_error("updating user %s in domain %s", 519 argp->newpw.pw_name, domain); 520 } 521 522 key.data = argp->newpw.pw_name; 523 key.size = strlen(argp->newpw.pw_name); 524 525 if ((rval = yp_get_record(domain,"master.passwd.byname", 526 &key, &data, 0)) != YP_TRUE) { 527 if (rval == YP_NOKEY) { 528 yp_error("user %s not found in passwd database", 529 argp->newpw.pw_name); 530 } else { 531 yp_error("database access error: %s", 532 yperr_string(rval)); 533 } 534 return(&result); 535 } 536 537 /* Nul terminate, please. */ 538 *((char *)data.data + data.size) = '\0'; 539 540 copy_yp_pass(data.data, 1, data.size); 541 542 /* Step 2: check that the supplied oldpass is valid. */ 543 544 cryptpw = crypt(argp->oldpass, yp_password.pw_passwd); 545 if (cryptpw == NULL || strcmp(cryptpw, yp_password.pw_passwd)) { 546 yp_error("rejected change attempt -- bad password"); 547 yp_error("client address: %s username: %s", 548 inet_ntoa(rqhost->sin_addr), 549 argp->newpw.pw_name); 550 return(&result); 551 } 552 553 /* Step 3: validate the arguments passed to us by the client. */ 554 555 if (validate(&yp_password, &argp->newpw)) { 556 yp_error("rejecting change attempt: bad arguments"); 557 yp_error("client address: %s username: %s", 558 inet_ntoa(rqhost->sin_addr), 559 argp->newpw.pw_name); 560 svcerr_decode(rqstp->rq_xprt); 561 return(&result); 562 } 563 564 /* Step 4: update the user's passwd structure. */ 565 566 if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) { 567 oldshell = yp_password.pw_shell; 568 yp_password.pw_shell = argp->newpw.pw_shell; 569 shell_changed++; 570 } 571 572 573 if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) { 574 oldgecos = yp_password.pw_gecos; 575 yp_password.pw_gecos = argp->newpw.pw_gecos; 576 gecos_changed++; 577 } 578 579 if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) { 580 yp_password.pw_passwd = argp->newpw.pw_passwd; 581 yp_password.pw_change = 0; 582 passwd_changed++; 583 } 584 585 /* 586 * If the caller specified a domain other than our 'default' 587 * domain, change the path to master.passwd accordingly. 588 */ 589 590 if (strcmp(domain, yppasswd_domain)) { 591 snprintf(passfile_buf, sizeof(passfile_buf), 592 "%s/%s/master.passwd", yp_dir, domain); 593 passfile = (char *)&passfile_buf; 594 } 595 596 /* 597 * Create a filename to hold the original master.passwd 598 * so if our call to yppwupdate fails we can roll back 599 */ 600 snprintf(passfile_hold_buf, sizeof(passfile_hold_buf), 601 "%s.hold", passfile); 602 passfile_hold = (char *)&passfile_hold_buf; 603 604 605 /* Step 5: make a new password file with the updated info. */ 606 607 if (pw_init(dirname(passfile), passfile)) { 608 yp_error("pw_init() failed"); 609 return &result; 610 } 611 if ((pfd = pw_lock()) == -1) { 612 pw_fini(); 613 yp_error("pw_lock() failed"); 614 return &result; 615 } 616 if ((tfd = pw_tmp(-1)) == -1) { 617 pw_fini(); 618 yp_error("pw_tmp() failed"); 619 return &result; 620 } 621 if (pw_copy(pfd, tfd, &yp_password, NULL) == -1) { 622 pw_fini(); 623 yp_error("pw_copy() failed"); 624 return &result; 625 } 626 if (rename(passfile, passfile_hold) == -1) { 627 pw_fini(); 628 yp_error("rename of %s to %s failed", passfile, 629 passfile_hold); 630 return &result; 631 } 632 633 if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) { 634 /* 635 * NIS server is exporting the system's master.passwd. 636 * Call pw_mkdb to rebuild passwd and the .db files 637 */ 638 if (pw_mkdb(yp_password.pw_name) == -1) { 639 pw_fini(); 640 yp_error("pw_mkdb() failed"); 641 rename(passfile_hold, passfile); 642 return &result; 643 } 644 } else { 645 /* 646 * NIS server is exporting a private master.passwd. 647 * Rename tempfile into final location 648 */ 649 if (rename(pw_tempname(), passfile) == -1) { 650 pw_fini(); 651 yp_error("rename of %s to %s failed", 652 pw_tempname(), passfile); 653 rename(passfile_hold, passfile); 654 return &result; 655 } 656 } 657 658 pw_fini(); 659 660 if (inplace) { 661 if ((rval = update_inplace(&yp_password, domain))) { 662 yp_error("inplace update failed -- rebuilding maps"); 663 } 664 } 665 666 switch ((pid = fork())) { 667 case 0: 668 if (inplace && !rval) { 669 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 670 yppasswd_domain, "pushpw", (char *)NULL); 671 } else { 672 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 673 yppasswd_domain, (char *)NULL); 674 } 675 yp_error("couldn't exec map update process: %s", 676 strerror(errno)); 677 unlink(passfile); 678 rename(passfile_hold, passfile); 679 exit(1); 680 break; 681 case -1: 682 yp_error("fork() failed: %s", strerror(errno)); 683 unlink(passfile); 684 rename(passfile_hold, passfile); 685 return(&result); 686 break; 687 default: 688 unlink(passfile_hold); 689 break; 690 } 691 692 if (verbose) { 693 yp_error("update completed for user %s (uid %d) in %s:", 694 argp->newpw.pw_name, argp->newpw.pw_uid, passfile); 695 696 if (passwd_changed) 697 yp_error("password changed"); 698 699 if (gecos_changed) 700 yp_error("gecos changed ('%s' -> '%s')", 701 oldgecos, argp->newpw.pw_gecos); 702 703 if (shell_changed) 704 yp_error("shell changed ('%s' -> '%s')", 705 oldshell, argp->newpw.pw_shell); 706 } 707 708 result = 0; 709 return (&result); 710 } 711 712 /* 713 * Note that this function performs a little less sanity checking 714 * than the last one. Since only the superuser is allowed to use it, 715 * it is assumed that the caller knows what he's doing. 716 */ 717 int * 718 yppasswdproc_update_master_1_svc(master_yppasswd *argp, 719 struct svc_req *rqstp) 720 { 721 static int result; 722 int pfd, tfd; 723 int pid; 724 uid_t uid; 725 int rval = 0; 726 DBT key, data; 727 char *passfile_hold; 728 char passfile_buf[MAXPATHLEN + 2]; 729 char passfile_hold_buf[MAXPATHLEN + 2]; 730 struct sockaddr_in *rqhost; 731 SVCXPRT *transp; 732 struct passwd newpasswd; 733 734 result = 1; 735 transp = rqstp->rq_xprt; 736 737 /* 738 * NO AF_INET CONNETCIONS ALLOWED! 739 */ 740 rqhost = svc_getcaller(transp); 741 if (rqhost->sin_family != AF_UNIX) { 742 yp_error("Alert! %s/%d attempted to use superuser-only \ 743 procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port); 744 svcerr_auth(transp, AUTH_BADCRED); 745 return(&result); 746 } 747 748 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) { 749 yp_error("caller didn't send proper credentials"); 750 svcerr_auth(transp, AUTH_BADCRED); 751 return(&result); 752 } 753 754 if (__rpc_get_local_uid(transp, &uid) < 0) { 755 yp_error("caller didn't send proper credentials"); 756 svcerr_auth(transp, AUTH_BADCRED); 757 return(&result); 758 } 759 760 if (uid) { 761 yp_error("caller euid is %d, expecting 0 -- rejecting request", 762 uid); 763 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED); 764 return(&result); 765 } 766 767 passfile = passfile_default; 768 769 key.data = argp->newpw.pw_name; 770 key.size = strlen(argp->newpw.pw_name); 771 772 /* 773 * The superuser may add entries to the passwd maps if 774 * rpc.yppasswdd is started with the -a flag. Paranoia 775 * prevents me from allowing additions by default. 776 */ 777 if ((rval = yp_get_record(argp->domain, "master.passwd.byname", 778 &key, &data, 0)) != YP_TRUE) { 779 if (rval == YP_NOKEY) { 780 yp_error("user %s not found in passwd database", 781 argp->newpw.pw_name); 782 if (allow_additions) 783 yp_error("notice: adding user %s to \ 784 master.passwd database for domain %s", argp->newpw.pw_name, argp->domain); 785 else 786 yp_error("restart rpc.yppasswdd with the -a flag to \ 787 allow additions to be made to the password database"); 788 } else { 789 yp_error("database access error: %s", 790 yperr_string(rval)); 791 } 792 if (!allow_additions) 793 return(&result); 794 } else { 795 796 /* Nul terminate, please. */ 797 *((char *)data.data + data.size) = '\0'; 798 799 copy_yp_pass(data.data, 1, data.size); 800 } 801 802 /* 803 * Perform a small bit of sanity checking. 804 */ 805 if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){ 806 yp_error("rejecting update attempt for %s: bad arguments", 807 argp->newpw.pw_name); 808 return(&result); 809 } 810 811 /* 812 * If the caller specified a domain other than our 'default' 813 * domain, change the path to master.passwd accordingly. 814 */ 815 816 if (strcmp(argp->domain, yppasswd_domain)) { 817 snprintf(passfile_buf, sizeof(passfile_buf), 818 "%s/%s/master.passwd", yp_dir, argp->domain); 819 passfile = (char *)&passfile_buf; 820 } 821 822 /* 823 * Create a filename to hold the original master.passwd 824 * so if our call to yppwupdate fails we can roll back 825 */ 826 snprintf(passfile_hold_buf, sizeof(passfile_hold_buf), 827 "%s.hold", passfile); 828 passfile_hold = (char *)&passfile_hold_buf; 829 830 if (pw_init(dirname(passfile), passfile)) { 831 yp_error("pw_init() failed"); 832 return &result; 833 } 834 if ((pfd = pw_lock()) == -1) { 835 pw_fini(); 836 yp_error("pw_lock() failed"); 837 return &result; 838 } 839 if ((tfd = pw_tmp(-1)) == -1) { 840 pw_fini(); 841 yp_error("pw_tmp() failed"); 842 return &result; 843 } 844 xlate_passwd(&argp->newpw, &newpasswd); 845 if (pw_copy(pfd, tfd, &newpasswd, NULL) == -1) { 846 pw_fini(); 847 yp_error("pw_copy() failed"); 848 return &result; 849 } 850 if (rename(passfile, passfile_hold) == -1) { 851 pw_fini(); 852 yp_error("rename of %s to %s failed", passfile, 853 passfile_hold); 854 return &result; 855 } 856 if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) { 857 /* 858 * NIS server is exporting the system's master.passwd. 859 * Call pw_mkdb to rebuild passwd and the .db files 860 */ 861 if (pw_mkdb(argp->newpw.pw_name) == -1) { 862 pw_fini(); 863 yp_error("pw_mkdb() failed"); 864 rename(passfile_hold, passfile); 865 return &result; 866 } 867 } else { 868 /* 869 * NIS server is exporting a private master.passwd. 870 * Rename tempfile into final location 871 */ 872 if (rename(pw_tempname(), passfile) == -1) { 873 pw_fini(); 874 yp_error("rename of %s to %s failed", 875 pw_tempname(), passfile); 876 rename(passfile_hold, passfile); 877 return &result; 878 } 879 } 880 pw_fini(); 881 882 if (inplace) { 883 xlate_passwd(&argp->newpw, &newpasswd); 884 if ((rval = update_inplace(&newpasswd, argp->domain))) { 885 yp_error("inplace update failed -- rebuilding maps"); 886 } 887 } 888 889 switch ((pid = fork())) { 890 case 0: 891 if (inplace && !rval) { 892 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 893 argp->domain, "pushpw", (char *)NULL); 894 } else { 895 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 896 argp->domain, (char *)NULL); 897 } 898 yp_error("couldn't exec map update process: %s", 899 strerror(errno)); 900 unlink(passfile); 901 rename(passfile_hold, passfile); 902 exit(1); 903 break; 904 case -1: 905 yp_error("fork() failed: %s", strerror(errno)); 906 unlink(passfile); 907 rename(passfile_hold, passfile); 908 return(&result); 909 break; 910 default: 911 unlink(passfile_hold); 912 break; 913 } 914 915 yp_error("performed update of user %s (uid %d) domain %s", 916 argp->newpw.pw_name, 917 argp->newpw.pw_uid, 918 argp->domain); 919 920 result = 0; 921 return(&result); 922 } 923