]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/cache.c
Stuff
[nfs-utils.git] / utils / mountd / cache.c
1
2 /*
3  * Handle communication with knfsd internal cache
4  *
5  * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel
6  * and listen for requests (using my_svc_run)
7  * 
8  */
9 #include "config.h"
10
11 #include <sys/types.h>
12 #include <sys/select.h>
13 #include <sys/stat.h>
14 #include <time.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include "misc.h"
22 #include "nfslib.h"
23 #include "exportfs.h"
24 #include "mountd.h"
25 #include "xmalloc.h"
26
27 /*
28  * Support routines for text-based upcalls.
29  * Fields are separated by spaces.
30  * Fields are either mangled to quote space tab newline slosh with slosh
31  * or a hexified with a leading \x
32  * Record is terminated with newline.
33  *
34  */
35 void cache_export_ent(char *domain, struct exportent *exp);
36
37
38 char *lbuf  = NULL;
39 int lbuflen = 0;
40
41 void auth_unix_ip(FILE *f)
42 {
43         /* requests are
44          *  class IP-ADDR
45          * Ignore if class != "nfsd"
46          * Otherwise find domainname and write back:
47          *
48          *  "nfsd" IP-ADDR expiry domainname
49          */
50         char *cp;
51         char class[20];
52         char ipaddr[20];
53         char *client;
54         struct in_addr addr;
55         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
56                 return;
57
58         cp = lbuf;
59
60         if (qword_get(&cp, class, 20) <= 0 ||
61             strcmp(class, "nfsd") != 0)
62                 return;
63
64         if (qword_get(&cp, ipaddr, 20) <= 0)
65                 return;
66
67         if (inet_aton(ipaddr, &addr)==0)
68                 return;
69
70         /* addr is a valid, interesting address, find the domain name... */
71         client = client_compose(addr);
72
73         
74         qword_print(f, "nfsd");
75         qword_print(f, ipaddr);
76         qword_printint(f, time(0)+30*60);
77         if (client)
78                 qword_print(f, *client?client:"DEFAULT");
79         qword_eol(f);
80
81         if (client) free(client);
82         
83 }
84
85 void nfsd_fh(FILE *f)
86 {
87         /* request are:
88          *  domain fsidtype fsid
89          * interpret fsid, find export point and options, and write:
90          *  domain fsidtype fsid expiry path
91          */
92         char *cp;
93         char *dom;
94         int fsidtype;
95         int fsidlen;
96         unsigned int dev, major=0, minor=0;
97         unsigned int inode=0;
98         unsigned int fsidnum=0;
99         char fsid[32];
100         struct exportent *found = NULL;
101         nfs_export *exp;
102         int i;
103
104         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
105                 return;
106
107         cp = lbuf;
108
109         dom = malloc(strlen(cp));
110         if (dom == NULL)
111                 return;
112         if (qword_get(&cp, dom, strlen(cp)) <= 0)
113                 goto out;
114         if (qword_get_int(&cp, &fsidtype) != 0)
115                 goto out;
116         if (fsidtype < 0 || fsidtype > 1)
117                 goto out; /* unknown type */
118         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
119                 goto out;
120         switch(fsidtype) {
121         case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
122                 if (fsidlen != 8)
123                         goto out;
124                 memcpy(&dev, fsid, 4);
125                 memcpy(&inode, fsid+4, 4);
126                 major = ntohl(dev)>>16;
127                 minor = ntohl(dev) & 0xFFFF;
128                 break;
129
130         case 1: /* 4 bytes - fsid */
131                 if (fsidlen != 4)
132                         goto out;
133                 memcpy(&fsidnum, fsid, 4);
134                 break;
135         }
136
137         /* Now determine export point for this fsid/domain */
138         for (i=0 ; i < MCL_MAXTYPES; i++) {
139                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
140                         if (!client_member(dom, exp->m_client->m_hostname))
141                                 continue;
142                         if (fsidtype == 1 &&
143                             ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
144                              exp->m_export.e_fsid != fsidnum))
145                                 continue;
146                         if (fsidtype == 0) {
147                                 struct stat stb;
148                                 if (stat(exp->m_export.e_path, &stb) != 0)
149                                         continue;
150                                 if (stb.st_ino != inode)
151                                         continue;
152                                 if (major != major(stb.st_dev) ||
153                                     minor != minor(stb.st_dev))
154                                         continue;
155                         }
156                         /* It's a match !! */
157                         if (!found)
158                                 found = &exp->m_export;
159                         else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
160                         {
161                                 xlog(L_WARNING, "%s and %s have name filehandle for %s, using first",
162                                      found->e_path, exp->m_export.e_path, dom);
163                         }
164                 }
165         }
166         cache_export_ent(dom, found);
167
168         qword_print(f, dom);
169         qword_printint(f, fsidtype);
170         qword_printhex(f, fsid, fsidlen);
171         qword_printint(f, time(0)+30*60);
172         if (found)
173                 qword_print(f, found->e_path);
174         qword_eol(f);
175  out:
176         free(dom);
177         return;         
178 }
179
180 void nfsd_export(FILE *f)
181 {
182         /* requests are:
183          *  domain path
184          * determine export options and return:
185          *  domain path expiry flags anonuid anongid fsid
186          */
187
188         char *cp;
189         int i;
190         char *dom, *path;
191         nfs_export *exp, *found = NULL;
192
193
194         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
195                 return;
196
197         cp = lbuf;
198         dom = malloc(strlen(cp));
199         path = malloc(strlen(cp));
200
201         if (!dom || !path)
202                 goto out;
203
204         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
205                 goto out;
206         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
207                 goto out;
208
209         /* now find flags for this export point in this domain */
210         for (i=0 ; i < MCL_MAXTYPES; i++) {
211                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
212                         if (!client_member(dom, exp->m_client->m_hostname))
213                                 continue;
214                         if (strcmp(path, exp->m_export.e_path))
215                                 continue;
216                         if (!found)
217                                 found = exp;
218                         else {
219                                 xlog(L_WARNING, "%s exported to both %s and %s in %s",
220                                      path, exp->m_client->m_hostname, found->m_client->m_hostname,
221                                      dom);
222                         }
223                 }
224         }
225
226         qword_print(f, dom);
227         qword_print(f, path);
228         qword_printint(f, time(0)+30*60);
229         if (found) {
230                 qword_printint(f, found->m_export.e_flags);
231                 qword_printint(f, found->m_export.e_anonuid);
232                 qword_printint(f, found->m_export.e_anongid);
233                 qword_printint(f, found->m_export.e_fsid);
234         }
235         qword_eol(f);
236  out:
237         if (dom) free(dom);
238         if (path) free(path);
239 }
240
241
242 struct {
243         char *cache_name;
244         void (*cache_handle)(FILE *f);
245         FILE *f;
246 } cachelist[] = {
247         { "auth.unix.ip", auth_unix_ip},
248         { "nfsd.export", nfsd_export},
249         { "nfsd.fh", nfsd_fh},
250         { NULL, NULL }
251 };
252
253 void cache_open(void) 
254 {
255         int i;
256         for (i=0; cachelist[i].cache_name; i++ ){
257                 char path[100];
258                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
259                 cachelist[i].f = fopen(path, "r+");
260         }
261 }
262
263 void cache_set_fds(fd_set *fdset)
264 {
265         int i;
266         for (i=0; cachelist[i].cache_name; i++) {
267                 if (cachelist[i].f)
268                         FD_SET(fileno(cachelist[i].f), fdset);
269         }
270 }
271
272 int cache_process_req(fd_set *readfds) 
273 {
274         int i;
275         int cnt = 0;
276         for (i=0; cachelist[i].cache_name; i++) {
277                 if (cachelist[i].f != NULL &&
278                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
279                         cnt++;
280                         cachelist[i].cache_handle(cachelist[i].f);
281                 }
282         }
283         return cnt;
284 }
285
286
287 /*
288  * Give IP->domain and domain+path->options to kernel
289  * % echo nfsd $IP  $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
290  * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
291  */
292
293 void cache_export_ent(char *domain, struct exportent *exp)
294 {
295
296         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "r+");
297         if (!f)
298                 return;
299
300         qword_print(f, domain);
301         qword_print(f, exp->e_path);
302         qword_printint(f, time(0)+30*60);
303         qword_printint(f, exp->e_flags);
304         qword_printint(f, exp->e_anonuid);
305         qword_printint(f, exp->e_anongid);
306         qword_printint(f, exp->e_fsid);
307         qword_eol(f);
308
309         fclose(f);
310 }
311
312 void cache_export(nfs_export *exp)
313 {
314         FILE *f;
315
316         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "r+");
317         if (!f)
318                 return;
319
320         qword_print(f, "nfsd");
321         qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
322         qword_printint(f, time(0)+30*60);
323         qword_print(f, exp->m_client->m_hostname);
324         qword_eol(f);
325         
326         fclose(f);
327
328         cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
329 }
330
331 /* Get a filehandle.
332  * { 
333  *   echo $domain $path $length 
334  *   read filehandle <&0
335  * } <> /proc/fs/nfs/filehandle
336  */
337 struct nfs_fh_len *
338 cache_get_filehandle(nfs_export *exp, int len, char *p)
339 {
340         FILE *f = fopen("/proc/fs/nfs/filehandle", "r+");
341         char buf[200];
342         char *bp = buf;
343         static struct nfs_fh_len fh;
344         if (!f)
345                 return NULL;
346
347         qword_print(f, exp->m_client->m_hostname);
348         qword_print(f, p);
349         qword_printint(f, len); 
350         qword_eol(f);
351         
352         if (fgets(buf, sizeof(buf), f) == NULL)
353                 return NULL;
354         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
355         fh.fh_size = qword_get(&bp, fh.fh_handle, NFS3_FHSIZE);
356         return &fh;
357 }
358