]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
rpc.mountd: create client_resolve and change client_compose to take a hostent arg
[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         new->m_hostname = NULL;
122
123         client_init(new, (char *) hp->h_name, hp);
124         client_add(new);
125         return new;
126 }
127
128 static void
129 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
130 {
131         xfree(clp->m_hostname);
132         if (hp)
133                 clp->m_hostname = xstrdup(hp->h_name);
134         else
135                 clp->m_hostname = xstrdup(hname);
136
137         clp->m_exported = 0;
138         clp->m_count = 0;
139
140         if (clp->m_type == MCL_SUBNETWORK) {
141                 char    *cp = strchr(clp->m_hostname, '/');
142                 static char slash32[] = "/32";
143
144                 if(!cp) cp = slash32;
145                 *cp = '\0';
146                 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
147                 if (strchr(cp + 1, '.')) {
148                         clp->m_addrlist[1].s_addr = inet_addr(cp+1);
149                 }
150                 else {
151                         int netmask = atoi(cp + 1);
152                         if (0 < netmask && netmask <= 32) {
153                                 clp->m_addrlist[1].s_addr =
154                                         htonl ((uint32_t) ~0 << (32 - netmask));
155                         }
156                         else {
157                                 xlog(L_FATAL, "invalid netmask `%s' for %s",
158                                      cp + 1, clp->m_hostname);
159                         }
160                 }
161                 *cp = '/';
162                 clp->m_naddr = 0;
163         } else if (!hp) {
164                 clp->m_naddr = 0;
165         } else {
166                 char    **ap = hp->h_addr_list;
167                 int     i;
168
169                 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
170                         clp->m_addrlist[i] = *(struct in_addr *)*ap;
171                 }
172                 clp->m_naddr = i;
173         }
174 }
175
176 void
177 client_add(nfs_client *clp)
178 {
179         nfs_client      **cpp;
180
181         if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
182                 xlog(L_FATAL, "unknown client type in client_add");
183         cpp = clientlist + clp->m_type;
184         while (*cpp)
185                 cpp = &((*cpp)->m_next);
186         clp->m_next = NULL;
187         *cpp = clp;
188 }
189
190 void
191 client_release(nfs_client *clp)
192 {
193         if (clp->m_count <= 0)
194                 xlog(L_FATAL, "client_free: m_count <= 0!");
195         clp->m_count--;
196 }
197
198 void
199 client_freeall(void)
200 {
201         nfs_client      *clp, **head;
202         int             i;
203
204         for (i = 0; i < MCL_MAXTYPES; i++) {
205                 head = clientlist + i;
206                 while (*head) {
207                         *head = (clp = *head)->m_next;
208                         xfree(clp->m_hostname);
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 struct hostent *
237 client_resolve(struct in_addr addr)
238 {
239         struct hostent *he = NULL;
240
241         if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
242                 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
243         if (he == NULL)
244                 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
245
246         return he;
247 }
248
249 /*
250  * Find client name given an IP address
251  * This is found by gathering all known names that match that IP address,
252  * sorting them and joining them with '+'
253  *
254  */
255 static char *add_name(char *old, char *add);
256
257 char *
258 client_compose(struct hostent *he)
259 {
260         char *name = NULL;
261         int i;
262
263         for (i = 0 ; i < MCL_MAXTYPES; i++) {
264                 nfs_client      *clp;
265                 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
266                         if (!client_check(clp, he))
267                                 continue;
268                         name = add_name(name, clp->m_hostname);
269                 }
270         }
271         return name;
272 }
273
274 int
275 client_member(char *client, char *name)
276 {
277         /* check if "client" (a ',' separated list of names)
278          * contains 'name' as a member
279          */
280         int l = strlen(name);
281         while (*client) {
282                 if (strncmp(client, name, l) == 0 &&
283                     (client[l] == ',' || client[l] == '\0'))
284                         return 1;
285                 client = strchr(client, ',');
286                 if (client == NULL)
287                         return 0;
288                 client++;
289         }
290         return 0;
291 }
292
293
294 int
295 name_cmp(char *a, char *b)
296 {
297         /* compare strings a and b, but only upto ',' in a */
298         while (*a && *b && *a != ',' && *a == *b)
299                 a++, b++;
300         if (!*b && (!*a || !a == ',') )
301                 return 0;
302         if (!*b) return 1;
303         if (!*a || *a == ',') return -1;
304         return *a - *b;
305 }
306
307 static char *
308 add_name(char *old, char *add)
309 {
310         int len = strlen(add)+2;
311         char *new;
312         char *cp;
313         if (old) len += strlen(old);
314         
315         new = malloc(len);
316         if (!new) {
317                 free(old);
318                 return NULL;
319         }
320         cp = old;
321         while (cp && *cp && name_cmp(cp, add) < 0) {
322                 /* step cp forward over a name */
323                 char *e = strchr(cp, ',');
324                 if (e)
325                         cp = e+1;
326                 else
327                         cp = cp + strlen(cp);
328         }
329         strncpy(new, old, cp-old);
330         new[cp-old] = 0;
331         if (cp != old && !*cp)
332                 strcat(new, ",");
333         strcat(new, add);
334         if (cp && *cp) {
335                 strcat(new, ",");
336                 strcat(new, cp);
337         }
338         free(old);
339         return new;
340 }
341
342 /*
343  * Match a host (given its hostent record) to a client record. This
344  * is usually called from mountd.
345  */
346 int
347 client_check(nfs_client *clp, struct hostent *hp)
348 {
349         char    *hname = (char *) hp->h_name;
350         char    *cname = clp->m_hostname;
351         char    **ap;
352
353         switch (clp->m_type) {
354         case MCL_FQDN:
355         case MCL_SUBNETWORK:
356                 for (ap = hp->h_addr_list; *ap; ap++) {
357                         if (client_checkaddr(clp, *(struct in_addr *) *ap))
358                                 return 1;
359                 }
360                 return 0;
361         case MCL_WILDCARD:
362                 if (wildmat(hname, cname))
363                         return 1;
364                 else {
365                         for (ap = hp->h_aliases; *ap; ap++)
366                                 if (wildmat(*ap, cname))
367                                         return 1;
368                 }
369                 return 0;
370         case MCL_NETGROUP:
371 #ifdef HAVE_INNETGR
372                 {
373                         char    *dot;
374                         int     match;
375                         struct hostent *nhp = NULL;
376                         struct sockaddr_in addr;
377
378                         /* First, try to match the hostname without
379                          * splitting off the domain */
380                         if (innetgr(cname+1, hname, NULL, NULL))
381                                 return 1;
382
383                         /* If hname is ip address convert to FQDN */
384                         if (inet_aton(hname, &addr.sin_addr) &&
385                            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
386                             sizeof(addr.sin_addr), AF_INET))) {
387                                 hname = (char *)nhp->h_name;
388                                 if (innetgr(cname+1, hname, NULL, NULL))
389                                         return 1;
390                         }
391
392                         /* Okay, strip off the domain (if we have one) */
393                         if ((dot = strchr(hname, '.')) == NULL)
394                                 return 0;
395
396                         *dot = '\0';
397                         match = innetgr(cname+1, hname, NULL, NULL);
398                         *dot = '.';
399
400                         return match;
401                 }
402 #else
403                 return 0;
404 #endif
405         case MCL_ANONYMOUS:
406                 return 1;
407         case MCL_GSS:
408                 return 0;
409         default:
410                 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
411         }
412
413         return 0;
414 }
415
416 static int
417 client_checkaddr(nfs_client *clp, struct in_addr addr)
418 {
419         int     i;
420
421         switch (clp->m_type) {
422         case MCL_FQDN:
423                 for (i = 0; i < clp->m_naddr; i++) {
424                         if (clp->m_addrlist[i].s_addr == addr.s_addr)
425                                 return 1;
426                 }
427                 return 0;
428         case MCL_SUBNETWORK:
429                 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
430                         & clp->m_addrlist[1].s_addr);
431         }
432         return 0;
433 }
434
435 int
436 client_gettype(char *ident)
437 {
438         char    *sp;
439
440         if (ident[0] == '\0' || strcmp(ident, "*")==0)
441                 return MCL_ANONYMOUS;
442         if (strncmp(ident, "gss/", 4) == 0)
443                 return MCL_GSS;
444         if (ident[0] == '@') {
445 #ifndef HAVE_INNETGR
446                 xlog(L_WARNING, "netgroup support not compiled in");
447 #endif
448                 return MCL_NETGROUP;
449         }
450         for (sp = ident; *sp; sp++) {
451                 if (*sp == '*' || *sp == '?' || *sp == '[')
452                         return MCL_WILDCARD;
453                 if (*sp == '/')
454                         return MCL_SUBNETWORK;
455                 if (*sp == '\\' && sp[1])
456                         sp++;
457         }
458         /* check for N.N.N.N */
459         sp = ident;
460         if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
461         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
462         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
463         sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
464         /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
465         return MCL_SUBNETWORK;
466 }