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