1 /* $NetBSD: fs.c,v 1.19 2009/05/20 13:56:36 pooka Exp $ */
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
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
28 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: fs.c,v 1.19 2009/05/20 13:56:36 pooka Exp $");
42 #include "sftp_proto.h"
44 #define DO_IO(fname, a1, a2, a3, a4, rv) \
46 puffs_framebuf_seekset(a2, 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; \
54 } while (/*CONSTCOND*/0)
56 #define reterr(str, rv) \
60 } while (/*CONSTCOND*/0)
62 /* openssh extensions */
63 static const struct extunit
{
69 "posix-rename@openssh.com",
71 SFTP_EXT_POSIX_RENAME
,
73 "statvfs@openssh.com",
77 "fstatvfs@openssh.com",
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
;
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.
117 if (psbuf_get_str(pb
, &ext
, NULL
) != 0)
119 if (psbuf_get_str(pb
, &val
, NULL
) != 0)
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
);
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
),
162 pn_root
= puffs_getroot(pu
);
163 rva
= &pn_root
->pn_va
;
164 puffs_setvattr(rva
, &va
);
166 po_root
= puffs_getrootpathobj(pu
);
168 err(1, "getrootpathobj");
169 po_root
->po_path
= rootpath
;
170 po_root
->po_len
= strlen(rootpath
);
176 psshfs_fs_statvfs(struct puffs_usermount
*pu
, struct statvfs
*sbp
)
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)
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 */
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
;
219 psshfs_fs_unmount(struct puffs_usermount
*pu
, int flags
)
221 struct psshfs_ctx
*pctx
= puffs_getspecific(pu
);
223 kill(pctx
->sshpid
, SIGTERM
);
225 if (pctx
->numconnections
== 2) {
226 kill(pctx
->sshpid_data
, SIGTERM
);
227 close(pctx
->sshfd_data
);
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
;
245 psn
->stat
|= PSN_HASFH
;
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
;
260 if (pf
->mounttime
!= pctx
->mounttime
)
265 if ((psn
->stat
& PSN_HASFH
) == 0)
268 /* update node attributes */
269 rv
= getnodeattr(pu
, pn
);
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
);