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