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