sftp-common.c revision 76259
1124882Srwatson/* 2124882Srwatson * Copyright (c) 2001 Markus Friedl. All rights reserved. 3124882Srwatson * Copyright (c) 2001 Damien Miller. All rights reserved. 4124882Srwatson * 5124882Srwatson * Redistribution and use in source and binary forms, with or without 6124882Srwatson * modification, are permitted provided that the following conditions 7124882Srwatson * are met: 8124882Srwatson * 1. Redistributions of source code must retain the above copyright 9124882Srwatson * notice, this list of conditions and the following disclaimer. 10124882Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11124882Srwatson * notice, this list of conditions and the following disclaimer in the 12124882Srwatson * documentation and/or other materials provided with the distribution. 13124882Srwatson * 14124882Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15124882Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16124882Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17124882Srwatson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18124882Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19124882Srwatson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20124882Srwatson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21124882Srwatson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22124882Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23124882Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24124882Srwatson */ 25124882Srwatson 26124882Srwatson#include "includes.h" 27124882SrwatsonRCSID("$OpenBSD: sftp-common.c,v 1.2 2001/02/06 23:50:10 markus Exp $"); 28124882Srwatson 29124882Srwatson#include "buffer.h" 30124882Srwatson#include "bufaux.h" 31124882Srwatson#include "getput.h" 32124882Srwatson#include "log.h" 33124882Srwatson#include "xmalloc.h" 34124882Srwatson 35124882Srwatson#include "sftp.h" 36124882Srwatson#include "sftp-common.h" 37124882Srwatson 38124882Srwatsonvoid 39210377Smdfattrib_clear(Attrib *a) 40210377Smdf{ 41210377Smdf a->flags = 0; 42210377Smdf a->size = 0; 43124882Srwatson a->uid = 0; 44124882Srwatson a->gid = 0; 45124882Srwatson a->perm = 0; 46124882Srwatson a->atime = 0; 47210377Smdf a->mtime = 0; 48210377Smdf} 49210377Smdf 50210377Smdfvoid 51210377Smdfstat_to_attrib(struct stat *st, Attrib *a) 52124882Srwatson{ 53124882Srwatson attrib_clear(a); 54124882Srwatson a->flags = 0; 55 a->flags |= SSH2_FILEXFER_ATTR_SIZE; 56 a->size = st->st_size; 57 a->flags |= SSH2_FILEXFER_ATTR_UIDGID; 58 a->uid = st->st_uid; 59 a->gid = st->st_gid; 60 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; 61 a->perm = st->st_mode; 62 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME; 63 a->atime = st->st_atime; 64 a->mtime = st->st_mtime; 65} 66 67Attrib * 68decode_attrib(Buffer *b) 69{ 70 static Attrib a; 71 attrib_clear(&a); 72 a.flags = buffer_get_int(b); 73 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) 74 a.size = buffer_get_int64(b); 75 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { 76 a.uid = buffer_get_int(b); 77 a.gid = buffer_get_int(b); 78 } 79 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 80 a.perm = buffer_get_int(b); 81 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 82 a.atime = buffer_get_int(b); 83 a.mtime = buffer_get_int(b); 84 } 85 /* vendor-specific extensions */ 86 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) { 87 char *type, *data; 88 int i, count; 89 count = buffer_get_int(b); 90 for (i = 0; i < count; i++) { 91 type = buffer_get_string(b, NULL); 92 data = buffer_get_string(b, NULL); 93 debug3("Got file attribute \"%s\"", type); 94 xfree(type); 95 xfree(data); 96 } 97 } 98 return &a; 99} 100 101void 102encode_attrib(Buffer *b, Attrib *a) 103{ 104 buffer_put_int(b, a->flags); 105 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) 106 buffer_put_int64(b, a->size); 107 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 108 buffer_put_int(b, a->uid); 109 buffer_put_int(b, a->gid); 110 } 111 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 112 buffer_put_int(b, a->perm); 113 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 114 buffer_put_int(b, a->atime); 115 buffer_put_int(b, a->mtime); 116 } 117} 118 119const char * 120fx2txt(int status) 121{ 122 switch (status) { 123 case SSH2_FX_OK: 124 return("No error"); 125 case SSH2_FX_EOF: 126 return("End of file"); 127 case SSH2_FX_NO_SUCH_FILE: 128 return("No such file or directory"); 129 case SSH2_FX_PERMISSION_DENIED: 130 return("Permission denied"); 131 case SSH2_FX_FAILURE: 132 return("Failure"); 133 case SSH2_FX_BAD_MESSAGE: 134 return("Bad message"); 135 case SSH2_FX_NO_CONNECTION: 136 return("No connection"); 137 case SSH2_FX_CONNECTION_LOST: 138 return("Connection lost"); 139 case SSH2_FX_OP_UNSUPPORTED: 140 return("Operation unsupported"); 141 default: 142 return("Unknown status"); 143 }; 144 /* NOTREACHED */ 145} 146 147