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