blob: 6bed0ab8a06d1f26c6af458df4e9a3b9885e3a7f [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Ben Lindstromb1f483f2002-06-23 21:27:18 +000027RCSID("$OpenBSD: sftp-common.c,v 1.6 2002/06/23 09:30:14 deraadt Exp $");
Damien Miller33804262001-02-04 23:20:18 +110028
29#include "buffer.h"
30#include "bufaux.h"
Damien Miller33804262001-02-04 23:20:18 +110031#include "log.h"
32#include "xmalloc.h"
33
34#include "sftp.h"
35#include "sftp-common.h"
36
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000037/* Clear contents of attributes structure */
Damien Miller33804262001-02-04 23:20:18 +110038void
39attrib_clear(Attrib *a)
40{
41 a->flags = 0;
42 a->size = 0;
43 a->uid = 0;
44 a->gid = 0;
45 a->perm = 0;
46 a->atime = 0;
47 a->mtime = 0;
48}
49
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000050/* Convert from struct stat to filexfer attribs */
Damien Miller33804262001-02-04 23:20:18 +110051void
52stat_to_attrib(struct stat *st, Attrib *a)
53{
54 attrib_clear(a);
55 a->flags = 0;
56 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
57 a->size = st->st_size;
58 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
59 a->uid = st->st_uid;
60 a->gid = st->st_gid;
61 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
62 a->perm = st->st_mode;
63 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
64 a->atime = st->st_atime;
65 a->mtime = st->st_mtime;
66}
67
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000068/* Decode attributes in buffer */
Damien Miller33804262001-02-04 23:20:18 +110069Attrib *
70decode_attrib(Buffer *b)
71{
72 static Attrib a;
Ben Lindstromb1f483f2002-06-23 21:27:18 +000073
Damien Miller33804262001-02-04 23:20:18 +110074 attrib_clear(&a);
75 a.flags = buffer_get_int(b);
76 if (a.flags & SSH2_FILEXFER_ATTR_SIZE)
77 a.size = buffer_get_int64(b);
78 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
79 a.uid = buffer_get_int(b);
80 a.gid = buffer_get_int(b);
81 }
82 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
83 a.perm = buffer_get_int(b);
84 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
85 a.atime = buffer_get_int(b);
86 a.mtime = buffer_get_int(b);
87 }
88 /* vendor-specific extensions */
89 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
90 char *type, *data;
91 int i, count;
Ben Lindstromb1f483f2002-06-23 21:27:18 +000092
Damien Miller33804262001-02-04 23:20:18 +110093 count = buffer_get_int(b);
94 for (i = 0; i < count; i++) {
95 type = buffer_get_string(b, NULL);
96 data = buffer_get_string(b, NULL);
97 debug3("Got file attribute \"%s\"", type);
98 xfree(type);
99 xfree(data);
100 }
101 }
102 return &a;
103}
104
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000105/* Encode attributes to buffer */
Damien Miller33804262001-02-04 23:20:18 +1100106void
107encode_attrib(Buffer *b, Attrib *a)
108{
109 buffer_put_int(b, a->flags);
110 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
111 buffer_put_int64(b, a->size);
112 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
113 buffer_put_int(b, a->uid);
114 buffer_put_int(b, a->gid);
115 }
116 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
117 buffer_put_int(b, a->perm);
118 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
119 buffer_put_int(b, a->atime);
120 buffer_put_int(b, a->mtime);
121 }
122}
123
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000124/* Convert from SSH2_FX_ status to text error message */
Damien Miller33804262001-02-04 23:20:18 +1100125const char *
126fx2txt(int status)
127{
128 switch (status) {
129 case SSH2_FX_OK:
Damien Millerd7686fd2001-02-10 00:40:03 +1100130 return("No error");
Damien Miller33804262001-02-04 23:20:18 +1100131 case SSH2_FX_EOF:
Damien Millerd7686fd2001-02-10 00:40:03 +1100132 return("End of file");
Damien Miller33804262001-02-04 23:20:18 +1100133 case SSH2_FX_NO_SUCH_FILE:
Damien Millerd7686fd2001-02-10 00:40:03 +1100134 return("No such file or directory");
Damien Miller33804262001-02-04 23:20:18 +1100135 case SSH2_FX_PERMISSION_DENIED:
Damien Millerd7686fd2001-02-10 00:40:03 +1100136 return("Permission denied");
Damien Miller33804262001-02-04 23:20:18 +1100137 case SSH2_FX_FAILURE:
138 return("Failure");
139 case SSH2_FX_BAD_MESSAGE:
140 return("Bad message");
141 case SSH2_FX_NO_CONNECTION:
142 return("No connection");
143 case SSH2_FX_CONNECTION_LOST:
144 return("Connection lost");
145 case SSH2_FX_OP_UNSUPPORTED:
146 return("Operation unsupported");
147 default:
148 return("Unknown status");
Ben Lindstromd05487d2001-12-06 17:50:03 +0000149 }
Damien Miller33804262001-02-04 23:20:18 +1100150 /* NOTREACHED */
151}