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