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