1 /* $OpenBSD: sftp-common.c,v 1.32 2020/10/18 11:32:02 djm Exp $ */
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2001 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/types.h>
51 #include "sftp-common.h"
53 /* Clear contents of attributes structure */
55 attrib_clear(Attrib
*a
)
66 /* Convert from struct stat to filexfer attribs */
68 stat_to_attrib(const struct stat
*st
, Attrib
*a
)
72 a
->flags
|= SSH2_FILEXFER_ATTR_SIZE
;
73 a
->size
= st
->st_size
;
74 a
->flags
|= SSH2_FILEXFER_ATTR_UIDGID
;
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
;
84 /* Convert from filexfer attribs to struct stat */
86 attrib_to_stat(const Attrib
*a
, struct stat
*st
)
88 memset(st
, 0, sizeof(*st
));
90 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
91 st
->st_size
= a
->size
;
92 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
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
;
104 /* Decode attributes in buffer */
106 decode_attrib(struct sshbuf
*b
, Attrib
*a
)
111 if ((r
= sshbuf_get_u32(b
, &a
->flags
)) != 0)
113 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
114 if ((r
= sshbuf_get_u64(b
, &a
->size
)) != 0)
117 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)
122 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
123 if ((r
= sshbuf_get_u32(b
, &a
->perm
)) != 0)
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)
131 /* vendor-specific extensions */
132 if (a
->flags
& SSH2_FILEXFER_ATTR_EXTENDED
) {
138 if ((r
= sshbuf_get_u32(b
, &count
)) != 0)
140 for (i
= 0; i
< count
; i
++) {
141 if ((r
= sshbuf_get_cstring(b
, &type
, NULL
)) != 0 ||
142 (r
= sshbuf_get_string(b
, &data
, &dlen
)) != 0)
144 debug3("Got file attribute \"%.100s\" len %zu",
153 /* Encode attributes to buffer */
155 encode_attrib(struct sshbuf
*b
, const Attrib
*a
)
159 if ((r
= sshbuf_put_u32(b
, a
->flags
)) != 0)
161 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
162 if ((r
= sshbuf_put_u64(b
, a
->size
)) != 0)
165 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
166 if ((r
= sshbuf_put_u32(b
, a
->uid
)) != 0 ||
167 (r
= sshbuf_put_u32(b
, a
->gid
)) != 0)
170 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
171 if ((r
= sshbuf_put_u32(b
, a
->perm
)) != 0)
174 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
175 if ((r
= sshbuf_put_u32(b
, a
->atime
)) != 0 ||
176 (r
= sshbuf_put_u32(b
, a
->mtime
)) != 0)
182 /* Convert from SSH2_FX_ status to text error message */
190 return("End of file");
191 case SSH2_FX_NO_SUCH_FILE
:
192 return("No such file or directory");
193 case SSH2_FX_PERMISSION_DENIED
:
194 return("Permission denied");
195 case SSH2_FX_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");
206 return("Unknown status");
212 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
215 ls_file(const char *name
, const struct stat
*st
, int remote
, int si_units
)
217 int ulen
, glen
, sz
= 0;
218 struct tm
*ltime
= localtime(&st
->st_mtime
);
219 const char *user
, *group
;
220 char buf
[1024], lc
[8], mode
[11+1], tbuf
[12+1], ubuf
[11+1], gbuf
[11+1];
221 char sbuf
[FMT_SCALED_STRSIZE
];
224 strmode(st
->st_mode
, mode
);
226 snprintf(ubuf
, sizeof ubuf
, "%u", (u_int
)st
->st_uid
);
228 snprintf(gbuf
, sizeof gbuf
, "%u", (u_int
)st
->st_gid
);
230 strlcpy(lc
, "?", sizeof(lc
));
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
);
238 if (now
- (365*24*60*60)/2 < st
->st_mtime
&&
240 sz
= strftime(tbuf
, sizeof tbuf
, "%b %e %H:%M", ltime
);
242 sz
= strftime(tbuf
, sizeof tbuf
, "%b %e %Y", ltime
);
246 ulen
= MAXIMUM(strlen(user
), 8);
247 glen
= MAXIMUM(strlen(group
), 8);
249 fmt_scaled((long long)st
->st_size
, sbuf
);
250 snprintf(buf
, sizeof buf
, "%s %3s %-*s %-*s %8s %s %s",
251 mode
, lc
, ulen
, user
, glen
, group
,
254 snprintf(buf
, sizeof buf
, "%s %3s %-*s %-*s %8llu %s %s",
255 mode
, lc
, ulen
, user
, glen
, group
,
256 (unsigned long long)st
->st_size
, tbuf
, name
);