xref: /openbsd-src/lib/libc/rpc/pmap_rmt.c (revision e512315e955209fedb2d53461c3973f04fc1dd55)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: pmap_rmt.c,v 1.16 1998/08/14 21:39:37 deraadt Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 /*
35  * pmap_rmt.c
36  * Client interface to pmap rpc service.
37  * remote call and broadcast service
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41 
42 #include <rpc/rpc.h>
43 #include <rpc/pmap_prot.h>
44 #include <rpc/pmap_clnt.h>
45 #include <rpc/pmap_rmt.h>
46 #include <sys/socket.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <net/if.h>
53 #include <sys/ioctl.h>
54 #include <arpa/inet.h>
55 #define MAX_BROADCAST_SIZE 1400
56 
57 static struct timeval timeout = { 3, 0 };
58 
59 
60 /*
61  * pmapper remote-call-service interface.
62  * This routine is used to call the pmapper remote call service
63  * which will look up a service program in the port maps, and then
64  * remotely call that routine with the given parameters.  This allows
65  * programs to do a lookup and call in one step.
66 */
67 enum clnt_stat
68 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
69 	struct sockaddr_in *addr;
70 	u_long prog, vers, proc;
71 	xdrproc_t xdrargs, xdrres;
72 	caddr_t argsp, resp;
73 	struct timeval tout;
74 	u_long *port_ptr;
75 {
76 	int sock = -1;
77 	register CLIENT *client;
78 	struct rmtcallargs a;
79 	struct rmtcallres r;
80 	enum clnt_stat stat;
81 
82 	addr->sin_port = htons(PMAPPORT);
83 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
84 	if (client != (CLIENT *)NULL) {
85 		a.prog = prog;
86 		a.vers = vers;
87 		a.proc = proc;
88 		a.args_ptr = argsp;
89 		a.xdr_args = xdrargs;
90 		r.port_ptr = port_ptr;
91 		r.results_ptr = resp;
92 		r.xdr_results = xdrres;
93 		stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
94 		    xdr_rmtcallres, &r, tout);
95 		CLNT_DESTROY(client);
96 	} else {
97 		stat = RPC_FAILED;
98 	}
99 	if (sock != -1)
100 		(void)close(sock);
101 	addr->sin_port = 0;
102 	return (stat);
103 }
104 
105 
106 /*
107  * XDR remote call arguments
108  * written for XDR_ENCODE direction only
109  */
110 bool_t
111 xdr_rmtcall_args(xdrs, cap)
112 	register XDR *xdrs;
113 	register struct rmtcallargs *cap;
114 {
115 	u_int lenposition, argposition, position;
116 
117 	if (xdr_u_long(xdrs, &(cap->prog)) &&
118 	    xdr_u_long(xdrs, &(cap->vers)) &&
119 	    xdr_u_long(xdrs, &(cap->proc))) {
120 		lenposition = XDR_GETPOS(xdrs);
121 		if (! xdr_u_long(xdrs, &(cap->arglen)))
122 		    return (FALSE);
123 		argposition = XDR_GETPOS(xdrs);
124 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
125 		    return (FALSE);
126 		position = XDR_GETPOS(xdrs);
127 		cap->arglen = (u_long)position - (u_long)argposition;
128 		XDR_SETPOS(xdrs, lenposition);
129 		if (! xdr_u_long(xdrs, &(cap->arglen)))
130 		    return (FALSE);
131 		XDR_SETPOS(xdrs, position);
132 		return (TRUE);
133 	}
134 	return (FALSE);
135 }
136 
137 /*
138  * XDR remote call results
139  * written for XDR_DECODE direction only
140  */
141 bool_t
142 xdr_rmtcallres(xdrs, crp)
143 	register XDR *xdrs;
144 	register struct rmtcallres *crp;
145 {
146 	caddr_t port_ptr;
147 
148 	port_ptr = (caddr_t)crp->port_ptr;
149 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
150 	    xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
151 		crp->port_ptr = (u_long *)port_ptr;
152 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
153 	}
154 	return (FALSE);
155 }
156 
157 
158 /*
159  * The following is kludged-up support for simple rpc broadcasts.
160  * Someday a large, complicated system will replace these trivial
161  * routines which only support udp/ip .
162  */
163 
164 static int
165 newgetbroadcastnets(addrsp, sock)
166 	struct in_addr **addrsp;
167 	int sock;  /* any valid socket will do */
168 {
169 	char *inbuf = NULL, *ninbuf;
170 	struct ifconf ifc;
171 	struct ifreq ifreq, *ifr;
172 	struct sockaddr_in *sin;
173 	char *cp, *cplim;
174 	int inbuflen = 256;
175 	struct in_addr *addrs;
176 	int i = 0;
177 
178 	while (1) {
179 		ifc.ifc_len = inbuflen;
180 		ninbuf = realloc(inbuf, inbuflen);
181 		if (ninbuf == NULL) {
182 			if (inbuf)
183 				free(inbuf);
184 			return (0);
185 		}
186 		ifc.ifc_buf = inbuf = ninbuf;
187 		if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
188 			perror("broadcast: ioctl (get interface configuration)");
189 			free(inbuf);
190 			return (0);
191 		}
192 		if (ifc.ifc_len + sizeof(ifreq) < inbuflen)
193 			break;
194 		inbuflen *= 2;
195 	}
196 	addrs = (struct in_addr *)malloc((inbuflen / sizeof *sin) * sizeof sin);
197 	if (addrs == NULL) {
198 		*addrsp = NULL;
199 		free(inbuf);
200 		return (0);
201 	}
202 
203 #define max(a, b) (a > b ? a : b)
204 #define size(p)	max((p).sa_len, sizeof(p))
205 	cplim = inbuf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
206 	for (cp = inbuf; cp < cplim;
207 	    cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
208 		ifr = (struct ifreq *)cp;
209 		if (ifr->ifr_addr.sa_family != AF_INET)
210 			continue;
211 		ifreq = *ifr;
212 		if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
213 			perror("broadcast: ioctl (get interface flags)");
214 			continue;
215 		}
216 		if ((ifreq.ifr_flags & IFF_BROADCAST) &&
217 		    (ifreq.ifr_flags & IFF_UP)) {
218 			sin = (struct sockaddr_in *)&ifr->ifr_addr;
219 			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
220 				addrs[i++] =
221 				    inet_makeaddr(inet_netof(sin->sin_addr),
222 				    INADDR_ANY);
223 			} else {
224 				addrs[i++] = ((struct sockaddr_in*)
225 				  &ifreq.ifr_addr)->sin_addr;
226 			}
227 		}
228 	}
229 	free(inbuf);
230 	*addrsp = addrs;
231 	return (i);
232 }
233 
234 typedef bool_t (*resultproc_t)();
235 
236 enum clnt_stat
237 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
238 	u_long		prog;		/* program number */
239 	u_long		vers;		/* version number */
240 	u_long		proc;		/* procedure number */
241 	xdrproc_t	xargs;		/* xdr routine for args */
242 	caddr_t		argsp;		/* pointer to args */
243 	xdrproc_t	xresults;	/* xdr routine for results */
244 	caddr_t		resultsp;	/* pointer to results */
245 	resultproc_t	eachresult;	/* call with each result obtained */
246 {
247 	enum clnt_stat stat;
248 	AUTH *unix_auth = authunix_create_default();
249 	XDR xdr_stream;
250 	register XDR *xdrs = &xdr_stream;
251 	int outlen, inlen, fromlen, nets;
252 	register int sock = -1;
253 	int on = 1;
254 	fd_set *fds, readfds;
255 	register int i;
256 	bool_t done = FALSE;
257 	register u_long xid;
258 	u_long port;
259 	struct in_addr *addrs;
260 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
261 	struct rmtcallargs a;
262 	struct rmtcallres r;
263 	struct rpc_msg msg;
264 	struct timeval t;
265 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
266 
267 	/*
268 	 * initialization: create a socket, a broadcast address, and
269 	 * preserialize the arguments into a send buffer.
270 	 */
271 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
272 		perror("Cannot create socket for broadcast rpc");
273 		stat = RPC_CANTSEND;
274 		goto done_broad;
275 	}
276 #ifdef SO_BROADCAST
277 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
278 		perror("Cannot set socket option SO_BROADCAST");
279 		stat = RPC_CANTSEND;
280 		goto done_broad;
281 	}
282 #endif /* def SO_BROADCAST */
283 
284 	if (sock+1 > FD_SETSIZE) {
285 		int bytes = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
286 		fds = (fd_set *)malloc(bytes);
287 		if (fds == NULL) {
288 			stat = RPC_CANTSEND;
289 			goto done_broad;
290 		}
291 		memset(fds, 0, bytes);
292 	} else {
293 		fds = &readfds;
294 		FD_ZERO(fds);
295 	}
296 
297 	nets = newgetbroadcastnets(&addrs, sock);
298 	memset(&baddr, 0, sizeof (baddr));
299 	baddr.sin_len = sizeof(struct sockaddr_in);
300 	baddr.sin_family = AF_INET;
301 	baddr.sin_port = htons(PMAPPORT);
302 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
303 	(void)gettimeofday(&t, (struct timezone *)0);
304 	msg.rm_xid = xid = arc4random();
305 	t.tv_usec = 0;
306 	msg.rm_direction = CALL;
307 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
308 	msg.rm_call.cb_prog = PMAPPROG;
309 	msg.rm_call.cb_vers = PMAPVERS;
310 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
311 	msg.rm_call.cb_cred = unix_auth->ah_cred;
312 	msg.rm_call.cb_verf = unix_auth->ah_verf;
313 	a.prog = prog;
314 	a.vers = vers;
315 	a.proc = proc;
316 	a.xdr_args = xargs;
317 	a.args_ptr = argsp;
318 	r.port_ptr = &port;
319 	r.xdr_results = xresults;
320 	r.results_ptr = resultsp;
321 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
322 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
323 		stat = RPC_CANTENCODEARGS;
324 		goto done_broad;
325 	}
326 	outlen = (int)xdr_getpos(xdrs);
327 	xdr_destroy(xdrs);
328 
329 	/*
330 	 * Basic loop: broadcast a packet and wait a while for response(s).
331 	 * The response timeout grows larger per iteration.
332 	 *
333 	 * XXX This will loop about 5 times the stop. If there are
334 	 * lots of signals being received by the process it will quit
335 	 * send them all in one quick burst, not paying attention to
336 	 * the intended function of sending them slowly over half a
337 	 * minute or so
338 	 */
339 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
340 		for (i = 0; i < nets; i++) {
341 			baddr.sin_addr = addrs[i];
342 			if (sendto(sock, outbuf, outlen, 0,
343 			    (struct sockaddr *)&baddr,
344 			    sizeof (struct sockaddr)) != outlen) {
345 				perror("Cannot send broadcast packet");
346 				stat = RPC_CANTSEND;
347 				goto done_broad;
348 			}
349 		}
350 		if (eachresult == NULL) {
351 			stat = RPC_SUCCESS;
352 			goto done_broad;
353 		}
354 	recv_again:
355 		msg.acpted_rply.ar_verf = _null_auth;
356 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
357 		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
358 
359 		/* XXX we know the other bits are still clear */
360 		FD_SET(sock, fds);
361 		switch (select(sock+1, fds, NULL, NULL, &t)) {
362 		case 0:  /* timed out */
363 			stat = RPC_TIMEDOUT;
364 			continue;
365 		case -1:  /* some kind of error */
366 			if (errno == EINTR)
367 				goto recv_again;
368 			perror("Broadcast select problem");
369 			stat = RPC_CANTRECV;
370 			goto done_broad;
371 		}
372 	try_again:
373 		fromlen = sizeof(struct sockaddr);
374 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
375 		    (struct sockaddr *)&raddr, &fromlen);
376 		if (inlen < 0) {
377 			if (errno == EINTR)
378 				goto try_again;
379 			perror("Cannot receive reply to broadcast");
380 			stat = RPC_CANTRECV;
381 			goto done_broad;
382 		}
383 		if (inlen < sizeof(u_int32_t))
384 			goto recv_again;
385 		/*
386 		 * see if reply transaction id matches sent id.
387 		 * If so, decode the results.
388 		 */
389 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
390 		if (xdr_replymsg(xdrs, &msg)) {
391 			if ((msg.rm_xid == xid) &&
392 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
393 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
394 				raddr.sin_port = htons((u_short)port);
395 				done = (*eachresult)(resultsp, &raddr);
396 			}
397 			/* otherwise, we just ignore the errors ... */
398 		}
399 		xdrs->x_op = XDR_FREE;
400 		msg.acpted_rply.ar_results.proc = xdr_void;
401 		(void)xdr_replymsg(xdrs, &msg);
402 		(void)(*xresults)(xdrs, resultsp);
403 		xdr_destroy(xdrs);
404 		if (done) {
405 			stat = RPC_SUCCESS;
406 			goto done_broad;
407 		} else {
408 			goto recv_again;
409 		}
410 	}
411 done_broad:
412 	if (addrs)
413 		free(addrs);
414 	if (fds != &readfds)
415 		free(fds);
416 	if (sock >= 0)
417 		(void)close(sock);
418 	AUTH_DESTROY(unix_auth);
419 	return (stat);
420 }
421