2 * support/export/client.c
4 * Maintain list of nfsd clients.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
27 /* netgroup stuff never seems to be defined in any header file. Linux is
30 #if !defined(__GLIBC__) || __GLIBC__ < 2
31 extern int innetgr(char *netgr, char *host, char *, char *);
34 static char *add_name(char *old, const char *add);
36 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
40 init_addrlist(nfs_client *clp, const struct addrinfo *ai)
47 for (i = 0; (ai != NULL) && (i < NFSCLNT_ADDRMAX); i++) {
48 set_addrlist(clp, i, ai->ai_addr);
56 client_free(nfs_client *clp)
58 free(clp->m_hostname);
63 init_netmask4(nfs_client *clp, const char *slash)
65 struct sockaddr_in sin = {
66 .sin_family = AF_INET,
71 * Decide what kind of netmask was specified. If there's
72 * no '/' present, assume the netmask is all ones. If
73 * there is a '/' and at least one '.', look for a spelled-
74 * out netmask. Otherwise, assume it was a prefixlen.
79 unsigned long prefixlen;
81 if (strchr(slash + 1, '.') != NULL) {
82 if (inet_pton(AF_INET, slash + 1,
83 &sin.sin_addr.s_addr) == 0)
85 set_addrlist_in(clp, 1, &sin);
90 prefixlen = strtoul(slash + 1, &endptr, 10);
91 if (*endptr != '\0' && prefixlen != ULONG_MAX &&
97 shift = 32 - (uint32_t)prefixlen;
101 * Now construct the full netmask bitmask in a sockaddr_in,
102 * and plant it in the nfs_client record.
104 sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
105 set_addrlist_in(clp, 1, &sin);
110 xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
114 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
118 #ifdef IPV6_SUPPORTED
120 init_netmask6(nfs_client *clp, const char *slash)
122 struct sockaddr_in6 sin6 = {
123 .sin6_family = AF_INET6,
125 unsigned long prefixlen;
130 * Decide what kind of netmask was specified. If there's
131 * no '/' present, assume the netmask is all ones. If
132 * there is a '/' and at least one ':', look for a spelled-
133 * out netmask. Otherwise, assume it was a prefixlen.
138 if (strchr(slash + 1, ':') != NULL) {
139 if (!inet_pton(AF_INET6, slash + 1, &sin6.sin6_addr))
141 set_addrlist_in6(clp, 1, &sin6);
146 prefixlen = strtoul(slash + 1, &endptr, 10);
147 if (*endptr != '\0' && prefixlen != ULONG_MAX &&
156 * Now construct the full netmask bitmask in a sockaddr_in6,
157 * and plant it in the nfs_client record.
159 for (i = 0; prefixlen > 32; i++) {
160 sin6.sin6_addr.s6_addr32[i] = 0xffffffff;
163 shift = 32 - (uint32_t)prefixlen;
164 sin6.sin6_addr.s6_addr32[i] = htonl((uint32_t)~0 << shift);
165 set_addrlist_in6(clp, 1, &sin6);
170 xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
174 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
177 #else /* IPV6_SUPPORTED */
179 init_netmask6(nfs_client *UNUSED(clp), const char *UNUSED(slash))
183 #endif /* IPV6_SUPPORTED */
186 * Parse the network mask for M_SUBNETWORK type clients.
188 * Return TRUE if successful, or FALSE if some error occurred.
191 init_subnetwork(nfs_client *clp)
198 slash = strchr(clp->m_hostname, '/');
201 ai = host_pton(clp->m_hostname);
204 ai = host_pton(clp->m_hostname);
206 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
210 set_addrlist(clp, 0, ai->ai_addr);
211 family = ai->ai_addr->sa_family;
217 result = init_netmask4(clp, slash);
220 result = init_netmask6(clp, slash);
223 xlog(L_ERROR, "Unsupported address family for %s",
231 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
233 clp->m_hostname = strdup(hname);
234 if (clp->m_hostname == NULL)
241 if (clp->m_type == MCL_SUBNETWORK)
242 return init_subnetwork(clp);
244 init_addrlist(clp, ai);
249 client_add(nfs_client *clp)
253 cpp = &clientlist[clp->m_type];
255 cpp = &((*cpp)->m_next);
261 * client_lookup - look for @hname in our list of cached nfs_clients
262 * @hname: '\0'-terminated ASCII string containing hostname to look for
263 * @canonical: if set, @hname is known to be canonical DNS name
265 * Returns pointer to a matching or freshly created nfs_client. NULL
266 * is returned if some problem occurs.
269 client_lookup(char *hname, int canonical)
271 nfs_client *clp = NULL;
273 struct addrinfo *ai = NULL;
275 htype = client_gettype(hname);
277 if (htype == MCL_FQDN && !canonical) {
278 ai = host_addrinfo(hname);
280 xlog(L_ERROR, "Failed to resolve %s", hname);
283 hname = ai->ai_canonname;
285 for (clp = clientlist[htype]; clp; clp = clp->m_next)
286 if (client_check(clp, ai))
289 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
290 if (strcasecmp(hname, clp->m_hostname)==0)
296 clp = calloc(1, sizeof(*clp));
300 if (!client_init(clp, hname, NULL)) {
308 if (htype == MCL_FQDN && clp->m_naddr == 0)
309 init_addrlist(clp, ai);
317 * client_dup - create a copy of an nfs_client
318 * @clp: pointer to nfs_client to copy
319 * @ai: pointer to addrinfo used to initialize the new client's addrlist
321 * Returns a dynamically allocated nfs_client if successful, or
322 * NULL if some problem occurs. Caller must free the returned
323 * nfs_client with free(3).
326 client_dup(const nfs_client *clp, const struct addrinfo *ai)
330 new = (nfs_client *)malloc(sizeof(*new));
333 memcpy(new, clp, sizeof(*new));
334 new->m_type = MCL_FQDN;
335 new->m_hostname = NULL;
337 if (!client_init(new, ai->ai_canonname, ai)) {
346 * client_release - drop a reference to an nfs_client record
350 client_release(nfs_client *clp)
352 if (clp->m_count <= 0)
353 xlog(L_FATAL, "client_free: m_count <= 0!");
358 * client_freeall - deallocate all nfs_client records
364 nfs_client *clp, **head;
367 for (i = 0; i < MCL_MAXTYPES; i++) {
368 head = clientlist + i;
370 *head = (clp = *head)->m_next;
377 * client_resolve - look up an IP address
378 * @sap: pointer to socket address to resolve
380 * Returns an addrinfo structure, or NULL if some problem occurred.
381 * Caller must free the result with freeaddrinfo(3).
384 client_resolve(const struct sockaddr *sap)
386 struct addrinfo *ai = NULL;
388 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
389 ai = host_reliable_addrinfo(sap);
391 ai = host_numeric_addrinfo(sap);
397 * client_compose - Make a list of cached hostnames that match an IP address
398 * @ai: pointer to addrinfo containing IP address information to match
400 * Gather all known client hostnames that match the IP address, and sort
401 * the result into a comma-separated list.
403 * Returns a '\0'-terminated ASCII string containing a comma-separated
404 * sorted list of client hostnames, or NULL if no client records matched
405 * the IP address or memory could not be allocated. Caller must free the
406 * returned string with free(3).
409 client_compose(const struct addrinfo *ai)
414 for (i = 0 ; i < MCL_MAXTYPES; i++) {
416 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
417 if (!client_check(clp, ai))
419 name = add_name(name, clp->m_hostname);
426 * client_member - check if @name is contained in the list @client
427 * @client: '\0'-terminated ASCII string containing
428 * comma-separated list of hostnames
429 * @name: '\0'-terminated ASCII string containing hostname to look for
431 * Returns 1 if @name was found in @client, otherwise zero is returned.
434 client_member(const char *client, const char *name)
436 size_t l = strlen(name);
439 if (strncmp(client, name, l) == 0 &&
440 (client[l] == ',' || client[l] == '\0'))
442 client = strchr(client, ',');
451 name_cmp(const char *a, const char *b)
453 /* compare strings a and b, but only upto ',' in a */
454 while (*a && *b && *a != ',' && *a == *b)
456 if (!*b && (!*a || *a == ','))
459 if (!*a || *a == ',') return -1;
464 add_name(char *old, const char *add)
466 size_t len = strlen(add) + 2;
469 if (old) len += strlen(old);
477 while (cp && *cp && name_cmp(cp, add) < 0) {
478 /* step cp forward over a name */
479 char *e = strchr(cp, ',');
483 cp = cp + strlen(cp);
485 strncpy(new, old, cp-old);
487 if (cp != old && !*cp)
499 * Check each address listed in @ai against each address
500 * stored in @clp. Return 1 if a match is found, otherwise
504 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
508 for (; ai; ai = ai->ai_next)
509 for (i = 0; i < clp->m_naddr; i++)
510 if (nfs_compare_sockaddr(ai->ai_addr,
511 get_addrlist(clp, i)))
518 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
520 return ((a ^ b) & m) == 0;
524 check_subnet_v4(const struct sockaddr_in *address,
525 const struct sockaddr_in *mask, const struct addrinfo *ai)
527 for (; ai; ai = ai->ai_next) {
528 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
530 if (sin->sin_family != AF_INET)
533 if (mask_match(address->sin_addr.s_addr,
534 sin->sin_addr.s_addr,
535 mask->sin_addr.s_addr))
541 #ifdef IPV6_SUPPORTED
543 check_subnet_v6(const struct sockaddr_in6 *address,
544 const struct sockaddr_in6 *mask, const struct addrinfo *ai)
546 for (; ai; ai = ai->ai_next) {
547 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
549 if (sin6->sin6_family != AF_INET6)
552 if (mask_match(address->sin6_addr.s6_addr32[0],
553 sin6->sin6_addr.s6_addr32[0],
554 mask->sin6_addr.s6_addr32[0]) &&
555 mask_match(address->sin6_addr.s6_addr32[1],
556 sin6->sin6_addr.s6_addr32[1],
557 mask->sin6_addr.s6_addr32[1]) &&
558 mask_match(address->sin6_addr.s6_addr32[2],
559 sin6->sin6_addr.s6_addr32[2],
560 mask->sin6_addr.s6_addr32[2]) &&
561 mask_match(address->sin6_addr.s6_addr32[3],
562 sin6->sin6_addr.s6_addr32[3],
563 mask->sin6_addr.s6_addr32[3]))
568 #else /* !IPV6_SUPPORTED */
570 check_subnet_v6(const struct sockaddr_in6 *UNUSED(address),
571 const struct sockaddr_in6 *UNUSED(mask),
572 const struct addrinfo *UNUSED(ai))
576 #endif /* !IPV6_SUPPORTED */
579 * Check each address listed in @ai against the subnetwork or
580 * host address stored in @clp. Return 1 if an address in @hp
581 * matches the host address stored in @clp, otherwise zero.
584 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
586 switch (get_addrlist(clp, 0)->sa_family) {
588 return check_subnet_v4(get_addrlist_in(clp, 0),
589 get_addrlist_in(clp, 1), ai);
591 return check_subnet_v6(get_addrlist_in6(clp, 0),
592 get_addrlist_in6(clp, 1), ai);
599 * Check if a wildcard nfs_client record matches the canonical name
600 * or the aliases of a host. Return 1 if a match is found, otherwise
604 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
606 char *cname = clp->m_hostname;
607 char *hname = ai->ai_canonname;
611 if (wildmat(hname, cname))
614 /* See if hname aliases listed in /etc/hosts or nis[+]
615 * match the requested wildcard */
616 hp = gethostbyname(hname);
618 for (ap = hp->h_aliases; *ap; ap++)
619 if (wildmat(*ap, cname))
627 * Check if @ai's hostname or aliases fall in a given netgroup.
628 * Return 1 if @ai represents a host in the netgroup, otherwise
633 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
635 const char *netgroup = clp->m_hostname + 1;
636 struct addrinfo *tmp = NULL;
643 hname = strdup(ai->ai_canonname);
645 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
649 /* First, try to match the hostname without
650 * splitting off the domain */
651 if (innetgr(netgroup, hname, NULL, NULL)) {
656 /* See if hname aliases listed in /etc/hosts or nis[+]
657 * match the requested netgroup */
658 hp = gethostbyname(hname);
660 for (i = 0; hp->h_aliases[i]; i++)
661 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
667 /* If hname happens to be an IP address, convert it
668 * to a the canonical DNS name bound to this address. */
669 tmp = host_pton(hname);
671 char *cname = host_canonname(tmp->ai_addr);
674 /* The resulting FQDN may be in our netgroup. */
678 if (innetgr(netgroup, hname, NULL, NULL)) {
685 /* Okay, strip off the domain (if we have one) */
686 dot = strchr(hname, '.');
691 match = innetgr(netgroup, hname, NULL, NULL);
697 #else /* !HAVE_INNETGR */
699 check_netgroup(__attribute__((unused)) const nfs_client *clp,
700 __attribute__((unused)) const struct addrinfo *ai)
704 #endif /* !HAVE_INNETGR */
707 * client_check - check if IP address information matches a cached nfs_client
708 * @clp: pointer to a cached nfs_client record
709 * @ai: pointer to addrinfo to compare it with
711 * Returns 1 if the address information matches the cached nfs_client,
715 client_check(const nfs_client *clp, const struct addrinfo *ai)
717 switch (clp->m_type) {
719 return check_fqdn(clp, ai);
721 return check_subnetwork(clp, ai);
723 return check_wildcard(clp, ai);
725 return check_netgroup(clp, ai);
731 xlog(D_GENERAL, "%s: unrecognized client type: %d",
732 __func__, clp->m_type);
739 * client_gettype - determine type of nfs_client given an identifier
740 * @ident: '\0'-terminated ASCII string containing a client identifier
742 * Returns the type of nfs_client record that would be used for
746 client_gettype(char *ident)
751 if (ident[0] == '\0' || strcmp(ident, "*")==0)
752 return MCL_ANONYMOUS;
753 if (strncmp(ident, "gss/", 4) == 0)
755 if (ident[0] == '@') {
757 xlog(L_WARNING, "netgroup support not compiled in");
761 for (sp = ident; *sp; sp++) {
762 if (*sp == '*' || *sp == '?' || *sp == '[')
765 return MCL_SUBNETWORK;
766 if (*sp == '\\' && sp[1])
771 * Treat unadorned IP addresses as MCL_SUBNETWORK.
772 * Everything else is MCL_FQDN.
774 ai = host_pton(ident);
777 return MCL_SUBNETWORK;