ypxfr_main.c revision 30008
1/* 2 * Copyright (c) 1995 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: ypxfr_main.c,v 1.9 1997/03/28 15:48:21 imp Exp $ 33 */ 34#include <stdio.h> 35#include <stdlib.h> 36#include <unistd.h> 37#include <string.h> 38#include <syslog.h> 39#include <errno.h> 40#include <sys/types.h> 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <netinet/in.h> 44#include <arpa/inet.h> 45#include <rpc/rpc.h> 46#include <rpc/clnt.h> 47#include <rpcsvc/yp.h> 48struct dom_binding {}; 49#include <rpcsvc/ypclnt.h> 50#include <rpcsvc/ypxfrd.h> 51#include "ypxfr_extern.h" 52 53#ifndef lint 54static const char rcsid[] = "$Id: ypxfr_main.c,v 1.9 1997/03/28 15:48:21 imp Exp $"; 55#endif 56 57char *progname = "ypxfr"; 58char *yp_dir = _PATH_YP; 59int _rpcpmstart = 0; 60int ypxfr_use_yplib = 0; /* Assume the worst. */ 61int ypxfr_clear = 1; 62int ypxfr_prognum = 0; 63struct sockaddr_in ypxfr_callback_addr; 64struct yppushresp_xfr ypxfr_resp; 65DB *dbp; 66 67static void ypxfr_exit(retval, temp) 68 ypxfrstat retval; 69 char *temp; 70{ 71 CLIENT *clnt; 72 int sock = RPC_ANYSOCK; 73 struct timeval timeout; 74 75 /* Clean up no matter what happened previously. */ 76 if (temp != NULL) { 77 if (dbp != NULL) 78 (void)(dbp->close)(dbp); 79 if (unlink(temp) == -1) { 80 yp_error("failed to unlink %s",strerror(errno)); 81 } 82 } 83 84 if (_rpcpmstart) { 85 timeout.tv_sec = 20; 86 timeout.tv_usec = 0; 87 88 if ((clnt = clntudp_create(&ypxfr_callback_addr, ypxfr_prognum, 89 1, timeout, &sock)) == NULL) { 90 yp_error("%s", clnt_spcreateerror("failed to \ 91establish callback handle")); 92 exit(1); 93 } 94 95 ypxfr_resp.status = retval; 96 97 if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) { 98 yp_error("%s", clnt_sperror(clnt, "callback failed")); 99 clnt_destroy(clnt); 100 exit(1); 101 } 102 clnt_destroy(clnt); 103 } else { 104 yp_error("Exiting: %s", ypxfrerr_string(retval)); 105 } 106 107 exit(0); 108} 109 110static void usage() 111{ 112 if (_rpcpmstart) { 113 ypxfr_exit(YPXFR_BADARGS,NULL); 114 } else { 115 fprintf(stderr,"usage: %s [-f] [-c] [-d target domain] \ 116[-h source host] [-s source domain]\n", progname); 117 fprintf(stderr,"\t [-p path] [-C taskid program-number \ 118ipaddr port] mapname\n"); 119 exit(1); 120 } 121} 122 123int ypxfr_foreach(status, key, keylen, val, vallen, data) 124 int status; 125 char *key; 126 int keylen; 127 char *val; 128 int vallen; 129 char *data; 130{ 131 DBT dbkey, dbval; 132 133 if (status != YP_TRUE) 134 return (status); 135 136 /* 137 * XXX Do not attempt to write zero-length keys or 138 * data into a Berkeley DB hash database. It causes a 139 * strange failure mode where sequential searches get 140 * caught in an infinite loop. 141 */ 142 if (keylen) { 143 dbkey.data = key; 144 dbkey.size = keylen; 145 } else { 146 dbkey.data = ""; 147 dbkey.size = 1; 148 } 149 if (vallen) { 150 dbval.data = val; 151 dbval.size = vallen; 152 } else { 153 dbval.data = ""; 154 dbval.size = 1; 155 } 156 157 if (yp_put_record(dbp, &dbkey, &dbval, 0) != YP_TRUE) 158 return(yp_errno); 159 160 return (0); 161} 162 163main(argc,argv) 164 int argc; 165 char *argv[]; 166{ 167 int ch; 168 int ypxfr_force = 0; 169 char *ypxfr_dest_domain = NULL; 170 char *ypxfr_source_host = NULL; 171 char *ypxfr_source_domain = NULL; 172 char *ypxfr_local_domain = NULL; 173 char *ypxfr_master = NULL; 174 unsigned long ypxfr_order = -1, ypxfr_skew_check = -1; 175 char *ypxfr_mapname = NULL; 176 int ypxfr_args = 0; 177 char ypxfr_temp_map[MAXPATHLEN + 2]; 178 char tempmap[MAXPATHLEN + 2]; 179 char buf[MAXPATHLEN + 2]; 180 DBT key, data; 181 int remoteport; 182 int interdom = 0; 183 int secure = 0; 184 185 debug = 1; 186 187 if (!isatty(fileno(stderr))) { 188 openlog(progname, LOG_PID, LOG_DAEMON); 189 _rpcpmstart = 1; 190 } 191 192 if (argc < 2) 193 usage(); 194 195 while ((ch = getopt(argc, argv, "fcd:h:s:p:C:")) != -1) { 196 int my_optind; 197 switch(ch) { 198 case 'f': 199 ypxfr_force++; 200 ypxfr_args++; 201 break; 202 case 'c': 203 ypxfr_clear = 0; 204 ypxfr_args++; 205 break; 206 case 'd': 207 ypxfr_dest_domain = optarg; 208 ypxfr_args += 2; 209 break; 210 case 'h': 211 ypxfr_source_host = optarg; 212 ypxfr_args += 2; 213 break; 214 case 's': 215 ypxfr_source_domain = optarg; 216 ypxfr_args += 2; 217 break; 218 case 'p': 219 yp_dir = optarg; 220 ypxfr_args += 2; 221 break; 222 case 'C': 223 /* 224 * Whoever decided that the -C flag should take 225 * four arguments is a twit. 226 */ 227 my_optind = optind - 1; 228 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 229 yp_error("transaction ID not specified"); 230 usage(); 231 } 232 ypxfr_resp.transid = atol(argv[my_optind]); 233 my_optind++; 234 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 235 yp_error("RPC program number not specified"); 236 usage(); 237 } 238 ypxfr_prognum = atol(argv[my_optind]); 239 my_optind++; 240 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 241 yp_error("address not specified"); 242 usage(); 243 } 244 if (!inet_aton(argv[my_optind], &ypxfr_callback_addr.sin_addr)) { 245 yp_error("failed to convert '%s' to IP addr", 246 argv[my_optind]); 247 exit(1); 248 } 249 my_optind++; 250 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 251 yp_error("port not specified"); 252 usage(); 253 } 254 ypxfr_callback_addr.sin_port = htons((u_short)atoi(argv[my_optind])); 255 ypxfr_args += 5; 256 break; 257 default: 258 usage(); 259 break; 260 } 261 } 262 263 ypxfr_mapname = argv[ypxfr_args + 1]; 264 265 if (ypxfr_mapname == NULL) { 266 yp_error("no map name specified"); 267 usage(); 268 } 269 270 /* Always the case. */ 271 ypxfr_callback_addr.sin_family = AF_INET; 272 273 /* Determine if local NIS client facilities are turned on. */ 274 if (!yp_get_default_domain(&ypxfr_local_domain) && 275 _yp_check(&ypxfr_local_domain)) 276 ypxfr_use_yplib = 1; 277 278 /* 279 * If no destination domain is specified, assume that the 280 * local default domain is to be used and try to obtain it. 281 * Fails if NIS client facilities are turned off. 282 */ 283 if (ypxfr_dest_domain == NULL) { 284 if (ypxfr_use_yplib) { 285 yp_get_default_domain(&ypxfr_dest_domain); 286 } else { 287 yp_error("no destination domain specified and \ 288the local domain name isn't set"); 289 ypxfr_exit(YPXFR_BADARGS,NULL); 290 } 291 } 292 293 /* 294 * If a source domain is not specified, assume it to 295 * be the same as the destination domain. 296 */ 297 if (ypxfr_source_domain == NULL) { 298 ypxfr_source_domain = ypxfr_dest_domain; 299 } 300 301 /* 302 * If the source host is not specified, assume it to be the 303 * master for the specified map. If local NIS client facilities 304 * are turned on, we can figure this out using yp_master(). 305 * If not, we have to see if a local copy of the map exists 306 * and extract its YP_MASTER_NAME record. If _that_ fails, 307 * we are stuck and must ask the user for more information. 308 */ 309 if (ypxfr_source_host == NULL) { 310 if (!ypxfr_use_yplib) { 311 /* 312 * Double whammy: NIS isn't turned on and the user 313 * didn't specify a source host. 314 */ 315 char *dptr; 316 key.data = "YP_MASTER_NAME"; 317 key.size = sizeof("YP_MASTER_NAME") - 1; 318 319 if (yp_get_record(ypxfr_dest_domain, ypxfr_mapname, 320 &key, &data, 1) != YP_TRUE) { 321 yp_error("no source host specified"); 322 ypxfr_exit(YPXFR_BADARGS,NULL); 323 } 324 dptr = data.data; 325 dptr[data.size] = '\0'; 326 ypxfr_master = ypxfr_source_host = strdup(dptr); 327 } 328 } else { 329 if (ypxfr_use_yplib) 330 ypxfr_use_yplib = 0; 331 } 332 333 if (ypxfr_master == NULL) { 334 if ((ypxfr_master = ypxfr_get_master(ypxfr_source_domain, 335 ypxfr_mapname, 336 ypxfr_source_host, 337 ypxfr_use_yplib)) == NULL) { 338 yp_error("failed to find master of %s in domain %s: %s", 339 ypxfr_mapname, ypxfr_source_domain, 340 ypxfrerr_string(yp_errno)); 341 ypxfr_exit(YPXFR_MADDR,NULL); 342 } 343 } 344 345 /* 346 * If we got here and ypxfr_source_host is still undefined, 347 * it means we had to resort to using yp_master() to find the 348 * master server for the map. The source host and master should 349 * be identical. 350 */ 351 if (ypxfr_source_host == NULL) 352 ypxfr_source_host = ypxfr_master; 353 354 /* 355 * Don't talk to ypservs on unprivileged ports. 356 */ 357 remoteport = getrpcport(ypxfr_source_host, YPPROG, YPVERS, IPPROTO_UDP); 358 if (remoteport >= IPPORT_RESERVED) { 359 yp_error("ypserv on %s not running on reserved port", 360 ypxfr_source_host); 361 ypxfr_exit(YPXFR_REFUSED, NULL); 362 } 363 364 if ((ypxfr_order = ypxfr_get_order(ypxfr_source_domain, 365 ypxfr_mapname, 366 ypxfr_master, 0)) == 0) { 367 yp_error("failed to get order number of %s: %s", 368 ypxfr_mapname, yp_errno == YPXFR_SUCC ? 369 "map has order 0" : ypxfrerr_string(yp_errno)); 370 ypxfr_exit(YPXFR_YPERR,NULL); 371 } 372 373 if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname, 374 "YP_INTERDOMAIN", sizeof("YP_INTERDOMAIN") - 1)) 375 interdom++; 376 377 if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname, 378 "YP_SECURE", sizeof("YP_SECURE") - 1)) 379 secure++; 380 381 key.data = "YP_LAST_MODIFIED"; 382 key.size = sizeof("YP_LAST_MODIFIED") - 1; 383 384 /* The order number is immaterial when the 'force' flag is set. */ 385 386 if (!ypxfr_force) { 387 int ignore = 0; 388 if (yp_get_record(ypxfr_dest_domain,ypxfr_mapname,&key,&data,1) != YP_TRUE) { 389 switch(yp_errno) { 390 case YP_NOKEY: 391 ypxfr_exit(YPXFR_FORCE,NULL); 392 break; 393 case YP_NOMAP: 394 /* 395 * If the map doesn't exist, we're 396 * creating it. Ignore the error. 397 */ 398 ignore++; 399 break; 400 case YP_BADDB: 401 default: 402 ypxfr_exit(YPXFR_DBM,NULL); 403 break; 404 } 405 } 406 if (!ignore && ypxfr_order <= atoi(data.data)) 407 ypxfr_exit(YPXFR_AGE, NULL); 408 409 } 410 411 /* Construct a temporary map file name */ 412 snprintf(tempmap, sizeof(tempmap), "%s.%d",ypxfr_mapname, getpid()); 413 snprintf(ypxfr_temp_map, sizeof(ypxfr_temp_map), "%s/%s/%s", yp_dir, 414 ypxfr_dest_domain, tempmap); 415 416 if ((remoteport = getrpcport(ypxfr_source_host, YPXFRD_FREEBSD_PROG, 417 YPXFRD_FREEBSD_VERS, IPPROTO_TCP))) { 418 419 /* Don't talk to rpc.ypxfrds on unprovileged ports. */ 420 if (remoteport >= IPPORT_RESERVED) { 421 yp_error("rpc.ypxfrd on %s not using privileged port", 422 ypxfr_source_host); 423 ypxfr_exit(YPXFR_REFUSED, NULL); 424 } 425 426 /* Try to send using ypxfrd. If it fails, use old method. */ 427 if (!ypxfrd_get_map(ypxfr_source_host, ypxfr_mapname, 428 ypxfr_source_domain, ypxfr_temp_map)) 429 goto leave; 430 } 431 432 /* Open the temporary map read/write. */ 433 if ((dbp = yp_open_db_rw(ypxfr_dest_domain, tempmap, 0)) == NULL) { 434 yp_error("failed to open temporary map file"); 435 ypxfr_exit(YPXFR_DBM,NULL); 436 } 437 438 /* 439 * Fill in the keys we already know, such as the order number, 440 * master name, input file name (we actually make up a bogus 441 * name for that) and output file name. 442 */ 443 snprintf(buf, sizeof(buf), "%d", ypxfr_order); 444 data.data = buf; 445 data.size = strlen(buf); 446 447 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 448 yp_error("failed to write order number to database"); 449 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 450 } 451 452 key.data = "YP_MASTER_NAME"; 453 key.size = sizeof("YP_MASTER_NAME") - 1; 454 data.data = ypxfr_master; 455 data.size = strlen(ypxfr_master); 456 457 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 458 yp_error("failed to write master name to database"); 459 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 460 } 461 462 key.data = "YP_DOMAIN_NAME"; 463 key.size = sizeof("YP_DOMAIN_NAME") - 1; 464 data.data = ypxfr_dest_domain; 465 data.size = strlen(ypxfr_dest_domain); 466 467 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 468 yp_error("failed to write domain name to database"); 469 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 470 } 471 472 snprintf (buf, sizeof(buf), "%s:%s", ypxfr_source_host, ypxfr_mapname); 473 474 key.data = "YP_INPUT_NAME"; 475 key.size = sizeof("YP_INPUT_NAME") - 1; 476 data.data = &buf; 477 data.size = strlen(buf); 478 479 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 480 yp_error("failed to write input name to database"); 481 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 482 483 } 484 485 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain, 486 ypxfr_mapname); 487 488 key.data = "YP_OUTPUT_NAME"; 489 key.size = sizeof("YP_OUTPUT_NAME") - 1; 490 data.data = &buf; 491 data.size = strlen(buf); 492 493 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 494 yp_error("failed to write output name to database"); 495 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 496 } 497 498 if (interdom) { 499 key.data = "YP_INTERDOMAIN"; 500 key.size = sizeof("YP_INTERDOMAIN") - 1; 501 data.data = ""; 502 data.size = 0; 503 504 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 505 yp_error("failed to add interdomain flag to database"); 506 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 507 } 508 } 509 510 if (secure) { 511 key.data = "YP_SECURE"; 512 key.size = sizeof("YP_SECURE") - 1; 513 data.data = ""; 514 data.size = 0; 515 516 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 517 yp_error("failed to add secure flag to database"); 518 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 519 } 520 } 521 522 /* Now suck over the contents of the map from the master. */ 523 524 if (ypxfr_get_map(ypxfr_mapname,ypxfr_source_domain, 525 ypxfr_source_host, ypxfr_foreach)){ 526 yp_error("failed to retrieve map from source host"); 527 ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map); 528 } 529 530 (void)(dbp->close)(dbp); 531 dbp = NULL; /* <- yes, it seems this is necessary. */ 532 533leave: 534 535 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain, 536 ypxfr_mapname); 537 538 /* Peek at the order number again and check for skew. */ 539 if ((ypxfr_skew_check = ypxfr_get_order(ypxfr_source_domain, 540 ypxfr_mapname, 541 ypxfr_master, 0)) == 0) { 542 yp_error("failed to get order number of %s: %s", 543 ypxfr_mapname, yp_errno == YPXFR_SUCC ? 544 "map has order 0" : ypxfrerr_string(yp_errno)); 545 ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map); 546 } 547 548 if (ypxfr_order != ypxfr_skew_check) 549 ypxfr_exit(YPXFR_SKEW,&ypxfr_temp_map); 550 551 /* 552 * Send a YPPROC_CLEAR to the local ypserv. 553 */ 554 if (ypxfr_clear) { 555 char in = 0; 556 char *out = NULL; 557 int stat; 558 if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR, 559 xdr_void, (void *)&in, 560 xdr_void, (void *)out)) != RPC_SUCCESS) { 561 yp_error("failed to send 'clear' to local ypserv: %s", 562 clnt_sperrno((enum clnt_stat) stat)); 563 ypxfr_exit(YPXFR_CLEAR, &ypxfr_temp_map); 564 } 565 } 566 567 /* 568 * Put the new map in place immediately. I'm not sure if the 569 * kernel does an unlink() and rename() atomically in the event 570 * that we move a new copy of a map over the top of an existing 571 * one, but there's less chance of a race condition happening 572 * than if we were to do the unlink() ourselves. 573 */ 574 if (rename(ypxfr_temp_map, buf) == -1) { 575 yp_error("rename(%s,%s) failed: %s", ypxfr_temp_map, buf, 576 strerror(errno)); 577 ypxfr_exit(YPXFR_FILE,NULL); 578 } 579 580 ypxfr_exit(YPXFR_SUCC,NULL); 581 582 return(1); 583} 584