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