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