1*c4291c19Spooka /* $NetBSD: psbuf.c,v 1.16 2010/01/07 21:05:50 pooka Exp $ */ 2c3ef8ea5Spooka 3c3ef8ea5Spooka /* 4dac06153Spooka * Copyright (c) 2006-2009 Antti Kantee. All Rights Reserved. 5c3ef8ea5Spooka * 6c3ef8ea5Spooka * Redistribution and use in source and binary forms, with or without 7c3ef8ea5Spooka * modification, are permitted provided that the following conditions 8c3ef8ea5Spooka * are met: 9c3ef8ea5Spooka * 1. Redistributions of source code must retain the above copyright 10c3ef8ea5Spooka * notice, this list of conditions and the following disclaimer. 11c3ef8ea5Spooka * 2. Redistributions in binary form must reproduce the above copyright 12c3ef8ea5Spooka * notice, this list of conditions and the following disclaimer in the 13c3ef8ea5Spooka * documentation and/or other materials provided with the distribution. 14c3ef8ea5Spooka * 15c3ef8ea5Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16c3ef8ea5Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17c3ef8ea5Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18c3ef8ea5Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19c3ef8ea5Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20c3ef8ea5Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21c3ef8ea5Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22c3ef8ea5Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23c3ef8ea5Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24c3ef8ea5Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25c3ef8ea5Spooka * SUCH DAMAGE. 26c3ef8ea5Spooka */ 27c3ef8ea5Spooka 28c3ef8ea5Spooka #include <sys/cdefs.h> 29c3ef8ea5Spooka #ifndef lint 30*c4291c19Spooka __RCSID("$NetBSD: psbuf.c,v 1.16 2010/01/07 21:05:50 pooka Exp $"); 31c3ef8ea5Spooka #endif /* !lint */ 32c3ef8ea5Spooka 33c3ef8ea5Spooka /* 34c3ef8ea5Spooka * buffering functions for network input/output. slightly different 35c3ef8ea5Spooka * from the average joe buffer routines, as is usually the case ... 36c3ef8ea5Spooka * these use efuns for now. 37c3ef8ea5Spooka */ 38c3ef8ea5Spooka 39c3ef8ea5Spooka #include <sys/types.h> 40c3ef8ea5Spooka #include <sys/time.h> 41c3ef8ea5Spooka #include <sys/vnode.h> 42c3ef8ea5Spooka 43c3ef8ea5Spooka #include <err.h> 44c3ef8ea5Spooka #include <errno.h> 45c3ef8ea5Spooka #include <stdlib.h> 46c3ef8ea5Spooka #include <util.h> 47c3ef8ea5Spooka #include <unistd.h> 48c3ef8ea5Spooka 49c3ef8ea5Spooka #include "psshfs.h" 50c3ef8ea5Spooka #include "sftp_proto.h" 51c3ef8ea5Spooka 520e7bdfc1Spooka #define FAILRV(x) do { int rv; if ((rv=x)) return (rv); } while (/*CONSTCOND*/0) 530e7bdfc1Spooka #define READSTATE_LENGTH(off) (off < 4) 54c3ef8ea5Spooka 550e7bdfc1Spooka #define SFTP_LENOFF 0 560e7bdfc1Spooka #define SFTP_TYPEOFF 4 570e7bdfc1Spooka #define SFTP_REQIDOFF 5 580e7bdfc1Spooka 590e7bdfc1Spooka #define CHECK(v) if (!(v)) abort() 600e7bdfc1Spooka 610e7bdfc1Spooka uint8_t 620e7bdfc1Spooka psbuf_get_type(struct puffs_framebuf *pb) 63c3ef8ea5Spooka { 640e7bdfc1Spooka uint8_t type; 650e7bdfc1Spooka 660e7bdfc1Spooka puffs_framebuf_getdata_atoff(pb, SFTP_TYPEOFF, &type, 1); 670e7bdfc1Spooka return type; 680e7bdfc1Spooka } 690e7bdfc1Spooka 700e7bdfc1Spooka uint32_t 710e7bdfc1Spooka psbuf_get_len(struct puffs_framebuf *pb) 720e7bdfc1Spooka { 730e7bdfc1Spooka uint32_t len; 740e7bdfc1Spooka 750e7bdfc1Spooka puffs_framebuf_getdata_atoff(pb, SFTP_LENOFF, &len, 4); 760e7bdfc1Spooka return be32toh(len); 770e7bdfc1Spooka } 780e7bdfc1Spooka 790e7bdfc1Spooka uint32_t 800e7bdfc1Spooka psbuf_get_reqid(struct puffs_framebuf *pb) 810e7bdfc1Spooka { 820e7bdfc1Spooka uint32_t req; 830e7bdfc1Spooka 840e7bdfc1Spooka puffs_framebuf_getdata_atoff(pb, SFTP_REQIDOFF, &req, 4); 850e7bdfc1Spooka return be32toh(req); 860e7bdfc1Spooka } 870e7bdfc1Spooka 880e7bdfc1Spooka #define CUROFF(pb) (puffs_framebuf_telloff(pb)) 890e7bdfc1Spooka int 900e7bdfc1Spooka psbuf_read(struct puffs_usermount *pu, struct puffs_framebuf *pb, 910e7bdfc1Spooka int fd, int *done) 920e7bdfc1Spooka { 930e7bdfc1Spooka void *win; 94c3ef8ea5Spooka ssize_t n; 950e7bdfc1Spooka size_t howmuch, winlen; 960e7bdfc1Spooka int lenstate; 97c3ef8ea5Spooka 980e7bdfc1Spooka the_next_level: 990e7bdfc1Spooka if ((lenstate = READSTATE_LENGTH(CUROFF(pb)))) 1000e7bdfc1Spooka howmuch = 4 - CUROFF(pb); 1010e7bdfc1Spooka else 1020e7bdfc1Spooka howmuch = psbuf_get_len(pb) - (CUROFF(pb) - 4); 103c3ef8ea5Spooka 1040e7bdfc1Spooka if (puffs_framebuf_reserve_space(pb, howmuch) == -1) 1050e7bdfc1Spooka return errno; 1060e7bdfc1Spooka 1070e7bdfc1Spooka while (howmuch) { 1080e7bdfc1Spooka winlen = howmuch; 1090e7bdfc1Spooka if (puffs_framebuf_getwindow(pb, CUROFF(pb), &win, &winlen)==-1) 1100e7bdfc1Spooka return errno; 111f423bfeeSpooka n = recv(fd, win, winlen, MSG_NOSIGNAL); 112c3ef8ea5Spooka switch (n) { 113c3ef8ea5Spooka case 0: 1140e7bdfc1Spooka return ECONNRESET; 115c3ef8ea5Spooka case -1: 116c3ef8ea5Spooka if (errno == EAGAIN) 117c3ef8ea5Spooka return 0; 1180e7bdfc1Spooka return errno; 119c3ef8ea5Spooka default: 1200e7bdfc1Spooka howmuch -= n; 1210e7bdfc1Spooka puffs_framebuf_seekset(pb, CUROFF(pb) + n); 1220e7bdfc1Spooka break; 1230e7bdfc1Spooka } 124c3ef8ea5Spooka } 125c3ef8ea5Spooka 1260e7bdfc1Spooka if (!lenstate) { 1270e7bdfc1Spooka /* XXX: initial exchange shorter.. but don't worry, be happy */ 1280e7bdfc1Spooka puffs_framebuf_seekset(pb, 9); 1290e7bdfc1Spooka *done = 1; 130c3ef8ea5Spooka return 0; 1310e7bdfc1Spooka } else 1320e7bdfc1Spooka goto the_next_level; 133c3ef8ea5Spooka } 134c3ef8ea5Spooka 135c3ef8ea5Spooka int 1360e7bdfc1Spooka psbuf_write(struct puffs_usermount *pu, struct puffs_framebuf *pb, 1370e7bdfc1Spooka int fd, int *done) 138c3ef8ea5Spooka { 1390e7bdfc1Spooka void *win; 140c3ef8ea5Spooka ssize_t n; 1410e7bdfc1Spooka size_t winlen, howmuch; 142c3ef8ea5Spooka 1430e7bdfc1Spooka /* finalize buffer.. could be elsewhere ... */ 1440e7bdfc1Spooka if (CUROFF(pb) == 0) { 145c3ef8ea5Spooka uint32_t len; 146c3ef8ea5Spooka 1470e7bdfc1Spooka len = htobe32(puffs_framebuf_tellsize(pb) - 4); 1480e7bdfc1Spooka puffs_framebuf_putdata_atoff(pb, 0, &len, 4); 149c3ef8ea5Spooka } 150c3ef8ea5Spooka 1510e7bdfc1Spooka howmuch = puffs_framebuf_tellsize(pb) - CUROFF(pb); 1520e7bdfc1Spooka while (howmuch) { 1530e7bdfc1Spooka winlen = howmuch; 1540e7bdfc1Spooka if (puffs_framebuf_getwindow(pb, CUROFF(pb), &win, &winlen)==-1) 1550e7bdfc1Spooka return errno; 156e3468dbcSpooka n = send(fd, win, winlen, MSG_NOSIGNAL); 1570e7bdfc1Spooka switch (n) { 1580e7bdfc1Spooka case 0: 1590e7bdfc1Spooka return ECONNRESET; 1600e7bdfc1Spooka case -1: 161c3ef8ea5Spooka if (errno == EAGAIN) 162c3ef8ea5Spooka return 0; 1630e7bdfc1Spooka return errno; 1640e7bdfc1Spooka default: 1650e7bdfc1Spooka howmuch -= n; 1660e7bdfc1Spooka puffs_framebuf_seekset(pb, CUROFF(pb) + n); 1670e7bdfc1Spooka break; 1680e7bdfc1Spooka } 169c3ef8ea5Spooka } 170c3ef8ea5Spooka 1710e7bdfc1Spooka *done = 1; 172c3ef8ea5Spooka return 0; 173c3ef8ea5Spooka } 1740e7bdfc1Spooka #undef CUROFF 175c3ef8ea5Spooka 1760e7bdfc1Spooka int 1770e7bdfc1Spooka psbuf_cmp(struct puffs_usermount *pu, 17808386a8cSpooka struct puffs_framebuf *cmp1, struct puffs_framebuf *cmp2, int *notresp) 179c3ef8ea5Spooka { 180c3ef8ea5Spooka 1817c537b89Spooka return psbuf_get_reqid(cmp1) != psbuf_get_reqid(cmp2); 1820e7bdfc1Spooka } 183c3ef8ea5Spooka 1840e7bdfc1Spooka struct puffs_framebuf * 1850e7bdfc1Spooka psbuf_makeout() 1860e7bdfc1Spooka { 1870e7bdfc1Spooka struct puffs_framebuf *pb; 188c3ef8ea5Spooka 1890e7bdfc1Spooka pb = puffs_framebuf_make(); 1900e7bdfc1Spooka puffs_framebuf_seekset(pb, 4); 191c3ef8ea5Spooka return pb; 192c3ef8ea5Spooka } 193c3ef8ea5Spooka 194c3ef8ea5Spooka void 1950e7bdfc1Spooka psbuf_recycleout(struct puffs_framebuf *pb) 196c3ef8ea5Spooka { 197c3ef8ea5Spooka 1980e7bdfc1Spooka puffs_framebuf_recycle(pb); 1990e7bdfc1Spooka puffs_framebuf_seekset(pb, 4); 200c3ef8ea5Spooka } 201c3ef8ea5Spooka 202c3ef8ea5Spooka void 2030e7bdfc1Spooka psbuf_put_1(struct puffs_framebuf *pb, uint8_t val) 204c3ef8ea5Spooka { 2050e7bdfc1Spooka int rv; 206c3ef8ea5Spooka 2070e7bdfc1Spooka rv = puffs_framebuf_putdata(pb, &val, 1); 2080e7bdfc1Spooka CHECK(rv == 0); 209c3ef8ea5Spooka } 210c3ef8ea5Spooka 2110e7bdfc1Spooka void 2120e7bdfc1Spooka psbuf_put_2(struct puffs_framebuf *pb, uint16_t val) 213c3ef8ea5Spooka { 2140e7bdfc1Spooka int rv; 215c3ef8ea5Spooka 2160e7bdfc1Spooka HTOBE16(val); 2170e7bdfc1Spooka rv = puffs_framebuf_putdata(pb, &val, 2); 2180e7bdfc1Spooka CHECK(rv == 0); 219c3ef8ea5Spooka } 220c3ef8ea5Spooka 2210e7bdfc1Spooka void 2220e7bdfc1Spooka psbuf_put_4(struct puffs_framebuf *pb, uint32_t val) 223c3ef8ea5Spooka { 2240e7bdfc1Spooka int rv; 225c3ef8ea5Spooka 2260e7bdfc1Spooka HTOBE32(val); 2270e7bdfc1Spooka rv = puffs_framebuf_putdata(pb, &val, 4); 2280e7bdfc1Spooka CHECK(rv == 0); 229c3ef8ea5Spooka } 230c3ef8ea5Spooka 2310e7bdfc1Spooka void 2320e7bdfc1Spooka psbuf_put_8(struct puffs_framebuf *pb, uint64_t val) 233c3ef8ea5Spooka { 2340e7bdfc1Spooka int rv; 235c3ef8ea5Spooka 2360e7bdfc1Spooka HTOBE64(val); 2370e7bdfc1Spooka rv = puffs_framebuf_putdata(pb, &val, 8); 2380e7bdfc1Spooka CHECK(rv == 0); 239c3ef8ea5Spooka } 240c3ef8ea5Spooka 2410e7bdfc1Spooka void 2420e7bdfc1Spooka psbuf_put_data(struct puffs_framebuf *pb, const void *data, uint32_t dlen) 243c3ef8ea5Spooka { 2440e7bdfc1Spooka int rv; 245c3ef8ea5Spooka 246c3ef8ea5Spooka psbuf_put_4(pb, dlen); 2470e7bdfc1Spooka rv = puffs_framebuf_putdata(pb, data, dlen); 2480e7bdfc1Spooka CHECK(rv == 0); 249c3ef8ea5Spooka } 250c3ef8ea5Spooka 2510e7bdfc1Spooka void 2520e7bdfc1Spooka psbuf_put_str(struct puffs_framebuf *pb, const char *str) 253c3ef8ea5Spooka { 254c3ef8ea5Spooka 2550e7bdfc1Spooka psbuf_put_data(pb, str, strlen(str)); 256c3ef8ea5Spooka } 257c3ef8ea5Spooka 2580e7bdfc1Spooka void 259*c4291c19Spooka psbuf_put_vattr(struct puffs_framebuf *pb, const struct vattr *va, 260*c4291c19Spooka const struct psshfs_ctx *pctx) 261c3ef8ea5Spooka { 262c3ef8ea5Spooka uint32_t flags; 263c3ef8ea5Spooka flags = 0; 264c3ef8ea5Spooka 2655866121cSpooka if (va->va_size != (uint64_t)PUFFS_VNOVAL) 266c3ef8ea5Spooka flags |= SSH_FILEXFER_ATTR_SIZE; 2675866121cSpooka if (va->va_uid != (uid_t)PUFFS_VNOVAL) 268c3ef8ea5Spooka flags |= SSH_FILEXFER_ATTR_UIDGID; 2695866121cSpooka if (va->va_mode != (mode_t)PUFFS_VNOVAL) 270c3ef8ea5Spooka flags |= SSH_FILEXFER_ATTR_PERMISSIONS; 2717f864d60Spooka 272c3ef8ea5Spooka if (va->va_atime.tv_sec != PUFFS_VNOVAL) 273c3ef8ea5Spooka flags |= SSH_FILEXFER_ATTR_ACCESSTIME; 274c3ef8ea5Spooka 275c3ef8ea5Spooka psbuf_put_4(pb, flags); 276c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_SIZE) 277c3ef8ea5Spooka psbuf_put_8(pb, va->va_size); 278c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_UIDGID) { 279*c4291c19Spooka uid_t theuid; 280*c4291c19Spooka gid_t thegid; 281*c4291c19Spooka 282*c4291c19Spooka theuid = va->va_uid; 283*c4291c19Spooka if (pctx->domangleuid && theuid == pctx->myuid) 284*c4291c19Spooka theuid = pctx->mangleuid; 285*c4291c19Spooka thegid = va->va_gid; 286*c4291c19Spooka if (pctx->domanglegid && thegid == pctx->mygid) 287*c4291c19Spooka thegid = pctx->manglegid; 288*c4291c19Spooka 289*c4291c19Spooka psbuf_put_4(pb, theuid); 290*c4291c19Spooka psbuf_put_4(pb, thegid); 291c3ef8ea5Spooka } 292c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_PERMISSIONS) 293c3ef8ea5Spooka psbuf_put_4(pb, va->va_mode); 2947f864d60Spooka 2957f864d60Spooka /* XXX: this is totally wrong for protocol v3, see OpenSSH */ 2967f864d60Spooka if (flags & SSH_FILEXFER_ATTR_ACCESSTIME) { 297c3ef8ea5Spooka psbuf_put_4(pb, va->va_atime.tv_sec); 298c3ef8ea5Spooka psbuf_put_4(pb, va->va_mtime.tv_sec); 2997f864d60Spooka } 300c3ef8ea5Spooka } 301c3ef8ea5Spooka 3020e7bdfc1Spooka #define ERETURN(rv) return ((rv) == -1 ? errno : 0) 303c3ef8ea5Spooka 304c3ef8ea5Spooka int 3050e7bdfc1Spooka psbuf_get_1(struct puffs_framebuf *pb, uint8_t *val) 306c3ef8ea5Spooka { 307c3ef8ea5Spooka 3080e7bdfc1Spooka ERETURN(puffs_framebuf_getdata(pb, val, 1)); 309c3ef8ea5Spooka } 310c3ef8ea5Spooka 311c3ef8ea5Spooka int 3120e7bdfc1Spooka psbuf_get_2(struct puffs_framebuf *pb, uint16_t *val) 313c3ef8ea5Spooka { 3140e7bdfc1Spooka int rv; 315c3ef8ea5Spooka 3160e7bdfc1Spooka rv = puffs_framebuf_getdata(pb, val, 2); 3170e7bdfc1Spooka BE16TOH(*val); 318c3ef8ea5Spooka 3190e7bdfc1Spooka ERETURN(rv); 320c3ef8ea5Spooka } 321c3ef8ea5Spooka 322c3ef8ea5Spooka int 3230e7bdfc1Spooka psbuf_get_4(struct puffs_framebuf *pb, uint32_t *val) 324c3ef8ea5Spooka { 3250e7bdfc1Spooka int rv; 326c3ef8ea5Spooka 3270e7bdfc1Spooka rv = puffs_framebuf_getdata(pb, val, 4); 3280e7bdfc1Spooka BE32TOH(*val); 329c3ef8ea5Spooka 3300e7bdfc1Spooka ERETURN(rv); 331c3ef8ea5Spooka } 332c3ef8ea5Spooka 333c3ef8ea5Spooka int 3340e7bdfc1Spooka psbuf_get_8(struct puffs_framebuf *pb, uint64_t *val) 335c3ef8ea5Spooka { 3360e7bdfc1Spooka int rv; 337c3ef8ea5Spooka 3380e7bdfc1Spooka rv = puffs_framebuf_getdata(pb, val, 8); 3390e7bdfc1Spooka BE64TOH(*val); 340c3ef8ea5Spooka 3410e7bdfc1Spooka ERETURN(rv); 342c3ef8ea5Spooka } 343c3ef8ea5Spooka 344c3ef8ea5Spooka int 3450e7bdfc1Spooka psbuf_get_str(struct puffs_framebuf *pb, char **strp, uint32_t *strlenp) 346c3ef8ea5Spooka { 347c3ef8ea5Spooka char *str; 348c3ef8ea5Spooka uint32_t len; 349c3ef8ea5Spooka 3500e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &len)); 351c3ef8ea5Spooka 352d34b7523Spooka if (puffs_framebuf_remaining(pb) < len) 3530e7bdfc1Spooka return EPROTO; 354c3ef8ea5Spooka 355c3ef8ea5Spooka str = emalloc(len+1); 3560e7bdfc1Spooka puffs_framebuf_getdata(pb, str, len); 357c3ef8ea5Spooka str[len] = '\0'; 358c3ef8ea5Spooka *strp = str; 359c3ef8ea5Spooka 360c3ef8ea5Spooka if (strlenp) 361c3ef8ea5Spooka *strlenp = len; 362c3ef8ea5Spooka 3630e7bdfc1Spooka return 0; 364c3ef8ea5Spooka } 365c3ef8ea5Spooka 366c3ef8ea5Spooka int 3670e7bdfc1Spooka psbuf_get_vattr(struct puffs_framebuf *pb, struct vattr *vap) 368c3ef8ea5Spooka { 369c3ef8ea5Spooka uint32_t flags; 370c3ef8ea5Spooka uint32_t val; 371c3ef8ea5Spooka 372c3ef8ea5Spooka puffs_vattr_null(vap); 373c3ef8ea5Spooka 3740e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &flags)); 375c3ef8ea5Spooka 376c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_SIZE) { 3770e7bdfc1Spooka FAILRV(psbuf_get_8(pb, &vap->va_size)); 378c3ef8ea5Spooka vap->va_bytes = vap->va_size; 379c3ef8ea5Spooka } 380c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_UIDGID) { 3810e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &vap->va_uid)); 3820e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &vap->va_gid)); 383c3ef8ea5Spooka } 384c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_PERMISSIONS) { 3850e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &vap->va_mode)); 386c3ef8ea5Spooka vap->va_type = puffs_mode2vt(vap->va_mode); 387c3ef8ea5Spooka } 388c3ef8ea5Spooka if (flags & SSH_FILEXFER_ATTR_ACCESSTIME) { 389c3ef8ea5Spooka /* 390c3ef8ea5Spooka * XXX: this is utterly wrong if we want to speak 391c3ef8ea5Spooka * protocol version 3, but it seems like the 392c3ef8ea5Spooka * "internet standard" for doing this 393c3ef8ea5Spooka */ 3940e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &val)); 395c3ef8ea5Spooka vap->va_atime.tv_sec = val; 3960e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &val)); 397c3ef8ea5Spooka vap->va_mtime.tv_sec = val; 398c3ef8ea5Spooka /* make ctime the same as mtime */ 399c3ef8ea5Spooka vap->va_ctime.tv_sec = val; 400c3ef8ea5Spooka 401c3ef8ea5Spooka vap->va_atime.tv_nsec = 0; 402c3ef8ea5Spooka vap->va_ctime.tv_nsec = 0; 403c3ef8ea5Spooka vap->va_mtime.tv_nsec = 0; 404c3ef8ea5Spooka } 405c3ef8ea5Spooka 4060e7bdfc1Spooka return 0; 407c3ef8ea5Spooka } 408c3ef8ea5Spooka 409c3ef8ea5Spooka /* 410c3ef8ea5Spooka * Buffer content helpers. Caller frees all data. 411c3ef8ea5Spooka */ 412c3ef8ea5Spooka 413c3ef8ea5Spooka /* 414c3ef8ea5Spooka * error mapping.. most are not expected for a file system, but 415c3ef8ea5Spooka * should help with diagnosing a possible error 416c3ef8ea5Spooka */ 417c3ef8ea5Spooka static int emap[] = { 418c3ef8ea5Spooka 0, /* OK */ 419c3ef8ea5Spooka 0, /* EOF */ 420c3ef8ea5Spooka ENOENT, /* NO_SUCH_FILE */ 421c3ef8ea5Spooka EPERM, /* PERMISSION_DENIED */ 422c3ef8ea5Spooka EIO, /* FAILURE */ 423c3ef8ea5Spooka EBADMSG, /* BAD_MESSAGE */ 424c3ef8ea5Spooka ENOTCONN, /* NO_CONNECTION */ 425c3ef8ea5Spooka ECONNRESET, /* CONNECTION_LOST */ 426c3ef8ea5Spooka EOPNOTSUPP, /* OP_UNSUPPORTED */ 427c3ef8ea5Spooka EINVAL, /* INVALID_HANDLE */ 428c3ef8ea5Spooka ENXIO, /* NO_SUCH_PATH */ 429c3ef8ea5Spooka EEXIST, /* FILE_ALREADY_EXISTS */ 430c3ef8ea5Spooka ENODEV /* WRITE_PROTECT */ 431c3ef8ea5Spooka }; 4325866121cSpooka #define NERRORS ((int)(sizeof(emap) / sizeof(emap[0]))) 433c3ef8ea5Spooka 434c3ef8ea5Spooka static int 435c3ef8ea5Spooka sftperr_to_errno(int error) 436c3ef8ea5Spooka { 437c3ef8ea5Spooka 438c3ef8ea5Spooka if (!error) 439c3ef8ea5Spooka return 0; 440c3ef8ea5Spooka 441c3ef8ea5Spooka if (error >= NERRORS || error < 0) 442c3ef8ea5Spooka return EPROTO; 443c3ef8ea5Spooka 444c3ef8ea5Spooka return emap[error]; 445c3ef8ea5Spooka } 446c3ef8ea5Spooka 447c3ef8ea5Spooka #define INVALRESPONSE EPROTO 448c3ef8ea5Spooka 449c3ef8ea5Spooka static int 4500e7bdfc1Spooka expectcode(struct puffs_framebuf *pb, int value) 451c3ef8ea5Spooka { 452c3ef8ea5Spooka uint32_t error; 4530e7bdfc1Spooka uint8_t type; 454c3ef8ea5Spooka 4550e7bdfc1Spooka type = psbuf_get_type(pb); 4560e7bdfc1Spooka if (type == value) 457c3ef8ea5Spooka return 0; 458c3ef8ea5Spooka 4590e7bdfc1Spooka if (type != SSH_FXP_STATUS) 460c3ef8ea5Spooka return INVALRESPONSE; 461c3ef8ea5Spooka 4620e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &error)); 463c3ef8ea5Spooka 464c3ef8ea5Spooka return sftperr_to_errno(error); 465c3ef8ea5Spooka } 466c3ef8ea5Spooka 467c3ef8ea5Spooka #define CHECKCODE(pb,val) \ 468c3ef8ea5Spooka do { \ 469c3ef8ea5Spooka int rv; \ 470c3ef8ea5Spooka rv = expectcode(pb, val); \ 471c3ef8ea5Spooka if (rv) \ 472c3ef8ea5Spooka return rv; \ 473c3ef8ea5Spooka } while (/*CONSTCOND*/0) 474c3ef8ea5Spooka 475c3ef8ea5Spooka int 4760e7bdfc1Spooka psbuf_expect_status(struct puffs_framebuf *pb) 477c3ef8ea5Spooka { 478c3ef8ea5Spooka uint32_t error; 479c3ef8ea5Spooka 4800e7bdfc1Spooka if (psbuf_get_type(pb) != SSH_FXP_STATUS) 481c3ef8ea5Spooka return INVALRESPONSE; 482c3ef8ea5Spooka 4830e7bdfc1Spooka FAILRV(psbuf_get_4(pb, &error)); 484c3ef8ea5Spooka 485c3ef8ea5Spooka return sftperr_to_errno(error); 486c3ef8ea5Spooka } 487c3ef8ea5Spooka 488c3ef8ea5Spooka int 4890e7bdfc1Spooka psbuf_expect_handle(struct puffs_framebuf *pb, char **hand, uint32_t *handlen) 490c3ef8ea5Spooka { 491c3ef8ea5Spooka 492c3ef8ea5Spooka CHECKCODE(pb, SSH_FXP_HANDLE); 4930e7bdfc1Spooka FAILRV(psbuf_get_str(pb, hand, handlen)); 494c3ef8ea5Spooka 495c3ef8ea5Spooka return 0; 496c3ef8ea5Spooka } 497c3ef8ea5Spooka 498c3ef8ea5Spooka /* no memory allocation, direct copy */ 499c3ef8ea5Spooka int 5000e7bdfc1Spooka psbuf_do_data(struct puffs_framebuf *pb, uint8_t *data, uint32_t *dlen) 501c3ef8ea5Spooka { 5020e7bdfc1Spooka void *win; 503c495e43bSpooka size_t bufoff, winlen; 504c495e43bSpooka uint32_t len, dataoff; 505c3ef8ea5Spooka 5060e7bdfc1Spooka if (psbuf_get_type(pb) != SSH_FXP_DATA) { 507c3ef8ea5Spooka uint32_t val; 508c3ef8ea5Spooka 5090e7bdfc1Spooka if (psbuf_get_type(pb) != SSH_FXP_STATUS) 510c3ef8ea5Spooka return INVALRESPONSE; 511c3ef8ea5Spooka 5124a403dd9Spooka if (psbuf_get_4(pb, &val) != 0) 513c3ef8ea5Spooka return INVALRESPONSE; 514c3ef8ea5Spooka 515c3ef8ea5Spooka if (val != SSH_FX_EOF) 516c3ef8ea5Spooka return sftperr_to_errno(val); 517c3ef8ea5Spooka 518c3ef8ea5Spooka *dlen = 0; 519c3ef8ea5Spooka return 0; 520c3ef8ea5Spooka } 5214a403dd9Spooka if (psbuf_get_4(pb, &len) != 0) 522c3ef8ea5Spooka return INVALRESPONSE; 523c3ef8ea5Spooka 524c3ef8ea5Spooka if (*dlen < len) 525c3ef8ea5Spooka return EINVAL; 526c3ef8ea5Spooka 5270e7bdfc1Spooka *dlen = 0; 528c3ef8ea5Spooka 5290e7bdfc1Spooka dataoff = 0; 5300e7bdfc1Spooka while (dataoff < len) { 5310e7bdfc1Spooka winlen = len-dataoff; 5320e7bdfc1Spooka bufoff = puffs_framebuf_telloff(pb); 5330e7bdfc1Spooka if (puffs_framebuf_getwindow(pb, bufoff, 5340e7bdfc1Spooka &win, &winlen) == -1) 5350e7bdfc1Spooka return EINVAL; 5360e7bdfc1Spooka if (winlen == 0) 5370e7bdfc1Spooka break; 538c3ef8ea5Spooka 5390e7bdfc1Spooka memcpy(data + dataoff, win, winlen); 5400e7bdfc1Spooka dataoff += winlen; 5410e7bdfc1Spooka } 5420e7bdfc1Spooka 5430e7bdfc1Spooka *dlen = dataoff; 544c3ef8ea5Spooka 545c3ef8ea5Spooka return 0; 546c3ef8ea5Spooka } 547c3ef8ea5Spooka 548c3ef8ea5Spooka int 5490e7bdfc1Spooka psbuf_expect_name(struct puffs_framebuf *pb, uint32_t *count) 550c3ef8ea5Spooka { 551c3ef8ea5Spooka 552c3ef8ea5Spooka CHECKCODE(pb, SSH_FXP_NAME); 5530e7bdfc1Spooka FAILRV(psbuf_get_4(pb, count)); 554c3ef8ea5Spooka 555c3ef8ea5Spooka return 0; 556c3ef8ea5Spooka } 557c3ef8ea5Spooka 558c3ef8ea5Spooka int 5590e7bdfc1Spooka psbuf_expect_attrs(struct puffs_framebuf *pb, struct vattr *vap) 560c3ef8ea5Spooka { 561c3ef8ea5Spooka 562c3ef8ea5Spooka CHECKCODE(pb, SSH_FXP_ATTRS); 5630e7bdfc1Spooka FAILRV(psbuf_get_vattr(pb, vap)); 564c3ef8ea5Spooka 565c3ef8ea5Spooka return 0; 566c3ef8ea5Spooka } 567c3ef8ea5Spooka 568c3ef8ea5Spooka /* 569c3ef8ea5Spooka * More helpers: larger-scale put functions 570c3ef8ea5Spooka */ 571c3ef8ea5Spooka 5720e7bdfc1Spooka void 5730e7bdfc1Spooka psbuf_req_data(struct puffs_framebuf *pb, int type, uint32_t reqid, 5740e7bdfc1Spooka const void *data, uint32_t dlen) 575c3ef8ea5Spooka { 576c3ef8ea5Spooka 577c3ef8ea5Spooka psbuf_put_1(pb, type); 578c3ef8ea5Spooka psbuf_put_4(pb, reqid); 579c3ef8ea5Spooka psbuf_put_data(pb, data, dlen); 580c3ef8ea5Spooka } 581c3ef8ea5Spooka 5820e7bdfc1Spooka void 5830e7bdfc1Spooka psbuf_req_str(struct puffs_framebuf *pb, int type, uint32_t reqid, 5840e7bdfc1Spooka const char *str) 585c3ef8ea5Spooka { 586c3ef8ea5Spooka 5870e7bdfc1Spooka psbuf_req_data(pb, type, reqid, str, strlen(str)); 588c3ef8ea5Spooka } 589