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