blob: 92ce90661138702c5e39ad31ec1afdbf40acd0ab [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: sftp-common.c,v 1.15 2006/07/06 16:03:53 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 Miller427a1d52006-07-10 20:20:33 +100034
Damien Miller33804262001-02-04 23:20:18 +110035#include "buffer.h"
36#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110037#include "log.h"
38#include "xmalloc.h"
39
40#include "sftp.h"
41#include "sftp-common.h"
42
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000043/* Clear contents of attributes structure */
Damien Miller33804262001-02-04 23:20:18 +110044void
45attrib_clear(Attrib *a)
46{
47 a->flags = 0;
48 a->size = 0;
49 a->uid = 0;
50 a->gid = 0;
51 a->perm = 0;
52 a->atime = 0;
53 a->mtime = 0;
54}
55
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000056/* Convert from struct stat to filexfer attribs */
Damien Miller33804262001-02-04 23:20:18 +110057void
Damien Millerf58b58c2003-11-17 21:18:23 +110058stat_to_attrib(const struct stat *st, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +110059{
60 attrib_clear(a);
61 a->flags = 0;
62 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
63 a->size = st->st_size;
64 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
65 a->uid = st->st_uid;
66 a->gid = st->st_gid;
67 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
68 a->perm = st->st_mode;
69 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
70 a->atime = st->st_atime;
71 a->mtime = st->st_mtime;
72}
73
Damien Millere1a49812002-09-12 09:54:25 +100074/* Convert from filexfer attribs to struct stat */
75void
Damien Millerf58b58c2003-11-17 21:18:23 +110076attrib_to_stat(const Attrib *a, struct stat *st)
Damien Millere1a49812002-09-12 09:54:25 +100077{
78 memset(st, 0, sizeof(*st));
79
80 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
81 st->st_size = a->size;
82 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
83 st->st_uid = a->uid;
84 st->st_gid = a->gid;
85 }
86 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
87 st->st_mode = a->perm;
88 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
89 st->st_atime = a->atime;
90 st->st_mtime = a->mtime;
91 }
92}
93
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000094/* Decode attributes in buffer */
Damien Miller33804262001-02-04 23:20:18 +110095Attrib *
96decode_attrib(Buffer *b)
97{
98 static Attrib a;
Ben Lindstromb1f483f2002-06-23 21:27:18 +000099
Damien Miller33804262001-02-04 23:20:18 +1100100 attrib_clear(&a);
101 a.flags = buffer_get_int(b);
102 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
103 a.size = buffer_get_int64(b);
104 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
105 a.uid = buffer_get_int(b);
106 a.gid = buffer_get_int(b);
107 }
108 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
109 a.perm = buffer_get_int(b);
110 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
111 a.atime = buffer_get_int(b);
112 a.mtime = buffer_get_int(b);
113 }
114 /* vendor-specific extensions */
115 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
116 char *type, *data;
117 int i, count;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000118
Damien Miller33804262001-02-04 23:20:18 +1100119 count = buffer_get_int(b);
120 for (i = 0; i < count; i++) {
121 type = buffer_get_string(b, NULL);
122 data = buffer_get_string(b, NULL);
123 debug3("Got file attribute \"%s\"", type);
124 xfree(type);
125 xfree(data);
126 }
127 }
128 return &a;
129}
130
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000131/* Encode attributes to buffer */
Damien Miller33804262001-02-04 23:20:18 +1100132void
Damien Millerf58b58c2003-11-17 21:18:23 +1100133encode_attrib(Buffer *b, const Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100134{
135 buffer_put_int(b, a->flags);
136 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
137 buffer_put_int64(b, a->size);
138 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
139 buffer_put_int(b, a->uid);
140 buffer_put_int(b, a->gid);
141 }
142 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
143 buffer_put_int(b, a->perm);
144 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
145 buffer_put_int(b, a->atime);
146 buffer_put_int(b, a->mtime);
147 }
148}
149
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000150/* Convert from SSH2_FX_ status to text error message */
Damien Miller33804262001-02-04 23:20:18 +1100151const char *
152fx2txt(int status)
153{
154 switch (status) {
155 case SSH2_FX_OK:
Damien Millerd7686fd2001-02-10 00:40:03 +1100156 return("No error");
Damien Miller33804262001-02-04 23:20:18 +1100157 case SSH2_FX_EOF:
Damien Millerd7686fd2001-02-10 00:40:03 +1100158 return("End of file");
Damien Miller33804262001-02-04 23:20:18 +1100159 case SSH2_FX_NO_SUCH_FILE:
Damien Millerd7686fd2001-02-10 00:40:03 +1100160 return("No such file or directory");
Damien Miller33804262001-02-04 23:20:18 +1100161 case SSH2_FX_PERMISSION_DENIED:
Damien Millerd7686fd2001-02-10 00:40:03 +1100162 return("Permission denied");
Damien Miller33804262001-02-04 23:20:18 +1100163 case SSH2_FX_FAILURE:
164 return("Failure");
165 case SSH2_FX_BAD_MESSAGE:
166 return("Bad message");
167 case SSH2_FX_NO_CONNECTION:
168 return("No connection");
169 case SSH2_FX_CONNECTION_LOST:
170 return("Connection lost");
171 case SSH2_FX_OP_UNSUPPORTED:
172 return("Operation unsupported");
173 default:
174 return("Unknown status");
Ben Lindstromd05487d2001-12-06 17:50:03 +0000175 }
Damien Miller33804262001-02-04 23:20:18 +1100176 /* NOTREACHED */
177}
Damien Millere1a49812002-09-12 09:54:25 +1000178
179/*
180 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
181 */
182char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100183ls_file(const char *name, const struct stat *st, int remote)
Damien Millere1a49812002-09-12 09:54:25 +1000184{
185 int ulen, glen, sz = 0;
186 struct passwd *pw;
187 struct group *gr;
188 struct tm *ltime = localtime(&st->st_mtime);
189 char *user, *group;
190 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
191
192 strmode(st->st_mode, mode);
193 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
194 user = pw->pw_name;
195 } else {
196 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
197 user = ubuf;
198 }
199 if (!remote && (gr = getgrgid(st->st_gid)) != NULL) {
200 group = gr->gr_name;
201 } else {
202 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
203 group = gbuf;
204 }
205 if (ltime != NULL) {
206 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
207 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
208 else
209 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
210 }
211 if (sz == 0)
212 tbuf[0] = '\0';
213 ulen = MAX(strlen(user), 8);
214 glen = MAX(strlen(group), 8);
Damien Miller04bd8b02003-05-25 14:38:33 +1000215 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
216 (u_int)st->st_nlink, ulen, user, glen, group,
Ben Lindstrom08513812002-11-09 15:40:34 +0000217 (unsigned long long)st->st_size, tbuf, name);
Damien Millere1a49812002-09-12 09:54:25 +1000218 return xstrdup(buf);
219}