fs.c revision 1.18 1 /* $NetBSD: fs.c,v 1.18 2009/02/23 18:43:46 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: fs.c,v 1.18 2009/02/23 18:43:46 pooka Exp $");
31 #endif /* !lint */
32
33 #include <err.h>
34 #include <errno.h>
35 #include <puffs.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "psshfs.h"
42 #include "sftp_proto.h"
43
44 #define DO_IO(fname, a1, a2, a3, a4, rv) \
45 do { \
46 puffs_framebuf_seekset(a2, 0); \
47 *(a4) = 0; \
48 rv = fname(a1, a2, a3, a4); \
49 if (rv || a4 == 0) { \
50 fprintf(stderr, "psshfs_handshake failed %d (%s) %d\n", \
51 rv, strerror(rv), *a4); \
52 return rv ? rv : EPROTO; \
53 } \
54 } while (/*CONSTCOND*/0)
55
56 #define reterr(str, rv) \
57 do { \
58 fprintf str; \
59 return rv; \
60 } while (/*CONSTCOND*/0)
61
62 /* openssh extensions */
63 static const struct extunit {
64 const char *ext;
65 const char *val;
66 int extflag;
67 } exttable[] = {
68 {
69 "posix-rename (at) openssh.com",
70 "1",
71 SFTP_EXT_POSIX_RENAME,
72 },{
73 "statvfs (at) openssh.com",
74 "2",
75 SFTP_EXT_STATVFS,
76 },{
77 "fstatvfs (at) openssh.com",
78 "2",
79 SFTP_EXT_FSTATVFS,
80 },{
81 NULL,
82 NULL,
83 0
84 }};
85
86 int
87 psshfs_handshake(struct puffs_usermount *pu)
88 {
89 struct psshfs_ctx *pctx = puffs_getspecific(pu);
90 struct puffs_framebuf *pb;
91 struct puffs_pathobj *po_root;
92 struct puffs_node *pn_root;
93 struct vattr va, *rva;
94 const struct extunit *extu;
95 char *rootpath;
96 char *ext, *val;
97 uint32_t count;
98 int rv, done;
99
100 pb = psbuf_makeout();
101 psbuf_put_1(pb, SSH_FXP_INIT);
102 psbuf_put_4(pb, SFTP_PROTOVERSION);
103 DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
104
105 puffs_framebuf_recycle(pb);
106 DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
107 if (psbuf_get_type(pb) != SSH_FXP_VERSION)
108 reterr((stderr, "invalid server response: %d",
109 psbuf_get_type(pb)), EPROTO);
110 pctx->protover = psbuf_get_reqid(pb);
111
112 /*
113 * Check out which extensions are available. Currently
114 * we are only interested in the openssh statvfs extension.
115 */
116 for (;;) {
117 if (psbuf_get_str(pb, &ext, NULL) != 0)
118 break;
119 if (psbuf_get_str(pb, &val, NULL) != 0)
120 break;
121
122 for (extu = exttable; extu->ext; extu++)
123 if (strcmp(ext, extu->ext) == 0
124 && strcmp(val, extu->val) == 0)
125 pctx->extensions |= extu->extflag;
126 }
127
128 /* scope out our rootpath */
129 psbuf_recycleout(pb);
130 psbuf_put_1(pb, SSH_FXP_REALPATH);
131 psbuf_put_4(pb, NEXTREQ(pctx));
132 psbuf_put_str(pb, pctx->mountpath);
133 DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
134
135 puffs_framebuf_recycle(pb);
136 DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
137 if (psbuf_get_type(pb) != SSH_FXP_NAME)
138 reterr((stderr, "invalid server realpath response for \"%s\"",
139 pctx->mountpath), EPROTO);
140 if (psbuf_get_4(pb, &count) == -1)
141 reterr((stderr, "invalid realpath response: count"), EPROTO);
142 if (psbuf_get_str(pb, &rootpath, NULL) == -1)
143 reterr((stderr, "invalid realpath response: rootpath"), EPROTO);
144
145 /* stat the rootdir so that we know it's a dir */
146 psbuf_recycleout(pb);
147 psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
148 DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
149
150 puffs_framebuf_recycle(pb);
151 DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
152
153 rv = psbuf_expect_attrs(pb, &va);
154 if (rv)
155 reterr((stderr, "couldn't stat rootpath"), rv);
156 puffs_framebuf_destroy(pb);
157
158 if (puffs_mode2vt(va.va_mode) != VDIR)
159 reterr((stderr, "remote path (%s) not a directory", rootpath),
160 ENOTDIR);
161
162 pn_root = puffs_getroot(pu);
163 rva = &pn_root->pn_va;
164 puffs_setvattr(rva, &va);
165
166 po_root = puffs_getrootpathobj(pu);
167 if (po_root == NULL)
168 err(1, "getrootpathobj");
169 po_root->po_path = rootpath;
170 po_root->po_len = strlen(rootpath);
171
172 return 0;
173 }
174
175 int
176 psshfs_fs_statvfs(struct puffs_usermount *pu, struct statvfs *sbp)
177 {
178 PSSHFSAUTOVAR(pu);
179 uint64_t tmpval;
180 uint8_t type;
181
182 memset(sbp, 0, sizeof(*sbp));
183 sbp->f_bsize = sbp->f_frsize = sbp->f_iosize = 512;
184
185 if ((pctx->extensions & SFTP_EXT_STATVFS) == 0)
186 goto out;
187
188 psbuf_req_str(pb, SSH_FXP_EXTENDED, reqid, "statvfs (at) openssh.com");
189 psbuf_put_str(pb, pctx->mountpath);
190 GETRESPONSE(pb);
191
192 type = psbuf_get_type(pb);
193 if (type != SSH_FXP_EXTENDED_REPLY) {
194 /* use the default */
195 goto out;
196 }
197
198 psbuf_get_8(pb, &tmpval);
199 sbp->f_bsize = tmpval;
200 psbuf_get_8(pb, &tmpval);
201 sbp->f_frsize = tmpval;
202 psbuf_get_8(pb, &sbp->f_blocks);
203 psbuf_get_8(pb, &sbp->f_bfree);
204 psbuf_get_8(pb, &sbp->f_bavail);
205 psbuf_get_8(pb, &sbp->f_files);
206 psbuf_get_8(pb, &sbp->f_ffree);
207 psbuf_get_8(pb, &sbp->f_favail);
208
209 psbuf_get_8(pb, &tmpval); /* fsid */
210 psbuf_get_8(pb, &tmpval); /* flag */
211 psbuf_get_8(pb, &tmpval);
212 sbp->f_namemax = tmpval;
213
214 out:
215 PSSHFSRETURN(rv);
216 }
217
218 int
219 psshfs_fs_unmount(struct puffs_usermount *pu, int flags)
220 {
221 struct psshfs_ctx *pctx = puffs_getspecific(pu);
222
223 kill(pctx->sshpid, SIGTERM);
224 close(pctx->sshfd);
225 return 0;
226 }
227
228 int
229 psshfs_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
230 void *fid, size_t *fidsize)
231 {
232 struct psshfs_ctx *pctx = puffs_getspecific(pu);
233 struct puffs_node *pn = cookie;
234 struct psshfs_node *psn = pn->pn_data;
235 struct psshfs_fid *pf = fid;
236
237 pf->mounttime = pctx->mounttime;
238 pf->node = pn;
239
240 psn->stat |= PSN_HASFH;
241
242 return 0;
243 }
244
245 int
246 psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
247 struct puffs_newinfo *pni)
248 {
249 struct psshfs_ctx *pctx = puffs_getspecific(pu);
250 struct psshfs_fid *pf = fid;
251 struct puffs_node *pn = pf->node;
252 struct psshfs_node *psn;
253 int rv;
254
255 if (pf->mounttime != pctx->mounttime)
256 return EINVAL;
257 if (pn == 0)
258 return EINVAL;
259 psn = pn->pn_data;
260 if ((psn->stat & PSN_HASFH) == 0)
261 return EINVAL;
262
263 /* update node attributes */
264 rv = getnodeattr(pu, pn);
265 if (rv)
266 return EINVAL;
267
268 puffs_newinfo_setcookie(pni, pn);
269 puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
270 puffs_newinfo_setsize(pni, pn->pn_va.va_size);
271
272 return 0;
273 }
274