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>
26 /* netgroup stuff never seems to be defined in any header file. Linux is
29 #if !defined(__GLIBC__) || __GLIBC__ < 2
30 extern int innetgr(char *netgr, char *host, char *, char *);
33 static char *add_name(char *old, const char *add);
35 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
39 init_addrlist(nfs_client *clp, const struct addrinfo *ai)
46 for (i = 0; (ai != NULL) && (i < NFSCLNT_ADDRMAX); i++) {
47 set_addrlist(clp, i, ai->ai_addr);
55 client_free(nfs_client *clp)
57 free(clp->m_hostname);
62 init_netmask(nfs_client *clp, const char *slash, const sa_family_t family)
64 struct sockaddr_in sin = {
65 .sin_family = AF_INET,
67 unsigned long prefixlen;
70 struct sockaddr_in6 sin6 = {
71 .sin6_family = AF_INET6,
76 /* No slash present; assume netmask is all ones */
93 /* A spelled out netmask address, perhaps? */
94 if (strchr(slash + 1, '.') != NULL) {
95 if (inet_pton(AF_INET, slash + 1,
96 &sin.sin_addr.s_addr) == 0)
98 set_addrlist_in(clp, 1, &sin);
101 #ifdef IPV6_SUPPORTED
102 if (strchr(slash + 1, ':')) {
103 if (!inet_pton(AF_INET6, slash + 1, &sin6.sin6_addr))
105 set_addrlist_in6(clp, 1, &sin6);
110 /* A prefixlen was given */
111 prefixlen = strtoul(slash + 1, &endptr, 10);
112 if (*endptr != '\0' && prefixlen != ULONG_MAX && errno != ERANGE)
120 shift = 32 - (uint32_t)prefixlen;
121 sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
122 set_addrlist_in(clp, 1, &sin);
124 #ifdef IPV6_SUPPORTED
128 for (i = 0; prefixlen > 32; i++) {
129 sin6.sin6_addr.s6_addr32[i] = 0xffffffff;
132 shift = 32 - (uint32_t)prefixlen;
133 sin6.sin6_addr.s6_addr32[i] = htonl((uint32_t)~0 << shift);
134 set_addrlist_in6(clp, 1, &sin6);
140 xlog(L_ERROR, "Unsupported address family for %s", clp->m_hostname);
144 xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
148 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
153 init_subnetwork(nfs_client *clp)
159 slash = strchr(clp->m_hostname, '/');
162 ai = host_pton(clp->m_hostname);
165 ai = host_pton(clp->m_hostname);
167 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
171 set_addrlist(clp, 0, ai->ai_addr);
172 family = ai->ai_addr->sa_family;
176 return init_netmask(clp, slash, family);
180 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
182 clp->m_hostname = strdup(hname);
183 if (clp->m_hostname == NULL)
190 if (clp->m_type == MCL_SUBNETWORK)
191 return init_subnetwork(clp);
193 init_addrlist(clp, ai);
198 client_add(nfs_client *clp)
202 cpp = &clientlist[clp->m_type];
204 cpp = &((*cpp)->m_next);
210 * client_lookup - look for @hname in our list of cached nfs_clients
211 * @hname: '\0'-terminated ASCII string containing hostname to look for
212 * @canonical: if set, @hname is known to be canonical DNS name
214 * Returns pointer to a matching or freshly created nfs_client. NULL
215 * is returned if some problem occurs.
218 client_lookup(char *hname, int canonical)
220 nfs_client *clp = NULL;
222 struct addrinfo *ai = NULL;
224 htype = client_gettype(hname);
226 if (htype == MCL_FQDN && !canonical) {
227 ai = host_addrinfo(hname);
229 xlog(L_ERROR, "Failed to resolve %s", hname);
232 hname = ai->ai_canonname;
234 for (clp = clientlist[htype]; clp; clp = clp->m_next)
235 if (client_check(clp, ai))
238 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
239 if (strcasecmp(hname, clp->m_hostname)==0)
245 clp = calloc(1, sizeof(*clp));
249 if (!client_init(clp, hname, NULL)) {
257 if (htype == MCL_FQDN && clp->m_naddr == 0)
258 init_addrlist(clp, ai);
266 * client_dup - create a copy of an nfs_client
267 * @clp: pointer to nfs_client to copy
268 * @ai: pointer to addrinfo used to initialize the new client's addrlist
270 * Returns a dynamically allocated nfs_client if successful, or
271 * NULL if some problem occurs. Caller must free the returned
272 * nfs_client with free(3).
275 client_dup(const nfs_client *clp, const struct addrinfo *ai)
279 new = (nfs_client *)malloc(sizeof(*new));
282 memcpy(new, clp, sizeof(*new));
283 new->m_type = MCL_FQDN;
284 new->m_hostname = NULL;
286 if (!client_init(new, ai->ai_canonname, ai)) {
295 * client_release - drop a reference to an nfs_client record
299 client_release(nfs_client *clp)
301 if (clp->m_count <= 0)
302 xlog(L_FATAL, "client_free: m_count <= 0!");
307 * client_freeall - deallocate all nfs_client records
313 nfs_client *clp, **head;
316 for (i = 0; i < MCL_MAXTYPES; i++) {
317 head = clientlist + i;
319 *head = (clp = *head)->m_next;
326 * client_resolve - look up an IP address
327 * @sap: pointer to socket address to resolve
329 * Returns an addrinfo structure, or NULL if some problem occurred.
330 * Caller must free the result with freeaddrinfo(3).
333 client_resolve(const struct sockaddr *sap)
335 struct addrinfo *ai = NULL;
337 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
338 ai = host_reliable_addrinfo(sap);
340 ai = host_numeric_addrinfo(sap);
346 * client_compose - Make a list of cached hostnames that match an IP address
347 * @ai: pointer to addrinfo containing IP address information to match
349 * Gather all known client hostnames that match the IP address, and sort
350 * the result into a comma-separated list.
352 * Returns a '\0'-terminated ASCII string containing a comma-separated
353 * sorted list of client hostnames, or NULL if no client records matched
354 * the IP address or memory could not be allocated. Caller must free the
355 * returned string with free(3).
358 client_compose(const struct addrinfo *ai)
363 for (i = 0 ; i < MCL_MAXTYPES; i++) {
365 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
366 if (!client_check(clp, ai))
368 name = add_name(name, clp->m_hostname);
375 * client_member - check if @name is contained in the list @client
376 * @client: '\0'-terminated ASCII string containing
377 * comma-separated list of hostnames
378 * @name: '\0'-terminated ASCII string containing hostname to look for
380 * Returns 1 if @name was found in @client, otherwise zero is returned.
383 client_member(const char *client, const char *name)
385 size_t l = strlen(name);
388 if (strncmp(client, name, l) == 0 &&
389 (client[l] == ',' || client[l] == '\0'))
391 client = strchr(client, ',');
400 name_cmp(const char *a, const char *b)
402 /* compare strings a and b, but only upto ',' in a */
403 while (*a && *b && *a != ',' && *a == *b)
405 if (!*b && (!*a || *a == ','))
408 if (!*a || *a == ',') return -1;
413 add_name(char *old, const char *add)
415 size_t len = strlen(add) + 2;
418 if (old) len += strlen(old);
426 while (cp && *cp && name_cmp(cp, add) < 0) {
427 /* step cp forward over a name */
428 char *e = strchr(cp, ',');
432 cp = cp + strlen(cp);
434 strncpy(new, old, cp-old);
436 if (cp != old && !*cp)
448 addrs_match4(const struct sockaddr *sa1, const struct sockaddr *sa2)
450 const struct sockaddr_in *si1 = (const struct sockaddr_in *)sa1;
451 const struct sockaddr_in *si2 = (const struct sockaddr_in *)sa2;
453 return si1->sin_addr.s_addr == si2->sin_addr.s_addr;
457 addrs_match(const struct sockaddr *sa1, const struct sockaddr *sa2)
459 if (sa1->sa_family == sa2->sa_family)
460 switch (sa1->sa_family) {
462 return addrs_match4(sa1, sa2);
469 * Check each address listed in @ai against each address
470 * stored in @clp. Return 1 if a match is found, otherwise
474 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
478 for (; ai; ai = ai->ai_next)
479 for (i = 0; i < clp->m_naddr; i++)
480 if (addrs_match(ai->ai_addr, get_addrlist(clp, i)))
487 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
489 return ((a ^ b) & m) == 0;
493 check_subnet_v4(const struct sockaddr_in *address,
494 const struct sockaddr_in *mask, const struct addrinfo *ai)
496 for (; ai; ai = ai->ai_next) {
497 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
499 if (sin->sin_family != AF_INET)
502 if (mask_match(address->sin_addr.s_addr,
503 sin->sin_addr.s_addr,
504 mask->sin_addr.s_addr))
511 * Check each address listed in @ai against the subnetwork or
512 * host address stored in @clp. Return 1 if an address in @hp
513 * matches the host address stored in @clp, otherwise zero.
516 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
518 switch (get_addrlist(clp, 0)->sa_family) {
520 return check_subnet_v4(get_addrlist_in(clp, 0),
521 get_addrlist_in(clp, 1), ai);
528 * Check if a wildcard nfs_client record matches the canonical name
529 * or the aliases of a host. Return 1 if a match is found, otherwise
533 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
535 char *cname = clp->m_hostname;
536 char *hname = ai->ai_canonname;
540 if (wildmat(hname, cname))
543 /* See if hname aliases listed in /etc/hosts or nis[+]
544 * match the requested wildcard */
545 hp = gethostbyname(hname);
547 for (ap = hp->h_aliases; *ap; ap++)
548 if (wildmat(*ap, cname))
556 * Check if @ai's hostname or aliases fall in a given netgroup.
557 * Return 1 if @ai represents a host in the netgroup, otherwise
562 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
564 const char *netgroup = clp->m_hostname + 1;
565 struct addrinfo *tmp = NULL;
572 hname = strdup(ai->ai_canonname);
574 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
578 /* First, try to match the hostname without
579 * splitting off the domain */
580 if (innetgr(netgroup, hname, NULL, NULL)) {
585 /* See if hname aliases listed in /etc/hosts or nis[+]
586 * match the requested netgroup */
587 hp = gethostbyname(hname);
589 for (i = 0; hp->h_aliases[i]; i++)
590 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
596 /* If hname happens to be an IP address, convert it
597 * to a the canonical DNS name bound to this address. */
598 tmp = host_pton(hname);
600 char *cname = host_canonname(tmp->ai_addr);
603 /* The resulting FQDN may be in our netgroup. */
607 if (innetgr(netgroup, hname, NULL, NULL)) {
614 /* Okay, strip off the domain (if we have one) */
615 dot = strchr(hname, '.');
620 match = innetgr(netgroup, hname, NULL, NULL);
626 #else /* !HAVE_INNETGR */
628 check_netgroup(__attribute__((unused)) const nfs_client *clp,
629 __attribute__((unused)) const struct addrinfo *ai)
633 #endif /* !HAVE_INNETGR */
636 * client_check - check if IP address information matches a cached nfs_client
637 * @clp: pointer to a cached nfs_client record
638 * @ai: pointer to addrinfo to compare it with
640 * Returns 1 if the address information matches the cached nfs_client,
644 client_check(const nfs_client *clp, const struct addrinfo *ai)
646 switch (clp->m_type) {
648 return check_fqdn(clp, ai);
650 return check_subnetwork(clp, ai);
652 return check_wildcard(clp, ai);
654 return check_netgroup(clp, ai);
660 xlog(D_GENERAL, "%s: unrecognized client type: %d",
661 __func__, clp->m_type);
668 * client_gettype - determine type of nfs_client given an identifier
669 * @ident: '\0'-terminated ASCII string containing a client identifier
671 * Returns the type of nfs_client record that would be used for
675 client_gettype(char *ident)
680 if (ident[0] == '\0' || strcmp(ident, "*")==0)
681 return MCL_ANONYMOUS;
682 if (strncmp(ident, "gss/", 4) == 0)
684 if (ident[0] == '@') {
686 xlog(L_WARNING, "netgroup support not compiled in");
690 for (sp = ident; *sp; sp++) {
691 if (*sp == '*' || *sp == '?' || *sp == '[')
694 return MCL_SUBNETWORK;
695 if (*sp == '\\' && sp[1])
700 * Treat unadorned IP addresses as MCL_SUBNETWORK.
701 * Everything else is MCL_FQDN.
703 ai = host_pton(ident);
706 return MCL_SUBNETWORK;