]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
eafc2bfa6d79f3f5a532d23717a9a2df5c666a91
[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 static int      client_init(nfs_client *clp, const char *hname,
34                                         struct hostent *hp);
35
36 nfs_client      *clientlist[MCL_MAXTYPES] = { NULL, };
37
38
39 static void
40 init_addrlist(nfs_client *clp, const struct hostent *hp)
41 {
42         char **ap;
43         int i;
44
45         if (hp == NULL)
46                 return;
47
48         ap = hp->h_addr_list;
49         for (i = 0; *ap != NULL && i < NFSCLNT_ADDRMAX; i++, ap++)
50                 clp->m_addrlist[i] = *(struct in_addr *)*ap;
51
52         clp->m_naddr = i;
53 }
54
55 static void
56 client_free(nfs_client *clp)
57 {
58         free(clp->m_hostname);
59         free(clp);
60 }
61
62 /* if canonical is set, then we *know* this is already a canonical name
63  * so hostname lookup is avoided.
64  * This is used when reading /proc/fs/nfs/exports
65  */
66 nfs_client *
67 client_lookup(char *hname, int canonical)
68 {
69         nfs_client      *clp = NULL;
70         int             htype;
71         struct hostent  *hp = NULL;
72
73         htype = client_gettype(hname);
74
75         if (htype == MCL_FQDN && !canonical) {
76                 struct hostent *hp2;
77                 hp = gethostbyname(hname);
78                 if (hp == NULL || hp->h_addrtype != AF_INET) {
79                         xlog(L_ERROR, "%s has non-inet addr", hname);
80                         return NULL;
81                 }
82                 /* make sure we have canonical name */
83                 hp2 = hostent_dup(hp);
84                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
85                                    hp2->h_addrtype);
86                 if (hp) {
87                         hp = hostent_dup(hp);
88                         /* but now we might not have all addresses... */
89                         if (hp2->h_addr_list[1]) {
90                                 struct hostent *hp3 =
91                                         gethostbyname(hp->h_name);
92                                 if (hp3) {
93                                         free(hp);
94                                         hp = hostent_dup(hp3);
95                                 }
96                         }
97                         free(hp2);
98                 } else
99                         hp = hp2;
100
101                 hname = (char *) hp->h_name;
102
103                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
104                         if (client_check(clp, hp))
105                                 break;
106                 }
107         } else {
108                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
109                         if (strcasecmp(hname, clp->m_hostname)==0)
110                                 break;
111                 }
112         }
113
114         if (clp == NULL) {
115                 clp = calloc(1, sizeof(*clp));
116                 if (clp == NULL)
117                         goto out;
118                 clp->m_type = htype;
119                 if (!client_init(clp, hname, NULL)) {
120                         client_free(clp);
121                         clp = NULL;
122                         goto out;
123                 }
124                 client_add(clp);
125         }
126
127         if (htype == MCL_FQDN && clp->m_naddr == 0)
128                 init_addrlist(clp, hp);
129
130 out:
131         if (hp)
132                 free (hp);
133
134         return clp;
135 }
136
137 nfs_client *
138 client_dup(nfs_client *clp, struct hostent *hp)
139 {
140         nfs_client              *new;
141
142         new = (nfs_client *)malloc(sizeof(*new));
143         if (new == NULL)
144                 return NULL;
145         memcpy(new, clp, sizeof(*new));
146         new->m_type = MCL_FQDN;
147         new->m_hostname = NULL;
148
149         if (!client_init(new, hp->h_name, hp)) {
150                 client_free(new);
151                 return NULL;
152         }
153         client_add(new);
154         return new;
155 }
156
157 static int
158 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
159 {
160         clp->m_hostname = strdup(hname);
161         if (clp->m_hostname == NULL)
162                 return 0;
163
164         clp->m_exported = 0;
165         clp->m_count = 0;
166         clp->m_naddr = 0;
167
168         if (clp->m_type == MCL_SUBNETWORK) {
169                 char    *cp = strchr(clp->m_hostname, '/');
170                 static char slash32[] = "/32";
171
172                 if(!cp) cp = slash32;
173                 *cp = '\0';
174                 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
175                 if (strchr(cp + 1, '.')) {
176                         clp->m_addrlist[1].s_addr = inet_addr(cp+1);
177                 }
178                 else {
179                         int netmask = atoi(cp + 1);
180                         if (0 < netmask && netmask <= 32) {
181                                 clp->m_addrlist[1].s_addr =
182                                         htonl ((uint32_t) ~0 << (32 - netmask));
183                         }
184                         else {
185                                 xlog(L_ERROR, "invalid netmask `%s' for %s",
186                                              cp + 1, clp->m_hostname);
187                                 return 0;
188                         }
189                 }
190                 *cp = '/';
191                 return 1;
192         }
193         
194         init_addrlist(clp, hp);
195         return 1;
196 }
197
198 void
199 client_add(nfs_client *clp)
200 {
201         nfs_client      **cpp;
202
203         if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
204                 xlog(L_FATAL, "unknown client type in client_add");
205         cpp = clientlist + clp->m_type;
206         while (*cpp)
207                 cpp = &((*cpp)->m_next);
208         clp->m_next = NULL;
209         *cpp = clp;
210 }
211
212 void
213 client_release(nfs_client *clp)
214 {
215         if (clp->m_count <= 0)
216                 xlog(L_FATAL, "client_free: m_count <= 0!");
217         clp->m_count--;
218 }
219
220 void
221 client_freeall(void)
222 {
223         nfs_client      *clp, **head;
224         int             i;
225
226         for (i = 0; i < MCL_MAXTYPES; i++) {
227                 head = clientlist + i;
228                 while (*head) {
229                         *head = (clp = *head)->m_next;
230                         client_free(clp);
231                 }
232         }
233 }
234
235 struct hostent *
236 client_resolve(struct in_addr addr)
237 {
238         struct hostent *he = NULL;
239
240         if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
241                 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
242         if (he == NULL)
243                 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
244
245         return he;
246 }
247
248 /**
249  * client_compose - Make a list of cached hostnames that match an IP address
250  * @he: pointer to hostent containing IP address information to match
251  *
252  * Gather all known client hostnames that match the IP address, and sort
253  * the result into a comma-separated list.
254  *
255  * Returns a '\0'-terminated ASCII string containing a comma-separated
256  * sorted list of client hostnames, or NULL if no client records matched
257  * the IP address or memory could not be allocated.  Caller must free the
258  * returned string with free(3).
259  */
260 char *
261 client_compose(struct hostent *he)
262 {
263         char *name = NULL;
264         int i;
265
266         for (i = 0 ; i < MCL_MAXTYPES; i++) {
267                 nfs_client      *clp;
268                 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
269                         if (!client_check(clp, he))
270                                 continue;
271                         name = add_name(name, clp->m_hostname);
272                 }
273         }
274         return name;
275 }
276
277 /**
278  * client_member - check if @name is contained in the list @client
279  * @client: '\0'-terminated ASCII string containing
280  *              comma-separated list of hostnames
281  * @name: '\0'-terminated ASCII string containing hostname to look for
282  *
283  * Returns 1 if @name was found in @client, otherwise zero is returned.
284  */
285 int
286 client_member(const char *client, const char *name)
287 {
288         size_t l = strlen(name);
289
290         while (*client) {
291                 if (strncmp(client, name, l) == 0 &&
292                     (client[l] == ',' || client[l] == '\0'))
293                         return 1;
294                 client = strchr(client, ',');
295                 if (client == NULL)
296                         return 0;
297                 client++;
298         }
299         return 0;
300 }
301
302 static int
303 name_cmp(const char *a, const char *b)
304 {
305         /* compare strings a and b, but only upto ',' in a */
306         while (*a && *b && *a != ',' && *a == *b)
307                 a++, b++;
308         if (!*b && (!*a || *a == ','))
309                 return 0;
310         if (!*b) return 1;
311         if (!*a || *a == ',') return -1;
312         return *a - *b;
313 }
314
315 static char *
316 add_name(char *old, const char *add)
317 {
318         size_t len = strlen(add) + 2;
319         char *new;
320         char *cp;
321         if (old) len += strlen(old);
322         
323         new = malloc(len);
324         if (!new) {
325                 free(old);
326                 return NULL;
327         }
328         cp = old;
329         while (cp && *cp && name_cmp(cp, add) < 0) {
330                 /* step cp forward over a name */
331                 char *e = strchr(cp, ',');
332                 if (e)
333                         cp = e+1;
334                 else
335                         cp = cp + strlen(cp);
336         }
337         strncpy(new, old, cp-old);
338         new[cp-old] = 0;
339         if (cp != old && !*cp)
340                 strcat(new, ",");
341         strcat(new, add);
342         if (cp && *cp) {
343                 strcat(new, ",");
344                 strcat(new, cp);
345         }
346         free(old);
347         return new;
348 }
349
350 /*
351  * Check each address listed in @hp against each address
352  * stored in @clp.  Return 1 if a match is found, otherwise
353  * zero.
354  */
355 static int
356 check_fqdn(const nfs_client *clp, const struct hostent *hp)
357 {
358         struct in_addr addr;
359         char **ap;
360         int i;
361
362         for (ap = hp->h_addr_list; *ap; ap++) {
363                 addr = *(struct in_addr *)*ap;
364
365                 for (i = 0; i < clp->m_naddr; i++)
366                         if (clp->m_addrlist[i].s_addr == addr.s_addr)
367                                 return 1;
368         }
369         return 0;
370 }
371
372 /*
373  * Check each address listed in @hp against the subnetwork or
374  * host address stored in @clp.  Return 1 if an address in @hp
375  * matches the host address stored in @clp, otherwise zero.
376  */
377 static int
378 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
379 {
380         struct in_addr addr;
381         char **ap;
382
383         for (ap = hp->h_addr_list; *ap; ap++) {
384                 addr = *(struct in_addr *)*ap;
385
386                 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
387                       clp->m_addrlist[1].s_addr))
388                         return 1;
389         }
390         return 0;
391 }
392
393 /*
394  * Check if a wildcard nfs_client record matches the canonical name
395  * or the aliases of a host.  Return 1 if a match is found, otherwise
396  * zero.
397  */
398 static int
399 check_wildcard(const nfs_client *clp, const struct hostent *hp)
400 {
401         char *cname = clp->m_hostname;
402         char *hname = hp->h_name;
403         char **ap;
404
405         if (wildmat(hname, cname))
406                 return 1;
407
408         /* See if hname aliases listed in /etc/hosts or nis[+]
409          * match the requested wildcard */
410         for (ap = hp->h_aliases; *ap; ap++) {
411                 if (wildmat(*ap, cname))
412                         return 1;
413         }
414
415         return 0;
416 }
417
418 /*
419  * Check if @hp's hostname or aliases fall in a given netgroup.
420  * Return 1 if @hp represents a host in the netgroup, otherwise zero.
421  */
422 #ifdef HAVE_INNETGR
423 static int
424 check_netgroup(const nfs_client *clp, const struct hostent *hp)
425 {
426         const char *netgroup = clp->m_hostname + 1;
427         const char *hname = hp->h_name;
428         struct hostent *nhp = NULL;
429         struct sockaddr_in addr;
430         int match, i;
431         char *dot;
432
433         /* First, try to match the hostname without
434          * splitting off the domain */
435         if (innetgr(netgroup, hname, NULL, NULL))
436                 return 1;
437
438         /* See if hname aliases listed in /etc/hosts or nis[+]
439          * match the requested netgroup */
440         for (i = 0; hp->h_aliases[i]; i++) {
441                 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
442                         return 1;
443         }
444
445         /* If hname is ip address convert to FQDN */
446         if (inet_aton(hname, &addr.sin_addr) &&
447            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
448             sizeof(addr.sin_addr), AF_INET))) {
449                 hname = nhp->h_name;
450                 if (innetgr(netgroup, hname, NULL, NULL))
451                         return 1;
452         }
453
454         /* Okay, strip off the domain (if we have one) */
455         dot = strchr(hname, '.');
456         if (dot == NULL)
457                 return 0;
458
459         *dot = '\0';
460         match = innetgr(netgroup, hname, NULL, NULL);
461         *dot = '.';
462
463         return match;
464 }
465 #else   /* !HAVE_INNETGR */
466 static int
467 check_netgroup(__attribute__((unused)) const nfs_client *clp,
468                 __attribute__((unused)) const struct hostent *hp)
469 {
470         return 0;
471 }
472 #endif  /* !HAVE_INNETGR */
473
474 /**
475  * client_check - check if IP address information matches a cached nfs_client
476  * @clp: pointer to a cached nfs_client record
477  * @hp: pointer to hostent containing host IP information
478  *
479  * Returns 1 if the address information matches the cached nfs_client,
480  * otherwise zero.
481  */
482 int
483 client_check(nfs_client *clp, struct hostent *hp)
484 {
485         switch (clp->m_type) {
486         case MCL_FQDN:
487                 return check_fqdn(clp, hp);
488         case MCL_SUBNETWORK:
489                 return check_subnetwork(clp, hp);
490         case MCL_WILDCARD:
491                 return check_wildcard(clp, hp);
492         case MCL_NETGROUP:
493                 return check_netgroup(clp, hp);
494         case MCL_ANONYMOUS:
495                 return 1;
496         case MCL_GSS:
497                 return 0;
498         default:
499                 xlog(D_GENERAL, "%s: unrecognized client type: %d",
500                                 __func__, clp->m_type);
501         }
502
503         return 0;
504 }
505
506 int
507 client_gettype(char *ident)
508 {
509         char    *sp;
510
511         if (ident[0] == '\0' || strcmp(ident, "*")==0)
512                 return MCL_ANONYMOUS;
513         if (strncmp(ident, "gss/", 4) == 0)
514                 return MCL_GSS;
515         if (ident[0] == '@') {
516 #ifndef HAVE_INNETGR
517                 xlog(L_WARNING, "netgroup support not compiled in");
518 #endif
519                 return MCL_NETGROUP;
520         }
521         for (sp = ident; *sp; sp++) {
522                 if (*sp == '*' || *sp == '?' || *sp == '[')
523                         return MCL_WILDCARD;
524                 if (*sp == '/')
525                         return MCL_SUBNETWORK;
526                 if (*sp == '\\' && sp[1])
527                         sp++;
528         }
529         /* check for N.N.N.N */
530         sp = ident;
531         if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
532         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
533         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
534         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
535         /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
536         return MCL_SUBNETWORK;
537 }