2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 * Copyright (c) 2001 Damien Miller. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 RCSID("$OpenBSD: sftp-common.c,v 1.11 2006/02/20 17:19:54 stevesk Exp $");
29 #include <sys/types.h>
38 #include "sftp-common.h"
40 /* Clear contents of attributes structure */
42 attrib_clear(Attrib *a)
53 /* Convert from struct stat to filexfer attribs */
55 stat_to_attrib(const struct stat *st, Attrib *a)
59 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
60 a->size = st->st_size;
61 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
64 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
65 a->perm = st->st_mode;
66 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
67 a->atime = st->st_atime;
68 a->mtime = st->st_mtime;
71 /* Convert from filexfer attribs to struct stat */
73 attrib_to_stat(const Attrib *a, struct stat *st)
75 memset(st, 0, sizeof(*st));
77 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
78 st->st_size = a->size;
79 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
83 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
84 st->st_mode = a->perm;
85 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
86 st->st_atime = a->atime;
87 st->st_mtime = a->mtime;
91 /* Decode attributes in buffer */
93 decode_attrib(Buffer *b)
98 a.flags = buffer_get_int(b);
99 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
100 a.size = buffer_get_int64(b);
101 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
102 a.uid = buffer_get_int(b);
103 a.gid = buffer_get_int(b);
105 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
106 a.perm = buffer_get_int(b);
107 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
108 a.atime = buffer_get_int(b);
109 a.mtime = buffer_get_int(b);
111 /* vendor-specific extensions */
112 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
116 count = buffer_get_int(b);
117 for (i = 0; i < count; i++) {
118 type = buffer_get_string(b, NULL);
119 data = buffer_get_string(b, NULL);
120 debug3("Got file attribute \"%s\"", type);
128 /* Encode attributes to buffer */
130 encode_attrib(Buffer *b, const Attrib *a)
132 buffer_put_int(b, a->flags);
133 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
134 buffer_put_int64(b, a->size);
135 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
136 buffer_put_int(b, a->uid);
137 buffer_put_int(b, a->gid);
139 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
140 buffer_put_int(b, a->perm);
141 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
142 buffer_put_int(b, a->atime);
143 buffer_put_int(b, a->mtime);
147 /* Convert from SSH2_FX_ status to text error message */
155 return("End of file");
156 case SSH2_FX_NO_SUCH_FILE:
157 return("No such file or directory");
158 case SSH2_FX_PERMISSION_DENIED:
159 return("Permission denied");
160 case SSH2_FX_FAILURE:
162 case SSH2_FX_BAD_MESSAGE:
163 return("Bad message");
164 case SSH2_FX_NO_CONNECTION:
165 return("No connection");
166 case SSH2_FX_CONNECTION_LOST:
167 return("Connection lost");
168 case SSH2_FX_OP_UNSUPPORTED:
169 return("Operation unsupported");
171 return("Unknown status");
177 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
180 ls_file(const char *name, const struct stat *st, int remote)
182 int ulen, glen, sz = 0;
185 struct tm *ltime = localtime(&st->st_mtime);
187 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
189 strmode(st->st_mode, mode);
190 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
193 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
196 if (!remote && (gr = getgrgid(st->st_gid)) != NULL) {
199 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
203 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
204 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
206 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
210 ulen = MAX(strlen(user), 8);
211 glen = MAX(strlen(group), 8);
212 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
213 (u_int)st->st_nlink, ulen, user, glen, group,
214 (unsigned long long)st->st_size, tbuf, name);