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