xref: /openbsd-src/lib/libc/rpc/pmap_rmt.c (revision 17ba841738be3bbd25eeb6dec4538d430637026f)
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.13 1997/01/02 09:21:07 deraadt 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 #define MAX_BROADCAST_SIZE 1400
56 
57 static struct timeval timeout = { 3, 0 };
58 
59 
60 /*
61  * pmapper remote-call-service interface.
62  * This routine is used to call the pmapper remote call service
63  * which will look up a service program in the port maps, and then
64  * remotely call that routine with the given parameters.  This allows
65  * programs to do a lookup and call in one step.
66 */
67 enum clnt_stat
68 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
69 	struct sockaddr_in *addr;
70 	u_long prog, vers, proc;
71 	xdrproc_t xdrargs, xdrres;
72 	caddr_t argsp, resp;
73 	struct timeval tout;
74 	u_long *port_ptr;
75 {
76 	int socket = -1;
77 	register CLIENT *client;
78 	struct rmtcallargs a;
79 	struct rmtcallres r;
80 	enum clnt_stat stat;
81 
82 	addr->sin_port = htons(PMAPPORT);
83 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
84 	if (client != (CLIENT *)NULL) {
85 		a.prog = prog;
86 		a.vers = vers;
87 		a.proc = proc;
88 		a.args_ptr = argsp;
89 		a.xdr_args = xdrargs;
90 		r.port_ptr = port_ptr;
91 		r.results_ptr = resp;
92 		r.xdr_results = xdrres;
93 		stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
94 		    xdr_rmtcallres, &r, tout);
95 		CLNT_DESTROY(client);
96 	} else {
97 		stat = RPC_FAILED;
98 	}
99 	if (socket != -1)
100 		(void)close(socket);
101 	addr->sin_port = 0;
102 	return (stat);
103 }
104 
105 
106 /*
107  * XDR remote call arguments
108  * written for XDR_ENCODE direction only
109  */
110 bool_t
111 xdr_rmtcall_args(xdrs, cap)
112 	register XDR *xdrs;
113 	register struct rmtcallargs *cap;
114 {
115 	u_int lenposition, argposition, position;
116 
117 	if (xdr_u_long(xdrs, &(cap->prog)) &&
118 	    xdr_u_long(xdrs, &(cap->vers)) &&
119 	    xdr_u_long(xdrs, &(cap->proc))) {
120 		lenposition = XDR_GETPOS(xdrs);
121 		if (! xdr_u_long(xdrs, &(cap->arglen)))
122 		    return (FALSE);
123 		argposition = XDR_GETPOS(xdrs);
124 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
125 		    return (FALSE);
126 		position = XDR_GETPOS(xdrs);
127 		cap->arglen = (u_long)position - (u_long)argposition;
128 		XDR_SETPOS(xdrs, lenposition);
129 		if (! xdr_u_long(xdrs, &(cap->arglen)))
130 		    return (FALSE);
131 		XDR_SETPOS(xdrs, position);
132 		return (TRUE);
133 	}
134 	return (FALSE);
135 }
136 
137 /*
138  * XDR remote call results
139  * written for XDR_DECODE direction only
140  */
141 bool_t
142 xdr_rmtcallres(xdrs, crp)
143 	register XDR *xdrs;
144 	register struct rmtcallres *crp;
145 {
146 	caddr_t port_ptr;
147 
148 	port_ptr = (caddr_t)crp->port_ptr;
149 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
150 	    xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
151 		crp->port_ptr = (u_long *)port_ptr;
152 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
153 	}
154 	return (FALSE);
155 }
156 
157 
158 /*
159  * The following is kludged-up support for simple rpc broadcasts.
160  * Someday a large, complicated system will replace these trivial
161  * routines which only support udp/ip .
162  */
163 
164 static int
165 getbroadcastnets(addrs, sock, buf)
166 	struct in_addr *addrs;
167 	int sock;  /* any valid socket will do */
168 	char *buf;  /* why allocxate more when we can use existing... */
169 {
170 	struct ifconf ifc;
171         struct ifreq ifreq, *ifr;
172 	struct sockaddr_in *sin;
173         char *cp, *cplim;
174         int i = 0;
175 
176         ifc.ifc_len = UDPMSGSIZE;
177         ifc.ifc_buf = buf;
178         if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
179                 perror("broadcast: ioctl (get interface configuration)");
180                 return (0);
181         }
182 #define max(a, b) (a > b ? a : b)
183 #define size(p)	max((p).sa_len, sizeof(p))
184 	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
185 	for (cp = buf; cp < cplim;
186 	    cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
187 		ifr = (struct ifreq *)cp;
188 		if (ifr->ifr_addr.sa_family != AF_INET)
189 			continue;
190 		ifreq = *ifr;
191                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
192                         perror("broadcast: ioctl (get interface flags)");
193                         continue;
194                 }
195                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
196 		    (ifreq.ifr_flags & IFF_UP)) {
197 			sin = (struct sockaddr_in *)&ifr->ifr_addr;
198 			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
199 				addrs[i++] =
200 				    inet_makeaddr(inet_netof(sin->sin_addr),
201 				    INADDR_ANY);
202 			} else {
203 				addrs[i++] = ((struct sockaddr_in*)
204 				  &ifreq.ifr_addr)->sin_addr;
205 			}
206 		}
207 	}
208 	return (i);
209 }
210 
211 typedef bool_t (*resultproc_t)();
212 
213 enum clnt_stat
214 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
215 	u_long		prog;		/* program number */
216 	u_long		vers;		/* version number */
217 	u_long		proc;		/* procedure number */
218 	xdrproc_t	xargs;		/* xdr routine for args */
219 	caddr_t		argsp;		/* pointer to args */
220 	xdrproc_t	xresults;	/* xdr routine for results */
221 	caddr_t		resultsp;	/* pointer to results */
222 	resultproc_t	eachresult;	/* call with each result obtained */
223 {
224 	enum clnt_stat stat;
225 	AUTH *unix_auth = authunix_create_default();
226 	XDR xdr_stream;
227 	register XDR *xdrs = &xdr_stream;
228 	int outlen, inlen, fromlen, nets;
229 	register int sock;
230 	int on = 1;
231 	fd_set *fds, readfds;
232 	register int i;
233 	bool_t done = FALSE;
234 	register u_long xid;
235 	u_long port;
236 	struct in_addr addrs[20];
237 	struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
238 	struct rmtcallargs a;
239 	struct rmtcallres r;
240 	struct rpc_msg msg;
241 	struct timeval t;
242 	char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
243 
244 	/*
245 	 * initialization: create a socket, a broadcast address, and
246 	 * preserialize the arguments into a send buffer.
247 	 */
248 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
249 		perror("Cannot create socket for broadcast rpc");
250 		stat = RPC_CANTSEND;
251 		goto done_broad;
252 	}
253 #ifdef SO_BROADCAST
254 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
255 		perror("Cannot set socket option SO_BROADCAST");
256 		stat = RPC_CANTSEND;
257 		goto done_broad;
258 	}
259 #endif /* def SO_BROADCAST */
260 
261 	if (sock+1 > FD_SETSIZE) {
262 		int bytes = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
263 		fds = (fd_set *)malloc(bytes);
264 		if (fds == NULL) {
265 			stat = RPC_CANTSEND;
266 			goto done_broad;
267 		}
268 		memset(fds, 0, bytes);
269 	} else {
270 		fds = &readfds;
271 		FD_ZERO(fds);
272 	}
273 
274 	nets = getbroadcastnets(addrs, sock, inbuf);
275 	memset(&baddr, 0, sizeof (baddr));
276 	baddr.sin_len = sizeof(struct sockaddr_in);
277 	baddr.sin_family = AF_INET;
278 	baddr.sin_port = htons(PMAPPORT);
279 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
280 	(void)gettimeofday(&t, (struct timezone *)0);
281 	msg.rm_xid = xid = arc4random();
282 	t.tv_usec = 0;
283 	msg.rm_direction = CALL;
284 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
285 	msg.rm_call.cb_prog = PMAPPROG;
286 	msg.rm_call.cb_vers = PMAPVERS;
287 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
288 	msg.rm_call.cb_cred = unix_auth->ah_cred;
289 	msg.rm_call.cb_verf = unix_auth->ah_verf;
290 	a.prog = prog;
291 	a.vers = vers;
292 	a.proc = proc;
293 	a.xdr_args = xargs;
294 	a.args_ptr = argsp;
295 	r.port_ptr = &port;
296 	r.xdr_results = xresults;
297 	r.results_ptr = resultsp;
298 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
299 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
300 		stat = RPC_CANTENCODEARGS;
301 		goto done_broad;
302 	}
303 	outlen = (int)xdr_getpos(xdrs);
304 	xdr_destroy(xdrs);
305 
306 	/*
307 	 * Basic loop: broadcast a packet and wait a while for response(s).
308 	 * The response timeout grows larger per iteration.
309 	 *
310 	 * XXX This will loop about 5 times the stop. If there are
311 	 * lots of signals being received by the process it will quit
312 	 * send them all in one quick burst, not paying attention to
313 	 * the intended function of sending them slowly over half a
314 	 * minute or so
315 	 */
316 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
317 		for (i = 0; i < nets; i++) {
318 			baddr.sin_addr = addrs[i];
319 			if (sendto(sock, outbuf, outlen, 0,
320 			    (struct sockaddr *)&baddr,
321 			    sizeof (struct sockaddr)) != outlen) {
322 				perror("Cannot send broadcast packet");
323 				stat = RPC_CANTSEND;
324 				goto done_broad;
325 			}
326 		}
327 		if (eachresult == NULL) {
328 			stat = RPC_SUCCESS;
329 			goto done_broad;
330 		}
331 	recv_again:
332 		msg.acpted_rply.ar_verf = _null_auth;
333 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
334                 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
335 
336 		/* XXX we know the other bits are still clear */
337 		FD_SET(sock, fds);
338 		switch (select(sock+1, fds, NULL, NULL, &t)) {
339 		case 0:  /* timed out */
340 			stat = RPC_TIMEDOUT;
341 			continue;
342 		case -1:  /* some kind of error */
343 			if (errno == EINTR)
344 				goto recv_again;
345 			perror("Broadcast select problem");
346 			stat = RPC_CANTRECV;
347 			goto done_broad;
348 		}
349 	try_again:
350 		fromlen = sizeof(struct sockaddr);
351 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
352 		    (struct sockaddr *)&raddr, &fromlen);
353 		if (inlen < 0) {
354 			if (errno == EINTR)
355 				goto try_again;
356 			perror("Cannot receive reply to broadcast");
357 			stat = RPC_CANTRECV;
358 			goto done_broad;
359 		}
360 		if (inlen < sizeof(u_int32_t))
361 			goto recv_again;
362 		/*
363 		 * see if reply transaction id matches sent id.
364 		 * If so, decode the results.
365 		 */
366 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
367 		if (xdr_replymsg(xdrs, &msg)) {
368 			if ((msg.rm_xid == xid) &&
369 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
370 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
371 				raddr.sin_port = htons((u_short)port);
372 				done = (*eachresult)(resultsp, &raddr);
373 			}
374 			/* otherwise, we just ignore the errors ... */
375 		}
376 		xdrs->x_op = XDR_FREE;
377 		msg.acpted_rply.ar_results.proc = xdr_void;
378 		(void)xdr_replymsg(xdrs, &msg);
379 		(void)(*xresults)(xdrs, resultsp);
380 		xdr_destroy(xdrs);
381 		if (done) {
382 			stat = RPC_SUCCESS;
383 			goto done_broad;
384 		} else {
385 			goto recv_again;
386 		}
387 	}
388 done_broad:
389 	if (fds != &readfds)
390 		free(fds);
391 	if (sock >= 0)
392 		(void)close(sock);
393 	AUTH_DESTROY(unix_auth);
394 	return (stat);
395 }
396 
397