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