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