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