xref: /openbsd-src/lib/libc/rpc/pmap_rmt.c (revision ce7e0fc6a9d74d25b78fb6ad846387717f5172b6)
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.19 2001/09/15 13:51:01 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 #include <ifaddrs.h>
56 #define MAX_BROADCAST_SIZE 1400
57 
58 static struct timeval timeout = { 3, 0 };
59 
60 
61 /*
62  * pmapper remote-call-service interface.
63  * This routine is used to call the pmapper remote call service
64  * which will look up a service program in the port maps, and then
65  * remotely call that routine with the given parameters.  This allows
66  * programs to do a lookup and call in one step.
67 */
68 enum clnt_stat
69 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
70 	struct sockaddr_in *addr;
71 	u_long prog, vers, proc;
72 	xdrproc_t xdrargs, xdrres;
73 	caddr_t argsp, resp;
74 	struct timeval tout;
75 	u_long *port_ptr;
76 {
77 	int sock = -1;
78 	CLIENT *client;
79 	struct rmtcallargs a;
80 	struct rmtcallres r;
81 	enum clnt_stat stat;
82 
83 	addr->sin_port = htons(PMAPPORT);
84 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
85 	if (client != (CLIENT *)NULL) {
86 		a.prog = prog;
87 		a.vers = vers;
88 		a.proc = proc;
89 		a.args_ptr = argsp;
90 		a.xdr_args = xdrargs;
91 		r.port_ptr = port_ptr;
92 		r.results_ptr = resp;
93 		r.xdr_results = xdrres;
94 		stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
95 		    xdr_rmtcallres, &r, tout);
96 		CLNT_DESTROY(client);
97 	} else {
98 		stat = RPC_FAILED;
99 	}
100 	if (sock != -1)
101 		(void)close(sock);
102 	addr->sin_port = 0;
103 	return (stat);
104 }
105 
106 
107 /*
108  * XDR remote call arguments
109  * written for XDR_ENCODE direction only
110  */
111 bool_t
112 xdr_rmtcall_args(xdrs, cap)
113 	XDR *xdrs;
114 	struct rmtcallargs *cap;
115 {
116 	u_int lenposition, argposition, position;
117 
118 	if (xdr_u_long(xdrs, &(cap->prog)) &&
119 	    xdr_u_long(xdrs, &(cap->vers)) &&
120 	    xdr_u_long(xdrs, &(cap->proc))) {
121 		lenposition = XDR_GETPOS(xdrs);
122 		if (! xdr_u_long(xdrs, &(cap->arglen)))
123 		    return (FALSE);
124 		argposition = XDR_GETPOS(xdrs);
125 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
126 		    return (FALSE);
127 		position = XDR_GETPOS(xdrs);
128 		cap->arglen = (u_long)position - (u_long)argposition;
129 		XDR_SETPOS(xdrs, lenposition);
130 		if (! xdr_u_long(xdrs, &(cap->arglen)))
131 		    return (FALSE);
132 		XDR_SETPOS(xdrs, position);
133 		return (TRUE);
134 	}
135 	return (FALSE);
136 }
137 
138 /*
139  * XDR remote call results
140  * written for XDR_DECODE direction only
141  */
142 bool_t
143 xdr_rmtcallres(xdrs, crp)
144 	XDR *xdrs;
145 	struct rmtcallres *crp;
146 {
147 	caddr_t port_ptr;
148 
149 	port_ptr = (caddr_t)crp->port_ptr;
150 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
151 	    xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
152 		crp->port_ptr = (u_long *)port_ptr;
153 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
154 	}
155 	return (FALSE);
156 }
157 
158 
159 /*
160  * The following is kludged-up support for simple rpc broadcasts.
161  * Someday a large, complicated system will replace these trivial
162  * routines which only support udp/ip .
163  */
164 
165 static int
166 newgetbroadcastnets(addrsp, sock)
167 	struct in_addr **addrsp;
168 	int sock;  /* any valid socket will do */
169 {
170 	struct ifaddrs *ifap, *ifa;
171 	struct sockaddr_in *sin;
172 	struct in_addr *addrs;
173 	int i = 0, n = 0;
174 
175 	if (getifaddrs(&ifap) != 0) {
176 		perror("broadcast: getifaddrs");
177 		return 0;
178 	}
179 
180 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
181 		if (ifa->ifa_addr->sa_family != AF_INET)
182 			continue;
183 		if ((ifa->ifa_flags & IFF_BROADCAST) &&
184 		    (ifa->ifa_flags & IFF_UP) &&
185 		    ifa->ifa_broadaddr &&
186 		    ifa->ifa_broadaddr->sa_family == AF_INET) {
187 			n++;
188 		}
189 	}
190 
191 	addrs = (struct in_addr *)malloc(n * sizeof(*addrs));
192 	if (addrs == NULL) {
193 		freeifaddrs(ifap);
194 		*addrsp = NULL;
195 		return 0;
196 	}
197 
198 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
199 		if (ifa->ifa_addr->sa_family != AF_INET)
200 			continue;
201 		if ((ifa->ifa_flags & IFF_BROADCAST) &&
202 		    (ifa->ifa_flags & IFF_UP) &&
203 		    ifa->ifa_broadaddr &&
204 		    ifa->ifa_broadaddr->sa_family == AF_INET) {
205 			sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
206 			addrs[i++] = sin->sin_addr;
207 		}
208 	}
209 
210 	freeifaddrs(ifap);
211 	*addrsp = addrs;
212 	return i;
213 }
214 
215 typedef bool_t (*resultproc_t)();
216 
217 enum clnt_stat
218 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
219 	u_long		prog;		/* program number */
220 	u_long		vers;		/* version number */
221 	u_long		proc;		/* procedure number */
222 	xdrproc_t	xargs;		/* xdr routine for args */
223 	caddr_t		argsp;		/* pointer to args */
224 	xdrproc_t	xresults;	/* xdr routine for results */
225 	caddr_t		resultsp;	/* pointer to results */
226 	resultproc_t	eachresult;	/* call with each result obtained */
227 {
228 	enum clnt_stat stat;
229 	AUTH *unix_auth = authunix_create_default();
230 	XDR xdr_stream;
231 	XDR *xdrs = &xdr_stream;
232 	int outlen, inlen, fromlen, nets;
233 	int sock = -1;
234 	int on = 1;
235 	fd_set *fds = NULL, readfds;
236 	int i;
237 	bool_t done = FALSE;
238 	u_long xid;
239 	u_long port;
240 	struct in_addr *addrs;
241 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
242 	struct rmtcallargs a;
243 	struct rmtcallres r;
244 	struct rpc_msg msg;
245 	struct timeval t;
246 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
247 
248 	/*
249 	 * initialization: create a socket, a broadcast address, and
250 	 * preserialize the arguments into a send buffer.
251 	 */
252 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
253 		perror("Cannot create socket for broadcast rpc");
254 		stat = RPC_CANTSEND;
255 		goto done_broad;
256 	}
257 #ifdef SO_BROADCAST
258 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
259 		perror("Cannot set socket option SO_BROADCAST");
260 		stat = RPC_CANTSEND;
261 		goto done_broad;
262 	}
263 #endif /* def SO_BROADCAST */
264 
265 	if (sock+1 > FD_SETSIZE) {
266 		int bytes = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
267 		fds = (fd_set *)malloc(bytes);
268 		if (fds == NULL) {
269 			stat = RPC_CANTSEND;
270 			goto done_broad;
271 		}
272 		memset(fds, 0, bytes);
273 	} else {
274 		fds = &readfds;
275 		FD_ZERO(fds);
276 	}
277 
278 	nets = newgetbroadcastnets(&addrs, sock);
279 	memset(&baddr, 0, sizeof (baddr));
280 	baddr.sin_len = sizeof(struct sockaddr_in);
281 	baddr.sin_family = AF_INET;
282 	baddr.sin_port = htons(PMAPPORT);
283 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
284 	(void)gettimeofday(&t, (struct timezone *)0);
285 	msg.rm_xid = xid = arc4random();
286 	t.tv_usec = 0;
287 	msg.rm_direction = CALL;
288 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
289 	msg.rm_call.cb_prog = PMAPPROG;
290 	msg.rm_call.cb_vers = PMAPVERS;
291 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
292 	msg.rm_call.cb_cred = unix_auth->ah_cred;
293 	msg.rm_call.cb_verf = unix_auth->ah_verf;
294 	a.prog = prog;
295 	a.vers = vers;
296 	a.proc = proc;
297 	a.xdr_args = xargs;
298 	a.args_ptr = argsp;
299 	r.port_ptr = &port;
300 	r.xdr_results = xresults;
301 	r.results_ptr = resultsp;
302 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
303 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
304 		stat = RPC_CANTENCODEARGS;
305 		goto done_broad;
306 	}
307 	outlen = (int)xdr_getpos(xdrs);
308 	xdr_destroy(xdrs);
309 
310 	/*
311 	 * Basic loop: broadcast a packet and wait a while for response(s).
312 	 * The response timeout grows larger per iteration.
313 	 *
314 	 * XXX This will loop about 5 times the stop. If there are
315 	 * lots of signals being received by the process it will quit
316 	 * send them all in one quick burst, not paying attention to
317 	 * the intended function of sending them slowly over half a
318 	 * minute or so
319 	 */
320 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
321 		for (i = 0; i < nets; i++) {
322 			baddr.sin_addr = addrs[i];
323 			if (sendto(sock, outbuf, outlen, 0,
324 			    (struct sockaddr *)&baddr,
325 			    sizeof (struct sockaddr)) != outlen) {
326 				perror("Cannot send broadcast packet");
327 				stat = RPC_CANTSEND;
328 				goto done_broad;
329 			}
330 		}
331 		if (eachresult == NULL) {
332 			stat = RPC_SUCCESS;
333 			goto done_broad;
334 		}
335 	recv_again:
336 		msg.acpted_rply.ar_verf = _null_auth;
337 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
338 		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
339 
340 		/* XXX we know the other bits are still clear */
341 		FD_SET(sock, fds);
342 		switch (select(sock+1, fds, NULL, NULL, &t)) {
343 		case 0:  /* timed out */
344 			stat = RPC_TIMEDOUT;
345 			continue;
346 		case -1:  /* some kind of error */
347 			if (errno == EINTR)
348 				goto recv_again;
349 			perror("Broadcast select problem");
350 			stat = RPC_CANTRECV;
351 			goto done_broad;
352 		}
353 	try_again:
354 		fromlen = sizeof(struct sockaddr);
355 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
356 		    (struct sockaddr *)&raddr, &fromlen);
357 		if (inlen < 0) {
358 			if (errno == EINTR)
359 				goto try_again;
360 			perror("Cannot receive reply to broadcast");
361 			stat = RPC_CANTRECV;
362 			goto done_broad;
363 		}
364 		if (inlen < sizeof(u_int32_t))
365 			goto recv_again;
366 		/*
367 		 * see if reply transaction id matches sent id.
368 		 * If so, decode the results.
369 		 */
370 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
371 		if (xdr_replymsg(xdrs, &msg)) {
372 			if ((msg.rm_xid == xid) &&
373 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
374 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
375 				raddr.sin_port = htons((u_short)port);
376 				done = (*eachresult)(resultsp, &raddr);
377 			}
378 			/* otherwise, we just ignore the errors ... */
379 		}
380 		xdrs->x_op = XDR_FREE;
381 		msg.acpted_rply.ar_results.proc = xdr_void;
382 		(void)xdr_replymsg(xdrs, &msg);
383 		(void)(*xresults)(xdrs, resultsp);
384 		xdr_destroy(xdrs);
385 		if (done) {
386 			stat = RPC_SUCCESS;
387 			goto done_broad;
388 		} else {
389 			goto recv_again;
390 		}
391 	}
392 done_broad:
393 	if (addrs)
394 		free(addrs);
395 	if (fds != &readfds)
396 		free(fds);
397 	if (sock >= 0)
398 		(void)close(sock);
399 	AUTH_DESTROY(unix_auth);
400 	return (stat);
401 }
402