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