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