1 /* $NetBSD: yppush.c,v 1.18 2002/07/06 00:46:12 wiz Exp $ */ 2 3 /* 4 * 5 * Copyright (c) 1997 Charles D. Cranor 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * yppush 33 * author: Chuck Cranor <chuck@ccrc.wustl.edu> 34 * date: 05-Nov-97 35 * 36 * notes: this is a full rewrite of Mats O Jansson <moj@stacken.kth.se>'s 37 * yppush.c. i have restructured and cleaned up the entire file. 38 */ 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 #include <sys/time.h> 43 #include <sys/wait.h> 44 45 #include <ctype.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <syslog.h> 54 #include <unistd.h> 55 56 #include <rpc/rpc.h> 57 #include <rpcsvc/yp_prot.h> 58 #include <rpcsvc/ypclnt.h> 59 60 #include "ypdb.h" 61 #include "ypdef.h" 62 #include "yplib_host.h" 63 #include "yppush.h" 64 65 /* 66 * yppush: push a new YP map out YP servers 67 * 68 * usage: 69 * yppush [-d domain] [-h host] [-v] mapname 70 * 71 * -d: the domainname the map lives in [if different from default] 72 * -h: push only to this host [otherwise, push to all hosts] 73 * -v: verbose 74 */ 75 76 /* 77 * structures 78 */ 79 80 struct yppush_info { 81 char *ourdomain; /* domain of interest */ 82 char *map; /* map we are pushing */ 83 char *owner; /* owner of map */ 84 int order; /* order number of map (version) */ 85 }; 86 /* 87 * global vars 88 */ 89 90 int verbo = 0; /* verbose */ 91 92 /* 93 * prototypes 94 */ 95 96 int main(int, char *[]); 97 int pushit(int, char *, int, char *, int, char *); 98 void push(char *, int, struct yppush_info *); 99 void _svc_run(void); 100 void usage(void); 101 102 103 /* 104 * main 105 */ 106 107 int 108 main(int argc, char *argv[]) 109 110 { 111 char *targhost = NULL; 112 struct yppush_info ypi = {NULL, NULL, NULL, 0}; 113 int c, rv; 114 const char *cp; 115 char *master; 116 DBM *ypdb; 117 datum datum; 118 CLIENT *ypserv; 119 struct timeval tv; 120 enum clnt_stat retval; 121 struct ypall_callback ypallcb; 122 123 /* 124 * parse command line 125 */ 126 while ((c = getopt(argc, argv, "d:h:v")) != -1) { 127 switch (c) { 128 case 'd': 129 ypi.ourdomain = optarg; 130 break; 131 case 'h': 132 targhost = optarg; 133 break; 134 case 'v': 135 verbo = 1; 136 break; 137 default: 138 usage(); 139 /* NOTREACHED */ 140 } 141 } 142 argc -= optind; 143 argv += optind; 144 if (argc != 1) 145 usage(); 146 openlog("yppush", LOG_PID, LOG_DAEMON); 147 ypi.map = argv[0]; 148 if (strlen(ypi.map) > YPMAXMAP) 149 errx(1, "%s: map name too long (limit %d)", ypi.map, YPMAXMAP); 150 151 /* 152 * ensure we have a domain 153 */ 154 if (ypi.ourdomain == NULL) { 155 c = yp_get_default_domain(&ypi.ourdomain); 156 if (ypi.ourdomain == NULL) 157 errx(1, "unable to get default domain: %s", 158 yperr_string(c)); 159 } 160 /* 161 * verify that the domain and specified database exsists 162 * 163 * XXXCDC: this effectively prevents us from pushing from any 164 * host but the master. an alternate plan is to find the master 165 * host for a map, clear it, ask for the order number, and then 166 * send xfr requests. if that was used we would not need local 167 * file access. 168 */ 169 if (chdir(YP_DB_PATH) < 0) 170 err(1, "%s", YP_DB_PATH); 171 if (chdir(ypi.ourdomain) < 0) 172 err(1, "%s/%s", YP_DB_PATH, ypi.ourdomain); 173 174 /* 175 * now open the database so we can extract "order number" 176 * (i.e. timestamp) of the map. 177 */ 178 ypdb = ypdb_open(ypi.map, 0, O_RDONLY); 179 if (ypdb == NULL) 180 err(1, "ypdb_open %s/%s/%s", YP_DB_PATH, ypi.ourdomain, 181 ypi.map); 182 datum.dptr = YP_LAST_KEY; 183 datum.dsize = YP_LAST_LEN; 184 datum = ypdb_fetch(ypdb, datum); 185 if (datum.dptr == NULL) 186 errx(1, 187 "unable to fetch %s key: check database with 'makedbm -u'", 188 YP_LAST_KEY); 189 ypi.order = 0; 190 cp = datum.dptr; 191 while (cp < datum.dptr + datum.dsize) { 192 if (!isdigit(*cp)) 193 errx(1, 194 "invalid order number: check database with 'makedbm -u'"); 195 ypi.order = (ypi.order * 10) + *cp - '0'; 196 cp++; 197 } 198 ypdb_close(ypdb); 199 200 if (verbo) 201 printf("pushing %s [order=%d] in domain %s\n", ypi.map, 202 ypi.order, ypi.ourdomain); 203 204 /* 205 * ok, we are ready to do it. first we send a clear_2 request 206 * to the local server [should be the master] to make sure it has 207 * the correct database open. 208 * 209 * XXXCDC: note that yp_bind_local exits on failure so ypserv can't 210 * be null. this makes it difficult to print a useful error message. 211 * [it will print "clntudp_create: no contact with localhost"] 212 */ 213 tv.tv_sec = 10; 214 tv.tv_usec = 0; 215 ypserv = yp_bind_local(YPPROG, YPVERS); 216 retval = clnt_call(ypserv, YPPROC_CLEAR, xdr_void, 0, xdr_void, 0, tv); 217 if (retval != RPC_SUCCESS) 218 errx(1, "clnt_call CLEAR to local ypserv: %s", 219 clnt_sperrno(retval)); 220 clnt_destroy(ypserv); 221 222 /* 223 * now use normal yplib functions to bind to the domain. 224 */ 225 rv = yp_bind(ypi.ourdomain); 226 if (rv) 227 errx(1, "error binding to %s: %s", ypi.ourdomain, 228 yperr_string(rv)); 229 230 /* 231 * find 'owner' of the map (see pushit for usage) 232 */ 233 rv = yp_master(ypi.ourdomain, ypi.map, &ypi.owner); 234 if (rv) 235 errx(1, "error finding master for %s in %s: %s", ypi.map, 236 ypi.ourdomain, yperr_string(rv)); 237 238 /* 239 * inform user of our progress 240 */ 241 if (verbo) { 242 printf("pushing map %s in %s: order=%d, owner=%s\n", ypi.map, 243 ypi.ourdomain, ypi.order, ypi.owner); 244 printf("pushing to %s\n", 245 (targhost) ? targhost : "<all ypservs>"); 246 } 247 248 /* 249 * finally, do it. 250 */ 251 if (targhost) { 252 push(targhost, strlen(targhost), &ypi); 253 } else { 254 255 /* 256 * no host specified, do all hosts the master knows about via 257 * the ypservers map. 258 */ 259 rv = yp_master(ypi.ourdomain, "ypservers", &master); 260 if (rv) 261 errx(1, "error finding master for ypservers in %s: %s", 262 ypi.ourdomain, yperr_string(rv)); 263 264 if (verbo) 265 printf( 266 "contacting ypservers %s master on %s for list of ypservs...\n", 267 ypi.ourdomain, master); 268 269 ypserv = yp_bind_host(master, YPPROG, YPVERS, 0, 1); 270 271 ypallcb.foreach = pushit; /* callback function */ 272 ypallcb.data = (char *) &ypi; /* data to pass into callback */ 273 274 rv = yp_all_host(ypserv, ypi.ourdomain, "ypservers", &ypallcb); 275 if (rv) 276 errx(1, "pushing %s in %s failed: %s", ypi.map, 277 ypi.ourdomain, yperr_string(rv)); 278 } 279 exit(0); 280 } 281 282 /* 283 * usage: print usage and exit 284 */ 285 void 286 usage(void) 287 { 288 fprintf(stderr, "usage: %s [-d domain] [-h host] [-v] map\n", 289 getprogname()); 290 exit(1); 291 } 292 293 /* 294 * pushit: called from yp_all_host to push a specific host. 295 * the key/value pairs are from the ypservers map. 296 */ 297 int 298 pushit(int instatus, char *inkey, int inkeylen, char *inval, 299 int invallen, char *indata) 300 { 301 struct yppush_info *ypi = (struct yppush_info *) indata; 302 303 push(inkey, inkeylen, ypi); /* do it! */ 304 return (0); 305 } 306 307 /* 308 * push: push a specific map on a specific host 309 */ 310 void 311 push(char *host, int hostlen, struct yppush_info *ypi) 312 { 313 char target[YPMAXPEER]; 314 CLIENT *ypserv; 315 SVCXPRT *transp; 316 int prog, pid, rv; 317 struct timeval tv; 318 struct ypreq_xfr req; 319 320 /* 321 * get our target host in a null terminated string 322 */ 323 snprintf(target, sizeof(target), "%*.*s", hostlen, hostlen, host); 324 325 /* 326 * XXXCDC: arg! we would like to use yp_bind_host here, except that 327 * it exits on failure and we don't want to give up just because 328 * one host fails. thus, we have to do it the hard way. 329 */ 330 ypserv = clnt_create(target, YPPROG, YPVERS, "tcp"); 331 if (ypserv == NULL) { 332 clnt_pcreateerror(target); 333 return; 334 } 335 336 /* 337 * our XFR rpc request to the client just starts the transfer. 338 * when the client is done, it wants to call a procedure that 339 * we are serving to tell us that it is done. so we must create 340 * and register a procedure for us for it to call. 341 */ 342 transp = svcudp_create(RPC_ANYSOCK); 343 if (transp == NULL) { 344 warnx("callback svcudp_create failed"); 345 goto error; 346 } 347 348 /* register it with portmap */ 349 for (prog = 0x40000000; prog < 0x5fffffff; prog++) { 350 if (svc_register(transp, prog, 1, yppush_xfrrespprog_1, 351 IPPROTO_UDP)) 352 break; 353 } 354 if (prog >= 0x5fffffff) { 355 warnx("unable to register callback"); 356 goto error; 357 } 358 359 /* 360 * now fork off a server to catch our reply 361 */ 362 pid = fork(); 363 if (pid == -1) { 364 svc_unregister(prog, 1); /* drop our mapping with 365 * portmap */ 366 warn("fork failed"); 367 goto error; 368 } 369 370 /* 371 * child process becomes the server 372 */ 373 if (pid == 0) { 374 _svc_run(); 375 exit(0); 376 } 377 378 /* 379 * we are the parent process: send XFR request to server. 380 * the "owner" field isn't used by ypserv (and shouldn't be, since 381 * the ypserv has no idea if we are a legitimate yppush or not). 382 * instead, the owner of the map is determined by the master value 383 * currently cached on the slave server. 384 */ 385 close(transp->xp_fd); /* close child's socket, we don't need it */ 386 /* don't wait for anything here, we will wait for child's exit */ 387 tv.tv_sec = 0; 388 tv.tv_usec = 0; 389 req.map_parms.domain = ypi->ourdomain; 390 req.map_parms.map = ypi->map; 391 req.map_parms.owner = ypi->owner; /* NOT USED */ 392 req.map_parms.ordernum = ypi->order; 393 req.transid = (u_int) pid; 394 req.proto = prog; 395 req.port = transp->xp_port; 396 397 if (verbo) 398 printf("asking host %s to transfer map (xid=%d)\n", target, 399 req.transid); 400 401 rv = clnt_call(ypserv, YPPROC_XFR, xdr_ypreq_xfr, &req, 402 xdr_void, NULL, tv); /* do it! */ 403 404 if (rv != RPC_SUCCESS && rv != RPC_TIMEDOUT) { 405 warnx("unable to xfr to host %s: %s", target, clnt_sperrno(rv)); 406 kill(pid, SIGTERM); 407 } 408 409 /* 410 * now wait for child to get the reply and exit 411 */ 412 wait4(pid, NULL, 0, NULL); 413 svc_unregister(prog, 1); 414 415 /* 416 * ... and we are done. fall through 417 */ 418 419 error: 420 if (transp) 421 svc_destroy(transp); 422 clnt_destroy(ypserv); 423 return; 424 } 425 426 /* 427 * _svc_run: this is the main loop for the RPC server that we fork off 428 * to await the reply from ypxfr. 429 */ 430 void 431 _svc_run(void) 432 { 433 fd_set readfds; 434 struct timeval tv; 435 int rv, nfds; 436 437 nfds = sysconf(_SC_OPEN_MAX); 438 while (1) { 439 440 readfds = svc_fdset; /* structure copy from global var */ 441 tv.tv_sec = 60; 442 tv.tv_usec = 0; 443 444 rv = select(nfds, &readfds, NULL, NULL, &tv); 445 446 if (rv < 0) { 447 if (errno == EINTR) 448 continue; 449 warn("_svc_run: select failed"); 450 return; 451 } 452 if (rv == 0) 453 errx(0, "_svc_run: callback timed out"); 454 455 /* 456 * got something 457 */ 458 svc_getreqset(&readfds); 459 460 } 461 } 462