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