summaryrefslogtreecommitdiff
path: root/sftp-common.c
diff options
context:
space:
mode:
Diffstat (limited to 'sftp-common.c')
-rw-r--r--sftp-common.c101
1 files changed, 62 insertions, 39 deletions
diff --git a/sftp-common.c b/sftp-common.c
index 70a929ccc..9dc1f9831 100644
--- a/sftp-common.c
+++ b/sftp-common.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sftp-common.c,v 1.26 2014/01/09 03:26:00 guenther Exp $ */ 1/* $OpenBSD: sftp-common.c,v 1.28 2015/01/20 23:14:00 deraadt Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2001 Damien Miller. All rights reserved. 4 * Copyright (c) 2001 Damien Miller. All rights reserved.
@@ -26,9 +26,9 @@
26 26
27#include "includes.h" 27#include "includes.h"
28 28
29#include <sys/param.h> /* MAX */
29#include <sys/types.h> 30#include <sys/types.h>
30#include <sys/stat.h> 31#include <sys/stat.h>
31#include <sys/param.h>
32 32
33#include <grp.h> 33#include <grp.h>
34#include <pwd.h> 34#include <pwd.h>
@@ -42,7 +42,8 @@
42#endif 42#endif
43 43
44#include "xmalloc.h" 44#include "xmalloc.h"
45#include "buffer.h" 45#include "ssherr.h"
46#include "sshbuf.h"
46#include "log.h" 47#include "log.h"
47 48
48#include "sftp.h" 49#include "sftp.h"
@@ -100,59 +101,81 @@ attrib_to_stat(const Attrib *a, struct stat *st)
100} 101}
101 102
102/* Decode attributes in buffer */ 103/* Decode attributes in buffer */
103Attrib * 104int
104decode_attrib(Buffer *b) 105decode_attrib(struct sshbuf *b, Attrib *a)
105{ 106{
106 static Attrib a; 107 int r;
107 108
108 attrib_clear(&a); 109 attrib_clear(a);
109 a.flags = buffer_get_int(b); 110 if ((r = sshbuf_get_u32(b, &a->flags)) != 0)
110 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) 111 return r;
111 a.size = buffer_get_int64(b); 112 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
112 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { 113 if ((r = sshbuf_get_u64(b, &a->size)) != 0)
113 a.uid = buffer_get_int(b); 114 return r;
114 a.gid = buffer_get_int(b); 115 }
115 } 116 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
116 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) 117 if ((r = sshbuf_get_u32(b, &a->uid)) != 0 ||
117 a.perm = buffer_get_int(b); 118 (r = sshbuf_get_u32(b, &a->gid)) != 0)
118 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 119 return r;
119 a.atime = buffer_get_int(b); 120 }
120 a.mtime = buffer_get_int(b); 121 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
122 if ((r = sshbuf_get_u32(b, &a->perm)) != 0)
123 return r;
124 }
125 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
126 if ((r = sshbuf_get_u32(b, &a->atime)) != 0 ||
127 (r = sshbuf_get_u32(b, &a->mtime)) != 0)
128 return r;
121 } 129 }
122 /* vendor-specific extensions */ 130 /* vendor-specific extensions */
123 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) { 131 if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) {
124 char *type, *data; 132 char *type;
125 int i, count; 133 u_char *data;
134 size_t dlen;
135 u_int i, count;
126 136
127 count = buffer_get_int(b); 137 if ((r = sshbuf_get_u32(b, &count)) != 0)
138 fatal("%s: buffer error: %s", __func__, ssh_err(r));
128 for (i = 0; i < count; i++) { 139 for (i = 0; i < count; i++) {
129 type = buffer_get_string(b, NULL); 140 if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
130 data = buffer_get_string(b, NULL); 141 (r = sshbuf_get_string(b, &data, &dlen)) != 0)
131 debug3("Got file attribute \"%s\"", type); 142 return r;
143 debug3("Got file attribute \"%.100s\" len %zu",
144 type, dlen);
132 free(type); 145 free(type);
133 free(data); 146 free(data);
134 } 147 }
135 } 148 }
136 return &a; 149 return 0;
137} 150}
138 151
139/* Encode attributes to buffer */ 152/* Encode attributes to buffer */
140void 153int
141encode_attrib(Buffer *b, const Attrib *a) 154encode_attrib(struct sshbuf *b, const Attrib *a)
142{ 155{
143 buffer_put_int(b, a->flags); 156 int r;
144 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) 157
145 buffer_put_int64(b, a->size); 158 if ((r = sshbuf_put_u32(b, a->flags)) != 0)
159 return r;
160 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
161 if ((r = sshbuf_put_u64(b, a->size)) != 0)
162 return r;
163 }
146 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { 164 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
147 buffer_put_int(b, a->uid); 165 if ((r = sshbuf_put_u32(b, a->uid)) != 0 ||
148 buffer_put_int(b, a->gid); 166 (r = sshbuf_put_u32(b, a->gid)) != 0)
167 return r;
168 }
169 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
170 if ((r = sshbuf_put_u32(b, a->perm)) != 0)
171 return r;
149 } 172 }
150 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
151 buffer_put_int(b, a->perm);
152 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { 173 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
153 buffer_put_int(b, a->atime); 174 if ((r = sshbuf_put_u32(b, a->atime)) != 0 ||
154 buffer_put_int(b, a->mtime); 175 (r = sshbuf_put_u32(b, a->mtime)) != 0)
176 return r;
155 } 177 }
178 return 0;
156} 179}
157 180
158/* Convert from SSH2_FX_ status to text error message */ 181/* Convert from SSH2_FX_ status to text error message */