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