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