1 /* 2 * Copyright (c) 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001 Damien Miller. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 /* $OpenBSD: sftp-common.c,v 1.20 2006/08/03 03:34:42 deraadt Exp $ */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include "includes.h" 31 32 #include <sys/types.h> 33 #include <sys/stat.h> 34 #include <sys/param.h> 35 36 #include <grp.h> 37 #include <pwd.h> 38 #include <stdio.h> 39 #include <string.h> 40 #include <time.h> 41 #include <stdarg.h> 42 43 #include "xmalloc.h" 44 #include "buffer.h" 45 #include "bufaux.h" 46 #include "log.h" 47 48 #include "sftp.h" 49 #include "sftp-common.h" 50 51 /* Clear contents of attributes structure */ 52 void 53 attrib_clear(Attrib *a) 54 { 55 a->flags = 0; 56 a->size = 0; 57 a->uid = 0; 58 a->gid = 0; 59 a->perm = 0; 60 a->atime = 0; 61 a->mtime = 0; 62 } 63 64 /* Convert from struct stat to filexfer attribs */ 65 void 66 stat_to_attrib(const struct stat *st, Attrib *a) 67 { 68 attrib_clear(a); 69 a->flags = 0; 70 a->flags |= SSH2_FILEXFER_ATTR_SIZE; 71 a->size = st->st_size; 72 a->flags |= SSH2_FILEXFER_ATTR_UIDGID; 73 a->uid = st->st_uid; 74 a->gid = st->st_gid; 75 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; 76 a->perm = st->st_mode; 77 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME; 78 a->atime = st->st_atime; 79 a->mtime = st->st_mtime; 80 } 81 82 /* Convert from filexfer attribs to struct stat */ 83 void 84 attrib_to_stat(const Attrib *a, struct stat *st) 85 { 86 memset(st, 0, sizeof(*st)); 87 88 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) 89 st->st_size = a->size; 90 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 91 st->st_uid = a->uid; 92 st->st_gid = a->gid; 93 } 94 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 95 st->st_mode = a->perm; 96 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 97 st->st_atime = a->atime; 98 st->st_mtime = a->mtime; 99 } 100 } 101 102 /* Decode attributes in buffer */ 103 Attrib * 104 decode_attrib(Buffer *b) 105 { 106 static Attrib a; 107 108 attrib_clear(&a); 109 a.flags = buffer_get_int(b); 110 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) 111 a.size = buffer_get_int64(b); 112 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { 113 a.uid = buffer_get_int(b); 114 a.gid = buffer_get_int(b); 115 } 116 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 117 a.perm = buffer_get_int(b); 118 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 119 a.atime = buffer_get_int(b); 120 a.mtime = buffer_get_int(b); 121 } 122 /* vendor-specific extensions */ 123 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) { 124 char *type, *data; 125 int i, count; 126 127 count = buffer_get_int(b); 128 for (i = 0; i < count; i++) { 129 type = buffer_get_string(b, NULL); 130 data = buffer_get_string(b, NULL); 131 debug3("Got file attribute \"%s\"", type); 132 xfree(type); 133 xfree(data); 134 } 135 } 136 return &a; 137 } 138 139 /* Encode attributes to buffer */ 140 void 141 encode_attrib(Buffer *b, const Attrib *a) 142 { 143 buffer_put_int(b, a->flags); 144 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) 145 buffer_put_int64(b, a->size); 146 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 147 buffer_put_int(b, a->uid); 148 buffer_put_int(b, a->gid); 149 } 150 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 151 buffer_put_int(b, a->perm); 152 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 153 buffer_put_int(b, a->atime); 154 buffer_put_int(b, a->mtime); 155 } 156 } 157 158 /* Convert from SSH2_FX_ status to text error message */ 159 const char * 160 fx2txt(int status) 161 { 162 switch (status) { 163 case SSH2_FX_OK: 164 return(gettext("No error")); 165 case SSH2_FX_EOF: 166 return(gettext("End of file")); 167 case SSH2_FX_NO_SUCH_FILE: 168 return(gettext("No such file or directory")); 169 case SSH2_FX_PERMISSION_DENIED: 170 return(gettext("Permission denied")); 171 case SSH2_FX_FAILURE: 172 return(gettext("Failure")); 173 case SSH2_FX_BAD_MESSAGE: 174 return(gettext("Bad message")); 175 case SSH2_FX_NO_CONNECTION: 176 return(gettext("No connection")); 177 case SSH2_FX_CONNECTION_LOST: 178 return(gettext("Connection lost")); 179 case SSH2_FX_OP_UNSUPPORTED: 180 return(gettext("Operation unsupported")); 181 default: 182 return(gettext("Unknown status")); 183 } 184 /* NOTREACHED */ 185 } 186 187 /* 188 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh 189 */ 190 char * 191 ls_file(const char *name, const struct stat *st, int remote) 192 { 193 int ulen, glen, sz = 0; 194 struct passwd *pw; 195 struct group *gr; 196 struct tm *ltime = localtime(&st->st_mtime); 197 char *user, *group; 198 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; 199 200 strmode(st->st_mode, mode); 201 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) { 202 user = pw->pw_name; 203 } else { 204 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); 205 user = ubuf; 206 } 207 if (!remote && (gr = getgrgid(st->st_gid)) != NULL) { 208 group = gr->gr_name; 209 } else { 210 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); 211 group = gbuf; 212 } 213 if (ltime != NULL) { 214 if (time(NULL) - st->st_mtime < (365*24*60*60)/2) 215 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); 216 else 217 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); 218 } 219 if (sz == 0) 220 tbuf[0] = '\0'; 221 ulen = MAX(strlen(user), 8); 222 glen = MAX(strlen(group), 8); 223 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, 224 (u_int)st->st_nlink, ulen, user, glen, group, 225 (unsigned long long)st->st_size, tbuf, name); 226 return xstrdup(buf); 227 }