xref: /openbsd/lib/libc/rpc/pmap_rmt.c (revision 07ea8d15)
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.12 1996/12/10 07:46:43 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 	static u_int32_t disrupt;
244 
245 	if (disrupt == 0)
246 		disrupt = (u_int32_t)(long)resultsp;
247 
248 	/*
249 	 * initialization: create a socket, a broadcast address, and
250 	 * preserialize the arguments into a send buffer.
251 	 */
252 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
253 		perror("Cannot create socket for broadcast rpc");
254 		stat = RPC_CANTSEND;
255 		goto done_broad;
256 	}
257 #ifdef SO_BROADCAST
258 	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
259 		perror("Cannot set socket option SO_BROADCAST");
260 		stat = RPC_CANTSEND;
261 		goto done_broad;
262 	}
263 #endif /* def SO_BROADCAST */
264 
265 	if (sock+1 > FD_SETSIZE) {
266 		int bytes = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
267 		fds = (fd_set *)malloc(bytes);
268 		if (fds == NULL) {
269 			stat = RPC_CANTSEND;
270 			goto done_broad;
271 		}
272 		memset(fds, 0, bytes);
273 	} else {
274 		fds = &readfds;
275 		FD_ZERO(fds);
276 	}
277 
278 	nets = getbroadcastnets(addrs, sock, inbuf);
279 	memset(&baddr, 0, sizeof (baddr));
280 	baddr.sin_len = sizeof(struct sockaddr_in);
281 	baddr.sin_family = AF_INET;
282 	baddr.sin_port = htons(PMAPPORT);
283 	baddr.sin_addr.s_addr = htonl(INADDR_ANY);
284 	(void)gettimeofday(&t, (struct timezone *)0);
285 	msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
286 	t.tv_usec = 0;
287 	msg.rm_direction = CALL;
288 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
289 	msg.rm_call.cb_prog = PMAPPROG;
290 	msg.rm_call.cb_vers = PMAPVERS;
291 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
292 	msg.rm_call.cb_cred = unix_auth->ah_cred;
293 	msg.rm_call.cb_verf = unix_auth->ah_verf;
294 	a.prog = prog;
295 	a.vers = vers;
296 	a.proc = proc;
297 	a.xdr_args = xargs;
298 	a.args_ptr = argsp;
299 	r.port_ptr = &port;
300 	r.xdr_results = xresults;
301 	r.results_ptr = resultsp;
302 	xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
303 	if (!xdr_callmsg(xdrs, &msg) || !xdr_rmtcall_args(xdrs, &a)) {
304 		stat = RPC_CANTENCODEARGS;
305 		goto done_broad;
306 	}
307 	outlen = (int)xdr_getpos(xdrs);
308 	xdr_destroy(xdrs);
309 
310 	/*
311 	 * Basic loop: broadcast a packet and wait a while for response(s).
312 	 * The response timeout grows larger per iteration.
313 	 *
314 	 * XXX This will loop about 5 times the stop. If there are
315 	 * lots of signals being received by the process it will quit
316 	 * send them all in one quick burst, not paying attention to
317 	 * the intended function of sending them slowly over half a
318 	 * minute or so
319 	 */
320 	for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
321 		for (i = 0; i < nets; i++) {
322 			baddr.sin_addr = addrs[i];
323 			if (sendto(sock, outbuf, outlen, 0,
324 			    (struct sockaddr *)&baddr,
325 			    sizeof (struct sockaddr)) != outlen) {
326 				perror("Cannot send broadcast packet");
327 				stat = RPC_CANTSEND;
328 				goto done_broad;
329 			}
330 		}
331 		if (eachresult == NULL) {
332 			stat = RPC_SUCCESS;
333 			goto done_broad;
334 		}
335 	recv_again:
336 		msg.acpted_rply.ar_verf = _null_auth;
337 		msg.acpted_rply.ar_results.where = (caddr_t)&r;
338                 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
339 
340 		/* XXX we know the other bits are still clear */
341 		FD_SET(sock, fds);
342 		switch (select(sock+1, fds, NULL, NULL, &t)) {
343 		case 0:  /* timed out */
344 			stat = RPC_TIMEDOUT;
345 			continue;
346 		case -1:  /* some kind of error */
347 			if (errno == EINTR)
348 				goto recv_again;
349 			perror("Broadcast select problem");
350 			stat = RPC_CANTRECV;
351 			goto done_broad;
352 		}
353 	try_again:
354 		fromlen = sizeof(struct sockaddr);
355 		inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
356 		    (struct sockaddr *)&raddr, &fromlen);
357 		if (inlen < 0) {
358 			if (errno == EINTR)
359 				goto try_again;
360 			perror("Cannot receive reply to broadcast");
361 			stat = RPC_CANTRECV;
362 			goto done_broad;
363 		}
364 		if (inlen < sizeof(u_int32_t))
365 			goto recv_again;
366 		/*
367 		 * see if reply transaction id matches sent id.
368 		 * If so, decode the results.
369 		 */
370 		xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
371 		if (xdr_replymsg(xdrs, &msg)) {
372 			if ((msg.rm_xid == xid) &&
373 			    (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
374 			    (msg.acpted_rply.ar_stat == SUCCESS)) {
375 				raddr.sin_port = htons((u_short)port);
376 				done = (*eachresult)(resultsp, &raddr);
377 			}
378 			/* otherwise, we just ignore the errors ... */
379 		}
380 		xdrs->x_op = XDR_FREE;
381 		msg.acpted_rply.ar_results.proc = xdr_void;
382 		(void)xdr_replymsg(xdrs, &msg);
383 		(void)(*xresults)(xdrs, resultsp);
384 		xdr_destroy(xdrs);
385 		if (done) {
386 			stat = RPC_SUCCESS;
387 			goto done_broad;
388 		} else {
389 			goto recv_again;
390 		}
391 	}
392 done_broad:
393 	if (fds != &readfds)
394 		free(fds);
395 	if (sock >= 0)
396 		(void)close(sock);
397 	AUTH_DESTROY(unix_auth);
398 	return (stat);
399 }
400 
401