psshfs.c revision 1.11 1 /* $NetBSD: psshfs.c,v 1.11 2007/04/12 20:42:46 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * psshfs: puffs sshfs
33 *
34 * psshfs implements sshfs functionality on top of puffs making it
35 * possible to mount a filesystme through the sftp service.
36 *
37 * psshfs can execute multiple operations in "parallel" by using the
38 * puffs_cc framework for continuations.
39 *
40 * Concurrency control is handled currently by vnode locking (this
41 * will change in the future). Context switch locations are easy to
42 * find by grepping for puffs_cc_yield().
43 *
44 * The operation revolves around an event loop. Incoming requests from
45 * the kernel are dispatched off to the libpuffs event handler, which
46 * eventually calls the callbacks. The callbacks do whatever they need
47 * to do, queue output and call puffs_cc_yield() to put them to sleep.
48 * Meanwhile execution continues. When an answer to the callback's
49 * query arrives, the blocker is located by the protocol's request
50 * id and awakened by calling puffs_docc(). All changes made to the
51 * buffer (psbuf->buf) will be seen by the callback. Here the buffer
52 * contains the response of the sftp server. The callback will then
53 * proceed to parse the buffer.
54 *
55 * XXX: struct psbuf is a mess. it will be fixed sooner or later
56 */
57
58 #include <sys/cdefs.h>
59 #ifndef lint
60 __RCSID("$NetBSD: psshfs.c,v 1.11 2007/04/12 20:42:46 pooka Exp $");
61 #endif /* !lint */
62
63 #include <sys/types.h>
64
65 #include <assert.h>
66 #include <err.h>
67 #include <errno.h>
68 #include <mntopts.h>
69 #include <poll.h>
70 #include <puffs.h>
71 #include <signal.h>
72 #include <stdlib.h>
73 #include <util.h>
74 #include <unistd.h>
75
76 #include "psshfs.h"
77
78 static void psshfs_eventloop(struct puffs_usermount *, struct psshfs_ctx *);
79 static void pssh_connect(struct psshfs_ctx *, char **);
80 static void usage(void);
81
82 #define SSH_PATH "/usr/bin/ssh"
83
84 static void
85 usage()
86 {
87
88 errx(1, "usage: %s [-s][-o opts]user@host:path mountpath",
89 getprogname());
90 }
91
92 int
93 main(int argc, char *argv[])
94 {
95 struct psshfs_ctx pctx;
96 struct puffs_usermount *pu;
97 struct puffs_ops *pops;
98 mntoptparse_t mp;
99 char *sshargs[16];
100 char *userhost;
101 char *hostpath;
102 int mntflags, pflags, ch;
103 int detach;
104
105 setprogname(argv[0]);
106
107 if (argc < 3)
108 usage();
109
110 mntflags = pflags = 0;
111 detach = 1;
112 while ((ch = getopt(argc, argv, "eo:s")) != -1) {
113 switch (ch) {
114 case 'e':
115 pflags |= PUFFS_KFLAG_CANEXPORT;
116 break;
117 case 'o':
118 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
119 if (mp == NULL)
120 err(1, "getmntopts");
121 freemntopts(mp);
122 break;
123 case 's':
124 detach = 0;
125 break;
126 default:
127 usage();
128 /*NOTREACHED*/
129 }
130 }
131 argc -= optind;
132 argv += optind;
133
134 #if 0
135 /* XXX: noatime is mandatory for now */
136 mntflags |= MNT_NOATIME;
137 #endif
138
139 if (pflags & PUFFS_FLAG_OPDUMP)
140 detach = 0;
141
142 if (argc != 2)
143 usage();
144
145 PUFFSOP_INIT(pops);
146
147 PUFFSOP_SET(pops, psshfs, fs, unmount);
148 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
149 PUFFSOP_SETFSNOP(pops, statvfs);
150 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
151 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
152
153 PUFFSOP_SET(pops, psshfs, node, lookup);
154 PUFFSOP_SET(pops, psshfs, node, create);
155 PUFFSOP_SET(pops, psshfs, node, readdir);
156 PUFFSOP_SET(pops, psshfs, node, getattr);
157 PUFFSOP_SET(pops, psshfs, node, setattr);
158 PUFFSOP_SET(pops, psshfs, node, mkdir);
159 PUFFSOP_SET(pops, psshfs, node, remove);
160 PUFFSOP_SET(pops, psshfs, node, readlink);
161 PUFFSOP_SET(pops, psshfs, node, rmdir);
162 PUFFSOP_SET(pops, psshfs, node, symlink);
163 PUFFSOP_SET(pops, psshfs, node, rename);
164 PUFFSOP_SET(pops, psshfs, node, read);
165 PUFFSOP_SET(pops, psshfs, node, write);
166 PUFFSOP_SET(pops, psshfs, node, reclaim);
167
168 memset(&pctx, 0, sizeof(pctx));
169 TAILQ_INIT(&pctx.outbufq);
170 TAILQ_INIT(&pctx.req_queue);
171 pctx.mounttime = time(NULL);
172
173 userhost = argv[0];
174 hostpath = strchr(userhost, ':');
175 if (hostpath) {
176 *hostpath++ = '\0';
177 pctx.mountpath = hostpath;
178 } else
179 pctx.mountpath = ".";
180
181 /* xblah */
182 sshargs[0] = SSH_PATH;
183 sshargs[1] = argv[0];
184 sshargs[2] = "-oClearAllForwardings=yes";
185 sshargs[3] = "-a";
186 sshargs[4] = "-x";
187 sshargs[5] = "-s";
188 sshargs[6] = "sftp";
189 sshargs[7] = 0;
190
191 if ((pu = puffs_mount(pops, argv[1], mntflags, "psshfs", &pctx,
192 PUFFS_FLAG_BUILDPATH | pflags, 0))==NULL)
193 err(1, "puffs_mount");
194
195 pssh_connect(&pctx, sshargs);
196
197 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
198 err(1, "setblockingmode");
199 if (psshfs_domount(pu) != 0)
200 errx(1, "domount");
201
202 if (detach)
203 daemon(1, 0);
204
205 psshfs_eventloop(pu, &pctx);
206 return 0;
207 }
208
209 /*
210 * enqueue buffer to be handled with cc
211 */
212 void
213 pssh_outbuf_enqueue(struct psshfs_ctx *pctx, struct psbuf *pb,
214 struct puffs_cc *pcc, uint32_t reqid)
215 {
216
217 pb->psr.reqid = reqid;
218 pb->psr.pcc = pcc;
219 pb->psr.func = NULL;
220 pb->psr.arg = NULL;
221 TAILQ_INSERT_TAIL(&pctx->outbufq, pb, psr.entries);
222 }
223
224 /*
225 * enqueue buffer to be handled with "f". "f" must not block.
226 * gives up struct psbuf ownership.
227 */
228 void
229 pssh_outbuf_enqueue_nocc(struct psshfs_ctx *pctx, struct psbuf *pb,
230 void (*f)(struct psshfs_ctx *, struct psbuf *, void *), void *arg,
231 uint32_t reqid)
232 {
233
234 pb->psr.reqid = reqid;
235 pb->psr.pcc = NULL;
236 pb->psr.func = f;
237 pb->psr.arg = arg;
238 TAILQ_INSERT_TAIL(&pctx->outbufq, pb, psr.entries);
239 }
240
241 struct psbuf *
242 psshreq_get(struct psshfs_ctx *pctx, uint32_t reqid)
243 {
244 struct psbuf *pb;
245
246 TAILQ_FOREACH(pb, &pctx->req_queue, psr.entries)
247 if (pb->psr.reqid == reqid)
248 break;
249
250 if (!pb)
251 return NULL;
252
253 TAILQ_REMOVE(&pctx->req_queue, pb, psr.entries);
254
255 return pb;
256 }
257
258 static void
259 handlebuf(struct psshfs_ctx *pctx, struct psbuf *datapb,
260 struct puffs_putreq *ppr)
261 {
262 struct psreq psrtmp;
263 struct psbuf *pb;
264
265 /* is this something we are expecting? */
266 pb = psshreq_get(pctx, datapb->reqid);
267
268 if (pb == NULL) {
269 printf("invalid server request response %d\n", datapb->reqid);
270 psbuf_destroy(datapb);
271 return;
272 }
273
274 /* keep psreq clean, xxx uknow */
275 psrtmp = pb->psr;
276 *pb = *datapb;
277 pb->psr = psrtmp;
278 free(datapb);
279
280 /* don't allow both cc and handler func, but allow neither */
281 assert((pb->psr.pcc && pb->psr.func) == 0);
282 if (pb->psr.pcc) {
283 puffs_docc(pb->psr.pcc, ppr);
284 } else if (pb->psr.func) {
285 pb->psr.func(pctx, pb, pb->psr.arg);
286 } else {
287 assert(pb->psr.arg == NULL);
288 psbuf_destroy(pb);
289 }
290 }
291
292 static int
293 psshinput(struct psshfs_ctx *pctx, struct puffs_putreq *ppr)
294 {
295 struct psbuf *cb;
296 int rv;
297
298 for (;;) {
299 if ((cb = pctx->curpb) == NULL) {
300 cb = psbuf_make(PSB_IN);
301 if (cb == NULL)
302 return -1;
303 pctx->curpb = cb;
304 }
305
306 rv = psbuf_read(pctx, cb);
307 if (rv == -1)
308 err(1, "psbuf read");
309 if (rv == 0)
310 break;
311
312 handlebuf(pctx, cb, ppr);
313 pctx->curpb = NULL;
314 }
315
316 return rv;
317 }
318
319 static int
320 psshoutput(struct psshfs_ctx *pctx)
321 {
322 struct psbuf *pb;
323 int rv;
324
325 TAILQ_FOREACH(pb, &pctx->outbufq, psr.entries) {
326 rv = psbuf_write(pctx, pb);
327 if (rv == -1)
328 return -1;
329 if (rv == 0)
330 return 0;
331
332 /* sent everything, move to cookiequeue */
333 TAILQ_REMOVE(&pctx->outbufq, pb, psr.entries);
334 free(pb->buf);
335 TAILQ_INSERT_TAIL(&pctx->req_queue, pb, psr.entries);
336 }
337
338 return 1;
339 }
340
341 volatile int timetodie;
342
343 #define PFD_SSH 0
344 #define PFD_PUFFS 1
345 static void
346 psshfs_eventloop(struct puffs_usermount *pu, struct psshfs_ctx *pctx)
347 {
348 struct puffs_getreq *pgr;
349 struct puffs_putreq *ppr;
350 struct pollfd pfds[2];
351 int x;
352
353 pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
354 if (!pgr)
355 err(1, "makegetreq");
356 ppr = puffs_req_makeput(pu);
357 if (!ppr)
358 err(1, "makeputreq");
359
360 x = 1;
361 if (ioctl(pctx->sshfd, FIONBIO, &x) == -1)
362 err(1, "nonblocking descriptor");
363
364 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
365 if (timetodie) {
366 kill(pctx->sshpid, SIGTERM);
367 break;
368 }
369
370 memset(pfds, 0, sizeof(pfds));
371 pfds[PFD_SSH].events = POLLIN;
372 if (!TAILQ_EMPTY(&pctx->outbufq))
373 pfds[PFD_SSH].events |= POLLOUT;
374 pfds[PFD_SSH].fd = pctx->sshfd;
375 pfds[PFD_PUFFS].fd = puffs_getselectable(pu);
376 pfds[PFD_PUFFS].events = POLLIN;
377
378 if (poll(pfds, 2, INFTIM) == -1)
379 err(1, "poll");
380
381 if (pfds[PFD_SSH].revents & POLLOUT)
382 if (psshoutput(pctx) == -1)
383 err(1, "psshoutput");
384
385 /* get & possibly dispatch events from kernel */
386 if (pfds[PFD_PUFFS].revents & POLLIN)
387 if (puffs_req_handle(pu, pgr, ppr, 0) == -1)
388 err(1, "puffs_handlereqs");
389
390 /* get input from sftpd, possibly build more responses */
391 if (pfds[PFD_SSH].revents & POLLIN)
392 if (psshinput(pctx, ppr) == -1)
393 errx(1, "psshinput");
394
395 /* it's likely we got outputtables, poke the ice with a stick */
396 if (psshoutput(pctx) == -1)
397 err(1, "psshoutput");
398
399 /* stuff all replies from both of the above into kernel */
400 if (puffs_req_putput(ppr) == -1)
401 err(1, "putputreq");
402 puffs_req_resetput(ppr);
403 }
404
405 puffs_req_destroyget(pgr);
406 }
407
408 static void
409 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
410 {
411 int fds[2];
412 pid_t pid;
413
414 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
415 err(1, "socketpair");
416
417 pid = fork();
418 switch (pid) {
419 case -1:
420 err(1, "fork");
421 /*NOTREACHED*/
422 case 0: /* child */
423 if (dup2(fds[0], STDIN_FILENO) == -1)
424 err(1, "child dup2");
425 if (dup2(fds[0], STDOUT_FILENO) == -1)
426 err(1, "child dup2");
427 close(fds[0]);
428 close(fds[1]);
429 execvp(sshargs[0], sshargs);
430 break;
431 default:
432 pctx->sshpid = pid;
433 pctx->sshfd = fds[1];
434 close(fds[0]);
435 break;
436 }
437 }
438