xref: /openbsd-src/lib/libc/rpc/pmap_rmt.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
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.20 2002/09/06 18:35:12 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, nets;
233 	socklen_t fromlen;
234 	int sock = -1;
235 	int on = 1;
236 	fd_set *fds = NULL, readfds;
237 	int i;
238 	bool_t done = FALSE;
239 	u_long xid;
240 	u_long port;
241 	struct in_addr *addrs;
242 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
243 	struct rmtcallargs a;
244 	struct rmtcallres r;
245 	struct rpc_msg msg;
246 	struct timeval t;
247 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
248 
249 	/*
250 	 * initialization: create a socket, a broadcast address, and
251 	 * preserialize the arguments into a send buffer.
252 	 */
253 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
254 		perror("Cannot create socket for broadcast rpc");
255 		stat = RPC_CANTSEND;
256 		goto done_broad;
257 	}
258 #ifdef SO_BROADCAST
259 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
260 		perror("Cannot set socket option SO_BROADCAST");
261 		stat = RPC_CANTSEND;
262 		goto done_broad;
263 	}
264 #endif /* def SO_BROADCAST */
265 
266 	if (sock+1 > FD_SETSIZE) {
267 		int bytes = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
268 		fds = (fd_set *)malloc(bytes);
269 		if (fds == NULL) {
270 			stat = RPC_CANTSEND;
271 			goto done_broad;
272 		}
273 		memset(fds, 0, bytes);
274 	} else {
275 		fds = &readfds;
276 		FD_ZERO(fds);
277 	}
278 
279 	nets = newgetbroadcastnets(&addrs, sock);
280 	memset(&baddr, 0, sizeof (baddr));
281 	baddr.sin_len = sizeof(struct sockaddr_in);
282 	baddr.sin_family = AF_INET;
283 	baddr.sin_port = htons(PMAPPORT);
284 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
285 	(void)gettimeofday(&t, (struct timezone *)0);
286 	msg.rm_xid = xid = arc4random();
287 	t.tv_usec = 0;
288 	msg.rm_direction = CALL;
289 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
290 	msg.rm_call.cb_prog = PMAPPROG;
291 	msg.rm_call.cb_vers = PMAPVERS;
292 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
293 	msg.rm_call.cb_cred = unix_auth->ah_cred;
294 	msg.rm_call.cb_verf = unix_auth->ah_verf;
295 	a.prog = prog;
296 	a.vers = vers;
297 	a.proc = proc;
298 	a.xdr_args = xargs;
299 	a.args_ptr = argsp;
300 	r.port_ptr = &port;
301 	r.xdr_results = xresults;
302 	r.results_ptr = resultsp;
303 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
304 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
305 		stat = RPC_CANTENCODEARGS;
306 		goto done_broad;
307 	}
308 	outlen = (int)xdr_getpos(xdrs);
309 	xdr_destroy(xdrs);
310 
311 	/*
312 	 * Basic loop: broadcast a packet and wait a while for response(s).
313 	 * The response timeout grows larger per iteration.
314 	 *
315 	 * XXX This will loop about 5 times the stop. If there are
316 	 * lots of signals being received by the process it will quit
317 	 * send them all in one quick burst, not paying attention to
318 	 * the intended function of sending them slowly over half a
319 	 * minute or so
320 	 */
321 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
322 		for (i = 0; i < nets; i++) {
323 			baddr.sin_addr = addrs[i];
324 			if (sendto(sock, outbuf, outlen, 0,
325 			    (struct sockaddr *)&baddr,
326 			    sizeof (struct sockaddr)) != outlen) {
327 				perror("Cannot send broadcast packet");
328 				stat = RPC_CANTSEND;
329 				goto done_broad;
330 			}
331 		}
332 		if (eachresult == NULL) {
333 			stat = RPC_SUCCESS;
334 			goto done_broad;
335 		}
336 	recv_again:
337 		msg.acpted_rply.ar_verf = _null_auth;
338 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
339 		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
340 
341 		/* XXX we know the other bits are still clear */
342 		FD_SET(sock, fds);
343 		switch (select(sock+1, fds, NULL, NULL, &t)) {
344 		case 0:  /* timed out */
345 			stat = RPC_TIMEDOUT;
346 			continue;
347 		case -1:  /* some kind of error */
348 			if (errno == EINTR)
349 				goto recv_again;
350 			perror("Broadcast select problem");
351 			stat = RPC_CANTRECV;
352 			goto done_broad;
353 		}
354 	try_again:
355 		fromlen = sizeof(struct sockaddr);
356 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
357 		    (struct sockaddr *)&raddr, &fromlen);
358 		if (inlen < 0) {
359 			if (errno == EINTR)
360 				goto try_again;
361 			perror("Cannot receive reply to broadcast");
362 			stat = RPC_CANTRECV;
363 			goto done_broad;
364 		}
365 		if (inlen < sizeof(u_int32_t))
366 			goto recv_again;
367 		/*
368 		 * see if reply transaction id matches sent id.
369 		 * If so, decode the results.
370 		 */
371 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
372 		if (xdr_replymsg(xdrs, &msg)) {
373 			if ((msg.rm_xid == xid) &&
374 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
375 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
376 				raddr.sin_port = htons((u_short)port);
377 				done = (*eachresult)(resultsp, &raddr);
378 			}
379 			/* otherwise, we just ignore the errors ... */
380 		}
381 		xdrs->x_op = XDR_FREE;
382 		msg.acpted_rply.ar_results.proc = xdr_void;
383 		(void)xdr_replymsg(xdrs, &msg);
384 		(void)(*xresults)(xdrs, resultsp);
385 		xdr_destroy(xdrs);
386 		if (done) {
387 			stat = RPC_SUCCESS;
388 			goto done_broad;
389 		} else {
390 			goto recv_again;
391 		}
392 	}
393 done_broad:
394 	if (addrs)
395 		free(addrs);
396 	if (fds != &readfds)
397 		free(fds);
398 	if (sock >= 0)
399 		(void)close(sock);
400 	AUTH_DESTROY(unix_auth);
401 	return (stat);
402 }
403