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