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 hostent *hp)
40 struct sockaddr_in sin = {
41 .sin_family = AF_INET,
50 for (i = 0; *ap != NULL && i < NFSCLNT_ADDRMAX; i++, ap++) {
51 sin.sin_addr = *(struct in_addr *)*ap;
52 set_addrlist_in(clp, i, &sin);
58 client_free(nfs_client *clp)
60 free(clp->m_hostname);
65 init_netmask(nfs_client *clp, const char *slash)
67 struct sockaddr_in sin = {
68 .sin_family = AF_INET,
71 if (strchr(slash + 1, '.') != NULL)
72 sin.sin_addr.s_addr = inet_addr(slash + 1);
74 int prefixlen = atoi(slash + 1);
75 if (0 < prefixlen && prefixlen <= 32)
77 htonl((uint32_t)~0 << (32 - prefixlen));
82 set_addrlist_in(clp, 1, &sin);
86 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
91 init_subnetwork(nfs_client *clp)
93 struct sockaddr_in sin = {
94 .sin_family = AF_INET,
96 static char slash32[] = "/32";
99 cp = strchr(clp->m_hostname, '/');
104 sin.sin_addr.s_addr = inet_addr(clp->m_hostname);
105 set_addrlist_in(clp, 0, &sin);
108 return init_netmask(clp, cp);
112 client_init(nfs_client *clp, const char *hname, const struct hostent *hp)
114 clp->m_hostname = strdup(hname);
115 if (clp->m_hostname == NULL)
122 if (clp->m_type == MCL_SUBNETWORK)
123 return init_subnetwork(clp);
125 init_addrlist(clp, hp);
130 client_add(nfs_client *clp)
134 cpp = &clientlist[clp->m_type];
136 cpp = &((*cpp)->m_next);
141 /* if canonical is set, then we *know* this is already a canonical name
142 * so hostname lookup is avoided.
143 * This is used when reading /proc/fs/nfs/exports
146 client_lookup(char *hname, int canonical)
148 nfs_client *clp = NULL;
150 struct hostent *hp = NULL;
152 htype = client_gettype(hname);
154 if (htype == MCL_FQDN && !canonical) {
156 hp = gethostbyname(hname);
157 if (hp == NULL || hp->h_addrtype != AF_INET) {
158 xlog(L_ERROR, "%s has non-inet addr", hname);
161 /* make sure we have canonical name */
162 hp2 = hostent_dup(hp);
163 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
166 hp = hostent_dup(hp);
167 /* but now we might not have all addresses... */
168 if (hp2->h_addr_list[1]) {
169 struct hostent *hp3 =
170 gethostbyname(hp->h_name);
173 hp = hostent_dup(hp3);
180 hname = (char *) hp->h_name;
182 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
183 if (client_check(clp, hp))
187 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
188 if (strcasecmp(hname, clp->m_hostname)==0)
194 clp = calloc(1, sizeof(*clp));
198 if (!client_init(clp, hname, NULL)) {
206 if (htype == MCL_FQDN && clp->m_naddr == 0)
207 init_addrlist(clp, hp);
217 client_dup(nfs_client *clp, struct hostent *hp)
221 new = (nfs_client *)malloc(sizeof(*new));
224 memcpy(new, clp, sizeof(*new));
225 new->m_type = MCL_FQDN;
226 new->m_hostname = NULL;
228 if (!client_init(new, hp->h_name, hp)) {
237 client_release(nfs_client *clp)
239 if (clp->m_count <= 0)
240 xlog(L_FATAL, "client_free: m_count <= 0!");
247 nfs_client *clp, **head;
250 for (i = 0; i < MCL_MAXTYPES; i++) {
251 head = clientlist + i;
253 *head = (clp = *head)->m_next;
260 client_resolve(struct in_addr addr)
262 struct hostent *he = NULL;
264 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
265 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
267 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
273 * client_compose - Make a list of cached hostnames that match an IP address
274 * @he: pointer to hostent containing IP address information to match
276 * Gather all known client hostnames that match the IP address, and sort
277 * the result into a comma-separated list.
279 * Returns a '\0'-terminated ASCII string containing a comma-separated
280 * sorted list of client hostnames, or NULL if no client records matched
281 * the IP address or memory could not be allocated. Caller must free the
282 * returned string with free(3).
285 client_compose(struct hostent *he)
290 for (i = 0 ; i < MCL_MAXTYPES; i++) {
292 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
293 if (!client_check(clp, he))
295 name = add_name(name, clp->m_hostname);
302 * client_member - check if @name is contained in the list @client
303 * @client: '\0'-terminated ASCII string containing
304 * comma-separated list of hostnames
305 * @name: '\0'-terminated ASCII string containing hostname to look for
307 * Returns 1 if @name was found in @client, otherwise zero is returned.
310 client_member(const char *client, const char *name)
312 size_t l = strlen(name);
315 if (strncmp(client, name, l) == 0 &&
316 (client[l] == ',' || client[l] == '\0'))
318 client = strchr(client, ',');
327 name_cmp(const char *a, const char *b)
329 /* compare strings a and b, but only upto ',' in a */
330 while (*a && *b && *a != ',' && *a == *b)
332 if (!*b && (!*a || *a == ','))
335 if (!*a || *a == ',') return -1;
340 add_name(char *old, const char *add)
342 size_t len = strlen(add) + 2;
345 if (old) len += strlen(old);
353 while (cp && *cp && name_cmp(cp, add) < 0) {
354 /* step cp forward over a name */
355 char *e = strchr(cp, ',');
359 cp = cp + strlen(cp);
361 strncpy(new, old, cp-old);
363 if (cp != old && !*cp)
375 * Check each address listed in @hp against each address
376 * stored in @clp. Return 1 if a match is found, otherwise
380 check_fqdn(const nfs_client *clp, const struct hostent *hp)
382 const struct sockaddr_in *sin;
387 for (ap = hp->h_addr_list; *ap; ap++) {
388 addr = *(struct in_addr *)*ap;
390 for (i = 0; i < clp->m_naddr; i++) {
391 sin = get_addrlist_in(clp, i);
392 if (sin->sin_addr.s_addr == addr.s_addr)
400 * Check each address listed in @hp against the subnetwork or
401 * host address stored in @clp. Return 1 if an address in @hp
402 * matches the host address stored in @clp, otherwise zero.
405 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
407 const struct sockaddr_in *address, *mask;
411 for (ap = hp->h_addr_list; *ap; ap++) {
412 address = get_addrlist_in(clp, 0);
413 mask = get_addrlist_in(clp, 1);
414 addr = *(struct in_addr *)*ap;
416 if (!((address->sin_addr.s_addr ^ addr.s_addr) &
417 mask->sin_addr.s_addr))
424 * Check if a wildcard nfs_client record matches the canonical name
425 * or the aliases of a host. Return 1 if a match is found, otherwise
429 check_wildcard(const nfs_client *clp, const struct hostent *hp)
431 char *cname = clp->m_hostname;
432 char *hname = hp->h_name;
435 if (wildmat(hname, cname))
438 /* See if hname aliases listed in /etc/hosts or nis[+]
439 * match the requested wildcard */
440 for (ap = hp->h_aliases; *ap; ap++) {
441 if (wildmat(*ap, cname))
449 * Check if @hp's hostname or aliases fall in a given netgroup.
450 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
454 check_netgroup(const nfs_client *clp, const struct hostent *hp)
456 const char *netgroup = clp->m_hostname + 1;
457 const char *hname = hp->h_name;
458 struct hostent *nhp = NULL;
459 struct sockaddr_in addr;
463 /* First, try to match the hostname without
464 * splitting off the domain */
465 if (innetgr(netgroup, hname, NULL, NULL))
468 /* See if hname aliases listed in /etc/hosts or nis[+]
469 * match the requested netgroup */
470 for (i = 0; hp->h_aliases[i]; i++) {
471 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
475 /* If hname is ip address convert to FQDN */
476 if (inet_aton(hname, &addr.sin_addr) &&
477 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
478 sizeof(addr.sin_addr), AF_INET))) {
480 if (innetgr(netgroup, hname, NULL, NULL))
484 /* Okay, strip off the domain (if we have one) */
485 dot = strchr(hname, '.');
490 match = innetgr(netgroup, hname, NULL, NULL);
495 #else /* !HAVE_INNETGR */
497 check_netgroup(__attribute__((unused)) const nfs_client *clp,
498 __attribute__((unused)) const struct hostent *hp)
502 #endif /* !HAVE_INNETGR */
505 * client_check - check if IP address information matches a cached nfs_client
506 * @clp: pointer to a cached nfs_client record
507 * @hp: pointer to hostent containing host IP information
509 * Returns 1 if the address information matches the cached nfs_client,
513 client_check(nfs_client *clp, struct hostent *hp)
515 switch (clp->m_type) {
517 return check_fqdn(clp, hp);
519 return check_subnetwork(clp, hp);
521 return check_wildcard(clp, hp);
523 return check_netgroup(clp, hp);
529 xlog(D_GENERAL, "%s: unrecognized client type: %d",
530 __func__, clp->m_type);
537 client_gettype(char *ident)
541 if (ident[0] == '\0' || strcmp(ident, "*")==0)
542 return MCL_ANONYMOUS;
543 if (strncmp(ident, "gss/", 4) == 0)
545 if (ident[0] == '@') {
547 xlog(L_WARNING, "netgroup support not compiled in");
551 for (sp = ident; *sp; sp++) {
552 if (*sp == '*' || *sp == '?' || *sp == '[')
555 return MCL_SUBNETWORK;
556 if (*sp == '\\' && sp[1])
559 /* check for N.N.N.N */
561 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
562 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
563 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
564 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
565 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
566 return MCL_SUBNETWORK;