xref: /openbsd/lib/libc/rpc/pmap_rmt.c (revision cecf84d4)
1 /*	$OpenBSD: pmap_rmt.c,v 1.31 2014/11/11 04:51:49 guenther Exp $ */
2 
3 /*
4  * Copyright (c) 2010, Oracle America, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials
15  *       provided with the distribution.
16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * pmap_rmt.c
36  * Client interface to pmap rpc service.
37  * remote call and broadcast service
38  */
39 
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_prot.h>
42 #include <rpc/pmap_clnt.h>
43 #include <rpc/pmap_rmt.h>
44 #include <sys/socket.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <net/if.h>
51 #include <sys/ioctl.h>
52 #include <arpa/inet.h>
53 #include <ifaddrs.h>
54 #define MAX_BROADCAST_SIZE 1400
55 
56 static struct timeval timeout = { 3, 0 };
57 
58 
59 /*
60  * pmapper remote-call-service interface.
61  * This routine is used to call the pmapper remote call service
62  * which will look up a service program in the port maps, and then
63  * remotely call that routine with the given parameters.  This allows
64  * programs to do a lookup and call in one step.
65 */
66 enum clnt_stat
67 pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
68     xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
69     struct timeval tout, u_long *port_ptr)
70 {
71 	int sock = -1;
72 	CLIENT *client;
73 	struct rmtcallargs a;
74 	struct rmtcallres r;
75 	enum clnt_stat stat;
76 
77 	addr->sin_port = htons(PMAPPORT);
78 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
79 	if (client != NULL) {
80 		a.prog = prog;
81 		a.vers = vers;
82 		a.proc = proc;
83 		a.args_ptr = argsp;
84 		a.xdr_args = xdrargs;
85 		r.port_ptr = port_ptr;
86 		r.results_ptr = resp;
87 		r.xdr_results = xdrres;
88 		stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
89 		    xdr_rmtcallres, &r, tout);
90 		CLNT_DESTROY(client);
91 	} else {
92 		stat = RPC_FAILED;
93 	}
94 	addr->sin_port = 0;
95 	return (stat);
96 }
97 
98 
99 /*
100  * XDR remote call arguments
101  * written for XDR_ENCODE direction only
102  */
103 bool_t
104 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
105 {
106 	u_int lenposition, argposition, position;
107 
108 	if (xdr_u_long(xdrs, &(cap->prog)) &&
109 	    xdr_u_long(xdrs, &(cap->vers)) &&
110 	    xdr_u_long(xdrs, &(cap->proc))) {
111 		lenposition = XDR_GETPOS(xdrs);
112 		if (! xdr_u_long(xdrs, &(cap->arglen)))
113 		    return (FALSE);
114 		argposition = XDR_GETPOS(xdrs);
115 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
116 		    return (FALSE);
117 		position = XDR_GETPOS(xdrs);
118 		cap->arglen = (u_long)position - (u_long)argposition;
119 		XDR_SETPOS(xdrs, lenposition);
120 		if (! xdr_u_long(xdrs, &(cap->arglen)))
121 		    return (FALSE);
122 		XDR_SETPOS(xdrs, position);
123 		return (TRUE);
124 	}
125 	return (FALSE);
126 }
127 
128 /*
129  * XDR remote call results
130  * written for XDR_DECODE direction only
131  */
132 bool_t
133 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
134 {
135 	caddr_t port_ptr;
136 
137 	port_ptr = (caddr_t)crp->port_ptr;
138 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
139 	    xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
140 		crp->port_ptr = (u_long *)port_ptr;
141 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
142 	}
143 	return (FALSE);
144 }
145 
146 
147 /*
148  * The following is kludged-up support for simple rpc broadcasts.
149  * Someday a large, complicated system will replace these trivial
150  * routines which only support udp/ip .
151  */
152 static int
153 newgetbroadcastnets(struct in_addr **addrsp)
154 {
155 	struct ifaddrs *ifap, *ifa;
156 	struct sockaddr_in *sin;
157 	struct in_addr *addrs;
158 	int i = 0, n = 0;
159 
160 	if (getifaddrs(&ifap) != 0)
161 		return 0;
162 
163 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
164 		if (ifa->ifa_addr->sa_family != AF_INET)
165 			continue;
166 		if ((ifa->ifa_flags & IFF_BROADCAST) &&
167 		    (ifa->ifa_flags & IFF_UP) &&
168 		    ifa->ifa_broadaddr &&
169 		    ifa->ifa_broadaddr->sa_family == AF_INET) {
170 			n++;
171 		}
172 	}
173 
174 	addrs = (struct in_addr *)calloc(n, sizeof(*addrs));
175 	if (addrs == NULL) {
176 		freeifaddrs(ifap);
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 			sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
188 			addrs[i++] = sin->sin_addr;
189 		}
190 	}
191 
192 	freeifaddrs(ifap);
193 	*addrsp = addrs;
194 	return i;
195 }
196 
197 typedef bool_t (*resultproc_t)(caddr_t, struct sockaddr_in *);
198 
199 enum clnt_stat
200 clnt_broadcast(u_long prog,	/* program number */
201     u_long vers,		/* version number */
202     u_long proc,		/* procedure number */
203     xdrproc_t xargs,		/* xdr routine for args */
204     caddr_t argsp,		/* pointer to args */
205     xdrproc_t xresults,		/* xdr routine for results */
206     caddr_t resultsp,		/* pointer to results */
207     resultproc_t eachresult)	/* call with each result obtained */
208 {
209 	enum clnt_stat stat;
210 	AUTH *unix_auth;
211 	XDR xdr_stream;
212 	XDR *xdrs = &xdr_stream;
213 	int outlen, inlen, nets;
214 	socklen_t fromlen;
215 	int sock = -1;
216 	int on = 1;
217 	struct pollfd pfd[1];
218 	int i;
219 	int timo;
220 	bool_t done = FALSE;
221 	u_long xid;
222 	u_long port;
223 	struct in_addr *addrs = NULL;
224 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
225 	struct rmtcallargs a;
226 	struct rmtcallres r;
227 	struct rpc_msg msg;
228 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
229 
230 	if ((unix_auth = authunix_create_default()) == NULL) {
231 		stat = RPC_AUTHERROR;
232 		goto done_broad;
233 	}
234 
235 	/*
236 	 * initialization: create a socket, a broadcast address, and
237 	 * preserialize the arguments into a send buffer.
238 	 */
239 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
240 		stat = RPC_CANTSEND;
241 		goto done_broad;
242 	}
243 #ifdef SO_BROADCAST
244 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
245 		stat = RPC_CANTSEND;
246 		goto done_broad;
247 	}
248 #endif /* def SO_BROADCAST */
249 
250 	pfd[0].fd = sock;
251 	pfd[0].events = POLLIN;
252 
253 	nets = newgetbroadcastnets(&addrs);
254 	if (nets == 0) {
255 		stat = RPC_CANTSEND;
256 		goto done_broad;
257 	}
258 
259 	memset(&baddr, 0, sizeof (baddr));
260 	baddr.sin_len = sizeof(struct sockaddr_in);
261 	baddr.sin_family = AF_INET;
262 	baddr.sin_port = htons(PMAPPORT);
263 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
264 	msg.rm_xid = xid = arc4random();
265 	msg.rm_direction = CALL;
266 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
267 	msg.rm_call.cb_prog = PMAPPROG;
268 	msg.rm_call.cb_vers = PMAPVERS;
269 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
270 	msg.rm_call.cb_cred = unix_auth->ah_cred;
271 	msg.rm_call.cb_verf = unix_auth->ah_verf;
272 	a.prog = prog;
273 	a.vers = vers;
274 	a.proc = proc;
275 	a.xdr_args = xargs;
276 	a.args_ptr = argsp;
277 	r.port_ptr = &port;
278 	r.xdr_results = xresults;
279 	r.results_ptr = resultsp;
280 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
281 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
282 		stat = RPC_CANTENCODEARGS;
283 		goto done_broad;
284 	}
285 	outlen = (int)xdr_getpos(xdrs);
286 	xdr_destroy(xdrs);
287 
288 	/*
289 	 * Basic loop: broadcast a packet and wait a while for response(s).
290 	 * The response timeout grows larger per iteration.
291 	 *
292 	 * XXX This will loop about 5 times the stop. If there are
293 	 * lots of signals being received by the process it will quit
294 	 * send them all in one quick burst, not paying attention to
295 	 * the intended function of sending them slowly over half a
296 	 * minute or so
297 	 */
298 	for (timo = 4000; timo <= 14000; timo += 2000) {
299 		for (i = 0; i < nets; i++) {
300 			baddr.sin_addr = addrs[i];
301 			if (sendto(sock, outbuf, outlen, 0,
302 			    (struct sockaddr *)&baddr,
303 			    sizeof (struct sockaddr)) != outlen) {
304 				stat = RPC_CANTSEND;
305 				goto done_broad;
306 			}
307 		}
308 		if (eachresult == NULL) {
309 			stat = RPC_SUCCESS;
310 			goto done_broad;
311 		}
312 	recv_again:
313 		msg.acpted_rply.ar_verf = _null_auth;
314 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
315 		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
316 
317 		switch (poll(pfd, 1, timo)) {
318 		case 0:  /* timed out */
319 			stat = RPC_TIMEDOUT;
320 			continue;
321 		case 1:
322 			if (pfd[0].revents & POLLNVAL)
323 				errno = EBADF;
324 			else if (pfd[0].revents & POLLERR)
325 				errno = EIO;
326 			else
327 				break;
328 			/* FALLTHROUGH */
329 		case -1:  /* some kind of error */
330 			if (errno == EINTR)
331 				goto recv_again;
332 			stat = RPC_CANTRECV;
333 			goto done_broad;
334 		}
335 	try_again:
336 		fromlen = sizeof(struct sockaddr);
337 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
338 		    (struct sockaddr *)&raddr, &fromlen);
339 		if (inlen < 0) {
340 			if (errno == EINTR)
341 				goto try_again;
342 			stat = RPC_CANTRECV;
343 			goto done_broad;
344 		}
345 		if (inlen < sizeof(u_int32_t))
346 			goto recv_again;
347 		/*
348 		 * see if reply transaction id matches sent id.
349 		 * If so, decode the results.
350 		 */
351 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
352 		if (xdr_replymsg(xdrs, &msg)) {
353 			if ((msg.rm_xid == xid) &&
354 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
355 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
356 				raddr.sin_port = htons((u_short)port);
357 				done = (*eachresult)(resultsp, &raddr);
358 			}
359 			/* otherwise, we just ignore the errors ... */
360 		}
361 		xdrs->x_op = XDR_FREE;
362 		msg.acpted_rply.ar_results.proc = xdr_void;
363 		(void)xdr_replymsg(xdrs, &msg);
364 		(void)(*xresults)(xdrs, resultsp);
365 		xdr_destroy(xdrs);
366 		if (done) {
367 			stat = RPC_SUCCESS;
368 			goto done_broad;
369 		} else {
370 			goto recv_again;
371 		}
372 	}
373 done_broad:
374 	if (addrs)
375 		free(addrs);
376 	if (sock >= 0)
377 		(void)close(sock);
378 	if (unix_auth != NULL)
379 		AUTH_DESTROY(unix_auth);
380 	return (stat);
381 }
382