Home | History | Annotate | Line # | Download | only in mount_psshfs
fs.c revision 1.17.6.1
      1  1.17.6.1    jym /*	$NetBSD: fs.c,v 1.17.6.1 2009/05/13 19:20:33 jym 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 #include <sys/cdefs.h>
     29       1.1  pooka #ifndef lint
     30  1.17.6.1    jym __RCSID("$NetBSD: fs.c,v 1.17.6.1 2009/05/13 19:20:33 jym Exp $");
     31       1.1  pooka #endif /* !lint */
     32       1.1  pooka 
     33       1.1  pooka #include <err.h>
     34       1.5  pooka #include <errno.h>
     35       1.1  pooka #include <puffs.h>
     36       1.1  pooka #include <signal.h>
     37       1.1  pooka #include <stdio.h>
     38       1.1  pooka #include <stdlib.h>
     39       1.1  pooka #include <unistd.h>
     40       1.1  pooka 
     41       1.1  pooka #include "psshfs.h"
     42       1.1  pooka #include "sftp_proto.h"
     43       1.1  pooka 
     44       1.8  pooka #define DO_IO(fname, a1, a2, a3, a4, rv)				\
     45      1.17  pooka do {									\
     46       1.8  pooka 	puffs_framebuf_seekset(a2, 0);					\
     47       1.8  pooka 	*(a4) = 0;							\
     48       1.8  pooka 	rv = fname(a1, a2, a3, a4);					\
     49      1.17  pooka 	if (rv || a4 == 0) {						\
     50      1.17  pooka 		fprintf(stderr, "psshfs_handshake failed %d (%s) %d\n",	\
     51      1.17  pooka 		    rv, strerror(rv), *a4);				\
     52      1.17  pooka 		return rv ? rv : EPROTO;				\
     53      1.17  pooka 	}								\
     54      1.17  pooka } while (/*CONSTCOND*/0)
     55      1.17  pooka 
     56      1.17  pooka #define reterr(str, rv)							\
     57      1.17  pooka do {									\
     58      1.17  pooka 	fprintf str;							\
     59      1.17  pooka 	return rv;							\
     60      1.17  pooka } while (/*CONSTCOND*/0)
     61       1.8  pooka 
     62  1.17.6.1    jym /* openssh extensions */
     63  1.17.6.1    jym static const struct extunit {
     64  1.17.6.1    jym 	const char *ext;
     65  1.17.6.1    jym 	const char *val;
     66  1.17.6.1    jym 	int extflag;
     67  1.17.6.1    jym } exttable[] = {
     68  1.17.6.1    jym {
     69  1.17.6.1    jym 	"posix-rename (at) openssh.com",
     70  1.17.6.1    jym 	"1",
     71  1.17.6.1    jym 	SFTP_EXT_POSIX_RENAME,
     72  1.17.6.1    jym },{
     73  1.17.6.1    jym 	"statvfs (at) openssh.com",
     74  1.17.6.1    jym 	"2",
     75  1.17.6.1    jym 	SFTP_EXT_STATVFS,
     76  1.17.6.1    jym },{
     77  1.17.6.1    jym 	"fstatvfs (at) openssh.com",
     78  1.17.6.1    jym 	"2",
     79  1.17.6.1    jym 	SFTP_EXT_FSTATVFS,
     80  1.17.6.1    jym },{
     81  1.17.6.1    jym 	NULL,
     82  1.17.6.1    jym 	NULL,
     83  1.17.6.1    jym 	0
     84  1.17.6.1    jym }};
     85  1.17.6.1    jym 
     86       1.1  pooka int
     87      1.17  pooka psshfs_handshake(struct puffs_usermount *pu)
     88       1.1  pooka {
     89       1.4  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
     90      1.17  pooka 	struct puffs_framebuf *pb;
     91       1.3  pooka 	struct puffs_pathobj *po_root;
     92       1.4  pooka 	struct puffs_node *pn_root;
     93      1.17  pooka 	struct vattr va, *rva;
     94  1.17.6.1    jym 	const struct extunit *extu;
     95       1.1  pooka 	char *rootpath;
     96  1.17.6.1    jym 	char *ext, *val;
     97       1.1  pooka 	uint32_t count;
     98       1.8  pooka 	int rv, done;
     99       1.1  pooka 
    100       1.8  pooka 	pb = psbuf_makeout();
    101       1.1  pooka 	psbuf_put_1(pb, SSH_FXP_INIT);
    102       1.1  pooka 	psbuf_put_4(pb, SFTP_PROTOVERSION);
    103       1.8  pooka 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
    104       1.1  pooka 
    105       1.8  pooka 	puffs_framebuf_recycle(pb);
    106       1.8  pooka 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
    107       1.8  pooka 	if (psbuf_get_type(pb) != SSH_FXP_VERSION)
    108      1.17  pooka 		reterr((stderr, "invalid server response: %d",
    109      1.17  pooka 		    psbuf_get_type(pb)), EPROTO);
    110       1.8  pooka 	pctx->protover = psbuf_get_reqid(pb);
    111  1.17.6.1    jym 
    112  1.17.6.1    jym 	/*
    113  1.17.6.1    jym 	 * Check out which extensions are available.  Currently
    114  1.17.6.1    jym 	 * we are only interested in the openssh statvfs extension.
    115  1.17.6.1    jym 	 */
    116  1.17.6.1    jym 	for (;;) {
    117  1.17.6.1    jym 		if (psbuf_get_str(pb, &ext, NULL) != 0)
    118  1.17.6.1    jym 			break;
    119  1.17.6.1    jym 		if (psbuf_get_str(pb, &val, NULL) != 0)
    120  1.17.6.1    jym 			break;
    121  1.17.6.1    jym 
    122  1.17.6.1    jym 		for (extu = exttable; extu->ext; extu++)
    123  1.17.6.1    jym 			if (strcmp(ext, extu->ext) == 0
    124  1.17.6.1    jym 			    && strcmp(val, extu->val) == 0)
    125  1.17.6.1    jym 				pctx->extensions |= extu->extflag;
    126  1.17.6.1    jym 	}
    127       1.1  pooka 
    128       1.1  pooka 	/* scope out our rootpath */
    129       1.8  pooka 	psbuf_recycleout(pb);
    130       1.1  pooka 	psbuf_put_1(pb, SSH_FXP_REALPATH);
    131       1.1  pooka 	psbuf_put_4(pb, NEXTREQ(pctx));
    132       1.1  pooka 	psbuf_put_str(pb, pctx->mountpath);
    133       1.8  pooka 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
    134       1.8  pooka 
    135       1.8  pooka 	puffs_framebuf_recycle(pb);
    136       1.8  pooka 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
    137       1.8  pooka 	if (psbuf_get_type(pb) != SSH_FXP_NAME)
    138      1.17  pooka 		reterr((stderr, "invalid server realpath response for \"%s\"",
    139      1.17  pooka 		    pctx->mountpath), EPROTO);
    140       1.8  pooka 	if (psbuf_get_4(pb, &count) == -1)
    141      1.17  pooka 		reterr((stderr, "invalid realpath response: count"), EPROTO);
    142       1.8  pooka 	if (psbuf_get_str(pb, &rootpath, NULL) == -1)
    143      1.17  pooka 		reterr((stderr, "invalid realpath response: rootpath"), EPROTO);
    144       1.1  pooka 
    145       1.1  pooka 	/* stat the rootdir so that we know it's a dir */
    146       1.8  pooka 	psbuf_recycleout(pb);
    147       1.1  pooka 	psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
    148       1.8  pooka 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
    149       1.8  pooka 
    150       1.8  pooka 	puffs_framebuf_recycle(pb);
    151       1.8  pooka 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
    152       1.1  pooka 
    153       1.1  pooka 	rv = psbuf_expect_attrs(pb, &va);
    154       1.1  pooka 	if (rv)
    155      1.17  pooka 		reterr((stderr, "couldn't stat rootpath"), rv);
    156       1.8  pooka 	puffs_framebuf_destroy(pb);
    157       1.1  pooka 
    158       1.1  pooka 	if (puffs_mode2vt(va.va_mode) != VDIR)
    159      1.17  pooka 		reterr((stderr, "remote path (%s) not a directory", rootpath),
    160      1.17  pooka 		    ENOTDIR);
    161       1.1  pooka 
    162      1.17  pooka 	pn_root = puffs_getroot(pu);
    163      1.17  pooka 	rva = &pn_root->pn_va;
    164      1.17  pooka 	puffs_setvattr(rva, &va);
    165       1.3  pooka 
    166       1.3  pooka 	po_root = puffs_getrootpathobj(pu);
    167       1.3  pooka 	if (po_root == NULL)
    168       1.3  pooka 		err(1, "getrootpathobj");
    169       1.3  pooka 	po_root->po_path = rootpath;
    170       1.3  pooka 	po_root->po_len = strlen(rootpath);
    171       1.1  pooka 
    172       1.1  pooka 	return 0;
    173       1.1  pooka }
    174       1.1  pooka 
    175       1.1  pooka int
    176  1.17.6.1    jym psshfs_fs_statvfs(struct puffs_usermount *pu, struct statvfs *sbp)
    177  1.17.6.1    jym {
    178  1.17.6.1    jym 	PSSHFSAUTOVAR(pu);
    179  1.17.6.1    jym 	uint64_t tmpval;
    180  1.17.6.1    jym 	uint8_t type;
    181  1.17.6.1    jym 
    182  1.17.6.1    jym 	memset(sbp, 0, sizeof(*sbp));
    183  1.17.6.1    jym 	sbp->f_bsize = sbp->f_frsize = sbp->f_iosize = 512;
    184  1.17.6.1    jym 
    185  1.17.6.1    jym 	if ((pctx->extensions & SFTP_EXT_STATVFS) == 0)
    186  1.17.6.1    jym 		goto out;
    187  1.17.6.1    jym 
    188  1.17.6.1    jym 	psbuf_req_str(pb, SSH_FXP_EXTENDED, reqid, "statvfs (at) openssh.com");
    189  1.17.6.1    jym 	psbuf_put_str(pb, pctx->mountpath);
    190  1.17.6.1    jym 	GETRESPONSE(pb);
    191  1.17.6.1    jym 
    192  1.17.6.1    jym 	type = psbuf_get_type(pb);
    193  1.17.6.1    jym 	if (type != SSH_FXP_EXTENDED_REPLY) {
    194  1.17.6.1    jym 		/* use the default */
    195  1.17.6.1    jym 		goto out;
    196  1.17.6.1    jym 	}
    197  1.17.6.1    jym 
    198  1.17.6.1    jym 	psbuf_get_8(pb, &tmpval);
    199  1.17.6.1    jym 	sbp->f_bsize = tmpval;
    200  1.17.6.1    jym 	psbuf_get_8(pb, &tmpval);
    201  1.17.6.1    jym 	sbp->f_frsize = tmpval;
    202  1.17.6.1    jym 	psbuf_get_8(pb, &sbp->f_blocks);
    203  1.17.6.1    jym 	psbuf_get_8(pb, &sbp->f_bfree);
    204  1.17.6.1    jym 	psbuf_get_8(pb, &sbp->f_bavail);
    205  1.17.6.1    jym 	psbuf_get_8(pb, &sbp->f_files);
    206  1.17.6.1    jym 	psbuf_get_8(pb, &sbp->f_ffree);
    207  1.17.6.1    jym 	psbuf_get_8(pb, &sbp->f_favail);
    208  1.17.6.1    jym 
    209  1.17.6.1    jym 	psbuf_get_8(pb, &tmpval); /* fsid */
    210  1.17.6.1    jym 	psbuf_get_8(pb, &tmpval); /* flag */
    211  1.17.6.1    jym 	psbuf_get_8(pb, &tmpval);
    212  1.17.6.1    jym 	sbp->f_namemax = tmpval;
    213  1.17.6.1    jym 
    214  1.17.6.1    jym  out:
    215  1.17.6.1    jym 	PSSHFSRETURN(rv);
    216  1.17.6.1    jym }
    217  1.17.6.1    jym 
    218  1.17.6.1    jym int
    219      1.15  pooka psshfs_fs_unmount(struct puffs_usermount *pu, int flags)
    220       1.1  pooka {
    221       1.4  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    222       1.1  pooka 
    223       1.1  pooka 	kill(pctx->sshpid, SIGTERM);
    224       1.1  pooka 	close(pctx->sshfd);
    225       1.1  pooka 	return 0;
    226       1.1  pooka }
    227       1.5  pooka 
    228       1.5  pooka int
    229      1.16  pooka psshfs_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
    230       1.5  pooka 	void *fid, size_t *fidsize)
    231       1.5  pooka {
    232       1.5  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    233       1.5  pooka 	struct puffs_node *pn = cookie;
    234       1.5  pooka 	struct psshfs_node *psn = pn->pn_data;
    235       1.5  pooka 	struct psshfs_fid *pf = fid;
    236       1.5  pooka 
    237       1.5  pooka 	pf->mounttime = pctx->mounttime;
    238       1.5  pooka 	pf->node = pn;
    239       1.5  pooka 
    240      1.10  pooka 	psn->stat |= PSN_HASFH;
    241       1.5  pooka 
    242       1.5  pooka 	return 0;
    243       1.5  pooka }
    244       1.5  pooka 
    245       1.5  pooka int
    246      1.15  pooka psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
    247      1.13  pooka 	struct puffs_newinfo *pni)
    248       1.5  pooka {
    249       1.5  pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    250       1.5  pooka 	struct psshfs_fid *pf = fid;
    251       1.5  pooka 	struct puffs_node *pn = pf->node;
    252       1.5  pooka 	struct psshfs_node *psn;
    253       1.5  pooka 	int rv;
    254       1.5  pooka 
    255       1.5  pooka 	if (pf->mounttime != pctx->mounttime)
    256       1.5  pooka 		return EINVAL;
    257       1.5  pooka 	if (pn == 0)
    258       1.5  pooka 		return EINVAL;
    259       1.5  pooka 	psn = pn->pn_data;
    260      1.10  pooka 	if ((psn->stat & PSN_HASFH) == 0)
    261       1.5  pooka 		return EINVAL;
    262       1.5  pooka 
    263       1.5  pooka 	/* update node attributes */
    264      1.15  pooka 	rv = getnodeattr(pu, pn);
    265       1.6  pooka 	if (rv)
    266       1.5  pooka 		return EINVAL;
    267       1.5  pooka 
    268      1.13  pooka 	puffs_newinfo_setcookie(pni, pn);
    269      1.13  pooka 	puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    270      1.13  pooka 	puffs_newinfo_setsize(pni, pn->pn_va.va_size);
    271       1.5  pooka 
    272       1.5  pooka 	return 0;
    273       1.5  pooka }
    274