X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=support%2Fnfs%2Fgetport.c;h=2b80dcea5960a35bc439ad4b97122ba93ef4a67c;hb=93b7d1f16db674827fc85e70bff1745f57fb6a60;hp=023c79a4b0153e2e1a3fbd71caad110760a1e8de;hpb=c8e40bc4a1a9f8e7c91ca909d553172b7a684a18;p=nfs-utils.git diff --git a/support/nfs/getport.c b/support/nfs/getport.c index 023c79a..2b80dce 100644 --- a/support/nfs/getport.c +++ b/support/nfs/getport.c @@ -40,9 +40,9 @@ #include #include -#ifdef HAVE_TIRPC_NETCONFIG_H -#include -#include +#ifdef HAVE_LIBTIRPC +#include +#include #endif #include "nfsrpc.h" @@ -53,25 +53,17 @@ * Rpcbind's local socket service does not seem to be working. * Disable this logic for now. */ -#ifdef HAVE_XDR_RPCB +#ifdef HAVE_LIBTIRPC #undef NFS_GP_LOCAL -#else /* HAVE_XDR_RPCB */ +#else /* !HAVE_LIBTIRPC */ #undef NFS_GP_LOCAL -#endif /* HAVE_XDR_RPCB */ +#endif /* !HAVE_LIBTIRPC */ -#ifdef HAVE_XDR_RPCB -const static rpcvers_t default_rpcb_version = RPCBVERS_4; -#else -const static rpcvers_t default_rpcb_version = PMAPVERS; -#endif - -static const char *nfs_gp_rpcb_pgmtbl[] = { - "rpcbind", - "portmap", - "portmapper", - "sunrpc", - NULL, -}; +#ifdef HAVE_LIBTIRPC +static const rpcvers_t default_rpcb_version = RPCBVERS_4; +#else /* !HAVE_LIBTIRPC */ +static const rpcvers_t default_rpcb_version = PMAPVERS; +#endif /* !HAVE_LIBTIRPC */ #ifdef HAVE_DECL_AI_ADDRCONFIG /* @@ -127,39 +119,6 @@ static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen) } #endif -/* - * Discover the port number that should be used to contact an - * rpcbind service. This will detect if the port has a local - * value that may have been set in /etc/services. - * - * NB: s_port is already in network byte order. - * - * Returns network byte-order port number of rpcbind service - * on this system. - */ -static in_port_t nfs_gp_get_rpcb_port(const unsigned short protocol) -{ - struct protoent *proto; - - proto = getprotobynumber((int)protocol); - if (proto != NULL) { - struct servent *entry; - - entry = getservbyname("rpcbind", proto->p_name); - if (entry != NULL) - return (in_port_t)entry->s_port; - - entry = getservbyname("portmapper", proto->p_name); - if (entry != NULL) - return (in_port_t)entry->s_port; - - entry = getservbyname("sunrpc", proto->p_name); - if (entry != NULL) - return (in_port_t)entry->s_port; - } - return htons((uint16_t)PMAPPORT); -} - /* * Plant port number in @sap. @port is already in network byte order. */ @@ -178,6 +137,61 @@ static void nfs_gp_set_port(struct sockaddr *sap, const in_port_t port) } } +/* + * Look up a network service in /etc/services and return the + * network-order port number of that service. + */ +static in_port_t nfs_gp_getservbyname(const char *service, + const unsigned short protocol) +{ + const struct addrinfo gai_hint = { + .ai_family = AF_INET, + .ai_protocol = protocol, + .ai_flags = AI_PASSIVE, + }; + struct addrinfo *gai_results; + const struct sockaddr_in *sin; + in_port_t port; + + if (getaddrinfo(NULL, service, &gai_hint, &gai_results) != 0) + return 0; + + sin = (const struct sockaddr_in *)gai_results->ai_addr; + port = sin->sin_port; + + freeaddrinfo(gai_results); + return port; +} + +/* + * Discover the port number that should be used to contact an + * rpcbind service. This will detect if the port has a local + * value that may have been set in /etc/services. + * + * Returns network byte-order port number of rpcbind service + * on this system. + */ +static in_port_t nfs_gp_get_rpcb_port(const unsigned short protocol) +{ + static const char *rpcb_netnametbl[] = { + "rpcbind", + "portmapper", + "sunrpc", + NULL, + }; + unsigned int i; + + for (i = 0; rpcb_netnametbl[i] != NULL; i++) { + in_port_t port; + + port = nfs_gp_getservbyname(rpcb_netnametbl[i], protocol); + if (port != 0) + return port; + } + + return (in_port_t)htons((uint16_t)PMAPPORT); +} + /* * Set up an RPC client for communicating with an rpcbind daemon at * @sap over @transport with protocol version @version. @@ -187,20 +201,23 @@ static void nfs_gp_set_port(struct sockaddr *sap, const in_port_t port) * client. Otherwise returns NULL, and rpc_createerr.cf_stat is set to * reflect the error. */ -static CLIENT *nfs_gp_get_rpcbclient(const struct sockaddr *sap, +static CLIENT *nfs_gp_get_rpcbclient(struct sockaddr *sap, const socklen_t salen, const unsigned short transport, const rpcvers_t version, struct timeval *timeout) { - struct sockaddr_storage address; - struct sockaddr *saddr = (struct sockaddr *)&address; - rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, nfs_gp_rpcb_pgmtbl); - - memcpy(saddr, sap, (size_t)salen); - nfs_gp_set_port(saddr, nfs_gp_get_rpcb_port(transport)); + static const char *rpcb_pgmtbl[] = { + "rpcbind", + "portmap", + "portmapper", + "sunrpc", + NULL, + }; + rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, rpcb_pgmtbl); - return nfs_get_rpcclient(saddr, salen, transport, rpcb_prog, + nfs_gp_set_port(sap, nfs_gp_get_rpcb_port(transport)); + return nfs_get_rpcclient(sap, salen, transport, rpcb_prog, version, timeout); } @@ -221,7 +238,7 @@ static CLIENT *nfs_gp_get_rpcbclient(const struct sockaddr *sap, * Returns a '\0'-terminated string if successful; otherwise NULL. * rpc_createerr.cf_stat is set to reflect the error. */ -#ifdef HAVE_XDR_RPCB +#ifdef HAVE_LIBTIRPC static char *nfs_gp_get_netid(const sa_family_t family, const unsigned short protocol) @@ -269,7 +286,7 @@ out: return NULL; } -#endif /* HAVE_XDR_RPCB */ +#endif /* HAVE_LIBTIRPC */ /* * Extract a port number from a universal address, and terminate the @@ -432,15 +449,11 @@ static int nfs_gp_ping(CLIENT *client, struct timeval timeout) return (int)(status == RPC_SUCCESS); } -#ifdef HAVE_XDR_RPCB +#ifdef HAVE_LIBTIRPC /* * Initialize the rpcb argument for a GETADDR request. * - * The rpcbind daemon ignores the parms.r_owner field in GETADDR - * requests, but we plant an eye-catcher to help distinguish these - * requests in network traces. - * * Returns 1 if successful, and caller must free strings pointed * to by r_netid and r_addr; otherwise 0. */ @@ -468,7 +481,7 @@ static int nfs_gp_init_rpcb_parms(const struct sockaddr *sap, parms->r_vers = version; parms->r_netid = netid; parms->r_addr = addr; - parms->r_owner = "nfs-utils"; /* eye-catcher */ + parms->r_owner = ""; return 1; } @@ -544,7 +557,7 @@ static unsigned short nfs_gp_rpcb_getaddr(CLIENT *client, return port; } -#endif /* HAVE_XDR_RPCB */ +#endif /* HAVE_LIBTIRPC */ /* * Try GETPORT request via rpcbind version 2. @@ -574,7 +587,7 @@ static unsigned long nfs_gp_pmap_getport(CLIENT *client, return port; } -#ifdef HAVE_XDR_RPCB +#ifdef HAVE_LIBTIRPC static unsigned short nfs_gp_getport_rpcb(CLIENT *client, const struct sockaddr *sap, @@ -596,7 +609,7 @@ static unsigned short nfs_gp_getport_rpcb(CLIENT *client, return port; } -#endif /* HAVE_XDR_RPCB */ +#endif /* HAVE_LIBTIRPC */ static unsigned long nfs_gp_getport_pmap(CLIENT *client, const rpcprog_t program, @@ -631,11 +644,11 @@ static unsigned short nfs_gp_getport(CLIENT *client, struct timeval timeout) { switch (sap->sa_family) { -#ifdef HAVE_XDR_RPCB +#ifdef HAVE_LIBTIRPC case AF_INET6: return nfs_gp_getport_rpcb(client, sap, salen, program, version, protocol, timeout); -#endif /* HAVE_XDR_RPCB */ +#endif /* HAVE_LIBTIRPC */ case AF_INET: return nfs_gp_getport_pmap(client, program, version, protocol, timeout); @@ -646,7 +659,7 @@ static unsigned short nfs_gp_getport(CLIENT *client, } /** - * nfs_rcp_ping - Determine if RPC service is responding to requests + * nfs_rpc_ping - Determine if RPC service is responding to requests * @sap: pointer to address of server to query (port is already filled in) * @salen: length of server address * @program: requested RPC program number @@ -661,6 +674,8 @@ int nfs_rpc_ping(const struct sockaddr *sap, const socklen_t salen, const rpcprog_t program, const rpcvers_t version, const unsigned short protocol, const struct timeval *timeout) { + struct sockaddr_storage address; + struct sockaddr *saddr = (struct sockaddr *)&address; CLIENT *client; struct timeval tout = { -1, 0 }; int result = 0; @@ -668,7 +683,9 @@ int nfs_rpc_ping(const struct sockaddr *sap, const socklen_t salen, if (timeout != NULL) tout = *timeout; - client = nfs_get_rpcclient(sap, salen, protocol, program, version, &tout); + memcpy(saddr, sap, (size_t)salen); + client = nfs_get_rpcclient(saddr, salen, protocol, + program, version, &tout); if (client != NULL) { result = nfs_gp_ping(client, tout); CLNT_DESTROY(client); @@ -723,14 +740,17 @@ unsigned short nfs_getport(const struct sockaddr *sap, const rpcvers_t version, const unsigned short protocol) { + struct sockaddr_storage address; + struct sockaddr *saddr = (struct sockaddr *)&address; struct timeval timeout = { -1, 0 }; unsigned short port = 0; CLIENT *client; - client = nfs_gp_get_rpcbclient(sap, salen, protocol, + memcpy(saddr, sap, (size_t)salen); + client = nfs_gp_get_rpcbclient(saddr, salen, protocol, default_rpcb_version, &timeout); if (client != NULL) { - port = nfs_gp_getport(client, sap, salen, program, + port = nfs_gp_getport(client, saddr, salen, program, version, protocol, timeout); CLNT_DESTROY(client); } @@ -901,7 +921,7 @@ unsigned short nfs_getlocalport(const rpcprot_t program, * address of the same address family. In this way an RPC server can * advertise via rpcbind that it does not support AF_INET6. */ -#ifdef HAVE_XDR_RPCB +#ifdef HAVE_LIBTIRPC unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap, const socklen_t salen, @@ -913,6 +933,8 @@ unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap, const unsigned short protocol, const struct timeval *timeout) { + struct sockaddr_storage address; + struct sockaddr *saddr = (struct sockaddr *)&address; CLIENT *client; struct rpcb parms; struct timeval tout = { -1, 0 }; @@ -921,7 +943,9 @@ unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap, if (timeout != NULL) tout = *timeout; - client = nfs_gp_get_rpcbclient(sap, salen, transport, RPCBVERS_4, &tout); + memcpy(saddr, sap, (size_t)salen); + client = nfs_gp_get_rpcbclient(saddr, salen, transport, + RPCBVERS_4, &tout); if (client != NULL) { if (nfs_gp_init_rpcb_parms(addr, addrlen, program, version, protocol, &parms) != 0) { @@ -934,7 +958,7 @@ unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap, return port; } -#else /* HAVE_XDR_RPCB */ +#else /* !HAVE_LIBTIRPC */ unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap, const socklen_t salen, @@ -950,7 +974,7 @@ unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap, return 0; } -#endif /* HAVE_XDR_RPCB */ +#endif /* !HAVE_LIBTIRPC */ /** * nfs_pmap_getport - query rpcbind via the portmap protocol (rpcbindv2) @@ -987,6 +1011,8 @@ unsigned long nfs_pmap_getport(const struct sockaddr_in *sin, const unsigned long protocol, const struct timeval *timeout) { + struct sockaddr_in address; + struct sockaddr *saddr = (struct sockaddr *)&address; CLIENT *client; struct pmap parms = { .pm_prog = program, @@ -999,8 +1025,8 @@ unsigned long nfs_pmap_getport(const struct sockaddr_in *sin, if (timeout != NULL) tout = *timeout; - client = nfs_gp_get_rpcbclient((struct sockaddr *)sin, - (socklen_t)sizeof(*sin), + memcpy(saddr, sin, sizeof(address)); + client = nfs_gp_get_rpcbclient(saddr, (socklen_t)sizeof(*sin), transport, PMAPVERS, &tout); if (client != NULL) { port = nfs_gp_pmap_getport(client, &parms, tout);