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