1 /* $NetBSD: fs.c,v 1.21 2010/02/03 17:02:52 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2006-2009 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.21 2010/02/03 17:02:52 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@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, 83 0 84 }}; 85 86 int 87 psshfs_handshake(struct puffs_usermount *pu, int fd) 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, fd, &done, rv); 104 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); 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, fd, &done, rv); 134 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); 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, fd, &done, rv); 149 150 puffs_framebuf_recycle(pb); 151 DO_IO(psbuf_read, pu, pb, fd, &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@openssh.com"); 189 psbuf_put_str(pb, pctx->mountpath); 190 GETRESPONSE(pb, pctx->sshfd); 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 sbp->f_bresvd = sbp->f_bfree - sbp->f_bavail; 215 sbp->f_fresvd = sbp->f_ffree - sbp->f_favail; 216 217 out: 218 PSSHFSRETURN(rv); 219 } 220 221 int 222 psshfs_fs_unmount(struct puffs_usermount *pu, int flags) 223 { 224 struct psshfs_ctx *pctx = puffs_getspecific(pu); 225 226 kill(pctx->sshpid, SIGTERM); 227 close(pctx->sshfd); 228 if (pctx->numconnections == 2) { 229 kill(pctx->sshpid_data, SIGTERM); 230 close(pctx->sshfd_data); 231 } 232 233 return 0; 234 } 235 236 int 237 psshfs_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie, 238 void *fid, size_t *fidsize) 239 { 240 struct psshfs_ctx *pctx = puffs_getspecific(pu); 241 struct puffs_node *pn = cookie; 242 struct psshfs_node *psn = pn->pn_data; 243 struct psshfs_fid *pf = fid; 244 245 pf->mounttime = pctx->mounttime; 246 pf->node = pn; 247 248 psn->stat |= PSN_HASFH; 249 250 return 0; 251 } 252 253 int 254 psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize, 255 struct puffs_newinfo *pni) 256 { 257 struct psshfs_ctx *pctx = puffs_getspecific(pu); 258 struct psshfs_fid *pf = fid; 259 struct puffs_node *pn = pf->node; 260 struct psshfs_node *psn; 261 int rv; 262 263 if (pf->mounttime != pctx->mounttime) 264 return EINVAL; 265 if (pn == 0) 266 return EINVAL; 267 psn = pn->pn_data; 268 if ((psn->stat & PSN_HASFH) == 0) 269 return EINVAL; 270 271 /* update node attributes */ 272 rv = getnodeattr(pu, pn); 273 if (rv) 274 return EINVAL; 275 276 puffs_newinfo_setcookie(pni, pn); 277 puffs_newinfo_setvtype(pni, pn->pn_va.va_type); 278 puffs_newinfo_setsize(pni, pn->pn_va.va_size); 279 280 return 0; 281 } 282