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 struct sockaddr_in sin = {
90 .sin_family = AF_INET,
92 static char slash32[] = "/32";
95 cp = strchr(clp->m_hostname, '/');
100 sin.sin_addr.s_addr = inet_addr(clp->m_hostname);
101 set_addrlist_in(clp, 0, &sin);
104 return init_netmask(clp, cp);
108 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
110 clp->m_hostname = strdup(hname);
111 if (clp->m_hostname == NULL)
118 if (clp->m_type == MCL_SUBNETWORK)
119 return init_subnetwork(clp);
121 init_addrlist(clp, ai);
126 client_add(nfs_client *clp)
130 cpp = &clientlist[clp->m_type];
132 cpp = &((*cpp)->m_next);
138 * client_lookup - look for @hname in our list of cached nfs_clients
139 * @hname: '\0'-terminated ASCII string containing hostname to look for
140 * @canonical: if set, @hname is known to be canonical DNS name
142 * Returns pointer to a matching or freshly created nfs_client. NULL
143 * is returned if some problem occurs.
146 client_lookup(char *hname, int canonical)
148 nfs_client *clp = NULL;
150 struct addrinfo *ai = NULL;
152 htype = client_gettype(hname);
154 if (htype == MCL_FQDN && !canonical) {
155 ai = host_addrinfo(hname);
157 xlog(L_ERROR, "Failed to resolve %s", hname);
160 hname = ai->ai_canonname;
162 for (clp = clientlist[htype]; clp; clp = clp->m_next)
163 if (client_check(clp, ai))
166 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
167 if (strcasecmp(hname, clp->m_hostname)==0)
173 clp = calloc(1, sizeof(*clp));
177 if (!client_init(clp, hname, NULL)) {
185 if (htype == MCL_FQDN && clp->m_naddr == 0)
186 init_addrlist(clp, ai);
194 * client_dup - create a copy of an nfs_client
195 * @clp: pointer to nfs_client to copy
196 * @ai: pointer to addrinfo used to initialize the new client's addrlist
198 * Returns a dynamically allocated nfs_client if successful, or
199 * NULL if some problem occurs. Caller must free the returned
200 * nfs_client with free(3).
203 client_dup(const nfs_client *clp, const struct addrinfo *ai)
207 new = (nfs_client *)malloc(sizeof(*new));
210 memcpy(new, clp, sizeof(*new));
211 new->m_type = MCL_FQDN;
212 new->m_hostname = NULL;
214 if (!client_init(new, ai->ai_canonname, ai)) {
223 * client_release - drop a reference to an nfs_client record
227 client_release(nfs_client *clp)
229 if (clp->m_count <= 0)
230 xlog(L_FATAL, "client_free: m_count <= 0!");
235 * client_freeall - deallocate all nfs_client records
241 nfs_client *clp, **head;
244 for (i = 0; i < MCL_MAXTYPES; i++) {
245 head = clientlist + i;
247 *head = (clp = *head)->m_next;
254 * client_resolve - look up an IP address
255 * @sap: pointer to socket address to resolve
257 * Returns an addrinfo structure, or NULL if some problem occurred.
258 * Caller must free the result with freeaddrinfo(3).
261 client_resolve(const struct sockaddr *sap)
263 struct addrinfo *ai = NULL;
265 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
266 ai = host_reliable_addrinfo(sap);
268 ai = host_numeric_addrinfo(sap);
274 * client_compose - Make a list of cached hostnames that match an IP address
275 * @ai: pointer to addrinfo containing IP address information to match
277 * Gather all known client hostnames that match the IP address, and sort
278 * the result into a comma-separated list.
280 * Returns a '\0'-terminated ASCII string containing a comma-separated
281 * sorted list of client hostnames, or NULL if no client records matched
282 * the IP address or memory could not be allocated. Caller must free the
283 * returned string with free(3).
286 client_compose(const struct addrinfo *ai)
291 for (i = 0 ; i < MCL_MAXTYPES; i++) {
293 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
294 if (!client_check(clp, ai))
296 name = add_name(name, clp->m_hostname);
303 * client_member - check if @name is contained in the list @client
304 * @client: '\0'-terminated ASCII string containing
305 * comma-separated list of hostnames
306 * @name: '\0'-terminated ASCII string containing hostname to look for
308 * Returns 1 if @name was found in @client, otherwise zero is returned.
311 client_member(const char *client, const char *name)
313 size_t l = strlen(name);
316 if (strncmp(client, name, l) == 0 &&
317 (client[l] == ',' || client[l] == '\0'))
319 client = strchr(client, ',');
328 name_cmp(const char *a, const char *b)
330 /* compare strings a and b, but only upto ',' in a */
331 while (*a && *b && *a != ',' && *a == *b)
333 if (!*b && (!*a || *a == ','))
336 if (!*a || *a == ',') return -1;
341 add_name(char *old, const char *add)
343 size_t len = strlen(add) + 2;
346 if (old) len += strlen(old);
354 while (cp && *cp && name_cmp(cp, add) < 0) {
355 /* step cp forward over a name */
356 char *e = strchr(cp, ',');
360 cp = cp + strlen(cp);
362 strncpy(new, old, cp-old);
364 if (cp != old && !*cp)
376 addrs_match4(const struct sockaddr *sa1, const struct sockaddr *sa2)
378 const struct sockaddr_in *si1 = (const struct sockaddr_in *)sa1;
379 const struct sockaddr_in *si2 = (const struct sockaddr_in *)sa2;
381 return si1->sin_addr.s_addr == si2->sin_addr.s_addr;
385 addrs_match(const struct sockaddr *sa1, const struct sockaddr *sa2)
387 if (sa1->sa_family == sa2->sa_family)
388 switch (sa1->sa_family) {
390 return addrs_match4(sa1, sa2);
397 * Check each address listed in @ai against each address
398 * stored in @clp. Return 1 if a match is found, otherwise
402 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
406 for (; ai; ai = ai->ai_next)
407 for (i = 0; i < clp->m_naddr; i++)
408 if (addrs_match(ai->ai_addr, get_addrlist(clp, i)))
415 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
417 return ((a ^ b) & m) == 0;
421 check_subnet_v4(const struct sockaddr_in *address,
422 const struct sockaddr_in *mask, const struct addrinfo *ai)
424 for (; ai; ai = ai->ai_next) {
425 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
427 if (sin->sin_family != AF_INET)
430 if (mask_match(address->sin_addr.s_addr,
431 sin->sin_addr.s_addr,
432 mask->sin_addr.s_addr))
439 * Check each address listed in @ai against the subnetwork or
440 * host address stored in @clp. Return 1 if an address in @hp
441 * matches the host address stored in @clp, otherwise zero.
444 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
446 switch (get_addrlist(clp, 0)->sa_family) {
448 return check_subnet_v4(get_addrlist_in(clp, 0),
449 get_addrlist_in(clp, 1), ai);
456 * Check if a wildcard nfs_client record matches the canonical name
457 * or the aliases of a host. Return 1 if a match is found, otherwise
461 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
463 char *cname = clp->m_hostname;
464 char *hname = ai->ai_canonname;
468 if (wildmat(hname, cname))
471 /* See if hname aliases listed in /etc/hosts or nis[+]
472 * match the requested wildcard */
473 hp = gethostbyname(hname);
475 for (ap = hp->h_aliases; *ap; ap++)
476 if (wildmat(*ap, cname))
484 * Check if @ai's hostname or aliases fall in a given netgroup.
485 * Return 1 if @ai represents a host in the netgroup, otherwise
490 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
492 const char *netgroup = clp->m_hostname + 1;
493 const char *hname = ai->ai_canonname;
494 struct addrinfo *tmp = NULL;
499 /* First, try to match the hostname without
500 * splitting off the domain */
501 if (innetgr(netgroup, hname, NULL, NULL))
504 /* See if hname aliases listed in /etc/hosts or nis[+]
505 * match the requested netgroup */
506 hp = gethostbyname(hname);
508 for (i = 0; hp->h_aliases[i]; i++)
509 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
513 /* If hname is ip address convert to FQDN */
514 tmp = host_pton(hname);
517 if (innetgr(netgroup, hname, NULL, NULL))
521 /* Okay, strip off the domain (if we have one) */
522 dot = strchr(hname, '.');
527 match = innetgr(netgroup, hname, NULL, NULL);
532 #else /* !HAVE_INNETGR */
534 check_netgroup(__attribute__((unused)) const nfs_client *clp,
535 __attribute__((unused)) const struct addrinfo *ai)
539 #endif /* !HAVE_INNETGR */
542 * client_check - check if IP address information matches a cached nfs_client
543 * @clp: pointer to a cached nfs_client record
544 * @ai: pointer to addrinfo to compare it with
546 * Returns 1 if the address information matches the cached nfs_client,
550 client_check(const nfs_client *clp, const struct addrinfo *ai)
552 switch (clp->m_type) {
554 return check_fqdn(clp, ai);
556 return check_subnetwork(clp, ai);
558 return check_wildcard(clp, ai);
560 return check_netgroup(clp, ai);
566 xlog(D_GENERAL, "%s: unrecognized client type: %d",
567 __func__, clp->m_type);
574 * client_gettype - determine type of nfs_client given an identifier
575 * @ident: '\0'-terminated ASCII string containing a client identifier
577 * Returns the type of nfs_client record that would be used for
581 client_gettype(char *ident)
585 if (ident[0] == '\0' || strcmp(ident, "*")==0)
586 return MCL_ANONYMOUS;
587 if (strncmp(ident, "gss/", 4) == 0)
589 if (ident[0] == '@') {
591 xlog(L_WARNING, "netgroup support not compiled in");
595 for (sp = ident; *sp; sp++) {
596 if (*sp == '*' || *sp == '?' || *sp == '[')
599 return MCL_SUBNETWORK;
600 if (*sp == '\\' && sp[1])
603 /* check for N.N.N.N */
605 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
606 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
607 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
608 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
609 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
610 return MCL_SUBNETWORK;