Index: stable/10/lib/libc/rpc/clnt_bcast.c =================================================================== --- stable/10/lib/libc/rpc/clnt_bcast.c (revision 290898) +++ stable/10/lib/libc/rpc/clnt_bcast.c (revision 290899) @@ -1,672 +1,669 @@ /* $NetBSD: clnt_bcast.c,v 1.3 2000/07/06 03:05:20 christos Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (c) 1986-1991 by Sun Microsystems Inc. */ #if defined(LIBC_SCCS) && !defined(lint) #ident "@(#)clnt_bcast.c 1.18 94/05/03 SMI" static char sccsid[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro"; #endif #include __FBSDID("$FreeBSD$"); /* * clnt_bcast.c * Client interface to broadcast service. * * Copyright (C) 1988, Sun Microsystems, Inc. * * The following is kludged-up support for simple rpc broadcasts. * Someday a large, complicated system will replace these routines. */ #include "namespace.h" #include #include #include #include #include #include #include #include #ifdef PORTMAP #include #include #include #endif /* PORTMAP */ #include #include #ifdef RPC_DEBUG #include #endif #include #include #include #include #include #include #include "un-namespace.h" #include "rpc_com.h" #define MAXBCAST 20 /* Max no of broadcasting transports */ #define INITTIME 4000 /* Time to wait initially */ #define WAITTIME 8000 /* Maximum time to wait */ /* * If nettype is NULL, it broadcasts on all the available * datagram_n transports. May potentially lead to broadacst storms * and hence should be used with caution, care and courage. * * The current parameter xdr packet size is limited by the max tsdu * size of the transport. If the max tsdu size of any transport is * smaller than the parameter xdr packet, then broadcast is not * sent on that transport. * * Also, the packet size should be less the packet size of * the data link layer (for ethernet it is 1400 bytes). There is * no easy way to find out the max size of the data link layer and * we are assuming that the args would be smaller than that. * * The result size has to be smaller than the transport tsdu size. * * If PORTMAP has been defined, we send two packets for UDP, one for * rpcbind and one for portmap. For those machines which support * both rpcbind and portmap, it will cause them to reply twice, and * also here it will get two responses ... inefficient and clumsy. */ struct broadif { int index; struct sockaddr_storage broadaddr; TAILQ_ENTRY(broadif) link; }; typedef TAILQ_HEAD(, broadif) broadlist_t; int __rpc_getbroadifs(int, int, int, broadlist_t *); void __rpc_freebroadifs(broadlist_t *); int __rpc_broadenable(int, int, struct broadif *); int __rpc_lowvers = 0; int __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list) { int count = 0; struct broadif *bip; struct ifaddrs *ifap, *ifp; #ifdef INET6 struct sockaddr_in6 *sin6; #endif struct sockaddr_in *sin; struct addrinfo hints, *res; if (getifaddrs(&ifp) < 0) return 0; memset(&hints, 0, sizeof hints); hints.ai_family = af; hints.ai_protocol = proto; hints.ai_socktype = socktype; if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0) { freeifaddrs(ifp); return 0; } for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) { if (ifap->ifa_addr->sa_family != af || !(ifap->ifa_flags & IFF_UP)) continue; bip = (struct broadif *)malloc(sizeof *bip); if (bip == NULL) break; bip->index = if_nametoindex(ifap->ifa_name); if ( #ifdef INET6 af != AF_INET6 && #endif (ifap->ifa_flags & IFF_BROADCAST) && ifap->ifa_broadaddr) { memcpy(&bip->broadaddr, ifap->ifa_broadaddr, (size_t)ifap->ifa_broadaddr->sa_len); sin = (struct sockaddr_in *)(void *)&bip->broadaddr; sin->sin_port = ((struct sockaddr_in *) (void *)res->ai_addr)->sin_port; } else #ifdef INET6 if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) { sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr; inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr); sin6->sin6_family = af; sin6->sin6_len = sizeof *sin6; sin6->sin6_port = ((struct sockaddr_in6 *) (void *)res->ai_addr)->sin6_port; sin6->sin6_scope_id = bip->index; } else #endif { free(bip); continue; } TAILQ_INSERT_TAIL(list, bip, link); count++; } freeifaddrs(ifp); freeaddrinfo(res); return count; } void __rpc_freebroadifs(broadlist_t *list) { struct broadif *bip, *next; bip = TAILQ_FIRST(list); while (bip != NULL) { next = TAILQ_NEXT(bip, link); free(bip); bip = next; } } int /*ARGSUSED*/ __rpc_broadenable(int af, int s, struct broadif *bip) { int o = 1; #if 0 if (af == AF_INET6) { fprintf(stderr, "set v6 multicast if to %d\n", bip->index); if (_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index, sizeof bip->index) < 0) return -1; } else #endif if (_setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0) return -1; return 0; } enum clnt_stat rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult, inittime, waittime, nettype) rpcprog_t prog; /* program number */ rpcvers_t vers; /* version number */ rpcproc_t proc; /* procedure number */ xdrproc_t xargs; /* xdr routine for args */ caddr_t argsp; /* pointer to args */ xdrproc_t xresults; /* xdr routine for results */ caddr_t resultsp; /* pointer to results */ resultproc_t eachresult; /* call with each result obtained */ int inittime; /* how long to wait initially */ int waittime; /* maximum time to wait */ const char *nettype; /* transport type */ { enum clnt_stat stat = RPC_SUCCESS; /* Return status */ XDR xdr_stream; /* XDR stream */ XDR *xdrs = &xdr_stream; struct rpc_msg msg; /* RPC message */ struct timeval t; char *outbuf = NULL; /* Broadcast msg buffer */ char *inbuf = NULL; /* Reply buf */ int inlen; u_int maxbufsize = 0; AUTH *sys_auth = authunix_create_default(); int i; void *handle; char uaddress[1024]; /* A self imposed limit */ char *uaddrp = uaddress; int pmap_reply_flag; /* reply recvd from PORTMAP */ /* An array of all the suitable broadcast transports */ struct { int fd; /* File descriptor */ int af; int proto; struct netconfig *nconf; /* Netconfig structure */ u_int asize; /* Size of the addr buf */ u_int dsize; /* Size of the data buf */ struct sockaddr_storage raddr; /* Remote address */ broadlist_t nal; } fdlist[MAXBCAST]; struct pollfd pfd[MAXBCAST]; size_t fdlistno = 0; struct r_rpcb_rmtcallargs barg; /* Remote arguments */ struct r_rpcb_rmtcallres bres; /* Remote results */ size_t outlen; struct netconfig *nconf; int msec; int pollretval; int fds_found; #ifdef PORTMAP size_t outlen_pmap = 0; u_long port; /* Remote port number */ int pmap_flag = 0; /* UDP exists ? */ char *outbuf_pmap = NULL; struct rmtcallargs barg_pmap; /* Remote arguments */ struct rmtcallres bres_pmap; /* Remote results */ u_int udpbufsz = 0; #endif /* PORTMAP */ if (sys_auth == NULL) { return (RPC_SYSTEMERROR); } /* * initialization: create a fd, a broadcast address, and send the * request on the broadcast transport. * Listen on all of them and on replies, call the user supplied * function. */ if (nettype == NULL) nettype = "datagram_n"; if ((handle = __rpc_setconf(nettype)) == NULL) { AUTH_DESTROY(sys_auth); return (RPC_UNKNOWNPROTO); } while ((nconf = __rpc_getconf(handle)) != NULL) { int fd; struct __rpc_sockinfo si; if (nconf->nc_semantics != NC_TPI_CLTS) continue; if (fdlistno >= MAXBCAST) break; /* No more slots available */ if (!__rpc_nconf2sockinfo(nconf, &si)) continue; TAILQ_INIT(&fdlist[fdlistno].nal); if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype, &fdlist[fdlistno].nal) == 0) continue; fd = _socket(si.si_af, si.si_socktype, si.si_proto); if (fd < 0) { stat = RPC_CANTSEND; continue; } fdlist[fdlistno].af = si.si_af; fdlist[fdlistno].proto = si.si_proto; fdlist[fdlistno].fd = fd; fdlist[fdlistno].nconf = nconf; fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af); pfd[fdlistno].events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND; pfd[fdlistno].fd = fdlist[fdlistno].fd = fd; fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto, 0); if (maxbufsize <= fdlist[fdlistno].dsize) maxbufsize = fdlist[fdlistno].dsize; #ifdef PORTMAP if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) { udpbufsz = fdlist[fdlistno].dsize; if ((outbuf_pmap = malloc(udpbufsz)) == NULL) { _close(fd); stat = RPC_SYSTEMERROR; goto done_broad; } pmap_flag = 1; } #endif /* PORTMAP */ fdlistno++; } if (fdlistno == 0) { if (stat == RPC_SUCCESS) stat = RPC_UNKNOWNPROTO; goto done_broad; } if (maxbufsize == 0) { if (stat == RPC_SUCCESS) stat = RPC_CANTSEND; goto done_broad; } inbuf = malloc(maxbufsize); outbuf = malloc(maxbufsize); if ((inbuf == NULL) || (outbuf == NULL)) { stat = RPC_SYSTEMERROR; goto done_broad; } /* Serialize all the arguments which have to be sent */ (void) gettimeofday(&t, NULL); msg.rm_xid = __RPC_GETXID(&t); msg.rm_direction = CALL; msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; msg.rm_call.cb_prog = RPCBPROG; msg.rm_call.cb_vers = RPCBVERS; msg.rm_call.cb_proc = RPCBPROC_CALLIT; barg.prog = prog; barg.vers = vers; barg.proc = proc; barg.args.args_val = argsp; barg.xdr_args = xargs; bres.addr = uaddrp; bres.results.results_val = resultsp; bres.xdr_res = xresults; msg.rm_call.cb_cred = sys_auth->ah_cred; msg.rm_call.cb_verf = sys_auth->ah_verf; xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE); if ((!xdr_callmsg(xdrs, &msg)) || (!xdr_rpcb_rmtcallargs(xdrs, (struct rpcb_rmtcallargs *)(void *)&barg))) { stat = RPC_CANTENCODEARGS; goto done_broad; } outlen = xdr_getpos(xdrs); xdr_destroy(xdrs); #ifdef PORTMAP /* Prepare the packet for version 2 PORTMAP */ if (pmap_flag) { msg.rm_xid++; /* One way to distinguish */ msg.rm_call.cb_prog = PMAPPROG; msg.rm_call.cb_vers = PMAPVERS; msg.rm_call.cb_proc = PMAPPROC_CALLIT; barg_pmap.prog = prog; barg_pmap.vers = vers; barg_pmap.proc = proc; barg_pmap.args_ptr = argsp; barg_pmap.xdr_args = xargs; bres_pmap.port_ptr = &port; bres_pmap.xdr_results = xresults; bres_pmap.results_ptr = resultsp; xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE); if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &barg_pmap))) { stat = RPC_CANTENCODEARGS; goto done_broad; } outlen_pmap = xdr_getpos(xdrs); xdr_destroy(xdrs); } #endif /* PORTMAP */ /* * Basic loop: broadcast the packets to transports which * support data packets of size such that one can encode * all the arguments. * Wait a while for response(s). * The response timeout grows larger per iteration. */ for (msec = inittime; msec <= waittime; msec += msec) { struct broadif *bip; /* Broadcast all the packets now */ for (i = 0; i < fdlistno; i++) { if (fdlist[i].dsize < outlen) { stat = RPC_CANTSEND; continue; } for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL; bip = TAILQ_NEXT(bip, link)) { void *addr; addr = &bip->broadaddr; __rpc_broadenable(fdlist[i].af, fdlist[i].fd, bip); /* * Only use version 3 if lowvers is not set */ if (!__rpc_lowvers) if (_sendto(fdlist[i].fd, outbuf, outlen, 0, (struct sockaddr*)addr, (size_t)fdlist[i].asize) != outlen) { #ifdef RPC_DEBUG perror("sendto"); #endif warnx("clnt_bcast: cannot send " "broadcast packet"); stat = RPC_CANTSEND; continue; }; #ifdef RPC_DEBUG if (!__rpc_lowvers) fprintf(stderr, "Broadcast packet sent " "for %s\n", fdlist[i].nconf->nc_netid); #endif #ifdef PORTMAP /* * Send the version 2 packet also * for UDP/IP */ if (pmap_flag && fdlist[i].proto == IPPROTO_UDP) { if (_sendto(fdlist[i].fd, outbuf_pmap, outlen_pmap, 0, addr, (size_t)fdlist[i].asize) != outlen_pmap) { warnx("clnt_bcast: " "Cannot send broadcast packet"); stat = RPC_CANTSEND; continue; } } #ifdef RPC_DEBUG fprintf(stderr, "PMAP Broadcast packet " "sent for %s\n", fdlist[i].nconf->nc_netid); #endif #endif /* PORTMAP */ } /* End for sending all packets on this transport */ } /* End for sending on all transports */ if (eachresult == NULL) { stat = RPC_SUCCESS; goto done_broad; } /* * Get all the replies from these broadcast requests */ recv_again: switch (pollretval = _poll(pfd, fdlistno, msec)) { case 0: /* timed out */ stat = RPC_TIMEDOUT; continue; case -1: /* some kind of error - we ignore it */ goto recv_again; } /* end of poll results switch */ for (i = fds_found = 0; i < fdlistno && fds_found < pollretval; i++) { bool_t done = FALSE; if (pfd[i].revents == 0) continue; else if (pfd[i].revents & POLLNVAL) { /* * Something bad has happened to this descri- * ptor. We can cause _poll() to ignore * it simply by using a negative fd. We do that * rather than compacting the pfd[] and fdlist[] * arrays. */ pfd[i].fd = -1; fds_found++; continue; } else fds_found++; #ifdef RPC_DEBUG fprintf(stderr, "response for %s\n", fdlist[i].nconf->nc_netid); #endif try_again: inlen = _recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize, 0, (struct sockaddr *)(void *)&fdlist[i].raddr, &fdlist[i].asize); if (inlen < 0) { if (errno == EINTR) goto try_again; warnx("clnt_bcast: Cannot receive reply to " "broadcast"); stat = RPC_CANTRECV; continue; } if (inlen < sizeof (u_int32_t)) continue; /* Drop that and go ahead */ /* * see if reply transaction id matches sent id. * If so, decode the results. If return id is xid + 1 * it was a PORTMAP reply */ if (*((u_int32_t *)(void *)(inbuf)) == *((u_int32_t *)(void *)(outbuf))) { pmap_reply_flag = 0; msg.acpted_rply.ar_verf = _null_auth; msg.acpted_rply.ar_results.where = (caddr_t)(void *)&bres; msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rpcb_rmtcallres; #ifdef PORTMAP } else if (pmap_flag && *((u_int32_t *)(void *)(inbuf)) == *((u_int32_t *)(void *)(outbuf_pmap))) { pmap_reply_flag = 1; msg.acpted_rply.ar_verf = _null_auth; msg.acpted_rply.ar_results.where = (caddr_t)(void *)&bres_pmap; msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres; #endif /* PORTMAP */ } else continue; xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE); if (xdr_replymsg(xdrs, &msg)) { if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) && (msg.acpted_rply.ar_stat == SUCCESS)) { struct netbuf taddr, *np; struct sockaddr_in *sin; #ifdef PORTMAP if (pmap_flag && pmap_reply_flag) { sin = (struct sockaddr_in *) (void *)&fdlist[i].raddr; sin->sin_port = htons((u_short)port); taddr.len = taddr.maxlen = fdlist[i].raddr.ss_len; taddr.buf = &fdlist[i].raddr; done = (*eachresult)(resultsp, &taddr, fdlist[i].nconf); } else { #endif /* PORTMAP */ #ifdef RPC_DEBUG fprintf(stderr, "uaddr %s\n", uaddrp); #endif np = uaddr2taddr( fdlist[i].nconf, uaddrp); done = (*eachresult)(resultsp, np, fdlist[i].nconf); free(np); #ifdef PORTMAP } #endif /* PORTMAP */ } /* otherwise, we just ignore the errors ... */ } /* else some kind of deserialization problem ... */ xdrs->x_op = XDR_FREE; msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; (void) xdr_replymsg(xdrs, &msg); (void) (*xresults)(xdrs, resultsp); XDR_DESTROY(xdrs); if (done) { stat = RPC_SUCCESS; goto done_broad; } else { goto recv_again; } } /* The recv for loop */ } /* The giant for loop */ done_broad: - if (inbuf) - (void) free(inbuf); - if (outbuf) - (void) free(outbuf); + free(inbuf); + free(outbuf); #ifdef PORTMAP - if (outbuf_pmap) - (void) free(outbuf_pmap); + free(outbuf_pmap); #endif /* PORTMAP */ for (i = 0; i < fdlistno; i++) { (void)_close(fdlist[i].fd); __rpc_freebroadifs(&fdlist[i].nal); } AUTH_DESTROY(sys_auth); (void) __rpc_endconf(handle); return (stat); } enum clnt_stat rpc_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult, nettype) rpcprog_t prog; /* program number */ rpcvers_t vers; /* version number */ rpcproc_t proc; /* procedure number */ xdrproc_t xargs; /* xdr routine for args */ caddr_t argsp; /* pointer to args */ xdrproc_t xresults; /* xdr routine for results */ caddr_t resultsp; /* pointer to results */ resultproc_t eachresult; /* call with each result obtained */ const char *nettype; /* transport type */ { enum clnt_stat dummy; dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult, INITTIME, WAITTIME, nettype); return (dummy); } Index: stable/10/lib/libc/rpc/clnt_vc.c =================================================================== --- stable/10/lib/libc/rpc/clnt_vc.c (revision 290898) +++ stable/10/lib/libc/rpc/clnt_vc.c (revision 290899) @@ -1,872 +1,871 @@ /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC"; static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro"; #endif #include __FBSDID("$FreeBSD$"); /* * clnt_tcp.c, Implements a TCP/IP based, client side RPC. * * Copyright (C) 1984, Sun Microsystems, Inc. * * TCP based RPC supports 'batched calls'. * A sequence of calls may be batched-up in a send buffer. The rpc call * return immediately to the client even though the call was not necessarily * sent. The batching occurs if the results' xdr routine is NULL (0) AND * the rpc timeout value is zero (see clnt.h, rpc). * * Clients should NOT casually batch calls that in fact return results; that is, * the server side should be aware that a call is batched and not produce any * return message. Batched calls that produce many result messages can * deadlock (netlock) the client and the server.... * * Now go hang yourself. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "rpc_com.h" #include "mt_misc.h" #define MCALL_MSG_SIZE 24 struct cmessage { struct cmsghdr cmsg; struct cmsgcred cmcred; }; static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t, void *, xdrproc_t, void *, struct timeval); static void clnt_vc_geterr(CLIENT *, struct rpc_err *); static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *); static void clnt_vc_abort(CLIENT *); static bool_t clnt_vc_control(CLIENT *, u_int, void *); static void clnt_vc_destroy(CLIENT *); static struct clnt_ops *clnt_vc_ops(void); static bool_t time_not_ok(struct timeval *); static int read_vc(void *, void *, int); static int write_vc(void *, void *, int); static int __msgwrite(int, void *, size_t); static int __msgread(int, void *, size_t); struct ct_data { int ct_fd; /* connection's fd */ bool_t ct_closeit; /* close it on destroy */ struct timeval ct_wait; /* wait interval in milliseconds */ bool_t ct_waitset; /* wait set by clnt_control? */ struct netbuf ct_addr; /* remote addr */ struct rpc_err ct_error; union { char ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */ u_int32_t ct_mcalli; } ct_u; u_int ct_mpos; /* pos after marshal */ XDR ct_xdrs; /* XDR stream */ }; /* * This machinery implements per-fd locks for MT-safety. It is not * sufficient to do per-CLIENT handle locks for MT-safety because a * user may create more than one CLIENT handle with the same fd behind * it. Therfore, we allocate an array of flags (vc_fd_locks), protected * by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables * similarly protected. Vc_fd_lock[fd] == 1 => a call is activte on some * CLIENT handle created for that fd. * The current implementation holds locks across the entire RPC and reply. * Yes, this is silly, and as soon as this code is proven to work, this * should be the first thing fixed. One step at a time. */ static int *vc_fd_locks; static cond_t *vc_cv; #define release_fd_lock(fd, mask) { \ mutex_lock(&clnt_fd_lock); \ vc_fd_locks[fd] = 0; \ mutex_unlock(&clnt_fd_lock); \ thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \ cond_signal(&vc_cv[fd]); \ } static const char clnt_vc_errstr[] = "%s : %s"; static const char clnt_vc_str[] = "clnt_vc_create"; static const char clnt_read_vc_str[] = "read_vc"; static const char __no_mem_str[] = "out of memory"; /* * Create a client handle for a connection. * Default options are set, which the user can change using clnt_control()'s. * The rpc/vc package does buffering similar to stdio, so the client * must pick send and receive buffer sizes, 0 => use the default. * NB: fd is copied into a private area. * NB: The rpch->cl_auth is set null authentication. Caller may wish to * set this something more useful. * * fd should be an open socket */ CLIENT * clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz) int fd; /* open file descriptor */ const struct netbuf *raddr; /* servers address */ const rpcprog_t prog; /* program number */ const rpcvers_t vers; /* version number */ u_int sendsz; /* buffer recv size */ u_int recvsz; /* buffer send size */ { CLIENT *cl; /* client handle */ struct ct_data *ct = NULL; /* client handle */ struct timeval now; struct rpc_msg call_msg; static u_int32_t disrupt; sigset_t mask; sigset_t newmask; struct sockaddr_storage ss; socklen_t slen; struct __rpc_sockinfo si; if (disrupt == 0) disrupt = (u_int32_t)(long)raddr; cl = (CLIENT *)mem_alloc(sizeof (*cl)); ct = (struct ct_data *)mem_alloc(sizeof (*ct)); if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) { (void) syslog(LOG_ERR, clnt_vc_errstr, clnt_vc_str, __no_mem_str); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto err; } ct->ct_addr.buf = NULL; sigfillset(&newmask); thr_sigsetmask(SIG_SETMASK, &newmask, &mask); mutex_lock(&clnt_fd_lock); if (vc_fd_locks == (int *) NULL) { int cv_allocsz, fd_allocsz; int dtbsize = __rpc_dtbsize(); fd_allocsz = dtbsize * sizeof (int); vc_fd_locks = (int *) mem_alloc(fd_allocsz); if (vc_fd_locks == (int *) NULL) { mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); goto err; } else memset(vc_fd_locks, '\0', fd_allocsz); assert(vc_cv == (cond_t *) NULL); cv_allocsz = dtbsize * sizeof (cond_t); vc_cv = (cond_t *) mem_alloc(cv_allocsz); if (vc_cv == (cond_t *) NULL) { mem_free(vc_fd_locks, fd_allocsz); vc_fd_locks = (int *) NULL; mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); goto err; } else { int i; for (i = 0; i < dtbsize; i++) cond_init(&vc_cv[i], 0, (void *) 0); } } else assert(vc_cv != (cond_t *) NULL); /* * XXX - fvdl connecting while holding a mutex? */ slen = sizeof ss; if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { if (errno != ENOTCONN) { rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); goto err; } if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){ rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); goto err; } } mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); if (!__rpc_fd2sockinfo(fd, &si)) goto err; ct->ct_closeit = FALSE; /* * Set up private data struct */ ct->ct_fd = fd; ct->ct_wait.tv_usec = 0; ct->ct_waitset = FALSE; ct->ct_addr.buf = malloc(raddr->maxlen); if (ct->ct_addr.buf == NULL) goto err; memcpy(ct->ct_addr.buf, raddr->buf, raddr->len); ct->ct_addr.len = raddr->len; ct->ct_addr.maxlen = raddr->maxlen; /* * Initialize call message */ (void)gettimeofday(&now, NULL); call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now); call_msg.rm_direction = CALL; call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; call_msg.rm_call.cb_prog = (u_int32_t)prog; call_msg.rm_call.cb_vers = (u_int32_t)vers; /* * pre-serialize the static part of the call msg and stash it away */ xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE, XDR_ENCODE); if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { if (ct->ct_closeit) { (void)_close(fd); } goto err; } ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs)); XDR_DESTROY(&(ct->ct_xdrs)); assert(ct->ct_mpos + sizeof(uint32_t) <= MCALL_MSG_SIZE); /* * Create a client handle which uses xdrrec for serialization * and authnone for authentication. */ cl->cl_ops = clnt_vc_ops(); cl->cl_private = ct; cl->cl_auth = authnone_create(); sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, cl->cl_private, read_vc, write_vc); return (cl); err: if (ct) { if (ct->ct_addr.len) mem_free(ct->ct_addr.buf, ct->ct_addr.len); mem_free(ct, sizeof (struct ct_data)); } if (cl) mem_free(cl, sizeof (CLIENT)); return ((CLIENT *)NULL); } static enum clnt_stat clnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) CLIENT *cl; rpcproc_t proc; xdrproc_t xdr_args; void *args_ptr; xdrproc_t xdr_results; void *results_ptr; struct timeval timeout; { struct ct_data *ct = (struct ct_data *) cl->cl_private; XDR *xdrs = &(ct->ct_xdrs); struct rpc_msg reply_msg; u_int32_t x_id; u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli; /* yuk */ bool_t shipnow; int refreshes = 2; sigset_t mask, newmask; int rpc_lock_value; bool_t reply_stat; assert(cl != NULL); sigfillset(&newmask); thr_sigsetmask(SIG_SETMASK, &newmask, &mask); mutex_lock(&clnt_fd_lock); while (vc_fd_locks[ct->ct_fd]) cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); if (__isthreaded) rpc_lock_value = 1; else rpc_lock_value = 0; vc_fd_locks[ct->ct_fd] = rpc_lock_value; mutex_unlock(&clnt_fd_lock); if (!ct->ct_waitset) { /* If time is not within limits, we ignore it. */ if (time_not_ok(&timeout) == FALSE) ct->ct_wait = timeout; } shipnow = (xdr_results == NULL && timeout.tv_sec == 0 && timeout.tv_usec == 0) ? FALSE : TRUE; call_again: xdrs->x_op = XDR_ENCODE; ct->ct_error.re_status = RPC_SUCCESS; x_id = ntohl(--(*msg_x_id)); if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) || (! XDR_PUTINT32(xdrs, &proc)) || (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || (! (*xdr_args)(xdrs, args_ptr))) { if (ct->ct_error.re_status == RPC_SUCCESS) ct->ct_error.re_status = RPC_CANTENCODEARGS; (void)xdrrec_endofrecord(xdrs, TRUE); release_fd_lock(ct->ct_fd, mask); return (ct->ct_error.re_status); } } else { *(uint32_t *) &ct->ct_u.ct_mcallc[ct->ct_mpos] = htonl(proc); if (! __rpc_gss_wrap(cl->cl_auth, ct->ct_u.ct_mcallc, ct->ct_mpos + sizeof(uint32_t), xdrs, xdr_args, args_ptr)) { if (ct->ct_error.re_status == RPC_SUCCESS) ct->ct_error.re_status = RPC_CANTENCODEARGS; (void)xdrrec_endofrecord(xdrs, TRUE); release_fd_lock(ct->ct_fd, mask); return (ct->ct_error.re_status); } } if (! xdrrec_endofrecord(xdrs, shipnow)) { release_fd_lock(ct->ct_fd, mask); return (ct->ct_error.re_status = RPC_CANTSEND); } if (! shipnow) { release_fd_lock(ct->ct_fd, mask); return (RPC_SUCCESS); } /* * Hack to provide rpc-based message passing */ if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { release_fd_lock(ct->ct_fd, mask); return(ct->ct_error.re_status = RPC_TIMEDOUT); } /* * Keep receiving until we get a valid transaction id */ xdrs->x_op = XDR_DECODE; while (TRUE) { reply_msg.acpted_rply.ar_verf = _null_auth; reply_msg.acpted_rply.ar_results.where = NULL; reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; if (! xdrrec_skiprecord(xdrs)) { release_fd_lock(ct->ct_fd, mask); return (ct->ct_error.re_status); } /* now decode and validate the response header */ if (! xdr_replymsg(xdrs, &reply_msg)) { if (ct->ct_error.re_status == RPC_SUCCESS) continue; release_fd_lock(ct->ct_fd, mask); return (ct->ct_error.re_status); } if (reply_msg.rm_xid == x_id) break; } /* * process header */ _seterr_reply(&reply_msg, &(ct->ct_error)); if (ct->ct_error.re_status == RPC_SUCCESS) { if (! AUTH_VALIDATE(cl->cl_auth, &reply_msg.acpted_rply.ar_verf)) { ct->ct_error.re_status = RPC_AUTHERROR; ct->ct_error.re_why = AUTH_INVALIDRESP; } else { if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { reply_stat = (*xdr_results)(xdrs, results_ptr); } else { reply_stat = __rpc_gss_unwrap(cl->cl_auth, xdrs, xdr_results, results_ptr); } if (! reply_stat) { if (ct->ct_error.re_status == RPC_SUCCESS) ct->ct_error.re_status = RPC_CANTDECODERES; } } /* free verifier ... */ if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { xdrs->x_op = XDR_FREE; (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf)); } } /* end successful completion */ else { /* maybe our credentials need to be refreshed ... */ if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg)) goto call_again; } /* end of unsuccessful completion */ release_fd_lock(ct->ct_fd, mask); return (ct->ct_error.re_status); } static void clnt_vc_geterr(cl, errp) CLIENT *cl; struct rpc_err *errp; { struct ct_data *ct; assert(cl != NULL); assert(errp != NULL); ct = (struct ct_data *) cl->cl_private; *errp = ct->ct_error; } static bool_t clnt_vc_freeres(cl, xdr_res, res_ptr) CLIENT *cl; xdrproc_t xdr_res; void *res_ptr; { struct ct_data *ct; XDR *xdrs; bool_t dummy; sigset_t mask; sigset_t newmask; assert(cl != NULL); ct = (struct ct_data *)cl->cl_private; xdrs = &(ct->ct_xdrs); sigfillset(&newmask); thr_sigsetmask(SIG_SETMASK, &newmask, &mask); mutex_lock(&clnt_fd_lock); while (vc_fd_locks[ct->ct_fd]) cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); xdrs->x_op = XDR_FREE; dummy = (*xdr_res)(xdrs, res_ptr); mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); cond_signal(&vc_cv[ct->ct_fd]); return dummy; } /*ARGSUSED*/ static void clnt_vc_abort(cl) CLIENT *cl; { } static bool_t clnt_vc_control(cl, request, info) CLIENT *cl; u_int request; void *info; { struct ct_data *ct; void *infop = info; sigset_t mask; sigset_t newmask; int rpc_lock_value; assert(cl != NULL); ct = (struct ct_data *)cl->cl_private; sigfillset(&newmask); thr_sigsetmask(SIG_SETMASK, &newmask, &mask); mutex_lock(&clnt_fd_lock); while (vc_fd_locks[ct->ct_fd]) cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); if (__isthreaded) rpc_lock_value = 1; else rpc_lock_value = 0; vc_fd_locks[ct->ct_fd] = rpc_lock_value; mutex_unlock(&clnt_fd_lock); switch (request) { case CLSET_FD_CLOSE: ct->ct_closeit = TRUE; release_fd_lock(ct->ct_fd, mask); return (TRUE); case CLSET_FD_NCLOSE: ct->ct_closeit = FALSE; release_fd_lock(ct->ct_fd, mask); return (TRUE); default: break; } /* for other requests which use info */ if (info == NULL) { release_fd_lock(ct->ct_fd, mask); return (FALSE); } switch (request) { case CLSET_TIMEOUT: if (time_not_ok((struct timeval *)info)) { release_fd_lock(ct->ct_fd, mask); return (FALSE); } ct->ct_wait = *(struct timeval *)infop; ct->ct_waitset = TRUE; break; case CLGET_TIMEOUT: *(struct timeval *)infop = ct->ct_wait; break; case CLGET_SERVER_ADDR: (void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len); break; case CLGET_FD: *(int *)info = ct->ct_fd; break; case CLGET_SVC_ADDR: /* The caller should not free this memory area */ *(struct netbuf *)info = ct->ct_addr; break; case CLSET_SVC_ADDR: /* set to new address */ release_fd_lock(ct->ct_fd, mask); return (FALSE); case CLGET_XID: /* * use the knowledge that xid is the * first element in the call structure * This will get the xid of the PREVIOUS call */ *(u_int32_t *)info = ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli); break; case CLSET_XID: /* This will set the xid of the NEXT call */ *(u_int32_t *)(void *)&ct->ct_u.ct_mcalli = htonl(*((u_int32_t *)info) + 1); /* increment by 1 as clnt_vc_call() decrements once */ break; case CLGET_VERS: /* * This RELIES on the information that, in the call body, * the version number field is the fifth field from the * begining of the RPC header. MUST be changed if the * call_struct is changed */ *(u_int32_t *)info = ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT)); break; case CLSET_VERS: *(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT) = htonl(*(u_int32_t *)info); break; case CLGET_PROG: /* * This RELIES on the information that, in the call body, * the program number field is the fourth field from the * begining of the RPC header. MUST be changed if the * call_struct is changed */ *(u_int32_t *)info = ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT)); break; case CLSET_PROG: *(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT) = htonl(*(u_int32_t *)info); break; default: release_fd_lock(ct->ct_fd, mask); return (FALSE); } release_fd_lock(ct->ct_fd, mask); return (TRUE); } static void clnt_vc_destroy(cl) CLIENT *cl; { struct ct_data *ct = (struct ct_data *) cl->cl_private; int ct_fd = ct->ct_fd; sigset_t mask; sigset_t newmask; assert(cl != NULL); ct = (struct ct_data *) cl->cl_private; sigfillset(&newmask); thr_sigsetmask(SIG_SETMASK, &newmask, &mask); mutex_lock(&clnt_fd_lock); while (vc_fd_locks[ct_fd]) cond_wait(&vc_cv[ct_fd], &clnt_fd_lock); if (ct->ct_closeit && ct->ct_fd != -1) { (void)_close(ct->ct_fd); } XDR_DESTROY(&(ct->ct_xdrs)); - if (ct->ct_addr.buf) - free(ct->ct_addr.buf); + free(ct->ct_addr.buf); mem_free(ct, sizeof(struct ct_data)); if (cl->cl_netid && cl->cl_netid[0]) mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); if (cl->cl_tp && cl->cl_tp[0]) mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); mem_free(cl, sizeof(CLIENT)); mutex_unlock(&clnt_fd_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); cond_signal(&vc_cv[ct_fd]); } /* * Interface between xdr serializer and tcp connection. * Behaves like the system calls, read & write, but keeps some error state * around for the rpc level. */ static int read_vc(ctp, buf, len) void *ctp; void *buf; int len; { struct sockaddr sa; socklen_t sal; struct ct_data *ct = (struct ct_data *)ctp; struct pollfd fd; int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) + (ct->ct_wait.tv_usec / 1000)); if (len == 0) return (0); fd.fd = ct->ct_fd; fd.events = POLLIN; for (;;) { switch (_poll(&fd, 1, milliseconds)) { case 0: ct->ct_error.re_status = RPC_TIMEDOUT; return (-1); case -1: if (errno == EINTR) continue; ct->ct_error.re_status = RPC_CANTRECV; ct->ct_error.re_errno = errno; return (-1); } break; } sal = sizeof(sa); if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) && (sa.sa_family == AF_LOCAL)) { len = __msgread(ct->ct_fd, buf, (size_t)len); } else { len = _read(ct->ct_fd, buf, (size_t)len); } switch (len) { case 0: /* premature eof */ ct->ct_error.re_errno = ECONNRESET; ct->ct_error.re_status = RPC_CANTRECV; len = -1; /* it's really an error */ break; case -1: ct->ct_error.re_errno = errno; ct->ct_error.re_status = RPC_CANTRECV; break; } return (len); } static int write_vc(ctp, buf, len) void *ctp; void *buf; int len; { struct sockaddr sa; socklen_t sal; struct ct_data *ct = (struct ct_data *)ctp; int i, cnt; sal = sizeof(sa); if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) && (sa.sa_family == AF_LOCAL)) { for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) { if ((i = __msgwrite(ct->ct_fd, buf, (size_t)cnt)) == -1) { ct->ct_error.re_errno = errno; ct->ct_error.re_status = RPC_CANTSEND; return (-1); } } } else { for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) { if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) { ct->ct_error.re_errno = errno; ct->ct_error.re_status = RPC_CANTSEND; return (-1); } } } return (len); } static struct clnt_ops * clnt_vc_ops() { static struct clnt_ops ops; sigset_t mask, newmask; /* VARIABLES PROTECTED BY ops_lock: ops */ sigfillset(&newmask); thr_sigsetmask(SIG_SETMASK, &newmask, &mask); mutex_lock(&ops_lock); if (ops.cl_call == NULL) { ops.cl_call = clnt_vc_call; ops.cl_abort = clnt_vc_abort; ops.cl_geterr = clnt_vc_geterr; ops.cl_freeres = clnt_vc_freeres; ops.cl_destroy = clnt_vc_destroy; ops.cl_control = clnt_vc_control; } mutex_unlock(&ops_lock); thr_sigsetmask(SIG_SETMASK, &(mask), NULL); return (&ops); } /* * Make sure that the time is not garbage. -1 value is disallowed. * Note this is different from time_not_ok in clnt_dg.c */ static bool_t time_not_ok(t) struct timeval *t; { return (t->tv_sec <= -1 || t->tv_sec > 100000000 || t->tv_usec <= -1 || t->tv_usec > 1000000); } static int __msgread(sock, buf, cnt) int sock; void *buf; size_t cnt; { struct iovec iov[1]; struct msghdr msg; union { struct cmsghdr cmsg; char control[CMSG_SPACE(sizeof(struct cmsgcred))]; } cm; bzero((char *)&cm, sizeof(cm)); iov[0].iov_base = buf; iov[0].iov_len = cnt; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = (caddr_t)&cm; msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred)); msg.msg_flags = 0; return(_recvmsg(sock, &msg, 0)); } static int __msgwrite(sock, buf, cnt) int sock; void *buf; size_t cnt; { struct iovec iov[1]; struct msghdr msg; union { struct cmsghdr cmsg; char control[CMSG_SPACE(sizeof(struct cmsgcred))]; } cm; bzero((char *)&cm, sizeof(cm)); iov[0].iov_base = buf; iov[0].iov_len = cnt; cm.cmsg.cmsg_type = SCM_CREDS; cm.cmsg.cmsg_level = SOL_SOCKET; cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred)); msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = (caddr_t)&cm; msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred)); msg.msg_flags = 0; return(_sendmsg(sock, &msg, 0)); } Index: stable/10/lib/libc/rpc/getnetconfig.c =================================================================== --- stable/10/lib/libc/rpc/getnetconfig.c (revision 290898) +++ stable/10/lib/libc/rpc/getnetconfig.c (revision 290899) @@ -1,740 +1,737 @@ /* $NetBSD: getnetconfig.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)getnetconfig.c 1.12 91/12/19 SMI"; #endif #include __FBSDID("$FreeBSD$"); /* * Copyright (c) 1989 by Sun Microsystems, Inc. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "rpc_com.h" /* * The five library routines in this file provide application access to the * system network configuration database, /etc/netconfig. In addition to the * netconfig database and the routines for accessing it, the environment * variable NETPATH and its corresponding routines in getnetpath.c may also be * used to specify the network transport to be used. */ /* * netconfig errors */ #define NC_NONETCONFIG ENOENT #define NC_NOMEM ENOMEM #define NC_NOTINIT EINVAL /* setnetconfig was not called first */ #define NC_BADFILE EBADF /* format for netconfig file is bad */ #define NC_NOTFOUND ENOPROTOOPT /* specified netid was not found */ /* * semantics as strings (should be in netconfig.h) */ #define NC_TPI_CLTS_S "tpi_clts" #define NC_TPI_COTS_S "tpi_cots" #define NC_TPI_COTS_ORD_S "tpi_cots_ord" #define NC_TPI_RAW_S "tpi_raw" /* * flags as characters (also should be in netconfig.h) */ #define NC_NOFLAG_C '-' #define NC_VISIBLE_C 'v' #define NC_BROADCAST_C 'b' /* * Character used to indicate there is no name-to-address lookup library */ #define NC_NOLOOKUP "-" static const char * const _nc_errors[] = { "Netconfig database not found", "Not enough memory", "Not initialized", "Netconfig database has invalid format", "Netid not found in netconfig database" }; struct netconfig_info { int eof; /* all entries has been read */ int ref; /* # of times setnetconfig() has been called */ struct netconfig_list *head; /* head of the list */ struct netconfig_list *tail; /* last of the list */ }; struct netconfig_list { char *linep; /* hold line read from netconfig */ struct netconfig *ncp; struct netconfig_list *next; }; struct netconfig_vars { int valid; /* token that indicates a valid netconfig_vars */ int flag; /* first time flag */ struct netconfig_list *nc_configs; /* pointer to the current netconfig entry */ }; #define NC_VALID 0xfeed #define NC_STORAGE 0xf00d #define NC_INVALID 0 static int *__nc_error(void); static int parse_ncp(char *, struct netconfig *); static struct netconfig *dup_ncp(struct netconfig *); static FILE *nc_file; /* for netconfig db */ static mutex_t nc_file_lock = MUTEX_INITIALIZER; static struct netconfig_info ni = { 0, 0, NULL, NULL}; static mutex_t ni_lock = MUTEX_INITIALIZER; static thread_key_t nc_key; static once_t nc_once = ONCE_INITIALIZER; static int nc_key_error; static void nc_key_init(void) { nc_key_error = thr_keycreate(&nc_key, free); } #define MAXNETCONFIGLINE 1000 static int * __nc_error() { static int nc_error = 0; int *nc_addr; /* * Use the static `nc_error' if we are the main thread * (including non-threaded programs), or if an allocation * fails. */ if (thr_main()) return (&nc_error); if (thr_once(&nc_once, nc_key_init) != 0 || nc_key_error != 0) return (&nc_error); if ((nc_addr = (int *)thr_getspecific(nc_key)) == NULL) { nc_addr = (int *)malloc(sizeof (int)); if (thr_setspecific(nc_key, (void *) nc_addr) != 0) { - if (nc_addr) - free(nc_addr); + free(nc_addr); return (&nc_error); } *nc_addr = 0; } return (nc_addr); } #define nc_error (*(__nc_error())) /* * A call to setnetconfig() establishes a /etc/netconfig "session". A session * "handle" is returned on a successful call. At the start of a session (after * a call to setnetconfig()) searches through the /etc/netconfig database will * proceed from the start of the file. The session handle must be passed to * getnetconfig() to parse the file. Each call to getnetconfig() using the * current handle will process one subsequent entry in /etc/netconfig. * setnetconfig() must be called before the first call to getnetconfig(). * (Handles are used to allow for nested calls to setnetpath()). * * A new session is established with each call to setnetconfig(), with a new * handle being returned on each call. Previously established sessions remain * active until endnetconfig() is called with that session's handle as an * argument. * * setnetconfig() need *not* be called before a call to getnetconfigent(). * setnetconfig() returns a NULL pointer on failure (for example, if * the netconfig database is not present). */ void * setnetconfig() { struct netconfig_vars *nc_vars; if ((nc_vars = (struct netconfig_vars *)malloc(sizeof (struct netconfig_vars))) == NULL) { return(NULL); } /* * For multiple calls, i.e. nc_file is not NULL, we just return the * handle without reopening the netconfig db. */ mutex_lock(&ni_lock); ni.ref++; mutex_unlock(&ni_lock); mutex_lock(&nc_file_lock); if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) { nc_vars->valid = NC_VALID; nc_vars->flag = 0; nc_vars->nc_configs = ni.head; mutex_unlock(&nc_file_lock); return ((void *)nc_vars); } mutex_unlock(&nc_file_lock); mutex_lock(&ni_lock); ni.ref--; mutex_unlock(&ni_lock); nc_error = NC_NONETCONFIG; free(nc_vars); return (NULL); } /* * When first called, getnetconfig() returns a pointer to the first entry in * the netconfig database, formatted as a struct netconfig. On each subsequent * call, getnetconfig() returns a pointer to the next entry in the database. * getnetconfig() can thus be used to search the entire netconfig file. * getnetconfig() returns NULL at end of file. */ struct netconfig * getnetconfig(handlep) void *handlep; { struct netconfig_vars *ncp = (struct netconfig_vars *)handlep; char *stringp; /* tmp string pointer */ struct netconfig_list *list; struct netconfig *np; struct netconfig *result; /* * Verify that handle is valid */ mutex_lock(&nc_file_lock); if (ncp == NULL || nc_file == NULL) { nc_error = NC_NOTINIT; mutex_unlock(&nc_file_lock); return (NULL); } mutex_unlock(&nc_file_lock); switch (ncp->valid) { case NC_VALID: /* * If entry has already been read into the list, * we return the entry in the linked list. * If this is the first time call, check if there are any entries in * linked list. If no entries, we need to read the netconfig db. * If we have been here and the next entry is there, we just return * it. */ if (ncp->flag == 0) { /* first time */ ncp->flag = 1; mutex_lock(&ni_lock); ncp->nc_configs = ni.head; mutex_unlock(&ni_lock); if (ncp->nc_configs != NULL) /* entry already exist */ return(ncp->nc_configs->ncp); } else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) { ncp->nc_configs = ncp->nc_configs->next; return(ncp->nc_configs->ncp); } /* * If we cannot find the entry in the list and is end of file, * we give up. */ mutex_lock(&ni_lock); if (ni.eof == 1) { mutex_unlock(&ni_lock); return(NULL); } mutex_unlock(&ni_lock); break; default: nc_error = NC_NOTINIT; return (NULL); } stringp = (char *) malloc(MAXNETCONFIGLINE); if (stringp == NULL) return (NULL); #ifdef MEM_CHK if (malloc_verify() == 0) { fprintf(stderr, "memory heap corrupted in getnetconfig\n"); exit(1); } #endif /* * Read a line from netconfig file. */ mutex_lock(&nc_file_lock); do { if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) { free(stringp); mutex_lock(&ni_lock); ni.eof = 1; mutex_unlock(&ni_lock); mutex_unlock(&nc_file_lock); return (NULL); } } while (*stringp == '#'); mutex_unlock(&nc_file_lock); list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list)); if (list == NULL) { free(stringp); return(NULL); } np = (struct netconfig *) malloc(sizeof (struct netconfig)); if (np == NULL) { free(stringp); free(list); return(NULL); } list->ncp = np; list->next = NULL; list->ncp->nc_lookups = NULL; list->linep = stringp; if (parse_ncp(stringp, list->ncp) == -1) { free(stringp); free(np); free(list); return (NULL); } else { /* * If this is the first entry that's been read, it is the head of * the list. If not, put the entry at the end of the list. * Reposition the current pointer of the handle to the last entry * in the list. */ mutex_lock(&ni_lock); if (ni.head == NULL) { /* first entry */ ni.head = ni.tail = list; } else { ni.tail->next = list; ni.tail = ni.tail->next; } ncp->nc_configs = ni.tail; result = ni.tail->ncp; mutex_unlock(&ni_lock); return(result); } } /* * endnetconfig() may be called to "unbind" or "close" the netconfig database * when processing is complete, releasing resources for reuse. endnetconfig() * may not be called before setnetconfig(). endnetconfig() returns 0 on * success and -1 on failure (for example, if setnetconfig() was not called * previously). */ int endnetconfig(handlep) void *handlep; { struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep; struct netconfig_list *q, *p; /* * Verify that handle is valid */ if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID && nc_handlep->valid != NC_STORAGE)) { nc_error = NC_NOTINIT; return (-1); } /* * Return 0 if anyone still needs it. */ nc_handlep->valid = NC_INVALID; nc_handlep->flag = 0; nc_handlep->nc_configs = NULL; mutex_lock(&ni_lock); if (--ni.ref > 0) { mutex_unlock(&ni_lock); free(nc_handlep); return(0); } /* * Noone needs these entries anymore, then frees them. * Make sure all info in netconfig_info structure has been reinitialized. */ q = ni.head; ni.eof = ni.ref = 0; ni.head = NULL; ni.tail = NULL; mutex_unlock(&ni_lock); while (q != NULL) { p = q->next; - if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups); + free(q->ncp->nc_lookups); free(q->ncp); free(q->linep); free(q); q = p; } free(nc_handlep); mutex_lock(&nc_file_lock); fclose(nc_file); nc_file = NULL; mutex_unlock(&nc_file_lock); return (0); } /* * getnetconfigent(netid) returns a pointer to the struct netconfig structure * corresponding to netid. It returns NULL if netid is invalid (that is, does * not name an entry in the netconfig database). It returns NULL and sets * errno in case of failure (for example, if the netconfig database cannot be * opened). */ struct netconfig * getnetconfigent(netid) const char *netid; { FILE *file; /* NETCONFIG db's file pointer */ char *linep; /* holds current netconfig line */ char *stringp; /* temporary string pointer */ struct netconfig *ncp = NULL; /* returned value */ struct netconfig_list *list; /* pointer to cache list */ nc_error = NC_NOTFOUND; /* default error. */ if (netid == NULL || strlen(netid) == 0) { return (NULL); } /* * Look up table if the entries have already been read and parsed in * getnetconfig(), then copy this entry into a buffer and return it. * If we cannot find the entry in the current list and there are more * entries in the netconfig db that has not been read, we then read the * db and try find the match netid. * If all the netconfig db has been read and placed into the list and * there is no match for the netid, return NULL. */ mutex_lock(&ni_lock); if (ni.head != NULL) { for (list = ni.head; list; list = list->next) { if (strcmp(list->ncp->nc_netid, netid) == 0) { mutex_unlock(&ni_lock); return(dup_ncp(list->ncp)); } } if (ni.eof == 1) { /* that's all the entries */ mutex_unlock(&ni_lock); return(NULL); } } mutex_unlock(&ni_lock); if ((file = fopen(NETCONFIG, "r")) == NULL) { nc_error = NC_NONETCONFIG; return (NULL); } if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) { fclose(file); nc_error = NC_NOMEM; return (NULL); } do { ptrdiff_t len; char *tmpp; /* tmp string pointer */ do { if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) { break; } } while (*stringp == '#'); if (stringp == NULL) { /* eof */ break; } if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { /* can't parse file */ nc_error = NC_BADFILE; break; } if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */ strncmp(stringp, netid, (size_t)len) == 0) { if ((ncp = (struct netconfig *) malloc(sizeof (struct netconfig))) == NULL) { break; } ncp->nc_lookups = NULL; if (parse_ncp(linep, ncp) == -1) { free(ncp); ncp = NULL; } break; } } while (stringp != NULL); if (ncp == NULL) { free(linep); } fclose(file); return(ncp); } /* * freenetconfigent(netconfigp) frees the netconfig structure pointed to by * netconfigp (previously returned by getnetconfigent()). */ void freenetconfigent(netconfigp) struct netconfig *netconfigp; { if (netconfigp != NULL) { free(netconfigp->nc_netid); /* holds all netconfigp's strings */ - if (netconfigp->nc_lookups != NULL) - free(netconfigp->nc_lookups); + free(netconfigp->nc_lookups); free(netconfigp); } return; } /* * Parse line and stuff it in a struct netconfig * Typical line might look like: * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so * * We return -1 if any of the tokens don't parse, or malloc fails. * * Note that we modify stringp (putting NULLs after tokens) and * we set the ncp's string field pointers to point to these tokens within * stringp. */ static int parse_ncp(stringp, ncp) char *stringp; /* string to parse */ struct netconfig *ncp; /* where to put results */ { char *tokenp; /* for processing tokens */ char *lasts; char **nc_lookups; nc_error = NC_BADFILE; /* nearly anything that breaks is for this reason */ stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */ /* netid */ if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) { return (-1); } /* semantics */ if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) { return (-1); } if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0) ncp->nc_semantics = NC_TPI_COTS_ORD; else if (strcmp(tokenp, NC_TPI_COTS_S) == 0) ncp->nc_semantics = NC_TPI_COTS; else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0) ncp->nc_semantics = NC_TPI_CLTS; else if (strcmp(tokenp, NC_TPI_RAW_S) == 0) ncp->nc_semantics = NC_TPI_RAW; else return (-1); /* flags */ if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) { return (-1); } for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0'; tokenp++) { switch (*tokenp) { case NC_NOFLAG_C: break; case NC_VISIBLE_C: ncp->nc_flag |= NC_VISIBLE; break; case NC_BROADCAST_C: ncp->nc_flag |= NC_BROADCAST; break; default: return (-1); } } /* protocol family */ if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) { return (-1); } /* protocol name */ if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) { return (-1); } /* network device */ if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) { return (-1); } if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) { return (-1); } if (strcmp(tokenp, NC_NOLOOKUP) == 0) { ncp->nc_nlookups = 0; ncp->nc_lookups = NULL; } else { char *cp; /* tmp string */ - if (ncp->nc_lookups != NULL) /* from last visit */ - free(ncp->nc_lookups); + free(ncp->nc_lookups); /* from last visit */ ncp->nc_lookups = NULL; ncp->nc_nlookups = 0; while ((cp = tokenp) != NULL) { if ((nc_lookups = realloc(ncp->nc_lookups, (ncp->nc_nlookups + 1) * sizeof *ncp->nc_lookups)) == NULL) { free(ncp->nc_lookups); ncp->nc_lookups = NULL; return (-1); } tokenp = _get_next_token(cp, ','); ncp->nc_lookups = nc_lookups; ncp->nc_lookups[ncp->nc_nlookups++] = cp; } } return (0); } /* * Returns a string describing the reason for failure. */ char * nc_sperror() { const char *message; switch(nc_error) { case NC_NONETCONFIG: message = _nc_errors[0]; break; case NC_NOMEM: message = _nc_errors[1]; break; case NC_NOTINIT: message = _nc_errors[2]; break; case NC_BADFILE: message = _nc_errors[3]; break; case NC_NOTFOUND: message = _nc_errors[4]; break; default: message = "Unknown network selection error"; } /* LINTED const castaway */ return ((char *)message); } /* * Prints a message onto standard error describing the reason for failure. */ void nc_perror(s) const char *s; { fprintf(stderr, "%s: %s\n", s, nc_sperror()); } /* * Duplicates the matched netconfig buffer. */ static struct netconfig * dup_ncp(ncp) struct netconfig *ncp; { struct netconfig *p; char *tmp; u_int i; if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL) return(NULL); if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) { free(tmp); return(NULL); } /* * First we dup all the data from matched netconfig buffer. Then we * adjust some of the member pointer to a pre-allocated buffer where * contains part of the data. * To follow the convention used in parse_ncp(), we store all the * necessary information in the pre-allocated buffer and let each * of the netconfig char pointer member point to the right address * in the buffer. */ *p = *ncp; p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid); tmp = strchr(tmp, '\0') + 1; p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly); tmp = strchr(tmp, '\0') + 1; p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto); tmp = strchr(tmp, '\0') + 1; p->nc_device = (char *)strcpy(tmp,ncp->nc_device); p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *)); if (p->nc_lookups == NULL) { free(p->nc_netid); free(p); return(NULL); } for (i=0; i < p->nc_nlookups; i++) { tmp = strchr(tmp, '\0') + 1; p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]); } return(p); } Index: stable/10/lib/libc/rpc/mt_misc.c =================================================================== --- stable/10/lib/libc/rpc/mt_misc.c (revision 290898) +++ stable/10/lib/libc/rpc/mt_misc.c (revision 290899) @@ -1,117 +1,116 @@ /* $NetBSD: mt_misc.c,v 1.1 2000/06/02 23:11:11 fvdl Exp $ */ /* #pragma ident "@(#)mt_misc.c 1.24 93/04/29 SMI" */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include "reentrant.h" #include #include #include #include #include "un-namespace.h" #include "mt_misc.h" /* Take these objects out of the application namespace. */ #define svc_lock __svc_lock #define svc_fd_lock __svc_fd_lock #define rpcbaddr_cache_lock __rpcbaddr_cache_lock #define authdes_ops_lock __authdes_ops_lock #define authnone_lock __authnone_lock #define authsvc_lock __authsvc_lock #define clnt_fd_lock __clnt_fd_lock #define clntraw_lock __clntraw_lock #define dupreq_lock __dupreq_lock #define loopnconf_lock __loopnconf_lock #define ops_lock __ops_lock #define proglst_lock __proglst_lock #define rpcsoc_lock __rpcsoc_lock #define svcraw_lock __svcraw_lock #define xprtlist_lock __xprtlist_lock /* protects the services list (svc.c) */ pthread_rwlock_t svc_lock = PTHREAD_RWLOCK_INITIALIZER; /* protects svc_fdset and the xports[] array */ pthread_rwlock_t svc_fd_lock = PTHREAD_RWLOCK_INITIALIZER; /* protects the RPCBIND address cache */ pthread_rwlock_t rpcbaddr_cache_lock = PTHREAD_RWLOCK_INITIALIZER; /* serializes authdes ops initializations */ pthread_mutex_t authdes_ops_lock = PTHREAD_MUTEX_INITIALIZER; /* protects des stats list */ pthread_mutex_t svcauthdesstats_lock = PTHREAD_MUTEX_INITIALIZER; /* auth_none.c serialization */ pthread_mutex_t authnone_lock = PTHREAD_MUTEX_INITIALIZER; /* protects the Auths list (svc_auth.c) */ pthread_mutex_t authsvc_lock = PTHREAD_MUTEX_INITIALIZER; /* protects client-side fd lock array */ pthread_mutex_t clnt_fd_lock = PTHREAD_MUTEX_INITIALIZER; /* clnt_raw.c serialization */ pthread_mutex_t clntraw_lock = PTHREAD_MUTEX_INITIALIZER; /* dupreq variables (svc_dg.c) */ pthread_mutex_t dupreq_lock = PTHREAD_MUTEX_INITIALIZER; /* loopnconf (rpcb_clnt.c) */ pthread_mutex_t loopnconf_lock = PTHREAD_MUTEX_INITIALIZER; /* serializes ops initializations */ pthread_mutex_t ops_lock = PTHREAD_MUTEX_INITIALIZER; /* protects proglst list (svc_simple.c) */ pthread_mutex_t proglst_lock = PTHREAD_MUTEX_INITIALIZER; /* serializes clnt_com_create() (rpc_soc.c) */ pthread_mutex_t rpcsoc_lock = PTHREAD_MUTEX_INITIALIZER; /* svc_raw.c serialization */ pthread_mutex_t svcraw_lock = PTHREAD_MUTEX_INITIALIZER; /* xprtlist (svc_generic.c) */ pthread_mutex_t xprtlist_lock = PTHREAD_MUTEX_INITIALIZER; #undef rpc_createerr struct rpc_createerr rpc_createerr; static thread_key_t rce_key; static once_t rce_once = ONCE_INITIALIZER; static int rce_key_error; static void rce_key_init(void) { rce_key_error = thr_keycreate(&rce_key, free); } struct rpc_createerr * __rpc_createerr() { struct rpc_createerr *rce_addr = 0; if (thr_main()) return (&rpc_createerr); if (thr_once(&rce_once, rce_key_init) != 0 || rce_key_error != 0) return (&rpc_createerr); rce_addr = (struct rpc_createerr *)thr_getspecific(rce_key); if (!rce_addr) { rce_addr = (struct rpc_createerr *) malloc(sizeof (struct rpc_createerr)); if (thr_setspecific(rce_key, (void *) rce_addr) != 0) { - if (rce_addr) - free(rce_addr); + free(rce_addr); return (&rpc_createerr); } memset(rce_addr, 0, sizeof (struct rpc_createerr)); return (rce_addr); } return (rce_addr); } Index: stable/10/lib/libc/rpc/rpc_soc.c =================================================================== --- stable/10/lib/libc/rpc/rpc_soc.c (revision 290898) +++ stable/10/lib/libc/rpc/rpc_soc.c (revision 290899) @@ -1,581 +1,580 @@ /* $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */ /* * Copyright (c) 1986-1991 by Sun Microsystems Inc. * In addition, portions of such source code were derived from Berkeley * 4.3 BSD under license from the Regents of the University of * California. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro"; #endif #include __FBSDID("$FreeBSD$"); #ifdef PORTMAP /* * rpc_soc.c * * The backward compatibility routines for the earlier implementation * of RPC, where the only transports supported were tcp/ip and udp/ip. * Based on berkeley socket abstraction, now implemented on the top * of TLI/Streams */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "rpc_com.h" #include "mt_misc.h" static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t, int *, u_int, u_int, char *); static SVCXPRT *svc_com_create(int, u_int, u_int, char *); static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *); /* XXX */ #define IN4_LOCALHOST_STRING "127.0.0.1" #define IN6_LOCALHOST_STRING "::1" /* * A common clnt create routine */ static CLIENT * clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp) struct sockaddr_in *raddr; rpcprog_t prog; rpcvers_t vers; int *sockp; u_int sendsz; u_int recvsz; char *tp; { CLIENT *cl; int madefd = FALSE; int fd = *sockp; struct netconfig *nconf; struct netbuf bindaddr; mutex_lock(&rpcsoc_lock); if ((nconf = __rpc_getconfip(tp)) == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; mutex_unlock(&rpcsoc_lock); return (NULL); } if (fd == RPC_ANYSOCK) { fd = __rpc_nconf2fd(nconf); if (fd == -1) goto syserror; madefd = TRUE; } if (raddr->sin_port == 0) { u_int proto; u_short sport; mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */ proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP; sport = pmap_getport(raddr, (u_long)prog, (u_long)vers, proto); if (sport == 0) { goto err; } raddr->sin_port = htons(sport); mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */ } /* Transform sockaddr_in to netbuf */ bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in); bindaddr.buf = raddr; bindresvport(fd, NULL); cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers, sendsz, recvsz); if (cl) { if (madefd == TRUE) { /* * The fd should be closed while destroying the handle. */ (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL); *sockp = fd; } (void) freenetconfigent(nconf); mutex_unlock(&rpcsoc_lock); return (cl); } goto err; syserror: rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; err: if (madefd == TRUE) (void)_close(fd); (void) freenetconfigent(nconf); mutex_unlock(&rpcsoc_lock); return (NULL); } CLIENT * clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz) struct sockaddr_in *raddr; u_long prog; u_long vers; struct timeval wait; int *sockp; u_int sendsz; u_int recvsz; { CLIENT *cl; cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, sendsz, recvsz, "udp"); if (cl == NULL) { return (NULL); } (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait); return (cl); } CLIENT * clntudp_create(raddr, program, version, wait, sockp) struct sockaddr_in *raddr; u_long program; u_long version; struct timeval wait; int *sockp; { return clntudp_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE); } CLIENT * clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) struct sockaddr_in *raddr; u_long prog; u_long vers; int *sockp; u_int sendsz; u_int recvsz; { return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp, sendsz, recvsz, "tcp"); } CLIENT * clntraw_create(prog, vers) u_long prog; u_long vers; { return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers); } /* * A common server create routine */ static SVCXPRT * svc_com_create(fd, sendsize, recvsize, netid) int fd; u_int sendsize; u_int recvsize; char *netid; { struct netconfig *nconf; SVCXPRT *svc; int madefd = FALSE; int port; struct sockaddr_in sin; if ((nconf = __rpc_getconfip(netid)) == NULL) { (void) syslog(LOG_ERR, "Could not get %s transport", netid); return (NULL); } if (fd == RPC_ANYSOCK) { fd = __rpc_nconf2fd(nconf); if (fd == -1) { (void) freenetconfigent(nconf); (void) syslog(LOG_ERR, "svc%s_create: could not open connection", netid); return (NULL); } madefd = TRUE; } memset(&sin, 0, sizeof sin); sin.sin_family = AF_INET; bindresvport(fd, &sin); _listen(fd, SOMAXCONN); svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize); (void) freenetconfigent(nconf); if (svc == NULL) { if (madefd) (void)_close(fd); return (NULL); } port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port); svc->xp_port = ntohs(port); return (svc); } SVCXPRT * svctcp_create(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { return svc_com_create(fd, sendsize, recvsize, "tcp"); } SVCXPRT * svcudp_bufcreate(fd, sendsz, recvsz) int fd; u_int sendsz, recvsz; { return svc_com_create(fd, sendsz, recvsz, "udp"); } SVCXPRT * svcfd_create(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { return svc_fd_create(fd, sendsize, recvsize); } SVCXPRT * svcudp_create(fd) int fd; { return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp"); } SVCXPRT * svcraw_create() { return svc_raw_create(); } int get_myaddress(addr) struct sockaddr_in *addr; { memset((void *) addr, 0, sizeof(*addr)); addr->sin_family = AF_INET; addr->sin_port = htons(PMAPPORT); addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK); return (0); } /* * For connectionless "udp" transport. Obsoleted by rpc_call(). */ int callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) const char *host; int prognum, versnum, procnum; xdrproc_t inproc, outproc; void *in, *out; { return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum, (rpcproc_t)procnum, inproc, in, outproc, out, "udp"); } /* * For connectionless kind of transport. Obsoleted by rpc_reg() */ int registerrpc(prognum, versnum, procnum, progname, inproc, outproc) int prognum, versnum, procnum; char *(*progname)(char [UDPMSGSIZE]); xdrproc_t inproc, outproc; { return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum, (rpcproc_t)procnum, progname, inproc, outproc, "udp"); } /* * All the following clnt_broadcast stuff is convulated; it supports * the earlier calling style of the callback function */ static thread_key_t clnt_broadcast_key; static resultproc_t clnt_broadcast_result_main; static once_t clnt_broadcast_once = ONCE_INITIALIZER; static void clnt_broadcast_key_init(void) { thr_keycreate(&clnt_broadcast_key, free); } /* * Need to translate the netbuf address into sockaddr_in address. * Dont care about netid here. */ /* ARGSUSED */ static bool_t rpc_wrap_bcast(resultp, addr, nconf) char *resultp; /* results of the call */ struct netbuf *addr; /* address of the guy who responded */ struct netconfig *nconf; /* Netconf of the transport */ { resultproc_t clnt_broadcast_result; if (strcmp(nconf->nc_netid, "udp")) return (FALSE); if (thr_main()) clnt_broadcast_result = clnt_broadcast_result_main; else clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key); return (*clnt_broadcast_result)(resultp, (struct sockaddr_in *)addr->buf); } /* * Broadcasts on UDP transport. Obsoleted by rpc_broadcast(). */ enum clnt_stat clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) u_long prog; /* program number */ u_long vers; /* version number */ u_long proc; /* procedure number */ xdrproc_t xargs; /* xdr routine for args */ void *argsp; /* pointer to args */ xdrproc_t xresults; /* xdr routine for results */ void *resultsp; /* pointer to results */ resultproc_t eachresult; /* call with each result obtained */ { if (thr_main()) clnt_broadcast_result_main = eachresult; else { thr_once(&clnt_broadcast_once, clnt_broadcast_key_init); thr_setspecific(clnt_broadcast_key, (void *) eachresult); } return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers, (rpcproc_t)proc, xargs, argsp, xresults, resultsp, (resultproc_t) rpc_wrap_bcast, "udp"); } /* * Create the client des authentication object. Obsoleted by * authdes_seccreate(). */ AUTH * authdes_create(servername, window, syncaddr, ckey) char *servername; /* network name of server */ u_int window; /* time to live */ struct sockaddr *syncaddr; /* optional hostaddr to sync with */ des_block *ckey; /* optional conversation key to use */ { AUTH *dummy; AUTH *nauth; char hostname[NI_MAXHOST]; if (syncaddr) { /* * Change addr to hostname, because that is the way * new interface takes it. */ if (getnameinfo(syncaddr, syncaddr->sa_len, hostname, sizeof hostname, NULL, 0, 0) != 0) goto fallback; nauth = authdes_seccreate(servername, window, hostname, ckey); return (nauth); } fallback: dummy = authdes_seccreate(servername, window, NULL, ckey); return (dummy); } /* * Create a client handle for a unix connection. Obsoleted by clnt_vc_create() */ CLIENT * clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz) struct sockaddr_un *raddr; u_long prog; u_long vers; int *sockp; u_int sendsz; u_int recvsz; { struct netbuf *svcaddr; struct netconfig *nconf; CLIENT *cl; int len; cl = NULL; nconf = NULL; svcaddr = NULL; if ((raddr->sun_len == 0) || ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) || ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) { - if (svcaddr != NULL) - free(svcaddr); + free(svcaddr); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; return(cl); } if (*sockp < 0) { *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0); len = raddr->sun_len = SUN_LEN(raddr); if ((*sockp < 0) || (_connect(*sockp, (struct sockaddr *)raddr, len) < 0)) { rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; if (*sockp != -1) (void)_close(*sockp); goto done; } } svcaddr->buf = raddr; svcaddr->len = raddr->sun_len; svcaddr->maxlen = sizeof (struct sockaddr_un); cl = clnt_vc_create(*sockp, svcaddr, prog, vers, sendsz, recvsz); done: free(svcaddr->buf); free(svcaddr); return(cl); } /* * Creates, registers, and returns a (rpc) unix based transporter. * Obsoleted by svc_vc_create(). */ SVCXPRT * svcunix_create(sock, sendsize, recvsize, path) int sock; u_int sendsize; u_int recvsize; char *path; { struct netconfig *nconf; void *localhandle; struct sockaddr_un sun; struct sockaddr *sa; struct t_bind taddr; SVCXPRT *xprt; int addrlen; xprt = (SVCXPRT *)NULL; localhandle = setnetconfig(); while ((nconf = getnetconfig(localhandle)) != NULL) { if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) break; } if (nconf == NULL) return(xprt); if ((sock = __rpc_nconf2fd(nconf)) < 0) goto done; memset(&sun, 0, sizeof sun); sun.sun_family = AF_LOCAL; if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) goto done; sun.sun_len = SUN_LEN(&sun); addrlen = sizeof (struct sockaddr_un); sa = (struct sockaddr *)&sun; if (_bind(sock, sa, addrlen) < 0) goto done; taddr.addr.len = taddr.addr.maxlen = addrlen; taddr.addr.buf = malloc(addrlen); if (taddr.addr.buf == NULL) goto done; memcpy(taddr.addr.buf, sa, addrlen); if (nconf->nc_semantics != NC_TPI_CLTS) { if (_listen(sock, SOMAXCONN) < 0) { free(taddr.addr.buf); goto done; } } xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize); done: endnetconfig(localhandle); return(xprt); } /* * Like svunix_create(), except the routine takes any *open* UNIX file * descriptor as its first input. Obsoleted by svc_fd_create(); */ SVCXPRT * svcunixfd_create(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { return (svc_fd_create(fd, sendsize, recvsize)); } #endif /* PORTMAP */ Index: stable/10/lib/libc/rpc/rpcb_clnt.c =================================================================== --- stable/10/lib/libc/rpc/rpcb_clnt.c (revision 290898) +++ stable/10/lib/libc/rpc/rpcb_clnt.c (revision 290899) @@ -1,1340 +1,1332 @@ /* $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $ */ /*- * Copyright (c) 2010, Oracle America, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the "Oracle America, Inc." nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro"; #endif #include __FBSDID("$FreeBSD$"); /* * rpcb_clnt.c * interface to rpcbind rpc service. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include #ifdef PORTMAP #include /* FOR IPPROTO_TCP/UDP definitions */ #include #endif /* PORTMAP */ #include #include #include #include #include #include #include #include "un-namespace.h" #include "rpc_com.h" #include "mt_misc.h" static struct timeval tottimeout = { 60, 0 }; static const struct timeval rmttimeout = { 3, 0 }; static struct timeval rpcbrmttime = { 15, 0 }; extern bool_t xdr_wrapstring(XDR *, char **); static const char nullstring[] = "\000"; #define CACHESIZE 6 struct address_cache { char *ac_host; char *ac_netid; char *ac_uaddr; struct netbuf *ac_taddr; struct address_cache *ac_next; }; static struct address_cache *front; static int cachesize; #define CLCR_GET_RPCB_TIMEOUT 1 #define CLCR_SET_RPCB_TIMEOUT 2 extern int __rpc_lowvers; static struct address_cache *check_cache(const char *, const char *); static void delete_cache(struct netbuf *); static void add_cache(const char *, const char *, struct netbuf *, char *); static CLIENT *getclnthandle(const char *, const struct netconfig *, char **); static CLIENT *local_rpcb(void); static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *); /* * This routine adjusts the timeout used for calls to the remote rpcbind. * Also, this routine can be used to set the use of portmapper version 2 * only when doing rpc_broadcasts * These are private routines that may not be provided in future releases. */ bool_t __rpc_control(request, info) int request; void *info; { switch (request) { case CLCR_GET_RPCB_TIMEOUT: *(struct timeval *)info = tottimeout; break; case CLCR_SET_RPCB_TIMEOUT: tottimeout = *(struct timeval *)info; break; case CLCR_SET_LOWVERS: __rpc_lowvers = *(int *)info; break; case CLCR_GET_LOWVERS: *(int *)info = __rpc_lowvers; break; default: return (FALSE); } return (TRUE); } /* * It might seem that a reader/writer lock would be more reasonable here. * However because getclnthandle(), the only user of the cache functions, * may do a delete_cache() operation if a check_cache() fails to return an * address useful to clnt_tli_create(), we may as well use a mutex. */ /* * As it turns out, if the cache lock is *not* a reader/writer lock, we will * block all clnt_create's if we are trying to connect to a host that's down, * since the lock will be held all during that time. */ /* * The routines check_cache(), add_cache(), delete_cache() manage the * cache of rpcbind addresses for (host, netid). */ static struct address_cache * check_cache(host, netid) const char *host, *netid; { struct address_cache *cptr; /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */ for (cptr = front; cptr != NULL; cptr = cptr->ac_next) { if (!strcmp(cptr->ac_host, host) && !strcmp(cptr->ac_netid, netid)) { #ifdef ND_DEBUG fprintf(stderr, "Found cache entry for %s: %s\n", host, netid); #endif return (cptr); } } return ((struct address_cache *) NULL); } static void delete_cache(addr) struct netbuf *addr; { struct address_cache *cptr, *prevptr = NULL; /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */ for (cptr = front; cptr != NULL; cptr = cptr->ac_next) { if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) { free(cptr->ac_host); free(cptr->ac_netid); free(cptr->ac_taddr->buf); free(cptr->ac_taddr); - if (cptr->ac_uaddr) - free(cptr->ac_uaddr); + free(cptr->ac_uaddr); if (prevptr) prevptr->ac_next = cptr->ac_next; else front = cptr->ac_next; free(cptr); cachesize--; break; } prevptr = cptr; } } static void add_cache(host, netid, taddr, uaddr) const char *host, *netid; char *uaddr; struct netbuf *taddr; { struct address_cache *ad_cache, *cptr, *prevptr; ad_cache = (struct address_cache *) malloc(sizeof (struct address_cache)); if (!ad_cache) { return; } ad_cache->ac_host = strdup(host); ad_cache->ac_netid = strdup(netid); ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL; ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf)); if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr || (uaddr && !ad_cache->ac_uaddr)) { goto out; } ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len; ad_cache->ac_taddr->buf = (char *) malloc(taddr->len); if (ad_cache->ac_taddr->buf == NULL) { out: - if (ad_cache->ac_host) - free(ad_cache->ac_host); - if (ad_cache->ac_netid) - free(ad_cache->ac_netid); - if (ad_cache->ac_uaddr) - free(ad_cache->ac_uaddr); - if (ad_cache->ac_taddr) - free(ad_cache->ac_taddr); + free(ad_cache->ac_host); + free(ad_cache->ac_netid); + free(ad_cache->ac_uaddr); + free(ad_cache->ac_taddr); free(ad_cache); return; } memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len); #ifdef ND_DEBUG fprintf(stderr, "Added to cache: %s : %s\n", host, netid); #endif /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */ rwlock_wrlock(&rpcbaddr_cache_lock); if (cachesize < CACHESIZE) { ad_cache->ac_next = front; front = ad_cache; cachesize++; } else { /* Free the last entry */ cptr = front; prevptr = NULL; while (cptr->ac_next) { prevptr = cptr; cptr = cptr->ac_next; } #ifdef ND_DEBUG fprintf(stderr, "Deleted from cache: %s : %s\n", cptr->ac_host, cptr->ac_netid); #endif free(cptr->ac_host); free(cptr->ac_netid); free(cptr->ac_taddr->buf); free(cptr->ac_taddr); - if (cptr->ac_uaddr) - free(cptr->ac_uaddr); + free(cptr->ac_uaddr); if (prevptr) { prevptr->ac_next = NULL; ad_cache->ac_next = front; front = ad_cache; } else { front = ad_cache; ad_cache->ac_next = NULL; } free(cptr); } rwlock_unlock(&rpcbaddr_cache_lock); } /* * This routine will return a client handle that is connected to the * rpcbind. If targaddr is non-NULL, the "universal address" of the * host will be stored in *targaddr; the caller is responsible for * freeing this string. * On error, returns NULL and free's everything. */ static CLIENT * getclnthandle(host, nconf, targaddr) const char *host; const struct netconfig *nconf; char **targaddr; { CLIENT *client; struct netbuf *addr, taddr; struct netbuf addr_to_delete; struct __rpc_sockinfo si; struct addrinfo hints, *res, *tres; struct address_cache *ad_cache; char *tmpaddr; /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */ /* Get the address of the rpcbind. Check cache first */ client = NULL; addr_to_delete.len = 0; rwlock_rdlock(&rpcbaddr_cache_lock); ad_cache = NULL; if (host != NULL) ad_cache = check_cache(host, nconf->nc_netid); if (ad_cache != NULL) { addr = ad_cache->ac_taddr; client = clnt_tli_create(RPC_ANYFD, nconf, addr, (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0); if (client != NULL) { if (targaddr) *targaddr = strdup(ad_cache->ac_uaddr); rwlock_unlock(&rpcbaddr_cache_lock); return (client); } addr_to_delete.len = addr->len; addr_to_delete.buf = (char *)malloc(addr->len); if (addr_to_delete.buf == NULL) { addr_to_delete.len = 0; } else { memcpy(addr_to_delete.buf, addr->buf, addr->len); } } rwlock_unlock(&rpcbaddr_cache_lock); if (addr_to_delete.len != 0) { /* * Assume this may be due to cache data being * outdated */ rwlock_wrlock(&rpcbaddr_cache_lock); delete_cache(&addr_to_delete); rwlock_unlock(&rpcbaddr_cache_lock); free(addr_to_delete.buf); } if (!__rpc_nconf2sockinfo(nconf, &si)) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return NULL; } memset(&hints, 0, sizeof hints); hints.ai_family = si.si_af; hints.ai_socktype = si.si_socktype; hints.ai_protocol = si.si_proto; #ifdef CLNT_DEBUG printf("trying netid %s family %d proto %d socktype %d\n", nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype); #endif if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) { client = local_rpcb(); if (! client) { #ifdef ND_DEBUG clnt_pcreateerror("rpcbind clnt interface"); #endif return (NULL); } else { struct sockaddr_un sun; if (targaddr) { *targaddr = malloc(sizeof(sun.sun_path)); if (*targaddr == NULL) { CLNT_DESTROY(client); return (NULL); } strncpy(*targaddr, _PATH_RPCBINDSOCK, sizeof(sun.sun_path)); } return (client); } } else { if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) { rpc_createerr.cf_stat = RPC_UNKNOWNHOST; return NULL; } } for (tres = res; tres != NULL; tres = tres->ai_next) { taddr.buf = tres->ai_addr; taddr.len = taddr.maxlen = tres->ai_addrlen; #ifdef ND_DEBUG { char *ua; ua = taddr2uaddr(nconf, &taddr); fprintf(stderr, "Got it [%s]\n", ua); free(ua); } #endif #ifdef ND_DEBUG { int i; fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n", taddr.len, taddr.maxlen); fprintf(stderr, "\tAddress is "); for (i = 0; i < taddr.len; i++) fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]); fprintf(stderr, "\n"); } #endif client = clnt_tli_create(RPC_ANYFD, nconf, &taddr, (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0); #ifdef ND_DEBUG if (! client) { clnt_pcreateerror("rpcbind clnt interface"); } #endif if (client) { tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL; add_cache(host, nconf->nc_netid, &taddr, tmpaddr); if (targaddr) *targaddr = tmpaddr; break; } } if (res) freeaddrinfo(res); return (client); } /* XXX */ #define IN4_LOCALHOST_STRING "127.0.0.1" #define IN6_LOCALHOST_STRING "::1" /* * This routine will return a client handle that is connected to the local * rpcbind. Returns NULL on error and free's everything. */ static CLIENT * local_rpcb() { CLIENT *client; static struct netconfig *loopnconf; static char *hostname; int sock; size_t tsize; struct netbuf nbuf; struct sockaddr_un sun; /* * Try connecting to the local rpcbind through a local socket * first. If this doesn't work, try all transports defined in * the netconfig file. */ memset(&sun, 0, sizeof sun); sock = _socket(AF_LOCAL, SOCK_STREAM, 0); if (sock < 0) goto try_nconf; sun.sun_family = AF_LOCAL; strcpy(sun.sun_path, _PATH_RPCBINDSOCK); nbuf.len = sun.sun_len = SUN_LEN(&sun); nbuf.maxlen = sizeof (struct sockaddr_un); nbuf.buf = &sun; tsize = __rpc_get_t_size(AF_LOCAL, 0, 0); client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS, tsize, tsize); if (client != NULL) { /* Mark the socket to be closed in destructor */ (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL); return client; } /* Nobody needs this socket anymore; free the descriptor. */ _close(sock); try_nconf: /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */ mutex_lock(&loopnconf_lock); if (loopnconf == NULL) { struct netconfig *nconf, *tmpnconf = NULL; void *nc_handle; int fd; nc_handle = setnetconfig(); if (nc_handle == NULL) { /* fails to open netconfig file */ syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; mutex_unlock(&loopnconf_lock); return (NULL); } while ((nconf = getnetconfig(nc_handle)) != NULL) { #ifdef INET6 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 || #else if (( #endif strcmp(nconf->nc_protofmly, NC_INET) == 0) && (nconf->nc_semantics == NC_TPI_COTS || nconf->nc_semantics == NC_TPI_COTS_ORD)) { fd = __rpc_nconf2fd(nconf); /* * Can't create a socket, assume that * this family isn't configured in the kernel. */ if (fd < 0) continue; _close(fd); tmpnconf = nconf; if (!strcmp(nconf->nc_protofmly, NC_INET)) hostname = IN4_LOCALHOST_STRING; else hostname = IN6_LOCALHOST_STRING; } } if (tmpnconf == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; mutex_unlock(&loopnconf_lock); return (NULL); } loopnconf = getnetconfigent(tmpnconf->nc_netid); /* loopnconf is never freed */ endnetconfig(nc_handle); } mutex_unlock(&loopnconf_lock); client = getclnthandle(hostname, loopnconf, NULL); return (client); } /* * Set a mapping between program, version and address. * Calls the rpcbind service to do the mapping. */ bool_t rpcb_set(program, version, nconf, address) rpcprog_t program; rpcvers_t version; const struct netconfig *nconf; /* Network structure of transport */ const struct netbuf *address; /* Services netconfig address */ { CLIENT *client; bool_t rslt = FALSE; RPCB parms; char uidbuf[32]; /* parameter checking */ if (nconf == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return (FALSE); } if (address == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNADDR; return (FALSE); } client = local_rpcb(); if (! client) { return (FALSE); } /* convert to universal */ /*LINTED const castaway*/ parms.r_addr = taddr2uaddr((struct netconfig *) nconf, (struct netbuf *)address); if (!parms.r_addr) { CLNT_DESTROY(client); rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE; return (FALSE); /* no universal address */ } parms.r_prog = program; parms.r_vers = version; parms.r_netid = nconf->nc_netid; /* * Though uid is not being used directly, we still send it for * completeness. For non-unix platforms, perhaps some other * string or an empty string can be sent. */ (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid()); parms.r_owner = uidbuf; CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, (xdrproc_t) xdr_bool, (char *)(void *)&rslt, tottimeout); CLNT_DESTROY(client); free(parms.r_addr); return (rslt); } /* * Remove the mapping between program, version and netbuf address. * Calls the rpcbind service to do the un-mapping. * If netbuf is NULL, unset for all the transports, otherwise unset * only for the given transport. */ bool_t rpcb_unset(program, version, nconf) rpcprog_t program; rpcvers_t version; const struct netconfig *nconf; { CLIENT *client; bool_t rslt = FALSE; RPCB parms; char uidbuf[32]; client = local_rpcb(); if (! client) { return (FALSE); } parms.r_prog = program; parms.r_vers = version; if (nconf) parms.r_netid = nconf->nc_netid; else { /*LINTED const castaway*/ parms.r_netid = (char *) &nullstring[0]; /* unsets all */ } /*LINTED const castaway*/ parms.r_addr = (char *) &nullstring[0]; (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid()); parms.r_owner = uidbuf; CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, (xdrproc_t) xdr_bool, (char *)(void *)&rslt, tottimeout); CLNT_DESTROY(client); return (rslt); } /* * From the merged list, find the appropriate entry */ static struct netbuf * got_entry(relp, nconf) rpcb_entry_list_ptr relp; const struct netconfig *nconf; { struct netbuf *na = NULL; rpcb_entry_list_ptr sp; rpcb_entry *rmap; for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) { rmap = &sp->rpcb_entry_map; if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) && (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) && (nconf->nc_semantics == rmap->r_nc_semantics) && (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) { na = uaddr2taddr(nconf, rmap->r_maddr); #ifdef ND_DEBUG fprintf(stderr, "\tRemote address is [%s].\n", rmap->r_maddr); if (!na) fprintf(stderr, "\tCouldn't resolve remote address!\n"); #endif break; } } return (na); } /* * Quick check to see if rpcbind is up. Tries to connect over * local transport. */ static bool_t __rpcbind_is_up() { struct netconfig *nconf; struct sockaddr_un sun; void *localhandle; int sock; nconf = NULL; localhandle = setnetconfig(); while ((nconf = getnetconfig(localhandle)) != NULL) { if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) break; } if (nconf == NULL) return (FALSE); endnetconfig(localhandle); memset(&sun, 0, sizeof sun); sock = _socket(AF_LOCAL, SOCK_STREAM, 0); if (sock < 0) return (FALSE); sun.sun_family = AF_LOCAL; strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path)); sun.sun_len = SUN_LEN(&sun); if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) { _close(sock); return (FALSE); } _close(sock); return (TRUE); } /* * An internal function which optimizes rpcb_getaddr function. It also * returns the client handle that it uses to contact the remote rpcbind. * * The algorithm used: If the transports is TCP or UDP, it first tries * version 2 (portmap), 4 and then 3 (svr4). This order should be * changed in the next OS release to 4, 2 and 3. We are assuming that by * that time, version 4 would be available on many machines on the network. * With this algorithm, we get performance as well as a plan for * obsoleting version 2. * * For all other transports, the algorithm remains as 4 and then 3. * * XXX: Due to some problems with t_connect(), we do not reuse the same client * handle for COTS cases and hence in these cases we do not return the * client handle. This code will change if t_connect() ever * starts working properly. Also look under clnt_vc.c. */ struct netbuf * __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp) rpcprog_t program; rpcvers_t version; const struct netconfig *nconf; const char *host; CLIENT **clpp; struct timeval *tp; { static bool_t check_rpcbind = TRUE; CLIENT *client = NULL; RPCB parms; enum clnt_stat clnt_st; char *ua = NULL; rpcvers_t vers; struct netbuf *address = NULL; rpcvers_t start_vers = RPCBVERS4; struct netbuf servaddr; /* parameter checking */ if (nconf == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return (NULL); } parms.r_addr = NULL; /* * Use default total timeout if no timeout is specified. */ if (tp == NULL) tp = &tottimeout; #ifdef PORTMAP /* Try version 2 for TCP or UDP */ if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { u_short port = 0; struct netbuf remote; rpcvers_t pmapvers = 2; struct pmap pmapparms; /* * Try UDP only - there are some portmappers out * there that use UDP only. */ if (strcmp(nconf->nc_proto, NC_TCP) == 0) { struct netconfig *newnconf; if ((newnconf = getnetconfigent("udp")) == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return (NULL); } client = getclnthandle(host, newnconf, &parms.r_addr); freenetconfigent(newnconf); } else { client = getclnthandle(host, nconf, &parms.r_addr); } if (client == NULL) return (NULL); /* * Set version and retry timeout. */ CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime); CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers); pmapparms.pm_prog = program; pmapparms.pm_vers = version; pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ? IPPROTO_UDP : IPPROTO_TCP; pmapparms.pm_port = 0; /* not needed */ clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT, (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms, (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port, *tp); if (clnt_st != RPC_SUCCESS) { if ((clnt_st == RPC_PROGVERSMISMATCH) || (clnt_st == RPC_PROGUNAVAIL)) goto try_rpcbind; /* Try different versions */ rpc_createerr.cf_stat = RPC_PMAPFAILURE; clnt_geterr(client, &rpc_createerr.cf_error); goto error; } else if (port == 0) { address = NULL; rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; goto error; } port = htons(port); CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote); if (((address = (struct netbuf *) malloc(sizeof (struct netbuf))) == NULL) || ((address->buf = (char *) malloc(remote.len)) == NULL)) { rpc_createerr.cf_stat = RPC_SYSTEMERROR; clnt_geterr(client, &rpc_createerr.cf_error); - if (address) { - free(address); - address = NULL; - } + free(address); + address = NULL; goto error; } memcpy(address->buf, remote.buf, remote.len); memcpy(&((char *)address->buf)[sizeof (short)], (char *)(void *)&port, sizeof (short)); address->len = address->maxlen = remote.len; goto done; } #endif /* PORTMAP */ try_rpcbind: /* * Check if rpcbind is up. This prevents needless delays when * accessing applications such as the keyserver while booting * disklessly. */ if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) { if (!__rpcbind_is_up()) { rpc_createerr.cf_stat = RPC_PMAPFAILURE; rpc_createerr.cf_error.re_errno = 0; goto error; } check_rpcbind = FALSE; } /* * Now we try version 4 and then 3. * We also send the remote system the address we used to * contact it in case it can help to connect back with us */ parms.r_prog = program; parms.r_vers = version; /*LINTED const castaway*/ parms.r_owner = (char *) &nullstring[0]; /* not needed; */ /* just for xdring */ parms.r_netid = nconf->nc_netid; /* not really needed */ /* * If a COTS transport is being used, try getting address via CLTS * transport. This works only with version 4. */ if (nconf->nc_semantics == NC_TPI_COTS_ORD || nconf->nc_semantics == NC_TPI_COTS) { void *handle; struct netconfig *nconf_clts; rpcb_entry_list_ptr relp = NULL; if (client == NULL) { /* This did not go through the above PORTMAP/TCP code */ if ((handle = __rpc_setconf("datagram_v")) != NULL) { while ((nconf_clts = __rpc_getconf(handle)) != NULL) { if (strcmp(nconf_clts->nc_protofmly, nconf->nc_protofmly) != 0) { continue; } client = getclnthandle(host, nconf_clts, &parms.r_addr); break; } __rpc_endconf(handle); } if (client == NULL) goto regular_rpcbind; /* Go the regular way */ } else { /* This is a UDP PORTMAP handle. Change to version 4 */ vers = RPCBVERS4; CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); } /* * We also send the remote system the address we used to * contact it in case it can help it connect back with us */ if (parms.r_addr == NULL) { /*LINTED const castaway*/ parms.r_addr = (char *) &nullstring[0]; /* for XDRing */ } CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime); clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST, (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, (xdrproc_t) xdr_rpcb_entry_list_ptr, (char *)(void *)&relp, *tp); if (clnt_st == RPC_SUCCESS) { if ((address = got_entry(relp, nconf)) != NULL) { xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr, (char *)(void *)&relp); CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&servaddr); __rpc_fixup_addr(address, &servaddr); goto done; } /* Entry not found for this transport */ xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr, (char *)(void *)&relp); /* * XXX: should have perhaps returned with error but * since the remote machine might not always be able * to send the address on all transports, we try the * regular way with regular_rpcbind */ goto regular_rpcbind; } else if ((clnt_st == RPC_PROGVERSMISMATCH) || (clnt_st == RPC_PROGUNAVAIL)) { start_vers = RPCBVERS; /* Try version 3 now */ goto regular_rpcbind; /* Try different versions */ } else { rpc_createerr.cf_stat = RPC_PMAPFAILURE; clnt_geterr(client, &rpc_createerr.cf_error); goto error; } } regular_rpcbind: /* Now the same transport is to be used to get the address */ if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) || (nconf->nc_semantics == NC_TPI_COTS))) { /* A CLTS type of client - destroy it */ CLNT_DESTROY(client); client = NULL; } if (client == NULL) { client = getclnthandle(host, nconf, &parms.r_addr); if (client == NULL) { goto error; } } if (parms.r_addr == NULL) { /*LINTED const castaway*/ parms.r_addr = (char *) &nullstring[0]; } /* First try from start_vers and then version 3 (RPCBVERS) */ CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime); for (vers = start_vers; vers >= RPCBVERS; vers--) { /* Set the version */ CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR, (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp); if (clnt_st == RPC_SUCCESS) { if ((ua == NULL) || (ua[0] == 0)) { /* address unknown */ rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; goto error; } address = uaddr2taddr(nconf, ua); #ifdef ND_DEBUG fprintf(stderr, "\tRemote address is [%s]\n", ua); if (!address) fprintf(stderr, "\tCouldn't resolve remote address!\n"); #endif xdr_free((xdrproc_t)xdr_wrapstring, (char *)(void *)&ua); if (! address) { /* We don't know about your universal address */ rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE; goto error; } CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&servaddr); __rpc_fixup_addr(address, &servaddr); goto done; } else if (clnt_st == RPC_PROGVERSMISMATCH) { struct rpc_err rpcerr; clnt_geterr(client, &rpcerr); if (rpcerr.re_vers.low > RPCBVERS4) goto error; /* a new version, can't handle */ } else if (clnt_st != RPC_PROGUNAVAIL) { /* Cant handle this error */ rpc_createerr.cf_stat = clnt_st; clnt_geterr(client, &rpc_createerr.cf_error); goto error; } } error: if (client) { CLNT_DESTROY(client); client = NULL; } done: if (nconf->nc_semantics != NC_TPI_CLTS) { /* This client is the connectionless one */ if (client) { CLNT_DESTROY(client); client = NULL; } } if (clpp) { *clpp = client; } else if (client) { CLNT_DESTROY(client); } if (parms.r_addr != NULL && parms.r_addr != nullstring) free(parms.r_addr); return (address); } /* * Find the mapped address for program, version. * Calls the rpcbind service remotely to do the lookup. * Uses the transport specified in nconf. * Returns FALSE (0) if no map exists, else returns 1. * * Assuming that the address is all properly allocated */ int rpcb_getaddr(program, version, nconf, address, host) rpcprog_t program; rpcvers_t version; const struct netconfig *nconf; struct netbuf *address; const char *host; { struct netbuf *na; if ((na = __rpcb_findaddr_timed(program, version, (struct netconfig *) nconf, (char *) host, (CLIENT **) NULL, (struct timeval *) NULL)) == NULL) return (FALSE); if (na->len > address->maxlen) { /* Too long address */ free(na->buf); free(na); rpc_createerr.cf_stat = RPC_FAILED; return (FALSE); } memcpy(address->buf, na->buf, (size_t)na->len); address->len = na->len; free(na->buf); free(na); return (TRUE); } /* * Get a copy of the current maps. * Calls the rpcbind service remotely to get the maps. * * It returns only a list of the services * It returns NULL on failure. */ rpcblist * rpcb_getmaps(nconf, host) const struct netconfig *nconf; const char *host; { rpcblist_ptr head = NULL; CLIENT *client; enum clnt_stat clnt_st; rpcvers_t vers = 0; client = getclnthandle(host, nconf, NULL); if (client == NULL) { return (head); } clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr, (char *)(void *)&head, tottimeout); if (clnt_st == RPC_SUCCESS) goto done; if ((clnt_st != RPC_PROGVERSMISMATCH) && (clnt_st != RPC_PROGUNAVAIL)) { rpc_createerr.cf_stat = RPC_RPCBFAILURE; clnt_geterr(client, &rpc_createerr.cf_error); goto done; } /* fall back to earlier version */ CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers); if (vers == RPCBVERS4) { vers = RPCBVERS; CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr, (char *)(void *)&head, tottimeout) == RPC_SUCCESS) goto done; } rpc_createerr.cf_stat = RPC_RPCBFAILURE; clnt_geterr(client, &rpc_createerr.cf_error); done: CLNT_DESTROY(client); return (head); } /* * rpcbinder remote-call-service interface. * This routine is used to call the rpcbind remote call service * which will look up a service program in the address maps, and then * remotely call that routine with the given parameters. This allows * programs to do a lookup and call in one step. */ enum clnt_stat rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, addr_ptr) const struct netconfig *nconf; /* Netconfig structure */ const char *host; /* Remote host name */ rpcprog_t prog; rpcvers_t vers; rpcproc_t proc; /* Remote proc identifiers */ xdrproc_t xdrargs, xdrres; /* XDR routines */ caddr_t argsp, resp; /* Argument and Result */ struct timeval tout; /* Timeout value for this call */ const struct netbuf *addr_ptr; /* Preallocated netbuf address */ { CLIENT *client; enum clnt_stat stat; struct r_rpcb_rmtcallargs a; struct r_rpcb_rmtcallres r; rpcvers_t rpcb_vers; stat = 0; client = getclnthandle(host, nconf, NULL); if (client == NULL) { return (RPC_FAILED); } /*LINTED const castaway*/ CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout); a.prog = prog; a.vers = vers; a.proc = proc; a.args.args_val = argsp; a.xdr_args = xdrargs; r.addr = NULL; r.results.results_val = resp; r.xdr_res = xdrres; for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) { CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers); stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT, (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a, (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout); if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) { struct netbuf *na; /*LINTED const castaway*/ na = uaddr2taddr((struct netconfig *) nconf, r.addr); if (!na) { stat = RPC_N2AXLATEFAILURE; /*LINTED const castaway*/ ((struct netbuf *) addr_ptr)->len = 0; goto error; } if (na->len > addr_ptr->maxlen) { /* Too long address */ stat = RPC_FAILED; /* XXX A better error no */ free(na->buf); free(na); /*LINTED const castaway*/ ((struct netbuf *) addr_ptr)->len = 0; goto error; } memcpy(addr_ptr->buf, na->buf, (size_t)na->len); /*LINTED const castaway*/ ((struct netbuf *)addr_ptr)->len = na->len; free(na->buf); free(na); break; } else if ((stat != RPC_PROGVERSMISMATCH) && (stat != RPC_PROGUNAVAIL)) { goto error; } } error: CLNT_DESTROY(client); if (r.addr) xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr); return (stat); } /* * Gets the time on the remote host. * Returns 1 if succeeds else 0. */ bool_t rpcb_gettime(host, timep) const char *host; time_t *timep; { CLIENT *client = NULL; void *handle; struct netconfig *nconf; rpcvers_t vers; enum clnt_stat st; if ((host == NULL) || (host[0] == 0)) { time(timep); return (TRUE); } if ((handle = __rpc_setconf("netpath")) == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return (FALSE); } rpc_createerr.cf_stat = RPC_SUCCESS; while (client == NULL) { if ((nconf = __rpc_getconf(handle)) == NULL) { if (rpc_createerr.cf_stat == RPC_SUCCESS) rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; break; } client = getclnthandle(host, nconf, NULL); if (client) break; } __rpc_endconf(handle); if (client == (CLIENT *) NULL) { return (FALSE); } st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout); if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) { CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers); if (vers == RPCBVERS4) { /* fall back to earlier version */ vers = RPCBVERS; CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout); } } CLNT_DESTROY(client); return (st == RPC_SUCCESS? TRUE: FALSE); } /* * Converts taddr to universal address. This routine should never * really be called because local n2a libraries are always provided. */ char * rpcb_taddr2uaddr(nconf, taddr) struct netconfig *nconf; struct netbuf *taddr; { CLIENT *client; char *uaddr = NULL; /* parameter checking */ if (nconf == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return (NULL); } if (taddr == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNADDR; return (NULL); } client = local_rpcb(); if (! client) { return (NULL); } CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR, (xdrproc_t) xdr_netbuf, (char *)(void *)taddr, (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout); CLNT_DESTROY(client); return (uaddr); } /* * Converts universal address to netbuf. This routine should never * really be called because local n2a libraries are always provided. */ struct netbuf * rpcb_uaddr2taddr(nconf, uaddr) struct netconfig *nconf; char *uaddr; { CLIENT *client; struct netbuf *taddr; /* parameter checking */ if (nconf == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; return (NULL); } if (uaddr == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNADDR; return (NULL); } client = local_rpcb(); if (! client) { return (NULL); } taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf)); if (taddr == NULL) { CLNT_DESTROY(client); return (NULL); } if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR, (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, (xdrproc_t) xdr_netbuf, (char *)(void *)taddr, tottimeout) != RPC_SUCCESS) { free(taddr); taddr = NULL; } CLNT_DESTROY(client); return (taddr); } Index: stable/10/lib/libc/rpc/svc.c =================================================================== --- stable/10/lib/libc/rpc/svc.c (revision 290898) +++ stable/10/lib/libc/rpc/svc.c (revision 290899) @@ -1,796 +1,794 @@ /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro"; static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC"; #endif #include __FBSDID("$FreeBSD$"); /* * svc.c, Server-side remote procedure call interface. * * There are two sets of procedures here. The xprt routines are * for handling transport handles. The svc routines handle the * list of service routines. * * Copyright (C) 1984, Sun Microsystems, Inc. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #ifdef PORTMAP #include #endif /* PORTMAP */ #include "un-namespace.h" #include "rpc_com.h" #include "mt_misc.h" #define RQCRED_SIZE 400 /* this size is excessive */ #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */ #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET) #define max(a, b) (a > b ? a : b) /* * The services list * Each entry represents a set of procedures (an rpc program). * The dispatch routine takes request structs and runs the * apropriate procedure. */ static struct svc_callout { struct svc_callout *sc_next; rpcprog_t sc_prog; rpcvers_t sc_vers; char *sc_netid; void (*sc_dispatch)(struct svc_req *, SVCXPRT *); } *svc_head; static struct svc_callout *svc_find(rpcprog_t, rpcvers_t, struct svc_callout **, char *); static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock); /* *************** SVCXPRT related stuff **************** */ /* * Activate a transport handle. */ void xprt_register(xprt) SVCXPRT *xprt; { int sock; assert(xprt != NULL); sock = xprt->xp_fd; rwlock_wrlock(&svc_fd_lock); if (__svc_xports == NULL) { __svc_xports = (SVCXPRT **) mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *)); if (__svc_xports == NULL) { rwlock_unlock(&svc_fd_lock); return; } memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *)); } if (sock < FD_SETSIZE) { __svc_xports[sock] = xprt; FD_SET(sock, &svc_fdset); svc_maxfd = max(svc_maxfd, sock); } rwlock_unlock(&svc_fd_lock); } void xprt_unregister(SVCXPRT *xprt) { __xprt_do_unregister(xprt, TRUE); } void __xprt_unregister_unlocked(SVCXPRT *xprt) { __xprt_do_unregister(xprt, FALSE); } /* * De-activate a transport handle. */ static void __xprt_do_unregister(xprt, dolock) SVCXPRT *xprt; bool_t dolock; { int sock; assert(xprt != NULL); sock = xprt->xp_fd; if (dolock) rwlock_wrlock(&svc_fd_lock); if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) { __svc_xports[sock] = NULL; FD_CLR(sock, &svc_fdset); if (sock >= svc_maxfd) { for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--) if (__svc_xports[svc_maxfd]) break; } } if (dolock) rwlock_unlock(&svc_fd_lock); } /* * Add a service program to the callout list. * The dispatch routine will be called when a rpc request for this * program number comes in. */ bool_t svc_reg(xprt, prog, vers, dispatch, nconf) SVCXPRT *xprt; const rpcprog_t prog; const rpcvers_t vers; void (*dispatch)(struct svc_req *, SVCXPRT *); const struct netconfig *nconf; { bool_t dummy; struct svc_callout *prev; struct svc_callout *s; struct netconfig *tnconf; char *netid = NULL; int flag = 0; /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */ if (xprt->xp_netid) { netid = strdup(xprt->xp_netid); flag = 1; } else if (nconf && nconf->nc_netid) { netid = strdup(nconf->nc_netid); flag = 1; } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) { netid = strdup(tnconf->nc_netid); flag = 1; freenetconfigent(tnconf); } /* must have been created with svc_raw_create */ if ((netid == NULL) && (flag == 1)) { return (FALSE); } rwlock_wrlock(&svc_lock); if ((s = svc_find(prog, vers, &prev, netid)) != NULL) { - if (netid) - free(netid); + free(netid); if (s->sc_dispatch == dispatch) goto rpcb_it; /* he is registering another xptr */ rwlock_unlock(&svc_lock); return (FALSE); } s = mem_alloc(sizeof (struct svc_callout)); if (s == NULL) { - if (netid) - free(netid); + free(netid); rwlock_unlock(&svc_lock); return (FALSE); } s->sc_prog = prog; s->sc_vers = vers; s->sc_dispatch = dispatch; s->sc_netid = netid; s->sc_next = svc_head; svc_head = s; if ((xprt->xp_netid == NULL) && (flag == 1) && netid) ((SVCXPRT *) xprt)->xp_netid = strdup(netid); rpcb_it: rwlock_unlock(&svc_lock); /* now register the information with the local binder service */ if (nconf) { /*LINTED const castaway*/ dummy = rpcb_set(prog, vers, (struct netconfig *) nconf, &((SVCXPRT *) xprt)->xp_ltaddr); return (dummy); } return (TRUE); } /* * Remove a service program from the callout list. */ void svc_unreg(prog, vers) const rpcprog_t prog; const rpcvers_t vers; { struct svc_callout *prev; struct svc_callout *s; /* unregister the information anyway */ (void) rpcb_unset(prog, vers, NULL); rwlock_wrlock(&svc_lock); while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) { if (prev == NULL) { svc_head = s->sc_next; } else { prev->sc_next = s->sc_next; } s->sc_next = NULL; if (s->sc_netid) mem_free(s->sc_netid, sizeof (s->sc_netid) + 1); mem_free(s, sizeof (struct svc_callout)); } rwlock_unlock(&svc_lock); } /* ********************** CALLOUT list related stuff ************* */ #ifdef PORTMAP /* * Add a service program to the callout list. * The dispatch routine will be called when a rpc request for this * program number comes in. */ bool_t svc_register(xprt, prog, vers, dispatch, protocol) SVCXPRT *xprt; u_long prog; u_long vers; void (*dispatch)(struct svc_req *, SVCXPRT *); int protocol; { struct svc_callout *prev; struct svc_callout *s; assert(xprt != NULL); assert(dispatch != NULL); if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) != NULL) { if (s->sc_dispatch == dispatch) goto pmap_it; /* he is registering another xptr */ return (FALSE); } s = mem_alloc(sizeof(struct svc_callout)); if (s == NULL) { return (FALSE); } s->sc_prog = (rpcprog_t)prog; s->sc_vers = (rpcvers_t)vers; s->sc_dispatch = dispatch; s->sc_next = svc_head; svc_head = s; pmap_it: /* now register the information with the local binder service */ if (protocol) { return (pmap_set(prog, vers, protocol, xprt->xp_port)); } return (TRUE); } /* * Remove a service program from the callout list. */ void svc_unregister(prog, vers) u_long prog; u_long vers; { struct svc_callout *prev; struct svc_callout *s; if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) == NULL) return; if (prev == NULL) { svc_head = s->sc_next; } else { prev->sc_next = s->sc_next; } s->sc_next = NULL; mem_free(s, sizeof(struct svc_callout)); /* now unregister the information with the local binder service */ (void)pmap_unset(prog, vers); } #endif /* PORTMAP */ /* * Search the callout list for a program number, return the callout * struct. */ static struct svc_callout * svc_find(prog, vers, prev, netid) rpcprog_t prog; rpcvers_t vers; struct svc_callout **prev; char *netid; { struct svc_callout *s, *p; assert(prev != NULL); p = NULL; for (s = svc_head; s != NULL; s = s->sc_next) { if (((s->sc_prog == prog) && (s->sc_vers == vers)) && ((netid == NULL) || (s->sc_netid == NULL) || (strcmp(netid, s->sc_netid) == 0))) break; p = s; } *prev = p; return (s); } /* ******************* REPLY GENERATION ROUTINES ************ */ /* * Send a reply to an rpc request */ bool_t svc_sendreply(xprt, xdr_results, xdr_location) SVCXPRT *xprt; xdrproc_t xdr_results; void * xdr_location; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = SUCCESS; rply.acpted_rply.ar_results.where = xdr_location; rply.acpted_rply.ar_results.proc = xdr_results; return (SVC_REPLY(xprt, &rply)); } /* * No procedure error reply */ void svcerr_noproc(xprt) SVCXPRT *xprt; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = PROC_UNAVAIL; SVC_REPLY(xprt, &rply); } /* * Can't decode args error reply */ void svcerr_decode(xprt) SVCXPRT *xprt; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = GARBAGE_ARGS; SVC_REPLY(xprt, &rply); } /* * Some system error */ void svcerr_systemerr(xprt) SVCXPRT *xprt; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = SYSTEM_ERR; SVC_REPLY(xprt, &rply); } #if 0 /* * Tell RPC package to not complain about version errors to the client. This * is useful when revving broadcast protocols that sit on a fixed address. * There is really one (or should be only one) example of this kind of * protocol: the portmapper (or rpc binder). */ void __svc_versquiet_on(xprt) SVCXPRT *xprt; { SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET; } void __svc_versquiet_off(xprt) SVCXPRT *xprt; { SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET; } void svc_versquiet(xprt) SVCXPRT *xprt; { __svc_versquiet_on(xprt); } int __svc_versquiet_get(xprt) SVCXPRT *xprt; { return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET); } #endif /* * Authentication error reply */ void svcerr_auth(xprt, why) SVCXPRT *xprt; enum auth_stat why; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_DENIED; rply.rjcted_rply.rj_stat = AUTH_ERROR; rply.rjcted_rply.rj_why = why; SVC_REPLY(xprt, &rply); } /* * Auth too weak error reply */ void svcerr_weakauth(xprt) SVCXPRT *xprt; { assert(xprt != NULL); svcerr_auth(xprt, AUTH_TOOWEAK); } /* * Program unavailable error reply */ void svcerr_noprog(xprt) SVCXPRT *xprt; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = PROG_UNAVAIL; SVC_REPLY(xprt, &rply); } /* * Program version mismatch error reply */ void svcerr_progvers(xprt, low_vers, high_vers) SVCXPRT *xprt; rpcvers_t low_vers; rpcvers_t high_vers; { struct rpc_msg rply; assert(xprt != NULL); rply.rm_direction = REPLY; rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = PROG_MISMATCH; rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers; rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers; SVC_REPLY(xprt, &rply); } /* * Allocate a new server transport structure. All fields are * initialized to zero and xp_p3 is initialized to point at an * extension structure to hold various flags and authentication * parameters. */ SVCXPRT * svc_xprt_alloc() { SVCXPRT *xprt; SVCXPRT_EXT *ext; xprt = mem_alloc(sizeof(SVCXPRT)); if (xprt == NULL) return (NULL); memset(xprt, 0, sizeof(SVCXPRT)); ext = mem_alloc(sizeof(SVCXPRT_EXT)); if (ext == NULL) { mem_free(xprt, sizeof(SVCXPRT)); return (NULL); } memset(ext, 0, sizeof(SVCXPRT_EXT)); xprt->xp_p3 = ext; ext->xp_auth.svc_ah_ops = &svc_auth_null_ops; return (xprt); } /* * Free a server transport structure. */ void svc_xprt_free(xprt) SVCXPRT *xprt; { mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT)); mem_free(xprt, sizeof(SVCXPRT)); } /* ******************* SERVER INPUT STUFF ******************* */ /* * Get server side input from some transport. * * Statement of authentication parameters management: * This function owns and manages all authentication parameters, specifically * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and * the "cooked" credentials (rqst->rq_clntcred). * However, this function does not know the structure of the cooked * credentials, so it make the following assumptions: * a) the structure is contiguous (no pointers), and * b) the cred structure size does not exceed RQCRED_SIZE bytes. * In all events, all three parameters are freed upon exit from this routine. * The storage is trivially management on the call stack in user land, but * is mallocated in kernel land. */ void svc_getreq(rdfds) int rdfds; { fd_set readfds; FD_ZERO(&readfds); readfds.fds_bits[0] = rdfds; svc_getreqset(&readfds); } void svc_getreqset(readfds) fd_set *readfds; { int bit, fd; fd_mask mask, *maskp; int sock; assert(readfds != NULL); maskp = readfds->fds_bits; for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) { for (mask = *maskp++; (bit = ffsl(mask)) != 0; mask ^= (1ul << (bit - 1))) { /* sock has input waiting */ fd = sock + bit - 1; svc_getreq_common(fd); } } } void svc_getreq_common(fd) int fd; { SVCXPRT *xprt; struct svc_req r; struct rpc_msg msg; int prog_found; rpcvers_t low_vers; rpcvers_t high_vers; enum xprt_stat stat; char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE]; msg.rm_call.cb_cred.oa_base = cred_area; msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]); r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]); rwlock_rdlock(&svc_fd_lock); xprt = __svc_xports[fd]; rwlock_unlock(&svc_fd_lock); if (xprt == NULL) /* But do we control sock? */ return; /* now receive msgs from xprtprt (support batch calls) */ do { if (SVC_RECV(xprt, &msg)) { /* now find the exported program and call it */ struct svc_callout *s; enum auth_stat why; r.rq_xprt = xprt; r.rq_prog = msg.rm_call.cb_prog; r.rq_vers = msg.rm_call.cb_vers; r.rq_proc = msg.rm_call.cb_proc; r.rq_cred = msg.rm_call.cb_cred; /* first authenticate the message */ if ((why = _authenticate(&r, &msg)) != AUTH_OK) { /* * RPCSEC_GSS uses this return code * for requests that form part of its * context establishment protocol and * should not be dispatched to the * application. */ if (why != RPCSEC_GSS_NODISPATCH) svcerr_auth(xprt, why); goto call_done; } /* now match message with a registered service*/ prog_found = FALSE; low_vers = (rpcvers_t) -1L; high_vers = (rpcvers_t) 0L; for (s = svc_head; s != NULL; s = s->sc_next) { if (s->sc_prog == r.rq_prog) { if (s->sc_vers == r.rq_vers) { (*s->sc_dispatch)(&r, xprt); goto call_done; } /* found correct version */ prog_found = TRUE; if (s->sc_vers < low_vers) low_vers = s->sc_vers; if (s->sc_vers > high_vers) high_vers = s->sc_vers; } /* found correct program */ } /* * if we got here, the program or version * is not served ... */ if (prog_found) svcerr_progvers(xprt, low_vers, high_vers); else svcerr_noprog(xprt); /* Fall through to ... */ } /* * Check if the xprt has been disconnected in a * recursive call in the service dispatch routine. * If so, then break. */ rwlock_rdlock(&svc_fd_lock); if (xprt != __svc_xports[fd]) { rwlock_unlock(&svc_fd_lock); break; } rwlock_unlock(&svc_fd_lock); call_done: if ((stat = SVC_STAT(xprt)) == XPRT_DIED){ SVC_DESTROY(xprt); break; } } while (stat == XPRT_MOREREQS); } void svc_getreq_poll(pfdp, pollretval) struct pollfd *pfdp; int pollretval; { int i; int fds_found; for (i = fds_found = 0; fds_found < pollretval; i++) { struct pollfd *p = &pfdp[i]; if (p->revents) { /* fd has input waiting */ fds_found++; /* * We assume that this function is only called * via someone _select()ing from svc_fdset or * _poll()ing from svc_pollset[]. Thus it's safe * to handle the POLLNVAL event by simply turning * the corresponding bit off in svc_fdset. The * svc_pollset[] array is derived from svc_fdset * and so will also be updated eventually. * * XXX Should we do an xprt_unregister() instead? */ if (p->revents & POLLNVAL) { rwlock_wrlock(&svc_fd_lock); FD_CLR(p->fd, &svc_fdset); rwlock_unlock(&svc_fd_lock); } else svc_getreq_common(p->fd); } } } bool_t rpc_control(int what, void *arg) { int val; switch (what) { case RPC_SVC_CONNMAXREC_SET: val = *(int *)arg; if (val <= 0) return FALSE; __svc_maxrec = val; return TRUE; case RPC_SVC_CONNMAXREC_GET: *(int *)arg = __svc_maxrec; return TRUE; default: break; } return FALSE; } Index: stable/10/lib/libc/rpc/svc_dg.c =================================================================== --- stable/10/lib/libc/rpc/svc_dg.c (revision 290898) +++ stable/10/lib/libc/rpc/svc_dg.c (revision 290899) @@ -1,738 +1,737 @@ /* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (c) 1986-1991 by Sun Microsystems Inc. */ #if defined(LIBC_SCCS) && !defined(lint) #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" #endif #include __FBSDID("$FreeBSD$"); /* * svc_dg.c, Server side for connectionless RPC. * * Does some caching in the hopes of achieving execute-at-most-once semantics. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include #include #include #ifdef RPC_CACHE_DEBUG #include #include #endif #include #include "un-namespace.h" #include "rpc_com.h" #include "mt_misc.h" #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2)) #define rpc_buffer(xprt) ((xprt)->xp_p1) #ifndef MAX #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #endif static void svc_dg_ops(SVCXPRT *); static enum xprt_stat svc_dg_stat(SVCXPRT *); static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *); static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *); static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *); static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *); static void svc_dg_destroy(SVCXPRT *); static bool_t svc_dg_control(SVCXPRT *, const u_int, void *); static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *); static void cache_set(SVCXPRT *, size_t); int svc_dg_enablecache(SVCXPRT *, u_int); /* * Usage: * xprt = svc_dg_create(sock, sendsize, recvsize); * Does other connectionless specific initializations. * Once *xprt is initialized, it is registered. * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable * system defaults are chosen. * The routines returns NULL if a problem occurred. */ static const char svc_dg_str[] = "svc_dg_create: %s"; static const char svc_dg_err1[] = "could not get transport information"; static const char svc_dg_err2[] = "transport does not support data transfer"; static const char svc_dg_err3[] = "getsockname failed"; static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR"; static const char __no_mem_str[] = "out of memory"; SVCXPRT * svc_dg_create(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { SVCXPRT *xprt; struct svc_dg_data *su = NULL; struct __rpc_sockinfo si; struct sockaddr_storage ss; socklen_t slen; if (!__rpc_fd2sockinfo(fd, &si)) { warnx(svc_dg_str, svc_dg_err1); return (NULL); } /* * Find the receive and the send size */ sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize); recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize); if ((sendsize == 0) || (recvsize == 0)) { warnx(svc_dg_str, svc_dg_err2); return (NULL); } xprt = svc_xprt_alloc(); if (xprt == NULL) goto freedata; su = mem_alloc(sizeof (*su)); if (su == NULL) goto freedata; su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4; if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) goto freedata; xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE); su->su_cache = NULL; xprt->xp_fd = fd; xprt->xp_p2 = su; xprt->xp_verf.oa_base = su->su_verfbody; svc_dg_ops(xprt); xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage); slen = sizeof ss; if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { warnx(svc_dg_str, svc_dg_err3); goto freedata_nowarn; } xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage)); xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage); xprt->xp_ltaddr.len = slen; memcpy(xprt->xp_ltaddr.buf, &ss, slen); if (ss.ss_family == AF_INET) { struct sockaddr_in *sin; static const int true_value = 1; sin = (struct sockaddr_in *)(void *)&ss; if (sin->sin_addr.s_addr == INADDR_ANY) { su->su_srcaddr.buf = mem_alloc(sizeof (ss)); su->su_srcaddr.maxlen = sizeof (ss); if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &true_value, sizeof(true_value))) { warnx(svc_dg_str, svc_dg_err4); goto freedata_nowarn; } } } xprt_register(xprt); return (xprt); freedata: (void) warnx(svc_dg_str, __no_mem_str); freedata_nowarn: if (xprt) { if (su) (void) mem_free(su, sizeof (*su)); svc_xprt_free(xprt); } return (NULL); } /*ARGSUSED*/ static enum xprt_stat svc_dg_stat(xprt) SVCXPRT *xprt; { return (XPRT_IDLE); } static int svc_dg_recvfrom(int fd, char *buf, int buflen, struct sockaddr *raddr, socklen_t *raddrlen, struct sockaddr *laddr, socklen_t *laddrlen) { struct msghdr msg; struct iovec msg_iov[1]; struct sockaddr_in *lin = (struct sockaddr_in *)laddr; int rlen; bool_t have_lin = FALSE; char tmp[CMSG_LEN(sizeof(*lin))]; struct cmsghdr *cmsg; memset((char *)&msg, 0, sizeof(msg)); msg_iov[0].iov_base = buf; msg_iov[0].iov_len = buflen; msg.msg_iov = msg_iov; msg.msg_iovlen = 1; msg.msg_namelen = *raddrlen; msg.msg_name = (char *)raddr; if (laddr != NULL) { msg.msg_control = (caddr_t)tmp; msg.msg_controllen = CMSG_LEN(sizeof(*lin)); } rlen = _recvmsg(fd, &msg, 0); if (rlen >= 0) *raddrlen = msg.msg_namelen; if (rlen == -1 || laddr == NULL || msg.msg_controllen < sizeof(struct cmsghdr) || msg.msg_flags & MSG_CTRUNC) return rlen; for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) { have_lin = TRUE; memcpy(&lin->sin_addr, (struct in_addr *)CMSG_DATA(cmsg), sizeof(struct in_addr)); break; } } lin->sin_family = AF_INET; lin->sin_port = 0; *laddrlen = sizeof(struct sockaddr_in); if (!have_lin) lin->sin_addr.s_addr = INADDR_ANY; return rlen; } static bool_t svc_dg_recv(xprt, msg) SVCXPRT *xprt; struct rpc_msg *msg; { struct svc_dg_data *su = su_data(xprt); XDR *xdrs = &(su->su_xdrs); char *reply; struct sockaddr_storage ss; socklen_t alen; size_t replylen; ssize_t rlen; again: alen = sizeof (struct sockaddr_storage); rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, (struct sockaddr *)(void *)&ss, &alen, (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len); if (rlen == -1 && errno == EINTR) goto again; if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t)))) return (FALSE); if (xprt->xp_rtaddr.len < alen) { if (xprt->xp_rtaddr.len != 0) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len); xprt->xp_rtaddr.buf = mem_alloc(alen); xprt->xp_rtaddr.len = alen; } memcpy(xprt->xp_rtaddr.buf, &ss, alen); #ifdef PORTMAP if (ss.ss_family == AF_INET) { xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf; xprt->xp_addrlen = sizeof (struct sockaddr_in); } #endif /* PORTMAP */ xdrs->x_op = XDR_DECODE; XDR_SETPOS(xdrs, 0); if (! xdr_callmsg(xdrs, msg)) { return (FALSE); } su->su_xid = msg->rm_xid; if (su->su_cache != NULL) { if (cache_get(xprt, msg, &reply, &replylen)) { (void)_sendto(xprt->xp_fd, reply, replylen, 0, (struct sockaddr *)(void *)&ss, alen); return (FALSE); } } return (TRUE); } static int svc_dg_sendto(int fd, char *buf, int buflen, const struct sockaddr *raddr, socklen_t raddrlen, const struct sockaddr *laddr, socklen_t laddrlen) { struct msghdr msg; struct iovec msg_iov[1]; struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr; struct in_addr *lin = &laddr_in->sin_addr; char tmp[CMSG_SPACE(sizeof(*lin))]; struct cmsghdr *cmsg; memset((char *)&msg, 0, sizeof(msg)); msg_iov[0].iov_base = buf; msg_iov[0].iov_len = buflen; msg.msg_iov = msg_iov; msg.msg_iovlen = 1; msg.msg_namelen = raddrlen; msg.msg_name = (char *)raddr; if (laddr != NULL && laddr->sa_family == AF_INET && lin->s_addr != INADDR_ANY) { msg.msg_control = (caddr_t)tmp; msg.msg_controllen = CMSG_LEN(sizeof(*lin)); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_len = CMSG_LEN(sizeof(*lin)); cmsg->cmsg_level = IPPROTO_IP; cmsg->cmsg_type = IP_SENDSRCADDR; memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin)); } return _sendmsg(fd, &msg, 0); } static bool_t svc_dg_reply(xprt, msg) SVCXPRT *xprt; struct rpc_msg *msg; { struct svc_dg_data *su = su_data(xprt); XDR *xdrs = &(su->su_xdrs); bool_t stat = TRUE; size_t slen; xdrproc_t xdr_proc; caddr_t xdr_where; xdrs->x_op = XDR_ENCODE; XDR_SETPOS(xdrs, 0); msg->rm_xid = su->su_xid; if (msg->rm_reply.rp_stat == MSG_ACCEPTED && msg->rm_reply.rp_acpt.ar_stat == SUCCESS) { xdr_proc = msg->acpted_rply.ar_results.proc; xdr_where = msg->acpted_rply.ar_results.where; msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; msg->acpted_rply.ar_results.where = NULL; if (!xdr_replymsg(xdrs, msg) || !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where)) stat = FALSE; } else { stat = xdr_replymsg(xdrs, msg); } if (stat) { slen = XDR_GETPOS(xdrs); if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen, (struct sockaddr *)xprt->xp_rtaddr.buf, (socklen_t)xprt->xp_rtaddr.len, (struct sockaddr *)su->su_srcaddr.buf, (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) { stat = TRUE; if (su->su_cache) cache_set(xprt, slen); } } return (stat); } static bool_t svc_dg_getargs(xprt, xdr_args, args_ptr) SVCXPRT *xprt; xdrproc_t xdr_args; void *args_ptr; { struct svc_dg_data *su; assert(xprt != NULL); su = su_data(xprt); return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), &su->su_xdrs, xdr_args, args_ptr)); } static bool_t svc_dg_freeargs(xprt, xdr_args, args_ptr) SVCXPRT *xprt; xdrproc_t xdr_args; void *args_ptr; { XDR *xdrs = &(su_data(xprt)->su_xdrs); xdrs->x_op = XDR_FREE; return (*xdr_args)(xdrs, args_ptr); } static void svc_dg_destroy(xprt) SVCXPRT *xprt; { struct svc_dg_data *su = su_data(xprt); xprt_unregister(xprt); if (xprt->xp_fd != -1) (void)_close(xprt->xp_fd); XDR_DESTROY(&(su->su_xdrs)); (void) mem_free(rpc_buffer(xprt), su->su_iosz); if (su->su_srcaddr.buf) (void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen); (void) mem_free(su, sizeof (*su)); if (xprt->xp_rtaddr.buf) (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); if (xprt->xp_ltaddr.buf) (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); - if (xprt->xp_tp) - (void) free(xprt->xp_tp); + free(xprt->xp_tp); svc_xprt_free(xprt); } static bool_t /*ARGSUSED*/ svc_dg_control(xprt, rq, in) SVCXPRT *xprt; const u_int rq; void *in; { return (FALSE); } static void svc_dg_ops(xprt) SVCXPRT *xprt; { static struct xp_ops ops; static struct xp_ops2 ops2; /* VARIABLES PROTECTED BY ops_lock: ops */ mutex_lock(&ops_lock); if (ops.xp_recv == NULL) { ops.xp_recv = svc_dg_recv; ops.xp_stat = svc_dg_stat; ops.xp_getargs = svc_dg_getargs; ops.xp_reply = svc_dg_reply; ops.xp_freeargs = svc_dg_freeargs; ops.xp_destroy = svc_dg_destroy; ops2.xp_control = svc_dg_control; } xprt->xp_ops = &ops; xprt->xp_ops2 = &ops2; mutex_unlock(&ops_lock); } /* The CACHING COMPONENT */ /* * Could have been a separate file, but some part of it depends upon the * private structure of the client handle. * * Fifo cache for cl server * Copies pointers to reply buffers into fifo cache * Buffers are sent again if retransmissions are detected. */ #define SPARSENESS 4 /* 75% sparse */ #define ALLOC(type, size) \ (type *) mem_alloc((sizeof (type) * (size))) #define MEMZERO(addr, type, size) \ (void) memset((void *) (addr), 0, sizeof (type) * (int) (size)) #define FREE(addr, type, size) \ mem_free((addr), (sizeof (type) * (size))) /* * An entry in the cache */ typedef struct cache_node *cache_ptr; struct cache_node { /* * Index into cache is xid, proc, vers, prog and address */ u_int32_t cache_xid; rpcproc_t cache_proc; rpcvers_t cache_vers; rpcprog_t cache_prog; struct netbuf cache_addr; /* * The cached reply and length */ char *cache_reply; size_t cache_replylen; /* * Next node on the list, if there is a collision */ cache_ptr cache_next; }; /* * The entire cache */ struct cl_cache { u_int uc_size; /* size of cache */ cache_ptr *uc_entries; /* hash table of entries in cache */ cache_ptr *uc_fifo; /* fifo list of entries in cache */ u_int uc_nextvictim; /* points to next victim in fifo list */ rpcprog_t uc_prog; /* saved program number */ rpcvers_t uc_vers; /* saved version number */ rpcproc_t uc_proc; /* saved procedure number */ }; /* * the hashing function */ #define CACHE_LOC(transp, xid) \ (xid % (SPARSENESS * ((struct cl_cache *) \ su_data(transp)->su_cache)->uc_size)) /* * Enable use of the cache. Returns 1 on success, 0 on failure. * Note: there is no disable. */ static const char cache_enable_str[] = "svc_enablecache: %s %s"; static const char alloc_err[] = "could not allocate cache "; static const char enable_err[] = "cache already enabled"; int svc_dg_enablecache(transp, size) SVCXPRT *transp; u_int size; { struct svc_dg_data *su = su_data(transp); struct cl_cache *uc; mutex_lock(&dupreq_lock); if (su->su_cache != NULL) { (void) warnx(cache_enable_str, enable_err, " "); mutex_unlock(&dupreq_lock); return (0); } uc = ALLOC(struct cl_cache, 1); if (uc == NULL) { warnx(cache_enable_str, alloc_err, " "); mutex_unlock(&dupreq_lock); return (0); } uc->uc_size = size; uc->uc_nextvictim = 0; uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS); if (uc->uc_entries == NULL) { warnx(cache_enable_str, alloc_err, "data"); FREE(uc, struct cl_cache, 1); mutex_unlock(&dupreq_lock); return (0); } MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS); uc->uc_fifo = ALLOC(cache_ptr, size); if (uc->uc_fifo == NULL) { warnx(cache_enable_str, alloc_err, "fifo"); FREE(uc->uc_entries, cache_ptr, size * SPARSENESS); FREE(uc, struct cl_cache, 1); mutex_unlock(&dupreq_lock); return (0); } MEMZERO(uc->uc_fifo, cache_ptr, size); su->su_cache = (char *)(void *)uc; mutex_unlock(&dupreq_lock); return (1); } /* * Set an entry in the cache. It assumes that the uc entry is set from * the earlier call to cache_get() for the same procedure. This will always * happen because cache_get() is calle by svc_dg_recv and cache_set() is called * by svc_dg_reply(). All this hoopla because the right RPC parameters are * not available at svc_dg_reply time. */ static const char cache_set_str[] = "cache_set: %s"; static const char cache_set_err1[] = "victim not found"; static const char cache_set_err2[] = "victim alloc failed"; static const char cache_set_err3[] = "could not allocate new rpc buffer"; static void cache_set(xprt, replylen) SVCXPRT *xprt; size_t replylen; { cache_ptr victim; cache_ptr *vicp; struct svc_dg_data *su = su_data(xprt); struct cl_cache *uc = (struct cl_cache *) su->su_cache; u_int loc; char *newbuf; #ifdef RPC_CACHE_DEBUG struct netconfig *nconf; char *uaddr; #endif mutex_lock(&dupreq_lock); /* * Find space for the new entry, either by * reusing an old entry, or by mallocing a new one */ victim = uc->uc_fifo[uc->uc_nextvictim]; if (victim != NULL) { loc = CACHE_LOC(xprt, victim->cache_xid); for (vicp = &uc->uc_entries[loc]; *vicp != NULL && *vicp != victim; vicp = &(*vicp)->cache_next) ; if (*vicp == NULL) { warnx(cache_set_str, cache_set_err1); mutex_unlock(&dupreq_lock); return; } *vicp = victim->cache_next; /* remove from cache */ newbuf = victim->cache_reply; } else { victim = ALLOC(struct cache_node, 1); if (victim == NULL) { warnx(cache_set_str, cache_set_err2); mutex_unlock(&dupreq_lock); return; } newbuf = mem_alloc(su->su_iosz); if (newbuf == NULL) { warnx(cache_set_str, cache_set_err3); FREE(victim, struct cache_node, 1); mutex_unlock(&dupreq_lock); return; } } /* * Store it away */ #ifdef RPC_CACHE_DEBUG if (nconf = getnetconfigent(xprt->xp_netid)) { uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); freenetconfigent(nconf); printf( "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n", su->su_xid, uc->uc_prog, uc->uc_vers, uc->uc_proc, uaddr); free(uaddr); } #endif victim->cache_replylen = replylen; victim->cache_reply = rpc_buffer(xprt); rpc_buffer(xprt) = newbuf; xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE); victim->cache_xid = su->su_xid; victim->cache_proc = uc->uc_proc; victim->cache_vers = uc->uc_vers; victim->cache_prog = uc->uc_prog; victim->cache_addr = xprt->xp_rtaddr; victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len); (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf, (size_t)xprt->xp_rtaddr.len); loc = CACHE_LOC(xprt, victim->cache_xid); victim->cache_next = uc->uc_entries[loc]; uc->uc_entries[loc] = victim; uc->uc_fifo[uc->uc_nextvictim++] = victim; uc->uc_nextvictim %= uc->uc_size; mutex_unlock(&dupreq_lock); } /* * Try to get an entry from the cache * return 1 if found, 0 if not found and set the stage for cache_set() */ static int cache_get(xprt, msg, replyp, replylenp) SVCXPRT *xprt; struct rpc_msg *msg; char **replyp; size_t *replylenp; { u_int loc; cache_ptr ent; struct svc_dg_data *su = su_data(xprt); struct cl_cache *uc = (struct cl_cache *) su->su_cache; #ifdef RPC_CACHE_DEBUG struct netconfig *nconf; char *uaddr; #endif mutex_lock(&dupreq_lock); loc = CACHE_LOC(xprt, su->su_xid); for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) { if (ent->cache_xid == su->su_xid && ent->cache_proc == msg->rm_call.cb_proc && ent->cache_vers == msg->rm_call.cb_vers && ent->cache_prog == msg->rm_call.cb_prog && ent->cache_addr.len == xprt->xp_rtaddr.len && (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len) == 0)) { #ifdef RPC_CACHE_DEBUG if (nconf = getnetconfigent(xprt->xp_netid)) { uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); freenetconfigent(nconf); printf( "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n", su->su_xid, msg->rm_call.cb_prog, msg->rm_call.cb_vers, msg->rm_call.cb_proc, uaddr); free(uaddr); } #endif *replyp = ent->cache_reply; *replylenp = ent->cache_replylen; mutex_unlock(&dupreq_lock); return (1); } } /* * Failed to find entry * Remember a few things so we can do a set later */ uc->uc_proc = msg->rm_call.cb_proc; uc->uc_vers = msg->rm_call.cb_vers; uc->uc_prog = msg->rm_call.cb_prog; mutex_unlock(&dupreq_lock); return (0); } Index: stable/10/lib/libc/rpc/svc_simple.c =================================================================== --- stable/10/lib/libc/rpc/svc_simple.c (revision 290898) +++ stable/10/lib/libc/rpc/svc_simple.c (revision 290899) @@ -1,312 +1,310 @@ /* $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (c) 1986-1991 by Sun Microsystems Inc. */ /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */ #include __FBSDID("$FreeBSD$"); /* * svc_simple.c * Simplified front end to rpc. */ /* * This interface creates a virtual listener for all the services * started thru rpc_reg(). It listens on the same endpoint for * all the services and then executes the corresponding service * for the given prognum and procnum. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include "un-namespace.h" #include "rpc_com.h" #include "mt_misc.h" static void universal(struct svc_req *, SVCXPRT *); static struct proglst { char *(*p_progname)(char *); rpcprog_t p_prognum; rpcvers_t p_versnum; rpcproc_t p_procnum; SVCXPRT *p_transp; char *p_netid; char *p_xdrbuf; int p_recvsz; xdrproc_t p_inproc, p_outproc; struct proglst *p_nxt; } *proglst; static const char rpc_reg_err[] = "%s: %s"; static const char rpc_reg_msg[] = "rpc_reg: "; static const char __reg_err1[] = "can't find appropriate transport"; static const char __reg_err2[] = "can't get protocol info"; static const char __reg_err3[] = "unsupported transport size"; static const char __no_mem_str[] = "out of memory"; /* * For simplified, easy to use kind of rpc interfaces. * nettype indicates the type of transport on which the service will be * listening. Used for conservation of the system resource. Only one * handle is created for all the services (actually one of each netid) * and same xdrbuf is used for same netid. The size of the arguments * is also limited by the recvsize for that transport, even if it is * a COTS transport. This may be wrong, but for cases like these, they * should not use the simplified interfaces like this. */ int rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype) rpcprog_t prognum; /* program number */ rpcvers_t versnum; /* version number */ rpcproc_t procnum; /* procedure number */ char *(*progname)(char *); /* Server routine */ xdrproc_t inproc, outproc; /* in/out XDR procedures */ char *nettype; /* nettype */ { struct netconfig *nconf; int done = FALSE; void *handle; if (procnum == NULLPROC) { warnx("%s can't reassign procedure number %u", rpc_reg_msg, NULLPROC); return (-1); } if (nettype == NULL) nettype = "netpath"; /* The default behavior */ if ((handle = __rpc_setconf(nettype)) == NULL) { warnx(rpc_reg_err, rpc_reg_msg, __reg_err1); return (-1); } /* VARIABLES PROTECTED BY proglst_lock: proglst */ mutex_lock(&proglst_lock); while ((nconf = __rpc_getconf(handle)) != NULL) { struct proglst *pl; SVCXPRT *svcxprt; int madenow; u_int recvsz; char *xdrbuf; char *netid; madenow = FALSE; svcxprt = NULL; recvsz = 0; xdrbuf = netid = NULL; for (pl = proglst; pl; pl = pl->p_nxt) { if (strcmp(pl->p_netid, nconf->nc_netid) == 0) { svcxprt = pl->p_transp; xdrbuf = pl->p_xdrbuf; recvsz = pl->p_recvsz; netid = pl->p_netid; break; } } if (svcxprt == NULL) { struct __rpc_sockinfo si; svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); if (svcxprt == NULL) continue; if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) { warnx(rpc_reg_err, rpc_reg_msg, __reg_err2); SVC_DESTROY(svcxprt); continue; } recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0); if (recvsz == 0) { warnx(rpc_reg_err, rpc_reg_msg, __reg_err3); SVC_DESTROY(svcxprt); continue; } if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) || ((netid = strdup(nconf->nc_netid)) == NULL)) { warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); - if (xdrbuf != NULL) - free(xdrbuf); - if (netid != NULL) - free(netid); + free(xdrbuf); + free(netid); SVC_DESTROY(svcxprt); break; } madenow = TRUE; } /* * Check if this (program, version, netid) had already been * registered. The check may save a few RPC calls to rpcbind */ for (pl = proglst; pl; pl = pl->p_nxt) if ((pl->p_prognum == prognum) && (pl->p_versnum == versnum) && (strcmp(pl->p_netid, netid) == 0)) break; if (pl == NULL) { /* Not yet */ (void) rpcb_unset(prognum, versnum, nconf); } else { /* so that svc_reg does not call rpcb_set() */ nconf = NULL; } if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) { warnx("%s couldn't register prog %u vers %u for %s", rpc_reg_msg, (unsigned)prognum, (unsigned)versnum, netid); if (madenow) { SVC_DESTROY(svcxprt); free(xdrbuf); free(netid); } continue; } pl = malloc(sizeof (struct proglst)); if (pl == NULL) { warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); if (madenow) { SVC_DESTROY(svcxprt); free(xdrbuf); free(netid); } break; } pl->p_progname = progname; pl->p_prognum = prognum; pl->p_versnum = versnum; pl->p_procnum = procnum; pl->p_inproc = inproc; pl->p_outproc = outproc; pl->p_transp = svcxprt; pl->p_xdrbuf = xdrbuf; pl->p_recvsz = recvsz; pl->p_netid = netid; pl->p_nxt = proglst; proglst = pl; done = TRUE; } __rpc_endconf(handle); mutex_unlock(&proglst_lock); if (done == FALSE) { warnx("%s cant find suitable transport for %s", rpc_reg_msg, nettype); return (-1); } return (0); } /* * The universal handler for the services registered using registerrpc. * It handles both the connectionless and the connection oriented cases. */ static void universal(rqstp, transp) struct svc_req *rqstp; SVCXPRT *transp; { rpcprog_t prog; rpcvers_t vers; rpcproc_t proc; char *outdata; char *xdrbuf; struct proglst *pl; /* * enforce "procnum 0 is echo" convention */ if (rqstp->rq_proc == NULLPROC) { if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) == FALSE) { warnx("svc_sendreply failed"); } return; } prog = rqstp->rq_prog; vers = rqstp->rq_vers; proc = rqstp->rq_proc; mutex_lock(&proglst_lock); for (pl = proglst; pl; pl = pl->p_nxt) if (pl->p_prognum == prog && pl->p_procnum == proc && pl->p_versnum == vers && (strcmp(pl->p_netid, transp->xp_netid) == 0)) { /* decode arguments into a CLEAN buffer */ xdrbuf = pl->p_xdrbuf; /* Zero the arguments: reqd ! */ (void) memset(xdrbuf, 0, sizeof (pl->p_recvsz)); /* * Assuming that sizeof (xdrbuf) would be enough * for the arguments; if not then the program * may bomb. BEWARE! */ if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) { svcerr_decode(transp); mutex_unlock(&proglst_lock); return; } outdata = (*(pl->p_progname))(xdrbuf); if (outdata == NULL && pl->p_outproc != (xdrproc_t) xdr_void){ /* there was an error */ mutex_unlock(&proglst_lock); return; } if (!svc_sendreply(transp, pl->p_outproc, outdata)) { warnx( "rpc: rpc_reg trouble replying to prog %u vers %u", (unsigned)prog, (unsigned)vers); mutex_unlock(&proglst_lock); return; } /* free the decoded arguments */ (void)svc_freeargs(transp, pl->p_inproc, xdrbuf); mutex_unlock(&proglst_lock); return; } mutex_unlock(&proglst_lock); /* This should never happen */ warnx("rpc: rpc_reg: never registered prog %u vers %u", (unsigned)prog, (unsigned)vers); return; } Index: stable/10/lib/libc/rpc/svc_vc.c =================================================================== --- stable/10/lib/libc/rpc/svc_vc.c (revision 290898) +++ stable/10/lib/libc/rpc/svc_vc.c (revision 290899) @@ -1,810 +1,808 @@ /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */ /*- * Copyright (c) 2009, Sun Microsystems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro"; static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC"; #endif #include __FBSDID("$FreeBSD$"); /* * svc_vc.c, Server side for Connection Oriented based RPC. * * Actually implements two flavors of transporter - * a tcp rendezvouser (a listner and connection establisher) * and a record/tcp stream. */ #include "namespace.h" #include "reentrant.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "rpc_com.h" #include "mt_misc.h" #include "un-namespace.h" static SVCXPRT *makefd_xprt(int, u_int, u_int); static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *); static enum xprt_stat rendezvous_stat(SVCXPRT *); static void svc_vc_destroy(SVCXPRT *); static void __svc_vc_dodestroy (SVCXPRT *); static int read_vc(void *, void *, int); static int write_vc(void *, void *, int); static enum xprt_stat svc_vc_stat(SVCXPRT *); static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *); static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *); static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *); static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *); static void svc_vc_rendezvous_ops(SVCXPRT *); static void svc_vc_ops(SVCXPRT *); static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in); static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq, void *in); struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */ u_int sendsize; u_int recvsize; int maxrec; }; struct cf_conn { /* kept in xprt->xp_p1 for actual connection */ enum xprt_stat strm_stat; u_int32_t x_id; XDR xdrs; char verf_body[MAX_AUTH_BYTES]; u_int sendsize; u_int recvsize; int maxrec; bool_t nonblock; struct timeval last_recv_time; }; /* * Usage: * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size); * * Creates, registers, and returns a (rpc) tcp based transporter. * Once *xprt is initialized, it is registered as a transporter * see (svc.h, xprt_register). This routine returns * a NULL if a problem occurred. * * The filedescriptor passed in is expected to refer to a bound, but * not yet connected socket. * * Since streams do buffered io similar to stdio, the caller can specify * how big the send and receive buffers are via the second and third parms; * 0 => use the system default. */ SVCXPRT * svc_vc_create(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { SVCXPRT *xprt; struct cf_rendezvous *r = NULL; struct __rpc_sockinfo si; struct sockaddr_storage sslocal; socklen_t slen; if (!__rpc_fd2sockinfo(fd, &si)) return NULL; r = mem_alloc(sizeof(*r)); if (r == NULL) { warnx("svc_vc_create: out of memory"); goto cleanup_svc_vc_create; } r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize); r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize); r->maxrec = __svc_maxrec; xprt = svc_xprt_alloc(); if (xprt == NULL) { warnx("svc_vc_create: out of memory"); goto cleanup_svc_vc_create; } xprt->xp_p1 = r; xprt->xp_verf = _null_auth; svc_vc_rendezvous_ops(xprt); xprt->xp_port = (u_short)-1; /* It is the rendezvouser */ xprt->xp_fd = fd; slen = sizeof (struct sockaddr_storage); if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) { warnx("svc_vc_create: could not retrieve local addr"); goto cleanup_svc_vc_create; } xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len; xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len); if (xprt->xp_ltaddr.buf == NULL) { warnx("svc_vc_create: no mem for local addr"); goto cleanup_svc_vc_create; } memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len); xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage); xprt_register(xprt); return (xprt); cleanup_svc_vc_create: if (xprt) mem_free(xprt, sizeof(*xprt)); if (r != NULL) mem_free(r, sizeof(*r)); return (NULL); } /* * Like svtcp_create(), except the routine takes any *open* UNIX file * descriptor as its first input. */ SVCXPRT * svc_fd_create(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { struct sockaddr_storage ss; socklen_t slen; SVCXPRT *ret; assert(fd != -1); ret = makefd_xprt(fd, sendsize, recvsize); if (ret == NULL) return NULL; slen = sizeof (struct sockaddr_storage); if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { warnx("svc_fd_create: could not retrieve local addr"); goto freedata; } ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len; ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len); if (ret->xp_ltaddr.buf == NULL) { warnx("svc_fd_create: no mem for local addr"); goto freedata; } memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len); slen = sizeof (struct sockaddr_storage); if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { warnx("svc_fd_create: could not retrieve remote addr"); goto freedata; } ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len; ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len); if (ret->xp_rtaddr.buf == NULL) { warnx("svc_fd_create: no mem for local addr"); goto freedata; } memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len); #ifdef PORTMAP if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) { ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf; ret->xp_addrlen = sizeof (struct sockaddr_in); } #endif /* PORTMAP */ return ret; freedata: if (ret->xp_ltaddr.buf != NULL) mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen); return NULL; } static SVCXPRT * makefd_xprt(fd, sendsize, recvsize) int fd; u_int sendsize; u_int recvsize; { SVCXPRT *xprt; struct cf_conn *cd; const char *netid; struct __rpc_sockinfo si; assert(fd != -1); xprt = svc_xprt_alloc(); if (xprt == NULL) { warnx("svc_vc: makefd_xprt: out of memory"); goto done; } cd = mem_alloc(sizeof(struct cf_conn)); if (cd == NULL) { warnx("svc_tcp: makefd_xprt: out of memory"); svc_xprt_free(xprt); xprt = NULL; goto done; } cd->strm_stat = XPRT_IDLE; xdrrec_create(&(cd->xdrs), sendsize, recvsize, xprt, read_vc, write_vc); xprt->xp_p1 = cd; xprt->xp_verf.oa_base = cd->verf_body; svc_vc_ops(xprt); /* truely deals with calls */ xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ xprt->xp_fd = fd; if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid)) xprt->xp_netid = strdup(netid); xprt_register(xprt); done: return (xprt); } /*ARGSUSED*/ static bool_t rendezvous_request(xprt, msg) SVCXPRT *xprt; struct rpc_msg *msg; { int sock, flags; struct cf_rendezvous *r; struct cf_conn *cd; struct sockaddr_storage addr; socklen_t len; struct __rpc_sockinfo si; SVCXPRT *newxprt; fd_set cleanfds; assert(xprt != NULL); assert(msg != NULL); r = (struct cf_rendezvous *)xprt->xp_p1; again: len = sizeof addr; if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr, &len)) < 0) { if (errno == EINTR) goto again; /* * Clean out the most idle file descriptor when we're * running out. */ if (errno == EMFILE || errno == ENFILE) { cleanfds = svc_fdset; __svc_clean_idle(&cleanfds, 0, FALSE); goto again; } return (FALSE); } /* * make a new transporter (re-uses xprt) */ newxprt = makefd_xprt(sock, r->sendsize, r->recvsize); newxprt->xp_rtaddr.buf = mem_alloc(len); if (newxprt->xp_rtaddr.buf == NULL) return (FALSE); memcpy(newxprt->xp_rtaddr.buf, &addr, len); newxprt->xp_rtaddr.len = len; #ifdef PORTMAP if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) { newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf; newxprt->xp_addrlen = sizeof (struct sockaddr_in); } #endif /* PORTMAP */ if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) { len = 1; /* XXX fvdl - is this useful? */ _setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len)); } cd = (struct cf_conn *)newxprt->xp_p1; cd->recvsize = r->recvsize; cd->sendsize = r->sendsize; cd->maxrec = r->maxrec; if (cd->maxrec != 0) { flags = _fcntl(sock, F_GETFL, 0); if (flags == -1) return (FALSE); if (_fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) return (FALSE); if (cd->recvsize > cd->maxrec) cd->recvsize = cd->maxrec; cd->nonblock = TRUE; __xdrrec_setnonblock(&cd->xdrs, cd->maxrec); } else cd->nonblock = FALSE; gettimeofday(&cd->last_recv_time, NULL); return (FALSE); /* there is never an rpc msg to be processed */ } /*ARGSUSED*/ static enum xprt_stat rendezvous_stat(xprt) SVCXPRT *xprt; { return (XPRT_IDLE); } static void svc_vc_destroy(xprt) SVCXPRT *xprt; { assert(xprt != NULL); xprt_unregister(xprt); __svc_vc_dodestroy(xprt); } static void __svc_vc_dodestroy(xprt) SVCXPRT *xprt; { struct cf_conn *cd; struct cf_rendezvous *r; cd = (struct cf_conn *)xprt->xp_p1; if (xprt->xp_fd != RPC_ANYFD) (void)_close(xprt->xp_fd); if (xprt->xp_port != 0) { /* a rendezvouser socket */ r = (struct cf_rendezvous *)xprt->xp_p1; mem_free(r, sizeof (struct cf_rendezvous)); xprt->xp_port = 0; } else { /* an actual connection socket */ XDR_DESTROY(&(cd->xdrs)); mem_free(cd, sizeof(struct cf_conn)); } if (xprt->xp_rtaddr.buf) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); if (xprt->xp_ltaddr.buf) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); - if (xprt->xp_tp) - free(xprt->xp_tp); - if (xprt->xp_netid) - free(xprt->xp_netid); + free(xprt->xp_tp); + free(xprt->xp_netid); svc_xprt_free(xprt); } /*ARGSUSED*/ static bool_t svc_vc_control(xprt, rq, in) SVCXPRT *xprt; const u_int rq; void *in; { return (FALSE); } static bool_t svc_vc_rendezvous_control(xprt, rq, in) SVCXPRT *xprt; const u_int rq; void *in; { struct cf_rendezvous *cfp; cfp = (struct cf_rendezvous *)xprt->xp_p1; if (cfp == NULL) return (FALSE); switch (rq) { case SVCGET_CONNMAXREC: *(int *)in = cfp->maxrec; break; case SVCSET_CONNMAXREC: cfp->maxrec = *(int *)in; break; default: return (FALSE); } return (TRUE); } /* * reads data from the tcp or uip connection. * any error is fatal and the connection is closed. * (And a read of zero bytes is a half closed stream => error.) * All read operations timeout after 35 seconds. A timeout is * fatal for the connection. */ static int read_vc(xprtp, buf, len) void *xprtp; void *buf; int len; { SVCXPRT *xprt; int sock; int milliseconds = 35 * 1000; struct pollfd pollfd; struct cf_conn *cfp; xprt = (SVCXPRT *)xprtp; assert(xprt != NULL); sock = xprt->xp_fd; cfp = (struct cf_conn *)xprt->xp_p1; if (cfp->nonblock) { len = _read(sock, buf, (size_t)len); if (len < 0) { if (errno == EAGAIN) len = 0; else goto fatal_err; } if (len != 0) gettimeofday(&cfp->last_recv_time, NULL); return len; } do { pollfd.fd = sock; pollfd.events = POLLIN; pollfd.revents = 0; switch (_poll(&pollfd, 1, milliseconds)) { case -1: if (errno == EINTR) continue; /*FALLTHROUGH*/ case 0: goto fatal_err; default: break; } } while ((pollfd.revents & POLLIN) == 0); if ((len = _read(sock, buf, (size_t)len)) > 0) { gettimeofday(&cfp->last_recv_time, NULL); return (len); } fatal_err: ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; return (-1); } /* * writes data to the tcp connection. * Any error is fatal and the connection is closed. */ static int write_vc(xprtp, buf, len) void *xprtp; void *buf; int len; { SVCXPRT *xprt; int i, cnt; struct cf_conn *cd; struct timeval tv0, tv1; xprt = (SVCXPRT *)xprtp; assert(xprt != NULL); cd = (struct cf_conn *)xprt->xp_p1; if (cd->nonblock) gettimeofday(&tv0, NULL); for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) { i = _write(xprt->xp_fd, buf, (size_t)cnt); if (i < 0) { if (errno != EAGAIN || !cd->nonblock) { cd->strm_stat = XPRT_DIED; return (-1); } if (cd->nonblock) { /* * For non-blocking connections, do not * take more than 2 seconds writing the * data out. * * XXX 2 is an arbitrary amount. */ gettimeofday(&tv1, NULL); if (tv1.tv_sec - tv0.tv_sec >= 2) { cd->strm_stat = XPRT_DIED; return (-1); } } i = 0; } } return (len); } static enum xprt_stat svc_vc_stat(xprt) SVCXPRT *xprt; { struct cf_conn *cd; assert(xprt != NULL); cd = (struct cf_conn *)(xprt->xp_p1); if (cd->strm_stat == XPRT_DIED) return (XPRT_DIED); if (! xdrrec_eof(&(cd->xdrs))) return (XPRT_MOREREQS); return (XPRT_IDLE); } static bool_t svc_vc_recv(xprt, msg) SVCXPRT *xprt; struct rpc_msg *msg; { struct cf_conn *cd; XDR *xdrs; assert(xprt != NULL); assert(msg != NULL); cd = (struct cf_conn *)(xprt->xp_p1); xdrs = &(cd->xdrs); if (cd->nonblock) { if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE)) return FALSE; } else { (void)xdrrec_skiprecord(xdrs); } xdrs->x_op = XDR_DECODE; if (xdr_callmsg(xdrs, msg)) { cd->x_id = msg->rm_xid; return (TRUE); } cd->strm_stat = XPRT_DIED; return (FALSE); } static bool_t svc_vc_getargs(xprt, xdr_args, args_ptr) SVCXPRT *xprt; xdrproc_t xdr_args; void *args_ptr; { struct cf_conn *cd; assert(xprt != NULL); cd = (struct cf_conn *)(xprt->xp_p1); return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), &cd->xdrs, xdr_args, args_ptr)); } static bool_t svc_vc_freeargs(xprt, xdr_args, args_ptr) SVCXPRT *xprt; xdrproc_t xdr_args; void *args_ptr; { XDR *xdrs; assert(xprt != NULL); /* args_ptr may be NULL */ xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs); xdrs->x_op = XDR_FREE; return ((*xdr_args)(xdrs, args_ptr)); } static bool_t svc_vc_reply(xprt, msg) SVCXPRT *xprt; struct rpc_msg *msg; { struct cf_conn *cd; XDR *xdrs; bool_t rstat; xdrproc_t xdr_proc; caddr_t xdr_where; u_int pos; assert(xprt != NULL); assert(msg != NULL); cd = (struct cf_conn *)(xprt->xp_p1); xdrs = &(cd->xdrs); xdrs->x_op = XDR_ENCODE; msg->rm_xid = cd->x_id; rstat = TRUE; if (msg->rm_reply.rp_stat == MSG_ACCEPTED && msg->rm_reply.rp_acpt.ar_stat == SUCCESS) { xdr_proc = msg->acpted_rply.ar_results.proc; xdr_where = msg->acpted_rply.ar_results.where; msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; msg->acpted_rply.ar_results.where = NULL; pos = XDR_GETPOS(xdrs); if (!xdr_replymsg(xdrs, msg) || !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where)) { XDR_SETPOS(xdrs, pos); rstat = FALSE; } } else { rstat = xdr_replymsg(xdrs, msg); } if (rstat) (void)xdrrec_endofrecord(xdrs, TRUE); return (rstat); } static void svc_vc_ops(xprt) SVCXPRT *xprt; { static struct xp_ops ops; static struct xp_ops2 ops2; /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */ mutex_lock(&ops_lock); if (ops.xp_recv == NULL) { ops.xp_recv = svc_vc_recv; ops.xp_stat = svc_vc_stat; ops.xp_getargs = svc_vc_getargs; ops.xp_reply = svc_vc_reply; ops.xp_freeargs = svc_vc_freeargs; ops.xp_destroy = svc_vc_destroy; ops2.xp_control = svc_vc_control; } xprt->xp_ops = &ops; xprt->xp_ops2 = &ops2; mutex_unlock(&ops_lock); } static void svc_vc_rendezvous_ops(xprt) SVCXPRT *xprt; { static struct xp_ops ops; static struct xp_ops2 ops2; mutex_lock(&ops_lock); if (ops.xp_recv == NULL) { ops.xp_recv = rendezvous_request; ops.xp_stat = rendezvous_stat; ops.xp_getargs = (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort; ops.xp_reply = (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort; ops.xp_freeargs = (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort, ops.xp_destroy = svc_vc_destroy; ops2.xp_control = svc_vc_rendezvous_control; } xprt->xp_ops = &ops; xprt->xp_ops2 = &ops2; mutex_unlock(&ops_lock); } /* * Get the effective UID of the sending process. Used by rpcbind, keyserv * and rpc.yppasswdd on AF_LOCAL. */ int __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) { int sock, ret; gid_t egid; uid_t euid; struct sockaddr *sa; sock = transp->xp_fd; sa = (struct sockaddr *)transp->xp_rtaddr.buf; if (sa->sa_family == AF_LOCAL) { ret = getpeereid(sock, &euid, &egid); if (ret == 0) *uid = euid; return (ret); } else return (-1); } /* * Destroy xprts that have not have had any activity in 'timeout' seconds. * If 'cleanblock' is true, blocking connections (the default) are also * cleaned. If timeout is 0, the least active connection is picked. */ bool_t __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock) { int i, ncleaned; SVCXPRT *xprt, *least_active; struct timeval tv, tdiff, tmax; struct cf_conn *cd; gettimeofday(&tv, NULL); tmax.tv_sec = tmax.tv_usec = 0; least_active = NULL; rwlock_wrlock(&svc_fd_lock); for (i = ncleaned = 0; i <= svc_maxfd; i++) { if (FD_ISSET(i, fds)) { xprt = __svc_xports[i]; if (xprt == NULL || xprt->xp_ops == NULL || xprt->xp_ops->xp_recv != svc_vc_recv) continue; cd = (struct cf_conn *)xprt->xp_p1; if (!cleanblock && !cd->nonblock) continue; if (timeout == 0) { timersub(&tv, &cd->last_recv_time, &tdiff); if (timercmp(&tdiff, &tmax, >)) { tmax = tdiff; least_active = xprt; } continue; } if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) { __xprt_unregister_unlocked(xprt); __svc_vc_dodestroy(xprt); ncleaned++; } } } if (timeout == 0 && least_active != NULL) { __xprt_unregister_unlocked(least_active); __svc_vc_dodestroy(least_active); ncleaned++; } rwlock_unlock(&svc_fd_lock); return ncleaned > 0 ? TRUE : FALSE; } Index: stable/10 =================================================================== --- stable/10 (revision 290898) +++ stable/10 (revision 290899) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r290253