psshfs.c revision 1.5 1 /* $NetBSD: psshfs.c,v 1.5 2007/01/20 13:52:35 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.5 2007/01/20 13:52:35 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, "o:s")) != -1) {
113 switch (ch) {
114 case 'o':
115 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
116 if (mp == NULL)
117 err(1, "getmntopts");
118 freemntopts(mp);
119 break;
120 case 's':
121 detach = 0;
122 break;
123 default:
124 usage();
125 /*NOTREACHED*/
126 }
127 }
128 argc -= optind;
129 argv += optind;
130
131 if (pflags & PUFFS_FLAG_OPDUMP)
132 detach = 0;
133
134 if (argc != 2)
135 usage();
136
137 PUFFSOP_INIT(pops);
138
139 PUFFSOP_SET(pops, psshfs, fs, unmount);
140 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
141 PUFFSOP_SETFSNOP(pops, statvfs);
142
143 PUFFSOP_SET(pops, psshfs, node, lookup);
144 PUFFSOP_SET(pops, psshfs, node, create);
145 PUFFSOP_SET(pops, psshfs, node, readdir);
146 PUFFSOP_SET(pops, psshfs, node, getattr);
147 PUFFSOP_SET(pops, psshfs, node, setattr);
148 PUFFSOP_SET(pops, psshfs, node, mkdir);
149 PUFFSOP_SET(pops, psshfs, node, remove);
150 PUFFSOP_SET(pops, psshfs, node, readlink);
151 PUFFSOP_SET(pops, psshfs, node, rmdir);
152 PUFFSOP_SET(pops, psshfs, node, symlink);
153 PUFFSOP_SET(pops, psshfs, node, rename);
154 PUFFSOP_SET(pops, psshfs, node, read);
155 PUFFSOP_SET(pops, psshfs, node, write);
156 #if 0
157 /* must support this some day */
158 PUFFSOP_SET(pops, psshfs, node, reclaim);
159 #endif
160
161 memset(&pctx, 0, sizeof(pctx));
162 TAILQ_INIT(&pctx.outbufq);
163 TAILQ_INIT(&pctx.req_queue);
164
165 userhost = argv[0];
166 hostpath = strchr(userhost, ':');
167 if (hostpath) {
168 *hostpath++ = '\0';
169 pctx.mountpath = hostpath;
170 } else
171 pctx.mountpath = ".";
172
173 /* xblah */
174 sshargs[0] = SSH_PATH;
175 sshargs[1] = argv[0];
176 sshargs[2] = "-oClearAllForwardings=yes";
177 sshargs[3] = "-a";
178 sshargs[4] = "-x";
179 sshargs[5] = "-s";
180 sshargs[6] = "sftp";
181 sshargs[7] = 0;
182
183 if ((pu = puffs_mount(pops, argv[1], mntflags, "psshfs", &pctx,
184 PUFFS_FLAG_BUILDPATH | pflags, 0))==NULL)
185 err(1, "puffs_mount");
186
187 pssh_connect(&pctx, sshargs);
188
189 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
190 err(1, "setblockingmode");
191 if (psshfs_domount(pu) != 0)
192 errx(1, "domount");
193
194 if (detach)
195 daemon(1, 0);
196
197 psshfs_eventloop(pu, &pctx);
198 return 0;
199 }
200
201 void
202 pssh_outbuf_enqueue(struct psshfs_ctx *pctx, struct psbuf *pb,
203 struct puffs_cc *pcc, uint32_t reqid)
204 {
205
206 pb->psr.reqid = reqid;
207 pb->psr.pcc = pcc;
208 TAILQ_INSERT_TAIL(&pctx->outbufq, pb, psr.entries);
209 }
210
211 void
212 psshreq_put(struct psshfs_ctx *pctx, struct psbuf *pb)
213 {
214
215 TAILQ_INSERT_TAIL(&pctx->req_queue, pb, psr.entries);
216 }
217
218 struct psbuf *
219 psshreq_get(struct psshfs_ctx *pctx, uint32_t reqid)
220 {
221 struct psbuf *pb;
222
223 TAILQ_FOREACH(pb, &pctx->req_queue, psr.entries)
224 if (pb->psr.reqid == reqid)
225 break;
226
227 if (!pb)
228 return NULL;
229
230 TAILQ_REMOVE(&pctx->req_queue, pb, psr.entries);
231
232 return pb;
233 }
234
235 static void
236 handlebuf(struct psshfs_ctx *pctx, struct psbuf *datapb,
237 struct puffs_putreq *ppr)
238 {
239 struct psreq psrtmp;
240 struct psbuf *pb;
241
242 /* is this something we are expecting? */
243 pb = psshreq_get(pctx, datapb->reqid);
244
245 if (pb) {
246 /* keep psreq clean, xxx uknow */
247 psrtmp = pb->psr;
248 *pb = *datapb;
249 pb->psr = psrtmp;
250 free(datapb);
251
252 puffs_docc(ppr, pb->psr.pcc);
253 return;
254 }
255
256 printf("invalid server request response %d\n", datapb->reqid);
257 psbuf_destroy(datapb);
258 }
259
260 static int
261 psshinput(struct psshfs_ctx *pctx, struct puffs_putreq *ppr)
262 {
263 struct psbuf *cb;
264 int rv;
265
266 for (;;) {
267 if ((cb = pctx->curpb) == NULL) {
268 cb = psbuf_make(PSB_IN);
269 if (cb == NULL)
270 return -1;
271 pctx->curpb = cb;
272 }
273
274 rv = psbuf_read(pctx, cb);
275 if (rv == -1)
276 err(1, "psbuf read");
277 if (rv == 0)
278 break;
279
280 handlebuf(pctx, cb, ppr);
281 pctx->curpb = NULL;
282 }
283
284 return rv;
285 }
286
287 static int
288 psshoutput(struct psshfs_ctx *pctx)
289 {
290 struct psbuf *pb;
291 int rv;
292
293 TAILQ_FOREACH(pb, &pctx->outbufq, psr.entries) {
294 rv = psbuf_write(pctx, pb);
295 if (rv == -1)
296 return -1;
297 if (rv == 0)
298 return 0;
299
300 /* sent everything, move to cookiequeue */
301 TAILQ_REMOVE(&pctx->outbufq, pb, psr.entries);
302 free(pb->buf);
303 TAILQ_INSERT_TAIL(&pctx->req_queue, pb, psr.entries);
304 }
305
306 return 1;
307 }
308
309 volatile int timetodie;
310
311 #define PFD_SSH 0
312 #define PFD_PUFFS 1
313 static void
314 psshfs_eventloop(struct puffs_usermount *pu, struct psshfs_ctx *pctx)
315 {
316 struct puffs_getreq *pgr;
317 struct puffs_putreq *ppr;
318 struct pollfd pfds[2];
319 int x;
320
321 pgr = puffs_req_makeget(pu, pu->pu_maxreqlen, 0);
322 if (!pgr)
323 err(1, "makegetreq");
324 ppr = puffs_req_makeput(pu);
325 if (!ppr)
326 err(1, "makeputreq");
327
328 x = 1;
329 if (ioctl(pctx->sshfd, FIONBIO, &x) == -1)
330 err(1, "nonblocking descriptor");
331
332 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
333 if (timetodie) {
334 kill(pctx->sshpid, SIGTERM);
335 break;
336 }
337
338 memset(pfds, 0, sizeof(pfds));
339 pfds[PFD_SSH].events = POLLIN;
340 if (!TAILQ_EMPTY(&pctx->outbufq))
341 pfds[PFD_SSH].events |= POLLOUT;
342 pfds[PFD_SSH].fd = pctx->sshfd;
343 pfds[PFD_PUFFS].fd = puffs_getselectable(pu);
344 pfds[PFD_PUFFS].events = POLLIN;
345
346 if (poll(pfds, 2, INFTIM) == -1)
347 err(1, "poll");
348
349 if (pfds[PFD_SSH].revents & POLLOUT)
350 if (psshoutput(pctx) == -1)
351 err(1, "psshoutput");
352
353 /* get & possibly dispatch events from kernel */
354 if (pfds[PFD_PUFFS].revents & POLLIN)
355 if (puffs_req_handle(pu, pgr, ppr, 0) == -1)
356 err(1, "puffs_handlereqs");
357
358 /* get input from sftpd, possibly build more responses */
359 if (pfds[PFD_SSH].revents & POLLIN)
360 if (psshinput(pctx, ppr) == -1)
361 errx(1, "psshinput");
362
363 /* it's likely we got outputtables, poke the ice with a stick */
364 if (psshoutput(pctx) == -1)
365 err(1, "psshoutput");
366
367 /* stuff all replies from both of the above into kernel */
368 if (puffs_req_putput(ppr) == -1)
369 err(1, "putputreq");
370 puffs_req_resetput(ppr);
371 }
372
373 puffs_req_destroyget(pgr);
374 }
375
376 static void
377 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
378 {
379 int fds[2];
380 pid_t pid;
381
382 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
383 err(1, "socketpair");
384
385 pid = fork();
386 switch (pid) {
387 case -1:
388 err(1, "fork");
389 /*NOTREACHED*/
390 case 0: /* child */
391 if (dup2(fds[0], STDIN_FILENO) == -1)
392 err(1, "child dup2");
393 if (dup2(fds[0], STDOUT_FILENO) == -1)
394 err(1, "child dup2");
395 close(fds[0]);
396 close(fds[1]);
397 execvp(sshargs[0], sshargs);
398 break;
399 default:
400 pctx->sshpid = pid;
401 pctx->sshfd = fds[1];
402 close(fds[0]);
403 break;
404 }
405 }
406