1 /* $NetBSD: psshfs.c,v 1.63 2011/05/19 15:07:16 riastradh Exp $ */
4 * Copyright (c) 2006-2009 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * psshfs implements sshfs functionality on top of puffs making it
32 * possible to mount a filesystme through the sftp service.
34 * psshfs can execute multiple operations in "parallel" by using the
35 * puffs_cc framework for continuations.
37 * Concurrency control is handled currently by vnode locking (this
38 * will change in the future). Context switch locations are easy to
39 * find by grepping for puffs_framebuf_enqueue_cc().
42 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
60 #include "util_compat.h"
62 static int pssh_connect(struct puffs_usermount *, int);
63 static void psshfs_loopfn(struct puffs_usermount *);
64 static void usage(void);
65 static void add_ssharg(char ***, int *, const char *);
66 static void psshfs_notify(struct puffs_usermount *, int, int);
68 #define SSH_PATH "/usr/bin/ssh"
70 unsigned int max_reads;
74 add_ssharg(char ***sshargs, int *nargs, const char *arg)
77 *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
80 (*sshargs)[(*nargs)++] = estrdup(arg);
81 (*sshargs)[*nargs] = NULL;
88 fprintf(stderr, "usage: %s "
89 "[-ceprst] [-F configfile] [-O sshopt=value] [-o opts] "
90 "user@host:path mountpath\n",
103 main(int argc, char *argv[])
105 struct psshfs_ctx pctx;
106 struct puffs_usermount *pu;
107 struct puffs_ops *pops;
108 struct psshfs_node *root = &pctx.psn_root;
109 struct puffs_node *pn_root;
110 puffs_framev_fdnotify_fn notfn;
115 int mntflags, pflags, ch;
117 int exportfs, refreshival, numconnections;
120 setprogname(argv[0]);
121 puffs_unmountonsignal(SIGINT, true);
122 puffs_unmountonsignal(SIGTERM, true);
127 memset(&pctx, 0, sizeof(pctx));
128 mntflags = pflags = exportfs = nargs = 0;
131 refreshival = DEFAULTREFRESH;
132 notfn = puffs_framev_unmountonclose;
134 add_ssharg(&sshargs, &nargs, SSH_PATH);
135 add_ssharg(&sshargs, &nargs, "-axs");
136 add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
138 while ((ch = getopt(argc, argv, "c:eF:g:o:O:pr:st:u:")) != -1) {
141 numconnections = atoi(optarg);
142 if (numconnections < 1 || numconnections > 2) {
143 fprintf(stderr, "%s: only 1 or 2 connections "
144 "permitted currently\n", getprogname());
153 add_ssharg(&sshargs, &nargs, "-F");
154 add_ssharg(&sshargs, &nargs, optarg);
157 pctx.domanglegid = 1;
158 pctx.manglegid = atoi(optarg);
159 if (pctx.manglegid == (gid_t)-1)
160 errx(1, "-1 not allowed for -g");
161 pctx.mygid = getegid();
164 add_ssharg(&sshargs, &nargs, "-o");
165 add_ssharg(&sshargs, &nargs, optarg);
168 getmntopts(optarg, puffsmopts, &mntflags, &pflags);
171 notfn = psshfs_notify;
174 max_reads = atoi(optarg);
180 refreshival = atoi(optarg);
181 if (refreshival < 0 && refreshival != -1)
182 errx(1, "invalid timeout %d", refreshival);
185 pctx.domangleuid = 1;
186 pctx.mangleuid = atoi(optarg);
187 if (pctx.mangleuid == (uid_t)-1)
188 errx(1, "-1 not allowed for -u");
189 pctx.myuid = geteuid();
199 if (pflags & PUFFS_FLAG_OPDUMP)
201 pflags |= PUFFS_FLAG_BUILDPATH;
202 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
209 PUFFSOP_SET(pops, psshfs, fs, unmount);
210 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
211 PUFFSOP_SET(pops, psshfs, fs, statvfs);
212 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
213 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
215 PUFFSOP_SET(pops, psshfs, node, lookup);
216 PUFFSOP_SET(pops, psshfs, node, create);
217 PUFFSOP_SET(pops, psshfs, node, open);
218 PUFFSOP_SET(pops, psshfs, node, inactive);
219 PUFFSOP_SET(pops, psshfs, node, readdir);
220 PUFFSOP_SET(pops, psshfs, node, getattr);
221 PUFFSOP_SET(pops, psshfs, node, setattr);
222 PUFFSOP_SET(pops, psshfs, node, mkdir);
223 PUFFSOP_SET(pops, psshfs, node, remove);
224 PUFFSOP_SET(pops, psshfs, node, readlink);
225 PUFFSOP_SET(pops, psshfs, node, rmdir);
226 PUFFSOP_SET(pops, psshfs, node, link);
227 PUFFSOP_SET(pops, psshfs, node, symlink);
228 PUFFSOP_SET(pops, psshfs, node, rename);
229 PUFFSOP_SET(pops, psshfs, node, read);
230 PUFFSOP_SET(pops, psshfs, node, write);
231 PUFFSOP_SET(pops, psshfs, node, reclaim);
233 pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
235 err(1, "puffs_init");
237 pctx.mounttime = time(NULL);
238 pctx.refreshival = refreshival;
239 pctx.numconnections = numconnections;
242 hostpath = strchr(userhost, ':');
245 pctx.mountpath = hostpath;
247 pctx.mountpath = ".";
249 add_ssharg(&sshargs, &nargs, argv[0]);
250 add_ssharg(&sshargs, &nargs, "sftp");
251 pctx.sshargs = sshargs;
254 memset(root, 0, sizeof(struct psshfs_node));
255 TAILQ_INIT(&root->pw);
256 pn_root = puffs_pn_new(pu, root);
259 puffs_setroot(pu, pn_root);
261 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn);
263 signal(SIGHUP, takehup);
264 puffs_ml_setloopfn(pu, psshfs_loopfn);
265 if (pssh_connect(pu, PSSHFD_META) == -1)
266 err(1, "can't connect meta");
267 if (puffs_framev_addfd(pu, pctx.sshfd,
268 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
269 err(1, "framebuf addfd meta");
270 if (numconnections == 2) {
271 if (pssh_connect(pu, PSSHFD_DATA) == -1)
272 err(1, "can't connect data");
273 if (puffs_framev_addfd(pu, pctx.sshfd_data,
274 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
275 err(1, "framebuf addfd data");
277 pctx.sshfd_data = pctx.sshfd;
281 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
282 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
284 rva = &pn_root->pn_va;
285 rva->va_fileid = pctx.nextino++;
288 * For root link count, just guess something ridiculously high.
289 * Guessing too high has no known adverse effects, but fts(3)
290 * doesn't like too low values. This guess will be replaced
291 * with the real value when readdir is first called for
292 * the root directory.
294 rva->va_nlink = 8811;
297 if (puffs_daemon(pu, 1, 1) == -1)
298 err(1, "puffs_daemon");
300 if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
301 err(1, "puffs_mount");
302 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
303 err(1, "setblockingmode");
305 if (puffs_mainloop(pu) == -1)
312 #define RETRY_MAX 100
315 psshfs_notify(struct puffs_usermount *pu, int fd, int what)
317 struct psshfs_ctx *pctx = puffs_getspecific(pu);
318 int nretry, which, newfd, dummy;
320 if (fd == pctx->sshfd) {
323 assert(fd == pctx->sshfd_data);
327 if (puffs_getstate(pu) != PUFFS_STATE_RUNNING)
330 if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) {
331 puffs_framev_removefd(pu, fd, ECONNRESET);
336 /* deal with zmobies, beware of half-eaten brain */
337 while (waitpid(-1, &dummy, WNOHANG) > 0)
340 for (nretry = 0;;nretry++) {
341 if ((newfd = pssh_connect(pu, which)) == -1)
344 if (puffs_framev_addfd(pu, newfd,
345 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
350 fprintf(stderr, "reconnect failed... ");
353 if (nretry < RETRY_MAX) {
354 fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry);
357 fprintf(stderr, "retry count exceeded, going south\n");
358 exit(1); /* XXXXXXX */
364 pssh_connect(struct puffs_usermount *pu, int which)
366 struct psshfs_ctx *pctx = puffs_getspecific(pu);
367 char * const *sshargs = pctx->sshargs;
374 if (which == PSSHFD_META) {
375 sshfd = &pctx->sshfd;
376 sshpid = &pctx->sshpid;
378 assert(which == PSSHFD_DATA);
379 sshfd = &pctx->sshfd_data;
380 sshpid = &pctx->sshpid_data;
383 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
392 if (dup2(fds[0], STDIN_FILENO) == -1)
393 err(1, "child dup2");
394 if (dup2(fds[0], STDOUT_FILENO) == -1)
395 err(1, "child dup2");
399 dnfd = open(_PATH_DEVNULL, O_RDWR);
401 dup2(dnfd, STDERR_FILENO);
403 execvp(sshargs[0], sshargs);
413 if (psshfs_handshake(pu, *sshfd) != 0)
414 errx(1, "handshake failed, server does not support sftp?");
416 if (ioctl(*sshfd, FIONBIO, &x) == -1)
417 err(1, "nonblocking descriptor %d", which);
423 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
425 struct psshfs_node *psn = pn->pn_data;
435 psshfs_loopfn(struct puffs_usermount *pu)
439 puffs_pn_nodewalk(pu, invalone, NULL);