]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
Merge branch 'sid'
[nfs-utils.git] / support / export / client.c
1 /*
2  * support/export/client.c
3  *
4  * Maintain list of nfsd clients.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <netdb.h>
20 #include <errno.h>
21
22 #include "sockaddr.h"
23 #include "misc.h"
24 #include "nfslib.h"
25 #include "exportfs.h"
26
27 /* netgroup stuff never seems to be defined in any header file. Linux is
28  * not alone in this.
29  */
30 #if !defined(__GLIBC__) || __GLIBC__ < 2
31 extern int      innetgr(char *netgr, char *host, char *, char *);
32 #endif
33
34 static char     *add_name(char *old, const char *add);
35
36 nfs_client      *clientlist[MCL_MAXTYPES] = { NULL, };
37
38
39 static void
40 init_addrlist(nfs_client *clp, const struct addrinfo *ai)
41 {
42         int i;
43
44         if (ai == NULL)
45                 return;
46
47         for (i = 0; (ai != NULL) && (i < NFSCLNT_ADDRMAX); i++) {
48                 set_addrlist(clp, i, ai->ai_addr);
49                 ai = ai->ai_next;
50         }
51
52         clp->m_naddr = i;
53 }
54
55 static void
56 client_free(nfs_client *clp)
57 {
58         free(clp->m_hostname);
59         free(clp);
60 }
61
62 static int
63 init_netmask4(nfs_client *clp, const char *slash)
64 {
65         struct sockaddr_in sin = {
66                 .sin_family             = AF_INET,
67         };
68         uint32_t shift;
69
70         /*
71          * Decide what kind of netmask was specified.  If there's
72          * no '/' present, assume the netmask is all ones.  If
73          * there is a '/' and at least one '.', look for a spelled-
74          * out netmask.  Otherwise, assume it was a prefixlen.
75          */
76         if (slash == NULL)
77                 shift = 0;
78         else {
79                 unsigned long prefixlen;
80
81                 if (strchr(slash + 1, '.') != NULL) {
82                         if (inet_pton(AF_INET, slash + 1,
83                                                 &sin.sin_addr.s_addr) == 0)
84                                 goto out_badmask;
85                         set_addrlist_in(clp, 1, &sin);
86                         return 1;
87                 } else {
88                         char *endptr;
89
90                         prefixlen = strtoul(slash + 1, &endptr, 10);
91                         if (*endptr != '\0' && prefixlen != ULONG_MAX &&
92                             errno != ERANGE)
93                                 goto out_badprefix;
94                 }
95                 if (prefixlen > 32)
96                         goto out_badprefix;
97                 shift = 32 - (uint32_t)prefixlen;
98         }
99
100         /*
101          * Now construct the full netmask bitmask in a sockaddr_in,
102          * and plant it in the nfs_client record.
103          */
104         sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
105         set_addrlist_in(clp, 1, &sin);
106
107         return 1;
108
109 out_badmask:
110         xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
111         return 0;
112
113 out_badprefix:
114         xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
115         return 0;
116 }
117
118 #ifdef IPV6_SUPPORTED
119 static int
120 init_netmask6(nfs_client *clp, const char *slash)
121 {
122         struct sockaddr_in6 sin6 = {
123                 .sin6_family            = AF_INET6,
124         };
125         unsigned long prefixlen;
126         uint32_t shift;
127         int i;
128
129         /*
130          * Decide what kind of netmask was specified.  If there's
131          * no '/' present, assume the netmask is all ones.  If
132          * there is a '/' and at least one ':', look for a spelled-
133          * out netmask.  Otherwise, assume it was a prefixlen.
134          */
135         if (slash == NULL)
136                 prefixlen = 128;
137         else {
138                 if (strchr(slash + 1, ':') != NULL) {
139                         if (!inet_pton(AF_INET6, slash + 1, &sin6.sin6_addr))
140                                 goto out_badmask;
141                         set_addrlist_in6(clp, 1, &sin6);
142                         return 1;
143                 } else {
144                         char *endptr;
145
146                         prefixlen = strtoul(slash + 1, &endptr, 10);
147                         if (*endptr != '\0' && prefixlen != ULONG_MAX &&
148                             errno != ERANGE)
149                                 goto out_badprefix;
150                 }
151                 if (prefixlen > 128)
152                         goto out_badprefix;
153         }
154
155         /*
156          * Now construct the full netmask bitmask in a sockaddr_in6,
157          * and plant it in the nfs_client record.
158          */
159         for (i = 0; prefixlen > 32; i++) {
160                 sin6.sin6_addr.s6_addr32[i] = 0xffffffff;
161                 prefixlen -= 32;
162         }
163         shift = 32 - (uint32_t)prefixlen;
164         sin6.sin6_addr.s6_addr32[i] = htonl((uint32_t)~0 << shift);
165         set_addrlist_in6(clp, 1, &sin6);
166
167         return 1;
168
169 out_badmask:
170         xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
171         return 0;
172
173 out_badprefix:
174         xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
175         return 0;
176 }
177 #else   /* IPV6_SUPPORTED */
178 static int
179 init_netmask6(nfs_client *UNUSED(clp), const char *UNUSED(slash))
180 {
181         return 0;
182 }
183 #endif  /* IPV6_SUPPORTED */
184
185 /*
186  * Parse the network mask for M_SUBNETWORK type clients.
187  *
188  * Return TRUE if successful, or FALSE if some error occurred.
189  */
190 static int
191 init_subnetwork(nfs_client *clp)
192 {
193         struct addrinfo *ai;
194         sa_family_t family;
195         int result = 0;
196         char *slash;
197
198         slash = strchr(clp->m_hostname, '/');
199         if (slash != NULL) {
200                 *slash = '\0';
201                 ai = host_pton(clp->m_hostname);
202                 *slash = '/';
203         } else
204                 ai = host_pton(clp->m_hostname);
205         if (ai == NULL) {
206                 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
207                 return result;
208         }
209
210         set_addrlist(clp, 0, ai->ai_addr);
211         family = ai->ai_addr->sa_family;
212
213         freeaddrinfo(ai);
214
215         switch (family) {
216         case AF_INET:
217                 result = init_netmask4(clp, slash);
218                 break;
219         case AF_INET6:
220                 result = init_netmask6(clp, slash);
221                 break;
222         default:
223                 xlog(L_ERROR, "Unsupported address family for %s",
224                         clp->m_hostname);
225         }
226
227         return result;
228 }
229
230 static int
231 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
232 {
233         clp->m_hostname = strdup(hname);
234         if (clp->m_hostname == NULL)
235                 return 0;
236
237         clp->m_exported = 0;
238         clp->m_count = 0;
239         clp->m_naddr = 0;
240
241         if (clp->m_type == MCL_SUBNETWORK)
242                 return init_subnetwork(clp);
243
244         init_addrlist(clp, ai);
245         return 1;
246 }
247
248 static void
249 client_add(nfs_client *clp)
250 {
251         nfs_client **cpp;
252
253         cpp = &clientlist[clp->m_type];
254         while (*cpp != NULL)
255                 cpp = &((*cpp)->m_next);
256         clp->m_next = NULL;
257         *cpp = clp;
258 }
259
260 /**
261  * client_lookup - look for @hname in our list of cached nfs_clients
262  * @hname: '\0'-terminated ASCII string containing hostname to look for
263  * @canonical: if set, @hname is known to be canonical DNS name
264  *
265  * Returns pointer to a matching or freshly created nfs_client.  NULL
266  * is returned if some problem occurs.
267  */
268 nfs_client *
269 client_lookup(char *hname, int canonical)
270 {
271         nfs_client      *clp = NULL;
272         int             htype;
273         struct addrinfo *ai = NULL;
274
275         htype = client_gettype(hname);
276
277         if (htype == MCL_FQDN && !canonical) {
278                 ai = host_addrinfo(hname);
279                 if (!ai) {
280                         xlog(L_ERROR, "Failed to resolve %s", hname);
281                         goto out;
282                 }
283                 hname = ai->ai_canonname;
284
285                 for (clp = clientlist[htype]; clp; clp = clp->m_next)
286                         if (client_check(clp, ai))
287                                 break;
288         } else {
289                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
290                         if (strcasecmp(hname, clp->m_hostname)==0)
291                                 break;
292                 }
293         }
294
295         if (clp == NULL) {
296                 clp = calloc(1, sizeof(*clp));
297                 if (clp == NULL)
298                         goto out;
299                 clp->m_type = htype;
300                 if (!client_init(clp, hname, NULL)) {
301                         client_free(clp);
302                         clp = NULL;
303                         goto out;
304                 }
305                 client_add(clp);
306         }
307
308         if (htype == MCL_FQDN && clp->m_naddr == 0)
309                 init_addrlist(clp, ai);
310
311 out:
312         freeaddrinfo(ai);
313         return clp;
314 }
315
316 /**
317  * client_dup - create a copy of an nfs_client
318  * @clp: pointer to nfs_client to copy
319  * @ai: pointer to addrinfo used to initialize the new client's addrlist
320  *
321  * Returns a dynamically allocated nfs_client if successful, or
322  * NULL if some problem occurs.  Caller must free the returned
323  * nfs_client with free(3).
324  */
325 nfs_client *
326 client_dup(const nfs_client *clp, const struct addrinfo *ai)
327 {
328         nfs_client              *new;
329
330         new = (nfs_client *)malloc(sizeof(*new));
331         if (new == NULL)
332                 return NULL;
333         memcpy(new, clp, sizeof(*new));
334         new->m_type = MCL_FQDN;
335         new->m_hostname = NULL;
336
337         if (!client_init(new, ai->ai_canonname, ai)) {
338                 client_free(new);
339                 return NULL;
340         }
341         client_add(new);
342         return new;
343 }
344
345 /**
346  * client_release - drop a reference to an nfs_client record
347  *
348  */
349 void
350 client_release(nfs_client *clp)
351 {
352         if (clp->m_count <= 0)
353                 xlog(L_FATAL, "client_free: m_count <= 0!");
354         clp->m_count--;
355 }
356
357 /**
358  * client_freeall - deallocate all nfs_client records
359  *
360  */
361 void
362 client_freeall(void)
363 {
364         nfs_client      *clp, **head;
365         int             i;
366
367         for (i = 0; i < MCL_MAXTYPES; i++) {
368                 head = clientlist + i;
369                 while (*head) {
370                         *head = (clp = *head)->m_next;
371                         client_free(clp);
372                 }
373         }
374 }
375
376 /**
377  * client_resolve - look up an IP address
378  * @sap: pointer to socket address to resolve
379  *
380  * Returns an addrinfo structure, or NULL if some problem occurred.
381  * Caller must free the result with freeaddrinfo(3).
382  */
383 struct addrinfo *
384 client_resolve(const struct sockaddr *sap)
385 {
386         struct addrinfo *ai = NULL;
387
388         if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
389                 ai = host_reliable_addrinfo(sap);
390         if (ai == NULL)
391                 ai = host_numeric_addrinfo(sap);
392
393         return ai;
394 }
395
396 /**
397  * client_compose - Make a list of cached hostnames that match an IP address
398  * @ai: pointer to addrinfo containing IP address information to match
399  *
400  * Gather all known client hostnames that match the IP address, and sort
401  * the result into a comma-separated list.
402  *
403  * Returns a '\0'-terminated ASCII string containing a comma-separated
404  * sorted list of client hostnames, or NULL if no client records matched
405  * the IP address or memory could not be allocated.  Caller must free the
406  * returned string with free(3).
407  */
408 char *
409 client_compose(const struct addrinfo *ai)
410 {
411         char *name = NULL;
412         int i;
413
414         for (i = 0 ; i < MCL_MAXTYPES; i++) {
415                 nfs_client      *clp;
416                 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
417                         if (!client_check(clp, ai))
418                                 continue;
419                         name = add_name(name, clp->m_hostname);
420                 }
421         }
422         return name;
423 }
424
425 /**
426  * client_member - check if @name is contained in the list @client
427  * @client: '\0'-terminated ASCII string containing
428  *              comma-separated list of hostnames
429  * @name: '\0'-terminated ASCII string containing hostname to look for
430  *
431  * Returns 1 if @name was found in @client, otherwise zero is returned.
432  */
433 int
434 client_member(const char *client, const char *name)
435 {
436         size_t l = strlen(name);
437
438         while (*client) {
439                 if (strncmp(client, name, l) == 0 &&
440                     (client[l] == ',' || client[l] == '\0'))
441                         return 1;
442                 client = strchr(client, ',');
443                 if (client == NULL)
444                         return 0;
445                 client++;
446         }
447         return 0;
448 }
449
450 static int
451 name_cmp(const char *a, const char *b)
452 {
453         /* compare strings a and b, but only upto ',' in a */
454         while (*a && *b && *a != ',' && *a == *b)
455                 a++, b++;
456         if (!*b && (!*a || *a == ','))
457                 return 0;
458         if (!*b) return 1;
459         if (!*a || *a == ',') return -1;
460         return *a - *b;
461 }
462
463 static char *
464 add_name(char *old, const char *add)
465 {
466         size_t len = strlen(add) + 2;
467         char *new;
468         char *cp;
469         if (old) len += strlen(old);
470         
471         new = malloc(len);
472         if (!new) {
473                 free(old);
474                 return NULL;
475         }
476         cp = old;
477         while (cp && *cp && name_cmp(cp, add) < 0) {
478                 /* step cp forward over a name */
479                 char *e = strchr(cp, ',');
480                 if (e)
481                         cp = e+1;
482                 else
483                         cp = cp + strlen(cp);
484         }
485         strncpy(new, old, cp-old);
486         new[cp-old] = 0;
487         if (cp != old && !*cp)
488                 strcat(new, ",");
489         strcat(new, add);
490         if (cp && *cp) {
491                 strcat(new, ",");
492                 strcat(new, cp);
493         }
494         free(old);
495         return new;
496 }
497
498 /*
499  * Check each address listed in @ai against each address
500  * stored in @clp.  Return 1 if a match is found, otherwise
501  * zero.
502  */
503 static int
504 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
505 {
506         int i;
507
508         for (; ai; ai = ai->ai_next)
509                 for (i = 0; i < clp->m_naddr; i++)
510                         if (nfs_compare_sockaddr(ai->ai_addr,
511                                                         get_addrlist(clp, i)))
512                                 return 1;
513
514         return 0;
515 }
516
517 static _Bool
518 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
519 {
520         return ((a ^ b) & m) == 0;
521 }
522
523 static int
524 check_subnet_v4(const struct sockaddr_in *address,
525                 const struct sockaddr_in *mask, const struct addrinfo *ai)
526 {
527         for (; ai; ai = ai->ai_next) {
528                 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
529
530                 if (sin->sin_family != AF_INET)
531                         continue;
532
533                 if (mask_match(address->sin_addr.s_addr,
534                                 sin->sin_addr.s_addr,
535                                 mask->sin_addr.s_addr))
536                         return 1;
537         }
538         return 0;
539 }
540
541 #ifdef IPV6_SUPPORTED
542 static int
543 check_subnet_v6(const struct sockaddr_in6 *address,
544                 const struct sockaddr_in6 *mask, const struct addrinfo *ai)
545 {
546         for (; ai; ai = ai->ai_next) {
547                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
548
549                 if (sin6->sin6_family != AF_INET6)
550                         continue;
551
552                 if (mask_match(address->sin6_addr.s6_addr32[0],
553                                 sin6->sin6_addr.s6_addr32[0],
554                                 mask->sin6_addr.s6_addr32[0]) &&
555                     mask_match(address->sin6_addr.s6_addr32[1],
556                                 sin6->sin6_addr.s6_addr32[1],
557                                 mask->sin6_addr.s6_addr32[1]) &&
558                     mask_match(address->sin6_addr.s6_addr32[2],
559                                 sin6->sin6_addr.s6_addr32[2],
560                                 mask->sin6_addr.s6_addr32[2]) &&
561                     mask_match(address->sin6_addr.s6_addr32[3],
562                                 sin6->sin6_addr.s6_addr32[3],
563                                 mask->sin6_addr.s6_addr32[3]))
564                         return 1;
565         }
566         return 0;
567 }
568 #else   /* !IPV6_SUPPORTED */
569 static int
570 check_subnet_v6(const struct sockaddr_in6 *UNUSED(address),
571                 const struct sockaddr_in6 *UNUSED(mask),
572                 const struct addrinfo *UNUSED(ai))
573 {
574         return 0;
575 }
576 #endif  /* !IPV6_SUPPORTED */
577
578 /*
579  * Check each address listed in @ai against the subnetwork or
580  * host address stored in @clp.  Return 1 if an address in @hp
581  * matches the host address stored in @clp, otherwise zero.
582  */
583 static int
584 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
585 {
586         switch (get_addrlist(clp, 0)->sa_family) {
587         case AF_INET:
588                 return check_subnet_v4(get_addrlist_in(clp, 0),
589                                 get_addrlist_in(clp, 1), ai);
590         case AF_INET6:
591                 return check_subnet_v6(get_addrlist_in6(clp, 0),
592                                 get_addrlist_in6(clp, 1), ai);
593         }
594
595         return 0;
596 }
597
598 /*
599  * Check if a wildcard nfs_client record matches the canonical name
600  * or the aliases of a host.  Return 1 if a match is found, otherwise
601  * zero.
602  */
603 static int
604 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
605 {
606         char *cname = clp->m_hostname;
607         char *hname = ai->ai_canonname;
608         struct hostent *hp;
609         char **ap;
610
611         if (wildmat(hname, cname))
612                 return 1;
613
614         /* See if hname aliases listed in /etc/hosts or nis[+]
615          * match the requested wildcard */
616         hp = gethostbyname(hname);
617         if (hp != NULL) {
618                 for (ap = hp->h_aliases; *ap; ap++)
619                         if (wildmat(*ap, cname))
620                                 return 1;
621         }
622
623         return 0;
624 }
625
626 /*
627  * Check if @ai's hostname or aliases fall in a given netgroup.
628  * Return 1 if @ai represents a host in the netgroup, otherwise
629  * zero.
630  */
631 #ifdef HAVE_INNETGR
632 static int
633 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
634 {
635         const char *netgroup = clp->m_hostname + 1;
636         struct addrinfo *tmp = NULL;
637         struct hostent *hp;
638         char *dot, *hname;
639         int i, match;
640
641         match = 0;
642
643         hname = strdup(ai->ai_canonname);
644         if (hname == NULL) {
645                 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
646                 goto out;
647         }
648
649         /* First, try to match the hostname without
650          * splitting off the domain */
651         if (innetgr(netgroup, hname, NULL, NULL)) {
652                 match = 1;
653                 goto out;
654         }
655
656         /* See if hname aliases listed in /etc/hosts or nis[+]
657          * match the requested netgroup */
658         hp = gethostbyname(hname);
659         if (hp != NULL) {
660                 for (i = 0; hp->h_aliases[i]; i++)
661                         if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
662                                 match = 1;
663                                 goto out;
664                         }
665         }
666
667         /* If hname happens to be an IP address, convert it
668          * to a the canonical DNS name bound to this address. */
669         tmp = host_pton(hname);
670         if (tmp != NULL) {
671                 char *cname = host_canonname(tmp->ai_addr);
672                 freeaddrinfo(tmp);
673
674                 /* The resulting FQDN may be in our netgroup. */
675                 if (cname != NULL) {
676                         free(hname);
677                         hname = cname;
678                         if (innetgr(netgroup, hname, NULL, NULL)) {
679                                 match = 1;
680                                 goto out;
681                         }
682                 }
683         }
684
685         /* Okay, strip off the domain (if we have one) */
686         dot = strchr(hname, '.');
687         if (dot == NULL)
688                 goto out;
689
690         *dot = '\0';
691         match = innetgr(netgroup, hname, NULL, NULL);
692
693 out:
694         free(hname);
695         return match;
696 }
697 #else   /* !HAVE_INNETGR */
698 static int
699 check_netgroup(__attribute__((unused)) const nfs_client *clp,
700                 __attribute__((unused)) const struct addrinfo *ai)
701 {
702         return 0;
703 }
704 #endif  /* !HAVE_INNETGR */
705
706 /**
707  * client_check - check if IP address information matches a cached nfs_client
708  * @clp: pointer to a cached nfs_client record
709  * @ai: pointer to addrinfo to compare it with
710  *
711  * Returns 1 if the address information matches the cached nfs_client,
712  * otherwise zero.
713  */
714 int
715 client_check(const nfs_client *clp, const struct addrinfo *ai)
716 {
717         switch (clp->m_type) {
718         case MCL_FQDN:
719                 return check_fqdn(clp, ai);
720         case MCL_SUBNETWORK:
721                 return check_subnetwork(clp, ai);
722         case MCL_WILDCARD:
723                 return check_wildcard(clp, ai);
724         case MCL_NETGROUP:
725                 return check_netgroup(clp, ai);
726         case MCL_ANONYMOUS:
727                 return 1;
728         case MCL_GSS:
729                 return 0;
730         default:
731                 xlog(D_GENERAL, "%s: unrecognized client type: %d",
732                                 __func__, clp->m_type);
733         }
734
735         return 0;
736 }
737
738 /**
739  * client_gettype - determine type of nfs_client given an identifier
740  * @ident: '\0'-terminated ASCII string containing a client identifier
741  *
742  * Returns the type of nfs_client record that would be used for
743  * this client.
744  */
745 int
746 client_gettype(char *ident)
747 {
748         struct addrinfo *ai;
749         char *sp;
750
751         if (ident[0] == '\0' || strcmp(ident, "*")==0)
752                 return MCL_ANONYMOUS;
753         if (strncmp(ident, "gss/", 4) == 0)
754                 return MCL_GSS;
755         if (ident[0] == '@') {
756 #ifndef HAVE_INNETGR
757                 xlog(L_WARNING, "netgroup support not compiled in");
758 #endif
759                 return MCL_NETGROUP;
760         }
761         for (sp = ident; *sp; sp++) {
762                 if (*sp == '*' || *sp == '?' || *sp == '[')
763                         return MCL_WILDCARD;
764                 if (*sp == '/')
765                         return MCL_SUBNETWORK;
766                 if (*sp == '\\' && sp[1])
767                         sp++;
768         }
769
770         /*
771          * Treat unadorned IP addresses as MCL_SUBNETWORK.
772          * Everything else is MCL_FQDN.
773          */
774         ai = host_pton(ident);
775         if (ai != NULL) {
776                 freeaddrinfo(ai);
777                 return MCL_SUBNETWORK;
778         }
779
780         return MCL_FQDN;
781 }