Home | History | Annotate | Line # | Download | only in mount_psshfs
fs.c revision 1.7
      1 /*	$NetBSD: fs.c,v 1.7 2007/04/18 15:53:20 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.7 2007/04/18 15:53:20 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 int
     48 psshfs_domount(struct puffs_usermount *pu)
     49 {
     50 	struct statvfs sb;
     51 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
     52 	struct psshfs_node *root = &pctx->psn_root;
     53 	struct puffs_pathobj *po_root;
     54 	struct puffs_node *pn_root;
     55 	struct vattr va;
     56 	struct vattr *rva;
     57 	struct psbuf *pb;
     58 	char *rootpath;
     59 	uint32_t count;
     60 	int rv;
     61 
     62 	pb = psbuf_make(PSB_OUT);
     63 	psbuf_put_1(pb, SSH_FXP_INIT);
     64 	psbuf_put_4(pb, SFTP_PROTOVERSION);
     65 
     66 	while ((rv = psbuf_write(pctx, pb)) != 1)
     67 		if (rv == -1)
     68 			err(1, "write handshake");
     69 
     70 	psbuf_recycle(pb, PSB_IN);
     71 
     72 	while ((rv = psbuf_read(pctx, pb)) != 1)
     73 		if (rv == -1)
     74 			err(1, "read handshake response");
     75 
     76 	if (pb->type != SSH_FXP_VERSION)
     77 		errx(1, "invalid server response");
     78 	pctx->protover = pb->reqid;
     79 
     80 	/* might contain some other stuff, but we're not interested */
     81 
     82 	/* scope out our rootpath */
     83 	psbuf_recycle(pb, PSB_OUT);
     84 	psbuf_put_1(pb, SSH_FXP_REALPATH);
     85 	psbuf_put_4(pb, NEXTREQ(pctx));
     86 	psbuf_put_str(pb, pctx->mountpath);
     87 	while ((rv = psbuf_write(pctx, pb)) != 1)
     88 		if (rv == -1)
     89 			err(1, "realpath query");
     90 
     91 	psbuf_recycle(pb, PSB_IN);
     92 
     93 	while ((rv = psbuf_read(pctx, pb)) != 1)
     94 		if (rv == -1)
     95 			err(1, "read realpath query response");
     96 	if (pb->type != SSH_FXP_NAME)
     97 		errx(1, "invalid server realpath response for \"%s\"",
     98 		    pctx->mountpath);
     99 
    100 	if (!psbuf_get_4(pb, &count))
    101 		errx(1, "invalid realpath response: count");
    102 	if (!psbuf_get_str(pb, &rootpath, NULL))
    103 		errx(1, "invalid realpath response: rootpath");
    104 
    105 	/* stat the rootdir so that we know it's a dir */
    106 	psbuf_recycle(pb, PSB_OUT);
    107 	psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
    108 	while ((rv == psbuf_write(pctx, pb)) != 1)
    109 		if (rv == -1)
    110 			errx(1, "lstat");
    111 
    112 	psbuf_recycle(pb, PSB_IN);
    113 
    114 	while ((rv = psbuf_read(pctx, pb)) != 1)
    115 		if (rv == -1)
    116 			errx(1, "read lstat response");
    117 
    118 	rv = psbuf_expect_attrs(pb, &va);
    119 	if (rv)
    120 		errx(1, "couldn't stat rootpath");
    121 	psbuf_destroy(pb);
    122 
    123 	if (puffs_mode2vt(va.va_mode) != VDIR)
    124 		errx(1, "remote path (%s) not a directory", rootpath);
    125 
    126 	pctx->nextino = 2;
    127 
    128 	memset(root, 0, sizeof(struct psshfs_node));
    129 	pn_root = puffs_pn_new(pu, root);
    130 	if (pn_root == NULL)
    131 		return errno;
    132 	puffs_setroot(pu, pn_root);
    133 
    134 	po_root = puffs_getrootpathobj(pu);
    135 	if (po_root == NULL)
    136 		err(1, "getrootpathobj");
    137 	po_root->po_path = rootpath;
    138 	po_root->po_len = strlen(rootpath);
    139 
    140 	rva = &pn_root->pn_va;
    141 	puffs_setvattr(rva, &va);
    142 	rva->va_fileid = pctx->nextino++;
    143 	rva->va_nlink = 0156; /* XXX */
    144 
    145 	puffs_zerostatvfs(&sb);
    146 	if (puffs_start(pu, pn_root, &sb) != 0)
    147 		return errno;
    148 
    149 	return 0;
    150 }
    151 
    152 int
    153 psshfs_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    154 {
    155 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    156 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    157 
    158 	kill(pctx->sshpid, SIGTERM);
    159 	close(pctx->sshfd);
    160 	return 0;
    161 }
    162 
    163 int
    164 psshfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
    165 	void *fid, size_t *fidsize)
    166 {
    167 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    168 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    169 	struct puffs_node *pn = cookie;
    170 	struct psshfs_node *psn = pn->pn_data;
    171 	struct psshfs_fid *pf = fid;
    172 
    173 	pf->mounttime = pctx->mounttime;
    174 	pf->node = pn;
    175 
    176 	psn->hasfh = 1;
    177 
    178 	return 0;
    179 }
    180 
    181 int
    182 psshfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
    183 	void **fcookie, enum vtype *ftype, voff_t *fsize, dev_t *fdev)
    184 {
    185 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    186 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    187 	struct psshfs_fid *pf = fid;
    188 	struct puffs_node *pn = pf->node;
    189 	struct psshfs_node *psn;
    190 	int rv;
    191 
    192 	if (pf->mounttime != pctx->mounttime)
    193 		return EINVAL;
    194 	if (pn == 0)
    195 		return EINVAL;
    196 	psn = pn->pn_data;
    197 	if (psn->hasfh == 0)
    198 		return EINVAL;
    199 
    200 	/* update node attributes */
    201 	rv = getnodeattr(pcc, pn);
    202 	if (rv)
    203 		return EINVAL;
    204 
    205 	*fcookie = pn;
    206 	*ftype = pn->pn_va.va_type;
    207 	*fsize = pn->pn_va.va_size;
    208 
    209 	return 0;
    210 }
    211