]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
dbfc2b1449bfe251a74f5e8d08026e5e2ba63d40
[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 }
182 #endif  /* IPV6_SUPPORTED */
183
184 /*
185  * Parse the network mask for M_SUBNETWORK type clients.
186  *
187  * Return TRUE if successful, or FALSE if some error occurred.
188  */
189 static int
190 init_subnetwork(nfs_client *clp)
191 {
192         struct addrinfo *ai;
193         sa_family_t family;
194         int result = 0;
195         char *slash;
196
197         slash = strchr(clp->m_hostname, '/');
198         if (slash != NULL) {
199                 *slash = '\0';
200                 ai = host_pton(clp->m_hostname);
201                 *slash = '/';
202         } else
203                 ai = host_pton(clp->m_hostname);
204         if (ai == NULL) {
205                 xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
206                 return result;
207         }
208
209         set_addrlist(clp, 0, ai->ai_addr);
210         family = ai->ai_addr->sa_family;
211
212         freeaddrinfo(ai);
213
214         switch (family) {
215         case AF_INET:
216                 result = init_netmask4(clp, slash);
217                 break;
218         case AF_INET6:
219                 result = init_netmask6(clp, slash);
220                 break;
221         default:
222                 xlog(L_ERROR, "Unsupported address family for %s",
223                         clp->m_hostname);
224         }
225
226         return result;
227 }
228
229 static int
230 client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
231 {
232         clp->m_hostname = strdup(hname);
233         if (clp->m_hostname == NULL)
234                 return 0;
235
236         clp->m_exported = 0;
237         clp->m_count = 0;
238         clp->m_naddr = 0;
239
240         if (clp->m_type == MCL_SUBNETWORK)
241                 return init_subnetwork(clp);
242
243         init_addrlist(clp, ai);
244         return 1;
245 }
246
247 static void
248 client_add(nfs_client *clp)
249 {
250         nfs_client **cpp;
251
252         cpp = &clientlist[clp->m_type];
253         while (*cpp != NULL)
254                 cpp = &((*cpp)->m_next);
255         clp->m_next = NULL;
256         *cpp = clp;
257 }
258
259 /**
260  * client_lookup - look for @hname in our list of cached nfs_clients
261  * @hname: '\0'-terminated ASCII string containing hostname to look for
262  * @canonical: if set, @hname is known to be canonical DNS name
263  *
264  * Returns pointer to a matching or freshly created nfs_client.  NULL
265  * is returned if some problem occurs.
266  */
267 nfs_client *
268 client_lookup(char *hname, int canonical)
269 {
270         nfs_client      *clp = NULL;
271         int             htype;
272         struct addrinfo *ai = NULL;
273
274         htype = client_gettype(hname);
275
276         if (htype == MCL_FQDN && !canonical) {
277                 ai = host_addrinfo(hname);
278                 if (!ai) {
279                         xlog(L_ERROR, "Failed to resolve %s", hname);
280                         goto out;
281                 }
282                 hname = ai->ai_canonname;
283
284                 for (clp = clientlist[htype]; clp; clp = clp->m_next)
285                         if (client_check(clp, ai))
286                                 break;
287         } else {
288                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
289                         if (strcasecmp(hname, clp->m_hostname)==0)
290                                 break;
291                 }
292         }
293
294         if (clp == NULL) {
295                 clp = calloc(1, sizeof(*clp));
296                 if (clp == NULL)
297                         goto out;
298                 clp->m_type = htype;
299                 if (!client_init(clp, hname, NULL)) {
300                         client_free(clp);
301                         clp = NULL;
302                         goto out;
303                 }
304                 client_add(clp);
305         }
306
307         if (htype == MCL_FQDN && clp->m_naddr == 0)
308                 init_addrlist(clp, ai);
309
310 out:
311         freeaddrinfo(ai);
312         return clp;
313 }
314
315 /**
316  * client_dup - create a copy of an nfs_client
317  * @clp: pointer to nfs_client to copy
318  * @ai: pointer to addrinfo used to initialize the new client's addrlist
319  *
320  * Returns a dynamically allocated nfs_client if successful, or
321  * NULL if some problem occurs.  Caller must free the returned
322  * nfs_client with free(3).
323  */
324 nfs_client *
325 client_dup(const nfs_client *clp, const struct addrinfo *ai)
326 {
327         nfs_client              *new;
328
329         new = (nfs_client *)malloc(sizeof(*new));
330         if (new == NULL)
331                 return NULL;
332         memcpy(new, clp, sizeof(*new));
333         new->m_type = MCL_FQDN;
334         new->m_hostname = NULL;
335
336         if (!client_init(new, ai->ai_canonname, ai)) {
337                 client_free(new);
338                 return NULL;
339         }
340         client_add(new);
341         return new;
342 }
343
344 /**
345  * client_release - drop a reference to an nfs_client record
346  *
347  */
348 void
349 client_release(nfs_client *clp)
350 {
351         if (clp->m_count <= 0)
352                 xlog(L_FATAL, "client_free: m_count <= 0!");
353         clp->m_count--;
354 }
355
356 /**
357  * client_freeall - deallocate all nfs_client records
358  *
359  */
360 void
361 client_freeall(void)
362 {
363         nfs_client      *clp, **head;
364         int             i;
365
366         for (i = 0; i < MCL_MAXTYPES; i++) {
367                 head = clientlist + i;
368                 while (*head) {
369                         *head = (clp = *head)->m_next;
370                         client_free(clp);
371                 }
372         }
373 }
374
375 /**
376  * client_resolve - look up an IP address
377  * @sap: pointer to socket address to resolve
378  *
379  * Returns an addrinfo structure, or NULL if some problem occurred.
380  * Caller must free the result with freeaddrinfo(3).
381  */
382 struct addrinfo *
383 client_resolve(const struct sockaddr *sap)
384 {
385         struct addrinfo *ai = NULL;
386
387         if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
388                 ai = host_reliable_addrinfo(sap);
389         if (ai == NULL)
390                 ai = host_numeric_addrinfo(sap);
391
392         return ai;
393 }
394
395 /**
396  * client_compose - Make a list of cached hostnames that match an IP address
397  * @ai: pointer to addrinfo containing IP address information to match
398  *
399  * Gather all known client hostnames that match the IP address, and sort
400  * the result into a comma-separated list.
401  *
402  * Returns a '\0'-terminated ASCII string containing a comma-separated
403  * sorted list of client hostnames, or NULL if no client records matched
404  * the IP address or memory could not be allocated.  Caller must free the
405  * returned string with free(3).
406  */
407 char *
408 client_compose(const struct addrinfo *ai)
409 {
410         char *name = NULL;
411         int i;
412
413         for (i = 0 ; i < MCL_MAXTYPES; i++) {
414                 nfs_client      *clp;
415                 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
416                         if (!client_check(clp, ai))
417                                 continue;
418                         name = add_name(name, clp->m_hostname);
419                 }
420         }
421         return name;
422 }
423
424 /**
425  * client_member - check if @name is contained in the list @client
426  * @client: '\0'-terminated ASCII string containing
427  *              comma-separated list of hostnames
428  * @name: '\0'-terminated ASCII string containing hostname to look for
429  *
430  * Returns 1 if @name was found in @client, otherwise zero is returned.
431  */
432 int
433 client_member(const char *client, const char *name)
434 {
435         size_t l = strlen(name);
436
437         while (*client) {
438                 if (strncmp(client, name, l) == 0 &&
439                     (client[l] == ',' || client[l] == '\0'))
440                         return 1;
441                 client = strchr(client, ',');
442                 if (client == NULL)
443                         return 0;
444                 client++;
445         }
446         return 0;
447 }
448
449 static int
450 name_cmp(const char *a, const char *b)
451 {
452         /* compare strings a and b, but only upto ',' in a */
453         while (*a && *b && *a != ',' && *a == *b)
454                 a++, b++;
455         if (!*b && (!*a || *a == ','))
456                 return 0;
457         if (!*b) return 1;
458         if (!*a || *a == ',') return -1;
459         return *a - *b;
460 }
461
462 static char *
463 add_name(char *old, const char *add)
464 {
465         size_t len = strlen(add) + 2;
466         char *new;
467         char *cp;
468         if (old) len += strlen(old);
469         
470         new = malloc(len);
471         if (!new) {
472                 free(old);
473                 return NULL;
474         }
475         cp = old;
476         while (cp && *cp && name_cmp(cp, add) < 0) {
477                 /* step cp forward over a name */
478                 char *e = strchr(cp, ',');
479                 if (e)
480                         cp = e+1;
481                 else
482                         cp = cp + strlen(cp);
483         }
484         strncpy(new, old, cp-old);
485         new[cp-old] = 0;
486         if (cp != old && !*cp)
487                 strcat(new, ",");
488         strcat(new, add);
489         if (cp && *cp) {
490                 strcat(new, ",");
491                 strcat(new, cp);
492         }
493         free(old);
494         return new;
495 }
496
497 /*
498  * Check each address listed in @ai against each address
499  * stored in @clp.  Return 1 if a match is found, otherwise
500  * zero.
501  */
502 static int
503 check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
504 {
505         int i;
506
507         for (; ai; ai = ai->ai_next)
508                 for (i = 0; i < clp->m_naddr; i++)
509                         if (nfs_compare_sockaddr(ai->ai_addr,
510                                                         get_addrlist(clp, i)))
511                                 return 1;
512
513         return 0;
514 }
515
516 static _Bool
517 mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
518 {
519         return ((a ^ b) & m) == 0;
520 }
521
522 static int
523 check_subnet_v4(const struct sockaddr_in *address,
524                 const struct sockaddr_in *mask, const struct addrinfo *ai)
525 {
526         for (; ai; ai = ai->ai_next) {
527                 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
528
529                 if (sin->sin_family != AF_INET)
530                         continue;
531
532                 if (mask_match(address->sin_addr.s_addr,
533                                 sin->sin_addr.s_addr,
534                                 mask->sin_addr.s_addr))
535                         return 1;
536         }
537         return 0;
538 }
539
540 #ifdef IPV6_SUPPORTED
541 static int
542 check_subnet_v6(const struct sockaddr_in6 *address,
543                 const struct sockaddr_in6 *mask, const struct addrinfo *ai)
544 {
545         for (; ai; ai = ai->ai_next) {
546                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
547
548                 if (sin6->sin6_family != AF_INET6)
549                         continue;
550
551                 if (mask_match(address->sin6_addr.s6_addr32[0],
552                                 sin6->sin6_addr.s6_addr32[0],
553                                 mask->sin6_addr.s6_addr32[0]) &&
554                     mask_match(address->sin6_addr.s6_addr32[1],
555                                 sin6->sin6_addr.s6_addr32[1],
556                                 mask->sin6_addr.s6_addr32[1]) &&
557                     mask_match(address->sin6_addr.s6_addr32[2],
558                                 sin6->sin6_addr.s6_addr32[2],
559                                 mask->sin6_addr.s6_addr32[2]) &&
560                     mask_match(address->sin6_addr.s6_addr32[3],
561                                 sin6->sin6_addr.s6_addr32[3],
562                                 mask->sin6_addr.s6_addr32[3]))
563                         return 1;
564         }
565         return 0;
566 }
567 #else   /* !IPV6_SUPPORTED */
568 static int
569 check_subnet_v6(const struct sockaddr_in6 *UNUSED(address),
570                 const struct sockaddr_in6 *UNUSED(mask),
571                 const struct addrinfo *UNUSED(ai))
572 {
573         return 0;
574 }
575 #endif  /* !IPV6_SUPPORTED */
576
577 /*
578  * Check each address listed in @ai against the subnetwork or
579  * host address stored in @clp.  Return 1 if an address in @hp
580  * matches the host address stored in @clp, otherwise zero.
581  */
582 static int
583 check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
584 {
585         switch (get_addrlist(clp, 0)->sa_family) {
586         case AF_INET:
587                 return check_subnet_v4(get_addrlist_in(clp, 0),
588                                 get_addrlist_in(clp, 1), ai);
589         case AF_INET6:
590                 return check_subnet_v6(get_addrlist_in6(clp, 0),
591                                 get_addrlist_in6(clp, 1), ai);
592         }
593
594         return 0;
595 }
596
597 /*
598  * Check if a wildcard nfs_client record matches the canonical name
599  * or the aliases of a host.  Return 1 if a match is found, otherwise
600  * zero.
601  */
602 static int
603 check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
604 {
605         char *cname = clp->m_hostname;
606         char *hname = ai->ai_canonname;
607         struct hostent *hp;
608         char **ap;
609
610         if (wildmat(hname, cname))
611                 return 1;
612
613         /* See if hname aliases listed in /etc/hosts or nis[+]
614          * match the requested wildcard */
615         hp = gethostbyname(hname);
616         if (hp != NULL) {
617                 for (ap = hp->h_aliases; *ap; ap++)
618                         if (wildmat(*ap, cname))
619                                 return 1;
620         }
621
622         return 0;
623 }
624
625 /*
626  * Check if @ai's hostname or aliases fall in a given netgroup.
627  * Return 1 if @ai represents a host in the netgroup, otherwise
628  * zero.
629  */
630 #ifdef HAVE_INNETGR
631 static int
632 check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
633 {
634         const char *netgroup = clp->m_hostname + 1;
635         struct addrinfo *tmp = NULL;
636         struct hostent *hp;
637         char *dot, *hname;
638         int i, match;
639
640         match = 0;
641
642         hname = strdup(ai->ai_canonname);
643         if (hname == NULL) {
644                 xlog(D_GENERAL, "%s: no memory for strdup", __func__);
645                 goto out;
646         }
647
648         /* First, try to match the hostname without
649          * splitting off the domain */
650         if (innetgr(netgroup, hname, NULL, NULL)) {
651                 match = 1;
652                 goto out;
653         }
654
655         /* See if hname aliases listed in /etc/hosts or nis[+]
656          * match the requested netgroup */
657         hp = gethostbyname(hname);
658         if (hp != NULL) {
659                 for (i = 0; hp->h_aliases[i]; i++)
660                         if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
661                                 match = 1;
662                                 goto out;
663                         }
664         }
665
666         /* If hname happens to be an IP address, convert it
667          * to a the canonical DNS name bound to this address. */
668         tmp = host_pton(hname);
669         if (tmp != NULL) {
670                 char *cname = host_canonname(tmp->ai_addr);
671                 freeaddrinfo(tmp);
672
673                 /* The resulting FQDN may be in our netgroup. */
674                 if (cname != NULL) {
675                         free(hname);
676                         hname = cname;
677                         if (innetgr(netgroup, hname, NULL, NULL)) {
678                                 match = 1;
679                                 goto out;
680                         }
681                 }
682         }
683
684         /* Okay, strip off the domain (if we have one) */
685         dot = strchr(hname, '.');
686         if (dot == NULL)
687                 goto out;
688
689         *dot = '\0';
690         match = innetgr(netgroup, hname, NULL, NULL);
691
692 out:
693         free(hname);
694         return match;
695 }
696 #else   /* !HAVE_INNETGR */
697 static int
698 check_netgroup(__attribute__((unused)) const nfs_client *clp,
699                 __attribute__((unused)) const struct addrinfo *ai)
700 {
701         return 0;
702 }
703 #endif  /* !HAVE_INNETGR */
704
705 /**
706  * client_check - check if IP address information matches a cached nfs_client
707  * @clp: pointer to a cached nfs_client record
708  * @ai: pointer to addrinfo to compare it with
709  *
710  * Returns 1 if the address information matches the cached nfs_client,
711  * otherwise zero.
712  */
713 int
714 client_check(const nfs_client *clp, const struct addrinfo *ai)
715 {
716         switch (clp->m_type) {
717         case MCL_FQDN:
718                 return check_fqdn(clp, ai);
719         case MCL_SUBNETWORK:
720                 return check_subnetwork(clp, ai);
721         case MCL_WILDCARD:
722                 return check_wildcard(clp, ai);
723         case MCL_NETGROUP:
724                 return check_netgroup(clp, ai);
725         case MCL_ANONYMOUS:
726                 return 1;
727         case MCL_GSS:
728                 return 0;
729         default:
730                 xlog(D_GENERAL, "%s: unrecognized client type: %d",
731                                 __func__, clp->m_type);
732         }
733
734         return 0;
735 }
736
737 /**
738  * client_gettype - determine type of nfs_client given an identifier
739  * @ident: '\0'-terminated ASCII string containing a client identifier
740  *
741  * Returns the type of nfs_client record that would be used for
742  * this client.
743  */
744 int
745 client_gettype(char *ident)
746 {
747         struct addrinfo *ai;
748         char *sp;
749
750         if (ident[0] == '\0' || strcmp(ident, "*")==0)
751                 return MCL_ANONYMOUS;
752         if (strncmp(ident, "gss/", 4) == 0)
753                 return MCL_GSS;
754         if (ident[0] == '@') {
755 #ifndef HAVE_INNETGR
756                 xlog(L_WARNING, "netgroup support not compiled in");
757 #endif
758                 return MCL_NETGROUP;
759         }
760         for (sp = ident; *sp; sp++) {
761                 if (*sp == '*' || *sp == '?' || *sp == '[')
762                         return MCL_WILDCARD;
763                 if (*sp == '/')
764                         return MCL_SUBNETWORK;
765                 if (*sp == '\\' && sp[1])
766                         sp++;
767         }
768
769         /*
770          * Treat unadorned IP addresses as MCL_SUBNETWORK.
771          * Everything else is MCL_FQDN.
772          */
773         ai = host_pton(ident);
774         if (ai != NULL) {
775                 freeaddrinfo(ai);
776                 return MCL_SUBNETWORK;
777         }
778
779         return MCL_FQDN;
780 }