]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/client.c
03f791721774b5937642cf83d150a8c8442636f4
[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                         free(hp2);
61                         hp = hostent_dup(hp);
62                 } else
63                         hp = hp2;
64
65                 hname = (char *) hp->h_name;
66
67                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
68                         if (client_check(clp, hp))
69                                 break;
70                 }
71         } else {
72                 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
73                         if (strcasecmp(hname, clp->m_hostname)==0)
74                                 break;
75                 }
76         }
77
78         if (!clp) {
79                 clp = (nfs_client *) xmalloc(sizeof(*clp));
80                 memset(clp, 0, sizeof(*clp));
81                 clp->m_type = htype;
82                 client_init(clp, hname, NULL);
83                 client_add(clp);
84         }
85
86         if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
87                 char    **ap = hp->h_addr_list;
88                 int     i;
89
90                 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
91                         clp->m_addrlist[i] = *(struct in_addr *)*ap;
92                 clp->m_naddr = i;
93         }
94
95         if (hp)
96                 free (hp);
97
98         return clp;
99 }
100
101 nfs_client *
102 client_dup(nfs_client *clp, struct hostent *hp)
103 {
104         nfs_client              *new;
105
106         new = (nfs_client *) xmalloc(sizeof(*new));
107         memcpy(new, clp, sizeof(*new));
108         new->m_type = MCL_FQDN;
109
110         client_init(new, (char *) hp->h_name, hp);
111         client_add(new);
112         return new;
113 }
114
115 static void
116 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
117 {
118         if (hp) {
119                 strncpy(clp->m_hostname, hp->h_name,
120                         sizeof (clp->m_hostname) -  1);
121         } else {
122                 strncpy(clp->m_hostname, hname,
123                         sizeof (clp->m_hostname) - 1);
124         }
125         clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
126
127         clp->m_exported = 0;
128         clp->m_count = 0;
129
130         if (clp->m_type == MCL_SUBNETWORK) {
131                 char    *cp = strchr(clp->m_hostname, '/');
132
133                 *cp = '\0';
134                 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
135                 if (strchr(cp + 1, '.')) {
136                         clp->m_addrlist[1].s_addr = inet_addr(cp+1);
137                 }
138                 else {
139                         int netmask = atoi(cp + 1);
140                         if (0 < netmask && netmask <= 32) {
141                                 clp->m_addrlist[1].s_addr =
142                                         htonl ((uint32_t) ~0 << (32 - netmask));
143                         }
144                         else {
145                                 xlog(L_FATAL, "invalid netmask `%s' for %s",
146                                      cp + 1, clp->m_hostname);
147                         }
148                 }
149                 *cp = '/';
150                 clp->m_naddr = 0;
151         } else if (!hp) {
152                 clp->m_naddr = 0;
153         } else {
154                 char    **ap = hp->h_addr_list;
155                 int     i;
156
157                 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
158                         clp->m_addrlist[i] = *(struct in_addr *)*ap;
159                 }
160                 clp->m_naddr = i;
161         }
162 }
163
164 void
165 client_add(nfs_client *clp)
166 {
167         nfs_client      **cpp;
168
169         if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
170                 xlog(L_FATAL, "unknown client type in client_add");
171         cpp = clientlist + clp->m_type;
172         while (*cpp)
173                 cpp = &((*cpp)->m_next);
174         clp->m_next = NULL;
175         *cpp = clp;
176 }
177
178 void
179 client_release(nfs_client *clp)
180 {
181         if (clp->m_count <= 0)
182                 xlog(L_FATAL, "client_free: m_count <= 0!");
183         clp->m_count--;
184 }
185
186 void
187 client_freeall(void)
188 {
189         nfs_client      *clp, **head;
190         int             i;
191
192         for (i = 0; i < MCL_MAXTYPES; i++) {
193                 head = clientlist + i;
194                 while (*head) {
195                         *head = (clp = *head)->m_next;
196                         xfree(clp);
197                 }
198         }
199 }
200
201 nfs_client *
202 client_find(struct hostent *hp)
203 {
204         nfs_client      *clp;
205         int             i;
206
207         for (i = 0; i < MCL_MAXTYPES; i++) {
208                 for (clp = clientlist[i]; clp; clp = clp->m_next) {
209                         if (!client_check(clp, hp))
210                                 continue;
211 #ifdef notdef
212                         if (clp->m_type == MCL_FQDN)
213                                 return clp;
214                         return client_dup(clp, hp);
215 #else
216                         return clp;
217 #endif
218                 }
219         }
220         return NULL;
221 }
222
223 /*
224  * Match a host (given its hostent record) to a client record. This
225  * is usually called from mountd.
226  */
227 int
228 client_check(nfs_client *clp, struct hostent *hp)
229 {
230         char    *hname = (char *) hp->h_name;
231         char    *cname = clp->m_hostname;
232         char    **ap;
233
234         switch (clp->m_type) {
235         case MCL_FQDN:
236         case MCL_SUBNETWORK:
237                 for (ap = hp->h_addr_list; *ap; ap++) {
238                         if (client_checkaddr(clp, *(struct in_addr *) *ap))
239                                 return 1;
240                 }
241                 return 0;
242         case MCL_WILDCARD:
243                 if (wildmat(hname, cname))
244                         return 1;
245                 else {
246                         for (ap = hp->h_aliases; *ap; ap++)
247                                 if (wildmat(*ap, cname))
248                                         return 1;
249                 }
250                 return 0;
251         case MCL_NETGROUP:
252 #ifdef HAVE_INNETGR
253                 {
254                         char    *dot;
255                         int     match;
256                         struct hostent *nhp = NULL;
257                         struct sockaddr_in addr;
258
259                         /* First, try to match the hostname without
260                          * splitting off the domain */
261                         if (innetgr(cname+1, hname, NULL, NULL))
262                                 return 1;
263
264                         /* If hname is ip address convert to FQDN */
265                         if (inet_aton(hname, &addr.sin_addr) &&
266                            (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
267                             sizeof(addr.sin_addr), AF_INET))) {
268                                 hname = (char *)nhp->h_name;
269                                 if (innetgr(cname+1, hname, NULL, NULL))
270                                         return 1;
271                         }
272
273                         /* Okay, strip off the domain (if we have one) */
274                         if ((dot = strchr(hname, '.')) == NULL)
275                                 return 0;
276
277                         *dot = '\0';
278                         match = innetgr(cname+1, hname, NULL, NULL);
279                         *dot = '.';
280
281                         return match;
282                 }
283 #else
284                 return 0;
285 #endif
286         case MCL_ANONYMOUS:
287                 return 1;
288         default:
289                 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
290         }
291
292         return 0;
293 }
294
295 static int
296 client_checkaddr(nfs_client *clp, struct in_addr addr)
297 {
298         int     i;
299
300         switch (clp->m_type) {
301         case MCL_FQDN:
302                 for (i = 0; i < clp->m_naddr; i++) {
303                         if (clp->m_addrlist[i].s_addr == addr.s_addr)
304                                 return 1;
305                 }
306                 return 0;
307         case MCL_SUBNETWORK:
308                 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
309                         & clp->m_addrlist[1].s_addr);
310         }
311         return 0;
312 }
313
314 int
315 client_gettype(char *ident)
316 {
317         char    *sp;
318
319         if (ident[0] == '\0' || strcmp(ident, "*")==0)
320                 return MCL_ANONYMOUS;
321         if (ident[0] == '@') {
322 #ifndef HAVE_INNETGR
323                 xlog(L_WARNING, "netgroup support not compiled in");
324 #endif
325                 return MCL_NETGROUP;
326         }
327         for (sp = ident; *sp; sp++) {
328                 if (*sp == '*' || *sp == '?' || *sp == '[')
329                         return MCL_WILDCARD;
330                 if (*sp == '/')
331                         return MCL_SUBNETWORK;
332                 if (*sp == '\\' && sp[1])
333                         sp++;
334         }
335         return MCL_FQDN;
336 }