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