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