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 /* No slash present; assume netmask is all ones */
82 /* A spelled out netmask address, perhaps? */
83 if (strchr(slash + 1, '.') != NULL) {
84 if (inet_pton(AF_INET, slash + 1,
85 &sin.sin_addr.s_addr) == 0)
87 set_addrlist_in(clp, 1, &sin);
91 /* A prefixlen was given */
92 prefixlen = strtoul(slash + 1, &endptr, 10);
93 if (*endptr != '\0' && prefixlen != ULONG_MAX && errno != ERANGE)
101 shift = 32 - (uint32_t)prefixlen;
102 sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
103 set_addrlist_in(clp, 1, &sin);
108 xlog(L_ERROR, "Unsupported address family for %s", clp->m_hostname);
112 xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
116 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
121 init_subnetwork(nfs_client *clp)
127 slash = strchr(clp->m_hostname, '/');
130 ai = host_pton(clp->m_hostname);
133 ai = host_pton(clp->m_hostname);
135 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
139 set_addrlist(clp, 0, ai->ai_addr);
140 family = ai->ai_addr->sa_family;
144 return init_netmask(clp, slash, family);
148 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
150 clp->m_hostname = strdup(hname);
151 if (clp->m_hostname == NULL)
158 if (clp->m_type == MCL_SUBNETWORK)
159 return init_subnetwork(clp);
161 init_addrlist(clp, ai);
166 client_add(nfs_client *clp)
170 cpp = &clientlist[clp->m_type];
172 cpp = &((*cpp)->m_next);
178 * client_lookup - look for @hname in our list of cached nfs_clients
179 * @hname: '\0'-terminated ASCII string containing hostname to look for
180 * @canonical: if set, @hname is known to be canonical DNS name
182 * Returns pointer to a matching or freshly created nfs_client. NULL
183 * is returned if some problem occurs.
186 client_lookup(char *hname, int canonical)
188 nfs_client *clp = NULL;
190 struct addrinfo *ai = NULL;
192 htype = client_gettype(hname);
194 if (htype == MCL_FQDN && !canonical) {
195 ai = host_addrinfo(hname);
197 xlog(L_ERROR, "Failed to resolve %s", hname);
200 hname = ai->ai_canonname;
202 for (clp = clientlist[htype]; clp; clp = clp->m_next)
203 if (client_check(clp, ai))
206 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
207 if (strcasecmp(hname, clp->m_hostname)==0)
213 clp = calloc(1, sizeof(*clp));
217 if (!client_init(clp, hname, NULL)) {
225 if (htype == MCL_FQDN && clp->m_naddr == 0)
226 init_addrlist(clp, ai);
234 * client_dup - create a copy of an nfs_client
235 * @clp: pointer to nfs_client to copy
236 * @ai: pointer to addrinfo used to initialize the new client's addrlist
238 * Returns a dynamically allocated nfs_client if successful, or
239 * NULL if some problem occurs. Caller must free the returned
240 * nfs_client with free(3).
243 client_dup(const nfs_client *clp, const struct addrinfo *ai)
247 new = (nfs_client *)malloc(sizeof(*new));
250 memcpy(new, clp, sizeof(*new));
251 new->m_type = MCL_FQDN;
252 new->m_hostname = NULL;
254 if (!client_init(new, ai->ai_canonname, ai)) {
263 * client_release - drop a reference to an nfs_client record
267 client_release(nfs_client *clp)
269 if (clp->m_count <= 0)
270 xlog(L_FATAL, "client_free: m_count <= 0!");
275 * client_freeall - deallocate all nfs_client records
281 nfs_client *clp, **head;
284 for (i = 0; i < MCL_MAXTYPES; i++) {
285 head = clientlist + i;
287 *head = (clp = *head)->m_next;
294 * client_resolve - look up an IP address
295 * @sap: pointer to socket address to resolve
297 * Returns an addrinfo structure, or NULL if some problem occurred.
298 * Caller must free the result with freeaddrinfo(3).
301 client_resolve(const struct sockaddr *sap)
303 struct addrinfo *ai = NULL;
305 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
306 ai = host_reliable_addrinfo(sap);
308 ai = host_numeric_addrinfo(sap);
314 * client_compose - Make a list of cached hostnames that match an IP address
315 * @ai: pointer to addrinfo containing IP address information to match
317 * Gather all known client hostnames that match the IP address, and sort
318 * the result into a comma-separated list.
320 * Returns a '\0'-terminated ASCII string containing a comma-separated
321 * sorted list of client hostnames, or NULL if no client records matched
322 * the IP address or memory could not be allocated. Caller must free the
323 * returned string with free(3).
326 client_compose(const struct addrinfo *ai)
331 for (i = 0 ; i < MCL_MAXTYPES; i++) {
333 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
334 if (!client_check(clp, ai))
336 name = add_name(name, clp->m_hostname);
343 * client_member - check if @name is contained in the list @client
344 * @client: '\0'-terminated ASCII string containing
345 * comma-separated list of hostnames
346 * @name: '\0'-terminated ASCII string containing hostname to look for
348 * Returns 1 if @name was found in @client, otherwise zero is returned.
351 client_member(const char *client, const char *name)
353 size_t l = strlen(name);
356 if (strncmp(client, name, l) == 0 &&
357 (client[l] == ',' || client[l] == '\0'))
359 client = strchr(client, ',');
368 name_cmp(const char *a, const char *b)
370 /* compare strings a and b, but only upto ',' in a */
371 while (*a && *b && *a != ',' && *a == *b)
373 if (!*b && (!*a || *a == ','))
376 if (!*a || *a == ',') return -1;
381 add_name(char *old, const char *add)
383 size_t len = strlen(add) + 2;
386 if (old) len += strlen(old);
394 while (cp && *cp && name_cmp(cp, add) < 0) {
395 /* step cp forward over a name */
396 char *e = strchr(cp, ',');
400 cp = cp + strlen(cp);
402 strncpy(new, old, cp-old);
404 if (cp != old && !*cp)
416 addrs_match4(const struct sockaddr *sa1, const struct sockaddr *sa2)
418 const struct sockaddr_in *si1 = (const struct sockaddr_in *)sa1;
419 const struct sockaddr_in *si2 = (const struct sockaddr_in *)sa2;
421 return si1->sin_addr.s_addr == si2->sin_addr.s_addr;
425 addrs_match(const struct sockaddr *sa1, const struct sockaddr *sa2)
427 if (sa1->sa_family == sa2->sa_family)
428 switch (sa1->sa_family) {
430 return addrs_match4(sa1, sa2);
437 * Check each address listed in @ai against each address
438 * stored in @clp. Return 1 if a match is found, otherwise
442 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
446 for (; ai; ai = ai->ai_next)
447 for (i = 0; i < clp->m_naddr; i++)
448 if (addrs_match(ai->ai_addr, get_addrlist(clp, i)))
455 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
457 return ((a ^ b) & m) == 0;
461 check_subnet_v4(const struct sockaddr_in *address,
462 const struct sockaddr_in *mask, const struct addrinfo *ai)
464 for (; ai; ai = ai->ai_next) {
465 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
467 if (sin->sin_family != AF_INET)
470 if (mask_match(address->sin_addr.s_addr,
471 sin->sin_addr.s_addr,
472 mask->sin_addr.s_addr))
479 * Check each address listed in @ai against the subnetwork or
480 * host address stored in @clp. Return 1 if an address in @hp
481 * matches the host address stored in @clp, otherwise zero.
484 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
486 switch (get_addrlist(clp, 0)->sa_family) {
488 return check_subnet_v4(get_addrlist_in(clp, 0),
489 get_addrlist_in(clp, 1), ai);
496 * Check if a wildcard nfs_client record matches the canonical name
497 * or the aliases of a host. Return 1 if a match is found, otherwise
501 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
503 char *cname = clp->m_hostname;
504 char *hname = ai->ai_canonname;
508 if (wildmat(hname, cname))
511 /* See if hname aliases listed in /etc/hosts or nis[+]
512 * match the requested wildcard */
513 hp = gethostbyname(hname);
515 for (ap = hp->h_aliases; *ap; ap++)
516 if (wildmat(*ap, cname))
524 * Check if @ai's hostname or aliases fall in a given netgroup.
525 * Return 1 if @ai represents a host in the netgroup, otherwise
530 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
532 const char *netgroup = clp->m_hostname + 1;
533 struct addrinfo *tmp = NULL;
540 hname = strdup(ai->ai_canonname);
542 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
546 /* First, try to match the hostname without
547 * splitting off the domain */
548 if (innetgr(netgroup, hname, NULL, NULL)) {
553 /* See if hname aliases listed in /etc/hosts or nis[+]
554 * match the requested netgroup */
555 hp = gethostbyname(hname);
557 for (i = 0; hp->h_aliases[i]; i++)
558 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
564 /* If hname happens to be an IP address, convert it
565 * to a the canonical DNS name bound to this address. */
566 tmp = host_pton(hname);
568 char *cname = host_canonname(tmp->ai_addr);
571 /* The resulting FQDN may be in our netgroup. */
575 if (innetgr(netgroup, hname, NULL, NULL)) {
582 /* Okay, strip off the domain (if we have one) */
583 dot = strchr(hname, '.');
588 match = innetgr(netgroup, hname, NULL, NULL);
594 #else /* !HAVE_INNETGR */
596 check_netgroup(__attribute__((unused)) const nfs_client *clp,
597 __attribute__((unused)) const struct addrinfo *ai)
601 #endif /* !HAVE_INNETGR */
604 * client_check - check if IP address information matches a cached nfs_client
605 * @clp: pointer to a cached nfs_client record
606 * @ai: pointer to addrinfo to compare it with
608 * Returns 1 if the address information matches the cached nfs_client,
612 client_check(const nfs_client *clp, const struct addrinfo *ai)
614 switch (clp->m_type) {
616 return check_fqdn(clp, ai);
618 return check_subnetwork(clp, ai);
620 return check_wildcard(clp, ai);
622 return check_netgroup(clp, ai);
628 xlog(D_GENERAL, "%s: unrecognized client type: %d",
629 __func__, clp->m_type);
636 * client_gettype - determine type of nfs_client given an identifier
637 * @ident: '\0'-terminated ASCII string containing a client identifier
639 * Returns the type of nfs_client record that would be used for
643 client_gettype(char *ident)
648 if (ident[0] == '\0' || strcmp(ident, "*")==0)
649 return MCL_ANONYMOUS;
650 if (strncmp(ident, "gss/", 4) == 0)
652 if (ident[0] == '@') {
654 xlog(L_WARNING, "netgroup support not compiled in");
658 for (sp = ident; *sp; sp++) {
659 if (*sp == '*' || *sp == '?' || *sp == '[')
662 return MCL_SUBNETWORK;
663 if (*sp == '\\' && sp[1])
668 * Treat unadorned IP addresses as MCL_SUBNETWORK.
669 * Everything else is MCL_FQDN.
671 ai = host_pton(ident);
674 return MCL_SUBNETWORK;