]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
Imported Debian patch 1.0.8-6
[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 <netdb.h>
19 #include "xmalloc.h"
20 #include "misc.h"
21 #include "nfslib.h"
22 #include "exportfs.h"
23
24 /* netgroup stuff never seems to be defined in any header file. Linux is
25  * not alone in this.
26  */
27 #if !defined(__GLIBC__) || __GLIBC__ < 2
28 extern int      innetgr(char *netgr, char *host, char *, char *);
29 #endif
30 static void     client_init(nfs_client *clp, const char *hname,
31                                         struct hostent *hp);
32 static int      client_checkaddr(nfs_client *clp, struct in_addr addr);
33
34 nfs_client      *clientlist[MCL_MAXTYPES] = { NULL, };
35
36
37 /* if canonical is set, then we *know* this is already a canonical name
38  * so hostname lookup is avoided.
39  * This is used when reading /proc/fs/nfs/exports
40  */
41 nfs_client *
42 client_lookup(char *hname, int canonical)
43 {
44         nfs_client      *clp = NULL;
45         int             htype;
46         struct hostent  *hp = NULL;
47
48         htype = client_gettype(hname);
49
50         if (htype == MCL_FQDN && !canonical) {
51                 struct hostent *hp2;
52                 hp = gethostbyname(hname);
53                 if (hp == NULL || hp->h_addrtype != AF_INET) {
54                         xlog(L_ERROR, "%s has non-inet addr", hname);
55                         return NULL;
56                 }
57                 /* make sure we have canonical name */
58                 hp2 = hostent_dup(hp);
59                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
60                                    hp2->h_addrtype);
61                 if (hp) {
62                         hp = hostent_dup(hp);
63                         /* but now we might not have all addresses... */
64                         if (hp2->h_addr_list[1]) {
65                                 struct hostent *hp3 =
66                                         gethostbyname(hp->h_name);
67                                 if (hp3) {
68                                         free(hp);
69                                         hp = hostent_dup(hp3);
70                                 }
71                         }
72                         free(hp2);
73                 } else
74                         hp = hp2;
75
76                 hname = (char *) hp->h_name;
77
78                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
79                         if (client_check(clp, hp))
80                                 break;
81                 }
82         } else {
83                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
84                         if (strcasecmp(hname, clp->m_hostname)==0)
85                                 break;
86                 }
87         }
88
89         if (!clp) {
90                 clp = (nfs_client *) xmalloc(sizeof(*clp));
91                 memset(clp, 0, sizeof(*clp));
92                 clp->m_type = htype;
93                 client_init(clp, hname, NULL);
94                 client_add(clp);
95         }
96
97         if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
98                 char    **ap = hp->h_addr_list;
99                 int     i;
100
101                 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
102                         clp->m_addrlist[i] = *(struct in_addr *)*ap;
103                 clp->m_naddr = i;
104         }
105
106         if (hp)
107                 free (hp);
108
109         return clp;
110 }
111
112 nfs_client *
113 client_dup(nfs_client *clp, struct hostent *hp)
114 {
115         nfs_client              *new;
116
117         new = (nfs_client *) xmalloc(sizeof(*new));
118         memcpy(new, clp, sizeof(*new));
119         new->m_type = MCL_FQDN;
120
121         client_init(new, (char *) hp->h_name, hp);
122         client_add(new);
123         return new;
124 }
125
126 static void
127 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
128 {
129         if (hp) {
130                 strncpy(clp->m_hostname, hp->h_name,
131                         sizeof (clp->m_hostname) -  1);
132         } else {
133                 strncpy(clp->m_hostname, hname,
134                         sizeof (clp->m_hostname) - 1);
135         }
136         clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
137
138         clp->m_exported = 0;
139         clp->m_count = 0;
140
141         if (clp->m_type == MCL_SUBNETWORK) {
142                 char    *cp = strchr(clp->m_hostname, '/');
143                 static char slash32[] = "/32";
144
145                 if(!cp) cp = slash32;
146                 *cp = '\0';
147                 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
148                 if (strchr(cp + 1, '.')) {
149                         clp->m_addrlist[1].s_addr = inet_addr(cp+1);
150                 }
151                 else {
152                         int netmask = atoi(cp + 1);
153                         if (0 < netmask && netmask <= 32) {
154                                 clp->m_addrlist[1].s_addr =
155                                         htonl ((uint32_t) ~0 << (32 - netmask));
156                         }
157                         else {
158                                 xlog(L_FATAL, "invalid netmask `%s' for %s",
159                                      cp + 1, clp->m_hostname);
160                         }
161                 }
162                 *cp = '/';
163                 clp->m_naddr = 0;
164         } else if (!hp) {
165                 clp->m_naddr = 0;
166         } else {
167                 char    **ap = hp->h_addr_list;
168                 int     i;
169
170                 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
171                         clp->m_addrlist[i] = *(struct in_addr *)*ap;
172                 }
173                 clp->m_naddr = i;
174         }
175 }
176
177 void
178 client_add(nfs_client *clp)
179 {
180         nfs_client      **cpp;
181
182         if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
183                 xlog(L_FATAL, "unknown client type in client_add");
184         cpp = clientlist + clp->m_type;
185         while (*cpp)
186                 cpp = &((*cpp)->m_next);
187         clp->m_next = NULL;
188         *cpp = clp;
189 }
190
191 void
192 client_release(nfs_client *clp)
193 {
194         if (clp->m_count <= 0)
195                 xlog(L_FATAL, "client_free: m_count <= 0!");
196         clp->m_count--;
197 }
198
199 void
200 client_freeall(void)
201 {
202         nfs_client      *clp, **head;
203         int             i;
204
205         for (i = 0; i < MCL_MAXTYPES; i++) {
206                 head = clientlist + i;
207                 while (*head) {
208                         *head = (clp = *head)->m_next;
209                         xfree(clp);
210                 }
211         }
212 }
213
214 nfs_client *
215 client_find(struct hostent *hp)
216 {
217         nfs_client      *clp;
218         int             i;
219
220         for (i = 0; i < MCL_MAXTYPES; i++) {
221                 for (clp = clientlist[i]; clp; clp = clp->m_next) {
222                         if (!client_check(clp, hp))
223                                 continue;
224 #ifdef notdef
225                         if (clp->m_type == MCL_FQDN)
226                                 return clp;
227                         return client_dup(clp, hp);
228 #else
229                         return clp;
230 #endif
231                 }
232         }
233         return NULL;
234 }
235
236 /*
237  * Find client name given an IP address
238  * This is found by gathering all known names that match that IP address,
239  * sorting them and joining them with '+'
240  *
241  */
242 static char *add_name(char *old, char *add);
243
244 char *
245 client_compose(struct in_addr addr)
246 {
247         struct hostent *he = NULL;
248         char *name = NULL;
249         int i;
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         for (i = 0 ; i < MCL_MAXTYPES; i++) {
257                 nfs_client      *clp;
258                 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
259                         if (!client_check(clp, he))
260                                 continue;
261                         name = add_name(name, clp->m_hostname);
262                 }
263         }
264         return name;
265 }
266
267 int
268 client_member(char *client, char *name)
269 {
270         /* check if "client" (a ',' separated list of names)
271          * contains 'name' as a member
272          */
273         int l = strlen(name);
274         while (*client) {
275                 if (strncmp(client, name, l) == 0 &&
276                     (client[l] == ',' || client[l] == '\0'))
277                         return 1;
278                 client = strchr(client, ',');
279                 if (client == NULL)
280                         return 0;
281                 client++;
282         }
283         return 0;
284 }
285
286
287 int
288 name_cmp(char *a, char *b)
289 {
290         /* compare strings a and b, but only upto ',' in a */
291         while (*a && *b && *a != ',' && *a == *b)
292                 a++, b++;
293         if (!*b && (!*a || !a == ',') )
294                 return 0;
295         if (!*b) return 1;
296         if (!*a || *a == ',') return -1;
297         return *a - *b;
298 }
299
300 static char *
301 add_name(char *old, char *add)
302 {
303         int len = strlen(add)+2;
304         char *new;
305         char *cp;
306         if (old) len += strlen(old);
307         
308         new = malloc(len);
309         if (!new) {
310                 free(old);
311                 return NULL;
312         }
313         cp = old;
314         while (cp && *cp && name_cmp(cp, add) < 0) {
315                 /* step cp forward over a name */
316                 char *e = strchr(cp, ',');
317                 if (e)
318                         cp = e+1;
319                 else
320                         cp = cp + strlen(cp);
321         }
322         strncpy(new, old, cp-old);
323         new[cp-old] = 0;
324         if (cp != old && !*cp)
325                 strcat(new, ",");
326         strcat(new, add);
327         if (cp && *cp) {
328                 strcat(new, ",");
329                 strcat(new, cp);
330         }
331         return new;
332 }
333
334 /*
335  * Match a host (given its hostent record) to a client record. This
336  * is usually called from mountd.
337  */
338 int
339 client_check(nfs_client *clp, struct hostent *hp)
340 {
341         char    *hname = (char *) hp->h_name;
342         char    *cname = clp->m_hostname;
343         char    **ap;
344
345         switch (clp->m_type) {
346         case MCL_FQDN:
347         case MCL_SUBNETWORK:
348                 for (ap = hp->h_addr_list; *ap; ap++) {
349                         if (client_checkaddr(clp, *(struct in_addr *) *ap))
350                                 return 1;
351                 }
352                 return 0;
353         case MCL_WILDCARD:
354                 if (wildmat(hname, cname))
355                         return 1;
356                 else {
357                         for (ap = hp->h_aliases; *ap; ap++)
358                                 if (wildmat(*ap, cname))
359                                         return 1;
360                 }
361                 return 0;
362         case MCL_NETGROUP:
363 #ifdef HAVE_INNETGR
364                 {
365                         char    *dot;
366                         int     match;
367                         struct hostent *nhp = NULL;
368                         struct sockaddr_in addr;
369
370                         /* First, try to match the hostname without
371                          * splitting off the domain */
372                         if (innetgr(cname+1, hname, NULL, NULL))
373                                 return 1;
374
375                         /* If hname is ip address convert to FQDN */
376                         if (inet_aton(hname, &addr.sin_addr) &&
377                            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
378                             sizeof(addr.sin_addr), AF_INET))) {
379                                 hname = (char *)nhp->h_name;
380                                 if (innetgr(cname+1, hname, NULL, NULL))
381                                         return 1;
382                         }
383
384                         /* Okay, strip off the domain (if we have one) */
385                         if ((dot = strchr(hname, '.')) == NULL)
386                                 return 0;
387
388                         *dot = '\0';
389                         match = innetgr(cname+1, hname, NULL, NULL);
390                         *dot = '.';
391
392                         return match;
393                 }
394 #else
395                 return 0;
396 #endif
397         case MCL_ANONYMOUS:
398                 return 1;
399         case MCL_GSS:
400                 return 0;
401         default:
402                 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
403         }
404
405         return 0;
406 }
407
408 static int
409 client_checkaddr(nfs_client *clp, struct in_addr addr)
410 {
411         int     i;
412
413         switch (clp->m_type) {
414         case MCL_FQDN:
415                 for (i = 0; i < clp->m_naddr; i++) {
416                         if (clp->m_addrlist[i].s_addr == addr.s_addr)
417                                 return 1;
418                 }
419                 return 0;
420         case MCL_SUBNETWORK:
421                 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
422                         & clp->m_addrlist[1].s_addr);
423         }
424         return 0;
425 }
426
427 int
428 client_gettype(char *ident)
429 {
430         char    *sp;
431
432         if (ident[0] == '\0' || strcmp(ident, "*")==0)
433                 return MCL_ANONYMOUS;
434         if (strncmp(ident, "gss/", 4) == 0)
435                 return MCL_GSS;
436         if (ident[0] == '@') {
437 #ifndef HAVE_INNETGR
438                 xlog(L_WARNING, "netgroup support not compiled in");
439 #endif
440                 return MCL_NETGROUP;
441         }
442         for (sp = ident; *sp; sp++) {
443                 if (*sp == '*' || *sp == '?' || *sp == '[')
444                         return MCL_WILDCARD;
445                 if (*sp == '/')
446                         return MCL_SUBNETWORK;
447                 if (*sp == '\\' && sp[1])
448                         sp++;
449         }
450         /* check for N.N.N.N */
451         sp = ident;
452         if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
453         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
454         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
455         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
456         /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
457         return MCL_SUBNETWORK;
458 }