X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=support%2Fexport%2Fclient.c;h=b1a3a09d680d32b2f5e70aaf3c847bf4b1b0b18f;hp=419a4a7d930f6999e8a0f26dc397fce2b3d5f899;hb=abdc32b6af6f38a741a481aeefb5623916152498;hpb=3455138100064d0213b124c72453accde2276be5 diff --git a/support/export/client.c b/support/export/client.c index 419a4a7..b1a3a09 100644 --- a/support/export/client.c +++ b/support/export/client.c @@ -17,7 +17,7 @@ #include #include #include -#include "xmalloc.h" + #include "misc.h" #include "nfslib.h" #include "exportfs.h" @@ -30,57 +30,138 @@ extern int innetgr(char *netgr, char *host, char *, char *); #endif static char *add_name(char *old, const char *add); -static void client_init(nfs_client *clp, const char *hname, - struct hostent *hp); nfs_client *clientlist[MCL_MAXTYPES] = { NULL, }; -/* if canonical is set, then we *know* this is already a canonical name - * so hostname lookup is avoided. - * This is used when reading /proc/fs/nfs/exports +static void +init_addrlist(nfs_client *clp, const struct addrinfo *ai) +{ + int i; + + if (ai == NULL) + return; + + for (i = 0; (ai != NULL) && (i < NFSCLNT_ADDRMAX); i++) { + set_addrlist(clp, i, ai->ai_addr); + ai = ai->ai_next; + } + + clp->m_naddr = i; +} + +static void +client_free(nfs_client *clp) +{ + free(clp->m_hostname); + free(clp); +} + +static int +init_netmask(nfs_client *clp, const char *slash) +{ + struct sockaddr_in sin = { + .sin_family = AF_INET, + }; + + if (strchr(slash + 1, '.') != NULL) + sin.sin_addr.s_addr = inet_addr(slash + 1); + else { + int prefixlen = atoi(slash + 1); + if (0 < prefixlen && prefixlen <= 32) + sin.sin_addr.s_addr = + htonl((uint32_t)~0 << (32 - prefixlen)); + else + goto out_badprefix; + } + + set_addrlist_in(clp, 1, &sin); + return 1; + +out_badprefix: + xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname); + return 0; +} + +static int +init_subnetwork(nfs_client *clp) +{ + struct sockaddr_in sin = { + .sin_family = AF_INET, + }; + static char slash32[] = "/32"; + char *cp; + + cp = strchr(clp->m_hostname, '/'); + if (cp == NULL) + cp = slash32; + + *cp = '\0'; + sin.sin_addr.s_addr = inet_addr(clp->m_hostname); + set_addrlist_in(clp, 0, &sin); + *cp = '/'; + + return init_netmask(clp, cp); +} + +static int +client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai) +{ + clp->m_hostname = strdup(hname); + if (clp->m_hostname == NULL) + return 0; + + clp->m_exported = 0; + clp->m_count = 0; + clp->m_naddr = 0; + + if (clp->m_type == MCL_SUBNETWORK) + return init_subnetwork(clp); + + init_addrlist(clp, ai); + return 1; +} + +static void +client_add(nfs_client *clp) +{ + nfs_client **cpp; + + cpp = &clientlist[clp->m_type]; + while (*cpp != NULL) + cpp = &((*cpp)->m_next); + clp->m_next = NULL; + *cpp = clp; +} + +/** + * client_lookup - look for @hname in our list of cached nfs_clients + * @hname: '\0'-terminated ASCII string containing hostname to look for + * @canonical: if set, @hname is known to be canonical DNS name + * + * Returns pointer to a matching or freshly created nfs_client. NULL + * is returned if some problem occurs. */ nfs_client * client_lookup(char *hname, int canonical) { nfs_client *clp = NULL; int htype; - struct hostent *hp = NULL; + struct addrinfo *ai = NULL; htype = client_gettype(hname); if (htype == MCL_FQDN && !canonical) { - struct hostent *hp2; - hp = gethostbyname(hname); - if (hp == NULL || hp->h_addrtype != AF_INET) { - xlog(L_ERROR, "%s has non-inet addr", hname); - return NULL; + ai = host_addrinfo(hname); + if (!ai) { + xlog(L_ERROR, "Failed to resolve %s", hname); + goto out; } - /* make sure we have canonical name */ - hp2 = hostent_dup(hp); - hp = gethostbyaddr(hp2->h_addr, hp2->h_length, - hp2->h_addrtype); - if (hp) { - hp = hostent_dup(hp); - /* but now we might not have all addresses... */ - if (hp2->h_addr_list[1]) { - struct hostent *hp3 = - gethostbyname(hp->h_name); - if (hp3) { - free(hp); - hp = hostent_dup(hp3); - } - } - free(hp2); - } else - hp = hp2; - - hname = (char *) hp->h_name; + hname = ai->ai_canonname; - for (clp = clientlist[htype]; clp; clp = clp->m_next) { - if (client_check(clp, hp)) + for (clp = clientlist[htype]; clp; clp = clp->m_next) + if (client_check(clp, ai)) break; - } } else { for (clp = clientlist[htype]; clp; clp = clp->m_next) { if (strcasecmp(hname, clp->m_hostname)==0) @@ -88,106 +169,60 @@ client_lookup(char *hname, int canonical) } } - if (!clp) { - clp = (nfs_client *) xmalloc(sizeof(*clp)); - memset(clp, 0, sizeof(*clp)); + if (clp == NULL) { + clp = calloc(1, sizeof(*clp)); + if (clp == NULL) + goto out; clp->m_type = htype; - client_init(clp, hname, NULL); + if (!client_init(clp, hname, NULL)) { + client_free(clp); + clp = NULL; + goto out; + } client_add(clp); } - if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) { - char **ap = hp->h_addr_list; - int i; - - for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) - clp->m_addrlist[i] = *(struct in_addr *)*ap; - clp->m_naddr = i; - } - - if (hp) - free (hp); + if (htype == MCL_FQDN && clp->m_naddr == 0) + init_addrlist(clp, ai); +out: + freeaddrinfo(ai); return clp; } +/** + * client_dup - create a copy of an nfs_client + * @clp: pointer to nfs_client to copy + * @ai: pointer to addrinfo used to initialize the new client's addrlist + * + * Returns a dynamically allocated nfs_client if successful, or + * NULL if some problem occurs. Caller must free the returned + * nfs_client with free(3). + */ nfs_client * -client_dup(nfs_client *clp, struct hostent *hp) +client_dup(const nfs_client *clp, const struct addrinfo *ai) { nfs_client *new; - new = (nfs_client *) xmalloc(sizeof(*new)); + new = (nfs_client *)malloc(sizeof(*new)); + if (new == NULL) + return NULL; memcpy(new, clp, sizeof(*new)); new->m_type = MCL_FQDN; new->m_hostname = NULL; - client_init(new, (char *) hp->h_name, hp); + if (!client_init(new, ai->ai_canonname, ai)) { + client_free(new); + return NULL; + } client_add(new); return new; } -static void -client_init(nfs_client *clp, const char *hname, struct hostent *hp) -{ - xfree(clp->m_hostname); - if (hp) - clp->m_hostname = xstrdup(hp->h_name); - else - clp->m_hostname = xstrdup(hname); - - clp->m_exported = 0; - clp->m_count = 0; - - if (clp->m_type == MCL_SUBNETWORK) { - char *cp = strchr(clp->m_hostname, '/'); - static char slash32[] = "/32"; - - if(!cp) cp = slash32; - *cp = '\0'; - clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname); - if (strchr(cp + 1, '.')) { - clp->m_addrlist[1].s_addr = inet_addr(cp+1); - } - else { - int netmask = atoi(cp + 1); - if (0 < netmask && netmask <= 32) { - clp->m_addrlist[1].s_addr = - htonl ((uint32_t) ~0 << (32 - netmask)); - } - else { - xlog(L_FATAL, "invalid netmask `%s' for %s", - cp + 1, clp->m_hostname); - } - } - *cp = '/'; - clp->m_naddr = 0; - } else if (!hp) { - clp->m_naddr = 0; - } else { - char **ap = hp->h_addr_list; - int i; - - for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) { - clp->m_addrlist[i] = *(struct in_addr *)*ap; - } - clp->m_naddr = i; - } -} - -void -client_add(nfs_client *clp) -{ - nfs_client **cpp; - - if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES) - xlog(L_FATAL, "unknown client type in client_add"); - cpp = clientlist + clp->m_type; - while (*cpp) - cpp = &((*cpp)->m_next); - clp->m_next = NULL; - *cpp = clp; -} - +/** + * client_release - drop a reference to an nfs_client record + * + */ void client_release(nfs_client *clp) { @@ -196,6 +231,10 @@ client_release(nfs_client *clp) clp->m_count--; } +/** + * client_freeall - deallocate all nfs_client records + * + */ void client_freeall(void) { @@ -206,28 +245,34 @@ client_freeall(void) head = clientlist + i; while (*head) { *head = (clp = *head)->m_next; - xfree(clp->m_hostname); - xfree(clp); + client_free(clp); } } } -struct hostent * -client_resolve(struct in_addr addr) +/** + * client_resolve - look up an IP address + * @sap: pointer to socket address to resolve + * + * Returns an addrinfo structure, or NULL if some problem occurred. + * Caller must free the result with freeaddrinfo(3). + */ +struct addrinfo * +client_resolve(const struct sockaddr *sap) { - struct hostent *he = NULL; + struct addrinfo *ai = NULL; if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP]) - he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET); - if (he == NULL) - he = get_hostent((const char*)&addr, sizeof(addr), AF_INET); + ai = host_reliable_addrinfo(sap); + if (ai == NULL) + ai = host_numeric_addrinfo(sap); - return he; + return ai; } /** * client_compose - Make a list of cached hostnames that match an IP address - * @he: pointer to hostent containing IP address information to match + * @ai: pointer to addrinfo containing IP address information to match * * Gather all known client hostnames that match the IP address, and sort * the result into a comma-separated list. @@ -238,7 +283,7 @@ client_resolve(struct in_addr addr) * returned string with free(3). */ char * -client_compose(struct hostent *he) +client_compose(const struct addrinfo *ai) { char *name = NULL; int i; @@ -246,7 +291,7 @@ client_compose(struct hostent *he) for (i = 0 ; i < MCL_MAXTYPES; i++) { nfs_client *clp; for (clp = clientlist[i]; clp ; clp = clp->m_next) { - if (!client_check(clp, he)) + if (!client_check(clp, ai)) continue; name = add_name(name, clp->m_hostname); } @@ -327,126 +372,220 @@ add_name(char *old, const char *add) return new; } +static _Bool +addrs_match4(const struct sockaddr *sa1, const struct sockaddr *sa2) +{ + const struct sockaddr_in *si1 = (const struct sockaddr_in *)sa1; + const struct sockaddr_in *si2 = (const struct sockaddr_in *)sa2; + + return si1->sin_addr.s_addr == si2->sin_addr.s_addr; +} + +static _Bool +addrs_match(const struct sockaddr *sa1, const struct sockaddr *sa2) +{ + if (sa1->sa_family == sa2->sa_family) + switch (sa1->sa_family) { + case AF_INET: + return addrs_match4(sa1, sa2); + } + + return false; +} + /* - * Check each address listed in @hp against each address + * Check each address listed in @ai against each address * stored in @clp. Return 1 if a match is found, otherwise * zero. */ static int -check_fqdn(const nfs_client *clp, const struct hostent *hp) +check_fqdn(const nfs_client *clp, const struct addrinfo *ai) { - struct in_addr addr; - char **ap; int i; - for (ap = hp->h_addr_list; *ap; ap++) { - addr = *(struct in_addr *)*ap; - + for (; ai; ai = ai->ai_next) for (i = 0; i < clp->m_naddr; i++) - if (clp->m_addrlist[i].s_addr == addr.s_addr) + if (addrs_match(ai->ai_addr, get_addrlist(clp, i))) return 1; + + return 0; +} + +static _Bool +mask_match(const uint32_t a, const uint32_t b, const uint32_t m) +{ + return ((a ^ b) & m) == 0; +} + +static int +check_subnet_v4(const struct sockaddr_in *address, + const struct sockaddr_in *mask, const struct addrinfo *ai) +{ + for (; ai; ai = ai->ai_next) { + struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr; + + if (sin->sin_family != AF_INET) + continue; + + if (mask_match(address->sin_addr.s_addr, + sin->sin_addr.s_addr, + mask->sin_addr.s_addr)) + return 1; } return 0; } /* - * Check each address listed in @hp against the subnetwork or + * Check each address listed in @ai against the subnetwork or * host address stored in @clp. Return 1 if an address in @hp * matches the host address stored in @clp, otherwise zero. */ static int -check_subnetwork(const nfs_client *clp, const struct hostent *hp) +check_subnetwork(const nfs_client *clp, const struct addrinfo *ai) { - struct in_addr addr; + switch (get_addrlist(clp, 0)->sa_family) { + case AF_INET: + return check_subnet_v4(get_addrlist_in(clp, 0), + get_addrlist_in(clp, 1), ai); + } + + return 0; +} + +/* + * Check if a wildcard nfs_client record matches the canonical name + * or the aliases of a host. Return 1 if a match is found, otherwise + * zero. + */ +static int +check_wildcard(const nfs_client *clp, const struct addrinfo *ai) +{ + char *cname = clp->m_hostname; + char *hname = ai->ai_canonname; + struct hostent *hp; char **ap; - for (ap = hp->h_addr_list; *ap; ap++) { - addr = *(struct in_addr *)*ap; + if (wildmat(hname, cname)) + return 1; - if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) & - clp->m_addrlist[1].s_addr)) - return 1; + /* See if hname aliases listed in /etc/hosts or nis[+] + * match the requested wildcard */ + hp = gethostbyname(hname); + if (hp != NULL) { + for (ap = hp->h_aliases; *ap; ap++) + if (wildmat(*ap, cname)) + return 1; } + return 0; } /* - * Match a host (given its hostent record) to a client record. This - * is usually called from mountd. + * Check if @ai's hostname or aliases fall in a given netgroup. + * Return 1 if @ai represents a host in the netgroup, otherwise + * zero. */ -int -client_check(nfs_client *clp, struct hostent *hp) -{ - char *hname = (char *) hp->h_name; - char *cname = clp->m_hostname; - char **ap; - - switch (clp->m_type) { - case MCL_FQDN: - return check_fqdn(clp, hp); - case MCL_SUBNETWORK: - return check_subnetwork(clp, hp); - case MCL_WILDCARD: - if (wildmat(hname, cname)) - return 1; - else { - for (ap = hp->h_aliases; *ap; ap++) - if (wildmat(*ap, cname)) - return 1; - } - return 0; - case MCL_NETGROUP: #ifdef HAVE_INNETGR - { - char *dot; - int match, i; - struct hostent *nhp = NULL; - struct sockaddr_in addr; - - /* First, try to match the hostname without - * splitting off the domain */ - if (innetgr(cname+1, hname, NULL, NULL)) - return 1; +static int +check_netgroup(const nfs_client *clp, const struct addrinfo *ai) +{ + const char *netgroup = clp->m_hostname + 1; + const char *hname = ai->ai_canonname; + struct addrinfo *tmp = NULL; + struct hostent *hp; + int i, match; + char *dot; + + match = 0; + + /* First, try to match the hostname without + * splitting off the domain */ + if (innetgr(netgroup, hname, NULL, NULL)) { + match = 1; + goto out; + } - /* try the aliases as well */ - for (i = 0; hp->h_aliases[i]; i++) { - if (innetgr(cname+1, hp->h_aliases[i], NULL, NULL)) - return 1; + /* See if hname aliases listed in /etc/hosts or nis[+] + * match the requested netgroup */ + hp = gethostbyname(hname); + if (hp != NULL) { + for (i = 0; hp->h_aliases[i]; i++) + if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) { + match = 1; + goto out; } + } - /* If hname is ip address convert to FQDN */ - if (inet_aton(hname, &addr.sin_addr) && - (nhp = gethostbyaddr((const char *)&(addr.sin_addr), - sizeof(addr.sin_addr), AF_INET))) { - hname = (char *)nhp->h_name; - if (innetgr(cname+1, hname, NULL, NULL)) - return 1; - } + /* If hname is ip address convert to FQDN */ + tmp = host_pton(hname); + if (tmp != NULL) { + freeaddrinfo(tmp); + if (innetgr(netgroup, hname, NULL, NULL)) { + match = 1; + goto out; + } + } - /* Okay, strip off the domain (if we have one) */ - if ((dot = strchr(hname, '.')) == NULL) - return 0; + /* Okay, strip off the domain (if we have one) */ + dot = strchr(hname, '.'); + if (dot == NULL) + goto out; - *dot = '\0'; - match = innetgr(cname+1, hname, NULL, NULL); - *dot = '.'; + *dot = '\0'; + match = innetgr(netgroup, hname, NULL, NULL); + *dot = '.'; - return match; - } -#else - return 0; -#endif +out: + return match; +} +#else /* !HAVE_INNETGR */ +static int +check_netgroup(__attribute__((unused)) const nfs_client *clp, + __attribute__((unused)) const struct addrinfo *ai) +{ + return 0; +} +#endif /* !HAVE_INNETGR */ + +/** + * client_check - check if IP address information matches a cached nfs_client + * @clp: pointer to a cached nfs_client record + * @ai: pointer to addrinfo to compare it with + * + * Returns 1 if the address information matches the cached nfs_client, + * otherwise zero. + */ +int +client_check(const nfs_client *clp, const struct addrinfo *ai) +{ + switch (clp->m_type) { + case MCL_FQDN: + return check_fqdn(clp, ai); + case MCL_SUBNETWORK: + return check_subnetwork(clp, ai); + case MCL_WILDCARD: + return check_wildcard(clp, ai); + case MCL_NETGROUP: + return check_netgroup(clp, ai); case MCL_ANONYMOUS: return 1; case MCL_GSS: return 0; default: - xlog(L_FATAL, "internal: bad client type %d", clp->m_type); + xlog(D_GENERAL, "%s: unrecognized client type: %d", + __func__, clp->m_type); } return 0; } +/** + * client_gettype - determine type of nfs_client given an identifier + * @ident: '\0'-terminated ASCII string containing a client identifier + * + * Returns the type of nfs_client record that would be used for + * this client. + */ int client_gettype(char *ident) {