]> andersk Git - openssh.git/blame - sftp-common.c
- stevesk@cvs.openbsd.org 2006/07/26 02:35:17
[openssh.git] / sftp-common.c
CommitLineData
536c14e8 1/* $OpenBSD: sftp-common.c,v 1.18 2006/07/26 02:35:17 stevesk Exp $ */
61e96248 2/*
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"
4095f623 28
29#include <sys/types.h>
30#include <sys/stat.h>
536c14e8 31#include <sys/param.h>
61e96248 32
16fbe330 33#include <grp.h>
b1842393 34#include <pwd.h>
00146caa 35#include <string.h>
b0f6943a 36#include <time.h>
16fbe330 37
61e96248 38#include "buffer.h"
39#include "bufaux.h"
61e96248 40#include "log.h"
41#include "xmalloc.h"
42
43#include "sftp.h"
44#include "sftp-common.h"
45
255cabd9 46/* Clear contents of attributes structure */
61e96248 47void
48attrib_clear(Attrib *a)
49{
50 a->flags = 0;
51 a->size = 0;
52 a->uid = 0;
53 a->gid = 0;
54 a->perm = 0;
55 a->atime = 0;
56 a->mtime = 0;
57}
58
255cabd9 59/* Convert from struct stat to filexfer attribs */
61e96248 60void
b6c7b7b7 61stat_to_attrib(const struct stat *st, Attrib *a)
61e96248 62{
63 attrib_clear(a);
64 a->flags = 0;
65 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
66 a->size = st->st_size;
67 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
68 a->uid = st->st_uid;
69 a->gid = st->st_gid;
70 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
71 a->perm = st->st_mode;
72 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
73 a->atime = st->st_atime;
74 a->mtime = st->st_mtime;
75}
76
00b3ad3e 77/* Convert from filexfer attribs to struct stat */
78void
b6c7b7b7 79attrib_to_stat(const Attrib *a, struct stat *st)
00b3ad3e 80{
81 memset(st, 0, sizeof(*st));
82
83 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
84 st->st_size = a->size;
85 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
86 st->st_uid = a->uid;
87 st->st_gid = a->gid;
88 }
89 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
90 st->st_mode = a->perm;
91 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
92 st->st_atime = a->atime;
93 st->st_mtime = a->mtime;
94 }
95}
96
255cabd9 97/* Decode attributes in buffer */
61e96248 98Attrib *
99decode_attrib(Buffer *b)
100{
101 static Attrib a;
9906a836 102
61e96248 103 attrib_clear(&a);
104 a.flags = buffer_get_int(b);
105 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
106 a.size = buffer_get_int64(b);
107 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
108 a.uid = buffer_get_int(b);
109 a.gid = buffer_get_int(b);
110 }
111 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
112 a.perm = buffer_get_int(b);
113 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
114 a.atime = buffer_get_int(b);
115 a.mtime = buffer_get_int(b);
116 }
117 /* vendor-specific extensions */
118 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
119 char *type, *data;
120 int i, count;
9906a836 121
61e96248 122 count = buffer_get_int(b);
123 for (i = 0; i < count; i++) {
124 type = buffer_get_string(b, NULL);
125 data = buffer_get_string(b, NULL);
126 debug3("Got file attribute \"%s\"", type);
127 xfree(type);
128 xfree(data);
129 }
130 }
131 return &a;
132}
133
255cabd9 134/* Encode attributes to buffer */
61e96248 135void
b6c7b7b7 136encode_attrib(Buffer *b, const Attrib *a)
61e96248 137{
138 buffer_put_int(b, a->flags);
139 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
140 buffer_put_int64(b, a->size);
141 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
142 buffer_put_int(b, a->uid);
143 buffer_put_int(b, a->gid);
144 }
145 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
146 buffer_put_int(b, a->perm);
147 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
148 buffer_put_int(b, a->atime);
149 buffer_put_int(b, a->mtime);
150 }
151}
152
255cabd9 153/* Convert from SSH2_FX_ status to text error message */
61e96248 154const char *
155fx2txt(int status)
156{
157 switch (status) {
158 case SSH2_FX_OK:
0426a3b4 159 return("No error");
61e96248 160 case SSH2_FX_EOF:
0426a3b4 161 return("End of file");
61e96248 162 case SSH2_FX_NO_SUCH_FILE:
0426a3b4 163 return("No such file or directory");
61e96248 164 case SSH2_FX_PERMISSION_DENIED:
0426a3b4 165 return("Permission denied");
61e96248 166 case SSH2_FX_FAILURE:
167 return("Failure");
168 case SSH2_FX_BAD_MESSAGE:
169 return("Bad message");
170 case SSH2_FX_NO_CONNECTION:
171 return("No connection");
172 case SSH2_FX_CONNECTION_LOST:
173 return("Connection lost");
174 case SSH2_FX_OP_UNSUPPORTED:
175 return("Operation unsupported");
176 default:
177 return("Unknown status");
947b64c7 178 }
61e96248 179 /* NOTREACHED */
180}
00b3ad3e 181
182/*
183 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
184 */
185char *
b6c7b7b7 186ls_file(const char *name, const struct stat *st, int remote)
00b3ad3e 187{
188 int ulen, glen, sz = 0;
189 struct passwd *pw;
190 struct group *gr;
191 struct tm *ltime = localtime(&st->st_mtime);
192 char *user, *group;
193 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
194
195 strmode(st->st_mode, mode);
196 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
197 user = pw->pw_name;
198 } else {
199 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
200 user = ubuf;
201 }
202 if (!remote && (gr = getgrgid(st->st_gid)) != NULL) {
203 group = gr->gr_name;
204 } else {
205 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
206 group = gbuf;
207 }
208 if (ltime != NULL) {
209 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
210 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
211 else
212 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
213 }
214 if (sz == 0)
215 tbuf[0] = '\0';
216 ulen = MAX(strlen(user), 8);
217 glen = MAX(strlen(group), 8);
b9ed513a 218 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
219 (u_int)st->st_nlink, ulen, user, glen, group,
c9826b28 220 (unsigned long long)st->st_size, tbuf, name);
00b3ad3e 221 return xstrdup(buf);
222}
This page took 0.230505 seconds and 5 git commands to generate.