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