Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | #include "includes.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 25 | RCSID("$OpenBSD: sftp-server.c,v 1.14 2001/01/21 19:05:56 markus Exp $"); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 26 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 27 | #include "buffer.h" |
| 28 | #include "bufaux.h" |
| 29 | #include "getput.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include "log.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 31 | #include "xmalloc.h" |
| 32 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 33 | #include "sftp.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 34 | |
| 35 | /* helper */ |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 36 | #define get_int64() buffer_get_int64(&iqueue); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 37 | #define get_int() buffer_get_int(&iqueue); |
| 38 | #define get_string(lenp) buffer_get_string(&iqueue, lenp); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 39 | #define TRACE debug |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 40 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 41 | #ifdef HAVE___PROGNAME |
| 42 | extern char *__progname; |
| 43 | #else |
| 44 | char *__progname; |
| 45 | #endif |
| 46 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 47 | /* input and output queue */ |
| 48 | Buffer iqueue; |
| 49 | Buffer oqueue; |
| 50 | |
| 51 | /* portable attibutes, etc. */ |
| 52 | |
| 53 | typedef struct Attrib Attrib; |
| 54 | typedef struct Stat Stat; |
| 55 | |
| 56 | struct Attrib |
| 57 | { |
| 58 | u_int32_t flags; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 59 | u_int64_t size; |
| 60 | u_int32_t uid; |
| 61 | u_int32_t gid; |
| 62 | u_int32_t perm; |
| 63 | u_int32_t atime; |
| 64 | u_int32_t mtime; |
| 65 | }; |
| 66 | |
| 67 | struct Stat |
| 68 | { |
| 69 | char *name; |
| 70 | char *long_name; |
| 71 | Attrib attrib; |
| 72 | }; |
| 73 | |
| 74 | int |
| 75 | errno_to_portable(int unixerrno) |
| 76 | { |
| 77 | int ret = 0; |
| 78 | switch (unixerrno) { |
| 79 | case 0: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 80 | ret = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 81 | break; |
| 82 | case ENOENT: |
| 83 | case ENOTDIR: |
| 84 | case EBADF: |
| 85 | case ELOOP: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 86 | ret = SSH2_FX_NO_SUCH_FILE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 87 | break; |
| 88 | case EPERM: |
| 89 | case EACCES: |
| 90 | case EFAULT: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 91 | ret = SSH2_FX_PERMISSION_DENIED; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 92 | break; |
| 93 | case ENAMETOOLONG: |
| 94 | case EINVAL: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 95 | ret = SSH2_FX_BAD_MESSAGE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 96 | break; |
| 97 | default: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 98 | ret = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 99 | break; |
| 100 | } |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | flags_from_portable(int pflags) |
| 106 | { |
| 107 | int flags = 0; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 108 | if (pflags & SSH2_FXF_READ && |
| 109 | pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 110 | flags = O_RDWR; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 111 | } else if (pflags & SSH2_FXF_READ) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 112 | flags = O_RDONLY; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 113 | } else if (pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 114 | flags = O_WRONLY; |
| 115 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 116 | if (pflags & SSH2_FXF_CREAT) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 117 | flags |= O_CREAT; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 118 | if (pflags & SSH2_FXF_TRUNC) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 119 | flags |= O_TRUNC; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 120 | if (pflags & SSH2_FXF_EXCL) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 121 | flags |= O_EXCL; |
| 122 | return flags; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | attrib_clear(Attrib *a) |
| 127 | { |
| 128 | a->flags = 0; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 129 | a->size = 0; |
| 130 | a->uid = 0; |
| 131 | a->gid = 0; |
| 132 | a->perm = 0; |
| 133 | a->atime = 0; |
| 134 | a->mtime = 0; |
| 135 | } |
| 136 | |
| 137 | Attrib * |
| 138 | decode_attrib(Buffer *b) |
| 139 | { |
| 140 | static Attrib a; |
| 141 | attrib_clear(&a); |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 142 | a.flags = buffer_get_int(b); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 143 | if (a.flags & SSH2_FILEXFER_ATTR_SIZE) { |
| 144 | a.size = buffer_get_int64(b); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 145 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 146 | if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 147 | a.uid = buffer_get_int(b); |
| 148 | a.gid = buffer_get_int(b); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 149 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 150 | if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 151 | a.perm = buffer_get_int(b); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 152 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 153 | if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 154 | a.atime = buffer_get_int(b); |
| 155 | a.mtime = buffer_get_int(b); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 156 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 157 | /* vendor-specific extensions */ |
| 158 | if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) { |
| 159 | char *type, *data; |
| 160 | int i, count; |
| 161 | count = buffer_get_int(b); |
| 162 | for (i = 0; i < count; i++) { |
| 163 | type = buffer_get_string(b, NULL); |
| 164 | data = buffer_get_string(b, NULL); |
| 165 | xfree(type); |
| 166 | xfree(data); |
| 167 | } |
| 168 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 169 | return &a; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | encode_attrib(Buffer *b, Attrib *a) |
| 174 | { |
| 175 | buffer_put_int(b, a->flags); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 176 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
| 177 | buffer_put_int64(b, a->size); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 178 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 179 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 180 | buffer_put_int(b, a->uid); |
| 181 | buffer_put_int(b, a->gid); |
| 182 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 183 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 184 | buffer_put_int(b, a->perm); |
| 185 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 186 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 187 | buffer_put_int(b, a->atime); |
| 188 | buffer_put_int(b, a->mtime); |
| 189 | } |
| 190 | } |
| 191 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 192 | void |
| 193 | stat_to_attrib(struct stat *st, Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 194 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 195 | attrib_clear(a); |
| 196 | a->flags = 0; |
| 197 | a->flags |= SSH2_FILEXFER_ATTR_SIZE; |
| 198 | a->size = st->st_size; |
| 199 | a->flags |= SSH2_FILEXFER_ATTR_UIDGID; |
| 200 | a->uid = st->st_uid; |
| 201 | a->gid = st->st_gid; |
| 202 | a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 203 | a->perm = st->st_mode; |
| 204 | a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME; |
| 205 | a->atime = st->st_atime; |
| 206 | a->mtime = st->st_mtime; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | Attrib * |
| 210 | get_attrib(void) |
| 211 | { |
| 212 | return decode_attrib(&iqueue); |
| 213 | } |
| 214 | |
| 215 | /* handle handles */ |
| 216 | |
| 217 | typedef struct Handle Handle; |
| 218 | struct Handle { |
| 219 | int use; |
| 220 | DIR *dirp; |
| 221 | int fd; |
| 222 | char *name; |
| 223 | }; |
| 224 | enum { |
| 225 | HANDLE_UNUSED, |
| 226 | HANDLE_DIR, |
| 227 | HANDLE_FILE |
| 228 | }; |
| 229 | Handle handles[100]; |
| 230 | |
| 231 | void |
| 232 | handle_init(void) |
| 233 | { |
| 234 | int i; |
| 235 | for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
| 236 | handles[i].use = HANDLE_UNUSED; |
| 237 | } |
| 238 | |
| 239 | int |
| 240 | handle_new(int use, char *name, int fd, DIR *dirp) |
| 241 | { |
| 242 | int i; |
| 243 | for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) { |
| 244 | if (handles[i].use == HANDLE_UNUSED) { |
| 245 | handles[i].use = use; |
| 246 | handles[i].dirp = dirp; |
| 247 | handles[i].fd = fd; |
| 248 | handles[i].name = name; |
| 249 | return i; |
| 250 | } |
| 251 | } |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | handle_is_ok(int i, int type) |
| 257 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 258 | return i >= 0 && i < sizeof(handles)/sizeof(Handle) && |
| 259 | handles[i].use == type; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | int |
| 263 | handle_to_string(int handle, char **stringp, int *hlenp) |
| 264 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 265 | if (stringp == NULL || hlenp == NULL) |
| 266 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 267 | *stringp = xmalloc(sizeof(int32_t)); |
| 268 | PUT_32BIT(*stringp, handle); |
| 269 | *hlenp = sizeof(int32_t); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 274 | handle_from_string(char *handle, u_int hlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 275 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 276 | int val; |
| 277 | if (hlen != sizeof(int32_t)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 278 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 279 | val = GET_32BIT(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 280 | if (handle_is_ok(val, HANDLE_FILE) || |
| 281 | handle_is_ok(val, HANDLE_DIR)) |
| 282 | return val; |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | char * |
| 287 | handle_to_name(int handle) |
| 288 | { |
| 289 | if (handle_is_ok(handle, HANDLE_DIR)|| |
| 290 | handle_is_ok(handle, HANDLE_FILE)) |
| 291 | return handles[handle].name; |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | DIR * |
| 296 | handle_to_dir(int handle) |
| 297 | { |
| 298 | if (handle_is_ok(handle, HANDLE_DIR)) |
| 299 | return handles[handle].dirp; |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | int |
| 304 | handle_to_fd(int handle) |
| 305 | { |
| 306 | if (handle_is_ok(handle, HANDLE_FILE)) |
| 307 | return handles[handle].fd; |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | int |
| 312 | handle_close(int handle) |
| 313 | { |
| 314 | int ret = -1; |
| 315 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 316 | ret = close(handles[handle].fd); |
| 317 | handles[handle].use = HANDLE_UNUSED; |
| 318 | } else if (handle_is_ok(handle, HANDLE_DIR)) { |
| 319 | ret = closedir(handles[handle].dirp); |
| 320 | handles[handle].use = HANDLE_UNUSED; |
| 321 | } else { |
| 322 | errno = ENOENT; |
| 323 | } |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | int |
| 328 | get_handle(void) |
| 329 | { |
| 330 | char *handle; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 331 | int val = -1; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 332 | u_int hlen; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 333 | handle = get_string(&hlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 334 | if (hlen < 256) |
| 335 | val = handle_from_string(handle, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 336 | xfree(handle); |
| 337 | return val; |
| 338 | } |
| 339 | |
| 340 | /* send replies */ |
| 341 | |
| 342 | void |
| 343 | send_msg(Buffer *m) |
| 344 | { |
| 345 | int mlen = buffer_len(m); |
| 346 | buffer_put_int(&oqueue, mlen); |
| 347 | buffer_append(&oqueue, buffer_ptr(m), mlen); |
| 348 | buffer_consume(m, mlen); |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | send_status(u_int32_t id, u_int32_t error) |
| 353 | { |
| 354 | Buffer msg; |
| 355 | TRACE("sent status id %d error %d", id, error); |
| 356 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 357 | buffer_put_char(&msg, SSH2_FXP_STATUS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 358 | buffer_put_int(&msg, id); |
| 359 | buffer_put_int(&msg, error); |
| 360 | send_msg(&msg); |
| 361 | buffer_free(&msg); |
| 362 | } |
| 363 | void |
| 364 | send_data_or_handle(char type, u_int32_t id, char *data, int dlen) |
| 365 | { |
| 366 | Buffer msg; |
| 367 | buffer_init(&msg); |
| 368 | buffer_put_char(&msg, type); |
| 369 | buffer_put_int(&msg, id); |
| 370 | buffer_put_string(&msg, data, dlen); |
| 371 | send_msg(&msg); |
| 372 | buffer_free(&msg); |
| 373 | } |
| 374 | |
| 375 | void |
| 376 | send_data(u_int32_t id, char *data, int dlen) |
| 377 | { |
| 378 | TRACE("sent data id %d len %d", id, dlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 379 | send_data_or_handle(SSH2_FXP_DATA, id, data, dlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void |
| 383 | send_handle(u_int32_t id, int handle) |
| 384 | { |
| 385 | char *string; |
| 386 | int hlen; |
| 387 | handle_to_string(handle, &string, &hlen); |
| 388 | TRACE("sent handle id %d handle %d", id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 389 | send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 390 | xfree(string); |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | send_names(u_int32_t id, int count, Stat *stats) |
| 395 | { |
| 396 | Buffer msg; |
| 397 | int i; |
| 398 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 399 | buffer_put_char(&msg, SSH2_FXP_NAME); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 400 | buffer_put_int(&msg, id); |
| 401 | buffer_put_int(&msg, count); |
| 402 | TRACE("sent names id %d count %d", id, count); |
| 403 | for (i = 0; i < count; i++) { |
| 404 | buffer_put_cstring(&msg, stats[i].name); |
| 405 | buffer_put_cstring(&msg, stats[i].long_name); |
| 406 | encode_attrib(&msg, &stats[i].attrib); |
| 407 | } |
| 408 | send_msg(&msg); |
| 409 | buffer_free(&msg); |
| 410 | } |
| 411 | |
| 412 | void |
| 413 | send_attrib(u_int32_t id, Attrib *a) |
| 414 | { |
| 415 | Buffer msg; |
| 416 | TRACE("sent attrib id %d have 0x%x", id, a->flags); |
| 417 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 418 | buffer_put_char(&msg, SSH2_FXP_ATTRS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 419 | buffer_put_int(&msg, id); |
| 420 | encode_attrib(&msg, a); |
| 421 | send_msg(&msg); |
| 422 | buffer_free(&msg); |
| 423 | } |
| 424 | |
| 425 | /* parse incoming */ |
| 426 | |
| 427 | void |
| 428 | process_init(void) |
| 429 | { |
| 430 | Buffer msg; |
| 431 | int version = buffer_get_int(&iqueue); |
| 432 | |
| 433 | TRACE("client version %d", version); |
| 434 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 435 | buffer_put_char(&msg, SSH2_FXP_VERSION); |
| 436 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 437 | send_msg(&msg); |
| 438 | buffer_free(&msg); |
| 439 | } |
| 440 | |
| 441 | void |
| 442 | process_open(void) |
| 443 | { |
| 444 | u_int32_t id, pflags; |
| 445 | Attrib *a; |
| 446 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 447 | int handle, fd, flags, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 448 | |
| 449 | id = get_int(); |
| 450 | name = get_string(NULL); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 451 | pflags = get_int(); /* portable flags */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 452 | a = get_attrib(); |
| 453 | flags = flags_from_portable(pflags); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 454 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 455 | TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode); |
| 456 | fd = open(name, flags, mode); |
| 457 | if (fd < 0) { |
| 458 | status = errno_to_portable(errno); |
| 459 | } else { |
| 460 | handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL); |
| 461 | if (handle < 0) { |
| 462 | close(fd); |
| 463 | } else { |
| 464 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 465 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 466 | } |
| 467 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 468 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 469 | send_status(id, status); |
| 470 | xfree(name); |
| 471 | } |
| 472 | |
| 473 | void |
| 474 | process_close(void) |
| 475 | { |
| 476 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 477 | int handle, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 478 | |
| 479 | id = get_int(); |
| 480 | handle = get_handle(); |
| 481 | TRACE("close id %d handle %d", id, handle); |
| 482 | ret = handle_close(handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 483 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 484 | send_status(id, status); |
| 485 | } |
| 486 | |
| 487 | void |
| 488 | process_read(void) |
| 489 | { |
| 490 | char buf[64*1024]; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 491 | u_int32_t id, len; |
| 492 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 493 | u_int64_t off; |
| 494 | |
| 495 | id = get_int(); |
| 496 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 497 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 498 | len = get_int(); |
| 499 | |
Damien Miller | f056e23 | 2000-09-23 14:58:32 +1100 | [diff] [blame] | 500 | TRACE("read id %d handle %d off %lld len %d", id, handle, off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 501 | if (len > sizeof buf) { |
| 502 | len = sizeof buf; |
| 503 | log("read change len %d", len); |
| 504 | } |
| 505 | fd = handle_to_fd(handle); |
| 506 | if (fd >= 0) { |
| 507 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 508 | error("process_read: seek failed"); |
| 509 | status = errno_to_portable(errno); |
| 510 | } else { |
| 511 | ret = read(fd, buf, len); |
| 512 | if (ret < 0) { |
| 513 | status = errno_to_portable(errno); |
| 514 | } else if (ret == 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 515 | status = SSH2_FX_EOF; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 516 | } else { |
| 517 | send_data(id, buf, ret); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 518 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 522 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 523 | send_status(id, status); |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | process_write(void) |
| 528 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 529 | u_int32_t id; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 530 | u_int64_t off; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 531 | u_int len; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 532 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 533 | char *data; |
| 534 | |
| 535 | id = get_int(); |
| 536 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 537 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 538 | data = get_string(&len); |
| 539 | |
Damien Miller | f056e23 | 2000-09-23 14:58:32 +1100 | [diff] [blame] | 540 | TRACE("write id %d handle %d off %lld len %d", id, handle, off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 541 | fd = handle_to_fd(handle); |
| 542 | if (fd >= 0) { |
| 543 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 544 | status = errno_to_portable(errno); |
| 545 | error("process_write: seek failed"); |
| 546 | } else { |
| 547 | /* XXX ATOMICIO ? */ |
| 548 | ret = write(fd, data, len); |
| 549 | if (ret == -1) { |
| 550 | error("process_write: write failed"); |
| 551 | status = errno_to_portable(errno); |
| 552 | } else if (ret == len) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 553 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 554 | } else { |
| 555 | log("nothing at all written"); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | send_status(id, status); |
| 560 | xfree(data); |
| 561 | } |
| 562 | |
| 563 | void |
| 564 | process_do_stat(int do_lstat) |
| 565 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 566 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 567 | struct stat st; |
| 568 | u_int32_t id; |
| 569 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 570 | int ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 571 | |
| 572 | id = get_int(); |
| 573 | name = get_string(NULL); |
| 574 | TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name); |
| 575 | ret = do_lstat ? lstat(name, &st) : stat(name, &st); |
| 576 | if (ret < 0) { |
| 577 | status = errno_to_portable(errno); |
| 578 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 579 | stat_to_attrib(&st, &a); |
| 580 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 581 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 582 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 583 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 584 | send_status(id, status); |
| 585 | xfree(name); |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | process_stat(void) |
| 590 | { |
| 591 | process_do_stat(0); |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | process_lstat(void) |
| 596 | { |
| 597 | process_do_stat(1); |
| 598 | } |
| 599 | |
| 600 | void |
| 601 | process_fstat(void) |
| 602 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 603 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 604 | struct stat st; |
| 605 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 606 | int fd, ret, handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 607 | |
| 608 | id = get_int(); |
| 609 | handle = get_handle(); |
| 610 | TRACE("fstat id %d handle %d", id, handle); |
| 611 | fd = handle_to_fd(handle); |
| 612 | if (fd >= 0) { |
| 613 | ret = fstat(fd, &st); |
| 614 | if (ret < 0) { |
| 615 | status = errno_to_portable(errno); |
| 616 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 617 | stat_to_attrib(&st, &a); |
| 618 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 619 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 620 | } |
| 621 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 622 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 623 | send_status(id, status); |
| 624 | } |
| 625 | |
| 626 | struct timeval * |
| 627 | attrib_to_tv(Attrib *a) |
| 628 | { |
| 629 | static struct timeval tv[2]; |
| 630 | tv[0].tv_sec = a->atime; |
| 631 | tv[0].tv_usec = 0; |
| 632 | tv[1].tv_sec = a->mtime; |
| 633 | tv[1].tv_usec = 0; |
| 634 | return tv; |
| 635 | } |
| 636 | |
| 637 | void |
| 638 | process_setstat(void) |
| 639 | { |
| 640 | Attrib *a; |
| 641 | u_int32_t id; |
| 642 | char *name; |
| 643 | int ret; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 644 | int status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 645 | |
| 646 | id = get_int(); |
| 647 | name = get_string(NULL); |
| 648 | a = get_attrib(); |
| 649 | TRACE("setstat id %d name %s", id, name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 650 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 651 | ret = chmod(name, a->perm & 0777); |
| 652 | if (ret == -1) |
| 653 | status = errno_to_portable(errno); |
| 654 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 655 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 656 | ret = utimes(name, attrib_to_tv(a)); |
| 657 | if (ret == -1) |
| 658 | status = errno_to_portable(errno); |
| 659 | } |
| 660 | send_status(id, status); |
| 661 | xfree(name); |
| 662 | } |
| 663 | |
| 664 | void |
| 665 | process_fsetstat(void) |
| 666 | { |
| 667 | Attrib *a; |
| 668 | u_int32_t id; |
| 669 | int handle, fd, ret; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 670 | int status = SSH2_FX_OK; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 671 | char *name; |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 672 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 673 | id = get_int(); |
| 674 | handle = get_handle(); |
| 675 | a = get_attrib(); |
| 676 | TRACE("fsetstat id %d handle %d", id, handle); |
| 677 | fd = handle_to_fd(handle); |
| 678 | name = handle_to_name(handle); |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 679 | if (fd < 0 || name == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 680 | status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 681 | } else { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 682 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 683 | #ifdef HAVE_FCHMOD |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 684 | ret = fchmod(fd, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 685 | #else |
Kevin Steves | b6b37ba | 2001-01-24 20:01:44 +0000 | [diff] [blame] | 686 | ret = chmod(name, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 687 | #endif |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 688 | if (ret == -1) |
| 689 | status = errno_to_portable(errno); |
| 690 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 691 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 692 | #ifdef HAVE_FUTIMES |
| 693 | ret = futimes(fd, attrib_to_tv(a)); |
| 694 | #else |
| 695 | ret = utimes(name, attrib_to_tv(a)); |
| 696 | #endif |
| 697 | if (ret == -1) |
| 698 | status = errno_to_portable(errno); |
| 699 | } |
| 700 | } |
| 701 | send_status(id, status); |
| 702 | } |
| 703 | |
| 704 | void |
| 705 | process_opendir(void) |
| 706 | { |
| 707 | DIR *dirp = NULL; |
| 708 | char *path; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 709 | int handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 710 | u_int32_t id; |
| 711 | |
| 712 | id = get_int(); |
| 713 | path = get_string(NULL); |
| 714 | TRACE("opendir id %d path %s", id, path); |
| 715 | dirp = opendir(path); |
| 716 | if (dirp == NULL) { |
| 717 | status = errno_to_portable(errno); |
| 718 | } else { |
| 719 | handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp); |
| 720 | if (handle < 0) { |
| 721 | closedir(dirp); |
| 722 | } else { |
| 723 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 724 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 728 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 729 | send_status(id, status); |
| 730 | xfree(path); |
| 731 | } |
| 732 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 733 | /* |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 734 | * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 735 | */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 736 | char * |
| 737 | ls_file(char *name, struct stat *st) |
| 738 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 739 | int sz = 0; |
| 740 | struct passwd *pw; |
| 741 | struct group *gr; |
| 742 | struct tm *ltime = localtime(&st->st_mtime); |
| 743 | char *user, *group; |
| 744 | char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; |
| 745 | |
| 746 | strmode(st->st_mode, mode); |
| 747 | if ((pw = getpwuid(st->st_uid)) != NULL) { |
| 748 | user = pw->pw_name; |
| 749 | } else { |
| 750 | snprintf(ubuf, sizeof ubuf, "%d", st->st_uid); |
| 751 | user = ubuf; |
| 752 | } |
| 753 | if ((gr = getgrgid(st->st_gid)) != NULL) { |
| 754 | group = gr->gr_name; |
| 755 | } else { |
| 756 | snprintf(gbuf, sizeof gbuf, "%d", st->st_gid); |
| 757 | group = gbuf; |
| 758 | } |
| 759 | if (ltime != NULL) { |
| 760 | if (time(NULL) - st->st_mtime < (365*24*60*60)/2) |
| 761 | sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); |
| 762 | else |
| 763 | sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); |
| 764 | } |
| 765 | if (sz == 0) |
| 766 | tbuf[0] = '\0'; |
Kevin Steves | b71eb58 | 2001-01-29 16:57:27 +0000 | [diff] [blame] | 767 | snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8lld %s %s", mode, |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 768 | st->st_nlink, user, group, (long long)st->st_size, tbuf, name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 769 | return xstrdup(buf); |
| 770 | } |
| 771 | |
| 772 | void |
| 773 | process_readdir(void) |
| 774 | { |
| 775 | DIR *dirp; |
| 776 | struct dirent *dp; |
| 777 | char *path; |
| 778 | int handle; |
| 779 | u_int32_t id; |
| 780 | |
| 781 | id = get_int(); |
| 782 | handle = get_handle(); |
| 783 | TRACE("readdir id %d handle %d", id, handle); |
| 784 | dirp = handle_to_dir(handle); |
| 785 | path = handle_to_name(handle); |
| 786 | if (dirp == NULL || path == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 787 | send_status(id, SSH2_FX_FAILURE); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 788 | } else { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 789 | struct stat st; |
| 790 | char pathname[1024]; |
| 791 | Stat *stats; |
| 792 | int nstats = 10, count = 0, i; |
| 793 | stats = xmalloc(nstats * sizeof(Stat)); |
| 794 | while ((dp = readdir(dirp)) != NULL) { |
| 795 | if (count >= nstats) { |
| 796 | nstats *= 2; |
| 797 | stats = xrealloc(stats, nstats * sizeof(Stat)); |
| 798 | } |
| 799 | /* XXX OVERFLOW ? */ |
| 800 | snprintf(pathname, sizeof pathname, |
| 801 | "%s/%s", path, dp->d_name); |
| 802 | if (lstat(pathname, &st) < 0) |
| 803 | continue; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 804 | stat_to_attrib(&st, &(stats[count].attrib)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 805 | stats[count].name = xstrdup(dp->d_name); |
| 806 | stats[count].long_name = ls_file(dp->d_name, &st); |
| 807 | count++; |
| 808 | /* send up to 100 entries in one message */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 809 | /* XXX check packet size instead */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 810 | if (count == 100) |
| 811 | break; |
| 812 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 813 | if (count > 0) { |
| 814 | send_names(id, count, stats); |
| 815 | for(i = 0; i < count; i++) { |
| 816 | xfree(stats[i].name); |
| 817 | xfree(stats[i].long_name); |
| 818 | } |
| 819 | } else { |
| 820 | send_status(id, SSH2_FX_EOF); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 821 | } |
| 822 | xfree(stats); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | void |
| 827 | process_remove(void) |
| 828 | { |
| 829 | char *name; |
| 830 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 831 | int status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 832 | int ret; |
| 833 | |
| 834 | id = get_int(); |
| 835 | name = get_string(NULL); |
| 836 | TRACE("remove id %d name %s", id, name); |
Kevin Steves | a074feb | 2000-12-21 22:33:45 +0000 | [diff] [blame] | 837 | ret = unlink(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 838 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 839 | send_status(id, status); |
| 840 | xfree(name); |
| 841 | } |
| 842 | |
| 843 | void |
| 844 | process_mkdir(void) |
| 845 | { |
| 846 | Attrib *a; |
| 847 | u_int32_t id; |
| 848 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 849 | int ret, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 850 | |
| 851 | id = get_int(); |
| 852 | name = get_string(NULL); |
| 853 | a = get_attrib(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 854 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? |
| 855 | a->perm & 0777 : 0777; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 856 | TRACE("mkdir id %d name %s mode 0%o", id, name, mode); |
| 857 | ret = mkdir(name, mode); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 858 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 859 | send_status(id, status); |
| 860 | xfree(name); |
| 861 | } |
| 862 | |
| 863 | void |
| 864 | process_rmdir(void) |
| 865 | { |
| 866 | u_int32_t id; |
| 867 | char *name; |
| 868 | int ret, status; |
| 869 | |
| 870 | id = get_int(); |
| 871 | name = get_string(NULL); |
| 872 | TRACE("rmdir id %d name %s", id, name); |
| 873 | ret = rmdir(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 874 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 875 | send_status(id, status); |
| 876 | xfree(name); |
| 877 | } |
| 878 | |
| 879 | void |
| 880 | process_realpath(void) |
| 881 | { |
| 882 | char resolvedname[MAXPATHLEN]; |
| 883 | u_int32_t id; |
| 884 | char *path; |
| 885 | |
| 886 | id = get_int(); |
| 887 | path = get_string(NULL); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 888 | if (path[0] == '\0') { |
| 889 | xfree(path); |
| 890 | path = xstrdup("."); |
| 891 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 892 | TRACE("realpath id %d path %s", id, path); |
| 893 | if (realpath(path, resolvedname) == NULL) { |
| 894 | send_status(id, errno_to_portable(errno)); |
| 895 | } else { |
| 896 | Stat s; |
| 897 | attrib_clear(&s.attrib); |
| 898 | s.name = s.long_name = resolvedname; |
| 899 | send_names(id, 1, &s); |
| 900 | } |
| 901 | xfree(path); |
| 902 | } |
| 903 | |
| 904 | void |
| 905 | process_rename(void) |
| 906 | { |
| 907 | u_int32_t id; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 908 | struct stat st; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 909 | char *oldpath, *newpath; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 910 | int ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 911 | |
| 912 | id = get_int(); |
| 913 | oldpath = get_string(NULL); |
| 914 | newpath = get_string(NULL); |
| 915 | TRACE("rename id %d old %s new %s", id, oldpath, newpath); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 916 | /* fail if 'newpath' exists */ |
| 917 | if (stat(newpath, &st) == -1) { |
| 918 | ret = rename(oldpath, newpath); |
| 919 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
| 920 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 921 | send_status(id, status); |
| 922 | xfree(oldpath); |
| 923 | xfree(newpath); |
| 924 | } |
| 925 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 926 | void |
| 927 | process_extended(void) |
| 928 | { |
| 929 | u_int32_t id; |
| 930 | char *request; |
| 931 | |
| 932 | id = get_int(); |
| 933 | request = get_string(NULL); |
| 934 | send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */ |
| 935 | xfree(request); |
| 936 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 937 | |
| 938 | /* stolen from ssh-agent */ |
| 939 | |
| 940 | void |
| 941 | process(void) |
| 942 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 943 | u_int msg_len; |
| 944 | u_int type; |
| 945 | u_char *cp; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 946 | |
| 947 | if (buffer_len(&iqueue) < 5) |
| 948 | return; /* Incomplete message. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 949 | cp = (u_char *) buffer_ptr(&iqueue); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 950 | msg_len = GET_32BIT(cp); |
| 951 | if (msg_len > 256 * 1024) { |
| 952 | error("bad message "); |
| 953 | exit(11); |
| 954 | } |
| 955 | if (buffer_len(&iqueue) < msg_len + 4) |
| 956 | return; |
| 957 | buffer_consume(&iqueue, 4); |
| 958 | type = buffer_get_char(&iqueue); |
| 959 | switch (type) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 960 | case SSH2_FXP_INIT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 961 | process_init(); |
| 962 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 963 | case SSH2_FXP_OPEN: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 964 | process_open(); |
| 965 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 966 | case SSH2_FXP_CLOSE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 967 | process_close(); |
| 968 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 969 | case SSH2_FXP_READ: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 970 | process_read(); |
| 971 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 972 | case SSH2_FXP_WRITE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 973 | process_write(); |
| 974 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 975 | case SSH2_FXP_LSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 976 | process_lstat(); |
| 977 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 978 | case SSH2_FXP_FSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 979 | process_fstat(); |
| 980 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 981 | case SSH2_FXP_SETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 982 | process_setstat(); |
| 983 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 984 | case SSH2_FXP_FSETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 985 | process_fsetstat(); |
| 986 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 987 | case SSH2_FXP_OPENDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 988 | process_opendir(); |
| 989 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 990 | case SSH2_FXP_READDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 991 | process_readdir(); |
| 992 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 993 | case SSH2_FXP_REMOVE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 994 | process_remove(); |
| 995 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 996 | case SSH2_FXP_MKDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 997 | process_mkdir(); |
| 998 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 999 | case SSH2_FXP_RMDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1000 | process_rmdir(); |
| 1001 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1002 | case SSH2_FXP_REALPATH: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1003 | process_realpath(); |
| 1004 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1005 | case SSH2_FXP_STAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1006 | process_stat(); |
| 1007 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1008 | case SSH2_FXP_RENAME: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1009 | process_rename(); |
| 1010 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1011 | case SSH2_FXP_EXTENDED: |
| 1012 | process_extended(); |
| 1013 | break; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1014 | default: |
| 1015 | error("Unknown message %d", type); |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | int |
| 1021 | main(int ac, char **av) |
| 1022 | { |
| 1023 | fd_set rset, wset; |
| 1024 | int in, out, max; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 1025 | ssize_t len, olen; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1026 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 1027 | __progname = get_progname(av[0]); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1028 | handle_init(); |
| 1029 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1030 | #ifdef DEBUG_SFTP_SERVER |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1031 | log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1032 | #endif |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1033 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1034 | in = dup(STDIN_FILENO); |
| 1035 | out = dup(STDOUT_FILENO); |
| 1036 | |
| 1037 | max = 0; |
| 1038 | if (in > max) |
| 1039 | max = in; |
| 1040 | if (out > max) |
| 1041 | max = out; |
| 1042 | |
| 1043 | buffer_init(&iqueue); |
| 1044 | buffer_init(&oqueue); |
| 1045 | |
| 1046 | for (;;) { |
| 1047 | FD_ZERO(&rset); |
| 1048 | FD_ZERO(&wset); |
| 1049 | |
| 1050 | FD_SET(in, &rset); |
| 1051 | olen = buffer_len(&oqueue); |
| 1052 | if (olen > 0) |
| 1053 | FD_SET(out, &wset); |
| 1054 | |
| 1055 | if (select(max+1, &rset, &wset, NULL, NULL) < 0) { |
| 1056 | if (errno == EINTR) |
| 1057 | continue; |
| 1058 | exit(2); |
| 1059 | } |
| 1060 | |
| 1061 | /* copy stdin to iqueue */ |
| 1062 | if (FD_ISSET(in, &rset)) { |
| 1063 | char buf[4*4096]; |
| 1064 | len = read(in, buf, sizeof buf); |
| 1065 | if (len == 0) { |
| 1066 | debug("read eof"); |
| 1067 | exit(0); |
| 1068 | } else if (len < 0) { |
| 1069 | error("read error"); |
| 1070 | exit(1); |
| 1071 | } else { |
| 1072 | buffer_append(&iqueue, buf, len); |
| 1073 | } |
| 1074 | } |
| 1075 | /* send oqueue to stdout */ |
| 1076 | if (FD_ISSET(out, &wset)) { |
| 1077 | len = write(out, buffer_ptr(&oqueue), olen); |
| 1078 | if (len < 0) { |
| 1079 | error("write error"); |
| 1080 | exit(1); |
| 1081 | } else { |
| 1082 | buffer_consume(&oqueue, len); |
| 1083 | } |
| 1084 | } |
| 1085 | /* process requests from client */ |
| 1086 | process(); |
| 1087 | } |
| 1088 | } |