Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / puffs / mount_psshfs / fs.c
blobcaf9b66fc811eb8451c7693d6e09b33bc6d4aa36
1 /* $NetBSD: fs.c,v 1.19 2009/05/20 13:56:36 pooka Exp $ */
3 /*
4 * Copyright (c) 2006-2009 Antti Kantee. All Rights Reserved.
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.
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.
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: fs.c,v 1.19 2009/05/20 13:56:36 pooka Exp $");
31 #endif /* !lint */
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>
41 #include "psshfs.h"
42 #include "sftp_proto.h"
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)
56 #define reterr(str, rv) \
57 do { \
58 fprintf str; \
59 return rv; \
60 } while (/*CONSTCOND*/0)
62 /* openssh extensions */
63 static const struct extunit {
64 const char *ext;
65 const char *val;
66 int extflag;
67 } exttable[] = {
69 "posix-rename@openssh.com",
70 "1",
71 SFTP_EXT_POSIX_RENAME,
72 },{
73 "statvfs@openssh.com",
74 "2",
75 SFTP_EXT_STATVFS,
76 },{
77 "fstatvfs@openssh.com",
78 "2",
79 SFTP_EXT_FSTATVFS,
80 },{
81 NULL,
82 NULL,
84 }};
86 int
87 psshfs_handshake(struct puffs_usermount *pu, int fd)
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;
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, fd, &done, rv);
105 puffs_framebuf_recycle(pb);
106 DO_IO(psbuf_read, pu, pb, fd, &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);
113 * Check out which extensions are available. Currently
114 * we are only interested in the openssh statvfs extension.
116 for (;;) {
117 if (psbuf_get_str(pb, &ext, NULL) != 0)
118 break;
119 if (psbuf_get_str(pb, &val, NULL) != 0)
120 break;
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;
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, fd, &done, rv);
135 puffs_framebuf_recycle(pb);
136 DO_IO(psbuf_read, pu, pb, fd, &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);
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, fd, &done, rv);
150 puffs_framebuf_recycle(pb);
151 DO_IO(psbuf_read, pu, pb, fd, &done, rv);
153 rv = psbuf_expect_attrs(pb, &va);
154 if (rv)
155 reterr((stderr, "couldn't stat rootpath"), rv);
156 puffs_framebuf_destroy(pb);
158 if (puffs_mode2vt(va.va_mode) != VDIR)
159 reterr((stderr, "remote path (%s) not a directory", rootpath),
160 ENOTDIR);
162 pn_root = puffs_getroot(pu);
163 rva = &pn_root->pn_va;
164 puffs_setvattr(rva, &va);
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);
172 return 0;
176 psshfs_fs_statvfs(struct puffs_usermount *pu, struct statvfs *sbp)
178 PSSHFSAUTOVAR(pu);
179 uint64_t tmpval;
180 uint8_t type;
182 memset(sbp, 0, sizeof(*sbp));
183 sbp->f_bsize = sbp->f_frsize = sbp->f_iosize = 512;
185 if ((pctx->extensions & SFTP_EXT_STATVFS) == 0)
186 goto out;
188 psbuf_req_str(pb, SSH_FXP_EXTENDED, reqid, "statvfs@openssh.com");
189 psbuf_put_str(pb, pctx->mountpath);
190 GETRESPONSE(pb, pctx->sshfd);
192 type = psbuf_get_type(pb);
193 if (type != SSH_FXP_EXTENDED_REPLY) {
194 /* use the default */
195 goto out;
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);
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;
214 out:
215 PSSHFSRETURN(rv);
219 psshfs_fs_unmount(struct puffs_usermount *pu, int flags)
221 struct psshfs_ctx *pctx = puffs_getspecific(pu);
223 kill(pctx->sshpid, SIGTERM);
224 close(pctx->sshfd);
225 if (pctx->numconnections == 2) {
226 kill(pctx->sshpid_data, SIGTERM);
227 close(pctx->sshfd_data);
230 return 0;
234 psshfs_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
235 void *fid, size_t *fidsize)
237 struct psshfs_ctx *pctx = puffs_getspecific(pu);
238 struct puffs_node *pn = cookie;
239 struct psshfs_node *psn = pn->pn_data;
240 struct psshfs_fid *pf = fid;
242 pf->mounttime = pctx->mounttime;
243 pf->node = pn;
245 psn->stat |= PSN_HASFH;
247 return 0;
251 psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
252 struct puffs_newinfo *pni)
254 struct psshfs_ctx *pctx = puffs_getspecific(pu);
255 struct psshfs_fid *pf = fid;
256 struct puffs_node *pn = pf->node;
257 struct psshfs_node *psn;
258 int rv;
260 if (pf->mounttime != pctx->mounttime)
261 return EINVAL;
262 if (pn == 0)
263 return EINVAL;
264 psn = pn->pn_data;
265 if ((psn->stat & PSN_HASFH) == 0)
266 return EINVAL;
268 /* update node attributes */
269 rv = getnodeattr(pu, pn);
270 if (rv)
271 return EINVAL;
273 puffs_newinfo_setcookie(pni, pn);
274 puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
275 puffs_newinfo_setsize(pni, pn->pn_va.va_size);
277 return 0;