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