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))
182 #endif /* IPV6_SUPPORTED */
185 * Parse the network mask for M_SUBNETWORK type clients.
187 * Return TRUE if successful, or FALSE if some error occurred.
190 init_subnetwork(nfs_client *clp)
197 slash = strchr(clp->m_hostname, '/');
200 ai = host_pton(clp->m_hostname);
203 ai = host_pton(clp->m_hostname);
205 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
209 set_addrlist(clp, 0, ai->ai_addr);
210 family = ai->ai_addr->sa_family;
216 result = init_netmask4(clp, slash);
219 result = init_netmask6(clp, slash);
222 xlog(L_ERROR, "Unsupported address family for %s",
230 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
232 clp->m_hostname = strdup(hname);
233 if (clp->m_hostname == NULL)
240 if (clp->m_type == MCL_SUBNETWORK)
241 return init_subnetwork(clp);
243 init_addrlist(clp, ai);
248 client_add(nfs_client *clp)
252 cpp = &clientlist[clp->m_type];
254 cpp = &((*cpp)->m_next);
260 * client_lookup - look for @hname in our list of cached nfs_clients
261 * @hname: '\0'-terminated ASCII string containing hostname to look for
262 * @canonical: if set, @hname is known to be canonical DNS name
264 * Returns pointer to a matching or freshly created nfs_client. NULL
265 * is returned if some problem occurs.
268 client_lookup(char *hname, int canonical)
270 nfs_client *clp = NULL;
272 struct addrinfo *ai = NULL;
274 htype = client_gettype(hname);
276 if (htype == MCL_FQDN && !canonical) {
277 ai = host_addrinfo(hname);
279 xlog(L_ERROR, "Failed to resolve %s", hname);
282 hname = ai->ai_canonname;
284 for (clp = clientlist[htype]; clp; clp = clp->m_next)
285 if (client_check(clp, ai))
288 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
289 if (strcasecmp(hname, clp->m_hostname)==0)
295 clp = calloc(1, sizeof(*clp));
299 if (!client_init(clp, hname, NULL)) {
307 if (htype == MCL_FQDN && clp->m_naddr == 0)
308 init_addrlist(clp, ai);
316 * client_dup - create a copy of an nfs_client
317 * @clp: pointer to nfs_client to copy
318 * @ai: pointer to addrinfo used to initialize the new client's addrlist
320 * Returns a dynamically allocated nfs_client if successful, or
321 * NULL if some problem occurs. Caller must free the returned
322 * nfs_client with free(3).
325 client_dup(const nfs_client *clp, const struct addrinfo *ai)
329 new = (nfs_client *)malloc(sizeof(*new));
332 memcpy(new, clp, sizeof(*new));
333 new->m_type = MCL_FQDN;
334 new->m_hostname = NULL;
336 if (!client_init(new, ai->ai_canonname, ai)) {
345 * client_release - drop a reference to an nfs_client record
349 client_release(nfs_client *clp)
351 if (clp->m_count <= 0)
352 xlog(L_FATAL, "client_free: m_count <= 0!");
357 * client_freeall - deallocate all nfs_client records
363 nfs_client *clp, **head;
366 for (i = 0; i < MCL_MAXTYPES; i++) {
367 head = clientlist + i;
369 *head = (clp = *head)->m_next;
376 * client_resolve - look up an IP address
377 * @sap: pointer to socket address to resolve
379 * Returns an addrinfo structure, or NULL if some problem occurred.
380 * Caller must free the result with freeaddrinfo(3).
383 client_resolve(const struct sockaddr *sap)
385 struct addrinfo *ai = NULL;
387 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
388 ai = host_reliable_addrinfo(sap);
390 ai = host_numeric_addrinfo(sap);
396 * client_compose - Make a list of cached hostnames that match an IP address
397 * @ai: pointer to addrinfo containing IP address information to match
399 * Gather all known client hostnames that match the IP address, and sort
400 * the result into a comma-separated list.
402 * Returns a '\0'-terminated ASCII string containing a comma-separated
403 * sorted list of client hostnames, or NULL if no client records matched
404 * the IP address or memory could not be allocated. Caller must free the
405 * returned string with free(3).
408 client_compose(const struct addrinfo *ai)
413 for (i = 0 ; i < MCL_MAXTYPES; i++) {
415 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
416 if (!client_check(clp, ai))
418 name = add_name(name, clp->m_hostname);
425 * client_member - check if @name is contained in the list @client
426 * @client: '\0'-terminated ASCII string containing
427 * comma-separated list of hostnames
428 * @name: '\0'-terminated ASCII string containing hostname to look for
430 * Returns 1 if @name was found in @client, otherwise zero is returned.
433 client_member(const char *client, const char *name)
435 size_t l = strlen(name);
438 if (strncmp(client, name, l) == 0 &&
439 (client[l] == ',' || client[l] == '\0'))
441 client = strchr(client, ',');
450 name_cmp(const char *a, const char *b)
452 /* compare strings a and b, but only upto ',' in a */
453 while (*a && *b && *a != ',' && *a == *b)
455 if (!*b && (!*a || *a == ','))
458 if (!*a || *a == ',') return -1;
463 add_name(char *old, const char *add)
465 size_t len = strlen(add) + 2;
468 if (old) len += strlen(old);
476 while (cp && *cp && name_cmp(cp, add) < 0) {
477 /* step cp forward over a name */
478 char *e = strchr(cp, ',');
482 cp = cp + strlen(cp);
484 strncpy(new, old, cp-old);
486 if (cp != old && !*cp)
498 * Check each address listed in @ai against each address
499 * stored in @clp. Return 1 if a match is found, otherwise
503 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
507 for (; ai; ai = ai->ai_next)
508 for (i = 0; i < clp->m_naddr; i++)
509 if (nfs_compare_sockaddr(ai->ai_addr,
510 get_addrlist(clp, i)))
517 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
519 return ((a ^ b) & m) == 0;
523 check_subnet_v4(const struct sockaddr_in *address,
524 const struct sockaddr_in *mask, const struct addrinfo *ai)
526 for (; ai; ai = ai->ai_next) {
527 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
529 if (sin->sin_family != AF_INET)
532 if (mask_match(address->sin_addr.s_addr,
533 sin->sin_addr.s_addr,
534 mask->sin_addr.s_addr))
540 #ifdef IPV6_SUPPORTED
542 check_subnet_v6(const struct sockaddr_in6 *address,
543 const struct sockaddr_in6 *mask, const struct addrinfo *ai)
545 for (; ai; ai = ai->ai_next) {
546 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
548 if (sin6->sin6_family != AF_INET6)
551 if (mask_match(address->sin6_addr.s6_addr32[0],
552 sin6->sin6_addr.s6_addr32[0],
553 mask->sin6_addr.s6_addr32[0]) &&
554 mask_match(address->sin6_addr.s6_addr32[1],
555 sin6->sin6_addr.s6_addr32[1],
556 mask->sin6_addr.s6_addr32[1]) &&
557 mask_match(address->sin6_addr.s6_addr32[2],
558 sin6->sin6_addr.s6_addr32[2],
559 mask->sin6_addr.s6_addr32[2]) &&
560 mask_match(address->sin6_addr.s6_addr32[3],
561 sin6->sin6_addr.s6_addr32[3],
562 mask->sin6_addr.s6_addr32[3]))
567 #else /* !IPV6_SUPPORTED */
569 check_subnet_v6(const struct sockaddr_in6 *UNUSED(address),
570 const struct sockaddr_in6 *UNUSED(mask),
571 const struct addrinfo *UNUSED(ai))
575 #endif /* !IPV6_SUPPORTED */
578 * Check each address listed in @ai against the subnetwork or
579 * host address stored in @clp. Return 1 if an address in @hp
580 * matches the host address stored in @clp, otherwise zero.
583 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
585 switch (get_addrlist(clp, 0)->sa_family) {
587 return check_subnet_v4(get_addrlist_in(clp, 0),
588 get_addrlist_in(clp, 1), ai);
590 return check_subnet_v6(get_addrlist_in6(clp, 0),
591 get_addrlist_in6(clp, 1), ai);
598 * Check if a wildcard nfs_client record matches the canonical name
599 * or the aliases of a host. Return 1 if a match is found, otherwise
603 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
605 char *cname = clp->m_hostname;
606 char *hname = ai->ai_canonname;
610 if (wildmat(hname, cname))
613 /* See if hname aliases listed in /etc/hosts or nis[+]
614 * match the requested wildcard */
615 hp = gethostbyname(hname);
617 for (ap = hp->h_aliases; *ap; ap++)
618 if (wildmat(*ap, cname))
626 * Check if @ai's hostname or aliases fall in a given netgroup.
627 * Return 1 if @ai represents a host in the netgroup, otherwise
632 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
634 const char *netgroup = clp->m_hostname + 1;
635 struct addrinfo *tmp = NULL;
642 hname = strdup(ai->ai_canonname);
644 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
648 /* First, try to match the hostname without
649 * splitting off the domain */
650 if (innetgr(netgroup, hname, NULL, NULL)) {
655 /* See if hname aliases listed in /etc/hosts or nis[+]
656 * match the requested netgroup */
657 hp = gethostbyname(hname);
659 for (i = 0; hp->h_aliases[i]; i++)
660 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
666 /* If hname happens to be an IP address, convert it
667 * to a the canonical DNS name bound to this address. */
668 tmp = host_pton(hname);
670 char *cname = host_canonname(tmp->ai_addr);
673 /* The resulting FQDN may be in our netgroup. */
677 if (innetgr(netgroup, hname, NULL, NULL)) {
684 /* Okay, strip off the domain (if we have one) */
685 dot = strchr(hname, '.');
690 match = innetgr(netgroup, hname, NULL, NULL);
696 #else /* !HAVE_INNETGR */
698 check_netgroup(__attribute__((unused)) const nfs_client *clp,
699 __attribute__((unused)) const struct addrinfo *ai)
703 #endif /* !HAVE_INNETGR */
706 * client_check - check if IP address information matches a cached nfs_client
707 * @clp: pointer to a cached nfs_client record
708 * @ai: pointer to addrinfo to compare it with
710 * Returns 1 if the address information matches the cached nfs_client,
714 client_check(const nfs_client *clp, const struct addrinfo *ai)
716 switch (clp->m_type) {
718 return check_fqdn(clp, ai);
720 return check_subnetwork(clp, ai);
722 return check_wildcard(clp, ai);
724 return check_netgroup(clp, ai);
730 xlog(D_GENERAL, "%s: unrecognized client type: %d",
731 __func__, clp->m_type);
738 * client_gettype - determine type of nfs_client given an identifier
739 * @ident: '\0'-terminated ASCII string containing a client identifier
741 * Returns the type of nfs_client record that would be used for
745 client_gettype(char *ident)
750 if (ident[0] == '\0' || strcmp(ident, "*")==0)
751 return MCL_ANONYMOUS;
752 if (strncmp(ident, "gss/", 4) == 0)
754 if (ident[0] == '@') {
756 xlog(L_WARNING, "netgroup support not compiled in");
760 for (sp = ident; *sp; sp++) {
761 if (*sp == '*' || *sp == '?' || *sp == '[')
764 return MCL_SUBNETWORK;
765 if (*sp == '\\' && sp[1])
770 * Treat unadorned IP addresses as MCL_SUBNETWORK.
771 * Everything else is MCL_FQDN.
773 ai = host_pton(ident);
776 return MCL_SUBNETWORK;