yppasswdd_server.c revision 17431
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 * $Id: yppasswdd_server.c,v 1.6 1996/07/01 19:38:38 guido Exp $ 33 */ 34 35#include <stdio.h> 36#include <string.h> 37#include <ctype.h> 38#include <stdlib.h> 39#include <unistd.h> 40#include <dirent.h> 41#include <sys/stat.h> 42#include <sys/socket.h> 43#include <netinet/in.h> 44#include <arpa/inet.h> 45#include <limits.h> 46#include <db.h> 47#include <pwd.h> 48#include <errno.h> 49#include <signal.h> 50#include <rpc/rpc.h> 51#include <rpcsvc/yp.h> 52#include <sys/types.h> 53#include <sys/wait.h> 54#include <sys/param.h> 55#include <sys/fcntl.h> 56struct dom_binding {}; 57#include <rpcsvc/ypclnt.h> 58#include "yppasswdd_extern.h" 59#include "yppasswd.h" 60#include "yppasswd_private.h" 61#include "yppasswd_comm.h" 62 63#ifndef lint 64static const char rcsid[] = "$Id: yppasswdd_server.c,v 1.6 1996/07/01 19:38:38 guido Exp $"; 65#endif /* not lint */ 66 67char *tempname; 68 69void reaper(sig) 70 int sig; 71{ 72 extern pid_t pid; 73 extern int pstat; 74 int st; 75 76 if (sig > 0) { 77 if (sig == SIGCHLD) 78 while(wait3(&st, WNOHANG, NULL) > 0) ; 79 } else { 80 pid = waitpid(pid, &pstat, 0); 81 } 82 return; 83} 84 85void install_reaper(on) 86 int on; 87{ 88 if (on) { 89 signal(SIGCHLD, reaper); 90 } else { 91 signal(SIGCHLD, SIG_DFL); 92 } 93 return; 94} 95 96static struct passwd yp_password; 97 98static void copy_yp_pass(p, x, m) 99char *p; 100int x, m; 101{ 102 register char *t, *s = p; 103 static char *buf; 104 105 yp_password.pw_fields = 0; 106 107 buf = (char *)realloc(buf, m + 10); 108 bzero(buf, m + 10); 109 110 /* Turn all colons into NULLs */ 111 while (strchr(s, ':')) { 112 s = (strchr(s, ':') + 1); 113 *(s - 1)= '\0'; 114 } 115 116 t = buf; 117#define EXPAND(e) e = t; while ((*t++ = *p++)); 118 EXPAND(yp_password.pw_name); 119 yp_password.pw_fields |= _PWF_NAME; 120 EXPAND(yp_password.pw_passwd); 121 yp_password.pw_fields |= _PWF_PASSWD; 122 yp_password.pw_uid = atoi(p); 123 p += (strlen(p) + 1); 124 yp_password.pw_fields |= _PWF_UID; 125 yp_password.pw_gid = atoi(p); 126 p += (strlen(p) + 1); 127 yp_password.pw_fields |= _PWF_GID; 128 if (x) { 129 EXPAND(yp_password.pw_class); 130 yp_password.pw_fields |= _PWF_CLASS; 131 yp_password.pw_change = atol(p); 132 p += (strlen(p) + 1); 133 yp_password.pw_fields |= _PWF_CHANGE; 134 yp_password.pw_expire = atol(p); 135 p += (strlen(p) + 1); 136 yp_password.pw_fields |= _PWF_EXPIRE; 137 } 138 EXPAND(yp_password.pw_gecos); 139 yp_password.pw_fields |= _PWF_GECOS; 140 EXPAND(yp_password.pw_dir); 141 yp_password.pw_fields |= _PWF_DIR; 142 EXPAND(yp_password.pw_shell); 143 yp_password.pw_fields |= _PWF_SHELL; 144 145 return; 146} 147 148static int validchars(arg) 149 char *arg; 150{ 151 int i; 152 153 for (i = 0; i < strlen(arg); i++) { 154 if (iscntrl(arg[i])) { 155 yp_error("string contains a control character"); 156 return(1); 157 } 158 if (arg[i] == ':') { 159 yp_error("string contains a colon"); 160 return(1); 161 } 162 /* Be evil: truncate strings with \n in them silently. */ 163 if (arg[i] == '\n') { 164 arg[i] = '\0'; 165 return(0); 166 } 167 } 168 return(0); 169} 170 171static int validate_master(opw, npw) 172 struct passwd *opw; 173 struct x_master_passwd *npw; 174{ 175 176 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') { 177 yp_error("client tried to modify an NIS entry"); 178 return(1); 179 } 180 181 if (validchars(npw->pw_shell)) { 182 yp_error("specified shell contains invalid characters"); 183 return(1); 184 } 185 186 if (validchars(npw->pw_gecos)) { 187 yp_error("specified gecos field contains invalid characters"); 188 return(1); 189 } 190 191 if (validchars(npw->pw_passwd)) { 192 yp_error("specified password contains invalid characters"); 193 return(1); 194 } 195 return(0); 196} 197 198static int validate(opw, npw) 199 struct passwd *opw; 200 struct x_passwd *npw; 201{ 202 203 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') { 204 yp_error("client tried to modify an NIS entry"); 205 return(1); 206 } 207 208 if (npw->pw_uid != opw->pw_uid) { 209 yp_error("UID mismatch: client says user %s has UID %d", 210 npw->pw_name, npw->pw_uid); 211 yp_error("database says user %s has UID %d", opw->pw_name, 212 opw->pw_uid); 213 return(1); 214 } 215 216 if (npw->pw_gid != opw->pw_gid) { 217 yp_error("GID mismatch: client says user %s has GID %d", 218 npw->pw_name, npw->pw_gid); 219 yp_error("database says user %s has GID %d", opw->pw_name, 220 opw->pw_gid); 221 return(1); 222 } 223 224 /* 225 * Don't allow the user to shoot himself in the foot, 226 * even on purpose. 227 */ 228 if (!ok_shell(npw->pw_shell)) { 229 yp_error("%s is not a valid shell", npw->pw_shell); 230 return(1); 231 } 232 233 if (validchars(npw->pw_shell)) { 234 yp_error("specified shell contains invalid characters"); 235 return(1); 236 } 237 238 if (validchars(npw->pw_gecos)) { 239 yp_error("specified gecos field contains invalid characters"); 240 return(1); 241 } 242 243 if (validchars(npw->pw_passwd)) { 244 yp_error("specified password contains invalid characters"); 245 return(1); 246 } 247 return(0); 248} 249 250/* 251 * Kludge alert: 252 * In order to have one rpc.yppasswdd support multiple domains, 253 * we have to cheat: we search each directory under /var/yp 254 * and try to match the user in each master.passwd.byname 255 * map that we find. If the user matches (username, uid and gid 256 * all agree), then we use that domain. If we match the user in 257 * more than one database, we must abort. 258 */ 259static char *find_domain(pw) 260 struct x_passwd *pw; 261{ 262 struct stat statbuf; 263 struct dirent *dirp; 264 DIR *dird; 265 char yp_mapdir[MAXPATHLEN + 2]; 266 static char domain[YPMAXDOMAIN]; 267 char *tmp = NULL; 268 DBT key, data; 269 int hit = 0; 270 271 yp_error("performing multidomain lookup"); 272 273 if ((dird = opendir(yp_dir)) == NULL) { 274 yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno)); 275 return(NULL); 276 } 277 278 while ((dirp = readdir(dird)) != NULL) { 279 snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", 280 yp_dir, dirp->d_name); 281 if (stat(yp_mapdir, &statbuf) < 0) { 282 yp_error("stat(%s) failed: %s", yp_mapdir, 283 strerror(errno)); 284 closedir(dird); 285 return(NULL); 286 } 287 if (S_ISDIR(statbuf.st_mode)) { 288 tmp = (char *)dirp->d_name; 289 key.data = pw->pw_name; 290 key.size = strlen(pw->pw_name); 291 292 if (yp_get_record(tmp,"master.passwd.byname", 293 &key, &data, 0) != YP_TRUE) { 294 continue; 295 } 296 *(char *)(data.data + data.size) = '\0'; 297 copy_yp_pass(data.data, 1, data.size); 298 if (yp_password.pw_uid == pw->pw_uid && 299 yp_password.pw_gid == pw->pw_gid) { 300 hit++; 301 snprintf(domain, YPMAXDOMAIN, "%s", tmp); 302 } 303 } 304 } 305 306 closedir(dird); 307 if (hit > 1) { 308 yp_error("found same user in two different domains"); 309 return(NULL); 310 } else 311 return((char *)&domain); 312} 313 314static int update_inplace(pw, domain) 315 struct passwd *pw; 316 char *domain; 317{ 318 DB *dbp = NULL; 319 DBT key = { NULL, 0 }; 320 DBT data = { NULL, 0 }; 321 char pwbuf[YPMAXRECORD]; 322 char keybuf[20]; 323 int rval, i; 324 char *maps[] = { "master.passwd.byname", "master.passwd.byuid", 325 "passwd.byname", "passwd.byuid" }; 326 327 char *formats[] = { "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 328 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", 329 "%s:%s:%d:%d:%s:%s:%s", "%s:%s:%d:%d:%s:%s:%s" }; 330 char *ptr = NULL; 331 char *yp_last = "YP_LAST_MODIFIED"; 332 char yplastbuf[YPMAXRECORD]; 333 334 snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL)); 335 336 for (i = 0; i < 4; i++) { 337 338 if (i % 2) { 339 snprintf(keybuf, sizeof(keybuf), "%ld", pw->pw_uid); 340 key.data = (char *)&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 return(1); 367 } 368 369 if ((ptr = strchr(data.data, ':')) == NULL) { 370 yp_error("no colon in passwd record?!"); 371 return(1); 372 } 373 374 if (i < 2) { 375 snprintf(pwbuf, sizeof(pwbuf), formats[i], 376 pw->pw_name, pw->pw_passwd, pw->pw_uid, 377 pw->pw_gid, pw->pw_class, pw->pw_change, 378 pw->pw_expire, pw->pw_gecos, pw->pw_dir, 379 pw->pw_shell); 380 } else { 381 snprintf(pwbuf, sizeof(pwbuf), formats[i], 382 pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd, 383 pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir, 384 pw->pw_shell); 385 } 386 387#define FLAGS O_RDWR|O_CREAT 388 389 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) { 390 yp_error("couldn't open %s/%s r/w: %s",domain, 391 maps[i],strerror(errno)); 392 return(1); 393 } 394 395 data.data = pwbuf; 396 data.size = strlen(pwbuf); 397 398 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) { 399 yp_error("failed to update record in %s/%s", domain, 400 maps[i]); 401 (void)(dbp->close)(dbp); 402 return(1); 403 } 404 405 key.data = yp_last; 406 key.size = strlen(yp_last); 407 data.data = (char *)&yplastbuf; 408 data.size = strlen(yplastbuf); 409 410 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) { 411 yp_error("failed to update timestamp in %s/%s", domain, 412 maps[i]); 413 (void)(dbp->close)(dbp); 414 return(1); 415 } 416 417 (void)(dbp->close)(dbp); 418 } 419 420 return(0); 421} 422 423static char *yp_mktmpnam() 424{ 425 static char path[MAXPATHLEN]; 426 char *p; 427 428 sprintf(path,"%s",passfile); 429 if ((p = strrchr(path, '/'))) 430 ++p; 431 else 432 p = path; 433 strcpy(p, "yppwtmp.XXXXXX"); 434 return(mktemp(path)); 435} 436 437int * 438yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) 439{ 440 static int result; 441 struct sockaddr_in *rqhost; 442 DBT key, data; 443 int rval = 0; 444 int pfd, tfd; 445 int pid; 446 int passwd_changed = 0; 447 int shell_changed = 0; 448 int gecos_changed = 0; 449 char *oldshell = NULL; 450 char *oldgecos = NULL; 451 char *passfile_hold; 452 char passfile_buf[MAXPATHLEN + 2]; 453 char *domain = yppasswd_domain; 454 static struct sockaddr_in clntaddr; 455 static struct timeval t_saved, t_test; 456 457 /* 458 * Normal user updates always use the 'default' master.passwd file. 459 */ 460 461 passfile = passfile_default; 462 result = 1; 463 464 rqhost = svc_getcaller(rqstp->rq_xprt); 465 466 gettimeofday(&t_test, NULL); 467 if (!bcmp((char *)rqhost, (char *)&clntaddr, 468 sizeof(struct sockaddr_in)) && 469 t_test.tv_sec > t_saved.tv_sec && 470 t_test.tv_sec - t_saved.tv_sec < 300) { 471 472 bzero((char *)&clntaddr, sizeof(struct sockaddr_in)); 473 bzero((char *)&t_saved, sizeof(struct timeval)); 474 return(NULL); 475 } 476 477 bcopy((char *)rqhost, (char *)&clntaddr, sizeof(struct sockaddr_in)); 478 gettimeofday(&t_saved, NULL); 479 480 if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) { 481 yp_error("rejected update request from unauthorized host"); 482 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED); 483 return(&result); 484 } 485 486 /* 487 * Step one: find the user. (It's kinda pointless to 488 * proceed if the user doesn't exist.) We look for the 489 * user in the master.passwd.byname database, _NOT_ by 490 * using getpwent() and friends! We can't use getpwent() 491 * since the NIS master server is not guaranteed to be 492 * configured as an NIS client. 493 */ 494 495 if (multidomain) { 496 if ((domain = find_domain(&argp->newpw)) == NULL) { 497 yp_error("multidomain lookup failed - aborting update"); 498 return(&result); 499 } else 500 yp_error("updating user %s in domain %s", 501 argp->newpw.pw_name, domain); 502 } 503 504 key.data = argp->newpw.pw_name; 505 key.size = strlen(argp->newpw.pw_name); 506 507 if ((rval=yp_get_record(domain,"master.passwd.byname", 508 &key, &data, 0)) != YP_TRUE) { 509 if (rval == YP_NOKEY) { 510 yp_error("user %s not found in passwd database", 511 argp->newpw.pw_name); 512 } else { 513 yp_error("database access error: %s", 514 yperr_string(rval)); 515 } 516 return(&result); 517 } 518 519 /* Nul terminate, please. */ 520 *(char *)(data.data + data.size) = '\0'; 521 522 copy_yp_pass(data.data, 1, data.size); 523 524 /* Step 2: check that the supplied oldpass is valid. */ 525 526 if (strcmp(crypt(argp->oldpass, yp_password.pw_passwd), 527 yp_password.pw_passwd)) { 528 yp_error("rejected change attempt -- bad password"); 529 yp_error("client address: %s username: %s", 530 inet_ntoa(rqhost->sin_addr), 531 argp->newpw.pw_name); 532 return(&result); 533 } 534 535 /* Step 3: validate the arguments passed to us by the client. */ 536 537 if (validate(&yp_password, &argp->newpw)) { 538 yp_error("rejecting change attempt: bad arguments"); 539 yp_error("client address: %s username: %s", 540 inet_ntoa(rqhost->sin_addr), 541 argp->newpw.pw_name); 542 svcerr_decode(rqstp->rq_xprt); 543 return(&result); 544 } 545 546 /* Step 4: update the user's passwd structure. */ 547 548 if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) { 549 oldshell = yp_password.pw_shell; 550 yp_password.pw_shell = argp->newpw.pw_shell; 551 shell_changed++; 552 } 553 554 555 if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) { 556 oldgecos = yp_password.pw_gecos; 557 yp_password.pw_gecos = argp->newpw.pw_gecos; 558 gecos_changed++; 559 } 560 561 if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) { 562 yp_password.pw_passwd = argp->newpw.pw_passwd; 563 passwd_changed++; 564 } 565 566 /* 567 * If the caller specified a domain other than our 'default' 568 * domain, change the path to master.passwd accordingly. 569 */ 570 571 if (strcmp(domain, yppasswd_domain)) { 572 snprintf(passfile_buf, sizeof(passfile_buf), 573 "%s/%s/master.passwd", yp_dir, domain); 574 passfile = (char *)&passfile_buf; 575 } 576 577 /* Step 5: make a new password file with the updated info. */ 578 579 if ((pfd = pw_lock()) < 0) { 580 return (&result); 581 } 582 if ((tfd = pw_tmp()) < 0) { 583 return (&result); 584 } 585 586 if (pw_copy(pfd, tfd, &yp_password)) { 587 yp_error("failed to created updated password file -- \ 588cleaning up and bailing out"); 589 unlink(tempname); 590 return(&result); 591 } 592 593 passfile_hold = yp_mktmpnam(); 594 rename(passfile, passfile_hold); 595 if (strcmp(passfile, _PATH_MASTERPASSWD)) { 596 rename(tempname, passfile); 597 } else { 598 if (pw_mkdb(argp->newpw.pw_name) < 0) { 599 yp_error("pwd_mkdb failed"); 600 return(&result); 601 } 602 } 603 604 if (inplace) { 605 if ((rval = update_inplace(&yp_password, domain))) { 606 yp_error("inplace update failed -- rebuilding maps"); 607 } 608 } 609 610 switch((pid = fork())) { 611 case 0: 612 if (inplace && !rval) { 613 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 614 yppasswd_domain, "pushpw", NULL); 615 } else { 616 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 617 yppasswd_domain, NULL); 618 } 619 yp_error("couldn't exec map update process: %s", 620 strerror(errno)); 621 unlink(passfile); 622 rename(passfile_hold, passfile); 623 exit(1); 624 break; 625 case -1: 626 yp_error("fork() failed: %s", strerror(errno)); 627 unlink(passfile); 628 rename(passfile_hold, passfile); 629 return(&result); 630 break; 631 default: 632 unlink(passfile_hold); 633 break; 634 } 635 636 if (verbose) { 637 yp_error("update completed for user %s (uid %d):", 638 argp->newpw.pw_name, 639 argp->newpw.pw_uid); 640 641 if (passwd_changed) 642 yp_error("password changed"); 643 644 if (gecos_changed) 645 yp_error("gecos changed ('%s' -> '%s')", 646 oldgecos, argp->newpw.pw_gecos); 647 648 if (shell_changed) 649 yp_error("shell changed ('%s' -> '%s')", 650 oldshell, argp->newpw.pw_shell); 651 } 652 653 result = 0; 654 return (&result); 655} 656 657/* 658 * Note that this function performs a little less sanity checking 659 * than the last one. Since only the superuser is allowed to use it, 660 * it is assumed that the caller knows what he's doing. 661 */ 662static int update_master(master_yppasswd *argp) 663{ 664 int result; 665 int pfd, tfd; 666 int pid; 667 int rval = 0; 668 DBT key, data; 669 char *passfile_hold; 670 char passfile_buf[MAXPATHLEN + 2]; 671 672 result = 1; 673 passfile = passfile_default; 674 675 key.data = argp->newpw.pw_name; 676 key.size = strlen(argp->newpw.pw_name); 677 678 /* 679 * The superuser may add entries to the passwd maps if 680 * rpc.yppasswdd is started with the -a flag. Paranoia 681 * prevents me from allowing additions by default. 682 */ 683 if ((rval = yp_get_record(argp->domain, "master.passwd.byname", 684 &key, &data, 0)) != YP_TRUE) { 685 if (rval == YP_NOKEY) { 686 yp_error("user %s not found in passwd database", 687 argp->newpw.pw_name); 688 if (allow_additions) 689 yp_error("notice: adding user %s to \ 690master.passwd database for domain %s", argp->newpw.pw_name, argp->domain); 691 else 692 yp_error("restart %s with the -a flag to \ 693allow additions to be made to the password database", progname); 694 } else { 695 yp_error("database access error: %s", 696 yperr_string(rval)); 697 } 698 if (!allow_additions) 699 return(result); 700 } else { 701 702 /* Nul terminate, please. */ 703 *(char *)(data.data + data.size) = '\0'; 704 705 copy_yp_pass(data.data, 1, data.size); 706 } 707 708 /* 709 * Perform a small bit of sanity checking. 710 */ 711 if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){ 712 yp_error("rejecting update attempt for %s: bad arguments", 713 argp->newpw.pw_name); 714 return(result); 715 } 716 717 /* 718 * If the caller specified a domain other than our 'default' 719 * domain, change the path to master.passwd accordingly. 720 */ 721 722 if (strcmp(argp->domain, yppasswd_domain)) { 723 snprintf(passfile_buf, sizeof(passfile_buf), 724 "%s/%s/master.passwd", yp_dir, argp->domain); 725 passfile = (char *)&passfile_buf; 726 } 727 728 if ((pfd = pw_lock()) < 0) { 729 return (result); 730 } 731 if ((tfd = pw_tmp()) < 0) { 732 return (result); 733 } 734 735 if (pw_copy(pfd, tfd, (struct passwd *)&argp->newpw)) { 736 yp_error("failed to created updated password file -- \ 737cleaning up and bailing out"); 738 unlink(tempname); 739 return(result); 740 } 741 742 passfile_hold = yp_mktmpnam(); 743 rename(passfile, passfile_hold); 744 if (strcmp(passfile, _PATH_MASTERPASSWD)) { 745 rename(tempname, passfile); 746 } else { 747 if (pw_mkdb(argp->newpw.pw_name) < 0) { 748 yp_error("pwd_mkdb failed"); 749 return(result); 750 } 751 } 752 753 if (inplace) { 754 if ((rval = update_inplace((struct passwd *)&argp->newpw, 755 argp->domain))) { 756 yp_error("inplace update failed -- rebuilding maps"); 757 } 758 } 759 760 switch((pid = fork())) { 761 case 0: 762 close(yp_sock); 763 if (inplace && !rval) { 764 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 765 argp->domain, "pushpw", NULL); 766 } else { 767 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile, 768 argp->domain, NULL); 769 } 770 yp_error("couldn't exec map update process: %s", 771 strerror(errno)); 772 unlink(passfile); 773 rename(passfile_hold, passfile); 774 exit(1); 775 break; 776 case -1: 777 yp_error("fork() failed: %s", strerror(errno)); 778 unlink(passfile); 779 rename(passfile_hold, passfile); 780 return(result); 781 break; 782 default: 783 unlink(passfile_hold); 784 break; 785 } 786 787 yp_error("performed update of user %s (uid %d) domain %s", 788 argp->newpw.pw_name, 789 argp->newpw.pw_uid, 790 argp->domain); 791 792 result = 0; 793 return(result); 794} 795 796/* 797 * Pseudo-dispatcher for private 'superuser-only' update handler. 798 */ 799void do_master() 800{ 801 struct master_yppasswd *pw; 802 803 if ((pw = getdat(yp_sock)) == NULL) { 804 return; 805 } 806 807 yp_error("received update request from superuser on localhost"); 808 sendresp(update_master(pw)); 809 810 /* Remember to free args. */ 811 xdr_free(xdr_master_yppasswd, (char *)pw); 812 813 return; 814} 815