Home | History | Annotate | Line # | Download | only in mount_psshfs
fs.c revision 1.15.8.1
      1  1.15.8.1  wrstuden /*	$NetBSD: fs.c,v 1.15.8.1 2008/09/18 04:30:10 wrstuden 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.15.8.1  wrstuden __RCSID("$NetBSD: fs.c,v 1.15.8.1 2008/09/18 04:30:10 wrstuden 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.15.8.1  wrstuden 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.15.8.1  wrstuden 	if (rv || a4 == 0) {						\
     50  1.15.8.1  wrstuden 		fprintf(stderr, "psshfs_handshake failed %d (%s) %d\n",	\
     51  1.15.8.1  wrstuden 		    rv, strerror(rv), *a4);				\
     52  1.15.8.1  wrstuden 		return rv ? rv : EPROTO;				\
     53  1.15.8.1  wrstuden 	}								\
     54  1.15.8.1  wrstuden } while (/*CONSTCOND*/0)
     55  1.15.8.1  wrstuden 
     56  1.15.8.1  wrstuden #define reterr(str, rv)							\
     57  1.15.8.1  wrstuden do {									\
     58  1.15.8.1  wrstuden 	fprintf str;							\
     59  1.15.8.1  wrstuden 	return rv;							\
     60  1.15.8.1  wrstuden } while (/*CONSTCOND*/0)
     61       1.8     pooka 
     62       1.1     pooka int
     63  1.15.8.1  wrstuden psshfs_handshake(struct puffs_usermount *pu)
     64       1.1     pooka {
     65       1.4     pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
     66  1.15.8.1  wrstuden 	struct puffs_framebuf *pb;
     67       1.3     pooka 	struct puffs_pathobj *po_root;
     68       1.4     pooka 	struct puffs_node *pn_root;
     69  1.15.8.1  wrstuden 	struct vattr va, *rva;
     70       1.1     pooka 	char *rootpath;
     71       1.1     pooka 	uint32_t count;
     72       1.8     pooka 	int rv, done;
     73       1.1     pooka 
     74       1.8     pooka 	pb = psbuf_makeout();
     75       1.1     pooka 	psbuf_put_1(pb, SSH_FXP_INIT);
     76       1.1     pooka 	psbuf_put_4(pb, SFTP_PROTOVERSION);
     77       1.8     pooka 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     78       1.1     pooka 
     79       1.8     pooka 	puffs_framebuf_recycle(pb);
     80       1.8     pooka 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
     81       1.8     pooka 	if (psbuf_get_type(pb) != SSH_FXP_VERSION)
     82  1.15.8.1  wrstuden 		reterr((stderr, "invalid server response: %d",
     83  1.15.8.1  wrstuden 		    psbuf_get_type(pb)), EPROTO);
     84       1.8     pooka 	pctx->protover = psbuf_get_reqid(pb);
     85       1.1     pooka 	/* might contain some other stuff, but we're not interested */
     86       1.1     pooka 
     87       1.1     pooka 	/* scope out our rootpath */
     88       1.8     pooka 	psbuf_recycleout(pb);
     89       1.1     pooka 	psbuf_put_1(pb, SSH_FXP_REALPATH);
     90       1.1     pooka 	psbuf_put_4(pb, NEXTREQ(pctx));
     91       1.1     pooka 	psbuf_put_str(pb, pctx->mountpath);
     92       1.8     pooka 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
     93       1.8     pooka 
     94       1.8     pooka 	puffs_framebuf_recycle(pb);
     95       1.8     pooka 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
     96       1.8     pooka 	if (psbuf_get_type(pb) != SSH_FXP_NAME)
     97  1.15.8.1  wrstuden 		reterr((stderr, "invalid server realpath response for \"%s\"",
     98  1.15.8.1  wrstuden 		    pctx->mountpath), EPROTO);
     99       1.8     pooka 	if (psbuf_get_4(pb, &count) == -1)
    100  1.15.8.1  wrstuden 		reterr((stderr, "invalid realpath response: count"), EPROTO);
    101       1.8     pooka 	if (psbuf_get_str(pb, &rootpath, NULL) == -1)
    102  1.15.8.1  wrstuden 		reterr((stderr, "invalid realpath response: rootpath"), EPROTO);
    103       1.1     pooka 
    104       1.1     pooka 	/* stat the rootdir so that we know it's a dir */
    105       1.8     pooka 	psbuf_recycleout(pb);
    106       1.1     pooka 	psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
    107       1.8     pooka 	DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
    108       1.8     pooka 
    109       1.8     pooka 	puffs_framebuf_recycle(pb);
    110       1.8     pooka 	DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
    111       1.1     pooka 
    112       1.1     pooka 	rv = psbuf_expect_attrs(pb, &va);
    113       1.1     pooka 	if (rv)
    114  1.15.8.1  wrstuden 		reterr((stderr, "couldn't stat rootpath"), rv);
    115       1.8     pooka 	puffs_framebuf_destroy(pb);
    116       1.1     pooka 
    117       1.1     pooka 	if (puffs_mode2vt(va.va_mode) != VDIR)
    118  1.15.8.1  wrstuden 		reterr((stderr, "remote path (%s) not a directory", rootpath),
    119  1.15.8.1  wrstuden 		    ENOTDIR);
    120       1.1     pooka 
    121  1.15.8.1  wrstuden 	pn_root = puffs_getroot(pu);
    122  1.15.8.1  wrstuden 	rva = &pn_root->pn_va;
    123  1.15.8.1  wrstuden 	puffs_setvattr(rva, &va);
    124       1.3     pooka 
    125       1.3     pooka 	po_root = puffs_getrootpathobj(pu);
    126       1.3     pooka 	if (po_root == NULL)
    127       1.3     pooka 		err(1, "getrootpathobj");
    128       1.3     pooka 	po_root->po_path = rootpath;
    129       1.3     pooka 	po_root->po_len = strlen(rootpath);
    130       1.1     pooka 
    131       1.1     pooka 	return 0;
    132       1.1     pooka }
    133       1.1     pooka 
    134       1.1     pooka int
    135      1.15     pooka psshfs_fs_unmount(struct puffs_usermount *pu, int flags)
    136       1.1     pooka {
    137       1.4     pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    138       1.1     pooka 
    139       1.1     pooka 	kill(pctx->sshpid, SIGTERM);
    140       1.1     pooka 	close(pctx->sshfd);
    141       1.1     pooka 	return 0;
    142       1.1     pooka }
    143       1.5     pooka 
    144       1.5     pooka int
    145  1.15.8.1  wrstuden psshfs_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
    146       1.5     pooka 	void *fid, size_t *fidsize)
    147       1.5     pooka {
    148       1.5     pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    149       1.5     pooka 	struct puffs_node *pn = cookie;
    150       1.5     pooka 	struct psshfs_node *psn = pn->pn_data;
    151       1.5     pooka 	struct psshfs_fid *pf = fid;
    152       1.5     pooka 
    153       1.5     pooka 	pf->mounttime = pctx->mounttime;
    154       1.5     pooka 	pf->node = pn;
    155       1.5     pooka 
    156      1.10     pooka 	psn->stat |= PSN_HASFH;
    157       1.5     pooka 
    158       1.5     pooka 	return 0;
    159       1.5     pooka }
    160       1.5     pooka 
    161       1.5     pooka int
    162      1.15     pooka psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
    163      1.13     pooka 	struct puffs_newinfo *pni)
    164       1.5     pooka {
    165       1.5     pooka 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
    166       1.5     pooka 	struct psshfs_fid *pf = fid;
    167       1.5     pooka 	struct puffs_node *pn = pf->node;
    168       1.5     pooka 	struct psshfs_node *psn;
    169       1.5     pooka 	int rv;
    170       1.5     pooka 
    171       1.5     pooka 	if (pf->mounttime != pctx->mounttime)
    172       1.5     pooka 		return EINVAL;
    173       1.5     pooka 	if (pn == 0)
    174       1.5     pooka 		return EINVAL;
    175       1.5     pooka 	psn = pn->pn_data;
    176      1.10     pooka 	if ((psn->stat & PSN_HASFH) == 0)
    177       1.5     pooka 		return EINVAL;
    178       1.5     pooka 
    179       1.5     pooka 	/* update node attributes */
    180      1.15     pooka 	rv = getnodeattr(pu, pn);
    181       1.6     pooka 	if (rv)
    182       1.5     pooka 		return EINVAL;
    183       1.5     pooka 
    184      1.13     pooka 	puffs_newinfo_setcookie(pni, pn);
    185      1.13     pooka 	puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    186      1.13     pooka 	puffs_newinfo_setsize(pni, pn->pn_va.va_size);
    187       1.5     pooka 
    188       1.5     pooka 	return 0;
    189       1.5     pooka }
    190