]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/9nfs/nfsmount.c
fix filetype detecton by suffix so that multiple dots dont confuse it. (thanks kvik)
[plan9front.git] / sys / src / cmd / 9nfs / nfsmount.c
1 #include "all.h"
2
3 /*
4  *      Cf. /lib/rfc/rfc1094
5  */
6
7 static int      mntnull(int, Rpccall*, Rpccall*);
8 static int      mntmnt(int, Rpccall*, Rpccall*);
9 static int      mntdump(int, Rpccall*, Rpccall*);
10 static int      mntumnt(int, Rpccall*, Rpccall*);
11 static int      mntumntall(int, Rpccall*, Rpccall*);
12 static int      mntexport(int, Rpccall*, Rpccall*);
13
14 Procmap mntproc[] = {
15         0, mntnull,
16         1, mntmnt,
17         2, mntdump,
18         3, mntumnt,
19         4, mntumntall,
20         5, mntexport,
21         0, 0
22 };
23
24 long            starttime;
25 static int      noauth;
26 char *          config;
27 Session *       head;
28 Session *       tail;
29 int staletime = 10*60;
30
31 void
32 mnttimer(long now)
33 {
34         Session *s;
35
36         for(s=head; s; s=s->next)
37                 fidtimer(s, now);
38 }
39
40 static void
41 usage(void)
42 {
43         sysfatal("usage: %s %s [-ns] [-a dialstring] [-c uidmap] [-f srvfile] "
44                 "[-T staletime]", argv0, commonopts);
45 }
46
47 void
48 mntinit(int argc, char **argv)
49 {
50         int tries;
51
52         config = "config";
53         starttime = time(0);
54         clog("nfs mount server init, starttime = %lud\n", starttime);
55         tries = 0;
56         ARGBEGIN{
57         case 'a':
58                 ++tries;
59                 srvinit(-1, 0, EARGF(usage()));
60                 break;
61         case 'c':
62                 config = EARGF(usage());
63                 break;
64         case 'f':
65                 ++tries;
66                 srvinit(-1, EARGF(usage()), 0);
67                 break;
68         case 'n':
69                 ++noauth;
70                 break;
71         case 's':
72                 ++tries;
73                 srvinit(1, 0, 0);
74                 break;
75         case 'T':
76                 staletime = atoi(EARGF(usage()));
77                 break;
78         default:
79                 if(argopt(ARGC()) < 0)
80                         sysfatal("usage: %s %s [-ns] [-a dialstring] "
81                                 "[-c uidmap] [-f srvfile] [-T staletime]",
82                                 argv0, commonopts);
83                 break;
84         }ARGEND
85 noauth=1;       /* ZZZ */
86         if(tries == 0 && head == 0)
87                 srvinit(-1, 0, "tcp!fs");
88         if(head == 0)
89                 panic("can't initialize services");
90         readunixidmaps(config);
91 }
92
93 void
94 srvinit(int fd, char *file, char *addr)
95 {
96         char fdservice[16], *naddr;
97         Session *s;
98         Xfile *xp;
99         Xfid *xf;
100         Fid *f;
101
102         s = calloc(1, sizeof(Session));
103         s->spec = "";
104         s->fd = -1;
105         if(fd >= 0){
106                 s->fd = fd;
107                 sprint(fdservice, "/fd/%d", s->fd);
108                 s->service = strstore(fdservice);
109                 chat("fd = %d\n", s->fd);
110         }else if(file){
111                 chat("file = \"%s\"\n", file);
112                 s->service = file;
113                 s->fd = open(file, ORDWR);
114                 if(s->fd < 0){
115                         clog("can't open %s: %r\n", file);
116                         goto error;
117                 }
118         }else if(addr){
119                 chat("addr = \"%s\"\n", addr);
120                 naddr = netmkaddr(addr, 0, "9fs");
121                 s->service = addr;
122                 s->fd = dial(naddr, 0, 0, 0);
123                 if(s->fd < 0){
124                         clog("can't dial %s: %r\n", naddr);
125                         goto error;
126                 }
127         }
128
129         chat("version...");
130         s->tag = NOTAG-1;
131         s->f.msize = Maxfdata+IOHDRSZ;
132         s->f.version = "9P2000";
133         xmesg(s, Tversion);
134         messagesize = IOHDRSZ+s->f.msize;
135         chat("version spec %s size %d\n", s->f.version, s->f.msize);
136
137         s->tag = 0;
138
139         chat("authenticate...");
140         if(authhostowner(s) < 0){
141                 clog("auth failed %r\n");
142                 goto error;
143         }
144
145         chat("attach as none...");
146         f = newfid(s);
147         s->f.fid = f - s->fids;
148         s->f.afid = ~0x0UL;
149         s->f.uname = "none";
150         s->f.aname = s->spec;
151         if(xmesg(s, Tattach)){
152                 clog("attach failed\n");
153                 goto error;
154         }
155
156         xp = xfile(&s->f.qid, s, 1);
157         s->root = xp;
158         xp->parent = xp;
159         xp->name = "/";
160         xf = xfid("none", xp, 1);
161         xf->urfid = f;
162         clog("service=%s uid=%s fid=%zd\n",
163                 s->service, xf->uid, xf->urfid - s->fids);
164         if(tail)
165                 tail->next = s;
166         else
167                 head = s;
168         tail = s;
169         return;
170
171 error:
172         if(s->fd >= 0)
173                 close(s->fd);
174         free(s);
175 }
176
177 static int
178 mntnull(int n, Rpccall *cmd, Rpccall *reply)
179 {
180         USED(n, cmd, reply);
181         chat("mntnull\n");
182         return 0;
183 }
184
185 static int
186 mntmnt(int n, Rpccall *cmd, Rpccall *reply)
187 {
188         int i;
189         char dom[64];
190         uchar *argptr = cmd->args;
191         uchar *dataptr = reply->results;
192         Authunix au;
193         Xfile *xp;
194         String root;
195
196         chat("mntmnt...\n");
197         if(n < 8)
198                 return garbage(reply, "n too small");
199         argptr += string2S(argptr, &root);
200         if(argptr != &((uchar *)cmd->args)[n])
201                 return garbage(reply, "bad count");
202         clog("host=%I, port=%ld, root=\"%.*s\"...",
203                 cmd->host, cmd->port, utfnlen(root.s, root.n), root.s);
204         if(auth2unix(&cmd->cred, &au) != 0){
205                 chat("auth flavor=%ld, count=%ld\n",
206                         cmd->cred.flavor, cmd->cred.count);
207                 for(i=0; i<cmd->cred.count; i++)
208                         chat(" %.2ux", ((uchar *)cmd->cred.data)[i]);
209                 chat("\n");
210                 clog("auth: bad credentials");
211                 return error(reply, 1);
212         }
213         clog("auth: %ld %.*s u=%ld g=%ld",
214                 au.stamp, utfnlen(au.mach.s, au.mach.n), au.mach.s, au.uid, au.gid);
215         for(i=0; i<au.gidlen; i++)
216                 chat(", %ld", au.gids[i]);
217         chat("...");
218         if(getdom(cmd->host, dom, sizeof(dom))<0){
219                 clog("auth: unknown ip address");
220                 return error(reply, 1);
221         }
222         chat("dom=%s...", dom);
223         xp = xfroot(root.s, root.n);
224         if(xp == 0){
225                 chat("xp=0...");
226                 clog("mntmnt: no fs");
227                 return error(reply, 3);
228         }
229
230         PLONG(0);
231         dataptr += xp2fhandle(xp, dataptr);
232         chat("OK\n");
233         return dataptr - (uchar *)reply->results;
234 }
235
236 static int
237 mntdump(int n, Rpccall *cmd, Rpccall *reply)
238 {
239         if(n != 0)
240                 return garbage(reply, "mntdump");
241         USED(cmd);
242         chat("mntdump...");
243         return error(reply, FALSE);
244 }
245
246 static int
247 mntumnt(int n, Rpccall *cmd, Rpccall *reply)
248 {
249         if(n <= 0)
250                 return garbage(reply, "mntumnt");
251         USED(cmd);
252         chat("mntumnt\n");
253         return 0;
254 }
255
256 static int
257 mntumntall(int n, Rpccall *cmd, Rpccall *reply)
258 {
259         if(n != 0)
260                 return garbage(reply, "mntumntall");
261         USED(cmd);
262         chat("mntumntall\n");
263         return 0;
264 }
265
266 static int
267 mntexport(int n, Rpccall *cmd, Rpccall *reply)
268 {
269         uchar *dataptr = reply->results;
270         Authunix au;
271         int i;
272
273         chat("mntexport...");
274         if(n != 0)
275                 return garbage(reply, "mntexport");
276         if(auth2unix(&cmd->cred, &au) != 0){
277                 chat("auth flavor=%ld, count=%ld\n",
278                         cmd->cred.flavor, cmd->cred.count);
279                 for(i=0; i<cmd->cred.count; i++)
280                         chat(" %.2ux", ((uchar *)cmd->cred.data)[i]);
281                 chat("...");
282                 au.mach.n = 0;
283         }else
284                 chat("%ld@%.*s...", au.uid, utfnlen(au.mach.s, au.mach.n), au.mach.s);
285         PLONG(TRUE);
286         PLONG(1);
287         PPTR("/", 1);
288         if(au.mach.n > 0){
289                 PLONG(TRUE);
290                 PLONG(au.mach.n);
291                 PPTR(au.mach.s, au.mach.n);
292         }
293         PLONG(FALSE);
294         PLONG(FALSE);
295         chat("OK\n");
296         return dataptr - (uchar *)reply->results;
297 }
298
299 Xfile *
300 xfroot(char *name, int n)
301 {
302         Session *s;
303         char *p;
304
305         if(n <= 0)
306                 n = strlen(name);
307         chat("xfroot: %.*s...", utfnlen(name, n), name);
308         if(n == 1 && name[0] == '/')
309                 return head->root;
310         for(s=head; s; s=s->next){
311                 if(strncmp(name, s->service, n) == 0)
312                         return s->root;
313                 p = strrchr(s->service, '!');   /* for -a tcp!foo */
314                 if(p && strncmp(name, p+1, n) == 0)
315                         return s->root;
316                 p = strrchr(s->service, '/');   /* for -f /srv/foo */
317                 if(p && strncmp(name, p+1, n) == 0)
318                         return s->root;
319         }
320         return 0;
321 }