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