xref: /openssh-portable/sftp-common.c (revision 816036f1)
1*816036f1Sdjm@openbsd.org /* $OpenBSD: sftp-common.c,v 1.32 2020/10/18 11:32:02 djm Exp $ */
233804263SDamien Miller /*
333804263SDamien Miller  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
433804263SDamien Miller  * Copyright (c) 2001 Damien Miller.  All rights reserved.
533804263SDamien Miller  *
633804263SDamien Miller  * Redistribution and use in source and binary forms, with or without
733804263SDamien Miller  * modification, are permitted provided that the following conditions
833804263SDamien Miller  * are met:
933804263SDamien Miller  * 1. Redistributions of source code must retain the above copyright
1033804263SDamien Miller  *    notice, this list of conditions and the following disclaimer.
1133804263SDamien Miller  * 2. Redistributions in binary form must reproduce the above copyright
1233804263SDamien Miller  *    notice, this list of conditions and the following disclaimer in the
1333804263SDamien Miller  *    documentation and/or other materials provided with the distribution.
1433804263SDamien Miller  *
1533804263SDamien Miller  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1633804263SDamien Miller  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1733804263SDamien Miller  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1833804263SDamien Miller  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1933804263SDamien Miller  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2033804263SDamien Miller  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2133804263SDamien Miller  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2233804263SDamien Miller  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2333804263SDamien Miller  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2433804263SDamien Miller  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2533804263SDamien Miller  */
2633804263SDamien Miller 
2733804263SDamien Miller #include "includes.h"
28f17883e6SDamien Miller 
29f17883e6SDamien Miller #include <sys/types.h>
30f17883e6SDamien Miller #include <sys/stat.h>
3133804263SDamien Miller 
32427a1d57SDamien Miller #include <grp.h>
339f2abc47SDamien Miller #include <pwd.h>
34a7a73ee3SDamien Miller #include <stdio.h>
350600c702SDamien Miller #include <stdlib.h>
36e3476ed0SDamien Miller #include <string.h>
375598b4f1SDamien Miller #include <time.h>
38d7834353SDamien Miller #include <stdarg.h>
395cb503dfSDarren Tucker #include <unistd.h>
40096630d0SDarren Tucker #ifdef HAVE_UTIL_H
412901e2daSDarren Tucker #include <util.h>
42096630d0SDarren Tucker #endif
43427a1d57SDamien Miller 
4433804263SDamien Miller #include "xmalloc.h"
457d845f4aSdjm@openbsd.org #include "ssherr.h"
467d845f4aSdjm@openbsd.org #include "sshbuf.h"
47d7834353SDamien Miller #include "log.h"
489136ec13Sderaadt@openbsd.org #include "misc.h"
4933804263SDamien Miller 
5033804263SDamien Miller #include "sftp.h"
5133804263SDamien Miller #include "sftp-common.h"
5233804263SDamien Miller 
534cc240daSBen Lindstrom /* Clear contents of attributes structure */
5433804263SDamien Miller void
attrib_clear(Attrib * a)5533804263SDamien Miller attrib_clear(Attrib *a)
5633804263SDamien Miller {
5733804263SDamien Miller 	a->flags = 0;
5833804263SDamien Miller 	a->size = 0;
5933804263SDamien Miller 	a->uid = 0;
6033804263SDamien Miller 	a->gid = 0;
6133804263SDamien Miller 	a->perm = 0;
6233804263SDamien Miller 	a->atime = 0;
6333804263SDamien Miller 	a->mtime = 0;
6433804263SDamien Miller }
6533804263SDamien Miller 
664cc240daSBen Lindstrom /* Convert from struct stat to filexfer attribs */
6733804263SDamien Miller void
stat_to_attrib(const struct stat * st,Attrib * a)68f58b58ceSDamien Miller stat_to_attrib(const struct stat *st, Attrib *a)
6933804263SDamien Miller {
7033804263SDamien Miller 	attrib_clear(a);
7133804263SDamien Miller 	a->flags = 0;
7233804263SDamien Miller 	a->flags |= SSH2_FILEXFER_ATTR_SIZE;
7333804263SDamien Miller 	a->size = st->st_size;
7433804263SDamien Miller 	a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
7533804263SDamien Miller 	a->uid = st->st_uid;
7633804263SDamien Miller 	a->gid = st->st_gid;
7733804263SDamien Miller 	a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
7833804263SDamien Miller 	a->perm = st->st_mode;
7933804263SDamien Miller 	a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
8033804263SDamien Miller 	a->atime = st->st_atime;
8133804263SDamien Miller 	a->mtime = st->st_mtime;
8233804263SDamien Miller }
8333804263SDamien Miller 
84e1a49817SDamien Miller /* Convert from filexfer attribs to struct stat */
85e1a49817SDamien Miller void
attrib_to_stat(const Attrib * a,struct stat * st)86f58b58ceSDamien Miller attrib_to_stat(const Attrib *a, struct stat *st)
87e1a49817SDamien Miller {
88e1a49817SDamien Miller 	memset(st, 0, sizeof(*st));
89e1a49817SDamien Miller 
90e1a49817SDamien Miller 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
91e1a49817SDamien Miller 		st->st_size = a->size;
92e1a49817SDamien Miller 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
93e1a49817SDamien Miller 		st->st_uid = a->uid;
94e1a49817SDamien Miller 		st->st_gid = a->gid;
95e1a49817SDamien Miller 	}
96e1a49817SDamien Miller 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
97e1a49817SDamien Miller 		st->st_mode = a->perm;
98e1a49817SDamien Miller 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
99e1a49817SDamien Miller 		st->st_atime = a->atime;
100e1a49817SDamien Miller 		st->st_mtime = a->mtime;
101e1a49817SDamien Miller 	}
102e1a49817SDamien Miller }
103e1a49817SDamien Miller 
1044cc240daSBen Lindstrom /* Decode attributes in buffer */
1057d845f4aSdjm@openbsd.org int
decode_attrib(struct sshbuf * b,Attrib * a)1067d845f4aSdjm@openbsd.org decode_attrib(struct sshbuf *b, Attrib *a)
10733804263SDamien Miller {
1087d845f4aSdjm@openbsd.org 	int r;
109b1f483f4SBen Lindstrom 
1107d845f4aSdjm@openbsd.org 	attrib_clear(a);
1117d845f4aSdjm@openbsd.org 	if ((r = sshbuf_get_u32(b, &a->flags)) != 0)
1127d845f4aSdjm@openbsd.org 		return r;
1137d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
1147d845f4aSdjm@openbsd.org 		if ((r = sshbuf_get_u64(b, &a->size)) != 0)
1157d845f4aSdjm@openbsd.org 			return r;
11633804263SDamien Miller 	}
1177d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
1187d845f4aSdjm@openbsd.org 		if ((r = sshbuf_get_u32(b, &a->uid)) != 0 ||
1197d845f4aSdjm@openbsd.org 		    (r = sshbuf_get_u32(b, &a->gid)) != 0)
1207d845f4aSdjm@openbsd.org 			return r;
1217d845f4aSdjm@openbsd.org 	}
1227d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1237d845f4aSdjm@openbsd.org 		if ((r = sshbuf_get_u32(b, &a->perm)) != 0)
1247d845f4aSdjm@openbsd.org 			return r;
1257d845f4aSdjm@openbsd.org 	}
1267d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1277d845f4aSdjm@openbsd.org 		if ((r = sshbuf_get_u32(b, &a->atime)) != 0 ||
1287d845f4aSdjm@openbsd.org 		    (r = sshbuf_get_u32(b, &a->mtime)) != 0)
1297d845f4aSdjm@openbsd.org 			return r;
13033804263SDamien Miller 	}
13133804263SDamien Miller 	/* vendor-specific extensions */
1327d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) {
1337d845f4aSdjm@openbsd.org 		char *type;
1347d845f4aSdjm@openbsd.org 		u_char *data;
1357d845f4aSdjm@openbsd.org 		size_t dlen;
1367d845f4aSdjm@openbsd.org 		u_int i, count;
137b1f483f4SBen Lindstrom 
1387d845f4aSdjm@openbsd.org 		if ((r = sshbuf_get_u32(b, &count)) != 0)
139*816036f1Sdjm@openbsd.org 			return r;
14033804263SDamien Miller 		for (i = 0; i < count; i++) {
1417d845f4aSdjm@openbsd.org 			if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
1427d845f4aSdjm@openbsd.org 			    (r = sshbuf_get_string(b, &data, &dlen)) != 0)
1437d845f4aSdjm@openbsd.org 				return r;
1447d845f4aSdjm@openbsd.org 			debug3("Got file attribute \"%.100s\" len %zu",
1457d845f4aSdjm@openbsd.org 			    type, dlen);
146a627d42eSDarren Tucker 			free(type);
147a627d42eSDarren Tucker 			free(data);
14833804263SDamien Miller 		}
14933804263SDamien Miller 	}
1507d845f4aSdjm@openbsd.org 	return 0;
15133804263SDamien Miller }
15233804263SDamien Miller 
1534cc240daSBen Lindstrom /* Encode attributes to buffer */
1547d845f4aSdjm@openbsd.org int
encode_attrib(struct sshbuf * b,const Attrib * a)1557d845f4aSdjm@openbsd.org encode_attrib(struct sshbuf *b, const Attrib *a)
15633804263SDamien Miller {
1577d845f4aSdjm@openbsd.org 	int r;
1587d845f4aSdjm@openbsd.org 
1597d845f4aSdjm@openbsd.org 	if ((r = sshbuf_put_u32(b, a->flags)) != 0)
1607d845f4aSdjm@openbsd.org 		return r;
1617d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
1627d845f4aSdjm@openbsd.org 		if ((r = sshbuf_put_u64(b, a->size)) != 0)
1637d845f4aSdjm@openbsd.org 			return r;
1647d845f4aSdjm@openbsd.org 	}
16533804263SDamien Miller 	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
1667d845f4aSdjm@openbsd.org 		if ((r = sshbuf_put_u32(b, a->uid)) != 0 ||
1677d845f4aSdjm@openbsd.org 		    (r = sshbuf_put_u32(b, a->gid)) != 0)
1687d845f4aSdjm@openbsd.org 			return r;
16933804263SDamien Miller 	}
1707d845f4aSdjm@openbsd.org 	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1717d845f4aSdjm@openbsd.org 		if ((r = sshbuf_put_u32(b, a->perm)) != 0)
1727d845f4aSdjm@openbsd.org 			return r;
1737d845f4aSdjm@openbsd.org 	}
17433804263SDamien Miller 	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1757d845f4aSdjm@openbsd.org 		if ((r = sshbuf_put_u32(b, a->atime)) != 0 ||
1767d845f4aSdjm@openbsd.org 		    (r = sshbuf_put_u32(b, a->mtime)) != 0)
1777d845f4aSdjm@openbsd.org 			return r;
17833804263SDamien Miller 	}
1797d845f4aSdjm@openbsd.org 	return 0;
18033804263SDamien Miller }
18133804263SDamien Miller 
1824cc240daSBen Lindstrom /* Convert from SSH2_FX_ status to text error message */
18333804263SDamien Miller const char *
fx2txt(int status)18433804263SDamien Miller fx2txt(int status)
18533804263SDamien Miller {
18633804263SDamien Miller 	switch (status) {
18733804263SDamien Miller 	case SSH2_FX_OK:
188d7686fd1SDamien Miller 		return("No error");
18933804263SDamien Miller 	case SSH2_FX_EOF:
190d7686fd1SDamien Miller 		return("End of file");
19133804263SDamien Miller 	case SSH2_FX_NO_SUCH_FILE:
192d7686fd1SDamien Miller 		return("No such file or directory");
19333804263SDamien Miller 	case SSH2_FX_PERMISSION_DENIED:
194d7686fd1SDamien Miller 		return("Permission denied");
19533804263SDamien Miller 	case SSH2_FX_FAILURE:
19633804263SDamien Miller 		return("Failure");
19733804263SDamien Miller 	case SSH2_FX_BAD_MESSAGE:
19833804263SDamien Miller 		return("Bad message");
19933804263SDamien Miller 	case SSH2_FX_NO_CONNECTION:
20033804263SDamien Miller 		return("No connection");
20133804263SDamien Miller 	case SSH2_FX_CONNECTION_LOST:
20233804263SDamien Miller 		return("Connection lost");
20333804263SDamien Miller 	case SSH2_FX_OP_UNSUPPORTED:
20433804263SDamien Miller 		return("Operation unsupported");
20533804263SDamien Miller 	default:
20633804263SDamien Miller 		return("Unknown status");
207d05487d1SBen Lindstrom 	}
20833804263SDamien Miller 	/* NOTREACHED */
20933804263SDamien Miller }
210e1a49817SDamien Miller 
211e1a49817SDamien Miller /*
212e1a49817SDamien Miller  * drwxr-xr-x    5 markus   markus       1024 Jan 13 18:39 .ssh
213e1a49817SDamien Miller  */
214e1a49817SDamien Miller char *
ls_file(const char * name,const struct stat * st,int remote,int si_units)2152901e2daSDarren Tucker ls_file(const char *name, const struct stat *st, int remote, int si_units)
216e1a49817SDamien Miller {
217e1a49817SDamien Miller 	int ulen, glen, sz = 0;
218e1a49817SDamien Miller 	struct tm *ltime = localtime(&st->st_mtime);
219488c9325Smillert@openbsd.org 	const char *user, *group;
220072e172fSdjm@openbsd.org 	char buf[1024], lc[8], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
2212901e2daSDarren Tucker 	char sbuf[FMT_SCALED_STRSIZE];
222e00e413dSDamien Miller 	time_t now;
223e1a49817SDamien Miller 
224e1a49817SDamien Miller 	strmode(st->st_mode, mode);
225072e172fSdjm@openbsd.org 	if (remote) {
226e1a49817SDamien Miller 		snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
227e1a49817SDamien Miller 		user = ubuf;
228e1a49817SDamien Miller 		snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
229e1a49817SDamien Miller 		group = gbuf;
230072e172fSdjm@openbsd.org 		strlcpy(lc, "?", sizeof(lc));
231072e172fSdjm@openbsd.org 	} else {
232072e172fSdjm@openbsd.org 		user = user_from_uid(st->st_uid, 0);
233072e172fSdjm@openbsd.org 		group = group_from_gid(st->st_gid, 0);
234072e172fSdjm@openbsd.org 		snprintf(lc, sizeof(lc), "%u", (u_int)st->st_nlink);
235e1a49817SDamien Miller 	}
236e1a49817SDamien Miller 	if (ltime != NULL) {
237e00e413dSDamien Miller 		now = time(NULL);
238e00e413dSDamien Miller 		if (now - (365*24*60*60)/2 < st->st_mtime &&
239e00e413dSDamien Miller 		    now >= st->st_mtime)
240e1a49817SDamien Miller 			sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
241e1a49817SDamien Miller 		else
242e1a49817SDamien Miller 			sz = strftime(tbuf, sizeof tbuf, "%b %e  %Y", ltime);
243e1a49817SDamien Miller 	}
244e1a49817SDamien Miller 	if (sz == 0)
245e1a49817SDamien Miller 		tbuf[0] = '\0';
2469136ec13Sderaadt@openbsd.org 	ulen = MAXIMUM(strlen(user), 8);
2479136ec13Sderaadt@openbsd.org 	glen = MAXIMUM(strlen(group), 8);
2482901e2daSDarren Tucker 	if (si_units) {
2492901e2daSDarren Tucker 		fmt_scaled((long long)st->st_size, sbuf);
250072e172fSdjm@openbsd.org 		snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8s %s %s",
251072e172fSdjm@openbsd.org 		    mode, lc, ulen, user, glen, group,
2522901e2daSDarren Tucker 		    sbuf, tbuf, name);
2532901e2daSDarren Tucker 	} else {
254072e172fSdjm@openbsd.org 		snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8llu %s %s",
255072e172fSdjm@openbsd.org 		    mode, lc, ulen, user, glen, group,
2560851381bSBen Lindstrom 		    (unsigned long long)st->st_size, tbuf, name);
2572901e2daSDarren Tucker 	}
258e1a49817SDamien Miller 	return xstrdup(buf);
259e1a49817SDamien Miller }
260