Damien Miller | 35e18db | 2007-09-17 16:11:33 +1000 | [diff] [blame] | 1 | /* $OpenBSD: sftp-server.c,v 1.74 2007/09/13 04:39:04 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 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 17 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 18 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 19 | |
| 20 | #include <sys/types.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 21 | #include <sys/param.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 22 | #include <sys/stat.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 23 | #ifdef HAVE_SYS_TIME_H |
| 24 | # include <sys/time.h> |
| 25 | #endif |
Damien Miller | 88f254b | 2006-03-15 11:25:13 +1100 | [diff] [blame] | 26 | |
| 27 | #include <dirent.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 28 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 29 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 30 | #include <pwd.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 31 | #include <stdlib.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 32 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 33 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include <pwd.h> |
Damien Miller | 5598b4f | 2006-07-24 14:09:40 +1000 | [diff] [blame] | 35 | #include <time.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 36 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <stdarg.h> |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 38 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 39 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include "buffer.h" |
| 41 | #include "log.h" |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 42 | #include "misc.h" |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 43 | #include "uidswap.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 44 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 45 | #include "sftp.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 46 | #include "sftp-common.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 47 | |
| 48 | /* helper */ |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 49 | #define get_int64() buffer_get_int64(&iqueue); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 50 | #define get_int() buffer_get_int(&iqueue); |
| 51 | #define get_string(lenp) buffer_get_string(&iqueue, lenp); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 52 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 53 | /* Our verbosity */ |
| 54 | LogLevel log_level = SYSLOG_LEVEL_ERROR; |
| 55 | |
| 56 | /* Our client */ |
| 57 | struct passwd *pw = NULL; |
| 58 | char *client_addr = NULL; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 59 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 60 | /* input and output queue */ |
| 61 | Buffer iqueue; |
| 62 | Buffer oqueue; |
| 63 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 64 | /* Version of client */ |
| 65 | int version; |
| 66 | |
Darren Tucker | a6612d4 | 2003-06-28 12:39:03 +1000 | [diff] [blame] | 67 | /* portable attributes, etc. */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 68 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 69 | typedef struct Stat Stat; |
| 70 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 71 | struct Stat { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 72 | char *name; |
| 73 | char *long_name; |
| 74 | Attrib attrib; |
| 75 | }; |
| 76 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 77 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 78 | errno_to_portable(int unixerrno) |
| 79 | { |
| 80 | int ret = 0; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 81 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 82 | switch (unixerrno) { |
| 83 | case 0: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 84 | ret = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 85 | break; |
| 86 | case ENOENT: |
| 87 | case ENOTDIR: |
| 88 | case EBADF: |
| 89 | case ELOOP: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 90 | ret = SSH2_FX_NO_SUCH_FILE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 91 | break; |
| 92 | case EPERM: |
| 93 | case EACCES: |
| 94 | case EFAULT: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 95 | ret = SSH2_FX_PERMISSION_DENIED; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 96 | break; |
| 97 | case ENAMETOOLONG: |
| 98 | case EINVAL: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 99 | ret = SSH2_FX_BAD_MESSAGE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 100 | break; |
| 101 | default: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 102 | ret = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | return ret; |
| 106 | } |
| 107 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 108 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 109 | flags_from_portable(int pflags) |
| 110 | { |
| 111 | int flags = 0; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 112 | |
Ben Lindstrom | 3659251 | 2001-03-05 05:02:08 +0000 | [diff] [blame] | 113 | if ((pflags & SSH2_FXF_READ) && |
| 114 | (pflags & SSH2_FXF_WRITE)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 115 | flags = O_RDWR; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 116 | } else if (pflags & SSH2_FXF_READ) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 117 | flags = O_RDONLY; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 118 | } else if (pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 119 | flags = O_WRONLY; |
| 120 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 121 | if (pflags & SSH2_FXF_CREAT) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 122 | flags |= O_CREAT; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 123 | if (pflags & SSH2_FXF_TRUNC) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 124 | flags |= O_TRUNC; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 125 | if (pflags & SSH2_FXF_EXCL) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 126 | flags |= O_EXCL; |
| 127 | return flags; |
| 128 | } |
| 129 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 130 | static const char * |
| 131 | string_from_portable(int pflags) |
| 132 | { |
| 133 | static char ret[128]; |
| 134 | |
| 135 | *ret = '\0'; |
| 136 | |
| 137 | #define PAPPEND(str) { \ |
| 138 | if (*ret != '\0') \ |
| 139 | strlcat(ret, ",", sizeof(ret)); \ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 140 | strlcat(ret, str, sizeof(ret)); \ |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (pflags & SSH2_FXF_READ) |
| 144 | PAPPEND("READ") |
| 145 | if (pflags & SSH2_FXF_WRITE) |
| 146 | PAPPEND("WRITE") |
| 147 | if (pflags & SSH2_FXF_CREAT) |
| 148 | PAPPEND("CREATE") |
| 149 | if (pflags & SSH2_FXF_TRUNC) |
| 150 | PAPPEND("TRUNCATE") |
| 151 | if (pflags & SSH2_FXF_EXCL) |
| 152 | PAPPEND("EXCL") |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 157 | static Attrib * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 158 | get_attrib(void) |
| 159 | { |
| 160 | return decode_attrib(&iqueue); |
| 161 | } |
| 162 | |
| 163 | /* handle handles */ |
| 164 | |
| 165 | typedef struct Handle Handle; |
| 166 | struct Handle { |
| 167 | int use; |
| 168 | DIR *dirp; |
| 169 | int fd; |
| 170 | char *name; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 171 | u_int64_t bytes_read, bytes_write; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 172 | }; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 173 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 174 | enum { |
| 175 | HANDLE_UNUSED, |
| 176 | HANDLE_DIR, |
| 177 | HANDLE_FILE |
| 178 | }; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 179 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 180 | Handle handles[100]; |
| 181 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 182 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 183 | handle_init(void) |
| 184 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 185 | u_int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 186 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 187 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 188 | handles[i].use = HANDLE_UNUSED; |
| 189 | } |
| 190 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 191 | static int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 192 | handle_new(int use, const char *name, int fd, DIR *dirp) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 193 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 194 | u_int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 195 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 196 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 197 | if (handles[i].use == HANDLE_UNUSED) { |
| 198 | handles[i].use = use; |
| 199 | handles[i].dirp = dirp; |
| 200 | handles[i].fd = fd; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 201 | handles[i].name = xstrdup(name); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 202 | handles[i].bytes_read = handles[i].bytes_write = 0; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 203 | return i; |
| 204 | } |
| 205 | } |
| 206 | return -1; |
| 207 | } |
| 208 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 209 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 210 | handle_is_ok(int i, int type) |
| 211 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 212 | return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) && |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 213 | handles[i].use == type; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 214 | } |
| 215 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 216 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 217 | handle_to_string(int handle, char **stringp, int *hlenp) |
| 218 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 219 | if (stringp == NULL || hlenp == NULL) |
| 220 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 221 | *stringp = xmalloc(sizeof(int32_t)); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 222 | put_u32(*stringp, handle); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 223 | *hlenp = sizeof(int32_t); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 227 | static int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 228 | handle_from_string(const char *handle, u_int hlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 229 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 230 | int val; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 231 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 232 | if (hlen != sizeof(int32_t)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 233 | return -1; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 234 | val = get_u32(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 235 | if (handle_is_ok(val, HANDLE_FILE) || |
| 236 | handle_is_ok(val, HANDLE_DIR)) |
| 237 | return val; |
| 238 | return -1; |
| 239 | } |
| 240 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 241 | static char * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 242 | handle_to_name(int handle) |
| 243 | { |
| 244 | if (handle_is_ok(handle, HANDLE_DIR)|| |
| 245 | handle_is_ok(handle, HANDLE_FILE)) |
| 246 | return handles[handle].name; |
| 247 | return NULL; |
| 248 | } |
| 249 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 250 | static DIR * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 251 | handle_to_dir(int handle) |
| 252 | { |
| 253 | if (handle_is_ok(handle, HANDLE_DIR)) |
| 254 | return handles[handle].dirp; |
| 255 | return NULL; |
| 256 | } |
| 257 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 258 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 259 | handle_to_fd(int handle) |
| 260 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 261 | if (handle_is_ok(handle, HANDLE_FILE)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 262 | return handles[handle].fd; |
| 263 | return -1; |
| 264 | } |
| 265 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 266 | static void |
| 267 | handle_update_read(int handle, ssize_t bytes) |
| 268 | { |
| 269 | if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0) |
| 270 | handles[handle].bytes_read += bytes; |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | handle_update_write(int handle, ssize_t bytes) |
| 275 | { |
| 276 | if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0) |
| 277 | handles[handle].bytes_write += bytes; |
| 278 | } |
| 279 | |
| 280 | static u_int64_t |
| 281 | handle_bytes_read(int handle) |
| 282 | { |
| 283 | if (handle_is_ok(handle, HANDLE_FILE)) |
| 284 | return (handles[handle].bytes_read); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static u_int64_t |
| 289 | handle_bytes_write(int handle) |
| 290 | { |
| 291 | if (handle_is_ok(handle, HANDLE_FILE)) |
| 292 | return (handles[handle].bytes_write); |
| 293 | return 0; |
| 294 | } |
| 295 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 296 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 297 | handle_close(int handle) |
| 298 | { |
| 299 | int ret = -1; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 300 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 301 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 302 | ret = close(handles[handle].fd); |
| 303 | handles[handle].use = HANDLE_UNUSED; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 304 | xfree(handles[handle].name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 305 | } else if (handle_is_ok(handle, HANDLE_DIR)) { |
| 306 | ret = closedir(handles[handle].dirp); |
| 307 | handles[handle].use = HANDLE_UNUSED; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 308 | xfree(handles[handle].name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 309 | } else { |
| 310 | errno = ENOENT; |
| 311 | } |
| 312 | return ret; |
| 313 | } |
| 314 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 315 | static void |
| 316 | handle_log_close(int handle, char *emsg) |
| 317 | { |
| 318 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 319 | logit("%s%sclose \"%s\" bytes read %llu written %llu", |
| 320 | emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ", |
| 321 | handle_to_name(handle), |
Darren Tucker | 86473c5 | 2007-05-20 14:59:32 +1000 | [diff] [blame] | 322 | (unsigned long long)handle_bytes_read(handle), |
| 323 | (unsigned long long)handle_bytes_write(handle)); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 324 | } else { |
| 325 | logit("%s%sclosedir \"%s\"", |
| 326 | emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ", |
| 327 | handle_to_name(handle)); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | handle_log_exit(void) |
| 333 | { |
| 334 | u_int i; |
| 335 | |
| 336 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
| 337 | if (handles[i].use != HANDLE_UNUSED) |
| 338 | handle_log_close(i, "forced"); |
| 339 | } |
| 340 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 341 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 342 | get_handle(void) |
| 343 | { |
| 344 | char *handle; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 345 | int val = -1; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 346 | u_int hlen; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 347 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 348 | handle = get_string(&hlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 349 | if (hlen < 256) |
| 350 | val = handle_from_string(handle, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 351 | xfree(handle); |
| 352 | return val; |
| 353 | } |
| 354 | |
| 355 | /* send replies */ |
| 356 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 357 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 358 | send_msg(Buffer *m) |
| 359 | { |
| 360 | int mlen = buffer_len(m); |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 361 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 362 | buffer_put_int(&oqueue, mlen); |
| 363 | buffer_append(&oqueue, buffer_ptr(m), mlen); |
| 364 | buffer_consume(m, mlen); |
| 365 | } |
| 366 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 367 | static const char * |
| 368 | status_to_message(u_int32_t status) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 369 | { |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 370 | const char *status_messages[] = { |
| 371 | "Success", /* SSH_FX_OK */ |
| 372 | "End of file", /* SSH_FX_EOF */ |
| 373 | "No such file", /* SSH_FX_NO_SUCH_FILE */ |
| 374 | "Permission denied", /* SSH_FX_PERMISSION_DENIED */ |
| 375 | "Failure", /* SSH_FX_FAILURE */ |
| 376 | "Bad message", /* SSH_FX_BAD_MESSAGE */ |
| 377 | "No connection", /* SSH_FX_NO_CONNECTION */ |
| 378 | "Connection lost", /* SSH_FX_CONNECTION_LOST */ |
| 379 | "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */ |
| 380 | "Unknown error" /* Others */ |
| 381 | }; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 382 | return (status_messages[MIN(status,SSH2_FX_MAX)]); |
| 383 | } |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 384 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 385 | static void |
| 386 | send_status(u_int32_t id, u_int32_t status) |
| 387 | { |
| 388 | Buffer msg; |
| 389 | |
| 390 | debug3("request %u: sent status %u", id, status); |
| 391 | if (log_level > SYSLOG_LEVEL_VERBOSE || |
| 392 | (status != SSH2_FX_OK && status != SSH2_FX_EOF)) |
| 393 | logit("sent status %s", status_to_message(status)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 394 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 395 | buffer_put_char(&msg, SSH2_FXP_STATUS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 396 | buffer_put_int(&msg, id); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 397 | buffer_put_int(&msg, status); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 398 | if (version >= 3) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 399 | buffer_put_cstring(&msg, status_to_message(status)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 400 | buffer_put_cstring(&msg, ""); |
| 401 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 402 | send_msg(&msg); |
| 403 | buffer_free(&msg); |
| 404 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 405 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 406 | 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] | 407 | { |
| 408 | Buffer msg; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 409 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 410 | buffer_init(&msg); |
| 411 | buffer_put_char(&msg, type); |
| 412 | buffer_put_int(&msg, id); |
| 413 | buffer_put_string(&msg, data, dlen); |
| 414 | send_msg(&msg); |
| 415 | buffer_free(&msg); |
| 416 | } |
| 417 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 418 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 419 | send_data(u_int32_t id, const char *data, int dlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 420 | { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 421 | debug("request %u: sent data len %d", id, dlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 422 | send_data_or_handle(SSH2_FXP_DATA, id, data, dlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 423 | } |
| 424 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 425 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 426 | send_handle(u_int32_t id, int handle) |
| 427 | { |
| 428 | char *string; |
| 429 | int hlen; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 430 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 431 | handle_to_string(handle, &string, &hlen); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 432 | debug("request %u: sent handle handle %d", id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 433 | send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 434 | xfree(string); |
| 435 | } |
| 436 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 437 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 438 | send_names(u_int32_t id, int count, const Stat *stats) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 439 | { |
| 440 | Buffer msg; |
| 441 | int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 442 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 443 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 444 | buffer_put_char(&msg, SSH2_FXP_NAME); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 445 | buffer_put_int(&msg, id); |
| 446 | buffer_put_int(&msg, count); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 447 | debug("request %u: sent names count %d", id, count); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 448 | for (i = 0; i < count; i++) { |
| 449 | buffer_put_cstring(&msg, stats[i].name); |
| 450 | buffer_put_cstring(&msg, stats[i].long_name); |
| 451 | encode_attrib(&msg, &stats[i].attrib); |
| 452 | } |
| 453 | send_msg(&msg); |
| 454 | buffer_free(&msg); |
| 455 | } |
| 456 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 457 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 458 | send_attrib(u_int32_t id, const Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 459 | { |
| 460 | Buffer msg; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 461 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 462 | debug("request %u: sent attrib have 0x%x", id, a->flags); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 463 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 464 | buffer_put_char(&msg, SSH2_FXP_ATTRS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 465 | buffer_put_int(&msg, id); |
| 466 | encode_attrib(&msg, a); |
| 467 | send_msg(&msg); |
| 468 | buffer_free(&msg); |
| 469 | } |
| 470 | |
| 471 | /* parse incoming */ |
| 472 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 473 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 474 | process_init(void) |
| 475 | { |
| 476 | Buffer msg; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 477 | |
Ben Lindstrom | 937df1d | 2002-06-06 21:58:35 +0000 | [diff] [blame] | 478 | version = get_int(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 479 | verbose("received client version %d", version); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 480 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 481 | buffer_put_char(&msg, SSH2_FXP_VERSION); |
| 482 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 483 | send_msg(&msg); |
| 484 | buffer_free(&msg); |
| 485 | } |
| 486 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 487 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 488 | process_open(void) |
| 489 | { |
| 490 | u_int32_t id, pflags; |
| 491 | Attrib *a; |
| 492 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 493 | int handle, fd, flags, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 494 | |
| 495 | id = get_int(); |
| 496 | name = get_string(NULL); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 497 | pflags = get_int(); /* portable flags */ |
Damien Miller | 6444fe9 | 2006-07-10 21:31:27 +1000 | [diff] [blame] | 498 | debug3("request %u: open flags %d", id, pflags); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 499 | a = get_attrib(); |
| 500 | flags = flags_from_portable(pflags); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 501 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 502 | logit("open \"%s\" flags %s mode 0%o", |
| 503 | name, string_from_portable(pflags), mode); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 504 | fd = open(name, flags, mode); |
| 505 | if (fd < 0) { |
| 506 | status = errno_to_portable(errno); |
| 507 | } else { |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 508 | handle = handle_new(HANDLE_FILE, name, fd, NULL); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 509 | if (handle < 0) { |
| 510 | close(fd); |
| 511 | } else { |
| 512 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 513 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 514 | } |
| 515 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 516 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 517 | send_status(id, status); |
| 518 | xfree(name); |
| 519 | } |
| 520 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 521 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 522 | process_close(void) |
| 523 | { |
| 524 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 525 | int handle, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 526 | |
| 527 | id = get_int(); |
| 528 | handle = get_handle(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 529 | debug3("request %u: close handle %u", id, handle); |
| 530 | handle_log_close(handle, NULL); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 531 | ret = handle_close(handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 532 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 533 | send_status(id, status); |
| 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_read(void) |
| 538 | { |
| 539 | char buf[64*1024]; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 540 | u_int32_t id, len; |
| 541 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 542 | u_int64_t off; |
| 543 | |
| 544 | id = get_int(); |
| 545 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 546 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 547 | len = get_int(); |
| 548 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 549 | debug("request %u: read \"%s\" (handle %d) off %llu len %d", |
| 550 | id, handle_to_name(handle), handle, (unsigned long long)off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 551 | if (len > sizeof buf) { |
| 552 | len = sizeof buf; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 553 | debug2("read change len %d", len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 554 | } |
| 555 | fd = handle_to_fd(handle); |
| 556 | if (fd >= 0) { |
| 557 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 558 | error("process_read: seek failed"); |
| 559 | status = errno_to_portable(errno); |
| 560 | } else { |
| 561 | ret = read(fd, buf, len); |
| 562 | if (ret < 0) { |
| 563 | status = errno_to_portable(errno); |
| 564 | } else if (ret == 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 565 | status = SSH2_FX_EOF; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 566 | } else { |
| 567 | send_data(id, buf, ret); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 568 | status = SSH2_FX_OK; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 569 | handle_update_read(handle, ret); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 573 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 574 | send_status(id, status); |
| 575 | } |
| 576 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 577 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 578 | process_write(void) |
| 579 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 580 | u_int32_t id; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 581 | u_int64_t off; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 582 | u_int len; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 583 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 584 | char *data; |
| 585 | |
| 586 | id = get_int(); |
| 587 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 588 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 589 | data = get_string(&len); |
| 590 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 591 | debug("request %u: write \"%s\" (handle %d) off %llu len %d", |
| 592 | id, handle_to_name(handle), handle, (unsigned long long)off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 593 | fd = handle_to_fd(handle); |
| 594 | if (fd >= 0) { |
| 595 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 596 | status = errno_to_portable(errno); |
| 597 | error("process_write: seek failed"); |
| 598 | } else { |
| 599 | /* XXX ATOMICIO ? */ |
| 600 | ret = write(fd, data, len); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 601 | if (ret < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 602 | error("process_write: write failed"); |
| 603 | status = errno_to_portable(errno); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 604 | } else if ((size_t)ret == len) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 605 | status = SSH2_FX_OK; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 606 | handle_update_write(handle, ret); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 607 | } else { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 608 | debug2("nothing at all written"); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
| 612 | send_status(id, status); |
| 613 | xfree(data); |
| 614 | } |
| 615 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 616 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 617 | process_do_stat(int do_lstat) |
| 618 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 619 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 620 | struct stat st; |
| 621 | u_int32_t id; |
| 622 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 623 | int ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 624 | |
| 625 | id = get_int(); |
| 626 | name = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 627 | debug3("request %u: %sstat", id, do_lstat ? "l" : ""); |
| 628 | verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 629 | ret = do_lstat ? lstat(name, &st) : stat(name, &st); |
| 630 | if (ret < 0) { |
| 631 | status = errno_to_portable(errno); |
| 632 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 633 | stat_to_attrib(&st, &a); |
| 634 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 635 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 636 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 637 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 638 | send_status(id, status); |
| 639 | xfree(name); |
| 640 | } |
| 641 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 642 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 643 | process_stat(void) |
| 644 | { |
| 645 | process_do_stat(0); |
| 646 | } |
| 647 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 648 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 649 | process_lstat(void) |
| 650 | { |
| 651 | process_do_stat(1); |
| 652 | } |
| 653 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 654 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 655 | process_fstat(void) |
| 656 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 657 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 658 | struct stat st; |
| 659 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 660 | int fd, ret, handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 661 | |
| 662 | id = get_int(); |
| 663 | handle = get_handle(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 664 | debug("request %u: fstat \"%s\" (handle %u)", |
| 665 | id, handle_to_name(handle), handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 666 | fd = handle_to_fd(handle); |
Damien Miller | e2334d6 | 2007-01-05 16:31:02 +1100 | [diff] [blame] | 667 | if (fd >= 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 668 | ret = fstat(fd, &st); |
| 669 | if (ret < 0) { |
| 670 | status = errno_to_portable(errno); |
| 671 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 672 | stat_to_attrib(&st, &a); |
| 673 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 674 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 675 | } |
| 676 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 677 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 678 | send_status(id, status); |
| 679 | } |
| 680 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 681 | static struct timeval * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 682 | attrib_to_tv(const Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 683 | { |
| 684 | static struct timeval tv[2]; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 685 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 686 | tv[0].tv_sec = a->atime; |
| 687 | tv[0].tv_usec = 0; |
| 688 | tv[1].tv_sec = a->mtime; |
| 689 | tv[1].tv_usec = 0; |
| 690 | return tv; |
| 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_setstat(void) |
| 695 | { |
| 696 | Attrib *a; |
| 697 | u_int32_t id; |
| 698 | char *name; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 699 | int status = SSH2_FX_OK, ret; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 700 | |
| 701 | id = get_int(); |
| 702 | name = get_string(NULL); |
| 703 | a = get_attrib(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 704 | debug("request %u: setstat name \"%s\"", id, name); |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 705 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
Darren Tucker | 86473c5 | 2007-05-20 14:59:32 +1000 | [diff] [blame] | 706 | logit("set \"%s\" size %llu", |
| 707 | name, (unsigned long long)a->size); |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 708 | ret = truncate(name, a->size); |
| 709 | if (ret == -1) |
| 710 | status = errno_to_portable(errno); |
| 711 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 712 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 713 | logit("set \"%s\" mode %04o", name, a->perm); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 714 | ret = chmod(name, a->perm & 0777); |
| 715 | if (ret == -1) |
| 716 | status = errno_to_portable(errno); |
| 717 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 718 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 719 | char buf[64]; |
| 720 | time_t t = a->mtime; |
| 721 | |
| 722 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 723 | localtime(&t)); |
| 724 | logit("set \"%s\" modtime %s", name, buf); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 725 | ret = utimes(name, attrib_to_tv(a)); |
| 726 | if (ret == -1) |
| 727 | status = errno_to_portable(errno); |
| 728 | } |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 729 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 730 | logit("set \"%s\" owner %lu group %lu", name, |
| 731 | (u_long)a->uid, (u_long)a->gid); |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 732 | ret = chown(name, a->uid, a->gid); |
| 733 | if (ret == -1) |
| 734 | status = errno_to_portable(errno); |
| 735 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 736 | send_status(id, status); |
| 737 | xfree(name); |
| 738 | } |
| 739 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 740 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 741 | process_fsetstat(void) |
| 742 | { |
| 743 | Attrib *a; |
| 744 | u_int32_t id; |
| 745 | int handle, fd, ret; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 746 | int status = SSH2_FX_OK; |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 747 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 748 | id = get_int(); |
| 749 | handle = get_handle(); |
| 750 | a = get_attrib(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 751 | debug("request %u: fsetstat handle %d", id, handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 752 | fd = handle_to_fd(handle); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 753 | if (fd < 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 754 | status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 755 | } else { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 756 | char *name = handle_to_name(handle); |
| 757 | |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 758 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
Darren Tucker | 86473c5 | 2007-05-20 14:59:32 +1000 | [diff] [blame] | 759 | logit("set \"%s\" size %llu", |
| 760 | name, (unsigned long long)a->size); |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 761 | ret = ftruncate(fd, a->size); |
| 762 | if (ret == -1) |
| 763 | status = errno_to_portable(errno); |
| 764 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 765 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 766 | logit("set \"%s\" mode %04o", name, a->perm); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 767 | #ifdef HAVE_FCHMOD |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 768 | ret = fchmod(fd, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 769 | #else |
Kevin Steves | b6b37ba | 2001-01-24 20:01:44 +0000 | [diff] [blame] | 770 | ret = chmod(name, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 771 | #endif |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 772 | if (ret == -1) |
| 773 | status = errno_to_portable(errno); |
| 774 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 775 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 776 | char buf[64]; |
| 777 | time_t t = a->mtime; |
| 778 | |
| 779 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 780 | localtime(&t)); |
| 781 | logit("set \"%s\" modtime %s", name, buf); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 782 | #ifdef HAVE_FUTIMES |
| 783 | ret = futimes(fd, attrib_to_tv(a)); |
| 784 | #else |
| 785 | ret = utimes(name, attrib_to_tv(a)); |
| 786 | #endif |
| 787 | if (ret == -1) |
| 788 | status = errno_to_portable(errno); |
| 789 | } |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 790 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 791 | logit("set \"%s\" owner %lu group %lu", name, |
| 792 | (u_long)a->uid, (u_long)a->gid); |
Ben Lindstrom | 34bb0c7 | 2001-02-13 02:40:56 +0000 | [diff] [blame] | 793 | #ifdef HAVE_FCHOWN |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 794 | ret = fchown(fd, a->uid, a->gid); |
Ben Lindstrom | 34bb0c7 | 2001-02-13 02:40:56 +0000 | [diff] [blame] | 795 | #else |
| 796 | ret = chown(name, a->uid, a->gid); |
| 797 | #endif |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 798 | if (ret == -1) |
| 799 | status = errno_to_portable(errno); |
| 800 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 801 | } |
| 802 | send_status(id, status); |
| 803 | } |
| 804 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 805 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 806 | process_opendir(void) |
| 807 | { |
| 808 | DIR *dirp = NULL; |
| 809 | char *path; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 810 | int handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 811 | u_int32_t id; |
| 812 | |
| 813 | id = get_int(); |
| 814 | path = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 815 | debug3("request %u: opendir", id); |
| 816 | logit("opendir \"%s\"", path); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 817 | dirp = opendir(path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 818 | if (dirp == NULL) { |
| 819 | status = errno_to_portable(errno); |
| 820 | } else { |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 821 | handle = handle_new(HANDLE_DIR, path, 0, dirp); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 822 | if (handle < 0) { |
| 823 | closedir(dirp); |
| 824 | } else { |
| 825 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 826 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 827 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 828 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 829 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 830 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 831 | send_status(id, status); |
| 832 | xfree(path); |
| 833 | } |
| 834 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 835 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 836 | process_readdir(void) |
| 837 | { |
| 838 | DIR *dirp; |
| 839 | struct dirent *dp; |
| 840 | char *path; |
| 841 | int handle; |
| 842 | u_int32_t id; |
| 843 | |
| 844 | id = get_int(); |
| 845 | handle = get_handle(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 846 | debug("request %u: readdir \"%s\" (handle %d)", id, |
| 847 | handle_to_name(handle), handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 848 | dirp = handle_to_dir(handle); |
| 849 | path = handle_to_name(handle); |
| 850 | if (dirp == NULL || path == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 851 | send_status(id, SSH2_FX_FAILURE); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 852 | } else { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 853 | struct stat st; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 854 | char pathname[MAXPATHLEN]; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 855 | Stat *stats; |
| 856 | int nstats = 10, count = 0, i; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 857 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 858 | stats = xcalloc(nstats, sizeof(Stat)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 859 | while ((dp = readdir(dirp)) != NULL) { |
| 860 | if (count >= nstats) { |
| 861 | nstats *= 2; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 862 | stats = xrealloc(stats, nstats, sizeof(Stat)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 863 | } |
| 864 | /* XXX OVERFLOW ? */ |
Ben Lindstrom | 95148e3 | 2001-08-06 21:30:53 +0000 | [diff] [blame] | 865 | snprintf(pathname, sizeof pathname, "%s%s%s", path, |
| 866 | strcmp(path, "/") ? "/" : "", dp->d_name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 867 | if (lstat(pathname, &st) < 0) |
| 868 | continue; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 869 | stat_to_attrib(&st, &(stats[count].attrib)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 870 | stats[count].name = xstrdup(dp->d_name); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 871 | stats[count].long_name = ls_file(dp->d_name, &st, 0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 872 | count++; |
| 873 | /* send up to 100 entries in one message */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 874 | /* XXX check packet size instead */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 875 | if (count == 100) |
| 876 | break; |
| 877 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 878 | if (count > 0) { |
| 879 | send_names(id, count, stats); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 880 | for (i = 0; i < count; i++) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 881 | xfree(stats[i].name); |
| 882 | xfree(stats[i].long_name); |
| 883 | } |
| 884 | } else { |
| 885 | send_status(id, SSH2_FX_EOF); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 886 | } |
| 887 | xfree(stats); |
| 888 | } |
| 889 | } |
| 890 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 891 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 892 | process_remove(void) |
| 893 | { |
| 894 | char *name; |
| 895 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 896 | int status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 897 | int ret; |
| 898 | |
| 899 | id = get_int(); |
| 900 | name = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 901 | debug3("request %u: remove", id); |
| 902 | logit("remove name \"%s\"", name); |
Kevin Steves | a074feb | 2000-12-21 22:33:45 +0000 | [diff] [blame] | 903 | ret = unlink(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 904 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 905 | send_status(id, status); |
| 906 | xfree(name); |
| 907 | } |
| 908 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 909 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 910 | process_mkdir(void) |
| 911 | { |
| 912 | Attrib *a; |
| 913 | u_int32_t id; |
| 914 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 915 | int ret, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 916 | |
| 917 | id = get_int(); |
| 918 | name = get_string(NULL); |
| 919 | a = get_attrib(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 920 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? |
| 921 | a->perm & 0777 : 0777; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 922 | debug3("request %u: mkdir", id); |
| 923 | logit("mkdir name \"%s\" mode 0%o", name, mode); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 924 | ret = mkdir(name, mode); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 925 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 926 | send_status(id, status); |
| 927 | xfree(name); |
| 928 | } |
| 929 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 930 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 931 | process_rmdir(void) |
| 932 | { |
| 933 | u_int32_t id; |
| 934 | char *name; |
| 935 | int ret, status; |
| 936 | |
| 937 | id = get_int(); |
| 938 | name = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 939 | debug3("request %u: rmdir", id); |
| 940 | logit("rmdir name \"%s\"", name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 941 | ret = rmdir(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 942 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 943 | send_status(id, status); |
| 944 | xfree(name); |
| 945 | } |
| 946 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 947 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 948 | process_realpath(void) |
| 949 | { |
| 950 | char resolvedname[MAXPATHLEN]; |
| 951 | u_int32_t id; |
| 952 | char *path; |
| 953 | |
| 954 | id = get_int(); |
| 955 | path = get_string(NULL); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 956 | if (path[0] == '\0') { |
| 957 | xfree(path); |
| 958 | path = xstrdup("."); |
| 959 | } |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 960 | debug3("request %u: realpath", id); |
| 961 | verbose("realpath \"%s\"", path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 962 | if (realpath(path, resolvedname) == NULL) { |
| 963 | send_status(id, errno_to_portable(errno)); |
| 964 | } else { |
| 965 | Stat s; |
| 966 | attrib_clear(&s.attrib); |
| 967 | s.name = s.long_name = resolvedname; |
| 968 | send_names(id, 1, &s); |
| 969 | } |
| 970 | xfree(path); |
| 971 | } |
| 972 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 973 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 974 | process_rename(void) |
| 975 | { |
| 976 | u_int32_t id; |
| 977 | char *oldpath, *newpath; |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 978 | int status; |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 979 | struct stat sb; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 980 | |
| 981 | id = get_int(); |
| 982 | oldpath = get_string(NULL); |
| 983 | newpath = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 984 | debug3("request %u: rename", id); |
| 985 | logit("rename old \"%s\" new \"%s\"", oldpath, newpath); |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 986 | status = SSH2_FX_FAILURE; |
| 987 | if (lstat(oldpath, &sb) == -1) |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 988 | status = errno_to_portable(errno); |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 989 | else if (S_ISREG(sb.st_mode)) { |
| 990 | /* Race-free rename of regular files */ |
Darren Tucker | aedc1d6 | 2004-06-25 17:06:02 +1000 | [diff] [blame] | 991 | if (link(oldpath, newpath) == -1) { |
Darren Tucker | e59b508 | 2004-06-28 16:01:19 +1000 | [diff] [blame] | 992 | if (errno == EOPNOTSUPP |
| 993 | #ifdef LINK_OPNOTSUPP_ERRNO |
| 994 | || errno == LINK_OPNOTSUPP_ERRNO |
| 995 | #endif |
| 996 | ) { |
Darren Tucker | aedc1d6 | 2004-06-25 17:06:02 +1000 | [diff] [blame] | 997 | struct stat st; |
| 998 | |
| 999 | /* |
| 1000 | * fs doesn't support links, so fall back to |
| 1001 | * stat+rename. This is racy. |
| 1002 | */ |
| 1003 | if (stat(newpath, &st) == -1) { |
| 1004 | if (rename(oldpath, newpath) == -1) |
| 1005 | status = |
| 1006 | errno_to_portable(errno); |
| 1007 | else |
| 1008 | status = SSH2_FX_OK; |
| 1009 | } |
| 1010 | } else { |
| 1011 | status = errno_to_portable(errno); |
| 1012 | } |
| 1013 | } else if (unlink(oldpath) == -1) { |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 1014 | status = errno_to_portable(errno); |
| 1015 | /* clean spare link */ |
| 1016 | unlink(newpath); |
| 1017 | } else |
| 1018 | status = SSH2_FX_OK; |
| 1019 | } else if (stat(newpath, &sb) == -1) { |
| 1020 | if (rename(oldpath, newpath) == -1) |
| 1021 | status = errno_to_portable(errno); |
| 1022 | else |
| 1023 | status = SSH2_FX_OK; |
| 1024 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1025 | send_status(id, status); |
| 1026 | xfree(oldpath); |
| 1027 | xfree(newpath); |
| 1028 | } |
| 1029 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1030 | static void |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1031 | process_readlink(void) |
| 1032 | { |
| 1033 | u_int32_t id; |
Ben Lindstrom | abbb73d | 2001-05-17 03:14:57 +0000 | [diff] [blame] | 1034 | int len; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1035 | char buf[MAXPATHLEN]; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1036 | char *path; |
| 1037 | |
| 1038 | id = get_int(); |
| 1039 | path = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1040 | debug3("request %u: readlink", id); |
| 1041 | verbose("readlink \"%s\"", path); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1042 | if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1043 | send_status(id, errno_to_portable(errno)); |
| 1044 | else { |
| 1045 | Stat s; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1046 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1047 | buf[len] = '\0'; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1048 | attrib_clear(&s.attrib); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1049 | s.name = s.long_name = buf; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1050 | send_names(id, 1, &s); |
| 1051 | } |
| 1052 | xfree(path); |
| 1053 | } |
| 1054 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1055 | static void |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1056 | process_symlink(void) |
| 1057 | { |
| 1058 | u_int32_t id; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1059 | char *oldpath, *newpath; |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 1060 | int ret, status; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1061 | |
| 1062 | id = get_int(); |
| 1063 | oldpath = get_string(NULL); |
| 1064 | newpath = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1065 | debug3("request %u: symlink", id); |
| 1066 | logit("symlink old \"%s\" new \"%s\"", oldpath, newpath); |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 1067 | /* this will fail if 'newpath' exists */ |
| 1068 | ret = symlink(oldpath, newpath); |
| 1069 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1070 | send_status(id, status); |
| 1071 | xfree(oldpath); |
| 1072 | xfree(newpath); |
| 1073 | } |
| 1074 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1075 | static void |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1076 | process_extended(void) |
| 1077 | { |
| 1078 | u_int32_t id; |
| 1079 | char *request; |
| 1080 | |
| 1081 | id = get_int(); |
| 1082 | request = get_string(NULL); |
| 1083 | send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */ |
| 1084 | xfree(request); |
| 1085 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1086 | |
| 1087 | /* stolen from ssh-agent */ |
| 1088 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1089 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1090 | process(void) |
| 1091 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1092 | u_int msg_len; |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1093 | u_int buf_len; |
| 1094 | u_int consumed; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1095 | u_int type; |
| 1096 | u_char *cp; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1097 | |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1098 | buf_len = buffer_len(&iqueue); |
| 1099 | if (buf_len < 5) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1100 | return; /* Incomplete message. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 1101 | cp = buffer_ptr(&iqueue); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1102 | msg_len = get_u32(cp); |
Damien Miller | 5444618 | 2006-01-02 23:40:50 +1100 | [diff] [blame] | 1103 | if (msg_len > SFTP_MAX_MSG_LENGTH) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1104 | error("bad message from %s local user %s", |
| 1105 | client_addr, pw->pw_name); |
| 1106 | cleanup_exit(11); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1107 | } |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1108 | if (buf_len < msg_len + 4) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1109 | return; |
| 1110 | buffer_consume(&iqueue, 4); |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1111 | buf_len -= 4; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1112 | type = buffer_get_char(&iqueue); |
| 1113 | switch (type) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1114 | case SSH2_FXP_INIT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1115 | process_init(); |
| 1116 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1117 | case SSH2_FXP_OPEN: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1118 | process_open(); |
| 1119 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1120 | case SSH2_FXP_CLOSE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1121 | process_close(); |
| 1122 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1123 | case SSH2_FXP_READ: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1124 | process_read(); |
| 1125 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1126 | case SSH2_FXP_WRITE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1127 | process_write(); |
| 1128 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1129 | case SSH2_FXP_LSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1130 | process_lstat(); |
| 1131 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1132 | case SSH2_FXP_FSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1133 | process_fstat(); |
| 1134 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1135 | case SSH2_FXP_SETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1136 | process_setstat(); |
| 1137 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1138 | case SSH2_FXP_FSETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1139 | process_fsetstat(); |
| 1140 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1141 | case SSH2_FXP_OPENDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1142 | process_opendir(); |
| 1143 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1144 | case SSH2_FXP_READDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1145 | process_readdir(); |
| 1146 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1147 | case SSH2_FXP_REMOVE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1148 | process_remove(); |
| 1149 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1150 | case SSH2_FXP_MKDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1151 | process_mkdir(); |
| 1152 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1153 | case SSH2_FXP_RMDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1154 | process_rmdir(); |
| 1155 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1156 | case SSH2_FXP_REALPATH: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1157 | process_realpath(); |
| 1158 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1159 | case SSH2_FXP_STAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1160 | process_stat(); |
| 1161 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1162 | case SSH2_FXP_RENAME: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1163 | process_rename(); |
| 1164 | break; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1165 | case SSH2_FXP_READLINK: |
| 1166 | process_readlink(); |
| 1167 | break; |
| 1168 | case SSH2_FXP_SYMLINK: |
| 1169 | process_symlink(); |
| 1170 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1171 | case SSH2_FXP_EXTENDED: |
| 1172 | process_extended(); |
| 1173 | break; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1174 | default: |
| 1175 | error("Unknown message %d", type); |
| 1176 | break; |
| 1177 | } |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1178 | /* discard the remaining bytes from the current packet */ |
| 1179 | if (buf_len < buffer_len(&iqueue)) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1180 | fatal("iqueue grew unexpectedly"); |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1181 | consumed = buf_len - buffer_len(&iqueue); |
| 1182 | if (msg_len < consumed) |
| 1183 | fatal("msg_len %d < consumed %d", msg_len, consumed); |
| 1184 | if (msg_len > consumed) |
| 1185 | buffer_consume(&iqueue, msg_len - consumed); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1186 | } |
| 1187 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1188 | /* Cleanup handler that logs active handles upon normal exit */ |
| 1189 | void |
| 1190 | cleanup_exit(int i) |
| 1191 | { |
| 1192 | if (pw != NULL && client_addr != NULL) { |
| 1193 | handle_log_exit(); |
| 1194 | logit("session closed for local user %s from [%s]", |
| 1195 | pw->pw_name, client_addr); |
| 1196 | } |
| 1197 | _exit(i); |
| 1198 | } |
| 1199 | |
| 1200 | static void |
| 1201 | usage(void) |
| 1202 | { |
| 1203 | extern char *__progname; |
| 1204 | |
| 1205 | fprintf(stderr, |
| 1206 | "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname); |
| 1207 | exit(1); |
| 1208 | } |
| 1209 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1210 | int |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1211 | main(int argc, char **argv) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1212 | { |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1213 | fd_set *rset, *wset; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1214 | int in, out, max, ch, skipargs = 0, log_stderr = 0; |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1215 | ssize_t len, olen, set_size; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1216 | SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1217 | char *cp, buf[4*4096]; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1218 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1219 | extern char *optarg; |
| 1220 | extern char *__progname; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1221 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1222 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1223 | sanitise_stdfd(); |
| 1224 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1225 | __progname = ssh_get_progname(argv[0]); |
| 1226 | log_init(__progname, log_level, log_facility, log_stderr); |
Ben Lindstrom | c7f4ccd | 2001-03-15 00:09:15 +0000 | [diff] [blame] | 1227 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1228 | while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) { |
| 1229 | switch (ch) { |
| 1230 | case 'c': |
| 1231 | /* |
| 1232 | * Ignore all arguments if we are invoked as a |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 1233 | * shell using "sftp-server -c command" |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1234 | */ |
| 1235 | skipargs = 1; |
| 1236 | break; |
| 1237 | case 'e': |
| 1238 | log_stderr = 1; |
| 1239 | break; |
| 1240 | case 'l': |
| 1241 | log_level = log_level_number(optarg); |
| 1242 | if (log_level == SYSLOG_LEVEL_NOT_SET) |
| 1243 | error("Invalid log level \"%s\"", optarg); |
| 1244 | break; |
| 1245 | case 'f': |
| 1246 | log_facility = log_facility_number(optarg); |
Damien Miller | 35e18db | 2007-09-17 16:11:33 +1000 | [diff] [blame] | 1247 | if (log_facility == SYSLOG_FACILITY_NOT_SET) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1248 | error("Invalid log facility \"%s\"", optarg); |
| 1249 | break; |
| 1250 | case 'h': |
| 1251 | default: |
| 1252 | usage(); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | log_init(__progname, log_level, log_facility, log_stderr); |
| 1257 | |
| 1258 | if ((cp = getenv("SSH_CONNECTION")) != NULL) { |
| 1259 | client_addr = xstrdup(cp); |
| 1260 | if ((cp = strchr(client_addr, ' ')) == NULL) |
| 1261 | fatal("Malformed SSH_CONNECTION variable: \"%s\"", |
| 1262 | getenv("SSH_CONNECTION")); |
| 1263 | *cp = '\0'; |
| 1264 | } else |
| 1265 | client_addr = xstrdup("UNKNOWN"); |
| 1266 | |
| 1267 | if ((pw = getpwuid(getuid())) == NULL) |
| 1268 | fatal("No user found for uid %lu", (u_long)getuid()); |
| 1269 | pw = pwcopy(pw); |
| 1270 | |
| 1271 | logit("session opened for local user %s from [%s]", |
| 1272 | pw->pw_name, client_addr); |
| 1273 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1274 | handle_init(); |
| 1275 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1276 | in = dup(STDIN_FILENO); |
| 1277 | out = dup(STDOUT_FILENO); |
| 1278 | |
Damien Miller | 402b331 | 2001-04-14 00:28:42 +1000 | [diff] [blame] | 1279 | #ifdef HAVE_CYGWIN |
| 1280 | setmode(in, O_BINARY); |
| 1281 | setmode(out, O_BINARY); |
| 1282 | #endif |
| 1283 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1284 | max = 0; |
| 1285 | if (in > max) |
| 1286 | max = in; |
| 1287 | if (out > max) |
| 1288 | max = out; |
| 1289 | |
| 1290 | buffer_init(&iqueue); |
| 1291 | buffer_init(&oqueue); |
| 1292 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1293 | set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask); |
| 1294 | rset = (fd_set *)xmalloc(set_size); |
| 1295 | wset = (fd_set *)xmalloc(set_size); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1296 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1297 | for (;;) { |
| 1298 | memset(rset, 0, set_size); |
| 1299 | memset(wset, 0, set_size); |
| 1300 | |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1301 | /* |
| 1302 | * Ensure that we can read a full buffer and handle |
| 1303 | * the worst-case length packet it can generate, |
| 1304 | * otherwise apply backpressure by stopping reads. |
| 1305 | */ |
| 1306 | if (buffer_check_alloc(&iqueue, sizeof(buf)) && |
| 1307 | buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH)) |
| 1308 | FD_SET(in, rset); |
| 1309 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1310 | olen = buffer_len(&oqueue); |
| 1311 | if (olen > 0) |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1312 | FD_SET(out, wset); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1313 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1314 | if (select(max+1, rset, wset, NULL, NULL) < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1315 | if (errno == EINTR) |
| 1316 | continue; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1317 | error("select: %s", strerror(errno)); |
| 1318 | cleanup_exit(2); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* copy stdin to iqueue */ |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1322 | if (FD_ISSET(in, rset)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1323 | len = read(in, buf, sizeof buf); |
| 1324 | if (len == 0) { |
| 1325 | debug("read eof"); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1326 | cleanup_exit(0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1327 | } else if (len < 0) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1328 | error("read: %s", strerror(errno)); |
| 1329 | cleanup_exit(1); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1330 | } else { |
| 1331 | buffer_append(&iqueue, buf, len); |
| 1332 | } |
| 1333 | } |
| 1334 | /* send oqueue to stdout */ |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1335 | if (FD_ISSET(out, wset)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1336 | len = write(out, buffer_ptr(&oqueue), olen); |
| 1337 | if (len < 0) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1338 | error("write: %s", strerror(errno)); |
| 1339 | cleanup_exit(1); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1340 | } else { |
| 1341 | buffer_consume(&oqueue, len); |
| 1342 | } |
| 1343 | } |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1344 | |
| 1345 | /* |
| 1346 | * Process requests from client if we can fit the results |
| 1347 | * into the output buffer, otherwise stop processing input |
| 1348 | * and let the output queue drain. |
| 1349 | */ |
| 1350 | if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH)) |
| 1351 | process(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1352 | } |
| 1353 | } |