]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
libexport.a: Factor SUBNETWORK checking out of check_client()
[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 static int      client_checkaddr(nfs_client *clp, struct in_addr addr);
36
37 nfs_client      *clientlist[MCL_MAXTYPES] = { NULL, };
38
39
40 /* if canonical is set, then we *know* this is already a canonical name
41  * so hostname lookup is avoided.
42  * This is used when reading /proc/fs/nfs/exports
43  */
44 nfs_client *
45 client_lookup(char *hname, int canonical)
46 {
47         nfs_client      *clp = NULL;
48         int             htype;
49         struct hostent  *hp = NULL;
50
51         htype = client_gettype(hname);
52
53         if (htype == MCL_FQDN && !canonical) {
54                 struct hostent *hp2;
55                 hp = gethostbyname(hname);
56                 if (hp == NULL || hp->h_addrtype != AF_INET) {
57                         xlog(L_ERROR, "%s has non-inet addr", hname);
58                         return NULL;
59                 }
60                 /* make sure we have canonical name */
61                 hp2 = hostent_dup(hp);
62                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
63                                    hp2->h_addrtype);
64                 if (hp) {
65                         hp = hostent_dup(hp);
66                         /* but now we might not have all addresses... */
67                         if (hp2->h_addr_list[1]) {
68                                 struct hostent *hp3 =
69                                         gethostbyname(hp->h_name);
70                                 if (hp3) {
71                                         free(hp);
72                                         hp = hostent_dup(hp3);
73                                 }
74                         }
75                         free(hp2);
76                 } else
77                         hp = hp2;
78
79                 hname = (char *) hp->h_name;
80
81                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
82                         if (client_check(clp, hp))
83                                 break;
84                 }
85         } else {
86                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
87                         if (strcasecmp(hname, clp->m_hostname)==0)
88                                 break;
89                 }
90         }
91
92         if (!clp) {
93                 clp = (nfs_client *) xmalloc(sizeof(*clp));
94                 memset(clp, 0, sizeof(*clp));
95                 clp->m_type = htype;
96                 client_init(clp, hname, NULL);
97                 client_add(clp);
98         }
99
100         if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
101                 char    **ap = hp->h_addr_list;
102                 int     i;
103
104                 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
105                         clp->m_addrlist[i] = *(struct in_addr *)*ap;
106                 clp->m_naddr = i;
107         }
108
109         if (hp)
110                 free (hp);
111
112         return clp;
113 }
114
115 nfs_client *
116 client_dup(nfs_client *clp, struct hostent *hp)
117 {
118         nfs_client              *new;
119
120         new = (nfs_client *) xmalloc(sizeof(*new));
121         memcpy(new, clp, sizeof(*new));
122         new->m_type = MCL_FQDN;
123         new->m_hostname = NULL;
124
125         client_init(new, (char *) hp->h_name, hp);
126         client_add(new);
127         return new;
128 }
129
130 static void
131 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
132 {
133         xfree(clp->m_hostname);
134         if (hp)
135                 clp->m_hostname = xstrdup(hp->h_name);
136         else
137                 clp->m_hostname = xstrdup(hname);
138
139         clp->m_exported = 0;
140         clp->m_count = 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                 clp->m_naddr = 0;
165         } else if (!hp) {
166                 clp->m_naddr = 0;
167         } else {
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  * Match a host (given its hostent record) to a client record. This
376  * is usually called from mountd.
377  */
378 int
379 client_check(nfs_client *clp, struct hostent *hp)
380 {
381         char    *hname = (char *) hp->h_name;
382         char    *cname = clp->m_hostname;
383         char    **ap;
384
385         switch (clp->m_type) {
386         case MCL_FQDN:
387                 return check_fqdn(clp, hp);
388         case MCL_SUBNETWORK:
389                 return check_subnetwork(clp, hp);
390         case MCL_WILDCARD:
391                 if (wildmat(hname, cname))
392                         return 1;
393                 else {
394                         for (ap = hp->h_aliases; *ap; ap++)
395                                 if (wildmat(*ap, cname))
396                                         return 1;
397                 }
398                 return 0;
399         case MCL_NETGROUP:
400 #ifdef HAVE_INNETGR
401                 {
402                         char    *dot;
403                         int     match, i;
404                         struct hostent *nhp = NULL;
405                         struct sockaddr_in addr;
406
407                         /* First, try to match the hostname without
408                          * splitting off the domain */
409                         if (innetgr(cname+1, hname, NULL, NULL))
410                                 return 1;
411
412                         /* try the aliases as well */
413                         for (i = 0; hp->h_aliases[i]; i++) {
414                                 if (innetgr(cname+1, hp->h_aliases[i], NULL, NULL))
415                                         return 1;
416                         }
417
418                         /* If hname is ip address convert to FQDN */
419                         if (inet_aton(hname, &addr.sin_addr) &&
420                            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
421                             sizeof(addr.sin_addr), AF_INET))) {
422                                 hname = (char *)nhp->h_name;
423                                 if (innetgr(cname+1, hname, NULL, NULL))
424                                         return 1;
425                         }
426
427                         /* Okay, strip off the domain (if we have one) */
428                         if ((dot = strchr(hname, '.')) == NULL)
429                                 return 0;
430
431                         *dot = '\0';
432                         match = innetgr(cname+1, hname, NULL, NULL);
433                         *dot = '.';
434
435                         return match;
436                 }
437 #else
438                 return 0;
439 #endif
440         case MCL_ANONYMOUS:
441                 return 1;
442         case MCL_GSS:
443                 return 0;
444         default:
445                 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
446         }
447
448         return 0;
449 }
450
451 static int
452 client_checkaddr(nfs_client *clp, struct in_addr addr)
453 {
454         return 0;
455 }
456
457 int
458 client_gettype(char *ident)
459 {
460         char    *sp;
461
462         if (ident[0] == '\0' || strcmp(ident, "*")==0)
463                 return MCL_ANONYMOUS;
464         if (strncmp(ident, "gss/", 4) == 0)
465                 return MCL_GSS;
466         if (ident[0] == '@') {
467 #ifndef HAVE_INNETGR
468                 xlog(L_WARNING, "netgroup support not compiled in");
469 #endif
470                 return MCL_NETGROUP;
471         }
472         for (sp = ident; *sp; sp++) {
473                 if (*sp == '*' || *sp == '?' || *sp == '[')
474                         return MCL_WILDCARD;
475                 if (*sp == '/')
476                         return MCL_SUBNETWORK;
477                 if (*sp == '\\' && sp[1])
478                         sp++;
479         }
480         /* check for N.N.N.N */
481         sp = ident;
482         if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
483         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
484         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
485         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
486         /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
487         return MCL_SUBNETWORK;
488 }