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