Sync usage with man page.
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / sftp-common.c
blob516e8f38fabf97d4112566e314e7b45f4bd05653
1 /* $NetBSD$ */
2 /* $OpenBSD: sftp-common.c,v 1.20 2006/08/03 03:34:42 deraadt Exp $ */
3 /*
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Damien Miller. All rights reserved.
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.
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.
28 #include "includes.h"
29 __RCSID("$NetBSD: sftp-common.c,v 1.12 2006/09/28 21:22:15 christos Exp $");
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
34 #include <grp.h>
35 #include <pwd.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <time.h>
39 #include <stdarg.h>
40 #include <unistd.h>
42 #include "xmalloc.h"
43 #include "buffer.h"
44 #include "log.h"
46 #include "sftp.h"
47 #include "sftp-common.h"
49 /* Clear contents of attributes structure */
50 void
51 attrib_clear(Attrib *a)
53 a->flags = 0;
54 a->size = 0;
55 a->uid = 0;
56 a->gid = 0;
57 a->perm = 0;
58 a->atime = 0;
59 a->mtime = 0;
62 /* Convert from struct stat to filexfer attribs */
63 void
64 stat_to_attrib(const struct stat *st, Attrib *a)
66 attrib_clear(a);
67 a->flags = 0;
68 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
69 a->size = st->st_size;
70 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
71 a->uid = st->st_uid;
72 a->gid = st->st_gid;
73 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
74 a->perm = st->st_mode;
75 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
76 a->atime = st->st_atime;
77 a->mtime = st->st_mtime;
80 /* Convert from filexfer attribs to struct stat */
81 void
82 attrib_to_stat(const Attrib *a, struct stat *st)
84 memset(st, 0, sizeof(*st));
86 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
87 st->st_size = a->size;
88 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
89 st->st_uid = a->uid;
90 st->st_gid = a->gid;
92 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
93 st->st_mode = a->perm;
94 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
95 st->st_atime = a->atime;
96 st->st_mtime = a->mtime;
100 /* Decode attributes in buffer */
101 Attrib *
102 decode_attrib(Buffer *b)
104 static Attrib a;
106 attrib_clear(&a);
107 a.flags = buffer_get_int(b);
108 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
109 a.size = buffer_get_int64(b);
110 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
111 a.uid = buffer_get_int(b);
112 a.gid = buffer_get_int(b);
114 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
115 a.perm = buffer_get_int(b);
116 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
117 a.atime = buffer_get_int(b);
118 a.mtime = buffer_get_int(b);
120 /* vendor-specific extensions */
121 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
122 char *type, *data;
123 int i, count;
125 count = buffer_get_int(b);
126 for (i = 0; i < count; i++) {
127 type = buffer_get_string(b, NULL);
128 data = buffer_get_string(b, NULL);
129 debug3("Got file attribute \"%s\"", type);
130 xfree(type);
131 xfree(data);
134 return &a;
137 /* Encode attributes to buffer */
138 void
139 encode_attrib(Buffer *b, const Attrib *a)
141 buffer_put_int(b, a->flags);
142 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
143 buffer_put_int64(b, a->size);
144 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
145 buffer_put_int(b, a->uid);
146 buffer_put_int(b, a->gid);
148 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
149 buffer_put_int(b, a->perm);
150 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
151 buffer_put_int(b, a->atime);
152 buffer_put_int(b, a->mtime);
156 /* Convert from SSH2_FX_ status to text error message */
157 const char *
158 fx2txt(int status)
160 switch (status) {
161 case SSH2_FX_OK:
162 return("No error");
163 case SSH2_FX_EOF:
164 return("End of file");
165 case SSH2_FX_NO_SUCH_FILE:
166 return("No such file or directory");
167 case SSH2_FX_PERMISSION_DENIED:
168 return("Permission denied");
169 case SSH2_FX_FAILURE:
170 return("Failure");
171 case SSH2_FX_BAD_MESSAGE:
172 return("Bad message");
173 case SSH2_FX_NO_CONNECTION:
174 return("No connection");
175 case SSH2_FX_CONNECTION_LOST:
176 return("Connection lost");
177 case SSH2_FX_OP_UNSUPPORTED:
178 return("Operation unsupported");
179 default:
180 return("Unknown status");
182 /* NOTREACHED */
186 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
188 char *
189 ls_file(const char *name, const struct stat *st, int remote)
191 int ulen, glen, sz = 0;
192 struct passwd *pw;
193 struct group *gr;
194 struct tm *ltime = localtime(&st->st_mtime);
195 char *user, *group;
196 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
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;
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;
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);
217 if (sz == 0)
218 tbuf[0] = '\0';
219 ulen = MAX(strlen(user), 8);
220 glen = MAX(strlen(group), 8);
221 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
222 (u_int)st->st_nlink, ulen, user, glen, group,
223 (unsigned long long)st->st_size, tbuf, name);
224 return xstrdup(buf);