]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
9259180dd1d618f4a36196becade0751769df821
[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 hostent *hp)
39 {
40         char **ap;
41         int i;
42
43         if (hp == NULL)
44                 return;
45
46         ap = hp->h_addr_list;
47         for (i = 0; *ap != NULL && i < NFSCLNT_ADDRMAX; i++, ap++)
48                 clp->m_addrlist[i] = *(struct in_addr *)*ap;
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         if (strchr(slash + 1, '.') != NULL)
64                 clp->m_addrlist[1].s_addr = inet_addr(slash + 1);
65         else {
66                 int prefixlen = atoi(slash + 1);
67                 if (0 < prefixlen && prefixlen <= 32)
68                         clp->m_addrlist[1].s_addr =
69                                         htonl((uint32_t)~0 << (32 - prefixlen));
70                 else
71                         goto out_badprefix;
72         }
73
74         return 1;
75
76 out_badprefix:
77         xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
78         return 0;
79 }
80
81 static int
82 init_subnetwork(nfs_client *clp)
83 {
84         static char slash32[] = "/32";
85         char *cp;
86
87         cp = strchr(clp->m_hostname, '/');
88         if (cp == NULL)
89                 cp = slash32;
90
91         *cp = '\0';
92         clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
93         *cp = '/';
94
95         return init_netmask(clp, cp);
96 }
97
98 static int
99 client_init(nfs_client *clp, const char *hname, const struct hostent *hp)
100 {
101         clp->m_hostname = strdup(hname);
102         if (clp->m_hostname == NULL)
103                 return 0;
104
105         clp->m_exported = 0;
106         clp->m_count = 0;
107         clp->m_naddr = 0;
108
109         if (clp->m_type == MCL_SUBNETWORK)
110                 return init_subnetwork(clp);
111
112         init_addrlist(clp, hp);
113         return 1;
114 }
115
116 /* if canonical is set, then we *know* this is already a canonical name
117  * so hostname lookup is avoided.
118  * This is used when reading /proc/fs/nfs/exports
119  */
120 nfs_client *
121 client_lookup(char *hname, int canonical)
122 {
123         nfs_client      *clp = NULL;
124         int             htype;
125         struct hostent  *hp = NULL;
126
127         htype = client_gettype(hname);
128
129         if (htype == MCL_FQDN && !canonical) {
130                 struct hostent *hp2;
131                 hp = gethostbyname(hname);
132                 if (hp == NULL || hp->h_addrtype != AF_INET) {
133                         xlog(L_ERROR, "%s has non-inet addr", hname);
134                         return NULL;
135                 }
136                 /* make sure we have canonical name */
137                 hp2 = hostent_dup(hp);
138                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
139                                    hp2->h_addrtype);
140                 if (hp) {
141                         hp = hostent_dup(hp);
142                         /* but now we might not have all addresses... */
143                         if (hp2->h_addr_list[1]) {
144                                 struct hostent *hp3 =
145                                         gethostbyname(hp->h_name);
146                                 if (hp3) {
147                                         free(hp);
148                                         hp = hostent_dup(hp3);
149                                 }
150                         }
151                         free(hp2);
152                 } else
153                         hp = hp2;
154
155                 hname = (char *) hp->h_name;
156
157                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
158                         if (client_check(clp, hp))
159                                 break;
160                 }
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, hp);
183
184 out:
185         if (hp)
186                 free (hp);
187
188         return clp;
189 }
190
191 nfs_client *
192 client_dup(nfs_client *clp, struct hostent *hp)
193 {
194         nfs_client              *new;
195
196         new = (nfs_client *)malloc(sizeof(*new));
197         if (new == NULL)
198                 return NULL;
199         memcpy(new, clp, sizeof(*new));
200         new->m_type = MCL_FQDN;
201         new->m_hostname = NULL;
202
203         if (!client_init(new, hp->h_name, hp)) {
204                 client_free(new);
205                 return NULL;
206         }
207         client_add(new);
208         return new;
209 }
210
211 void
212 client_add(nfs_client *clp)
213 {
214         nfs_client      **cpp;
215
216         if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
217                 xlog(L_FATAL, "unknown client type in client_add");
218         cpp = clientlist + clp->m_type;
219         while (*cpp)
220                 cpp = &((*cpp)->m_next);
221         clp->m_next = NULL;
222         *cpp = clp;
223 }
224
225 void
226 client_release(nfs_client *clp)
227 {
228         if (clp->m_count <= 0)
229                 xlog(L_FATAL, "client_free: m_count <= 0!");
230         clp->m_count--;
231 }
232
233 void
234 client_freeall(void)
235 {
236         nfs_client      *clp, **head;
237         int             i;
238
239         for (i = 0; i < MCL_MAXTYPES; i++) {
240                 head = clientlist + i;
241                 while (*head) {
242                         *head = (clp = *head)->m_next;
243                         client_free(clp);
244                 }
245         }
246 }
247
248 struct hostent *
249 client_resolve(struct in_addr addr)
250 {
251         struct hostent *he = NULL;
252
253         if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
254                 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
255         if (he == NULL)
256                 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
257
258         return he;
259 }
260
261 /**
262  * client_compose - Make a list of cached hostnames that match an IP address
263  * @he: pointer to hostent 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(struct hostent *he)
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, he))
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 /*
364  * Check each address listed in @hp against each address
365  * stored in @clp.  Return 1 if a match is found, otherwise
366  * zero.
367  */
368 static int
369 check_fqdn(const nfs_client *clp, const struct hostent *hp)
370 {
371         struct in_addr addr;
372         char **ap;
373         int i;
374
375         for (ap = hp->h_addr_list; *ap; ap++) {
376                 addr = *(struct in_addr *)*ap;
377
378                 for (i = 0; i < clp->m_naddr; i++)
379                         if (clp->m_addrlist[i].s_addr == addr.s_addr)
380                                 return 1;
381         }
382         return 0;
383 }
384
385 /*
386  * Check each address listed in @hp against the subnetwork or
387  * host address stored in @clp.  Return 1 if an address in @hp
388  * matches the host address stored in @clp, otherwise zero.
389  */
390 static int
391 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
392 {
393         struct in_addr addr;
394         char **ap;
395
396         for (ap = hp->h_addr_list; *ap; ap++) {
397                 addr = *(struct in_addr *)*ap;
398
399                 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
400                       clp->m_addrlist[1].s_addr))
401                         return 1;
402         }
403         return 0;
404 }
405
406 /*
407  * Check if a wildcard nfs_client record matches the canonical name
408  * or the aliases of a host.  Return 1 if a match is found, otherwise
409  * zero.
410  */
411 static int
412 check_wildcard(const nfs_client *clp, const struct hostent *hp)
413 {
414         char *cname = clp->m_hostname;
415         char *hname = hp->h_name;
416         char **ap;
417
418         if (wildmat(hname, cname))
419                 return 1;
420
421         /* See if hname aliases listed in /etc/hosts or nis[+]
422          * match the requested wildcard */
423         for (ap = hp->h_aliases; *ap; ap++) {
424                 if (wildmat(*ap, cname))
425                         return 1;
426         }
427
428         return 0;
429 }
430
431 /*
432  * Check if @hp's hostname or aliases fall in a given netgroup.
433  * Return 1 if @hp represents a host in the netgroup, otherwise zero.
434  */
435 #ifdef HAVE_INNETGR
436 static int
437 check_netgroup(const nfs_client *clp, const struct hostent *hp)
438 {
439         const char *netgroup = clp->m_hostname + 1;
440         const char *hname = hp->h_name;
441         struct hostent *nhp = NULL;
442         struct sockaddr_in addr;
443         int match, i;
444         char *dot;
445
446         /* First, try to match the hostname without
447          * splitting off the domain */
448         if (innetgr(netgroup, hname, NULL, NULL))
449                 return 1;
450
451         /* See if hname aliases listed in /etc/hosts or nis[+]
452          * match the requested netgroup */
453         for (i = 0; hp->h_aliases[i]; i++) {
454                 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
455                         return 1;
456         }
457
458         /* If hname is ip address convert to FQDN */
459         if (inet_aton(hname, &addr.sin_addr) &&
460            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
461             sizeof(addr.sin_addr), AF_INET))) {
462                 hname = nhp->h_name;
463                 if (innetgr(netgroup, hname, NULL, NULL))
464                         return 1;
465         }
466
467         /* Okay, strip off the domain (if we have one) */
468         dot = strchr(hname, '.');
469         if (dot == NULL)
470                 return 0;
471
472         *dot = '\0';
473         match = innetgr(netgroup, hname, NULL, NULL);
474         *dot = '.';
475
476         return match;
477 }
478 #else   /* !HAVE_INNETGR */
479 static int
480 check_netgroup(__attribute__((unused)) const nfs_client *clp,
481                 __attribute__((unused)) const struct hostent *hp)
482 {
483         return 0;
484 }
485 #endif  /* !HAVE_INNETGR */
486
487 /**
488  * client_check - check if IP address information matches a cached nfs_client
489  * @clp: pointer to a cached nfs_client record
490  * @hp: pointer to hostent containing host IP information
491  *
492  * Returns 1 if the address information matches the cached nfs_client,
493  * otherwise zero.
494  */
495 int
496 client_check(nfs_client *clp, struct hostent *hp)
497 {
498         switch (clp->m_type) {
499         case MCL_FQDN:
500                 return check_fqdn(clp, hp);
501         case MCL_SUBNETWORK:
502                 return check_subnetwork(clp, hp);
503         case MCL_WILDCARD:
504                 return check_wildcard(clp, hp);
505         case MCL_NETGROUP:
506                 return check_netgroup(clp, hp);
507         case MCL_ANONYMOUS:
508                 return 1;
509         case MCL_GSS:
510                 return 0;
511         default:
512                 xlog(D_GENERAL, "%s: unrecognized client type: %d",
513                                 __func__, clp->m_type);
514         }
515
516         return 0;
517 }
518
519 int
520 client_gettype(char *ident)
521 {
522         char    *sp;
523
524         if (ident[0] == '\0' || strcmp(ident, "*")==0)
525                 return MCL_ANONYMOUS;
526         if (strncmp(ident, "gss/", 4) == 0)
527                 return MCL_GSS;
528         if (ident[0] == '@') {
529 #ifndef HAVE_INNETGR
530                 xlog(L_WARNING, "netgroup support not compiled in");
531 #endif
532                 return MCL_NETGROUP;
533         }
534         for (sp = ident; *sp; sp++) {
535                 if (*sp == '*' || *sp == '?' || *sp == '[')
536                         return MCL_WILDCARD;
537                 if (*sp == '/')
538                         return MCL_SUBNETWORK;
539                 if (*sp == '\\' && sp[1])
540                         sp++;
541         }
542         /* check for N.N.N.N */
543         sp = ident;
544         if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
545         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
546         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
547         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
548         /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
549         return MCL_SUBNETWORK;
550 }