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