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>
25 /* netgroup stuff never seems to be defined in any header file. Linux is
28 #if !defined(__GLIBC__) || __GLIBC__ < 2
29 extern int innetgr(char *netgr, char *host, char *, char *);
32 static char *add_name(char *old, const char *add);
34 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
38 init_addrlist(nfs_client *clp, const struct addrinfo *ai)
45 for (i = 0; (ai != NULL) && (i < NFSCLNT_ADDRMAX); i++) {
46 set_addrlist(clp, i, ai->ai_addr);
54 client_free(nfs_client *clp)
56 free(clp->m_hostname);
61 init_netmask(nfs_client *clp, const char *slash)
63 struct sockaddr_in sin = {
64 .sin_family = AF_INET,
67 if (strchr(slash + 1, '.') != NULL)
68 sin.sin_addr.s_addr = inet_addr(slash + 1);
70 int prefixlen = atoi(slash + 1);
71 if (0 < prefixlen && prefixlen <= 32)
73 htonl((uint32_t)~0 << (32 - prefixlen));
78 set_addrlist_in(clp, 1, &sin);
82 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
87 init_subnetwork(nfs_client *clp)
89 static char slash32[] = "/32";
93 cp = strchr(clp->m_hostname, '/');
98 ai = host_pton(clp->m_hostname);
101 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
104 set_addrlist(clp, 0, ai->ai_addr);
107 return init_netmask(clp, cp);
111 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
113 clp->m_hostname = strdup(hname);
114 if (clp->m_hostname == NULL)
121 if (clp->m_type == MCL_SUBNETWORK)
122 return init_subnetwork(clp);
124 init_addrlist(clp, ai);
129 client_add(nfs_client *clp)
133 cpp = &clientlist[clp->m_type];
135 cpp = &((*cpp)->m_next);
141 * client_lookup - look for @hname in our list of cached nfs_clients
142 * @hname: '\0'-terminated ASCII string containing hostname to look for
143 * @canonical: if set, @hname is known to be canonical DNS name
145 * Returns pointer to a matching or freshly created nfs_client. NULL
146 * is returned if some problem occurs.
149 client_lookup(char *hname, int canonical)
151 nfs_client *clp = NULL;
153 struct addrinfo *ai = NULL;
155 htype = client_gettype(hname);
157 if (htype == MCL_FQDN && !canonical) {
158 ai = host_addrinfo(hname);
160 xlog(L_ERROR, "Failed to resolve %s", hname);
163 hname = ai->ai_canonname;
165 for (clp = clientlist[htype]; clp; clp = clp->m_next)
166 if (client_check(clp, ai))
169 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
170 if (strcasecmp(hname, clp->m_hostname)==0)
176 clp = calloc(1, sizeof(*clp));
180 if (!client_init(clp, hname, NULL)) {
188 if (htype == MCL_FQDN && clp->m_naddr == 0)
189 init_addrlist(clp, ai);
197 * client_dup - create a copy of an nfs_client
198 * @clp: pointer to nfs_client to copy
199 * @ai: pointer to addrinfo used to initialize the new client's addrlist
201 * Returns a dynamically allocated nfs_client if successful, or
202 * NULL if some problem occurs. Caller must free the returned
203 * nfs_client with free(3).
206 client_dup(const nfs_client *clp, const struct addrinfo *ai)
210 new = (nfs_client *)malloc(sizeof(*new));
213 memcpy(new, clp, sizeof(*new));
214 new->m_type = MCL_FQDN;
215 new->m_hostname = NULL;
217 if (!client_init(new, ai->ai_canonname, ai)) {
226 * client_release - drop a reference to an nfs_client record
230 client_release(nfs_client *clp)
232 if (clp->m_count <= 0)
233 xlog(L_FATAL, "client_free: m_count <= 0!");
238 * client_freeall - deallocate all nfs_client records
244 nfs_client *clp, **head;
247 for (i = 0; i < MCL_MAXTYPES; i++) {
248 head = clientlist + i;
250 *head = (clp = *head)->m_next;
257 * client_resolve - look up an IP address
258 * @sap: pointer to socket address to resolve
260 * Returns an addrinfo structure, or NULL if some problem occurred.
261 * Caller must free the result with freeaddrinfo(3).
264 client_resolve(const struct sockaddr *sap)
266 struct addrinfo *ai = NULL;
268 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
269 ai = host_reliable_addrinfo(sap);
271 ai = host_numeric_addrinfo(sap);
277 * client_compose - Make a list of cached hostnames that match an IP address
278 * @ai: pointer to addrinfo containing IP address information to match
280 * Gather all known client hostnames that match the IP address, and sort
281 * the result into a comma-separated list.
283 * Returns a '\0'-terminated ASCII string containing a comma-separated
284 * sorted list of client hostnames, or NULL if no client records matched
285 * the IP address or memory could not be allocated. Caller must free the
286 * returned string with free(3).
289 client_compose(const struct addrinfo *ai)
294 for (i = 0 ; i < MCL_MAXTYPES; i++) {
296 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
297 if (!client_check(clp, ai))
299 name = add_name(name, clp->m_hostname);
306 * client_member - check if @name is contained in the list @client
307 * @client: '\0'-terminated ASCII string containing
308 * comma-separated list of hostnames
309 * @name: '\0'-terminated ASCII string containing hostname to look for
311 * Returns 1 if @name was found in @client, otherwise zero is returned.
314 client_member(const char *client, const char *name)
316 size_t l = strlen(name);
319 if (strncmp(client, name, l) == 0 &&
320 (client[l] == ',' || client[l] == '\0'))
322 client = strchr(client, ',');
331 name_cmp(const char *a, const char *b)
333 /* compare strings a and b, but only upto ',' in a */
334 while (*a && *b && *a != ',' && *a == *b)
336 if (!*b && (!*a || *a == ','))
339 if (!*a || *a == ',') return -1;
344 add_name(char *old, const char *add)
346 size_t len = strlen(add) + 2;
349 if (old) len += strlen(old);
357 while (cp && *cp && name_cmp(cp, add) < 0) {
358 /* step cp forward over a name */
359 char *e = strchr(cp, ',');
363 cp = cp + strlen(cp);
365 strncpy(new, old, cp-old);
367 if (cp != old && !*cp)
379 addrs_match4(const struct sockaddr *sa1, const struct sockaddr *sa2)
381 const struct sockaddr_in *si1 = (const struct sockaddr_in *)sa1;
382 const struct sockaddr_in *si2 = (const struct sockaddr_in *)sa2;
384 return si1->sin_addr.s_addr == si2->sin_addr.s_addr;
388 addrs_match(const struct sockaddr *sa1, const struct sockaddr *sa2)
390 if (sa1->sa_family == sa2->sa_family)
391 switch (sa1->sa_family) {
393 return addrs_match4(sa1, sa2);
400 * Check each address listed in @ai against each address
401 * stored in @clp. Return 1 if a match is found, otherwise
405 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
409 for (; ai; ai = ai->ai_next)
410 for (i = 0; i < clp->m_naddr; i++)
411 if (addrs_match(ai->ai_addr, get_addrlist(clp, i)))
418 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
420 return ((a ^ b) & m) == 0;
424 check_subnet_v4(const struct sockaddr_in *address,
425 const struct sockaddr_in *mask, const struct addrinfo *ai)
427 for (; ai; ai = ai->ai_next) {
428 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
430 if (sin->sin_family != AF_INET)
433 if (mask_match(address->sin_addr.s_addr,
434 sin->sin_addr.s_addr,
435 mask->sin_addr.s_addr))
442 * Check each address listed in @ai against the subnetwork or
443 * host address stored in @clp. Return 1 if an address in @hp
444 * matches the host address stored in @clp, otherwise zero.
447 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
449 switch (get_addrlist(clp, 0)->sa_family) {
451 return check_subnet_v4(get_addrlist_in(clp, 0),
452 get_addrlist_in(clp, 1), ai);
459 * Check if a wildcard nfs_client record matches the canonical name
460 * or the aliases of a host. Return 1 if a match is found, otherwise
464 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
466 char *cname = clp->m_hostname;
467 char *hname = ai->ai_canonname;
471 if (wildmat(hname, cname))
474 /* See if hname aliases listed in /etc/hosts or nis[+]
475 * match the requested wildcard */
476 hp = gethostbyname(hname);
478 for (ap = hp->h_aliases; *ap; ap++)
479 if (wildmat(*ap, cname))
487 * Check if @ai's hostname or aliases fall in a given netgroup.
488 * Return 1 if @ai represents a host in the netgroup, otherwise
493 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
495 const char *netgroup = clp->m_hostname + 1;
496 struct addrinfo *tmp = NULL;
503 hname = strdup(ai->ai_canonname);
505 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
509 /* First, try to match the hostname without
510 * splitting off the domain */
511 if (innetgr(netgroup, hname, NULL, NULL)) {
516 /* See if hname aliases listed in /etc/hosts or nis[+]
517 * match the requested netgroup */
518 hp = gethostbyname(hname);
520 for (i = 0; hp->h_aliases[i]; i++)
521 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
527 /* If hname happens to be an IP address, convert it
528 * to a the canonical DNS name bound to this address. */
529 tmp = host_pton(hname);
531 char *cname = host_canonname(tmp->ai_addr);
534 /* The resulting FQDN may be in our netgroup. */
538 if (innetgr(netgroup, hname, NULL, NULL)) {
545 /* Okay, strip off the domain (if we have one) */
546 dot = strchr(hname, '.');
551 match = innetgr(netgroup, hname, NULL, NULL);
557 #else /* !HAVE_INNETGR */
559 check_netgroup(__attribute__((unused)) const nfs_client *clp,
560 __attribute__((unused)) const struct addrinfo *ai)
564 #endif /* !HAVE_INNETGR */
567 * client_check - check if IP address information matches a cached nfs_client
568 * @clp: pointer to a cached nfs_client record
569 * @ai: pointer to addrinfo to compare it with
571 * Returns 1 if the address information matches the cached nfs_client,
575 client_check(const nfs_client *clp, const struct addrinfo *ai)
577 switch (clp->m_type) {
579 return check_fqdn(clp, ai);
581 return check_subnetwork(clp, ai);
583 return check_wildcard(clp, ai);
585 return check_netgroup(clp, ai);
591 xlog(D_GENERAL, "%s: unrecognized client type: %d",
592 __func__, clp->m_type);
599 * client_gettype - determine type of nfs_client given an identifier
600 * @ident: '\0'-terminated ASCII string containing a client identifier
602 * Returns the type of nfs_client record that would be used for
606 client_gettype(char *ident)
611 if (ident[0] == '\0' || strcmp(ident, "*")==0)
612 return MCL_ANONYMOUS;
613 if (strncmp(ident, "gss/", 4) == 0)
615 if (ident[0] == '@') {
617 xlog(L_WARNING, "netgroup support not compiled in");
621 for (sp = ident; *sp; sp++) {
622 if (*sp == '*' || *sp == '?' || *sp == '[')
625 return MCL_SUBNETWORK;
626 if (*sp == '\\' && sp[1])
631 * Treat unadorned IP addresses as MCL_SUBNETWORK.
632 * Everything else is MCL_FQDN.
634 ai = host_pton(ident);
637 return MCL_SUBNETWORK;