xref: /openbsd-src/lib/libc/rpc/pmap_rmt.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: pmap_rmt.c,v 1.28 2007/09/02 15:19:17 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 *)calloc(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;
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 	if ((unix_auth = authunix_create_default()) == NULL) {
232 		stat = RPC_AUTHERROR;
233 		goto done_broad;
234 	}
235 
236 	/*
237 	 * initialization: create a socket, a broadcast address, and
238 	 * preserialize the arguments into a send buffer.
239 	 */
240 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
241 		stat = RPC_CANTSEND;
242 		goto done_broad;
243 	}
244 #ifdef SO_BROADCAST
245 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
246 		stat = RPC_CANTSEND;
247 		goto done_broad;
248 	}
249 #endif /* def SO_BROADCAST */
250 
251 	pfd[0].fd = sock;
252 	pfd[0].events = POLLIN;
253 
254 	nets = newgetbroadcastnets(&addrs);
255 	if (nets == 0) {
256 		stat = RPC_CANTSEND;
257 		goto done_broad;
258 	}
259 
260 	memset(&baddr, 0, sizeof (baddr));
261 	baddr.sin_len = sizeof(struct sockaddr_in);
262 	baddr.sin_family = AF_INET;
263 	baddr.sin_port = htons(PMAPPORT);
264 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
265 	msg.rm_xid = xid = arc4random();
266 	msg.rm_direction = CALL;
267 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
268 	msg.rm_call.cb_prog = PMAPPROG;
269 	msg.rm_call.cb_vers = PMAPVERS;
270 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
271 	msg.rm_call.cb_cred = unix_auth->ah_cred;
272 	msg.rm_call.cb_verf = unix_auth->ah_verf;
273 	a.prog = prog;
274 	a.vers = vers;
275 	a.proc = proc;
276 	a.xdr_args = xargs;
277 	a.args_ptr = argsp;
278 	r.port_ptr = &port;
279 	r.xdr_results = xresults;
280 	r.results_ptr = resultsp;
281 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
282 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
283 		stat = RPC_CANTENCODEARGS;
284 		goto done_broad;
285 	}
286 	outlen = (int)xdr_getpos(xdrs);
287 	xdr_destroy(xdrs);
288 
289 	/*
290 	 * Basic loop: broadcast a packet and wait a while for response(s).
291 	 * The response timeout grows larger per iteration.
292 	 *
293 	 * XXX This will loop about 5 times the stop. If there are
294 	 * lots of signals being received by the process it will quit
295 	 * send them all in one quick burst, not paying attention to
296 	 * the intended function of sending them slowly over half a
297 	 * minute or so
298 	 */
299 	for (timo = 4000; timo <= 14000; timo += 2000) {
300 		for (i = 0; i < nets; i++) {
301 			baddr.sin_addr = addrs[i];
302 			if (sendto(sock, outbuf, outlen, 0,
303 			    (struct sockaddr *)&baddr,
304 			    sizeof (struct sockaddr)) != outlen) {
305 				stat = RPC_CANTSEND;
306 				goto done_broad;
307 			}
308 		}
309 		if (eachresult == NULL) {
310 			stat = RPC_SUCCESS;
311 			goto done_broad;
312 		}
313 	recv_again:
314 		msg.acpted_rply.ar_verf = _null_auth;
315 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
316 		msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
317 
318 		switch (poll(pfd, 1, timo)) {
319 		case 0:  /* timed out */
320 			stat = RPC_TIMEDOUT;
321 			continue;
322 		case 1:
323 			if (pfd[0].revents & POLLNVAL)
324 				errno = EBADF;
325 			else if (pfd[0].revents & POLLERR)
326 				errno = EIO;
327 			else
328 				break;
329 			/* FALLTHROUGH */
330 		case -1:  /* some kind of error */
331 			if (errno == EINTR)
332 				goto recv_again;
333 			stat = RPC_CANTRECV;
334 			goto done_broad;
335 		}
336 	try_again:
337 		fromlen = sizeof(struct sockaddr);
338 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
339 		    (struct sockaddr *)&raddr, &fromlen);
340 		if (inlen < 0) {
341 			if (errno == EINTR)
342 				goto try_again;
343 			stat = RPC_CANTRECV;
344 			goto done_broad;
345 		}
346 		if (inlen < sizeof(u_int32_t))
347 			goto recv_again;
348 		/*
349 		 * see if reply transaction id matches sent id.
350 		 * If so, decode the results.
351 		 */
352 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
353 		if (xdr_replymsg(xdrs, &msg)) {
354 			if ((msg.rm_xid == xid) &&
355 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
356 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
357 				raddr.sin_port = htons((u_short)port);
358 				done = (*eachresult)(resultsp, &raddr);
359 			}
360 			/* otherwise, we just ignore the errors ... */
361 		}
362 		xdrs->x_op = XDR_FREE;
363 		msg.acpted_rply.ar_results.proc = xdr_void;
364 		(void)xdr_replymsg(xdrs, &msg);
365 		(void)(*xresults)(xdrs, resultsp);
366 		xdr_destroy(xdrs);
367 		if (done) {
368 			stat = RPC_SUCCESS;
369 			goto done_broad;
370 		} else {
371 			goto recv_again;
372 		}
373 	}
374 done_broad:
375 	if (addrs)
376 		free(addrs);
377 	if (sock >= 0)
378 		(void)close(sock);
379 	if (unix_auth != NULL)
380 		AUTH_DESTROY(unix_auth);
381 	return (stat);
382 }
383