Home | History | Annotate | Line # | Download | only in mount_psshfs
fs.c revision 1.1
      1 /*	$NetBSD: fs.c,v 1.1 2006/12/29 15:35:39 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.1 2006/12/29 15:35:39 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <err.h>
     37 #include <puffs.h>
     38 #include <signal.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <unistd.h>
     42 
     43 #include "psshfs.h"
     44 #include "sftp_proto.h"
     45 
     46 int
     47 psshfs_fs_mount(struct puffs_usermount *pu, void **rootcookie,
     48 	struct statvfs *sbp)
     49 {
     50 	struct psshfs_ctx *pctx = pu->pu_privdata;
     51 	struct psshfs_node *root = &pctx->psn_root;
     52 	struct vattr va;
     53 	struct vattr *rva;
     54 	struct psbuf *pb;
     55 	char *rootpath;
     56 	uint32_t count;
     57 	int rv;
     58 
     59 	pb = psbuf_make(PSB_OUT);
     60 	psbuf_put_1(pb, SSH_FXP_INIT);
     61 	psbuf_put_4(pb, SFTP_PROTOVERSION);
     62 
     63 	while ((rv = psbuf_write(pctx, pb)) != 1)
     64 		if (rv == -1)
     65 			err(1, "write handshake");
     66 
     67 	psbuf_destroy(pb);
     68 	pb = psbuf_make(PSB_IN);
     69 
     70 	while ((rv = psbuf_read(pctx, pb)) != 1)
     71 		if (rv == -1)
     72 			err(1, "read handshake response");
     73 
     74 	if (pb->type != SSH_FXP_VERSION)
     75 		errx(1, "invalid server response");
     76 	pctx->protover = pb->reqid;
     77 
     78 	/* might contain some other stuff, but we're not interested */
     79 
     80 	/* scope out our rootpath */
     81 	psbuf_recycle(pb, PSB_OUT);
     82 	psbuf_put_1(pb, SSH_FXP_REALPATH);
     83 	psbuf_put_4(pb, NEXTREQ(pctx));
     84 	psbuf_put_str(pb, pctx->mountpath);
     85 	while ((rv = psbuf_write(pctx, pb)) != 1)
     86 		if (rv == -1)
     87 			err(1, "realpath query");
     88 
     89 	psbuf_recycle(pb, PSB_IN);
     90 
     91 	while ((rv = psbuf_read(pctx, pb)) != 1)
     92 		if (rv == -1)
     93 			err(1, "read realpath query response");
     94 	if (pb->type != SSH_FXP_NAME)
     95 		errx(1, "invalid server realpath response for \"%s\"",
     96 		    pctx->mountpath);
     97 
     98 	if (!psbuf_get_4(pb, &count))
     99 		errx(1, "invalid realpath response: count");
    100 	if (!psbuf_get_str(pb, &rootpath, NULL))
    101 		errx(1, "invalid realpath response: rootpath");
    102 
    103 	/* stat the rootdir so that we know it's a dir */
    104 	psbuf_recycle(pb, PSB_OUT);
    105 	psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
    106 	while ((rv == psbuf_write(pctx, pb)) != 1)
    107 		if (rv == -1)
    108 			errx(1, "lstat");
    109 
    110 	psbuf_recycle(pb, PSB_IN);
    111 
    112 	while ((rv = psbuf_read(pctx, pb)) != 1)
    113 		if (rv == -1)
    114 			errx(1, "read lstat response");
    115 
    116 	rv = psbuf_expect_attrs(pb, &va);
    117 	if (rv)
    118 		errx(1, "couldn't stat rootpath");
    119 	psbuf_destroy(pb);
    120 
    121 	if (puffs_mode2vt(va.va_mode) != VDIR)
    122 		errx(1, "remote path (%s) not a directory", rootpath);
    123 
    124 	pctx->nextino = 2;
    125 
    126 	memset(root, 0, sizeof(struct psshfs_node));
    127 	pu->pu_pn_root = puffs_pn_new(pu, root);
    128 	puffs_setrootpath(pu, rootpath);
    129 	free(rootpath);
    130 
    131 	rva = &pu->pu_pn_root->pn_va;
    132 	puffs_setvattr(rva, &va);
    133 	rva->va_fileid = pctx->nextino++;
    134 	rva->va_nlink = 0156; /* XXX */
    135 
    136 	puffs_fsnop_statvfs(NULL, sbp, 0);
    137 	*rootcookie = pu->pu_pn_root;
    138 
    139 	return 0;
    140 }
    141 
    142 int
    143 psshfs_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    144 {
    145 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    146 	struct psshfs_ctx *pctx = pu->pu_privdata;
    147 
    148 	kill(pctx->sshpid, SIGTERM);
    149 	close(pctx->sshfd);
    150 	return 0;
    151 }
    152