xref: /openbsd-src/lib/libc/rpc/pmap_rmt.c (revision 44cae175c1d2a32dc298b5246742509546283053)
1 /*	$OpenBSD: pmap_rmt.c,v 1.26 2006/03/31 17:31:59 deraadt 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 static int
154 newgetbroadcastnets(struct in_addr **addrsp)
155 {
156 	struct ifaddrs *ifap, *ifa;
157 	struct sockaddr_in *sin;
158 	struct in_addr *addrs;
159 	int i = 0, n = 0;
160 
161 	if (getifaddrs(&ifap) != 0)
162 		return 0;
163 
164 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
165 		if (ifa->ifa_addr->sa_family != AF_INET)
166 			continue;
167 		if ((ifa->ifa_flags & IFF_BROADCAST) &&
168 		    (ifa->ifa_flags & IFF_UP) &&
169 		    ifa->ifa_broadaddr &&
170 		    ifa->ifa_broadaddr->sa_family == AF_INET) {
171 			n++;
172 		}
173 	}
174 
175 	addrs = (struct in_addr *)malloc(n * sizeof(*addrs));
176 	if (addrs == NULL) {
177 		freeifaddrs(ifap);
178 		return 0;
179 	}
180 
181 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
182 		if (ifa->ifa_addr->sa_family != AF_INET)
183 			continue;
184 		if ((ifa->ifa_flags & IFF_BROADCAST) &&
185 		    (ifa->ifa_flags & IFF_UP) &&
186 		    ifa->ifa_broadaddr &&
187 		    ifa->ifa_broadaddr->sa_family == AF_INET) {
188 			sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
189 			addrs[i++] = sin->sin_addr;
190 		}
191 	}
192 
193 	freeifaddrs(ifap);
194 	*addrsp = addrs;
195 	return i;
196 }
197 
198 typedef bool_t (*resultproc_t)();
199 
200 enum clnt_stat
201 clnt_broadcast(u_long prog,	/* program number */
202     u_long vers,		/* version number */
203     u_long proc,		/* procedure number */
204     xdrproc_t xargs,		/* xdr routine for args */
205     caddr_t argsp,		/* pointer to args */
206     xdrproc_t xresults,		/* xdr routine for results */
207     caddr_t resultsp,		/* pointer to results */
208     resultproc_t eachresult)	/* call with each result obtained */
209 {
210 	enum clnt_stat stat;
211 	AUTH *unix_auth = authunix_create_default();
212 	XDR xdr_stream;
213 	XDR *xdrs = &xdr_stream;
214 	int outlen, inlen, nets;
215 	socklen_t fromlen;
216 	int sock = -1;
217 	int on = 1;
218 	struct pollfd pfd[1];
219 	int i;
220 	int timo;
221 	bool_t done = FALSE;
222 	u_long xid;
223 	u_long port;
224 	struct in_addr *addrs = NULL;
225 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
226 	struct rmtcallargs a;
227 	struct rmtcallres r;
228 	struct rpc_msg msg;
229 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
230 
231 	/*
232 	 * initialization: create a socket, a broadcast address, and
233 	 * preserialize the arguments into a send buffer.
234 	 */
235 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
236 		stat = RPC_CANTSEND;
237 		goto done_broad;
238 	}
239 #ifdef SO_BROADCAST
240 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
241 		stat = RPC_CANTSEND;
242 		goto done_broad;
243 	}
244 #endif /* def SO_BROADCAST */
245 
246 	pfd[0].fd = sock;
247 	pfd[0].events = POLLIN;
248 
249 	nets = newgetbroadcastnets(&addrs);
250 	if (nets == 0) {
251 		stat = RPC_CANTSEND;
252 		goto done_broad;
253 	}
254 
255 	memset(&baddr, 0, sizeof (baddr));
256 	baddr.sin_len = sizeof(struct sockaddr_in);
257 	baddr.sin_family = AF_INET;
258 	baddr.sin_port = htons(PMAPPORT);
259 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
260 	msg.rm_xid = xid = arc4random();
261 	msg.rm_direction = CALL;
262 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
263 	msg.rm_call.cb_prog = PMAPPROG;
264 	msg.rm_call.cb_vers = PMAPVERS;
265 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
266 	msg.rm_call.cb_cred = unix_auth->ah_cred;
267 	msg.rm_call.cb_verf = unix_auth->ah_verf;
268 	a.prog = prog;
269 	a.vers = vers;
270 	a.proc = proc;
271 	a.xdr_args = xargs;
272 	a.args_ptr = argsp;
273 	r.port_ptr = &port;
274 	r.xdr_results = xresults;
275 	r.results_ptr = resultsp;
276 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
277 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
278 		stat = RPC_CANTENCODEARGS;
279 		goto done_broad;
280 	}
281 	outlen = (int)xdr_getpos(xdrs);
282 	xdr_destroy(xdrs);
283 
284 	/*
285 	 * Basic loop: broadcast a packet and wait a while for response(s).
286 	 * The response timeout grows larger per iteration.
287 	 *
288 	 * XXX This will loop about 5 times the stop. If there are
289 	 * lots of signals being received by the process it will quit
290 	 * send them all in one quick burst, not paying attention to
291 	 * the intended function of sending them slowly over half a
292 	 * minute or so
293 	 */
294 	for (timo = 4000; timo <= 14000; timo += 2000) {
295 		for (i = 0; i < nets; i++) {
296 			baddr.sin_addr = addrs[i];
297 			if (sendto(sock, outbuf, outlen, 0,
298 			    (struct sockaddr *)&baddr,
299 			    sizeof (struct sockaddr)) != outlen) {
300 				stat = RPC_CANTSEND;
301 				goto done_broad;
302 			}
303 		}
304 		if (eachresult == NULL) {
305 			stat = RPC_SUCCESS;
306 			goto done_broad;
307 		}
308 	recv_again:
309 		msg.acpted_rply.ar_verf = _null_auth;
310 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
311 		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
312 
313 		switch (poll(pfd, 1, timo)) {
314 		case 0:  /* timed out */
315 			stat = RPC_TIMEDOUT;
316 			continue;
317 		case 1:
318 			if (pfd[0].revents & POLLNVAL)
319 				errno = EBADF;
320 			else if (pfd[0].revents & POLLERR)
321 				errno = EIO;
322 			else
323 				break;
324 			/* FALLTHROUGH */
325 		case -1:  /* some kind of error */
326 			if (errno == EINTR)
327 				goto recv_again;
328 			stat = RPC_CANTRECV;
329 			goto done_broad;
330 		}
331 	try_again:
332 		fromlen = sizeof(struct sockaddr);
333 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
334 		    (struct sockaddr *)&raddr, &fromlen);
335 		if (inlen < 0) {
336 			if (errno == EINTR)
337 				goto try_again;
338 			stat = RPC_CANTRECV;
339 			goto done_broad;
340 		}
341 		if (inlen < sizeof(u_int32_t))
342 			goto recv_again;
343 		/*
344 		 * see if reply transaction id matches sent id.
345 		 * If so, decode the results.
346 		 */
347 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
348 		if (xdr_replymsg(xdrs, &msg)) {
349 			if ((msg.rm_xid == xid) &&
350 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
351 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
352 				raddr.sin_port = htons((u_short)port);
353 				done = (*eachresult)(resultsp, &raddr);
354 			}
355 			/* otherwise, we just ignore the errors ... */
356 		}
357 		xdrs->x_op = XDR_FREE;
358 		msg.acpted_rply.ar_results.proc = xdr_void;
359 		(void)xdr_replymsg(xdrs, &msg);
360 		(void)(*xresults)(xdrs, resultsp);
361 		xdr_destroy(xdrs);
362 		if (done) {
363 			stat = RPC_SUCCESS;
364 			goto done_broad;
365 		} else {
366 			goto recv_again;
367 		}
368 	}
369 done_broad:
370 	if (addrs)
371 		free(addrs);
372 	if (sock >= 0)
373 		(void)close(sock);
374 	AUTH_DESTROY(unix_auth);
375 	return (stat);
376 }
377