blob: 4cea3c3056a41ca909f3d2334ddaf1b7309cd9f6 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#include "includes.h"
Damien Millerf58b58c2003-11-17 21:18:23 +110027RCSID("$OpenBSD: sftp-common.c,v 1.10 2003/11/10 16:23:41 jakob Exp $");
Damien Miller33804262001-02-04 23:20:18 +110028
29#include "buffer.h"
30#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110031#include "log.h"
32#include "xmalloc.h"
33
34#include "sftp.h"
35#include "sftp-common.h"
36
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000037/* Clear contents of attributes structure */
Damien Miller33804262001-02-04 23:20:18 +110038void
39attrib_clear(Attrib *a)
40{
41 a->flags = 0;
42 a->size = 0;
43 a->uid = 0;
44 a->gid = 0;
45 a->perm = 0;
46 a->atime = 0;
47 a->mtime = 0;
48}
49
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000050/* Convert from struct stat to filexfer attribs */
Damien Miller33804262001-02-04 23:20:18 +110051void
Damien Millerf58b58c2003-11-17 21:18:23 +110052stat_to_attrib(const struct stat *st, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +110053{
54 attrib_clear(a);
55 a->flags = 0;
56 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
57 a->size = st->st_size;
58 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
59 a->uid = st->st_uid;
60 a->gid = st->st_gid;
61 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
62 a->perm = st->st_mode;
63 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
64 a->atime = st->st_atime;
65 a->mtime = st->st_mtime;
66}
67
Damien Millere1a49812002-09-12 09:54:25 +100068/* Convert from filexfer attribs to struct stat */
69void
Damien Millerf58b58c2003-11-17 21:18:23 +110070attrib_to_stat(const Attrib *a, struct stat *st)
Damien Millere1a49812002-09-12 09:54:25 +100071{
72 memset(st, 0, sizeof(*st));
73
74 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
75 st->st_size = a->size;
76 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
77 st->st_uid = a->uid;
78 st->st_gid = a->gid;
79 }
80 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
81 st->st_mode = a->perm;
82 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
83 st->st_atime = a->atime;
84 st->st_mtime = a->mtime;
85 }
86}
87
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000088/* Decode attributes in buffer */
Damien Miller33804262001-02-04 23:20:18 +110089Attrib *
90decode_attrib(Buffer *b)
91{
92 static Attrib a;
Ben Lindstromb1f483f2002-06-23 21:27:18 +000093
Damien Miller33804262001-02-04 23:20:18 +110094 attrib_clear(&a);
95 a.flags = buffer_get_int(b);
96 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
97 a.size = buffer_get_int64(b);
98 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
99 a.uid = buffer_get_int(b);
100 a.gid = buffer_get_int(b);
101 }
102 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
103 a.perm = buffer_get_int(b);
104 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
105 a.atime = buffer_get_int(b);
106 a.mtime = buffer_get_int(b);
107 }
108 /* vendor-specific extensions */
109 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
110 char *type, *data;
111 int i, count;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000112
Damien Miller33804262001-02-04 23:20:18 +1100113 count = buffer_get_int(b);
114 for (i = 0; i < count; i++) {
115 type = buffer_get_string(b, NULL);
116 data = buffer_get_string(b, NULL);
117 debug3("Got file attribute \"%s\"", type);
118 xfree(type);
119 xfree(data);
120 }
121 }
122 return &a;
123}
124
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000125/* Encode attributes to buffer */
Damien Miller33804262001-02-04 23:20:18 +1100126void
Damien Millerf58b58c2003-11-17 21:18:23 +1100127encode_attrib(Buffer *b, const Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100128{
129 buffer_put_int(b, a->flags);
130 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
131 buffer_put_int64(b, a->size);
132 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
133 buffer_put_int(b, a->uid);
134 buffer_put_int(b, a->gid);
135 }
136 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
137 buffer_put_int(b, a->perm);
138 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
139 buffer_put_int(b, a->atime);
140 buffer_put_int(b, a->mtime);
141 }
142}
143
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000144/* Convert from SSH2_FX_ status to text error message */
Damien Miller33804262001-02-04 23:20:18 +1100145const char *
146fx2txt(int status)
147{
148 switch (status) {
149 case SSH2_FX_OK:
Damien Millerd7686fd2001-02-10 00:40:03 +1100150 return("No error");
Damien Miller33804262001-02-04 23:20:18 +1100151 case SSH2_FX_EOF:
Damien Millerd7686fd2001-02-10 00:40:03 +1100152 return("End of file");
Damien Miller33804262001-02-04 23:20:18 +1100153 case SSH2_FX_NO_SUCH_FILE:
Damien Millerd7686fd2001-02-10 00:40:03 +1100154 return("No such file or directory");
Damien Miller33804262001-02-04 23:20:18 +1100155 case SSH2_FX_PERMISSION_DENIED:
Damien Millerd7686fd2001-02-10 00:40:03 +1100156 return("Permission denied");
Damien Miller33804262001-02-04 23:20:18 +1100157 case SSH2_FX_FAILURE:
158 return("Failure");
159 case SSH2_FX_BAD_MESSAGE:
160 return("Bad message");
161 case SSH2_FX_NO_CONNECTION:
162 return("No connection");
163 case SSH2_FX_CONNECTION_LOST:
164 return("Connection lost");
165 case SSH2_FX_OP_UNSUPPORTED:
166 return("Operation unsupported");
167 default:
168 return("Unknown status");
Ben Lindstromd05487d2001-12-06 17:50:03 +0000169 }
Damien Miller33804262001-02-04 23:20:18 +1100170 /* NOTREACHED */
171}
Damien Millere1a49812002-09-12 09:54:25 +1000172
173/*
174 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
175 */
176char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100177ls_file(const char *name, const struct stat *st, int remote)
Damien Millere1a49812002-09-12 09:54:25 +1000178{
179 int ulen, glen, sz = 0;
180 struct passwd *pw;
181 struct group *gr;
182 struct tm *ltime = localtime(&st->st_mtime);
183 char *user, *group;
184 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
185
186 strmode(st->st_mode, mode);
187 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
188 user = pw->pw_name;
189 } else {
190 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
191 user = ubuf;
192 }
193 if (!remote && (gr = getgrgid(st->st_gid)) != NULL) {
194 group = gr->gr_name;
195 } else {
196 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
197 group = gbuf;
198 }
199 if (ltime != NULL) {
200 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
201 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
202 else
203 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
204 }
205 if (sz == 0)
206 tbuf[0] = '\0';
207 ulen = MAX(strlen(user), 8);
208 glen = MAX(strlen(group), 8);
Damien Miller04bd8b02003-05-25 14:38:33 +1000209 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
210 (u_int)st->st_nlink, ulen, user, glen, group,
Ben Lindstrom08513812002-11-09 15:40:34 +0000211 (unsigned long long)st->st_size, tbuf, name);
Damien Millere1a49812002-09-12 09:54:25 +1000212 return xstrdup(buf);
213}