blob: 677f27d63c534edd66da2d2bbea01f59563bb12c [file] [log] [blame]
millert@openbsd.org488c9322018-09-13 15:23:32 +00001/* $OpenBSD: sftp-common.c,v 1.31 2018/09/13 15:23:32 millert Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
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"
Damien Millerf17883e2006-03-15 11:45:54 +110028
29#include <sys/types.h>
30#include <sys/stat.h>
Damien Miller33804262001-02-04 23:20:18 +110031
Damien Miller427a1d52006-07-10 20:20:33 +100032#include <grp.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100033#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Miller0600c702013-11-21 13:55:43 +110035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100037#include <time.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <stdarg.h>
Darren Tucker5cb503d2019-01-24 09:55:16 +110039#include <unistd.h>
Darren Tucker096630d2010-01-13 23:00:38 +110040#ifdef HAVE_UTIL_H
Darren Tucker2901e2d2010-01-13 22:44:06 +110041#include <util.h>
Darren Tucker096630d2010-01-13 23:00:38 +110042#endif
Damien Miller427a1d52006-07-10 20:20:33 +100043
Damien Miller33804262001-02-04 23:20:18 +110044#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000045#include "ssherr.h"
46#include "sshbuf.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "log.h"
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +000048#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110049
50#include "sftp.h"
51#include "sftp-common.h"
52
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000053/* Clear contents of attributes structure */
Damien Miller33804262001-02-04 23:20:18 +110054void
55attrib_clear(Attrib *a)
56{
57 a->flags = 0;
58 a->size = 0;
59 a->uid = 0;
60 a->gid = 0;
61 a->perm = 0;
62 a->atime = 0;
63 a->mtime = 0;
64}
65
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000066/* Convert from struct stat to filexfer attribs */
Damien Miller33804262001-02-04 23:20:18 +110067void
Damien Millerf58b58c2003-11-17 21:18:23 +110068stat_to_attrib(const struct stat *st, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +110069{
70 attrib_clear(a);
71 a->flags = 0;
72 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
73 a->size = st->st_size;
74 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
75 a->uid = st->st_uid;
76 a->gid = st->st_gid;
77 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
78 a->perm = st->st_mode;
79 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
80 a->atime = st->st_atime;
81 a->mtime = st->st_mtime;
82}
83
Damien Millere1a49812002-09-12 09:54:25 +100084/* Convert from filexfer attribs to struct stat */
85void
Damien Millerf58b58c2003-11-17 21:18:23 +110086attrib_to_stat(const Attrib *a, struct stat *st)
Damien Millere1a49812002-09-12 09:54:25 +100087{
88 memset(st, 0, sizeof(*st));
89
90 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
91 st->st_size = a->size;
92 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
93 st->st_uid = a->uid;
94 st->st_gid = a->gid;
95 }
96 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
97 st->st_mode = a->perm;
98 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
99 st->st_atime = a->atime;
100 st->st_mtime = a->mtime;
101 }
102}
103
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000104/* Decode attributes in buffer */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000105int
106decode_attrib(struct sshbuf *b, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100107{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000108 int r;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000109
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000110 attrib_clear(a);
111 if ((r = sshbuf_get_u32(b, &a->flags)) != 0)
112 return r;
113 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
114 if ((r = sshbuf_get_u64(b, &a->size)) != 0)
115 return r;
Damien Miller33804262001-02-04 23:20:18 +1100116 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000117 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
118 if ((r = sshbuf_get_u32(b, &a->uid)) != 0 ||
119 (r = sshbuf_get_u32(b, &a->gid)) != 0)
120 return r;
121 }
122 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
123 if ((r = sshbuf_get_u32(b, &a->perm)) != 0)
124 return r;
125 }
126 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
127 if ((r = sshbuf_get_u32(b, &a->atime)) != 0 ||
128 (r = sshbuf_get_u32(b, &a->mtime)) != 0)
129 return r;
Damien Miller33804262001-02-04 23:20:18 +1100130 }
131 /* vendor-specific extensions */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000132 if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) {
133 char *type;
134 u_char *data;
135 size_t dlen;
136 u_int i, count;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000137
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000138 if ((r = sshbuf_get_u32(b, &count)) != 0)
139 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller33804262001-02-04 23:20:18 +1100140 for (i = 0; i < count; i++) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000141 if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
142 (r = sshbuf_get_string(b, &data, &dlen)) != 0)
143 return r;
144 debug3("Got file attribute \"%.100s\" len %zu",
145 type, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000146 free(type);
147 free(data);
Damien Miller33804262001-02-04 23:20:18 +1100148 }
149 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000150 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100151}
152
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000153/* Encode attributes to buffer */
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000154int
155encode_attrib(struct sshbuf *b, const Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100156{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000157 int r;
158
159 if ((r = sshbuf_put_u32(b, a->flags)) != 0)
160 return r;
161 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
162 if ((r = sshbuf_put_u64(b, a->size)) != 0)
163 return r;
164 }
Damien Miller33804262001-02-04 23:20:18 +1100165 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000166 if ((r = sshbuf_put_u32(b, a->uid)) != 0 ||
167 (r = sshbuf_put_u32(b, a->gid)) != 0)
168 return r;
Damien Miller33804262001-02-04 23:20:18 +1100169 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000170 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
171 if ((r = sshbuf_put_u32(b, a->perm)) != 0)
172 return r;
173 }
Damien Miller33804262001-02-04 23:20:18 +1100174 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000175 if ((r = sshbuf_put_u32(b, a->atime)) != 0 ||
176 (r = sshbuf_put_u32(b, a->mtime)) != 0)
177 return r;
Damien Miller33804262001-02-04 23:20:18 +1100178 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000179 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100180}
181
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000182/* Convert from SSH2_FX_ status to text error message */
Damien Miller33804262001-02-04 23:20:18 +1100183const char *
184fx2txt(int status)
185{
186 switch (status) {
187 case SSH2_FX_OK:
Damien Millerd7686fd2001-02-10 00:40:03 +1100188 return("No error");
Damien Miller33804262001-02-04 23:20:18 +1100189 case SSH2_FX_EOF:
Damien Millerd7686fd2001-02-10 00:40:03 +1100190 return("End of file");
Damien Miller33804262001-02-04 23:20:18 +1100191 case SSH2_FX_NO_SUCH_FILE:
Damien Millerd7686fd2001-02-10 00:40:03 +1100192 return("No such file or directory");
Damien Miller33804262001-02-04 23:20:18 +1100193 case SSH2_FX_PERMISSION_DENIED:
Damien Millerd7686fd2001-02-10 00:40:03 +1100194 return("Permission denied");
Damien Miller33804262001-02-04 23:20:18 +1100195 case SSH2_FX_FAILURE:
196 return("Failure");
197 case SSH2_FX_BAD_MESSAGE:
198 return("Bad message");
199 case SSH2_FX_NO_CONNECTION:
200 return("No connection");
201 case SSH2_FX_CONNECTION_LOST:
202 return("Connection lost");
203 case SSH2_FX_OP_UNSUPPORTED:
204 return("Operation unsupported");
205 default:
206 return("Unknown status");
Ben Lindstromd05487d2001-12-06 17:50:03 +0000207 }
Damien Miller33804262001-02-04 23:20:18 +1100208 /* NOTREACHED */
209}
Damien Millere1a49812002-09-12 09:54:25 +1000210
211/*
212 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
213 */
214char *
Darren Tucker2901e2d2010-01-13 22:44:06 +1100215ls_file(const char *name, const struct stat *st, int remote, int si_units)
Damien Millere1a49812002-09-12 09:54:25 +1000216{
217 int ulen, glen, sz = 0;
Damien Millere1a49812002-09-12 09:54:25 +1000218 struct tm *ltime = localtime(&st->st_mtime);
millert@openbsd.org488c9322018-09-13 15:23:32 +0000219 const char *user, *group;
djm@openbsd.org072e1722017-06-10 06:36:46 +0000220 char buf[1024], lc[8], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
Darren Tucker2901e2d2010-01-13 22:44:06 +1100221 char sbuf[FMT_SCALED_STRSIZE];
Damien Millere00e4132014-01-10 10:40:45 +1100222 time_t now;
Damien Millere1a49812002-09-12 09:54:25 +1000223
224 strmode(st->st_mode, mode);
djm@openbsd.org072e1722017-06-10 06:36:46 +0000225 if (remote) {
Damien Millere1a49812002-09-12 09:54:25 +1000226 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
227 user = ubuf;
Damien Millere1a49812002-09-12 09:54:25 +1000228 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
229 group = gbuf;
djm@openbsd.org072e1722017-06-10 06:36:46 +0000230 strlcpy(lc, "?", sizeof(lc));
231 } else {
232 user = user_from_uid(st->st_uid, 0);
233 group = group_from_gid(st->st_gid, 0);
234 snprintf(lc, sizeof(lc), "%u", (u_int)st->st_nlink);
Damien Millere1a49812002-09-12 09:54:25 +1000235 }
236 if (ltime != NULL) {
Damien Millere00e4132014-01-10 10:40:45 +1100237 now = time(NULL);
238 if (now - (365*24*60*60)/2 < st->st_mtime &&
239 now >= st->st_mtime)
Damien Millere1a49812002-09-12 09:54:25 +1000240 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
241 else
242 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
243 }
244 if (sz == 0)
245 tbuf[0] = '\0';
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000246 ulen = MAXIMUM(strlen(user), 8);
247 glen = MAXIMUM(strlen(group), 8);
Darren Tucker2901e2d2010-01-13 22:44:06 +1100248 if (si_units) {
249 fmt_scaled((long long)st->st_size, sbuf);
djm@openbsd.org072e1722017-06-10 06:36:46 +0000250 snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8s %s %s",
251 mode, lc, ulen, user, glen, group,
Darren Tucker2901e2d2010-01-13 22:44:06 +1100252 sbuf, tbuf, name);
253 } else {
djm@openbsd.org072e1722017-06-10 06:36:46 +0000254 snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8llu %s %s",
255 mode, lc, ulen, user, glen, group,
Darren Tucker2901e2d2010-01-13 22:44:06 +1100256 (unsigned long long)st->st_size, tbuf, name);
257 }
Damien Millere1a49812002-09-12 09:54:25 +1000258 return xstrdup(buf);
259}