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