1/* $NetBSD: sftp-common.c,v 1.14 2023/10/25 20:19:57 christos Exp $ */ 2/* $OpenBSD: sftp-common.c,v 1.34 2023/03/31 04:00:37 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.14 2023/10/25 20:19:57 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 */ 57void 58attrib_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 */ 70void 71stat_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 */ 88void 89attrib_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 */ 108int 109decode_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 if (count > 0x100000) 144 return SSH_ERR_INVALID_FORMAT; 145 for (i = 0; i < count; i++) { 146 if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 || 147 (r = sshbuf_get_string(b, &data, &dlen)) != 0) 148 return r; 149 debug3("Got file attribute \"%.100s\" len %zu", 150 type, dlen); 151 free(type); 152 free(data); 153 } 154 } 155 return 0; 156} 157 158/* Encode attributes to buffer */ 159int 160encode_attrib(struct sshbuf *b, const Attrib *a) 161{ 162 int r; 163 164 if ((r = sshbuf_put_u32(b, a->flags)) != 0) 165 return r; 166 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { 167 if ((r = sshbuf_put_u64(b, a->size)) != 0) 168 return r; 169 } 170 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 171 if ((r = sshbuf_put_u32(b, a->uid)) != 0 || 172 (r = sshbuf_put_u32(b, a->gid)) != 0) 173 return r; 174 } 175 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { 176 if ((r = sshbuf_put_u32(b, a->perm)) != 0) 177 return r; 178 } 179 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 180 if ((r = sshbuf_put_u32(b, a->atime)) != 0 || 181 (r = sshbuf_put_u32(b, a->mtime)) != 0) 182 return r; 183 } 184 return 0; 185} 186 187/* Convert from SSH2_FX_ status to text error message */ 188const char * 189fx2txt(int status) 190{ 191 switch (status) { 192 case SSH2_FX_OK: 193 return("No error"); 194 case SSH2_FX_EOF: 195 return("End of file"); 196 case SSH2_FX_NO_SUCH_FILE: 197 return("No such file or directory"); 198 case SSH2_FX_PERMISSION_DENIED: 199 return("Permission denied"); 200 case SSH2_FX_FAILURE: 201 return("Failure"); 202 case SSH2_FX_BAD_MESSAGE: 203 return("Bad message"); 204 case SSH2_FX_NO_CONNECTION: 205 return("No connection"); 206 case SSH2_FX_CONNECTION_LOST: 207 return("Connection lost"); 208 case SSH2_FX_OP_UNSUPPORTED: 209 return("Operation unsupported"); 210 default: 211 return("Unknown status"); 212 } 213 /* NOTREACHED */ 214} 215 216/* 217 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh 218 */ 219char * 220ls_file(const char *name, const struct stat *st, int remote, int si_units, 221 const char *user, const char *group) 222{ 223 int ulen, glen, sz = 0; 224 struct tm *ltime = localtime(&st->st_mtime); 225 char buf[1024], lc[8], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; 226 char sbuf[FMT_SCALED_STRSIZE]; 227 time_t now; 228 229 strmode(st->st_mode, mode); 230 if (remote) { 231 if (user == NULL) { 232 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); 233 user = ubuf; 234 } 235 if (group == NULL) { 236 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); 237 group = gbuf; 238 } 239 strlcpy(lc, "?", sizeof(lc)); 240 } else { 241 user = user_from_uid(st->st_uid, 0); 242 group = group_from_gid(st->st_gid, 0); 243 snprintf(lc, sizeof(lc), "%u", (u_int)st->st_nlink); 244 } 245 if (ltime != NULL) { 246 now = time(NULL); 247 if (now - (365*24*60*60)/2 < st->st_mtime && 248 now >= st->st_mtime) 249 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); 250 else 251 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); 252 } 253 if (sz == 0) 254 tbuf[0] = '\0'; 255 ulen = MAXIMUM(strlen(user), 8); 256 glen = MAXIMUM(strlen(group), 8); 257 if (si_units) { 258 fmt_scaled((long long)st->st_size, sbuf); 259 snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8s %s %s", 260 mode, lc, ulen, user, glen, group, 261 sbuf, tbuf, name); 262 } else { 263 snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8llu %s %s", 264 mode, lc, ulen, user, glen, group, 265 (unsigned long long)st->st_size, tbuf, name); 266 } 267 return xstrdup(buf); 268} 269