1 /* @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
3 * Copyright (c) 2010, Oracle America, Inc.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * * Neither the name of the "Oracle America, Inc." nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 #endif
37
38 /*
39 * pmap_rmt.c
40 * Client interface to pmap rpc service.
41 * remote call and broadcast service
42 */
43
44 #include "k5-platform.h"
45 #include <unistd.h>
46 #include <gssrpc/rpc.h>
47 #include <gssrpc/pmap_prot.h>
48 #include <gssrpc/pmap_clnt.h>
49 #include <gssrpc/pmap_rmt.h>
50 #include <sys/socket.h>
51 #ifdef sun
52 #include <sys/sockio.h>
53 #endif
54 #ifdef OSF1
55 #include <net/route.h>
56 #include <sys/mbuf.h>
57 #endif
58 #include <net/if.h>
59 #include <sys/ioctl.h>
60 #include <arpa/inet.h>
61 #define MAX_BROADCAST_SIZE 1400
62 #include <port-sockets.h>
63 #include "socket-utils.h"
64
65 static struct timeval timeout = { 3, 0 };
66
67 #ifndef GETSOCKNAME_ARG3_TYPE
68 #define GETSOCKNAME_ARG3_TYPE int
69 #endif
70
71 /*
72 * pmapper remote-call-service interface.
73 * This routine is used to call the pmapper remote call service
74 * which will look up a service program in the port maps, and then
75 * remotely call that routine with the given parameters. This allows
76 * programs to do a lookup and call in one step.
77 */
78 enum clnt_stat
pmap_rmtcall(struct sockaddr_in * addr,rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,struct timeval tout,rpcport_t * port_ptr)79 pmap_rmtcall(
80 struct sockaddr_in *addr,
81 rpcprog_t prog,
82 rpcvers_t vers,
83 rpcproc_t proc,
84 xdrproc_t xdrargs,
85 caddr_t argsp,
86 xdrproc_t xdrres,
87 caddr_t resp,
88 struct timeval tout,
89 rpcport_t *port_ptr)
90 {
91 SOCKET sock = INVALID_SOCKET;
92 CLIENT *client;
93 struct rmtcallargs a;
94 struct rmtcallres r;
95 enum clnt_stat stat;
96
97 addr->sin_port = htons(PMAPPORT);
98 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
99 if (client != (CLIENT *)NULL) {
100 a.prog = prog;
101 a.vers = vers;
102 a.proc = proc;
103 a.args_ptr = argsp;
104 a.xdr_args = xdrargs;
105 r.port_ptr = port_ptr;
106 r.results_ptr = resp;
107 r.xdr_results = xdrres;
108 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
109 xdr_rmtcallres, &r, tout);
110 CLNT_DESTROY(client);
111 } else {
112 stat = RPC_FAILED;
113 }
114 (void)closesocket(sock);
115 addr->sin_port = 0;
116 return (stat);
117 }
118
119
120 /*
121 * XDR remote call arguments
122 * written for XDR_ENCODE direction only
123 */
124 bool_t
xdr_rmtcall_args(XDR * xdrs,struct rmtcallargs * cap)125 xdr_rmtcall_args(
126 XDR *xdrs,
127 struct rmtcallargs *cap)
128 {
129 u_int lenposition, argposition, position;
130
131 if (xdr_u_int32(xdrs, &(cap->prog)) &&
132 xdr_u_int32(xdrs, &(cap->vers)) &&
133 xdr_u_int32(xdrs, &(cap->proc))) {
134 lenposition = XDR_GETPOS(xdrs);
135 if (! xdr_u_int32(xdrs, &(cap->arglen)))
136 return (FALSE);
137 argposition = XDR_GETPOS(xdrs);
138 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
139 return (FALSE);
140 position = XDR_GETPOS(xdrs);
141 cap->arglen = (uint32_t)position - (uint32_t)argposition;
142 XDR_SETPOS(xdrs, lenposition);
143 if (! xdr_u_int32(xdrs, &(cap->arglen)))
144 return (FALSE);
145 XDR_SETPOS(xdrs, position);
146 return (TRUE);
147 }
148 return (FALSE);
149 }
150
151 /*
152 * XDR remote call results
153 * written for XDR_DECODE direction only
154 */
155 bool_t
xdr_rmtcallres(XDR * xdrs,struct rmtcallres * crp)156 xdr_rmtcallres(
157 XDR *xdrs,
158 struct rmtcallres *crp)
159 {
160 caddr_t port_ptr;
161
162 port_ptr = (caddr_t)(void *)crp->port_ptr;
163 if (xdr_reference(xdrs, &port_ptr, sizeof (uint32_t),
164 xdr_u_int32) && xdr_u_int32(xdrs, &crp->resultslen)) {
165 crp->port_ptr = (uint32_t *)(void *)port_ptr;
166 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
167 }
168 return (FALSE);
169 }
170
171
172 /*
173 * The following is kludged-up support for simple rpc broadcasts.
174 * Someday a large, complicated system will replace these trivial
175 * routines which only support udp/ip .
176 */
177
178 #define GIFCONF_BUFSIZE (256 * sizeof (struct ifconf))
179
180 static int
getbroadcastnets(struct in_addr * addrs,int sock,char * buf)181 getbroadcastnets(
182 struct in_addr *addrs,
183 int sock, /* any valid socket will do */
184 char *buf /* why allocxate more when we can use existing... */
185 )
186 {
187 struct ifconf ifc;
188 struct ifreq ifreq, *ifr;
189 int n, i;
190
191 ifc.ifc_len = GIFCONF_BUFSIZE;
192 ifc.ifc_buf = buf;
193 memset (buf, 0, GIFCONF_BUFSIZE);
194 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
195 perror("broadcast: ioctl (get interface configuration)");
196 return (0);
197 }
198 ifr = ifc.ifc_req;
199 for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
200 ifreq = *ifr;
201 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
202 perror("broadcast: ioctl (get interface flags)");
203 continue;
204 }
205 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
206 (ifreq.ifr_flags & IFF_UP) &&
207 ifr->ifr_addr.sa_family == AF_INET) {
208 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
209 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
210 addrs[i++].s_addr = INADDR_ANY;
211 } else {
212 addrs[i++] = sa2sin(&ifreq.ifr_addr)->sin_addr;
213 }
214 #else /* 4.2 BSD */
215 struct sockaddr_in *sockin;
216 sockin = sa2sin(&ifr->ifr_addr);
217 addrs[i++] = inet_makeaddr(inet_netof
218 (sockin->sin_addr.s_addr), INADDR_ANY);
219 #endif
220 }
221 }
222 return (i);
223 }
224
225 enum clnt_stat
clnt_broadcast(rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xresults,caddr_t resultsp,resultproc_t eachresult)226 clnt_broadcast(
227 rpcprog_t prog, /* program number */
228 rpcvers_t vers, /* version number */
229 rpcproc_t proc, /* procedure number */
230 xdrproc_t xargs, /* xdr routine for args */
231 caddr_t argsp, /* pointer to args */
232 xdrproc_t xresults, /* xdr routine for results */
233 caddr_t resultsp, /* pointer to results */
234 resultproc_t eachresult /* call with each result obtained */
235 )
236 {
237 enum clnt_stat stat;
238 AUTH *unix_auth = authunix_create_default();
239 XDR xdr_stream;
240 XDR *xdrs = &xdr_stream;
241 int outlen, nets;
242 ssize_t inlen;
243 GETSOCKNAME_ARG3_TYPE fromlen;
244 SOCKET sock;
245 int on = 1;
246 #ifdef FD_SETSIZE
247 fd_set mask;
248 fd_set readfds;
249 #else
250 int readfds;
251 int mask;
252 #endif /* def FD_SETSIZE */
253 int i;
254 bool_t done = FALSE;
255 uint32_t xid;
256 rpcport_t port;
257 struct in_addr addrs[20];
258 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
259 struct rmtcallargs a;
260 struct rmtcallres r;
261 struct rpc_msg msg;
262 struct timeval t, t2;
263 char outbuf[MAX_BROADCAST_SIZE];
264 #ifndef MAX
265 #define MAX(A,B) ((A)<(B)?(B):(A))
266 #endif
267 char inbuf[MAX (UDPMSGSIZE, GIFCONF_BUFSIZE)];
268
269 /*
270 * initialization: create a socket, a broadcast address, and
271 * preserialize the arguments into a send buffer.
272 */
273 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
274 perror("Cannot create socket for broadcast rpc");
275 stat = RPC_CANTSEND;
276 goto done_broad;
277 }
278 set_cloexec_fd(sock);
279 #ifdef SO_BROADCAST
280 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *) &on,
281 sizeof (on)) < 0) {
282 perror("Cannot set socket option SO_BROADCAST");
283 stat = RPC_CANTSEND;
284 goto done_broad;
285 }
286 #endif /* def SO_BROADCAST */
287 #ifdef FD_SETSIZE
288 FD_ZERO(&mask);
289 FD_SET(sock, &mask);
290 #else
291 mask = (1 << sock);
292 #endif /* def FD_SETSIZE */
293 nets = getbroadcastnets(addrs, sock, inbuf);
294 memset(&baddr, 0, sizeof (baddr));
295 baddr.sin_family = AF_INET;
296 baddr.sin_port = htons(PMAPPORT);
297 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
298 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
299 (void)gettimeofday(&t, (struct timezone *)0);
300 msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
301 t.tv_usec = 0;
302 msg.rm_direction = CALL;
303 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
304 msg.rm_call.cb_prog = PMAPPROG;
305 msg.rm_call.cb_vers = PMAPVERS;
306 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
307 msg.rm_call.cb_cred = unix_auth->ah_cred;
308 msg.rm_call.cb_verf = unix_auth->ah_verf;
309 a.prog = prog;
310 a.vers = vers;
311 a.proc = proc;
312 a.xdr_args = xargs;
313 a.args_ptr = argsp;
314 r.port_ptr = &port;
315 r.xdr_results = xresults;
316 r.results_ptr = resultsp;
317 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
318 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
319 stat = RPC_CANTENCODEARGS;
320 goto done_broad;
321 }
322 outlen = (int)xdr_getpos(xdrs);
323 xdr_destroy(xdrs);
324 /*
325 * Basic loop: broadcast a packet and wait a while for response(s).
326 * The response timeout grows larger per iteration.
327 */
328 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
329 for (i = 0; i < nets; i++) {
330 baddr.sin_addr = addrs[i];
331 if (sendto(sock, outbuf, outlen, 0,
332 (struct sockaddr *)&baddr,
333 sizeof (struct sockaddr)) != outlen) {
334 perror("Cannot send broadcast packet");
335 stat = RPC_CANTSEND;
336 goto done_broad;
337 }
338 }
339 if (eachresult == NULL) {
340 stat = RPC_SUCCESS;
341 goto done_broad;
342 }
343 recv_again:
344 msg.acpted_rply.ar_verf = gssrpc__null_auth;
345 msg.acpted_rply.ar_results.where = (caddr_t)&r;
346 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
347 readfds = mask;
348 t2 = t;
349 switch (select(gssrpc__rpc_dtablesize(), &readfds, (fd_set *)NULL,
350 (fd_set *)NULL, &t2)) {
351
352 case 0: /* timed out */
353 stat = RPC_TIMEDOUT;
354 continue;
355
356 case -1: /* some kind of error */
357 if (errno == EINTR)
358 goto recv_again;
359 perror("Broadcast select problem");
360 stat = RPC_CANTRECV;
361 goto done_broad;
362
363 } /* end of select results switch */
364 try_again:
365 fromlen = sizeof(struct sockaddr);
366 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
367 (struct sockaddr *)&raddr, &fromlen);
368 if (inlen < 0) {
369 if (errno == EINTR)
370 goto try_again;
371 perror("Cannot receive reply to broadcast");
372 stat = RPC_CANTRECV;
373 goto done_broad;
374 }
375 if ((size_t)inlen < sizeof(uint32_t))
376 goto recv_again;
377 /*
378 * see if reply transaction id matches sent id.
379 * If so, decode the results.
380 */
381 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
382 if (xdr_replymsg(xdrs, &msg)) {
383 if ((msg.rm_xid == xid) &&
384 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
385 (msg.acpted_rply.ar_stat == SUCCESS)) {
386 raddr.sin_port = htons((u_short)port);
387 done = (*eachresult)(resultsp, &raddr);
388 }
389 /* otherwise, we just ignore the errors ... */
390 } else {
391 #ifdef notdef
392 /* some kind of deserialization problem ... */
393 if (msg.rm_xid == xid)
394 fprintf(stderr, "Broadcast deserialization problem");
395 /* otherwise, just random garbage */
396 #endif
397 }
398 xdrs->x_op = XDR_FREE;
399 msg.acpted_rply.ar_results.proc = xdr_void;
400 (void)xdr_replymsg(xdrs, &msg);
401 (void)(*xresults)(xdrs, resultsp);
402 xdr_destroy(xdrs);
403 if (done) {
404 stat = RPC_SUCCESS;
405 goto done_broad;
406 } else {
407 goto recv_again;
408 }
409 }
410 done_broad:
411 (void)closesocket(sock);
412 AUTH_DESTROY(unix_auth);
413 return (stat);
414 }
415