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