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