blob: 6fd0d76b69ee791461937317c866ae73d15af488 [file] [log] [blame]
Damien Miller5598b4f2006-07-24 14:09:40 +10001/* $OpenBSD: sftp-common.c,v 1.16 2006/07/22 19:08:54 stevesk 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 Miller5598b4f2006-07-24 14:09:40 +100034#include <time.h>
Damien Miller427a1d52006-07-10 20:20:33 +100035
Damien Miller33804262001-02-04 23:20:18 +110036#include "buffer.h"
37#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110038#include "log.h"
39#include "xmalloc.h"
40
41#include "sftp.h"
42#include "sftp-common.h"
43
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000044/* Clear contents of attributes structure */
Damien Miller33804262001-02-04 23:20:18 +110045void
46attrib_clear(Attrib *a)
47{
48 a->flags = 0;
49 a->size = 0;
50 a->uid = 0;
51 a->gid = 0;
52 a->perm = 0;
53 a->atime = 0;
54 a->mtime = 0;
55}
56
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000057/* Convert from struct stat to filexfer attribs */
Damien Miller33804262001-02-04 23:20:18 +110058void
Damien Millerf58b58c2003-11-17 21:18:23 +110059stat_to_attrib(const struct stat *st, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +110060{
61 attrib_clear(a);
62 a->flags = 0;
63 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
64 a->size = st->st_size;
65 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
66 a->uid = st->st_uid;
67 a->gid = st->st_gid;
68 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
69 a->perm = st->st_mode;
70 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
71 a->atime = st->st_atime;
72 a->mtime = st->st_mtime;
73}
74
Damien Millere1a49812002-09-12 09:54:25 +100075/* Convert from filexfer attribs to struct stat */
76void
Damien Millerf58b58c2003-11-17 21:18:23 +110077attrib_to_stat(const Attrib *a, struct stat *st)
Damien Millere1a49812002-09-12 09:54:25 +100078{
79 memset(st, 0, sizeof(*st));
80
81 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
82 st->st_size = a->size;
83 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
84 st->st_uid = a->uid;
85 st->st_gid = a->gid;
86 }
87 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
88 st->st_mode = a->perm;
89 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
90 st->st_atime = a->atime;
91 st->st_mtime = a->mtime;
92 }
93}
94
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000095/* Decode attributes in buffer */
Damien Miller33804262001-02-04 23:20:18 +110096Attrib *
97decode_attrib(Buffer *b)
98{
99 static Attrib a;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000100
Damien Miller33804262001-02-04 23:20:18 +1100101 attrib_clear(&a);
102 a.flags = buffer_get_int(b);
103 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
104 a.size = buffer_get_int64(b);
105 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
106 a.uid = buffer_get_int(b);
107 a.gid = buffer_get_int(b);
108 }
109 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
110 a.perm = buffer_get_int(b);
111 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
112 a.atime = buffer_get_int(b);
113 a.mtime = buffer_get_int(b);
114 }
115 /* vendor-specific extensions */
116 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
117 char *type, *data;
118 int i, count;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000119
Damien Miller33804262001-02-04 23:20:18 +1100120 count = buffer_get_int(b);
121 for (i = 0; i < count; i++) {
122 type = buffer_get_string(b, NULL);
123 data = buffer_get_string(b, NULL);
124 debug3("Got file attribute \"%s\"", type);
125 xfree(type);
126 xfree(data);
127 }
128 }
129 return &a;
130}
131
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000132/* Encode attributes to buffer */
Damien Miller33804262001-02-04 23:20:18 +1100133void
Damien Millerf58b58c2003-11-17 21:18:23 +1100134encode_attrib(Buffer *b, const Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100135{
136 buffer_put_int(b, a->flags);
137 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
138 buffer_put_int64(b, a->size);
139 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
140 buffer_put_int(b, a->uid);
141 buffer_put_int(b, a->gid);
142 }
143 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
144 buffer_put_int(b, a->perm);
145 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
146 buffer_put_int(b, a->atime);
147 buffer_put_int(b, a->mtime);
148 }
149}
150
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000151/* Convert from SSH2_FX_ status to text error message */
Damien Miller33804262001-02-04 23:20:18 +1100152const char *
153fx2txt(int status)
154{
155 switch (status) {
156 case SSH2_FX_OK:
Damien Millerd7686fd2001-02-10 00:40:03 +1100157 return("No error");
Damien Miller33804262001-02-04 23:20:18 +1100158 case SSH2_FX_EOF:
Damien Millerd7686fd2001-02-10 00:40:03 +1100159 return("End of file");
Damien Miller33804262001-02-04 23:20:18 +1100160 case SSH2_FX_NO_SUCH_FILE:
Damien Millerd7686fd2001-02-10 00:40:03 +1100161 return("No such file or directory");
Damien Miller33804262001-02-04 23:20:18 +1100162 case SSH2_FX_PERMISSION_DENIED:
Damien Millerd7686fd2001-02-10 00:40:03 +1100163 return("Permission denied");
Damien Miller33804262001-02-04 23:20:18 +1100164 case SSH2_FX_FAILURE:
165 return("Failure");
166 case SSH2_FX_BAD_MESSAGE:
167 return("Bad message");
168 case SSH2_FX_NO_CONNECTION:
169 return("No connection");
170 case SSH2_FX_CONNECTION_LOST:
171 return("Connection lost");
172 case SSH2_FX_OP_UNSUPPORTED:
173 return("Operation unsupported");
174 default:
175 return("Unknown status");
Ben Lindstromd05487d2001-12-06 17:50:03 +0000176 }
Damien Miller33804262001-02-04 23:20:18 +1100177 /* NOTREACHED */
178}
Damien Millere1a49812002-09-12 09:54:25 +1000179
180/*
181 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
182 */
183char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100184ls_file(const char *name, const struct stat *st, int remote)
Damien Millere1a49812002-09-12 09:54:25 +1000185{
186 int ulen, glen, sz = 0;
187 struct passwd *pw;
188 struct group *gr;
189 struct tm *ltime = localtime(&st->st_mtime);
190 char *user, *group;
191 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
192
193 strmode(st->st_mode, mode);
194 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
195 user = pw->pw_name;
196 } else {
197 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
198 user = ubuf;
199 }
200 if (!remote && (gr = getgrgid(st->st_gid)) != NULL) {
201 group = gr->gr_name;
202 } else {
203 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
204 group = gbuf;
205 }
206 if (ltime != NULL) {
207 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
208 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
209 else
210 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
211 }
212 if (sz == 0)
213 tbuf[0] = '\0';
214 ulen = MAX(strlen(user), 8);
215 glen = MAX(strlen(group), 8);
Damien Miller04bd8b02003-05-25 14:38:33 +1000216 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
217 (u_int)st->st_nlink, ulen, user, glen, group,
Ben Lindstrom08513812002-11-09 15:40:34 +0000218 (unsigned long long)st->st_size, tbuf, name);
Damien Millere1a49812002-09-12 09:54:25 +1000219 return xstrdup(buf);
220}