fs.c revision 1.3 1 1.3 pooka /* $NetBSD: fs.c,v 1.3 2007/01/15 00:42:21 pooka 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 * 3. The name of the company nor the name of the author may be used to
15 1.1 pooka * endorse or promote products derived from this software without specific
16 1.1 pooka * prior written permission.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <sys/cdefs.h>
32 1.1 pooka #ifndef lint
33 1.3 pooka __RCSID("$NetBSD: fs.c,v 1.3 2007/01/15 00:42:21 pooka Exp $");
34 1.1 pooka #endif /* !lint */
35 1.1 pooka
36 1.1 pooka #include <err.h>
37 1.1 pooka #include <puffs.h>
38 1.1 pooka #include <signal.h>
39 1.1 pooka #include <stdio.h>
40 1.1 pooka #include <stdlib.h>
41 1.1 pooka #include <unistd.h>
42 1.1 pooka
43 1.1 pooka #include "psshfs.h"
44 1.1 pooka #include "sftp_proto.h"
45 1.1 pooka
46 1.1 pooka int
47 1.2 pooka psshfs_domount(struct puffs_usermount *pu)
48 1.1 pooka {
49 1.2 pooka struct statvfs sb;
50 1.1 pooka struct psshfs_ctx *pctx = pu->pu_privdata;
51 1.1 pooka struct psshfs_node *root = &pctx->psn_root;
52 1.3 pooka struct puffs_pathobj *po_root;
53 1.1 pooka struct vattr va;
54 1.1 pooka struct vattr *rva;
55 1.1 pooka struct psbuf *pb;
56 1.1 pooka char *rootpath;
57 1.1 pooka uint32_t count;
58 1.1 pooka int rv;
59 1.1 pooka
60 1.1 pooka pb = psbuf_make(PSB_OUT);
61 1.1 pooka psbuf_put_1(pb, SSH_FXP_INIT);
62 1.1 pooka psbuf_put_4(pb, SFTP_PROTOVERSION);
63 1.1 pooka
64 1.1 pooka while ((rv = psbuf_write(pctx, pb)) != 1)
65 1.1 pooka if (rv == -1)
66 1.1 pooka err(1, "write handshake");
67 1.1 pooka
68 1.1 pooka psbuf_destroy(pb);
69 1.1 pooka pb = psbuf_make(PSB_IN);
70 1.1 pooka
71 1.1 pooka while ((rv = psbuf_read(pctx, pb)) != 1)
72 1.1 pooka if (rv == -1)
73 1.1 pooka err(1, "read handshake response");
74 1.1 pooka
75 1.1 pooka if (pb->type != SSH_FXP_VERSION)
76 1.1 pooka errx(1, "invalid server response");
77 1.1 pooka pctx->protover = pb->reqid;
78 1.1 pooka
79 1.1 pooka /* might contain some other stuff, but we're not interested */
80 1.1 pooka
81 1.1 pooka /* scope out our rootpath */
82 1.1 pooka psbuf_recycle(pb, PSB_OUT);
83 1.1 pooka psbuf_put_1(pb, SSH_FXP_REALPATH);
84 1.1 pooka psbuf_put_4(pb, NEXTREQ(pctx));
85 1.1 pooka psbuf_put_str(pb, pctx->mountpath);
86 1.1 pooka while ((rv = psbuf_write(pctx, pb)) != 1)
87 1.1 pooka if (rv == -1)
88 1.1 pooka err(1, "realpath query");
89 1.1 pooka
90 1.1 pooka psbuf_recycle(pb, PSB_IN);
91 1.1 pooka
92 1.1 pooka while ((rv = psbuf_read(pctx, pb)) != 1)
93 1.1 pooka if (rv == -1)
94 1.1 pooka err(1, "read realpath query response");
95 1.1 pooka if (pb->type != SSH_FXP_NAME)
96 1.1 pooka errx(1, "invalid server realpath response for \"%s\"",
97 1.1 pooka pctx->mountpath);
98 1.1 pooka
99 1.1 pooka if (!psbuf_get_4(pb, &count))
100 1.1 pooka errx(1, "invalid realpath response: count");
101 1.1 pooka if (!psbuf_get_str(pb, &rootpath, NULL))
102 1.1 pooka errx(1, "invalid realpath response: rootpath");
103 1.1 pooka
104 1.1 pooka /* stat the rootdir so that we know it's a dir */
105 1.1 pooka psbuf_recycle(pb, PSB_OUT);
106 1.1 pooka psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
107 1.1 pooka while ((rv == psbuf_write(pctx, pb)) != 1)
108 1.1 pooka if (rv == -1)
109 1.1 pooka errx(1, "lstat");
110 1.1 pooka
111 1.1 pooka psbuf_recycle(pb, PSB_IN);
112 1.1 pooka
113 1.1 pooka while ((rv = psbuf_read(pctx, pb)) != 1)
114 1.1 pooka if (rv == -1)
115 1.1 pooka errx(1, "read lstat response");
116 1.1 pooka
117 1.1 pooka rv = psbuf_expect_attrs(pb, &va);
118 1.1 pooka if (rv)
119 1.1 pooka errx(1, "couldn't stat rootpath");
120 1.1 pooka psbuf_destroy(pb);
121 1.1 pooka
122 1.1 pooka if (puffs_mode2vt(va.va_mode) != VDIR)
123 1.1 pooka errx(1, "remote path (%s) not a directory", rootpath);
124 1.1 pooka
125 1.1 pooka pctx->nextino = 2;
126 1.1 pooka
127 1.1 pooka memset(root, 0, sizeof(struct psshfs_node));
128 1.1 pooka pu->pu_pn_root = puffs_pn_new(pu, root);
129 1.3 pooka
130 1.3 pooka po_root = puffs_getrootpathobj(pu);
131 1.3 pooka if (po_root == NULL)
132 1.3 pooka err(1, "getrootpathobj");
133 1.3 pooka po_root->po_path = rootpath;
134 1.3 pooka po_root->po_len = strlen(rootpath);
135 1.1 pooka
136 1.1 pooka rva = &pu->pu_pn_root->pn_va;
137 1.1 pooka puffs_setvattr(rva, &va);
138 1.1 pooka rva->va_fileid = pctx->nextino++;
139 1.1 pooka rva->va_nlink = 0156; /* XXX */
140 1.1 pooka
141 1.2 pooka puffs_zerostatvfs(&sb);
142 1.2 pooka if (puffs_start(pu, pu->pu_pn_root, &sb) != 0)
143 1.2 pooka return errno;
144 1.1 pooka
145 1.1 pooka return 0;
146 1.1 pooka }
147 1.1 pooka
148 1.1 pooka int
149 1.1 pooka psshfs_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
150 1.1 pooka {
151 1.1 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
152 1.1 pooka struct psshfs_ctx *pctx = pu->pu_privdata;
153 1.1 pooka
154 1.1 pooka kill(pctx->sshpid, SIGTERM);
155 1.1 pooka close(pctx->sshfd);
156 1.1 pooka return 0;
157 1.1 pooka }
158