1323134Sdes/* $OpenBSD: sftp-common.c,v 1.29 2016/09/12 01:22:38 deraadt Exp $ */ 276259Sgreen/* 376259Sgreen * Copyright (c) 2001 Markus Friedl. All rights reserved. 476259Sgreen * Copyright (c) 2001 Damien Miller. All rights reserved. 576259Sgreen * 676259Sgreen * Redistribution and use in source and binary forms, with or without 776259Sgreen * modification, are permitted provided that the following conditions 876259Sgreen * are met: 976259Sgreen * 1. Redistributions of source code must retain the above copyright 1076259Sgreen * notice, this list of conditions and the following disclaimer. 1176259Sgreen * 2. Redistributions in binary form must reproduce the above copyright 1276259Sgreen * notice, this list of conditions and the following disclaimer in the 1376259Sgreen * documentation and/or other materials provided with the distribution. 1476259Sgreen * 1576259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1676259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1776259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1876259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1976259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2076259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2176259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2276259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2376259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2476259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2576259Sgreen */ 2676259Sgreen 2776259Sgreen#include "includes.h" 28263691Sdes__RCSID("$FreeBSD: stable/11/crypto/openssh/sftp-common.c 323134 2017-09-02 21:58:42Z des $"); 2976259Sgreen 30162852Sdes#include <sys/types.h> 31162852Sdes#include <sys/stat.h> 32162852Sdes 33162852Sdes#include <grp.h> 34162852Sdes#include <pwd.h> 35162852Sdes#include <stdio.h> 36261320Sdes#include <stdlib.h> 37162852Sdes#include <string.h> 38162852Sdes#include <time.h> 39162852Sdes#include <stdarg.h> 40204917Sdes#ifdef HAVE_UTIL_H 41204917Sdes#include <util.h> 42204917Sdes#endif 43162852Sdes 44162852Sdes#include "xmalloc.h" 45294332Sdes#include "ssherr.h" 46294332Sdes#include "sshbuf.h" 4776259Sgreen#include "log.h" 48323134Sdes#include "misc.h" 4976259Sgreen 5076259Sgreen#include "sftp.h" 5176259Sgreen#include "sftp-common.h" 5276259Sgreen 5392555Sdes/* Clear contents of attributes structure */ 5476259Sgreenvoid 5576259Sgreenattrib_clear(Attrib *a) 5676259Sgreen{ 5776259Sgreen a->flags = 0; 5876259Sgreen a->size = 0; 5976259Sgreen a->uid = 0; 6076259Sgreen a->gid = 0; 6176259Sgreen a->perm = 0; 6276259Sgreen a->atime = 0; 6376259Sgreen a->mtime = 0; 6476259Sgreen} 6576259Sgreen 6692555Sdes/* Convert from struct stat to filexfer attribs */ 6776259Sgreenvoid 68126274Sdesstat_to_attrib(const struct stat *st, Attrib *a) 6976259Sgreen{ 7076259Sgreen attrib_clear(a); 7176259Sgreen a->flags = 0; 7276259Sgreen a->flags |= SSH2_FILEXFER_ATTR_SIZE; 7376259Sgreen a->size = st->st_size; 7476259Sgreen a->flags |= SSH2_FILEXFER_ATTR_UIDGID; 7576259Sgreen a->uid = st->st_uid; 7676259Sgreen a->gid = st->st_gid; 7776259Sgreen a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; 7876259Sgreen a->perm = st->st_mode; 7976259Sgreen a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME; 8076259Sgreen a->atime = st->st_atime; 8176259Sgreen a->mtime = st->st_mtime; 8276259Sgreen} 8376259Sgreen 84106121Sdes/* Convert from filexfer attribs to struct stat */ 85106121Sdesvoid 86126274Sdesattrib_to_stat(const Attrib *a, struct stat *st) 87106121Sdes{ 88106121Sdes memset(st, 0, sizeof(*st)); 89106121Sdes 90106121Sdes if (a->flags & SSH2_FILEXFER_ATTR_SIZE) 91106121Sdes st->st_size = a->size; 92106121Sdes if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 93106121Sdes st->st_uid = a->uid; 94106121Sdes st->st_gid = a->gid; 95106121Sdes } 96106121Sdes if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 97106121Sdes st->st_mode = a->perm; 98106121Sdes if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 99106121Sdes st->st_atime = a->atime; 100106121Sdes st->st_mtime = a->mtime; 101106121Sdes } 102106121Sdes} 103106121Sdes 10492555Sdes/* Decode attributes in buffer */ 105294332Sdesint 106294332Sdesdecode_attrib(struct sshbuf *b, Attrib *a) 10776259Sgreen{ 108294332Sdes int r; 10999060Sdes 110294332Sdes attrib_clear(a); 111294332Sdes if ((r = sshbuf_get_u32(b, &a->flags)) != 0) 112294332Sdes return r; 113294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { 114294332Sdes if ((r = sshbuf_get_u64(b, &a->size)) != 0) 115294332Sdes return r; 11676259Sgreen } 117294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 118294332Sdes if ((r = sshbuf_get_u32(b, &a->uid)) != 0 || 119294332Sdes (r = sshbuf_get_u32(b, &a->gid)) != 0) 120294332Sdes return r; 12176259Sgreen } 122294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { 123294332Sdes if ((r = sshbuf_get_u32(b, &a->perm)) != 0) 124294332Sdes return r; 125294332Sdes } 126294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 127294332Sdes if ((r = sshbuf_get_u32(b, &a->atime)) != 0 || 128294332Sdes (r = sshbuf_get_u32(b, &a->mtime)) != 0) 129294332Sdes return r; 130294332Sdes } 13176259Sgreen /* vendor-specific extensions */ 132294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) { 133294332Sdes char *type; 134294332Sdes u_char *data; 135294332Sdes size_t dlen; 136294332Sdes u_int i, count; 13799060Sdes 138294332Sdes if ((r = sshbuf_get_u32(b, &count)) != 0) 139294332Sdes fatal("%s: buffer error: %s", __func__, ssh_err(r)); 14076259Sgreen for (i = 0; i < count; i++) { 141294332Sdes if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 || 142294332Sdes (r = sshbuf_get_string(b, &data, &dlen)) != 0) 143294332Sdes return r; 144294332Sdes debug3("Got file attribute \"%.100s\" len %zu", 145294332Sdes type, dlen); 146255767Sdes free(type); 147255767Sdes free(data); 14876259Sgreen } 14976259Sgreen } 150294332Sdes return 0; 15176259Sgreen} 15276259Sgreen 15392555Sdes/* Encode attributes to buffer */ 154294332Sdesint 155294332Sdesencode_attrib(struct sshbuf *b, const Attrib *a) 15676259Sgreen{ 157294332Sdes int r; 158294332Sdes 159294332Sdes if ((r = sshbuf_put_u32(b, a->flags)) != 0) 160294332Sdes return r; 161294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { 162294332Sdes if ((r = sshbuf_put_u64(b, a->size)) != 0) 163294332Sdes return r; 164294332Sdes } 16576259Sgreen if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 166294332Sdes if ((r = sshbuf_put_u32(b, a->uid)) != 0 || 167294332Sdes (r = sshbuf_put_u32(b, a->gid)) != 0) 168294332Sdes return r; 16976259Sgreen } 170294332Sdes if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { 171294332Sdes if ((r = sshbuf_put_u32(b, a->perm)) != 0) 172294332Sdes return r; 173294332Sdes } 17476259Sgreen if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 175294332Sdes if ((r = sshbuf_put_u32(b, a->atime)) != 0 || 176294332Sdes (r = sshbuf_put_u32(b, a->mtime)) != 0) 177294332Sdes return r; 17876259Sgreen } 179294332Sdes return 0; 18076259Sgreen} 18176259Sgreen 18292555Sdes/* Convert from SSH2_FX_ status to text error message */ 18376259Sgreenconst char * 18476259Sgreenfx2txt(int status) 18576259Sgreen{ 18676259Sgreen switch (status) { 18776259Sgreen case SSH2_FX_OK: 18876259Sgreen return("No error"); 18976259Sgreen case SSH2_FX_EOF: 19076259Sgreen return("End of file"); 19176259Sgreen case SSH2_FX_NO_SUCH_FILE: 19276259Sgreen return("No such file or directory"); 19376259Sgreen case SSH2_FX_PERMISSION_DENIED: 19476259Sgreen return("Permission denied"); 19576259Sgreen case SSH2_FX_FAILURE: 19676259Sgreen return("Failure"); 19776259Sgreen case SSH2_FX_BAD_MESSAGE: 19876259Sgreen return("Bad message"); 19976259Sgreen case SSH2_FX_NO_CONNECTION: 20076259Sgreen return("No connection"); 20176259Sgreen case SSH2_FX_CONNECTION_LOST: 20276259Sgreen return("Connection lost"); 20376259Sgreen case SSH2_FX_OP_UNSUPPORTED: 20476259Sgreen return("Operation unsupported"); 20576259Sgreen default: 20676259Sgreen return("Unknown status"); 20792555Sdes } 20876259Sgreen /* NOTREACHED */ 20976259Sgreen} 210106121Sdes 211106121Sdes/* 212106121Sdes * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh 213106121Sdes */ 214106121Sdeschar * 215204917Sdesls_file(const char *name, const struct stat *st, int remote, int si_units) 216106121Sdes{ 217106121Sdes int ulen, glen, sz = 0; 218106121Sdes struct tm *ltime = localtime(&st->st_mtime); 219255767Sdes const char *user, *group; 220106121Sdes char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; 221204917Sdes char sbuf[FMT_SCALED_STRSIZE]; 222261320Sdes time_t now; 223106121Sdes 224106121Sdes strmode(st->st_mode, mode); 225204917Sdes if (!remote) { 226204917Sdes user = user_from_uid(st->st_uid, 0); 227106121Sdes } else { 228106121Sdes snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); 229106121Sdes user = ubuf; 230106121Sdes } 231204917Sdes if (!remote) { 232204917Sdes group = group_from_gid(st->st_gid, 0); 233106121Sdes } else { 234106121Sdes snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); 235106121Sdes group = gbuf; 236106121Sdes } 237106121Sdes if (ltime != NULL) { 238261320Sdes now = time(NULL); 239261320Sdes if (now - (365*24*60*60)/2 < st->st_mtime && 240261320Sdes now >= st->st_mtime) 241106121Sdes sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); 242106121Sdes else 243106121Sdes sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); 244106121Sdes } 245106121Sdes if (sz == 0) 246106121Sdes tbuf[0] = '\0'; 247323134Sdes ulen = MAXIMUM(strlen(user), 8); 248323134Sdes glen = MAXIMUM(strlen(group), 8); 249204917Sdes if (si_units) { 250204917Sdes fmt_scaled((long long)st->st_size, sbuf); 251204917Sdes snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, 252204917Sdes (u_int)st->st_nlink, ulen, user, glen, group, 253204917Sdes sbuf, tbuf, name); 254204917Sdes } else { 255204917Sdes snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, 256204917Sdes (u_int)st->st_nlink, ulen, user, glen, group, 257204917Sdes (unsigned long long)st->st_size, tbuf, name); 258204917Sdes } 259106121Sdes return xstrdup(buf); 260106121Sdes} 261