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