Home | History | Annotate | Line # | Download | only in mount_psshfs
fs.c revision 1.12
      1 /*	$NetBSD: fs.c,v 1.12 2007/07/01 17:23:45 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  *
     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
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #ifndef lint
     30 __RCSID("$NetBSD: fs.c,v 1.12 2007/07/01 17:23:45 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <err.h>
     34 #include <errno.h>
     35 #include <puffs.h>
     36 #include <signal.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <unistd.h>
     40 
     41 #include "psshfs.h"
     42 #include "sftp_proto.h"
     43 
     44 #define DO_IO(fname, a1, a2, a3, a4, rv)				\
     45 	puffs_framebuf_seekset(a2, 0);					\
     46 	*(a4) = 0;							\
     47 	rv = fname(a1, a2, a3, a4);					\
     48 	if (rv || a4 == 0) errx(1, "psshfs_domount io failed %d, %d", rv, *a4)
     49 
     50 int
     51 psshfs_domount(struct puffs_usermount *pu)
     52 {
     53 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
     54 	struct psshfs_node *root = &pctx->psn_root;
     55 	struct puffs_pathobj *po_root;
     56 	struct puffs_node *pn_root;
     57 	struct vattr va;
     58 	struct vattr *rva;
     59 	struct puffs_framebuf *pb;
     60 	char *rootpath;
     61 	uint32_t count;
     62 	int rv, done;
     63 
     64 	pb = psbuf_makeout();
     65 	psbuf_put_1(pb, SSH_FXP_INIT);
     66 	psbuf_put_4(pb, SFTP_PROTOVERSION);
     67 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     68 
     69 	puffs_framebuf_recycle(pb);
     70 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
     71 	if (psbuf_get_type(pb) != SSH_FXP_VERSION)
     72 		errx(1, "invalid server response: %d", psbuf_get_type(pb));
     73 	pctx->protover = psbuf_get_reqid(pb);
     74 	/* might contain some other stuff, but we're not interested */
     75 
     76 	/* scope out our rootpath */
     77 	psbuf_recycleout(pb);
     78 	psbuf_put_1(pb, SSH_FXP_REALPATH);
     79 	psbuf_put_4(pb, NEXTREQ(pctx));
     80 	psbuf_put_str(pb, pctx->mountpath);
     81 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     82 
     83 	puffs_framebuf_recycle(pb);
     84 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
     85 	if (psbuf_get_type(pb) != SSH_FXP_NAME)
     86 		errx(1, "invalid server realpath response for \"%s\"",
     87 		    pctx->mountpath);
     88 	if (psbuf_get_4(pb, &count) == -1)
     89 		errx(1, "invalid realpath response: count");
     90 	if (psbuf_get_str(pb, &rootpath, NULL) == -1)
     91 		errx(1, "invalid realpath response: rootpath");
     92 
     93 	/* stat the rootdir so that we know it's a dir */
     94 	psbuf_recycleout(pb);
     95 	psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
     96 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     97 
     98 	puffs_framebuf_recycle(pb);
     99 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
    100 
    101 	rv = psbuf_expect_attrs(pb, &va);
    102 	if (rv)
    103 		errx(1, "couldn't stat rootpath");
    104 	puffs_framebuf_destroy(pb);
    105 
    106 	if (puffs_mode2vt(va.va_mode) != VDIR)
    107 		errx(1, "remote path (%s) not a directory", rootpath);
    108 
    109 	pctx->nextino = 2;
    110 
    111 	memset(root, 0, sizeof(struct psshfs_node));
    112 	pn_root = puffs_pn_new(pu, root);
    113 	if (pn_root == NULL)
    114 		return errno;
    115 	puffs_setroot(pu, pn_root);
    116 
    117 	po_root = puffs_getrootpathobj(pu);
    118 	if (po_root == NULL)
    119 		err(1, "getrootpathobj");
    120 	po_root->po_path = rootpath;
    121 	po_root->po_len = strlen(rootpath);
    122 
    123 	rva = &pn_root->pn_va;
    124 	puffs_setvattr(rva, &va);
    125 	rva->va_fileid = pctx->nextino++;
    126 	rva->va_nlink = 0156; /* XXX */
    127 
    128 	return 0;
    129 }
    130 
    131 int
    132 psshfs_fs_unmount(struct puffs_cc *pcc, int flags, const struct puffs_cid *pcid)
    133 {
    134 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    135 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    136 
    137 	kill(pctx->sshpid, SIGTERM);
    138 	close(pctx->sshfd);
    139 	return 0;
    140 }
    141 
    142 int
    143 psshfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
    144 	void *fid, size_t *fidsize)
    145 {
    146 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    147 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    148 	struct puffs_node *pn = cookie;
    149 	struct psshfs_node *psn = pn->pn_data;
    150 	struct psshfs_fid *pf = fid;
    151 
    152 	pf->mounttime = pctx->mounttime;
    153 	pf->node = pn;
    154 
    155 	psn->stat |= PSN_HASFH;
    156 
    157 	return 0;
    158 }
    159 
    160 int
    161 psshfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
    162 	void **fcookie, enum vtype *ftype, voff_t *fsize, dev_t *fdev)
    163 {
    164 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    165 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    166 	struct psshfs_fid *pf = fid;
    167 	struct puffs_node *pn = pf->node;
    168 	struct psshfs_node *psn;
    169 	int rv;
    170 
    171 	if (pf->mounttime != pctx->mounttime)
    172 		return EINVAL;
    173 	if (pn == 0)
    174 		return EINVAL;
    175 	psn = pn->pn_data;
    176 	if ((psn->stat & PSN_HASFH) == 0)
    177 		return EINVAL;
    178 
    179 	/* update node attributes */
    180 	rv = getnodeattr(pcc, pn);
    181 	if (rv)
    182 		return EINVAL;
    183 
    184 	*fcookie = pn;
    185 	*ftype = pn->pn_va.va_type;
    186 	*fsize = pn->pn_va.va_size;
    187 
    188 	return 0;
    189 }
    190