yppasswdd_server.c revision 351694
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: stable/11/usr.sbin/rpc.yppasswdd/yppasswdd_server.c 351694 2019-09-02 10:20:57Z kib $"); 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> 62struct 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 70static struct passwd yp_password; 71 72static void 73xlate_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 88static void 89copy_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 140static int 141validchars(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 163static int 164validate_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 189static int 190validate(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 */ 249static char * 250find_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 304static const char *maps[] = { 305 "master.passwd.byname", 306 "master.passwd.byuid", 307 "passwd.byname", 308 "passwd.byuid" 309}; 310 311static 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 318static int 319update_inplace(struct passwd *pw, char *domain) 320{ 321 DB *dbp = NULL; 322 DBT key = { NULL, 0 }; 323 DBT data = { NULL, 0 }; 324 char *pwbuf; 325 char keybuf[20]; 326 int i; 327 char *ptr = NULL; 328 static char yp_last[] = "YP_LAST_MODIFIED"; 329 char yplastbuf[64]; 330 331 snprintf(yplastbuf, sizeof yplastbuf, "%llu", 332 (unsigned long long)time(NULL)); 333 pwbuf = NULL; 334 335 for (i = 0; i < 4; i++) { 336 337 if (i % 2) { 338 snprintf(keybuf, sizeof keybuf, 339 "%llu", (unsigned long long)pw->pw_uid); 340 key.data = &keybuf; 341 key.size = strlen(keybuf); 342 } else { 343 key.data = pw->pw_name; 344 key.size = strlen(pw->pw_name); 345 } 346 347 /* 348 * XXX The passwd.byname and passwd.byuid maps come in 349 * two flavors: secure and insecure. The secure version 350 * has a '*' in the password field whereas the insecure one 351 * has a real crypted password. The maps will be insecure 352 * if they were built with 'unsecure = TRUE' enabled in 353 * /var/yp/Makefile, but we'd have no way of knowing if 354 * this has been done unless we were to try parsing the 355 * Makefile, which is a disgusting thought. Instead, we 356 * read the records from the maps, skip to the first ':' 357 * in them, and then look at the character immediately 358 * following it. If it's an '*' then the map is 'secure' 359 * and we must not insert a real password into the pw_passwd 360 * field. If it's not an '*', then we put the real crypted 361 * password in. 362 */ 363 if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) { 364 yp_error("couldn't read %s/%s: %s", domain, 365 maps[i], strerror(errno)); 366 goto ret1; 367 } 368 369 if ((ptr = strchr(data.data, ':')) == NULL) { 370 yp_error("no colon in passwd record?!"); 371 goto ret1; 372 } 373 374 /* 375 * XXX Supposing we have more than one user with the same 376 * UID? (Or more than one user with the same name?) We could 377 * end up modifying the wrong record if were not careful. 378 */ 379 if (i % 2) { 380 if (strncmp(data.data, pw->pw_name, 381 strlen(pw->pw_name))) { 382 yp_error("warning: found entry for UID %d \ 383in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain, 384 (int)(ptr - (char *)data.data), 385 (char *)data.data); 386 yp_error("there may be more than one user \ 387with the same UID - continuing"); 388 continue; 389 } 390 } else { 391 /* 392 * We're really being ultra-paranoid here. 393 * This is generally a 'can't happen' condition. 394 */ 395 free(pwbuf); 396 asprintf(&pwbuf, ":%d:%d:", pw->pw_uid, pw->pw_gid); 397 if (pwbuf == NULL) { 398 yp_error("no memory"); 399 goto ret1; 400 } 401 if (!strstr(data.data, pwbuf)) { 402 yp_error("warning: found entry for user %s \ 403in map %s@%s with wrong UID", pw->pw_name, maps[i], domain); 404 yp_error("there may be more than one user \ 405with the same name - continuing"); 406 continue; 407 } 408 } 409 410 if (i < 2) { 411 free(pwbuf); 412 asprintf(&pwbuf, formats[i], 413 pw->pw_name, pw->pw_passwd, pw->pw_uid, 414 pw->pw_gid, pw->pw_class, pw->pw_change, 415 pw->pw_expire, pw->pw_gecos, pw->pw_dir, 416 pw->pw_shell); 417 } else { 418 free(pwbuf); 419 asprintf(&pwbuf, formats[i], 420 pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd, 421 pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir, 422 pw->pw_shell); 423 } 424 if (pwbuf == NULL) { 425 yp_error("no memory"); 426 goto ret1; 427 } 428 429#define FLAGS O_RDWR|O_CREAT 430 431 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) { 432 yp_error("couldn't open %s/%s r/w: %s",domain, 433 maps[i],strerror(errno)); 434 goto ret1; 435 } 436 437 data.data = pwbuf; 438 data.size = strlen(pwbuf); 439 440 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) { 441 yp_error("failed to update record in %s/%s", domain, 442 maps[i]); 443 (void)(dbp->close)(dbp); 444 goto ret1; 445 } 446 447 key.data = yp_last; 448 key.size = strlen(yp_last); 449 data.data = (char *)&yplastbuf; 450 data.size = strlen(yplastbuf); 451 452 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) { 453 yp_error("failed to update timestamp in %s/%s", domain, 454 maps[i]); 455 (void)(dbp->close)(dbp); 456 goto ret1; 457 } 458 459 (void)(dbp->close)(dbp); 460 } 461 462 free(pwbuf); 463 return (0); 464ret1: 465 free(pwbuf); 466 return (1); 467} 468 469int * 470yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) 471{ 472 static int result; 473 struct sockaddr_in *rqhost; 474 DBT key, data; 475 int rval = 0; 476 int pfd, tfd; 477 int pid; 478 int passwd_changed = 0; 479 int shell_changed = 0; 480 int gecos_changed = 0; 481 char *cryptpw; 482 char *oldshell = NULL; 483 char *oldgecos = NULL; 484 char *passfile_hold; 485 char passfile_buf[MAXPATHLEN + 2]; 486 char passfile_hold_buf[MAXPATHLEN + 2]; 487 char *domain = yppasswd_domain; 488 static struct sockaddr_in clntaddr; 489 static struct timeval t_saved, t_test; 490 491 /* 492 * Normal user updates always use the 'default' master.passwd file. 493 */ 494 495 passfile = passfile_default; 496 result = 1; 497 498 rqhost = svc_getcaller(rqstp->rq_xprt); 499 500 gettimeofday(&t_test, NULL); 501 if (!bcmp(rqhost, &clntaddr, sizeof *rqhost) && 502 t_test.tv_sec > t_saved.tv_sec && 503 t_test.tv_sec - t_saved.tv_sec < 300) { 504 505 bzero(&clntaddr, sizeof clntaddr); 506 bzero(&t_saved, sizeof t_saved); 507 return(NULL); 508 } 509 510 bcopy(rqhost, &clntaddr, sizeof clntaddr); 511 gettimeofday(&t_saved, NULL); 512 513 if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) { 514 yp_error("rejected update request from unauthorized host"); 515 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED); 516 return(&result); 517 } 518 519 /* 520 * Step one: find the user. (It's kinda pointless to 521 * proceed if the user doesn't exist.) We look for the 522 * user in the master.passwd.byname database, _NOT_ by 523 * using getpwent() and friends! We can't use getpwent() 524 * since the NIS master server is not guaranteed to be 525 * configured as an NIS client. 526 */ 527 528 if (multidomain) { 529 if ((domain = find_domain(&argp->newpw)) == NULL) { 530 yp_error("multidomain lookup failed - aborting update"); 531 return(&result); 532 } else 533 yp_error("updating user %s in domain %s", 534 argp->newpw.pw_name, domain); 535 } 536 537 key.data = argp->newpw.pw_name; 538 key.size = strlen(argp->newpw.pw_name); 539 540 if ((rval = yp_get_record(domain,"master.passwd.byname", 541 &key, &data, 0)) != YP_TRUE) { 542 if (rval == YP_NOKEY) { 543 yp_error("user %s not found in passwd database", 544 argp->newpw.pw_name); 545 } else { 546 yp_error("database access error: %s", 547 yperr_string(rval)); 548 } 549 return(&result); 550 } 551 552 /* Nul terminate, please. */ 553 *((char *)data.data + data.size) = '\0'; 554 555 copy_yp_pass(data.data, 1, data.size); 556 557 /* Step 2: check that the supplied oldpass is valid. */ 558 559 cryptpw = crypt(argp->oldpass, yp_password.pw_passwd); 560 if (cryptpw == NULL || strcmp(cryptpw, yp_password.pw_passwd)) { 561 yp_error("rejected change attempt -- bad password"); 562 yp_error("client address: %s username: %s", 563 inet_ntoa(rqhost->sin_addr), 564 argp->newpw.pw_name); 565 return(&result); 566 } 567 568 /* Step 3: validate the arguments passed to us by the client. */ 569 570 if (validate(&yp_password, &argp->newpw)) { 571 yp_error("rejecting change attempt: bad arguments"); 572 yp_error("client address: %s username: %s", 573 inet_ntoa(rqhost->sin_addr), 574 argp->newpw.pw_name); 575 svcerr_decode(rqstp->rq_xprt); 576 return(&result); 577 } 578 579 /* Step 4: update the user's passwd structure. */ 580 581 if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) { 582 oldshell = yp_password.pw_shell; 583 yp_password.pw_shell = argp->newpw.pw_shell; 584 shell_changed++; 585 } 586 587 588 if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) { 589 oldgecos = yp_password.pw_gecos; 590 yp_password.pw_gecos = argp->newpw.pw_gecos; 591 gecos_changed++; 592 } 593 594 if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) { 595 yp_password.pw_passwd = argp->newpw.pw_passwd; 596 yp_password.pw_change = 0; 597 passwd_changed++; 598 } 599 600 /* 601 * If the caller specified a domain other than our 'default' 602 * domain, change the path to master.passwd accordingly. 603 */ 604 605 if (strcmp(domain, yppasswd_domain)) { 606 snprintf(passfile_buf, sizeof(passfile_buf), 607 "%s/%s/master.passwd", yp_dir, domain); 608 passfile = (char *)&passfile_buf; 609 } 610 611 /* 612 * Create a filename to hold the original master.passwd 613 * so if our call to yppwupdate fails we can roll back 614 */ 615 snprintf(passfile_hold_buf, sizeof(passfile_hold_buf), 616 "%s.hold", passfile); 617 passfile_hold = (char *)&passfile_hold_buf; 618 619 620 /* Step 5: make a new password file with the updated info. */ 621 622 if (pw_init(dirname(passfile), passfile)) { 623 yp_error("pw_init() failed"); 624 return &result; 625 } 626 if ((pfd = pw_lock()) == -1) { 627 pw_fini(); 628 yp_error("pw_lock() failed"); 629 return &result; 630 } 631 if ((tfd = pw_tmp(-1)) == -1) { 632 pw_fini(); 633 yp_error("pw_tmp() failed"); 634 return &result; 635 } 636 if (pw_copy(pfd, tfd, &yp_password, NULL) == -1) { 637 pw_fini(); 638 yp_error("pw_copy() failed"); 639 return &result; 640 } 641 if (rename(passfile, passfile_hold) == -1) { 642 pw_fini(); 643 yp_error("rename of %s to %s failed", passfile, 644 passfile_hold); 645 return &result; 646 } 647 648 if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) { 649 /* 650 * NIS server is exporting the system's master.passwd. 651 * Call pw_mkdb to rebuild passwd and the .db files 652 */ 653 if (pw_mkdb(yp_password.pw_name) == -1) { 654 pw_fini(); 655 yp_error("pw_mkdb() failed"); 656 rename(passfile_hold, passfile); 657 return &result; 658 } 659 } else { 660 /* 661 * NIS server is exporting a private master.passwd. 662 * Rename tempfile into final location 663 */ 664 if (rename(pw_tempname(), passfile) == -1) { 665 pw_fini(); 666 yp_error("rename of %s to %s failed", 667 pw_tempname(), passfile); 668 rename(passfile_hold, passfile); 669 return &result; 670 } 671 } 672 673 pw_fini(); 674 675 if (inplace) { 676 if ((rval = update_inplace(&yp_password, domain))) { 677 yp_error("inplace update failed -- rebuilding maps"); 678 } 679 } 680 681 switch ((pid = fork())) { 682 case 0: 683 if (inplace && !rval) { 684 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 685 yppasswd_domain, "pushpw", (char *)NULL); 686 } else { 687 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 688 yppasswd_domain, (char *)NULL); 689 } 690 yp_error("couldn't exec map update process: %s", 691 strerror(errno)); 692 unlink(passfile); 693 rename(passfile_hold, passfile); 694 exit(1); 695 break; 696 case -1: 697 yp_error("fork() failed: %s", strerror(errno)); 698 unlink(passfile); 699 rename(passfile_hold, passfile); 700 return(&result); 701 break; 702 default: 703 unlink(passfile_hold); 704 break; 705 } 706 707 if (verbose) { 708 yp_error("update completed for user %s (uid %d) in %s:", 709 argp->newpw.pw_name, argp->newpw.pw_uid, passfile); 710 711 if (passwd_changed) 712 yp_error("password changed"); 713 714 if (gecos_changed) 715 yp_error("gecos changed ('%s' -> '%s')", 716 oldgecos, argp->newpw.pw_gecos); 717 718 if (shell_changed) 719 yp_error("shell changed ('%s' -> '%s')", 720 oldshell, argp->newpw.pw_shell); 721 } 722 723 result = 0; 724 return (&result); 725} 726 727/* 728 * Note that this function performs a little less sanity checking 729 * than the last one. Since only the superuser is allowed to use it, 730 * it is assumed that the caller knows what he's doing. 731 */ 732int * 733yppasswdproc_update_master_1_svc(master_yppasswd *argp, 734 struct svc_req *rqstp) 735{ 736 static int result; 737 int pfd, tfd; 738 int pid; 739 uid_t uid; 740 int rval = 0; 741 DBT key, data; 742 char *passfile_hold; 743 char passfile_buf[MAXPATHLEN + 2]; 744 char passfile_hold_buf[MAXPATHLEN + 2]; 745 struct sockaddr_in *rqhost; 746 SVCXPRT *transp; 747 struct passwd newpasswd; 748 749 result = 1; 750 transp = rqstp->rq_xprt; 751 752 /* 753 * NO AF_INET CONNETCIONS ALLOWED! 754 */ 755 rqhost = svc_getcaller(transp); 756 if (rqhost->sin_family != AF_UNIX) { 757 yp_error("Alert! %s/%d attempted to use superuser-only \ 758procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port); 759 svcerr_auth(transp, AUTH_BADCRED); 760 return(&result); 761 } 762 763 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) { 764 yp_error("caller didn't send proper credentials"); 765 svcerr_auth(transp, AUTH_BADCRED); 766 return(&result); 767 } 768 769 if (__rpc_get_local_uid(transp, &uid) < 0) { 770 yp_error("caller didn't send proper credentials"); 771 svcerr_auth(transp, AUTH_BADCRED); 772 return(&result); 773 } 774 775 if (uid) { 776 yp_error("caller euid is %d, expecting 0 -- rejecting request", 777 uid); 778 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED); 779 return(&result); 780 } 781 782 passfile = passfile_default; 783 784 key.data = argp->newpw.pw_name; 785 key.size = strlen(argp->newpw.pw_name); 786 787 /* 788 * The superuser may add entries to the passwd maps if 789 * rpc.yppasswdd is started with the -a flag. Paranoia 790 * prevents me from allowing additions by default. 791 */ 792 if ((rval = yp_get_record(argp->domain, "master.passwd.byname", 793 &key, &data, 0)) != YP_TRUE) { 794 if (rval == YP_NOKEY) { 795 yp_error("user %s not found in passwd database", 796 argp->newpw.pw_name); 797 if (allow_additions) 798 yp_error("notice: adding user %s to \ 799master.passwd database for domain %s", argp->newpw.pw_name, argp->domain); 800 else 801 yp_error("restart rpc.yppasswdd with the -a flag to \ 802allow additions to be made to the password database"); 803 } else { 804 yp_error("database access error: %s", 805 yperr_string(rval)); 806 } 807 if (!allow_additions) 808 return(&result); 809 } else { 810 811 /* Nul terminate, please. */ 812 *((char *)data.data + data.size) = '\0'; 813 814 copy_yp_pass(data.data, 1, data.size); 815 } 816 817 /* 818 * Perform a small bit of sanity checking. 819 */ 820 if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){ 821 yp_error("rejecting update attempt for %s: bad arguments", 822 argp->newpw.pw_name); 823 return(&result); 824 } 825 826 /* 827 * If the caller specified a domain other than our 'default' 828 * domain, change the path to master.passwd accordingly. 829 */ 830 831 if (strcmp(argp->domain, yppasswd_domain)) { 832 snprintf(passfile_buf, sizeof(passfile_buf), 833 "%s/%s/master.passwd", yp_dir, argp->domain); 834 passfile = (char *)&passfile_buf; 835 } 836 837 /* 838 * Create a filename to hold the original master.passwd 839 * so if our call to yppwupdate fails we can roll back 840 */ 841 snprintf(passfile_hold_buf, sizeof(passfile_hold_buf), 842 "%s.hold", passfile); 843 passfile_hold = (char *)&passfile_hold_buf; 844 845 if (pw_init(dirname(passfile), passfile)) { 846 yp_error("pw_init() failed"); 847 return &result; 848 } 849 if ((pfd = pw_lock()) == -1) { 850 pw_fini(); 851 yp_error("pw_lock() failed"); 852 return &result; 853 } 854 if ((tfd = pw_tmp(-1)) == -1) { 855 pw_fini(); 856 yp_error("pw_tmp() failed"); 857 return &result; 858 } 859 xlate_passwd(&argp->newpw, &newpasswd); 860 if (pw_copy(pfd, tfd, &newpasswd, NULL) == -1) { 861 pw_fini(); 862 yp_error("pw_copy() failed"); 863 return &result; 864 } 865 if (rename(passfile, passfile_hold) == -1) { 866 pw_fini(); 867 yp_error("rename of %s to %s failed", passfile, 868 passfile_hold); 869 return &result; 870 } 871 if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) { 872 /* 873 * NIS server is exporting the system's master.passwd. 874 * Call pw_mkdb to rebuild passwd and the .db files 875 */ 876 if (pw_mkdb(argp->newpw.pw_name) == -1) { 877 pw_fini(); 878 yp_error("pw_mkdb() failed"); 879 rename(passfile_hold, passfile); 880 return &result; 881 } 882 } else { 883 /* 884 * NIS server is exporting a private master.passwd. 885 * Rename tempfile into final location 886 */ 887 if (rename(pw_tempname(), passfile) == -1) { 888 pw_fini(); 889 yp_error("rename of %s to %s failed", 890 pw_tempname(), passfile); 891 rename(passfile_hold, passfile); 892 return &result; 893 } 894 } 895 pw_fini(); 896 897 if (inplace) { 898 xlate_passwd(&argp->newpw, &newpasswd); 899 if ((rval = update_inplace(&newpasswd, argp->domain))) { 900 yp_error("inplace update failed -- rebuilding maps"); 901 } 902 } 903 904 switch ((pid = fork())) { 905 case 0: 906 if (inplace && !rval) { 907 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 908 argp->domain, "pushpw", (char *)NULL); 909 } else { 910 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 911 argp->domain, (char *)NULL); 912 } 913 yp_error("couldn't exec map update process: %s", 914 strerror(errno)); 915 unlink(passfile); 916 rename(passfile_hold, passfile); 917 exit(1); 918 break; 919 case -1: 920 yp_error("fork() failed: %s", strerror(errno)); 921 unlink(passfile); 922 rename(passfile_hold, passfile); 923 return(&result); 924 break; 925 default: 926 unlink(passfile_hold); 927 break; 928 } 929 930 yp_error("performed update of user %s (uid %d) domain %s", 931 argp->newpw.pw_name, 932 argp->newpw.pw_uid, 933 argp->domain); 934 935 result = 0; 936 return(&result); 937} 938