]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
see changelog
[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  * Match a host (given its hostent record) to a client record. This
234  * is usually called from mountd.
235  */
236 int
237 client_check(nfs_client *clp, struct hostent *hp)
238 {
239         char    *hname = (char *) hp->h_name;
240         char    *cname = clp->m_hostname;
241         char    **ap;
242
243         switch (clp->m_type) {
244         case MCL_FQDN:
245         case MCL_SUBNETWORK:
246                 for (ap = hp->h_addr_list; *ap; ap++) {
247                         if (client_checkaddr(clp, *(struct in_addr *) *ap))
248                                 return 1;
249                 }
250                 return 0;
251         case MCL_WILDCARD:
252                 if (wildmat(hname, cname))
253                         return 1;
254                 else {
255                         for (ap = hp->h_aliases; *ap; ap++)
256                                 if (wildmat(*ap, cname))
257                                         return 1;
258                 }
259                 return 0;
260         case MCL_NETGROUP:
261 #ifdef HAVE_INNETGR
262                 {
263                         char    *dot;
264                         int     match;
265                         struct hostent *nhp = NULL;
266                         struct sockaddr_in addr;
267
268                         /* First, try to match the hostname without
269                          * splitting off the domain */
270                         if (innetgr(cname+1, hname, NULL, NULL))
271                                 return 1;
272
273                         /* If hname is ip address convert to FQDN */
274                         if (inet_aton(hname, &addr.sin_addr) &&
275                            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
276                             sizeof(addr.sin_addr), AF_INET))) {
277                                 hname = (char *)nhp->h_name;
278                                 if (innetgr(cname+1, hname, NULL, NULL))
279                                         return 1;
280                         }
281
282                         /* Okay, strip off the domain (if we have one) */
283                         if ((dot = strchr(hname, '.')) == NULL)
284                                 return 0;
285
286                         *dot = '\0';
287                         match = innetgr(cname+1, hname, NULL, NULL);
288                         *dot = '.';
289
290                         return match;
291                 }
292 #else
293                 return 0;
294 #endif
295         case MCL_ANONYMOUS:
296                 return 1;
297         default:
298                 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
299         }
300
301         return 0;
302 }
303
304 static int
305 client_checkaddr(nfs_client *clp, struct in_addr addr)
306 {
307         int     i;
308
309         switch (clp->m_type) {
310         case MCL_FQDN:
311                 for (i = 0; i < clp->m_naddr; i++) {
312                         if (clp->m_addrlist[i].s_addr == addr.s_addr)
313                                 return 1;
314                 }
315                 return 0;
316         case MCL_SUBNETWORK:
317                 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
318                         & clp->m_addrlist[1].s_addr);
319         }
320         return 0;
321 }
322
323 int
324 client_gettype(char *ident)
325 {
326         char    *sp;
327
328         if (ident[0] == '\0' || strcmp(ident, "*")==0)
329                 return MCL_ANONYMOUS;
330         if (ident[0] == '@') {
331 #ifndef HAVE_INNETGR
332                 xlog(L_WARNING, "netgroup support not compiled in");
333 #endif
334                 return MCL_NETGROUP;
335         }
336         for (sp = ident; *sp; sp++) {
337                 if (*sp == '*' || *sp == '?' || *sp == '[')
338                         return MCL_WILDCARD;
339                 if (*sp == '/')
340                         return MCL_SUBNETWORK;
341                 if (*sp == '\\' && sp[1])
342                         sp++;
343         }
344         return MCL_FQDN;
345 }