Home | History | Annotate | Line # | Download | only in dist
      1 /*	$NetBSD: sftp-common.c,v 1.15 2026/04/08 18:58:41 christos Exp $	*/
      2 /* $OpenBSD: sftp-common.c,v 1.36 2026/02/11 17:05:32 dtucker 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.15 2026/04/08 18:58:41 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 <stdarg.h>
     41 #include <unistd.h>
     42 #include <stdlib.h>
     43 #include <time.h>
     44 #include <unistd.h>
     45 #include <util.h>
     46 
     47 #include "xmalloc.h"
     48 #include "ssherr.h"
     49 #include "sshbuf.h"
     50 #include "log.h"
     51 #include "misc.h"
     52 
     53 #include "sftp.h"
     54 #include "sftp-common.h"
     55 #include "fmt_scaled.h"
     56 
     57 /* Clear contents of attributes structure */
     58 void
     59 attrib_clear(Attrib *a)
     60 {
     61 	a->flags = 0;
     62 	a->size = 0;
     63 	a->uid = 0;
     64 	a->gid = 0;
     65 	a->perm = 0;
     66 	a->atime = 0;
     67 	a->mtime = 0;
     68 }
     69 
     70 /* Convert from struct stat to filexfer attribs */
     71 void
     72 stat_to_attrib(const struct stat *st, Attrib *a)
     73 {
     74 	attrib_clear(a);
     75 	a->flags = 0;
     76 	a->flags |= SSH2_FILEXFER_ATTR_SIZE;
     77 	a->size = st->st_size;
     78 	a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
     79 	a->uid = st->st_uid;
     80 	a->gid = st->st_gid;
     81 	a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
     82 	a->perm = st->st_mode;
     83 	a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
     84 	a->atime = st->st_atime;
     85 	a->mtime = st->st_mtime;
     86 }
     87 
     88 /* Convert from filexfer attribs to struct stat */
     89 void
     90 attrib_to_stat(const Attrib *a, struct stat *st)
     91 {
     92 	memset(st, 0, sizeof(*st));
     93 
     94 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
     95 		st->st_size = a->size;
     96 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
     97 		st->st_uid = a->uid;
     98 		st->st_gid = a->gid;
     99 	}
    100 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
    101 		st->st_mode = a->perm;
    102 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
    103 		st->st_atime = a->atime;
    104 		st->st_mtime = a->mtime;
    105 	}
    106 }
    107 
    108 /* Decode attributes in buffer */
    109 int
    110 decode_attrib(struct sshbuf *b, Attrib *a)
    111 {
    112 	int r;
    113 
    114 	attrib_clear(a);
    115 	if ((r = sshbuf_get_u32(b, &a->flags)) != 0)
    116 		return r;
    117 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
    118 		if ((r = sshbuf_get_u64(b, &a->size)) != 0)
    119 			return r;
    120 	}
    121 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
    122 		if ((r = sshbuf_get_u32(b, &a->uid)) != 0 ||
    123 		    (r = sshbuf_get_u32(b, &a->gid)) != 0)
    124 			return r;
    125 	}
    126 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
    127 		if ((r = sshbuf_get_u32(b, &a->perm)) != 0)
    128 			return r;
    129 	}
    130 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
    131 		if ((r = sshbuf_get_u32(b, &a->atime)) != 0 ||
    132 		    (r = sshbuf_get_u32(b, &a->mtime)) != 0)
    133 			return r;
    134 	}
    135 	/* vendor-specific extensions */
    136 	if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) {
    137 		char *type;
    138 		u_char *data;
    139 		size_t dlen;
    140 		u_int i, count;
    141 
    142 		if ((r = sshbuf_get_u32(b, &count)) != 0)
    143 			return r;
    144 		if (count > 0x100000)
    145 			return SSH_ERR_INVALID_FORMAT;
    146 		for (i = 0; i < count; i++) {
    147 			if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
    148 			    (r = sshbuf_get_string(b, &data, &dlen)) != 0)
    149 				return r;
    150 			debug3("Got file attribute \"%.100s\" len %zu",
    151 			    type, dlen);
    152 			free(type);
    153 			free(data);
    154 		}
    155 	}
    156 	return 0;
    157 }
    158 
    159 /* Encode attributes to buffer */
    160 int
    161 encode_attrib(struct sshbuf *b, const Attrib *a)
    162 {
    163 	int r;
    164 
    165 	if ((r = sshbuf_put_u32(b, a->flags)) != 0)
    166 		return r;
    167 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
    168 		if ((r = sshbuf_put_u64(b, a->size)) != 0)
    169 			return r;
    170 	}
    171 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
    172 		if ((r = sshbuf_put_u32(b, a->uid)) != 0 ||
    173 		    (r = sshbuf_put_u32(b, a->gid)) != 0)
    174 			return r;
    175 	}
    176 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
    177 		if ((r = sshbuf_put_u32(b, a->perm)) != 0)
    178 			return r;
    179 	}
    180 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
    181 		if ((r = sshbuf_put_u32(b, a->atime)) != 0 ||
    182 		    (r = sshbuf_put_u32(b, a->mtime)) != 0)
    183 			return r;
    184 	}
    185 	return 0;
    186 }
    187 
    188 /* Convert from SSH2_FX_ status to text error message */
    189 const char *
    190 fx2txt(int status)
    191 {
    192 	switch (status) {
    193 	case SSH2_FX_OK:
    194 		return("No error");
    195 	case SSH2_FX_EOF:
    196 		return("End of file");
    197 	case SSH2_FX_NO_SUCH_FILE:
    198 		return("No such file or directory");
    199 	case SSH2_FX_PERMISSION_DENIED:
    200 		return("Permission denied");
    201 	case SSH2_FX_FAILURE:
    202 		return("Failure");
    203 	case SSH2_FX_BAD_MESSAGE:
    204 		return("Bad message");
    205 	case SSH2_FX_NO_CONNECTION:
    206 		return("No connection");
    207 	case SSH2_FX_CONNECTION_LOST:
    208 		return("Connection lost");
    209 	case SSH2_FX_OP_UNSUPPORTED:
    210 		return("Operation unsupported");
    211 	default:
    212 		return("Unknown status");
    213 	}
    214 	/* NOTREACHED */
    215 }
    216 
    217 /*
    218  * drwxr-xr-x    5 markus   markus       1024 Jan 13 18:39 .ssh
    219  */
    220 char *
    221 ls_file(const char *name, const struct stat *st, int remote, int si_units,
    222     const char *user, const char *group)
    223 {
    224 	int ulen, glen, sz = 0;
    225 	struct tm *ltime = localtime(&st->st_mtime);
    226 	char buf[1024], lc[8], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
    227 	char sbuf[FMT_SCALED_STRSIZE];
    228 	time_t now;
    229 
    230 	strmode(st->st_mode, mode);
    231 	if (remote) {
    232 		if (user == NULL) {
    233 			snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
    234 			user = ubuf;
    235 		}
    236 		if (group == NULL) {
    237 			snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
    238 			group = gbuf;
    239 		}
    240 		strlcpy(lc, "?", sizeof(lc));
    241 	} else {
    242 		user = user_from_uid(st->st_uid, 0);
    243 		group = group_from_gid(st->st_gid, 0);
    244 		snprintf(lc, sizeof(lc), "%u", (u_int)st->st_nlink);
    245 	}
    246 	if (ltime != NULL) {
    247 		now = time(NULL);
    248 		if (now - (365*24*60*60)/2 < st->st_mtime &&
    249 		    now >= st->st_mtime)
    250 			sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
    251 		else
    252 			sz = strftime(tbuf, sizeof tbuf, "%b %e  %Y", ltime);
    253 	}
    254 	if (sz == 0)
    255 		tbuf[0] = '\0';
    256 	ulen = MAXIMUM(strlen(user), 8);
    257 	glen = MAXIMUM(strlen(group), 8);
    258 	if (si_units) {
    259 		fmt_scaled((long long)st->st_size, sbuf);
    260 		snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8s %s %s",
    261 		    mode, lc, ulen, user, glen, group,
    262 		    sbuf, tbuf, name);
    263 	} else {
    264 		snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8llu %s %s",
    265 		    mode, lc, ulen, user, glen, group,
    266 		    (unsigned long long)st->st_size, tbuf, name);
    267 	}
    268 	return xstrdup(buf);
    269 }
    270