]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - crypto/openssh/sftp-common.c
MFH (r237568, r255422, r255460, r255766, r255767, r255774, r255829,
[FreeBSD/stable/9.git] / crypto / openssh / sftp-common.c
1 /* $OpenBSD: sftp-common.c,v 1.26 2014/01/09 03:26:00 guenther Exp $ */
2 /* $FreeBSD$ */
3 /*
4  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5  * Copyright (c) 2001 Damien Miller.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "includes.h"
29 __RCSID("$FreeBSD$");
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/param.h>
34
35 #include <grp.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #include <stdarg.h>
42 #ifdef HAVE_UTIL_H
43 #include <util.h>
44 #endif
45
46 #include "xmalloc.h"
47 #include "buffer.h"
48 #include "log.h"
49
50 #include "sftp.h"
51 #include "sftp-common.h"
52
53 /* Clear contents of attributes structure */
54 void
55 attrib_clear(Attrib *a)
56 {
57         a->flags = 0;
58         a->size = 0;
59         a->uid = 0;
60         a->gid = 0;
61         a->perm = 0;
62         a->atime = 0;
63         a->mtime = 0;
64 }
65
66 /* Convert from struct stat to filexfer attribs */
67 void
68 stat_to_attrib(const struct stat *st, Attrib *a)
69 {
70         attrib_clear(a);
71         a->flags = 0;
72         a->flags |= SSH2_FILEXFER_ATTR_SIZE;
73         a->size = st->st_size;
74         a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
75         a->uid = st->st_uid;
76         a->gid = st->st_gid;
77         a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
78         a->perm = st->st_mode;
79         a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
80         a->atime = st->st_atime;
81         a->mtime = st->st_mtime;
82 }
83
84 /* Convert from filexfer attribs to struct stat */
85 void
86 attrib_to_stat(const Attrib *a, struct stat *st)
87 {
88         memset(st, 0, sizeof(*st));
89
90         if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
91                 st->st_size = a->size;
92         if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
93                 st->st_uid = a->uid;
94                 st->st_gid = a->gid;
95         }
96         if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
97                 st->st_mode = a->perm;
98         if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
99                 st->st_atime = a->atime;
100                 st->st_mtime = a->mtime;
101         }
102 }
103
104 /* Decode attributes in buffer */
105 Attrib *
106 decode_attrib(Buffer *b)
107 {
108         static Attrib a;
109
110         attrib_clear(&a);
111         a.flags = buffer_get_int(b);
112         if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
113                 a.size = buffer_get_int64(b);
114         if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
115                 a.uid = buffer_get_int(b);
116                 a.gid = buffer_get_int(b);
117         }
118         if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
119                 a.perm = buffer_get_int(b);
120         if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
121                 a.atime = buffer_get_int(b);
122                 a.mtime = buffer_get_int(b);
123         }
124         /* vendor-specific extensions */
125         if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
126                 char *type, *data;
127                 int i, count;
128
129                 count = buffer_get_int(b);
130                 for (i = 0; i < count; i++) {
131                         type = buffer_get_string(b, NULL);
132                         data = buffer_get_string(b, NULL);
133                         debug3("Got file attribute \"%s\"", type);
134                         free(type);
135                         free(data);
136                 }
137         }
138         return &a;
139 }
140
141 /* Encode attributes to buffer */
142 void
143 encode_attrib(Buffer *b, const Attrib *a)
144 {
145         buffer_put_int(b, a->flags);
146         if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
147                 buffer_put_int64(b, a->size);
148         if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
149                 buffer_put_int(b, a->uid);
150                 buffer_put_int(b, a->gid);
151         }
152         if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
153                 buffer_put_int(b, a->perm);
154         if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
155                 buffer_put_int(b, a->atime);
156                 buffer_put_int(b, a->mtime);
157         }
158 }
159
160 /* Convert from SSH2_FX_ status to text error message */
161 const char *
162 fx2txt(int status)
163 {
164         switch (status) {
165         case SSH2_FX_OK:
166                 return("No error");
167         case SSH2_FX_EOF:
168                 return("End of file");
169         case SSH2_FX_NO_SUCH_FILE:
170                 return("No such file or directory");
171         case SSH2_FX_PERMISSION_DENIED:
172                 return("Permission denied");
173         case SSH2_FX_FAILURE:
174                 return("Failure");
175         case SSH2_FX_BAD_MESSAGE:
176                 return("Bad message");
177         case SSH2_FX_NO_CONNECTION:
178                 return("No connection");
179         case SSH2_FX_CONNECTION_LOST:
180                 return("Connection lost");
181         case SSH2_FX_OP_UNSUPPORTED:
182                 return("Operation unsupported");
183         default:
184                 return("Unknown status");
185         }
186         /* NOTREACHED */
187 }
188
189 /*
190  * drwxr-xr-x    5 markus   markus       1024 Jan 13 18:39 .ssh
191  */
192 char *
193 ls_file(const char *name, const struct stat *st, int remote, int si_units)
194 {
195         int ulen, glen, sz = 0;
196         struct tm *ltime = localtime(&st->st_mtime);
197         const char *user, *group;
198         char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
199         char sbuf[FMT_SCALED_STRSIZE];
200         time_t now;
201
202         strmode(st->st_mode, mode);
203         if (!remote) {
204                 user = user_from_uid(st->st_uid, 0);
205         } else {
206                 snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
207                 user = ubuf;
208         }
209         if (!remote) {
210                 group = group_from_gid(st->st_gid, 0);
211         } else {
212                 snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
213                 group = gbuf;
214         }
215         if (ltime != NULL) {
216                 now = time(NULL);
217                 if (now - (365*24*60*60)/2 < st->st_mtime &&
218                     now >= st->st_mtime)
219                         sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
220                 else
221                         sz = strftime(tbuf, sizeof tbuf, "%b %e  %Y", ltime);
222         }
223         if (sz == 0)
224                 tbuf[0] = '\0';
225         ulen = MAX(strlen(user), 8);
226         glen = MAX(strlen(group), 8);
227         if (si_units) {
228                 fmt_scaled((long long)st->st_size, sbuf);
229                 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode,
230                     (u_int)st->st_nlink, ulen, user, glen, group,
231                     sbuf, tbuf, name);
232         } else {
233                 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
234                     (u_int)st->st_nlink, ulen, user, glen, group,
235                     (unsigned long long)st->st_size, tbuf, name);
236         }
237         return xstrdup(buf);
238 }