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.24 2005/04/01 07:44:04 otto 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(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc, 70 xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp, 71 struct timeval tout, u_long *port_ptr) 72 { 73 int sock = -1; 74 CLIENT *client; 75 struct rmtcallargs a; 76 struct rmtcallres r; 77 enum clnt_stat stat; 78 79 addr->sin_port = htons(PMAPPORT); 80 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock); 81 if (client != NULL) { 82 a.prog = prog; 83 a.vers = vers; 84 a.proc = proc; 85 a.args_ptr = argsp; 86 a.xdr_args = xdrargs; 87 r.port_ptr = port_ptr; 88 r.results_ptr = resp; 89 r.xdr_results = xdrres; 90 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a, 91 xdr_rmtcallres, &r, tout); 92 CLNT_DESTROY(client); 93 } else { 94 stat = RPC_FAILED; 95 } 96 if (sock != -1) 97 (void)close(sock); 98 addr->sin_port = 0; 99 return (stat); 100 } 101 102 103 /* 104 * XDR remote call arguments 105 * written for XDR_ENCODE direction only 106 */ 107 bool_t 108 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap) 109 { 110 u_int lenposition, argposition, position; 111 112 if (xdr_u_long(xdrs, &(cap->prog)) && 113 xdr_u_long(xdrs, &(cap->vers)) && 114 xdr_u_long(xdrs, &(cap->proc))) { 115 lenposition = XDR_GETPOS(xdrs); 116 if (! xdr_u_long(xdrs, &(cap->arglen))) 117 return (FALSE); 118 argposition = XDR_GETPOS(xdrs); 119 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr)) 120 return (FALSE); 121 position = XDR_GETPOS(xdrs); 122 cap->arglen = (u_long)position - (u_long)argposition; 123 XDR_SETPOS(xdrs, lenposition); 124 if (! xdr_u_long(xdrs, &(cap->arglen))) 125 return (FALSE); 126 XDR_SETPOS(xdrs, position); 127 return (TRUE); 128 } 129 return (FALSE); 130 } 131 132 /* 133 * XDR remote call results 134 * written for XDR_DECODE direction only 135 */ 136 bool_t 137 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp) 138 { 139 caddr_t port_ptr; 140 141 port_ptr = (caddr_t)crp->port_ptr; 142 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), 143 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) { 144 crp->port_ptr = (u_long *)port_ptr; 145 return ((*(crp->xdr_results))(xdrs, crp->results_ptr)); 146 } 147 return (FALSE); 148 } 149 150 151 /* 152 * The following is kludged-up support for simple rpc broadcasts. 153 * Someday a large, complicated system will replace these trivial 154 * routines which only support udp/ip . 155 */ 156 157 static int 158 newgetbroadcastnets(struct in_addr **addrsp, 159 int sock) /* any valid socket will do */ 160 { 161 struct ifaddrs *ifap, *ifa; 162 struct sockaddr_in *sin; 163 struct in_addr *addrs; 164 int i = 0, n = 0; 165 166 if (getifaddrs(&ifap) != 0) 167 return 0; 168 169 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 170 if (ifa->ifa_addr->sa_family != AF_INET) 171 continue; 172 if ((ifa->ifa_flags & IFF_BROADCAST) && 173 (ifa->ifa_flags & IFF_UP) && 174 ifa->ifa_broadaddr && 175 ifa->ifa_broadaddr->sa_family == AF_INET) { 176 n++; 177 } 178 } 179 180 addrs = (struct in_addr *)malloc(n * sizeof(*addrs)); 181 if (addrs == NULL) { 182 freeifaddrs(ifap); 183 return 0; 184 } 185 186 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 187 if (ifa->ifa_addr->sa_family != AF_INET) 188 continue; 189 if ((ifa->ifa_flags & IFF_BROADCAST) && 190 (ifa->ifa_flags & IFF_UP) && 191 ifa->ifa_broadaddr && 192 ifa->ifa_broadaddr->sa_family == AF_INET) { 193 sin = (struct sockaddr_in *)ifa->ifa_broadaddr; 194 addrs[i++] = sin->sin_addr; 195 } 196 } 197 198 freeifaddrs(ifap); 199 *addrsp = addrs; 200 return i; 201 } 202 203 typedef bool_t (*resultproc_t)(); 204 205 enum clnt_stat 206 clnt_broadcast(u_long prog, /* program number */ 207 u_long vers, /* version number */ 208 u_long proc, /* procedure number */ 209 xdrproc_t xargs, /* xdr routine for args */ 210 caddr_t argsp, /* pointer to args */ 211 xdrproc_t xresults, /* xdr routine for results */ 212 caddr_t resultsp, /* pointer to results */ 213 resultproc_t eachresult) /* call with each result obtained */ 214 { 215 enum clnt_stat stat; 216 AUTH *unix_auth = authunix_create_default(); 217 XDR xdr_stream; 218 XDR *xdrs = &xdr_stream; 219 int outlen, inlen, nets; 220 socklen_t fromlen; 221 int sock = -1; 222 int on = 1; 223 struct pollfd pfd[1]; 224 int i; 225 int timo; 226 bool_t done = FALSE; 227 u_long xid; 228 u_long port; 229 struct in_addr *addrs = NULL; 230 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */ 231 struct rmtcallargs a; 232 struct rmtcallres r; 233 struct rpc_msg msg; 234 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE]; 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, sock); 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 AUTH_DESTROY(unix_auth); 380 return (stat); 381 } 382