2007-12-14 13:56:22 +03:00
|
|
|
/* $NetBSD: psshfs.c,v 1.47 2007/12/14 10:56:22 jmmv Exp $ */
|
2006-12-29 18:35:39 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Antti Kantee. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* psshfs: puffs sshfs
|
|
|
|
*
|
|
|
|
* psshfs implements sshfs functionality on top of puffs making it
|
|
|
|
* possible to mount a filesystme through the sftp service.
|
|
|
|
*
|
|
|
|
* psshfs can execute multiple operations in "parallel" by using the
|
|
|
|
* puffs_cc framework for continuations.
|
|
|
|
*
|
|
|
|
* Concurrency control is handled currently by vnode locking (this
|
|
|
|
* will change in the future). Context switch locations are easy to
|
2007-05-05 19:49:51 +04:00
|
|
|
* find by grepping for puffs_framebuf_enqueue_cc().
|
2006-12-29 18:35:39 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2007-12-14 13:56:22 +03:00
|
|
|
__RCSID("$NetBSD: psshfs.c,v 1.47 2007/12/14 10:56:22 jmmv Exp $");
|
2006-12-29 18:35:39 +03:00
|
|
|
#endif /* !lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2007-01-06 21:25:19 +03:00
|
|
|
#include <mntopts.h>
|
2007-05-21 12:55:04 +04:00
|
|
|
#include <paths.h>
|
2006-12-29 18:35:39 +03:00
|
|
|
#include <poll.h>
|
|
|
|
#include <puffs.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <util.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "psshfs.h"
|
|
|
|
|
|
|
|
static void pssh_connect(struct psshfs_ctx *, char **);
|
2007-11-08 20:49:43 +03:00
|
|
|
static void psshfs_loopfn(struct puffs_usermount *);
|
2007-01-06 21:25:19 +03:00
|
|
|
static void usage(void);
|
2007-05-09 23:54:39 +04:00
|
|
|
static void add_ssharg(char ***, int *, char *);
|
2006-12-29 18:35:39 +03:00
|
|
|
|
|
|
|
#define SSH_PATH "/usr/bin/ssh"
|
|
|
|
|
2007-10-20 23:14:27 +04:00
|
|
|
unsigned int max_reads;
|
2007-11-08 20:49:43 +03:00
|
|
|
static int sighup;
|
2007-10-20 23:14:27 +04:00
|
|
|
|
2007-05-09 23:54:39 +04:00
|
|
|
static void
|
|
|
|
add_ssharg(char ***sshargs, int *nargs, char *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
*sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
|
|
|
|
if (!*sshargs)
|
|
|
|
err(1, "realloc");
|
|
|
|
(*sshargs)[(*nargs)++] = arg;
|
|
|
|
(*sshargs)[*nargs] = NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-06 21:25:19 +03:00
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
|
2007-05-10 01:46:27 +04:00
|
|
|
fprintf(stderr, "usage: %s "
|
2007-12-14 13:56:22 +03:00
|
|
|
"[-es] [-F configfile] [-O sshopt=value] [-o opts] "
|
|
|
|
"user@host:path mountpath\n",
|
2007-01-11 21:52:26 +03:00
|
|
|
getprogname());
|
2007-05-10 01:46:27 +04:00
|
|
|
exit(1);
|
2007-01-06 21:25:19 +03:00
|
|
|
}
|
|
|
|
|
2007-11-08 20:49:43 +03:00
|
|
|
static void
|
|
|
|
takehup(int sig)
|
|
|
|
{
|
|
|
|
|
|
|
|
sighup = 1;
|
|
|
|
}
|
|
|
|
|
2006-12-29 18:35:39 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct psshfs_ctx pctx;
|
|
|
|
struct puffs_usermount *pu;
|
|
|
|
struct puffs_ops *pops;
|
2007-01-06 21:25:19 +03:00
|
|
|
mntoptparse_t mp;
|
2007-05-09 23:54:39 +04:00
|
|
|
char **sshargs;
|
2006-12-29 18:35:39 +03:00
|
|
|
char *userhost;
|
|
|
|
char *hostpath;
|
2007-11-05 20:48:17 +03:00
|
|
|
int mntflags, pflags, ch;
|
|
|
|
int detach;
|
2007-11-08 19:40:14 +03:00
|
|
|
int exportfs, refreshival;
|
2007-05-09 23:54:39 +04:00
|
|
|
int nargs, x;
|
2006-12-29 18:35:39 +03:00
|
|
|
|
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2007-01-06 21:25:19 +03:00
|
|
|
if (argc < 3)
|
|
|
|
usage();
|
|
|
|
|
2007-11-05 20:48:17 +03:00
|
|
|
mntflags = pflags = exportfs = nargs = 0;
|
|
|
|
detach = 1;
|
2007-11-08 19:40:14 +03:00
|
|
|
refreshival = DEFAULTREFRESH;
|
2007-05-09 23:54:39 +04:00
|
|
|
sshargs = NULL;
|
|
|
|
add_ssharg(&sshargs, &nargs, SSH_PATH);
|
|
|
|
add_ssharg(&sshargs, &nargs, "-axs");
|
|
|
|
add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
|
|
|
|
|
2007-12-14 13:56:22 +03:00
|
|
|
while ((ch = getopt(argc, argv, "eF:o:O:r:st:")) != -1) {
|
2007-01-06 21:25:19 +03:00
|
|
|
switch (ch) {
|
2007-04-13 00:42:46 +04:00
|
|
|
case 'e':
|
2007-04-16 17:06:39 +04:00
|
|
|
exportfs = 1;
|
2007-04-13 00:42:46 +04:00
|
|
|
break;
|
2007-12-14 13:56:22 +03:00
|
|
|
case 'F':
|
|
|
|
add_ssharg(&sshargs, &nargs, "-F");
|
|
|
|
add_ssharg(&sshargs, &nargs, optarg);
|
|
|
|
break;
|
2007-05-09 23:54:39 +04:00
|
|
|
case 'O':
|
2007-05-10 00:25:20 +04:00
|
|
|
add_ssharg(&sshargs, &nargs, "-o");
|
|
|
|
add_ssharg(&sshargs, &nargs, optarg);
|
2007-05-09 23:54:39 +04:00
|
|
|
break;
|
2007-01-06 21:25:19 +03:00
|
|
|
case 'o':
|
|
|
|
mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
|
|
|
|
if (mp == NULL)
|
|
|
|
err(1, "getmntopts");
|
|
|
|
freemntopts(mp);
|
|
|
|
break;
|
2007-10-20 23:14:27 +04:00
|
|
|
case 'r':
|
|
|
|
max_reads = atoi(optarg);
|
|
|
|
break;
|
2007-01-11 21:52:26 +03:00
|
|
|
case 's':
|
2007-11-05 20:48:17 +03:00
|
|
|
detach = 0;
|
2007-01-11 21:52:26 +03:00
|
|
|
break;
|
2007-11-08 19:40:14 +03:00
|
|
|
case 't':
|
|
|
|
refreshival = atoi(optarg);
|
2007-11-08 19:42:31 +03:00
|
|
|
if (refreshival < 0 && refreshival != -1)
|
|
|
|
errx(1, "invalid timeout %d", refreshival);
|
2007-11-08 19:40:14 +03:00
|
|
|
break;
|
2007-01-06 21:25:19 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2007-01-11 21:52:26 +03:00
|
|
|
if (pflags & PUFFS_FLAG_OPDUMP)
|
2007-11-05 20:48:17 +03:00
|
|
|
detach = 0;
|
2007-05-07 21:20:58 +04:00
|
|
|
pflags |= PUFFS_FLAG_BUILDPATH;
|
|
|
|
pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
|
2007-01-11 21:52:26 +03:00
|
|
|
|
2007-01-06 21:25:19 +03:00
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
2006-12-29 18:35:39 +03:00
|
|
|
|
|
|
|
PUFFSOP_INIT(pops);
|
|
|
|
|
|
|
|
PUFFSOP_SET(pops, psshfs, fs, unmount);
|
|
|
|
PUFFSOP_SETFSNOP(pops, sync); /* XXX */
|
|
|
|
PUFFSOP_SETFSNOP(pops, statvfs);
|
2007-04-13 00:42:46 +04:00
|
|
|
PUFFSOP_SET(pops, psshfs, fs, nodetofh);
|
|
|
|
PUFFSOP_SET(pops, psshfs, fs, fhtonode);
|
2006-12-29 18:35:39 +03:00
|
|
|
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, lookup);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, create);
|
2007-05-06 23:48:51 +04:00
|
|
|
PUFFSOP_SET(pops, psshfs, node, open);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, inactive);
|
2006-12-29 18:35:39 +03:00
|
|
|
PUFFSOP_SET(pops, psshfs, node, readdir);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, getattr);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, setattr);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, mkdir);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, remove);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, readlink);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, rmdir);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, symlink);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, rename);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, read);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, write);
|
|
|
|
PUFFSOP_SET(pops, psshfs, node, reclaim);
|
|
|
|
|
2007-07-17 15:34:51 +04:00
|
|
|
pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
|
|
|
|
if (pu == NULL)
|
|
|
|
err(1, "puffs_init");
|
|
|
|
|
2006-12-29 18:35:39 +03:00
|
|
|
memset(&pctx, 0, sizeof(pctx));
|
2007-04-13 00:42:46 +04:00
|
|
|
pctx.mounttime = time(NULL);
|
2007-11-08 19:40:14 +03:00
|
|
|
pctx.refreshival = refreshival;
|
2006-12-29 18:35:39 +03:00
|
|
|
|
2007-01-06 21:25:19 +03:00
|
|
|
userhost = argv[0];
|
2006-12-29 18:35:39 +03:00
|
|
|
hostpath = strchr(userhost, ':');
|
|
|
|
if (hostpath) {
|
|
|
|
*hostpath++ = '\0';
|
|
|
|
pctx.mountpath = hostpath;
|
|
|
|
} else
|
|
|
|
pctx.mountpath = ".";
|
|
|
|
|
2007-05-09 23:54:39 +04:00
|
|
|
add_ssharg(&sshargs, &nargs, argv[0]);
|
|
|
|
add_ssharg(&sshargs, &nargs, "sftp");
|
2006-12-29 18:35:39 +03:00
|
|
|
|
2007-11-08 20:49:43 +03:00
|
|
|
signal(SIGHUP, takehup);
|
|
|
|
puffs_ml_setloopfn(pu, psshfs_loopfn);
|
|
|
|
|
2007-01-11 21:52:26 +03:00
|
|
|
pssh_connect(&pctx, sshargs);
|
|
|
|
|
2007-04-16 17:06:39 +04:00
|
|
|
if (exportfs)
|
|
|
|
puffs_setfhsize(pu, sizeof(struct psshfs_fid),
|
|
|
|
PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
|
|
|
|
|
2007-01-06 21:25:19 +03:00
|
|
|
if (psshfs_domount(pu) != 0)
|
2007-04-16 17:06:39 +04:00
|
|
|
errx(1, "psshfs_domount");
|
2006-12-29 18:35:39 +03:00
|
|
|
x = 1;
|
2007-05-05 19:49:51 +04:00
|
|
|
if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
|
2006-12-29 18:35:39 +03:00
|
|
|
err(1, "nonblocking descriptor");
|
|
|
|
|
2007-09-01 20:43:10 +04:00
|
|
|
puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL,
|
2007-05-15 17:46:47 +04:00
|
|
|
puffs_framev_unmountonclose);
|
2007-07-08 01:14:26 +04:00
|
|
|
if (puffs_framev_addfd(pu, pctx.sshfd,
|
|
|
|
PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
|
2007-05-12 01:27:45 +04:00
|
|
|
err(1, "framebuf addfd");
|
2006-12-29 18:35:39 +03:00
|
|
|
|
2007-11-05 20:48:17 +03:00
|
|
|
if (detach)
|
2007-11-16 21:39:01 +03:00
|
|
|
if (puffs_daemon(pu, 1, 1) == -1)
|
|
|
|
err(1, "puffs_daemon");
|
2007-11-05 20:48:17 +03:00
|
|
|
|
2007-11-05 20:54:31 +03:00
|
|
|
if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
|
|
|
|
err(1, "puffs_mount");
|
2007-11-06 18:09:07 +03:00
|
|
|
if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
|
|
|
|
err(1, "setblockingmode");
|
|
|
|
|
2007-11-05 20:48:17 +03:00
|
|
|
if (puffs_mainloop(pu) == -1)
|
2007-11-05 20:54:31 +03:00
|
|
|
err(1, "mainloop");
|
|
|
|
|
2007-11-05 20:48:17 +03:00
|
|
|
return 0;
|
2006-12-29 18:35:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
|
|
|
|
{
|
|
|
|
int fds[2];
|
|
|
|
pid_t pid;
|
2007-05-21 12:55:04 +04:00
|
|
|
int dnfd;
|
2006-12-29 18:35:39 +03:00
|
|
|
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
|
|
|
|
err(1, "socketpair");
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case -1:
|
|
|
|
err(1, "fork");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case 0: /* child */
|
|
|
|
if (dup2(fds[0], STDIN_FILENO) == -1)
|
|
|
|
err(1, "child dup2");
|
|
|
|
if (dup2(fds[0], STDOUT_FILENO) == -1)
|
|
|
|
err(1, "child dup2");
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
2007-05-21 12:55:04 +04:00
|
|
|
|
|
|
|
dnfd = open(_PATH_DEVNULL, O_RDWR);
|
|
|
|
if (dnfd != -1)
|
|
|
|
dup2(dnfd, STDERR_FILENO);
|
|
|
|
|
2006-12-29 18:35:39 +03:00
|
|
|
execvp(sshargs[0], sshargs);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pctx->sshpid = pid;
|
|
|
|
pctx->sshfd = fds[1];
|
|
|
|
close(fds[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-08 20:49:43 +03:00
|
|
|
|
|
|
|
static void *
|
|
|
|
invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
|
|
|
|
{
|
|
|
|
struct psshfs_node *psn = pn->pn_data;
|
|
|
|
|
|
|
|
psn->attrread = 0;
|
|
|
|
psn->dentread = 0;
|
2007-11-11 21:06:35 +03:00
|
|
|
psn->slread = 0;
|
2007-11-08 20:49:43 +03:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
psshfs_loopfn(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (sighup) {
|
|
|
|
puffs_pn_nodewalk(pu, invalone, NULL);
|
|
|
|
sighup = 0;
|
|
|
|
}
|
|
|
|
}
|