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