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