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