psshfs.c revision 1.51 1 1.51 pooka /* $NetBSD: psshfs.c,v 1.51 2009/05/20 13:56:36 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka /*
29 1.1 pooka * psshfs: puffs sshfs
30 1.1 pooka *
31 1.1 pooka * psshfs implements sshfs functionality on top of puffs making it
32 1.1 pooka * possible to mount a filesystme through the sftp service.
33 1.1 pooka *
34 1.1 pooka * psshfs can execute multiple operations in "parallel" by using the
35 1.1 pooka * puffs_cc framework for continuations.
36 1.1 pooka *
37 1.1 pooka * Concurrency control is handled currently by vnode locking (this
38 1.1 pooka * will change in the future). Context switch locations are easy to
39 1.17 pooka * find by grepping for puffs_framebuf_enqueue_cc().
40 1.1 pooka */
41 1.1 pooka
42 1.1 pooka #include <sys/cdefs.h>
43 1.1 pooka #ifndef lint
44 1.51 pooka __RCSID("$NetBSD: psshfs.c,v 1.51 2009/05/20 13:56:36 pooka Exp $");
45 1.1 pooka #endif /* !lint */
46 1.1 pooka
47 1.1 pooka #include <sys/types.h>
48 1.1 pooka
49 1.1 pooka #include <assert.h>
50 1.1 pooka #include <err.h>
51 1.1 pooka #include <errno.h>
52 1.2 pooka #include <mntopts.h>
53 1.28 pooka #include <paths.h>
54 1.1 pooka #include <poll.h>
55 1.1 pooka #include <puffs.h>
56 1.1 pooka #include <signal.h>
57 1.1 pooka #include <stdlib.h>
58 1.1 pooka #include <util.h>
59 1.1 pooka #include <unistd.h>
60 1.1 pooka
61 1.1 pooka #include "psshfs.h"
62 1.1 pooka
63 1.51 pooka static int pssh_connect(struct puffs_usermount *, int);
64 1.42 pooka static void psshfs_loopfn(struct puffs_usermount *);
65 1.2 pooka static void usage(void);
66 1.21 tnn static void add_ssharg(char ***, int *, char *);
67 1.49 pooka static void psshfs_notify(struct puffs_usermount *, int, int);
68 1.1 pooka
69 1.1 pooka #define SSH_PATH "/usr/bin/ssh"
70 1.1 pooka
71 1.36 pooka unsigned int max_reads;
72 1.42 pooka static int sighup;
73 1.36 pooka
74 1.2 pooka static void
75 1.21 tnn add_ssharg(char ***sshargs, int *nargs, char *arg)
76 1.21 tnn {
77 1.21 tnn
78 1.21 tnn *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
79 1.21 tnn if (!*sshargs)
80 1.21 tnn err(1, "realloc");
81 1.21 tnn (*sshargs)[(*nargs)++] = arg;
82 1.21 tnn (*sshargs)[*nargs] = NULL;
83 1.21 tnn }
84 1.21 tnn
85 1.21 tnn static void
86 1.2 pooka usage()
87 1.2 pooka {
88 1.2 pooka
89 1.23 pooka fprintf(stderr, "usage: %s "
90 1.51 pooka "[-ceprst] [-F configfile] [-O sshopt=value] [-o opts] "
91 1.47 jmmv "user@host:path mountpath\n",
92 1.4 pooka getprogname());
93 1.23 pooka exit(1);
94 1.2 pooka }
95 1.2 pooka
96 1.42 pooka static void
97 1.42 pooka takehup(int sig)
98 1.42 pooka {
99 1.42 pooka
100 1.42 pooka sighup = 1;
101 1.42 pooka }
102 1.42 pooka
103 1.1 pooka int
104 1.1 pooka main(int argc, char *argv[])
105 1.1 pooka {
106 1.1 pooka struct psshfs_ctx pctx;
107 1.1 pooka struct puffs_usermount *pu;
108 1.1 pooka struct puffs_ops *pops;
109 1.49 pooka struct psshfs_node *root = &pctx.psn_root;
110 1.49 pooka struct puffs_node *pn_root;
111 1.49 pooka puffs_framev_fdnotify_fn notfn;
112 1.49 pooka struct vattr *rva;
113 1.2 pooka mntoptparse_t mp;
114 1.21 tnn char **sshargs;
115 1.1 pooka char *userhost;
116 1.1 pooka char *hostpath;
117 1.37 pooka int mntflags, pflags, ch;
118 1.37 pooka int detach;
119 1.51 pooka int exportfs, refreshival, numconnections;
120 1.51 pooka int nargs;
121 1.1 pooka
122 1.1 pooka setprogname(argv[0]);
123 1.1 pooka
124 1.2 pooka if (argc < 3)
125 1.2 pooka usage();
126 1.2 pooka
127 1.37 pooka mntflags = pflags = exportfs = nargs = 0;
128 1.51 pooka numconnections = 1;
129 1.37 pooka detach = 1;
130 1.40 pooka refreshival = DEFAULTREFRESH;
131 1.49 pooka notfn = puffs_framev_unmountonclose;
132 1.21 tnn sshargs = NULL;
133 1.21 tnn add_ssharg(&sshargs, &nargs, SSH_PATH);
134 1.21 tnn add_ssharg(&sshargs, &nargs, "-axs");
135 1.21 tnn add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
136 1.21 tnn
137 1.51 pooka while ((ch = getopt(argc, argv, "c:eF:o:O:pr:st:")) != -1) {
138 1.2 pooka switch (ch) {
139 1.51 pooka case 'c':
140 1.51 pooka numconnections = atoi(optarg);
141 1.51 pooka if (numconnections < 1 || numconnections > 2) {
142 1.51 pooka fprintf(stderr, "%s: only 1 or 2 connections "
143 1.51 pooka "permitted currently\n", getprogname());
144 1.51 pooka usage();
145 1.51 pooka /*NOTREACHED*/
146 1.51 pooka }
147 1.51 pooka break;
148 1.11 pooka case 'e':
149 1.13 pooka exportfs = 1;
150 1.11 pooka break;
151 1.47 jmmv case 'F':
152 1.47 jmmv add_ssharg(&sshargs, &nargs, "-F");
153 1.47 jmmv add_ssharg(&sshargs, &nargs, optarg);
154 1.47 jmmv break;
155 1.21 tnn case 'O':
156 1.22 tnn add_ssharg(&sshargs, &nargs, "-o");
157 1.22 tnn add_ssharg(&sshargs, &nargs, optarg);
158 1.21 tnn break;
159 1.2 pooka case 'o':
160 1.2 pooka mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
161 1.2 pooka if (mp == NULL)
162 1.2 pooka err(1, "getmntopts");
163 1.2 pooka freemntopts(mp);
164 1.2 pooka break;
165 1.49 pooka case 'p':
166 1.49 pooka notfn = psshfs_notify;
167 1.49 pooka break;
168 1.36 pooka case 'r':
169 1.36 pooka max_reads = atoi(optarg);
170 1.36 pooka break;
171 1.4 pooka case 's':
172 1.37 pooka detach = 0;
173 1.4 pooka break;
174 1.40 pooka case 't':
175 1.40 pooka refreshival = atoi(optarg);
176 1.41 pooka if (refreshival < 0 && refreshival != -1)
177 1.41 pooka errx(1, "invalid timeout %d", refreshival);
178 1.40 pooka break;
179 1.2 pooka default:
180 1.2 pooka usage();
181 1.2 pooka /*NOTREACHED*/
182 1.2 pooka }
183 1.2 pooka }
184 1.2 pooka argc -= optind;
185 1.2 pooka argv += optind;
186 1.2 pooka
187 1.4 pooka if (pflags & PUFFS_FLAG_OPDUMP)
188 1.37 pooka detach = 0;
189 1.20 pooka pflags |= PUFFS_FLAG_BUILDPATH;
190 1.20 pooka pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
191 1.4 pooka
192 1.2 pooka if (argc != 2)
193 1.2 pooka usage();
194 1.1 pooka
195 1.1 pooka PUFFSOP_INIT(pops);
196 1.1 pooka
197 1.1 pooka PUFFSOP_SET(pops, psshfs, fs, unmount);
198 1.1 pooka PUFFSOP_SETFSNOP(pops, sync); /* XXX */
199 1.50 pooka PUFFSOP_SET(pops, psshfs, fs, statvfs);
200 1.11 pooka PUFFSOP_SET(pops, psshfs, fs, nodetofh);
201 1.11 pooka PUFFSOP_SET(pops, psshfs, fs, fhtonode);
202 1.1 pooka
203 1.1 pooka PUFFSOP_SET(pops, psshfs, node, lookup);
204 1.1 pooka PUFFSOP_SET(pops, psshfs, node, create);
205 1.19 pooka PUFFSOP_SET(pops, psshfs, node, open);
206 1.19 pooka PUFFSOP_SET(pops, psshfs, node, inactive);
207 1.1 pooka PUFFSOP_SET(pops, psshfs, node, readdir);
208 1.1 pooka PUFFSOP_SET(pops, psshfs, node, getattr);
209 1.1 pooka PUFFSOP_SET(pops, psshfs, node, setattr);
210 1.1 pooka PUFFSOP_SET(pops, psshfs, node, mkdir);
211 1.1 pooka PUFFSOP_SET(pops, psshfs, node, remove);
212 1.1 pooka PUFFSOP_SET(pops, psshfs, node, readlink);
213 1.1 pooka PUFFSOP_SET(pops, psshfs, node, rmdir);
214 1.1 pooka PUFFSOP_SET(pops, psshfs, node, symlink);
215 1.1 pooka PUFFSOP_SET(pops, psshfs, node, rename);
216 1.1 pooka PUFFSOP_SET(pops, psshfs, node, read);
217 1.1 pooka PUFFSOP_SET(pops, psshfs, node, write);
218 1.1 pooka PUFFSOP_SET(pops, psshfs, node, reclaim);
219 1.1 pooka
220 1.33 pooka pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
221 1.33 pooka if (pu == NULL)
222 1.33 pooka err(1, "puffs_init");
223 1.33 pooka
224 1.1 pooka memset(&pctx, 0, sizeof(pctx));
225 1.11 pooka pctx.mounttime = time(NULL);
226 1.40 pooka pctx.refreshival = refreshival;
227 1.51 pooka pctx.numconnections = numconnections;
228 1.1 pooka
229 1.2 pooka userhost = argv[0];
230 1.1 pooka hostpath = strchr(userhost, ':');
231 1.1 pooka if (hostpath) {
232 1.1 pooka *hostpath++ = '\0';
233 1.1 pooka pctx.mountpath = hostpath;
234 1.1 pooka } else
235 1.1 pooka pctx.mountpath = ".";
236 1.1 pooka
237 1.21 tnn add_ssharg(&sshargs, &nargs, argv[0]);
238 1.21 tnn add_ssharg(&sshargs, &nargs, "sftp");
239 1.49 pooka pctx.sshargs = sshargs;
240 1.49 pooka
241 1.49 pooka pctx.nextino = 2;
242 1.49 pooka memset(root, 0, sizeof(struct psshfs_node));
243 1.49 pooka pn_root = puffs_pn_new(pu, root);
244 1.49 pooka if (pn_root == NULL)
245 1.49 pooka return errno;
246 1.49 pooka puffs_setroot(pu, pn_root);
247 1.1 pooka
248 1.51 pooka puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn);
249 1.51 pooka
250 1.42 pooka signal(SIGHUP, takehup);
251 1.42 pooka puffs_ml_setloopfn(pu, psshfs_loopfn);
252 1.51 pooka if (pssh_connect(pu, PSSHFD_META) == -1)
253 1.51 pooka err(1, "can't connect meta");
254 1.51 pooka if (puffs_framev_addfd(pu, pctx.sshfd,
255 1.51 pooka PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
256 1.51 pooka err(1, "framebuf addfd meta");
257 1.51 pooka if (numconnections == 2) {
258 1.51 pooka if (pssh_connect(pu, PSSHFD_DATA) == -1)
259 1.51 pooka err(1, "can't connect data");
260 1.51 pooka if (puffs_framev_addfd(pu, pctx.sshfd_data,
261 1.51 pooka PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
262 1.51 pooka err(1, "framebuf addfd data");
263 1.51 pooka } else {
264 1.51 pooka pctx.sshfd_data = pctx.sshfd;
265 1.51 pooka }
266 1.4 pooka
267 1.13 pooka if (exportfs)
268 1.13 pooka puffs_setfhsize(pu, sizeof(struct psshfs_fid),
269 1.13 pooka PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
270 1.13 pooka
271 1.49 pooka rva = &pn_root->pn_va;
272 1.49 pooka rva->va_fileid = pctx.nextino++;
273 1.49 pooka rva->va_nlink = 101; /* XXX */
274 1.49 pooka
275 1.37 pooka if (detach)
276 1.44 pooka if (puffs_daemon(pu, 1, 1) == -1)
277 1.44 pooka err(1, "puffs_daemon");
278 1.37 pooka
279 1.38 pooka if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
280 1.38 pooka err(1, "puffs_mount");
281 1.39 pooka if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
282 1.39 pooka err(1, "setblockingmode");
283 1.39 pooka
284 1.37 pooka if (puffs_mainloop(pu) == -1)
285 1.38 pooka err(1, "mainloop");
286 1.48 pooka puffs_exit(pu, 1);
287 1.38 pooka
288 1.37 pooka return 0;
289 1.1 pooka }
290 1.1 pooka
291 1.49 pooka #define RETRY_MAX 100
292 1.49 pooka
293 1.49 pooka void
294 1.49 pooka psshfs_notify(struct puffs_usermount *pu, int fd, int what)
295 1.49 pooka {
296 1.49 pooka struct psshfs_ctx *pctx = puffs_getspecific(pu);
297 1.51 pooka int x, nretry, which, newfd;
298 1.51 pooka
299 1.51 pooka if (fd == pctx->sshfd) {
300 1.51 pooka which = PSSHFD_META;
301 1.51 pooka } else {
302 1.51 pooka assert(fd == pctx->sshfd_data);
303 1.51 pooka which = PSSHFD_DATA;
304 1.51 pooka }
305 1.49 pooka
306 1.49 pooka if (puffs_getstate(pu) != PUFFS_STATE_RUNNING)
307 1.49 pooka return;
308 1.49 pooka
309 1.49 pooka if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) {
310 1.51 pooka puffs_framev_removefd(pu, fd, ECONNRESET);
311 1.49 pooka return;
312 1.49 pooka }
313 1.51 pooka close(fd);
314 1.49 pooka
315 1.49 pooka for (nretry = 0;;nretry++) {
316 1.51 pooka if ((newfd = pssh_connect(pu, which)) == -1)
317 1.49 pooka goto retry2;
318 1.49 pooka
319 1.51 pooka if (psshfs_handshake(pu, newfd) != 0)
320 1.49 pooka goto retry1;
321 1.49 pooka
322 1.49 pooka x = 1;
323 1.51 pooka if (ioctl(newfd, FIONBIO, &x) == -1)
324 1.49 pooka goto retry1;
325 1.49 pooka
326 1.51 pooka if (puffs_framev_addfd(pu, newfd,
327 1.49 pooka PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
328 1.49 pooka goto retry1;
329 1.49 pooka
330 1.49 pooka break;
331 1.49 pooka retry1:
332 1.49 pooka fprintf(stderr, "reconnect failed... ");
333 1.51 pooka close(newfd);
334 1.49 pooka retry2:
335 1.49 pooka if (nretry < RETRY_MAX) {
336 1.49 pooka fprintf(stderr, "retrying\n");
337 1.49 pooka sleep(nretry);
338 1.49 pooka } else {
339 1.49 pooka fprintf(stderr, "retry count exceeded, going south\n");
340 1.49 pooka exit(1); /* XXXXXXX */
341 1.49 pooka }
342 1.49 pooka }
343 1.49 pooka }
344 1.49 pooka
345 1.49 pooka static int
346 1.51 pooka pssh_connect(struct puffs_usermount *pu, int which)
347 1.1 pooka {
348 1.51 pooka struct psshfs_ctx *pctx = puffs_getspecific(pu);
349 1.49 pooka char **sshargs = pctx->sshargs;
350 1.1 pooka int fds[2];
351 1.1 pooka pid_t pid;
352 1.51 pooka int dnfd, x;
353 1.51 pooka int *sshfd;
354 1.51 pooka pid_t *sshpid;
355 1.51 pooka
356 1.51 pooka if (which == PSSHFD_META) {
357 1.51 pooka sshfd = &pctx->sshfd;
358 1.51 pooka sshpid = &pctx->sshpid;
359 1.51 pooka } else {
360 1.51 pooka assert(which == PSSHFD_DATA);
361 1.51 pooka sshfd = &pctx->sshfd_data;
362 1.51 pooka sshpid = &pctx->sshpid_data;
363 1.51 pooka }
364 1.1 pooka
365 1.1 pooka if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
366 1.49 pooka return -1;
367 1.1 pooka
368 1.1 pooka pid = fork();
369 1.1 pooka switch (pid) {
370 1.1 pooka case -1:
371 1.49 pooka return -1;
372 1.1 pooka /*NOTREACHED*/
373 1.1 pooka case 0: /* child */
374 1.1 pooka if (dup2(fds[0], STDIN_FILENO) == -1)
375 1.1 pooka err(1, "child dup2");
376 1.1 pooka if (dup2(fds[0], STDOUT_FILENO) == -1)
377 1.1 pooka err(1, "child dup2");
378 1.1 pooka close(fds[0]);
379 1.1 pooka close(fds[1]);
380 1.28 pooka
381 1.28 pooka dnfd = open(_PATH_DEVNULL, O_RDWR);
382 1.28 pooka if (dnfd != -1)
383 1.28 pooka dup2(dnfd, STDERR_FILENO);
384 1.28 pooka
385 1.1 pooka execvp(sshargs[0], sshargs);
386 1.51 pooka /*NOTREACHED*/
387 1.1 pooka break;
388 1.1 pooka default:
389 1.51 pooka *sshpid = pid;
390 1.51 pooka *sshfd = fds[1];
391 1.1 pooka close(fds[0]);
392 1.1 pooka break;
393 1.1 pooka }
394 1.49 pooka
395 1.51 pooka if (psshfs_handshake(pu, *sshfd) != 0)
396 1.51 pooka errx(1, "psshfs_handshake %d", which);
397 1.51 pooka x = 1;
398 1.51 pooka if (ioctl(*sshfd, FIONBIO, &x) == -1)
399 1.51 pooka err(1, "nonblocking descriptor %d", which);
400 1.51 pooka
401 1.51 pooka return *sshfd;
402 1.1 pooka }
403 1.42 pooka
404 1.42 pooka static void *
405 1.42 pooka invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
406 1.42 pooka {
407 1.42 pooka struct psshfs_node *psn = pn->pn_data;
408 1.42 pooka
409 1.42 pooka psn->attrread = 0;
410 1.42 pooka psn->dentread = 0;
411 1.43 pooka psn->slread = 0;
412 1.42 pooka
413 1.42 pooka return NULL;
414 1.42 pooka }
415 1.42 pooka
416 1.42 pooka static void
417 1.42 pooka psshfs_loopfn(struct puffs_usermount *pu)
418 1.42 pooka {
419 1.42 pooka
420 1.42 pooka if (sighup) {
421 1.42 pooka puffs_pn_nodewalk(pu, invalone, NULL);
422 1.42 pooka sighup = 0;
423 1.42 pooka }
424 1.42 pooka }
425