ypxfr_main.c revision 19065
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.18 1996/10/20 19:44:45 wpaul 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.18 1996/10/20 19:44:45 wpaul 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 dbkey.data = key; 137 dbkey.size = keylen; 138 dbval.data = val; 139 dbval.size = vallen; 140 141 if (yp_put_record(dbp, &dbkey, &dbval, 0) != YP_TRUE) 142 return(yp_errno); 143 144 return (0); 145} 146 147main(argc,argv) 148 int argc; 149 char *argv[]; 150{ 151 int ch; 152 int ypxfr_force = 0; 153 char *ypxfr_dest_domain = NULL; 154 char *ypxfr_source_host = NULL; 155 char *ypxfr_source_domain = NULL; 156 char *ypxfr_local_domain = NULL; 157 char *ypxfr_master = NULL; 158 unsigned long ypxfr_order = -1, ypxfr_skew_check = -1; 159 char *ypxfr_mapname = NULL; 160 int ypxfr_args = 0; 161 char ypxfr_temp_map[MAXPATHLEN + 2]; 162 char tempmap[MAXPATHLEN + 2]; 163 char buf[MAXPATHLEN + 2]; 164 DBT key, data; 165 int remoteport; 166 167 debug = 1; 168 169 if (!isatty(fileno(stderr))) { 170 openlog(progname, LOG_PID, LOG_DAEMON); 171 _rpcpmstart = 1; 172 } 173 174 if (argc < 2) 175 usage(); 176 177 while ((ch = getopt(argc, argv, "fcd:h:s:p:C:")) != EOF) { 178 int my_optind; 179 switch(ch) { 180 case 'f': 181 ypxfr_force++; 182 ypxfr_args++; 183 break; 184 case 'c': 185 ypxfr_clear = 0; 186 ypxfr_args++; 187 break; 188 case 'd': 189 ypxfr_dest_domain = optarg; 190 ypxfr_args += 2; 191 break; 192 case 'h': 193 ypxfr_source_host = optarg; 194 ypxfr_args += 2; 195 break; 196 case 's': 197 ypxfr_source_domain = optarg; 198 ypxfr_args += 2; 199 break; 200 case 'p': 201 yp_dir = optarg; 202 ypxfr_args += 2; 203 break; 204 case 'C': 205 /* 206 * Whoever decided that the -C flag should take 207 * four arguments is a twit. 208 */ 209 my_optind = optind - 1; 210 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 211 yp_error("transaction ID not specified"); 212 usage(); 213 } 214 ypxfr_resp.transid = atol(argv[my_optind]); 215 my_optind++; 216 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 217 yp_error("RPC program number not specified"); 218 usage(); 219 } 220 ypxfr_prognum = atol(argv[my_optind]); 221 my_optind++; 222 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 223 yp_error("address not specified"); 224 usage(); 225 } 226 if (!inet_aton(argv[my_optind], &ypxfr_callback_addr.sin_addr)) { 227 yp_error("failed to convert '%s' to IP addr", 228 argv[my_optind]); 229 exit(1); 230 } 231 my_optind++; 232 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) { 233 yp_error("port not specified"); 234 usage(); 235 } 236 ypxfr_callback_addr.sin_port = htons((u_short)atoi(argv[my_optind])); 237 ypxfr_args += 5; 238 break; 239 default: 240 usage(); 241 break; 242 } 243 } 244 245 ypxfr_mapname = argv[ypxfr_args + 1]; 246 247 if (ypxfr_mapname == NULL) { 248 yp_error("no map name specified"); 249 usage(); 250 } 251 252 /* Always the case. */ 253 ypxfr_callback_addr.sin_family = AF_INET; 254 255 /* Determine if local NIS client facilities are turned on. */ 256 if (!yp_get_default_domain(&ypxfr_local_domain) && 257 _yp_check(&ypxfr_local_domain)) 258 ypxfr_use_yplib = 1; 259 260 /* 261 * If no destination domain is specified, assume that the 262 * local default domain is to be used and try to obtain it. 263 * Fails if NIS client facilities are turned off. 264 */ 265 if (ypxfr_dest_domain == NULL) { 266 if (ypxfr_use_yplib) { 267 yp_get_default_domain(&ypxfr_dest_domain); 268 } else { 269 yp_error("no destination domain specified and \ 270the local domain name isn't set"); 271 ypxfr_exit(YPXFR_BADARGS,NULL); 272 } 273 } 274 275 /* 276 * If a source domain is not specified, assume it to 277 * be the same as the destination domain. 278 */ 279 if (ypxfr_source_domain == NULL) { 280 ypxfr_source_domain = ypxfr_dest_domain; 281 } 282 283 /* 284 * If the source host is not specified, assume it to be the 285 * master for the specified map. If local NIS client facilities 286 * are turned on, we can figure this out using yp_master(). 287 * If not, we have to see if a local copy of the map exists 288 * and extract its YP_MASTER_NAME record. If _that_ fails, 289 * we are stuck and must ask the user for more information. 290 */ 291 if (ypxfr_source_host == NULL) { 292 if (!ypxfr_use_yplib) { 293 /* 294 * Double whammy: NIS isn't turned on and the user 295 * didn't specify a source host. 296 */ 297 char *dptr; 298 key.data = "YP_MASTER_NAME"; 299 key.size = sizeof("YP_MASTER_NAME") - 1; 300 301 if (yp_get_record(ypxfr_dest_domain, ypxfr_mapname, 302 &key, &data, 1) != YP_TRUE) { 303 yp_error("no source host specified"); 304 ypxfr_exit(YPXFR_BADARGS,NULL); 305 } 306 dptr = data.data; 307 dptr[data.size] = '\0'; 308 ypxfr_master = ypxfr_source_host = strdup(dptr); 309 } 310 } else { 311 if (ypxfr_use_yplib) 312 ypxfr_use_yplib = 0; 313 } 314 315 if (ypxfr_master == NULL) { 316 if ((ypxfr_master = ypxfr_get_master(ypxfr_source_domain, 317 ypxfr_mapname, 318 ypxfr_source_host, 319 ypxfr_use_yplib)) == NULL) { 320 yp_error("failed to find master of %s in domain %s: %s", 321 ypxfr_mapname, ypxfr_source_domain, 322 ypxfrerr_string(yp_errno)); 323 ypxfr_exit(YPXFR_MADDR,NULL); 324 } 325 } 326 327 /* 328 * If we got here and ypxfr_source_host is still undefined, 329 * it means we had to resort to using yp_master() to find the 330 * master server for the map. The source host and master should 331 * be identical. 332 */ 333 if (ypxfr_source_host == NULL) 334 ypxfr_source_host = ypxfr_master; 335 336 /* 337 * Don't talk to ypservs on unprivileged ports. 338 */ 339 remoteport = getrpcport(ypxfr_source_host, YPPROG, YPVERS, IPPROTO_UDP); 340 if (remoteport >= IPPORT_RESERVED) { 341 yp_error("ypserv on %s not running on reserved port", 342 ypxfr_source_host); 343 ypxfr_exit(YPXFR_REFUSED, NULL); 344 } 345 346 if ((ypxfr_order = ypxfr_get_order(ypxfr_source_domain, 347 ypxfr_mapname, 348 ypxfr_master, 0)) == 0) { 349 yp_error("failed to get order number of %s: %s", 350 ypxfr_mapname, yp_errno == YPXFR_SUCC ? 351 "map has order 0" : ypxfrerr_string(yp_errno)); 352 ypxfr_exit(YPXFR_YPERR,NULL); 353 } 354 355 key.data = "YP_LAST_MODIFIED"; 356 key.size = sizeof("YP_LAST_MODIFIED") - 1; 357 358 /* The order number is immaterial when the 'force' flag is set. */ 359 360 if (!ypxfr_force) { 361 int ignore = 0; 362 if (yp_get_record(ypxfr_dest_domain,ypxfr_mapname,&key,&data,1) != YP_TRUE) { 363 switch(yp_errno) { 364 case YP_NOKEY: 365 ypxfr_exit(YPXFR_FORCE,NULL); 366 break; 367 case YP_NOMAP: 368 /* 369 * If the map doesn't exist, we're 370 * creating it. Ignore the error. 371 */ 372 ignore++; 373 break; 374 case YP_BADDB: 375 default: 376 ypxfr_exit(YPXFR_DBM,NULL); 377 break; 378 } 379 } 380 if (!ignore && ypxfr_order <= atoi(data.data)) 381 ypxfr_exit(YPXFR_AGE, NULL); 382 383 } 384 385 /* Construct a temporary map file name */ 386 snprintf(tempmap, sizeof(tempmap), "%s.%d",ypxfr_mapname, getpid()); 387 snprintf(ypxfr_temp_map, sizeof(ypxfr_temp_map), "%s/%s/%s", yp_dir, 388 ypxfr_dest_domain, tempmap); 389 390 if ((remoteport = getrpcport(ypxfr_source_host, YPXFRD_FREEBSD_PROG, 391 YPXFRD_FREEBSD_VERS, IPPROTO_TCP))) { 392 393 /* Don't talk to rpc.ypxfrds on unprovileged ports. */ 394 if (remoteport >= IPPORT_RESERVED) { 395 yp_error("rpc.ypxfrd on %s not using privileged port", 396 ypxfr_source_host); 397 ypxfr_exit(YPXFR_REFUSED, NULL); 398 } 399 400 /* Try to send using ypxfrd. If it fails, use old method. */ 401 if (!ypxfrd_get_map(ypxfr_source_host, ypxfr_mapname, 402 ypxfr_source_domain, ypxfr_temp_map)) 403 goto leave; 404 } 405 406 /* Open the temporary map read/write. */ 407 if ((dbp = yp_open_db_rw(ypxfr_dest_domain, tempmap, 0)) == NULL) { 408 yp_error("failed to open temporary map file"); 409 ypxfr_exit(YPXFR_DBM,NULL); 410 } 411 412 /* 413 * Fill in the keys we already know, such as the order number, 414 * master name, input file name (we actually make up a bogus 415 * name for that) and output file name. 416 */ 417 snprintf(buf, sizeof(buf), "%d", ypxfr_order); 418 data.data = buf; 419 data.size = strlen(buf); 420 421 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 422 yp_error("failed to write order number to database"); 423 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 424 } 425 426 key.data = "YP_MASTER_NAME"; 427 key.size = sizeof("YP_MASTER_NAME") - 1; 428 data.data = ypxfr_master; 429 data.size = strlen(ypxfr_master); 430 431 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 432 yp_error("failed to write master name to database"); 433 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 434 } 435 436 key.data = "YP_DOMAIN_NAME"; 437 key.size = sizeof("YP_DOMAIN_NAME") - 1; 438 data.data = ypxfr_dest_domain; 439 data.size = strlen(ypxfr_dest_domain); 440 441 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 442 yp_error("failed to write domain name to database"); 443 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 444 } 445 446 snprintf (buf, sizeof(buf), "%s:%s", ypxfr_source_host, ypxfr_mapname); 447 448 key.data = "YP_INPUT_NAME"; 449 key.size = sizeof("YP_INPUT_NAME") - 1; 450 data.data = &buf; 451 data.size = strlen(buf); 452 453 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 454 yp_error("failed to write input name to database"); 455 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 456 457 } 458 459 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain, 460 ypxfr_mapname); 461 462 key.data = "YP_OUTPUT_NAME"; 463 key.size = sizeof("YP_OUTPUT_NAME") - 1; 464 data.data = &buf; 465 data.size = strlen(buf); 466 467 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) { 468 yp_error("failed to write output name to database"); 469 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map); 470 } 471 472 /* Now suck over the contents of the map from the master. */ 473 474 if (ypxfr_get_map(ypxfr_mapname,ypxfr_source_domain, 475 ypxfr_source_host, ypxfr_foreach)){ 476 yp_error("failed to retrieve map from source host"); 477 ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map); 478 } 479 480 (void)(dbp->close)(dbp); 481 dbp = NULL; /* <- yes, it seems this is necessary. */ 482 483leave: 484 485 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain, 486 ypxfr_mapname); 487 488 /* Peek at the order number again and check for skew. */ 489 if ((ypxfr_skew_check = ypxfr_get_order(ypxfr_source_domain, 490 ypxfr_mapname, 491 ypxfr_master, 0)) == 0) { 492 yp_error("failed to get order number of %s: %s", 493 ypxfr_mapname, yp_errno == YPXFR_SUCC ? 494 "map has order 0" : ypxfrerr_string(yp_errno)); 495 ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map); 496 } 497 498 if (ypxfr_order != ypxfr_skew_check) 499 ypxfr_exit(YPXFR_SKEW,&ypxfr_temp_map); 500 501 /* 502 * Send a YPPROC_CLEAR to the local ypserv. 503 */ 504 if (ypxfr_clear) { 505 char in = 0; 506 char *out = NULL; 507 int stat; 508 if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR, 509 xdr_void, (void *)&in, 510 xdr_void, (void *)out)) != RPC_SUCCESS) { 511 yp_error("failed to send 'clear' to local ypserv: %s", 512 clnt_sperrno((enum clnt_stat) stat)); 513 ypxfr_exit(YPXFR_CLEAR, &ypxfr_temp_map); 514 } 515 } 516 517 /* 518 * Put the new map in place immediately. I'm not sure if the 519 * kernel does an unlink() and rename() atomically in the event 520 * that we move a new copy of a map over the top of an existing 521 * one, but there's less chance of a race condition happening 522 * than if we were to do the unlink() ourselves. 523 */ 524 if (rename(ypxfr_temp_map, buf) == -1) { 525 yp_error("rename(%s,%s) failed: %s", ypxfr_temp_map, buf, 526 strerror(errno)); 527 ypxfr_exit(YPXFR_FILE,NULL); 528 } 529 530 ypxfr_exit(YPXFR_SUCC,NULL); 531 532 return(1); 533} 534