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