yppush.c revision 1.3 1 1.3 thorpej /* $NetBSD: yppush.c,v 1.3 1996/08/09 20:24:34 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1995 Mats O Jansson <moj (at) stacken.kth.se>
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 * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Mats O Jansson
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #include <sys/types.h>
35 1.1 thorpej #include <sys/stat.h>
36 1.1 thorpej #include <sys/resource.h>
37 1.1 thorpej #include <sys/signal.h>
38 1.1 thorpej
39 1.1 thorpej #include <errno.h>
40 1.1 thorpej #include <err.h>
41 1.1 thorpej #include <fcntl.h>
42 1.1 thorpej #include <netdb.h>
43 1.1 thorpej #include <stdio.h>
44 1.1 thorpej #include <string.h>
45 1.1 thorpej #include <unistd.h>
46 1.1 thorpej
47 1.1 thorpej #include <rpc/rpc.h>
48 1.1 thorpej #include <rpc/xdr.h>
49 1.1 thorpej #include <rpcsvc/yp_prot.h>
50 1.1 thorpej #include <rpcsvc/ypclnt.h>
51 1.1 thorpej
52 1.1 thorpej #include "yplib_host.h"
53 1.1 thorpej #include "ypdef.h"
54 1.1 thorpej #include "ypdb.h"
55 1.1 thorpej #include "yppush.h"
56 1.1 thorpej
57 1.1 thorpej extern char *__progname; /* from crt0.o */
58 1.1 thorpej extern char *optarg;
59 1.1 thorpej extern int optind;
60 1.1 thorpej
61 1.1 thorpej int pushit __P((int, char *, int, char *, int, char *));
62 1.1 thorpej void push __P((int, char *));
63 1.1 thorpej void req_xfr __P((pid_t, u_int, SVCXPRT *, char *, CLIENT *));
64 1.1 thorpej void _svc_run __P((void));
65 1.1 thorpej void cleanup __P((void));
66 1.1 thorpej void usage __P((void));
67 1.1 thorpej
68 1.1 thorpej int Verbose = 0;
69 1.1 thorpej u_int OrderNum;
70 1.1 thorpej
71 1.1 thorpej char Domain[YPMAXDOMAIN];
72 1.1 thorpej char Map[YPMAXMAP];
73 1.1 thorpej char LocalHost[YPMAXPEER];
74 1.1 thorpej
75 1.1 thorpej static DBM *yp_databas;
76 1.1 thorpej
77 1.1 thorpej int
78 1.1 thorpej main(argc, argv)
79 1.1 thorpej int argc;
80 1.1 thorpej char **argv;
81 1.1 thorpej {
82 1.1 thorpej struct ypall_callback ypcb;
83 1.1 thorpej char *master, *domain, *map, *hostname;
84 1.1 thorpej int c, r, i;
85 1.1 thorpej datum o;
86 1.1 thorpej char *ypmap = "ypservers";
87 1.1 thorpej CLIENT *client;
88 1.1 thorpej struct stat finfo;
89 1.1 thorpej char order_key[YP_LAST_LEN] = YP_LAST_KEY;
90 1.1 thorpej static char map_path[MAXPATHLEN];
91 1.1 thorpej
92 1.1 thorpej if (atexit(cleanup))
93 1.1 thorpej err(1, "can't register cleanup function");
94 1.1 thorpej
95 1.3 thorpej domain = NULL;
96 1.1 thorpej hostname = NULL;
97 1.1 thorpej
98 1.1 thorpej while ((c = getopt(argc, argv, "d:h:v")) != -1) {
99 1.1 thorpej switch(c) {
100 1.1 thorpej case 'd':
101 1.1 thorpej domain = optarg;
102 1.1 thorpej break;
103 1.1 thorpej
104 1.1 thorpej case 'h':
105 1.1 thorpej hostname = optarg;
106 1.1 thorpej break;
107 1.1 thorpej
108 1.1 thorpej case 'v':
109 1.1 thorpej Verbose = 1;
110 1.1 thorpej break;
111 1.1 thorpej
112 1.1 thorpej default:
113 1.1 thorpej usage();
114 1.1 thorpej /*NOTREACHED*/
115 1.1 thorpej }
116 1.1 thorpej }
117 1.1 thorpej argv += optind; argc -= optind;
118 1.1 thorpej
119 1.1 thorpej if (argc != 1)
120 1.1 thorpej usage();
121 1.1 thorpej
122 1.1 thorpej map = argv[0];
123 1.3 thorpej
124 1.3 thorpej if (domain == NULL)
125 1.3 thorpej if ((c = yp_get_default_domain(&domain)))
126 1.3 thorpej errx(1, "can't get YP domain name. Reason: %s",
127 1.3 thorpej yperr_string(c));
128 1.1 thorpej
129 1.1 thorpej memset(Domain, 0, sizeof(Domain));
130 1.1 thorpej snprintf(Domain, sizeof(Domain), "%s", domain);
131 1.1 thorpej memset(Map, 0, sizeof(Map));
132 1.1 thorpej snprintf(Map, sizeof(Map), "%s", map);
133 1.1 thorpej
134 1.1 thorpej /* Set up the hostname to be used for the xfr */
135 1.1 thorpej localhostname(LocalHost, sizeof(LocalHost));
136 1.1 thorpej
137 1.1 thorpej /* Check domain */
138 1.1 thorpej sprintf(map_path,"%s/%s",YP_DB_PATH,domain);
139 1.1 thorpej if (!((stat(map_path, &finfo) == 0) &&
140 1.1 thorpej ((finfo.st_mode & S_IFMT) == S_IFDIR))) {
141 1.1 thorpej fprintf(stderr,"yppush: Map does not exists.\n");
142 1.1 thorpej exit(1);
143 1.1 thorpej }
144 1.1 thorpej
145 1.1 thorpej /* Check map */
146 1.1 thorpej memset(map_path, 0, sizeof(map_path));
147 1.1 thorpej snprintf(map_path, sizeof(map_path), "%s/%s/%s%s", YP_DB_PATH,
148 1.1 thorpej Domain, Map, YPDB_SUFFIX);
149 1.1 thorpej if (stat(map_path, &finfo) != 0)
150 1.1 thorpej errx(1, "map does not exist");
151 1.1 thorpej
152 1.1 thorpej /* Open the database */
153 1.1 thorpej memset(map_path, 0, sizeof(map_path));
154 1.1 thorpej snprintf(map_path, sizeof(map_path), "%s/%s/%s", YP_DB_PATH,
155 1.1 thorpej Domain, Map);
156 1.1 thorpej yp_databas = ypdb_open(map_path, 0, O_RDONLY);
157 1.1 thorpej OrderNum=0xffffffff;
158 1.1 thorpej if (yp_databas == NULL)
159 1.1 thorpej errx(1, "%s%s: cannot open database", map_path, YPDB_SUFFIX);
160 1.1 thorpej else {
161 1.1 thorpej o.dptr = (char *) &order_key;
162 1.1 thorpej o.dsize = YP_LAST_LEN;
163 1.1 thorpej o = ypdb_fetch(yp_databas, o);
164 1.1 thorpej if (o.dptr == NULL)
165 1.1 thorpej errx(1, "%s: cannot determine order number", Map);
166 1.1 thorpej else {
167 1.1 thorpej OrderNum = 0;
168 1.1 thorpej for (i = 0; i < o.dsize - 1; i++)
169 1.1 thorpej if (!isdigit(o.dptr[i]))
170 1.1 thorpej OrderNum=0xffffffff;
171 1.1 thorpej
172 1.1 thorpej if (OrderNum != 0)
173 1.1 thorpej errx(1, "%s: invalid order number `%s'",
174 1.1 thorpej Map, o.dptr);
175 1.1 thorpej else
176 1.1 thorpej OrderNum = atoi(o.dptr);
177 1.1 thorpej }
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej /* No longer need YP map. */
181 1.1 thorpej ypdb_close(yp_databas);
182 1.1 thorpej yp_databas = NULL;
183 1.1 thorpej
184 1.1 thorpej r = yp_bind(Domain);
185 1.1 thorpej if (r != 0)
186 1.1 thorpej errx(1, "%s", yperr_string(r));
187 1.1 thorpej
188 1.1 thorpej if (hostname != NULL)
189 1.1 thorpej push(strlen(hostname), hostname);
190 1.1 thorpej else {
191 1.1 thorpej r = yp_master(Domain, ypmap, &master);
192 1.1 thorpej if (r != 0)
193 1.1 thorpej errx(1, "%s (map = %s)", yperr_string(r), ypmap);
194 1.1 thorpej
195 1.1 thorpej if (Verbose)
196 1.1 thorpej printf("Contacting master for ypservers (%s).\n",
197 1.1 thorpej master);
198 1.1 thorpej
199 1.1 thorpej client = yp_bind_host(master, YPPROG, YPVERS, 0, 1);
200 1.1 thorpej
201 1.1 thorpej ypcb.foreach = (int(*)())pushit;
202 1.1 thorpej ypcb.data = NULL;
203 1.1 thorpej
204 1.1 thorpej r = yp_all_host(client, Domain, ypmap, &ypcb);
205 1.1 thorpej if (r != 0)
206 1.1 thorpej errx(1, "%s (map = %s)", yperr_string(r), ypmap);
207 1.1 thorpej }
208 1.1 thorpej exit (0);
209 1.1 thorpej }
210 1.1 thorpej
211 1.1 thorpej void
212 1.1 thorpej usage()
213 1.1 thorpej {
214 1.1 thorpej
215 1.1 thorpej fprintf(stderr, "usage: %s [-d domainname] [-h host] [-v] mapname\n",
216 1.1 thorpej __progname);
217 1.1 thorpej exit(1);
218 1.1 thorpej }
219 1.1 thorpej
220 1.1 thorpej void
221 1.1 thorpej _svc_run()
222 1.1 thorpej {
223 1.1 thorpej fd_set readfds;
224 1.1 thorpej struct timeval timeout;
225 1.1 thorpej
226 1.1 thorpej timeout.tv_sec = 60;
227 1.1 thorpej timeout.tv_usec = 0;
228 1.1 thorpej
229 1.1 thorpej for(;;) {
230 1.1 thorpej readfds = svc_fdset;
231 1.1 thorpej
232 1.1 thorpej switch (select(_rpc_dtablesize(), &readfds, NULL, NULL,
233 1.1 thorpej &timeout)) {
234 1.1 thorpej
235 1.1 thorpej case -1:
236 1.1 thorpej if (errno == EINTR)
237 1.1 thorpej continue;
238 1.1 thorpej
239 1.1 thorpej warnx("_svc_run: select failed");
240 1.1 thorpej return;
241 1.1 thorpej
242 1.1 thorpej case 0:
243 1.1 thorpej errx(0, "Callback timed out.");
244 1.1 thorpej
245 1.1 thorpej default:
246 1.1 thorpej svc_getreqset(&readfds);
247 1.1 thorpej }
248 1.1 thorpej }
249 1.1 thorpej }
250 1.1 thorpej
251 1.1 thorpej void
252 1.1 thorpej req_xfr(pid, prog, transp, host, client)
253 1.1 thorpej pid_t pid;
254 1.1 thorpej u_int prog;
255 1.1 thorpej SVCXPRT *transp;
256 1.1 thorpej char *host;
257 1.1 thorpej CLIENT *client;
258 1.1 thorpej {
259 1.1 thorpej struct ypreq_xfr request;
260 1.1 thorpej struct timeval tv;
261 1.1 thorpej
262 1.1 thorpej tv.tv_sec = 0;
263 1.1 thorpej tv.tv_usec = 0;
264 1.1 thorpej
265 1.1 thorpej request.map_parms.domain = &Domain[0];
266 1.1 thorpej request.map_parms.map = &Map[0];
267 1.1 thorpej request.map_parms.owner = &LocalHost[0];
268 1.1 thorpej request.map_parms.ordernum = OrderNum;
269 1.1 thorpej request.transid = (u_int)pid;
270 1.1 thorpej request.proto = prog;
271 1.1 thorpej request.port = transp->xp_port;
272 1.1 thorpej
273 1.1 thorpej if (Verbose)
274 1.1 thorpej printf("%d: %s(%d@%s) -> %s@%s\n", request.transid,
275 1.1 thorpej request.map_parms.map, request.map_parms.ordernum,
276 1.1 thorpej host, request.map_parms.owner, request.map_parms.domain);
277 1.1 thorpej
278 1.1 thorpej switch (clnt_call(client, YPPROC_XFR, xdr_ypreq_xfr, &request,
279 1.1 thorpej xdr_void, NULL, tv)) {
280 1.1 thorpej case RPC_SUCCESS:
281 1.1 thorpej case RPC_TIMEDOUT:
282 1.1 thorpej break;
283 1.1 thorpej
284 1.1 thorpej default:
285 1.1 thorpej clnt_perror(client, "yppush: Cannot call YPPROC_XFR");
286 1.1 thorpej kill(pid, SIGTERM);
287 1.1 thorpej }
288 1.1 thorpej }
289 1.1 thorpej
290 1.1 thorpej void
291 1.1 thorpej push(inlen, indata)
292 1.1 thorpej int inlen;
293 1.1 thorpej char *indata;
294 1.1 thorpej {
295 1.1 thorpej char host[YPMAXPEER];
296 1.1 thorpej CLIENT *client;
297 1.1 thorpej SVCXPRT *transp;
298 1.1 thorpej int sock = RPC_ANYSOCK;
299 1.1 thorpej u_int prog;
300 1.1 thorpej bool_t sts;
301 1.1 thorpej pid_t pid;
302 1.1 thorpej int status;
303 1.1 thorpej struct rusage res;
304 1.1 thorpej
305 1.1 thorpej memset(host, 0, sizeof(host));
306 1.1 thorpej snprintf(host, sizeof(host), "%*.*s", inlen, inlen, indata);
307 1.1 thorpej
308 1.1 thorpej client = clnt_create(host, YPPROG, YPVERS, "tcp");
309 1.1 thorpej if (client == NULL) {
310 1.1 thorpej if (Verbose)
311 1.1 thorpej fprintf(stderr, "Target host: %s\n", host);
312 1.1 thorpej clnt_pcreateerror("yppush: cannot create client");
313 1.1 thorpej return;
314 1.1 thorpej }
315 1.1 thorpej
316 1.1 thorpej transp = svcudp_create(sock);
317 1.1 thorpej if (transp == NULL) {
318 1.1 thorpej warnx("cannot create callback transport");
319 1.1 thorpej return;
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej for (prog = 0x40000000; prog < 0x5fffffff; prog++) {
323 1.1 thorpej if (sts = svc_register(transp, prog, 1,
324 1.1 thorpej yppush_xfrrespprog_1, IPPROTO_UDP))
325 1.1 thorpej break;
326 1.1 thorpej }
327 1.1 thorpej
328 1.1 thorpej if (sts == FALSE) {
329 1.1 thorpej warnx("cannot register callback");
330 1.1 thorpej return;
331 1.1 thorpej }
332 1.1 thorpej
333 1.1 thorpej switch ((pid = fork())) {
334 1.1 thorpej case -1:
335 1.1 thorpej err(1, "fork failed");
336 1.1 thorpej
337 1.1 thorpej case 0:
338 1.1 thorpej _svc_run();
339 1.1 thorpej exit(0);
340 1.1 thorpej
341 1.1 thorpej default:
342 1.1 thorpej close(transp->xp_sock);
343 1.1 thorpej req_xfr(pid, prog, transp, host, client);
344 1.1 thorpej wait4(pid, &status, 0, &res);
345 1.1 thorpej svc_unregister(prog, 1);
346 1.1 thorpej if (client != NULL)
347 1.1 thorpej clnt_destroy(client);
348 1.1 thorpej }
349 1.1 thorpej }
350 1.1 thorpej
351 1.1 thorpej int
352 1.1 thorpej pushit(instatus, inkey, inkeylen, inval, invallen, indata)
353 1.1 thorpej int instatus;
354 1.1 thorpej char *inkey;
355 1.1 thorpej int inkeylen;
356 1.1 thorpej char *inval;
357 1.1 thorpej int invallen;
358 1.1 thorpej char *indata;
359 1.1 thorpej {
360 1.1 thorpej
361 1.1 thorpej if (instatus != YP_TRUE)
362 1.1 thorpej return instatus;
363 1.1 thorpej
364 1.2 thorpej push(inkeylen, inkey);
365 1.1 thorpej return 0;
366 1.1 thorpej }
367 1.1 thorpej
368 1.1 thorpej void
369 1.1 thorpej cleanup()
370 1.1 thorpej {
371 1.1 thorpej
372 1.1 thorpej /* Make sure the map file is closed. */
373 1.1 thorpej if (yp_databas != NULL)
374 1.1 thorpej ypdb_close(yp_databas);
375 1.1 thorpej }
376