| 1 | /* |
| 2 | * scp.c - Scp (Secure Copy) client for PuTTY. |
| 3 | * Joris van Rantwijk, Simon Tatham |
| 4 | * |
| 5 | * This is mainly based on ssh-1.2.26/scp.c by Timo Rinne & Tatu Ylonen. |
| 6 | * They, in turn, used stuff from BSD rcp. |
| 7 | * |
| 8 | * (SGT, 2001-09-10: Joris van Rantwijk assures me that although |
| 9 | * this file as originally submitted was inspired by, and |
| 10 | * _structurally_ based on, ssh-1.2.26's scp.c, there wasn't any |
| 11 | * actual code duplicated, so the above comment shouldn't give rise |
| 12 | * to licensing issues.) |
| 13 | */ |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <limits.h> |
| 19 | #include <time.h> |
| 20 | #include <assert.h> |
| 21 | |
| 22 | #define PUTTY_DO_GLOBALS |
| 23 | #include "putty.h" |
| 24 | #include "psftp.h" |
| 25 | #include "ssh.h" |
| 26 | #include "sftp.h" |
| 27 | #include "storage.h" |
| 28 | #include "int64.h" |
| 29 | |
| 30 | static int list = 0; |
| 31 | static int verbose = 0; |
| 32 | static int recursive = 0; |
| 33 | static int preserve = 0; |
| 34 | static int targetshouldbedirectory = 0; |
| 35 | static int statistics = 1; |
| 36 | static int prev_stats_len = 0; |
| 37 | static int scp_unsafe_mode = 0; |
| 38 | static int errs = 0; |
| 39 | static int try_scp = 1; |
| 40 | static int try_sftp = 1; |
| 41 | static int main_cmd_is_sftp = 0; |
| 42 | static int fallback_cmd_is_sftp = 0; |
| 43 | static int using_sftp = 0; |
| 44 | |
| 45 | static Backend *back; |
| 46 | static void *backhandle; |
| 47 | static Conf *conf; |
| 48 | int sent_eof = FALSE; |
| 49 | |
| 50 | static void source(char *src); |
| 51 | static void rsource(char *src); |
| 52 | static void sink(char *targ, char *src); |
| 53 | |
| 54 | const char *const appname = "PSCP"; |
| 55 | |
| 56 | /* |
| 57 | * The maximum amount of queued data we accept before we stop and |
| 58 | * wait for the server to process some. |
| 59 | */ |
| 60 | #define MAX_SCP_BUFSIZE 16384 |
| 61 | |
| 62 | void ldisc_send(void *handle, char *buf, int len, int interactive) |
| 63 | { |
| 64 | /* |
| 65 | * This is only here because of the calls to ldisc_send(NULL, |
| 66 | * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc |
| 67 | * as an ldisc. So if we get called with any real data, I want |
| 68 | * to know about it. |
| 69 | */ |
| 70 | assert(len == 0); |
| 71 | } |
| 72 | |
| 73 | static void tell_char(FILE * stream, char c) |
| 74 | { |
| 75 | fputc(c, stream); |
| 76 | } |
| 77 | |
| 78 | static void tell_str(FILE * stream, char *str) |
| 79 | { |
| 80 | unsigned int i; |
| 81 | |
| 82 | for (i = 0; i < strlen(str); ++i) |
| 83 | tell_char(stream, str[i]); |
| 84 | } |
| 85 | |
| 86 | static void tell_user(FILE * stream, char *fmt, ...) |
| 87 | { |
| 88 | char *str, *str2; |
| 89 | va_list ap; |
| 90 | va_start(ap, fmt); |
| 91 | str = dupvprintf(fmt, ap); |
| 92 | va_end(ap); |
| 93 | str2 = dupcat(str, "\n", NULL); |
| 94 | sfree(str); |
| 95 | tell_str(stream, str2); |
| 96 | sfree(str2); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Print an error message and perform a fatal exit. |
| 101 | */ |
| 102 | void fatalbox(char *fmt, ...) |
| 103 | { |
| 104 | char *str, *str2; |
| 105 | va_list ap; |
| 106 | va_start(ap, fmt); |
| 107 | str = dupvprintf(fmt, ap); |
| 108 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
| 109 | sfree(str); |
| 110 | va_end(ap); |
| 111 | tell_str(stderr, str2); |
| 112 | sfree(str2); |
| 113 | errs++; |
| 114 | |
| 115 | cleanup_exit(1); |
| 116 | } |
| 117 | void modalfatalbox(char *fmt, ...) |
| 118 | { |
| 119 | char *str, *str2; |
| 120 | va_list ap; |
| 121 | va_start(ap, fmt); |
| 122 | str = dupvprintf(fmt, ap); |
| 123 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
| 124 | sfree(str); |
| 125 | va_end(ap); |
| 126 | tell_str(stderr, str2); |
| 127 | sfree(str2); |
| 128 | errs++; |
| 129 | |
| 130 | cleanup_exit(1); |
| 131 | } |
| 132 | void connection_fatal(void *frontend, char *fmt, ...) |
| 133 | { |
| 134 | char *str, *str2; |
| 135 | va_list ap; |
| 136 | va_start(ap, fmt); |
| 137 | str = dupvprintf(fmt, ap); |
| 138 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
| 139 | sfree(str); |
| 140 | va_end(ap); |
| 141 | tell_str(stderr, str2); |
| 142 | sfree(str2); |
| 143 | errs++; |
| 144 | |
| 145 | cleanup_exit(1); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * In pscp, all agent requests should be synchronous, so this is a |
| 150 | * never-called stub. |
| 151 | */ |
| 152 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
| 153 | void *callback_ctx, void *data, int len) |
| 154 | { |
| 155 | assert(!"We shouldn't be here"); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Receive a block of data from the SSH link. Block until all data |
| 160 | * is available. |
| 161 | * |
| 162 | * To do this, we repeatedly call the SSH protocol module, with our |
| 163 | * own trap in from_backend() to catch the data that comes back. We |
| 164 | * do this until we have enough data. |
| 165 | */ |
| 166 | |
| 167 | static unsigned char *outptr; /* where to put the data */ |
| 168 | static unsigned outlen; /* how much data required */ |
| 169 | static unsigned char *pending = NULL; /* any spare data */ |
| 170 | static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ |
| 171 | int from_backend(void *frontend, int is_stderr, const char *data, int datalen) |
| 172 | { |
| 173 | unsigned char *p = (unsigned char *) data; |
| 174 | unsigned len = (unsigned) datalen; |
| 175 | |
| 176 | /* |
| 177 | * stderr data is just spouted to local stderr and otherwise |
| 178 | * ignored. |
| 179 | */ |
| 180 | if (is_stderr) { |
| 181 | if (len > 0) |
| 182 | if (fwrite(data, 1, len, stderr) < len) |
| 183 | /* oh well */; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | if ((outlen > 0) && (len > 0)) { |
| 188 | unsigned used = outlen; |
| 189 | if (used > len) |
| 190 | used = len; |
| 191 | memcpy(outptr, p, used); |
| 192 | outptr += used; |
| 193 | outlen -= used; |
| 194 | p += used; |
| 195 | len -= used; |
| 196 | } |
| 197 | |
| 198 | if (len > 0) { |
| 199 | if (pendsize < pendlen + len) { |
| 200 | pendsize = pendlen + len + 4096; |
| 201 | pending = sresize(pending, pendsize, unsigned char); |
| 202 | } |
| 203 | memcpy(pending + pendlen, p, len); |
| 204 | pendlen += len; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | int from_backend_untrusted(void *frontend_handle, const char *data, int len) |
| 210 | { |
| 211 | /* |
| 212 | * No "untrusted" output should get here (the way the code is |
| 213 | * currently, it's all diverted by FLAG_STDERR). |
| 214 | */ |
| 215 | assert(!"Unexpected call to from_backend_untrusted()"); |
| 216 | return 0; /* not reached */ |
| 217 | } |
| 218 | int from_backend_eof(void *frontend) |
| 219 | { |
| 220 | /* |
| 221 | * We expect to be the party deciding when to close the |
| 222 | * connection, so if we see EOF before we sent it ourselves, we |
| 223 | * should panic. |
| 224 | */ |
| 225 | if (!sent_eof) { |
| 226 | connection_fatal(frontend, |
| 227 | "Received unexpected end-of-file from server"); |
| 228 | } |
| 229 | return FALSE; |
| 230 | } |
| 231 | static int ssh_scp_recv(unsigned char *buf, int len) |
| 232 | { |
| 233 | outptr = buf; |
| 234 | outlen = len; |
| 235 | |
| 236 | /* |
| 237 | * See if the pending-input block contains some of what we |
| 238 | * need. |
| 239 | */ |
| 240 | if (pendlen > 0) { |
| 241 | unsigned pendused = pendlen; |
| 242 | if (pendused > outlen) |
| 243 | pendused = outlen; |
| 244 | memcpy(outptr, pending, pendused); |
| 245 | memmove(pending, pending + pendused, pendlen - pendused); |
| 246 | outptr += pendused; |
| 247 | outlen -= pendused; |
| 248 | pendlen -= pendused; |
| 249 | if (pendlen == 0) { |
| 250 | pendsize = 0; |
| 251 | sfree(pending); |
| 252 | pending = NULL; |
| 253 | } |
| 254 | if (outlen == 0) |
| 255 | return len; |
| 256 | } |
| 257 | |
| 258 | while (outlen > 0) { |
| 259 | if (back->exitcode(backhandle) >= 0 || ssh_sftp_loop_iteration() < 0) |
| 260 | return 0; /* doom */ |
| 261 | } |
| 262 | |
| 263 | return len; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Loop through the ssh connection and authentication process. |
| 268 | */ |
| 269 | static void ssh_scp_init(void) |
| 270 | { |
| 271 | while (!back->sendok(backhandle)) { |
| 272 | if (back->exitcode(backhandle) >= 0) { |
| 273 | errs++; |
| 274 | return; |
| 275 | } |
| 276 | if (ssh_sftp_loop_iteration() < 0) { |
| 277 | errs++; |
| 278 | return; /* doom */ |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* Work out which backend we ended up using. */ |
| 283 | if (!ssh_fallback_cmd(backhandle)) |
| 284 | using_sftp = main_cmd_is_sftp; |
| 285 | else |
| 286 | using_sftp = fallback_cmd_is_sftp; |
| 287 | |
| 288 | if (verbose) { |
| 289 | if (using_sftp) |
| 290 | tell_user(stderr, "Using SFTP"); |
| 291 | else |
| 292 | tell_user(stderr, "Using SCP1"); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Print an error message and exit after closing the SSH link. |
| 298 | */ |
| 299 | static void bump(char *fmt, ...) |
| 300 | { |
| 301 | char *str, *str2; |
| 302 | va_list ap; |
| 303 | va_start(ap, fmt); |
| 304 | str = dupvprintf(fmt, ap); |
| 305 | va_end(ap); |
| 306 | str2 = dupcat(str, "\n", NULL); |
| 307 | sfree(str); |
| 308 | tell_str(stderr, str2); |
| 309 | sfree(str2); |
| 310 | errs++; |
| 311 | |
| 312 | if (back != NULL && back->connected(backhandle)) { |
| 313 | char ch; |
| 314 | back->special(backhandle, TS_EOF); |
| 315 | sent_eof = TRUE; |
| 316 | ssh_scp_recv((unsigned char *) &ch, 1); |
| 317 | } |
| 318 | |
| 319 | cleanup_exit(1); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Open an SSH connection to user@host and execute cmd. |
| 324 | */ |
| 325 | static void do_cmd(char *host, char *user, char *cmd) |
| 326 | { |
| 327 | const char *err; |
| 328 | char *realhost; |
| 329 | void *logctx; |
| 330 | |
| 331 | if (host == NULL || host[0] == '\0') |
| 332 | bump("Empty host name"); |
| 333 | |
| 334 | /* |
| 335 | * Remove fiddly bits of address: remove a colon suffix, and |
| 336 | * the square brackets around an IPv6 literal address. |
| 337 | */ |
| 338 | if (host[0] == '[') { |
| 339 | host++; |
| 340 | host[strcspn(host, "]")] = '\0'; |
| 341 | } else { |
| 342 | host[strcspn(host, ":")] = '\0'; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * If we haven't loaded session details already (e.g., from -load), |
| 347 | * try looking for a session called "host". |
| 348 | */ |
| 349 | if (!loaded_session) { |
| 350 | /* Try to load settings for `host' into a temporary config */ |
| 351 | Conf *conf2 = conf_new(); |
| 352 | conf_set_str(conf2, CONF_host, ""); |
| 353 | do_defaults(host, conf2); |
| 354 | if (conf_get_str(conf2, CONF_host)[0] != '\0') { |
| 355 | /* Settings present and include hostname */ |
| 356 | /* Re-load data into the real config. */ |
| 357 | do_defaults(host, conf); |
| 358 | } else { |
| 359 | /* Session doesn't exist or mention a hostname. */ |
| 360 | /* Use `host' as a bare hostname. */ |
| 361 | conf_set_str(conf, CONF_host, host); |
| 362 | } |
| 363 | } else { |
| 364 | /* Patch in hostname `host' to session details. */ |
| 365 | conf_set_str(conf, CONF_host, host); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Force use of SSH. (If they got the protocol wrong we assume the |
| 370 | * port is useless too.) |
| 371 | */ |
| 372 | if (conf_get_int(conf, CONF_protocol) != PROT_SSH) { |
| 373 | conf_set_int(conf, CONF_protocol, PROT_SSH); |
| 374 | conf_set_int(conf, CONF_port, 22); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Enact command-line overrides. |
| 379 | */ |
| 380 | cmdline_run_saved(conf); |
| 381 | |
| 382 | /* |
| 383 | * Muck about with the hostname in various ways. |
| 384 | */ |
| 385 | { |
| 386 | char *hostbuf = dupstr(conf_get_str(conf, CONF_host)); |
| 387 | char *host = hostbuf; |
| 388 | char *p, *q; |
| 389 | |
| 390 | /* |
| 391 | * Trim leading whitespace. |
| 392 | */ |
| 393 | host += strspn(host, " \t"); |
| 394 | |
| 395 | /* |
| 396 | * See if host is of the form user@host, and separate out |
| 397 | * the username if so. |
| 398 | */ |
| 399 | if (host[0] != '\0') { |
| 400 | char *atsign = strrchr(host, '@'); |
| 401 | if (atsign) { |
| 402 | *atsign = '\0'; |
| 403 | conf_set_str(conf, CONF_username, host); |
| 404 | host = atsign + 1; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Remove any remaining whitespace. |
| 410 | */ |
| 411 | p = hostbuf; |
| 412 | q = host; |
| 413 | while (*q) { |
| 414 | if (*q != ' ' && *q != '\t') |
| 415 | *p++ = *q; |
| 416 | q++; |
| 417 | } |
| 418 | *p = '\0'; |
| 419 | |
| 420 | conf_set_str(conf, CONF_host, hostbuf); |
| 421 | sfree(hostbuf); |
| 422 | } |
| 423 | |
| 424 | /* Set username */ |
| 425 | if (user != NULL && user[0] != '\0') { |
| 426 | conf_set_str(conf, CONF_username, user); |
| 427 | } else if (conf_get_str(conf, CONF_username)[0] == '\0') { |
| 428 | user = get_username(); |
| 429 | if (!user) |
| 430 | bump("Empty user name"); |
| 431 | else { |
| 432 | if (verbose) |
| 433 | tell_user(stderr, "Guessing user name: %s", user); |
| 434 | conf_set_str(conf, CONF_username, user); |
| 435 | sfree(user); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Disable scary things which shouldn't be enabled for simple |
| 441 | * things like SCP and SFTP: agent forwarding, port forwarding, |
| 442 | * X forwarding. |
| 443 | */ |
| 444 | conf_set_int(conf, CONF_x11_forward, 0); |
| 445 | conf_set_int(conf, CONF_agentfwd, 0); |
| 446 | conf_set_int(conf, CONF_ssh_simple, TRUE); |
| 447 | { |
| 448 | char *key; |
| 449 | while ((key = conf_get_str_nthstrkey(conf, CONF_portfwd, 0)) != NULL) |
| 450 | conf_del_str_str(conf, CONF_portfwd, key); |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Set up main and possibly fallback command depending on |
| 455 | * options specified by user. |
| 456 | * Attempt to start the SFTP subsystem as a first choice, |
| 457 | * falling back to the provided scp command if that fails. |
| 458 | */ |
| 459 | conf_set_str(conf, CONF_remote_cmd2, ""); |
| 460 | if (try_sftp) { |
| 461 | /* First choice is SFTP subsystem. */ |
| 462 | main_cmd_is_sftp = 1; |
| 463 | conf_set_str(conf, CONF_remote_cmd, "sftp"); |
| 464 | conf_set_int(conf, CONF_ssh_subsys, TRUE); |
| 465 | if (try_scp) { |
| 466 | /* Fallback is to use the provided scp command. */ |
| 467 | fallback_cmd_is_sftp = 0; |
| 468 | conf_set_str(conf, CONF_remote_cmd2, cmd); |
| 469 | conf_set_int(conf, CONF_ssh_subsys2, FALSE); |
| 470 | } else { |
| 471 | /* Since we're not going to try SCP, we may as well try |
| 472 | * harder to find an SFTP server, since in the current |
| 473 | * implementation we have a spare slot. */ |
| 474 | fallback_cmd_is_sftp = 1; |
| 475 | /* see psftp.c for full explanation of this kludge */ |
| 476 | conf_set_str(conf, CONF_remote_cmd2, |
| 477 | "test -x /usr/lib/sftp-server &&" |
| 478 | " exec /usr/lib/sftp-server\n" |
| 479 | "test -x /usr/local/lib/sftp-server &&" |
| 480 | " exec /usr/local/lib/sftp-server\n" |
| 481 | "exec sftp-server"); |
| 482 | conf_set_int(conf, CONF_ssh_subsys2, FALSE); |
| 483 | } |
| 484 | } else { |
| 485 | /* Don't try SFTP at all; just try the scp command. */ |
| 486 | main_cmd_is_sftp = 0; |
| 487 | conf_set_str(conf, CONF_remote_cmd, cmd); |
| 488 | conf_set_int(conf, CONF_ssh_subsys, FALSE); |
| 489 | } |
| 490 | conf_set_int(conf, CONF_nopty, TRUE); |
| 491 | |
| 492 | back = &ssh_backend; |
| 493 | |
| 494 | err = back->init(NULL, &backhandle, conf, |
| 495 | conf_get_str(conf, CONF_host), |
| 496 | conf_get_int(conf, CONF_port), |
| 497 | &realhost, 0, |
| 498 | conf_get_int(conf, CONF_tcp_keepalives)); |
| 499 | if (err != NULL) |
| 500 | bump("ssh_init: %s", err); |
| 501 | logctx = log_init(NULL, conf); |
| 502 | back->provide_logctx(backhandle, logctx); |
| 503 | console_provide_logctx(logctx); |
| 504 | ssh_scp_init(); |
| 505 | if (verbose && realhost != NULL && errs == 0) |
| 506 | tell_user(stderr, "Connected to %s\n", realhost); |
| 507 | sfree(realhost); |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Update statistic information about current file. |
| 512 | */ |
| 513 | static void print_stats(char *name, uint64 size, uint64 done, |
| 514 | time_t start, time_t now) |
| 515 | { |
| 516 | float ratebs; |
| 517 | unsigned long eta; |
| 518 | char *etastr; |
| 519 | int pct; |
| 520 | int len; |
| 521 | int elap; |
| 522 | double donedbl; |
| 523 | double sizedbl; |
| 524 | |
| 525 | elap = (unsigned long) difftime(now, start); |
| 526 | |
| 527 | if (now > start) |
| 528 | ratebs = (float) (uint64_to_double(done) / elap); |
| 529 | else |
| 530 | ratebs = (float) uint64_to_double(done); |
| 531 | |
| 532 | if (ratebs < 1.0) |
| 533 | eta = (unsigned long) (uint64_to_double(uint64_subtract(size, done))); |
| 534 | else { |
| 535 | eta = (unsigned long) |
| 536 | ((uint64_to_double(uint64_subtract(size, done)) / ratebs)); |
| 537 | } |
| 538 | |
| 539 | etastr = dupprintf("%02ld:%02ld:%02ld", |
| 540 | eta / 3600, (eta % 3600) / 60, eta % 60); |
| 541 | |
| 542 | donedbl = uint64_to_double(done); |
| 543 | sizedbl = uint64_to_double(size); |
| 544 | pct = (int) (100 * (donedbl * 1.0 / sizedbl)); |
| 545 | |
| 546 | { |
| 547 | char donekb[40]; |
| 548 | /* divide by 1024 to provide kB */ |
| 549 | uint64_decimal(uint64_shift_right(done, 10), donekb); |
| 550 | len = printf("\r%-25.25s | %s kB | %5.1f kB/s | ETA: %8s | %3d%%", |
| 551 | name, |
| 552 | donekb, ratebs / 1024.0, etastr, pct); |
| 553 | if (len < prev_stats_len) |
| 554 | printf("%*s", prev_stats_len - len, ""); |
| 555 | prev_stats_len = len; |
| 556 | |
| 557 | if (uint64_compare(done, size) == 0) |
| 558 | printf("\n"); |
| 559 | |
| 560 | fflush(stdout); |
| 561 | } |
| 562 | |
| 563 | free(etastr); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Find a colon in str and return a pointer to the colon. |
| 568 | * This is used to separate hostname from filename. |
| 569 | */ |
| 570 | static char *colon(char *str) |
| 571 | { |
| 572 | /* We ignore a leading colon, since the hostname cannot be |
| 573 | empty. We also ignore a colon as second character because |
| 574 | of filenames like f:myfile.txt. */ |
| 575 | if (str[0] == '\0' || str[0] == ':' || |
| 576 | (str[0] != '[' && str[1] == ':')) |
| 577 | return (NULL); |
| 578 | while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\') { |
| 579 | if (*str == '[') { |
| 580 | /* Skip over IPv6 literal addresses |
| 581 | * (eg: 'jeroen@[2001:db8::1]:myfile.txt') */ |
| 582 | char *ipv6_end = strchr(str, ']'); |
| 583 | if (ipv6_end) { |
| 584 | str = ipv6_end; |
| 585 | } |
| 586 | } |
| 587 | str++; |
| 588 | } |
| 589 | if (*str == ':') |
| 590 | return (str); |
| 591 | else |
| 592 | return (NULL); |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Return a pointer to the portion of str that comes after the last |
| 597 | * slash (or backslash or colon, if `local' is TRUE). |
| 598 | */ |
| 599 | static char *stripslashes(char *str, int local) |
| 600 | { |
| 601 | char *p; |
| 602 | |
| 603 | if (local) { |
| 604 | p = strchr(str, ':'); |
| 605 | if (p) str = p+1; |
| 606 | } |
| 607 | |
| 608 | p = strrchr(str, '/'); |
| 609 | if (p) str = p+1; |
| 610 | |
| 611 | if (local) { |
| 612 | p = strrchr(str, '\\'); |
| 613 | if (p) str = p+1; |
| 614 | } |
| 615 | |
| 616 | return str; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Determine whether a string is entirely composed of dots. |
| 621 | */ |
| 622 | static int is_dots(char *str) |
| 623 | { |
| 624 | return str[strspn(str, ".")] == '\0'; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Wait for a response from the other side. |
| 629 | * Return 0 if ok, -1 if error. |
| 630 | */ |
| 631 | static int response(void) |
| 632 | { |
| 633 | char ch, resp, rbuf[2048]; |
| 634 | int p; |
| 635 | |
| 636 | if (ssh_scp_recv((unsigned char *) &resp, 1) <= 0) |
| 637 | bump("Lost connection"); |
| 638 | |
| 639 | p = 0; |
| 640 | switch (resp) { |
| 641 | case 0: /* ok */ |
| 642 | return (0); |
| 643 | default: |
| 644 | rbuf[p++] = resp; |
| 645 | /* fallthrough */ |
| 646 | case 1: /* error */ |
| 647 | case 2: /* fatal error */ |
| 648 | do { |
| 649 | if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0) |
| 650 | bump("Protocol error: Lost connection"); |
| 651 | rbuf[p++] = ch; |
| 652 | } while (p < sizeof(rbuf) && ch != '\n'); |
| 653 | rbuf[p - 1] = '\0'; |
| 654 | if (resp == 1) |
| 655 | tell_user(stderr, "%s\n", rbuf); |
| 656 | else |
| 657 | bump("%s", rbuf); |
| 658 | errs++; |
| 659 | return (-1); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | int sftp_recvdata(char *buf, int len) |
| 664 | { |
| 665 | return ssh_scp_recv((unsigned char *) buf, len); |
| 666 | } |
| 667 | int sftp_senddata(char *buf, int len) |
| 668 | { |
| 669 | back->send(backhandle, buf, len); |
| 670 | return 1; |
| 671 | } |
| 672 | |
| 673 | /* ---------------------------------------------------------------------- |
| 674 | * sftp-based replacement for the hacky `pscp -ls'. |
| 675 | */ |
| 676 | static int sftp_ls_compare(const void *av, const void *bv) |
| 677 | { |
| 678 | const struct fxp_name *a = (const struct fxp_name *) av; |
| 679 | const struct fxp_name *b = (const struct fxp_name *) bv; |
| 680 | return strcmp(a->filename, b->filename); |
| 681 | } |
| 682 | void scp_sftp_listdir(char *dirname) |
| 683 | { |
| 684 | struct fxp_handle *dirh; |
| 685 | struct fxp_names *names; |
| 686 | struct fxp_name *ournames; |
| 687 | struct sftp_packet *pktin; |
| 688 | struct sftp_request *req, *rreq; |
| 689 | int nnames, namesize; |
| 690 | int i; |
| 691 | |
| 692 | if (!fxp_init()) { |
| 693 | tell_user(stderr, "unable to initialise SFTP: %s", fxp_error()); |
| 694 | errs++; |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | printf("Listing directory %s\n", dirname); |
| 699 | |
| 700 | sftp_register(req = fxp_opendir_send(dirname)); |
| 701 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 702 | assert(rreq == req); |
| 703 | dirh = fxp_opendir_recv(pktin, rreq); |
| 704 | |
| 705 | if (dirh == NULL) { |
| 706 | printf("Unable to open %s: %s\n", dirname, fxp_error()); |
| 707 | } else { |
| 708 | nnames = namesize = 0; |
| 709 | ournames = NULL; |
| 710 | |
| 711 | while (1) { |
| 712 | |
| 713 | sftp_register(req = fxp_readdir_send(dirh)); |
| 714 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 715 | assert(rreq == req); |
| 716 | names = fxp_readdir_recv(pktin, rreq); |
| 717 | |
| 718 | if (names == NULL) { |
| 719 | if (fxp_error_type() == SSH_FX_EOF) |
| 720 | break; |
| 721 | printf("Reading directory %s: %s\n", dirname, fxp_error()); |
| 722 | break; |
| 723 | } |
| 724 | if (names->nnames == 0) { |
| 725 | fxp_free_names(names); |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | if (nnames + names->nnames >= namesize) { |
| 730 | namesize += names->nnames + 128; |
| 731 | ournames = sresize(ournames, namesize, struct fxp_name); |
| 732 | } |
| 733 | |
| 734 | for (i = 0; i < names->nnames; i++) |
| 735 | ournames[nnames++] = names->names[i]; |
| 736 | names->nnames = 0; /* prevent free_names */ |
| 737 | fxp_free_names(names); |
| 738 | } |
| 739 | sftp_register(req = fxp_close_send(dirh)); |
| 740 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 741 | assert(rreq == req); |
| 742 | fxp_close_recv(pktin, rreq); |
| 743 | |
| 744 | /* |
| 745 | * Now we have our filenames. Sort them by actual file |
| 746 | * name, and then output the longname parts. |
| 747 | */ |
| 748 | qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare); |
| 749 | |
| 750 | /* |
| 751 | * And print them. |
| 752 | */ |
| 753 | for (i = 0; i < nnames; i++) |
| 754 | printf("%s\n", ournames[i].longname); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /* ---------------------------------------------------------------------- |
| 759 | * Helper routines that contain the actual SCP protocol elements, |
| 760 | * implemented both as SCP1 and SFTP. |
| 761 | */ |
| 762 | |
| 763 | static struct scp_sftp_dirstack { |
| 764 | struct scp_sftp_dirstack *next; |
| 765 | struct fxp_name *names; |
| 766 | int namepos, namelen; |
| 767 | char *dirpath; |
| 768 | char *wildcard; |
| 769 | int matched_something; /* wildcard match set was non-empty */ |
| 770 | } *scp_sftp_dirstack_head; |
| 771 | static char *scp_sftp_remotepath, *scp_sftp_currentname; |
| 772 | static char *scp_sftp_wildcard; |
| 773 | static int scp_sftp_targetisdir, scp_sftp_donethistarget; |
| 774 | static int scp_sftp_preserve, scp_sftp_recursive; |
| 775 | static unsigned long scp_sftp_mtime, scp_sftp_atime; |
| 776 | static int scp_has_times; |
| 777 | static struct fxp_handle *scp_sftp_filehandle; |
| 778 | static struct fxp_xfer *scp_sftp_xfer; |
| 779 | static uint64 scp_sftp_fileoffset; |
| 780 | |
| 781 | int scp_source_setup(char *target, int shouldbedir) |
| 782 | { |
| 783 | if (using_sftp) { |
| 784 | /* |
| 785 | * Find out whether the target filespec is in fact a |
| 786 | * directory. |
| 787 | */ |
| 788 | struct sftp_packet *pktin; |
| 789 | struct sftp_request *req, *rreq; |
| 790 | struct fxp_attrs attrs; |
| 791 | int ret; |
| 792 | |
| 793 | if (!fxp_init()) { |
| 794 | tell_user(stderr, "unable to initialise SFTP: %s", fxp_error()); |
| 795 | errs++; |
| 796 | return 1; |
| 797 | } |
| 798 | |
| 799 | sftp_register(req = fxp_stat_send(target)); |
| 800 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 801 | assert(rreq == req); |
| 802 | ret = fxp_stat_recv(pktin, rreq, &attrs); |
| 803 | |
| 804 | if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) |
| 805 | scp_sftp_targetisdir = 0; |
| 806 | else |
| 807 | scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0; |
| 808 | |
| 809 | if (shouldbedir && !scp_sftp_targetisdir) { |
| 810 | bump("pscp: remote filespec %s: not a directory\n", target); |
| 811 | } |
| 812 | |
| 813 | scp_sftp_remotepath = dupstr(target); |
| 814 | |
| 815 | scp_has_times = 0; |
| 816 | } else { |
| 817 | (void) response(); |
| 818 | } |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | int scp_send_errmsg(char *str) |
| 823 | { |
| 824 | if (using_sftp) { |
| 825 | /* do nothing; we never need to send our errors to the server */ |
| 826 | } else { |
| 827 | back->send(backhandle, "\001", 1);/* scp protocol error prefix */ |
| 828 | back->send(backhandle, str, strlen(str)); |
| 829 | } |
| 830 | return 0; /* can't fail */ |
| 831 | } |
| 832 | |
| 833 | int scp_send_filetimes(unsigned long mtime, unsigned long atime) |
| 834 | { |
| 835 | if (using_sftp) { |
| 836 | scp_sftp_mtime = mtime; |
| 837 | scp_sftp_atime = atime; |
| 838 | scp_has_times = 1; |
| 839 | return 0; |
| 840 | } else { |
| 841 | char buf[80]; |
| 842 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
| 843 | back->send(backhandle, buf, strlen(buf)); |
| 844 | return response(); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | int scp_send_filename(char *name, uint64 size, int permissions) |
| 849 | { |
| 850 | if (using_sftp) { |
| 851 | char *fullname; |
| 852 | struct sftp_packet *pktin; |
| 853 | struct sftp_request *req, *rreq; |
| 854 | struct fxp_attrs attrs; |
| 855 | |
| 856 | if (scp_sftp_targetisdir) { |
| 857 | fullname = dupcat(scp_sftp_remotepath, "/", name, NULL); |
| 858 | } else { |
| 859 | fullname = dupstr(scp_sftp_remotepath); |
| 860 | } |
| 861 | |
| 862 | attrs.flags = 0; |
| 863 | PUT_PERMISSIONS(attrs, permissions); |
| 864 | |
| 865 | sftp_register(req = fxp_open_send(fullname, SSH_FXF_WRITE | |
| 866 | SSH_FXF_CREAT | SSH_FXF_TRUNC, |
| 867 | &attrs)); |
| 868 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 869 | assert(rreq == req); |
| 870 | scp_sftp_filehandle = fxp_open_recv(pktin, rreq); |
| 871 | |
| 872 | if (!scp_sftp_filehandle) { |
| 873 | tell_user(stderr, "pscp: unable to open %s: %s", |
| 874 | fullname, fxp_error()); |
| 875 | errs++; |
| 876 | return 1; |
| 877 | } |
| 878 | scp_sftp_fileoffset = uint64_make(0, 0); |
| 879 | scp_sftp_xfer = xfer_upload_init(scp_sftp_filehandle, |
| 880 | scp_sftp_fileoffset); |
| 881 | sfree(fullname); |
| 882 | return 0; |
| 883 | } else { |
| 884 | char buf[40]; |
| 885 | char sizestr[40]; |
| 886 | uint64_decimal(size, sizestr); |
| 887 | if (permissions < 0) |
| 888 | permissions = 0644; |
| 889 | sprintf(buf, "C%04o %s ", (int)(permissions & 07777), sizestr); |
| 890 | back->send(backhandle, buf, strlen(buf)); |
| 891 | back->send(backhandle, name, strlen(name)); |
| 892 | back->send(backhandle, "\n", 1); |
| 893 | return response(); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | int scp_send_filedata(char *data, int len) |
| 898 | { |
| 899 | if (using_sftp) { |
| 900 | int ret; |
| 901 | struct sftp_packet *pktin; |
| 902 | |
| 903 | if (!scp_sftp_filehandle) { |
| 904 | return 1; |
| 905 | } |
| 906 | |
| 907 | while (!xfer_upload_ready(scp_sftp_xfer)) { |
| 908 | pktin = sftp_recv(); |
| 909 | ret = xfer_upload_gotpkt(scp_sftp_xfer, pktin); |
| 910 | if (!ret) { |
| 911 | tell_user(stderr, "error while writing: %s\n", fxp_error()); |
| 912 | errs++; |
| 913 | return 1; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | xfer_upload_data(scp_sftp_xfer, data, len); |
| 918 | |
| 919 | scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, len); |
| 920 | return 0; |
| 921 | } else { |
| 922 | int bufsize = back->send(backhandle, data, len); |
| 923 | |
| 924 | /* |
| 925 | * If the network transfer is backing up - that is, the |
| 926 | * remote site is not accepting data as fast as we can |
| 927 | * produce it - then we must loop on network events until |
| 928 | * we have space in the buffer again. |
| 929 | */ |
| 930 | while (bufsize > MAX_SCP_BUFSIZE) { |
| 931 | if (ssh_sftp_loop_iteration() < 0) |
| 932 | return 1; |
| 933 | bufsize = back->sendbuffer(backhandle); |
| 934 | } |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | int scp_send_finish(void) |
| 941 | { |
| 942 | if (using_sftp) { |
| 943 | struct fxp_attrs attrs; |
| 944 | struct sftp_packet *pktin; |
| 945 | struct sftp_request *req, *rreq; |
| 946 | int ret; |
| 947 | |
| 948 | while (!xfer_done(scp_sftp_xfer)) { |
| 949 | pktin = sftp_recv(); |
| 950 | xfer_upload_gotpkt(scp_sftp_xfer, pktin); |
| 951 | } |
| 952 | xfer_cleanup(scp_sftp_xfer); |
| 953 | |
| 954 | if (!scp_sftp_filehandle) { |
| 955 | return 1; |
| 956 | } |
| 957 | if (scp_has_times) { |
| 958 | attrs.flags = SSH_FILEXFER_ATTR_ACMODTIME; |
| 959 | attrs.atime = scp_sftp_atime; |
| 960 | attrs.mtime = scp_sftp_mtime; |
| 961 | sftp_register(req = fxp_fsetstat_send(scp_sftp_filehandle, attrs)); |
| 962 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 963 | assert(rreq == req); |
| 964 | ret = fxp_fsetstat_recv(pktin, rreq); |
| 965 | if (!ret) { |
| 966 | tell_user(stderr, "unable to set file times: %s\n", fxp_error()); |
| 967 | errs++; |
| 968 | } |
| 969 | } |
| 970 | sftp_register(req = fxp_close_send(scp_sftp_filehandle)); |
| 971 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 972 | assert(rreq == req); |
| 973 | fxp_close_recv(pktin, rreq); |
| 974 | scp_has_times = 0; |
| 975 | return 0; |
| 976 | } else { |
| 977 | back->send(backhandle, "", 1); |
| 978 | return response(); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | char *scp_save_remotepath(void) |
| 983 | { |
| 984 | if (using_sftp) |
| 985 | return scp_sftp_remotepath; |
| 986 | else |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | void scp_restore_remotepath(char *data) |
| 991 | { |
| 992 | if (using_sftp) |
| 993 | scp_sftp_remotepath = data; |
| 994 | } |
| 995 | |
| 996 | int scp_send_dirname(char *name, int modes) |
| 997 | { |
| 998 | if (using_sftp) { |
| 999 | char *fullname; |
| 1000 | char const *err; |
| 1001 | struct fxp_attrs attrs; |
| 1002 | struct sftp_packet *pktin; |
| 1003 | struct sftp_request *req, *rreq; |
| 1004 | int ret; |
| 1005 | |
| 1006 | if (scp_sftp_targetisdir) { |
| 1007 | fullname = dupcat(scp_sftp_remotepath, "/", name, NULL); |
| 1008 | } else { |
| 1009 | fullname = dupstr(scp_sftp_remotepath); |
| 1010 | } |
| 1011 | |
| 1012 | /* |
| 1013 | * We don't worry about whether we managed to create the |
| 1014 | * directory, because if it exists already it's OK just to |
| 1015 | * use it. Instead, we will stat it afterwards, and if it |
| 1016 | * exists and is a directory we will assume we were either |
| 1017 | * successful or it didn't matter. |
| 1018 | */ |
| 1019 | sftp_register(req = fxp_mkdir_send(fullname)); |
| 1020 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1021 | assert(rreq == req); |
| 1022 | ret = fxp_mkdir_recv(pktin, rreq); |
| 1023 | |
| 1024 | if (!ret) |
| 1025 | err = fxp_error(); |
| 1026 | else |
| 1027 | err = "server reported no error"; |
| 1028 | |
| 1029 | sftp_register(req = fxp_stat_send(fullname)); |
| 1030 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1031 | assert(rreq == req); |
| 1032 | ret = fxp_stat_recv(pktin, rreq, &attrs); |
| 1033 | |
| 1034 | if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) || |
| 1035 | !(attrs.permissions & 0040000)) { |
| 1036 | tell_user(stderr, "unable to create directory %s: %s", |
| 1037 | fullname, err); |
| 1038 | errs++; |
| 1039 | return 1; |
| 1040 | } |
| 1041 | |
| 1042 | scp_sftp_remotepath = fullname; |
| 1043 | |
| 1044 | return 0; |
| 1045 | } else { |
| 1046 | char buf[40]; |
| 1047 | sprintf(buf, "D%04o 0 ", modes); |
| 1048 | back->send(backhandle, buf, strlen(buf)); |
| 1049 | back->send(backhandle, name, strlen(name)); |
| 1050 | back->send(backhandle, "\n", 1); |
| 1051 | return response(); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | int scp_send_enddir(void) |
| 1056 | { |
| 1057 | if (using_sftp) { |
| 1058 | sfree(scp_sftp_remotepath); |
| 1059 | return 0; |
| 1060 | } else { |
| 1061 | back->send(backhandle, "E\n", 2); |
| 1062 | return response(); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init. |
| 1068 | * That's bad. The difference is that scp_sink_setup is called once |
| 1069 | * right at the start, whereas scp_sink_init is called to |
| 1070 | * initialise every level of recursion in the protocol. |
| 1071 | */ |
| 1072 | int scp_sink_setup(char *source, int preserve, int recursive) |
| 1073 | { |
| 1074 | if (using_sftp) { |
| 1075 | char *newsource; |
| 1076 | |
| 1077 | if (!fxp_init()) { |
| 1078 | tell_user(stderr, "unable to initialise SFTP: %s", fxp_error()); |
| 1079 | errs++; |
| 1080 | return 1; |
| 1081 | } |
| 1082 | /* |
| 1083 | * It's possible that the source string we've been given |
| 1084 | * contains a wildcard. If so, we must split the directory |
| 1085 | * away from the wildcard itself (throwing an error if any |
| 1086 | * wildcardness comes before the final slash) and arrange |
| 1087 | * things so that a dirstack entry will be set up. |
| 1088 | */ |
| 1089 | newsource = snewn(1+strlen(source), char); |
| 1090 | if (!wc_unescape(newsource, source)) { |
| 1091 | /* Yes, here we go; it's a wildcard. Bah. */ |
| 1092 | char *dupsource, *lastpart, *dirpart, *wildcard; |
| 1093 | dupsource = dupstr(source); |
| 1094 | lastpart = stripslashes(dupsource, 0); |
| 1095 | wildcard = dupstr(lastpart); |
| 1096 | *lastpart = '\0'; |
| 1097 | if (*dupsource && dupsource[1]) { |
| 1098 | /* |
| 1099 | * The remains of dupsource are at least two |
| 1100 | * characters long, meaning the pathname wasn't |
| 1101 | * empty or just `/'. Hence, we remove the trailing |
| 1102 | * slash. |
| 1103 | */ |
| 1104 | lastpart[-1] = '\0'; |
| 1105 | } else if (!*dupsource) { |
| 1106 | /* |
| 1107 | * The remains of dupsource are _empty_ - the whole |
| 1108 | * pathname was a wildcard. Hence we need to |
| 1109 | * replace it with ".". |
| 1110 | */ |
| 1111 | sfree(dupsource); |
| 1112 | dupsource = dupstr("."); |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Now we have separated our string into dupsource (the |
| 1117 | * directory part) and wildcard. Both of these will |
| 1118 | * need freeing at some point. Next step is to remove |
| 1119 | * wildcard escapes from the directory part, throwing |
| 1120 | * an error if it contains a real wildcard. |
| 1121 | */ |
| 1122 | dirpart = snewn(1+strlen(dupsource), char); |
| 1123 | if (!wc_unescape(dirpart, dupsource)) { |
| 1124 | tell_user(stderr, "%s: multiple-level wildcards unsupported", |
| 1125 | source); |
| 1126 | errs++; |
| 1127 | sfree(dirpart); |
| 1128 | sfree(wildcard); |
| 1129 | sfree(dupsource); |
| 1130 | return 1; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Now we have dirpart (unescaped, ie a valid remote |
| 1135 | * path), and wildcard (a wildcard). This will be |
| 1136 | * sufficient to arrange a dirstack entry. |
| 1137 | */ |
| 1138 | scp_sftp_remotepath = dirpart; |
| 1139 | scp_sftp_wildcard = wildcard; |
| 1140 | sfree(dupsource); |
| 1141 | } else { |
| 1142 | scp_sftp_remotepath = newsource; |
| 1143 | scp_sftp_wildcard = NULL; |
| 1144 | } |
| 1145 | scp_sftp_preserve = preserve; |
| 1146 | scp_sftp_recursive = recursive; |
| 1147 | scp_sftp_donethistarget = 0; |
| 1148 | scp_sftp_dirstack_head = NULL; |
| 1149 | } |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | int scp_sink_init(void) |
| 1154 | { |
| 1155 | if (!using_sftp) { |
| 1156 | back->send(backhandle, "", 1); |
| 1157 | } |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | #define SCP_SINK_FILE 1 |
| 1162 | #define SCP_SINK_DIR 2 |
| 1163 | #define SCP_SINK_ENDDIR 3 |
| 1164 | #define SCP_SINK_RETRY 4 /* not an action; just try again */ |
| 1165 | struct scp_sink_action { |
| 1166 | int action; /* FILE, DIR, ENDDIR */ |
| 1167 | char *buf; /* will need freeing after use */ |
| 1168 | char *name; /* filename or dirname (not ENDDIR) */ |
| 1169 | long permissions; /* access permissions (not ENDDIR) */ |
| 1170 | uint64 size; /* file size (not ENDDIR) */ |
| 1171 | int settime; /* 1 if atime and mtime are filled */ |
| 1172 | unsigned long atime, mtime; /* access times for the file */ |
| 1173 | }; |
| 1174 | |
| 1175 | int scp_get_sink_action(struct scp_sink_action *act) |
| 1176 | { |
| 1177 | if (using_sftp) { |
| 1178 | char *fname; |
| 1179 | int must_free_fname; |
| 1180 | struct fxp_attrs attrs; |
| 1181 | struct sftp_packet *pktin; |
| 1182 | struct sftp_request *req, *rreq; |
| 1183 | int ret; |
| 1184 | |
| 1185 | if (!scp_sftp_dirstack_head) { |
| 1186 | if (!scp_sftp_donethistarget) { |
| 1187 | /* |
| 1188 | * Simple case: we are only dealing with one file. |
| 1189 | */ |
| 1190 | fname = scp_sftp_remotepath; |
| 1191 | must_free_fname = 0; |
| 1192 | scp_sftp_donethistarget = 1; |
| 1193 | } else { |
| 1194 | /* |
| 1195 | * Even simpler case: one file _which we've done_. |
| 1196 | * Return 1 (finished). |
| 1197 | */ |
| 1198 | return 1; |
| 1199 | } |
| 1200 | } else { |
| 1201 | /* |
| 1202 | * We're now in the middle of stepping through a list |
| 1203 | * of names returned from fxp_readdir(); so let's carry |
| 1204 | * on. |
| 1205 | */ |
| 1206 | struct scp_sftp_dirstack *head = scp_sftp_dirstack_head; |
| 1207 | while (head->namepos < head->namelen && |
| 1208 | (is_dots(head->names[head->namepos].filename) || |
| 1209 | (head->wildcard && |
| 1210 | !wc_match(head->wildcard, |
| 1211 | head->names[head->namepos].filename)))) |
| 1212 | head->namepos++; /* skip . and .. */ |
| 1213 | if (head->namepos < head->namelen) { |
| 1214 | head->matched_something = 1; |
| 1215 | fname = dupcat(head->dirpath, "/", |
| 1216 | head->names[head->namepos++].filename, |
| 1217 | NULL); |
| 1218 | must_free_fname = 1; |
| 1219 | } else { |
| 1220 | /* |
| 1221 | * We've come to the end of the list; pop it off |
| 1222 | * the stack and return an ENDDIR action (or RETRY |
| 1223 | * if this was a wildcard match). |
| 1224 | */ |
| 1225 | if (head->wildcard) { |
| 1226 | act->action = SCP_SINK_RETRY; |
| 1227 | if (!head->matched_something) { |
| 1228 | tell_user(stderr, "pscp: wildcard '%s' matched " |
| 1229 | "no files", head->wildcard); |
| 1230 | errs++; |
| 1231 | } |
| 1232 | sfree(head->wildcard); |
| 1233 | |
| 1234 | } else { |
| 1235 | act->action = SCP_SINK_ENDDIR; |
| 1236 | } |
| 1237 | |
| 1238 | sfree(head->dirpath); |
| 1239 | sfree(head->names); |
| 1240 | scp_sftp_dirstack_head = head->next; |
| 1241 | sfree(head); |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * Now we have a filename. Stat it, and see if it's a file |
| 1249 | * or a directory. |
| 1250 | */ |
| 1251 | sftp_register(req = fxp_stat_send(fname)); |
| 1252 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1253 | assert(rreq == req); |
| 1254 | ret = fxp_stat_recv(pktin, rreq, &attrs); |
| 1255 | |
| 1256 | if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) { |
| 1257 | tell_user(stderr, "unable to identify %s: %s", fname, |
| 1258 | ret ? "file type not supplied" : fxp_error()); |
| 1259 | errs++; |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
| 1263 | if (attrs.permissions & 0040000) { |
| 1264 | struct scp_sftp_dirstack *newitem; |
| 1265 | struct fxp_handle *dirhandle; |
| 1266 | int nnames, namesize; |
| 1267 | struct fxp_name *ournames; |
| 1268 | struct fxp_names *names; |
| 1269 | |
| 1270 | /* |
| 1271 | * It's a directory. If we're not in recursive mode, |
| 1272 | * this merits a complaint (which is fatal if the name |
| 1273 | * was specified directly, but not if it was matched by |
| 1274 | * a wildcard). |
| 1275 | * |
| 1276 | * We skip this complaint completely if |
| 1277 | * scp_sftp_wildcard is set, because that's an |
| 1278 | * indication that we're not actually supposed to |
| 1279 | * _recursively_ transfer the dir, just scan it for |
| 1280 | * things matching the wildcard. |
| 1281 | */ |
| 1282 | if (!scp_sftp_recursive && !scp_sftp_wildcard) { |
| 1283 | tell_user(stderr, "pscp: %s: is a directory", fname); |
| 1284 | errs++; |
| 1285 | if (must_free_fname) sfree(fname); |
| 1286 | if (scp_sftp_dirstack_head) { |
| 1287 | act->action = SCP_SINK_RETRY; |
| 1288 | return 0; |
| 1289 | } else { |
| 1290 | return 1; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Otherwise, the fun begins. We must fxp_opendir() the |
| 1296 | * directory, slurp the filenames into memory, return |
| 1297 | * SCP_SINK_DIR (unless this is a wildcard match), and |
| 1298 | * set targetisdir. The next time we're called, we will |
| 1299 | * run through the list of filenames one by one, |
| 1300 | * matching them against a wildcard if present. |
| 1301 | * |
| 1302 | * If targetisdir is _already_ set (meaning we're |
| 1303 | * already in the middle of going through another such |
| 1304 | * list), we must push the other (target,namelist) pair |
| 1305 | * on a stack. |
| 1306 | */ |
| 1307 | sftp_register(req = fxp_opendir_send(fname)); |
| 1308 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1309 | assert(rreq == req); |
| 1310 | dirhandle = fxp_opendir_recv(pktin, rreq); |
| 1311 | |
| 1312 | if (!dirhandle) { |
| 1313 | tell_user(stderr, "scp: unable to open directory %s: %s", |
| 1314 | fname, fxp_error()); |
| 1315 | if (must_free_fname) sfree(fname); |
| 1316 | errs++; |
| 1317 | return 1; |
| 1318 | } |
| 1319 | nnames = namesize = 0; |
| 1320 | ournames = NULL; |
| 1321 | while (1) { |
| 1322 | int i; |
| 1323 | |
| 1324 | sftp_register(req = fxp_readdir_send(dirhandle)); |
| 1325 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1326 | assert(rreq == req); |
| 1327 | names = fxp_readdir_recv(pktin, rreq); |
| 1328 | |
| 1329 | if (names == NULL) { |
| 1330 | if (fxp_error_type() == SSH_FX_EOF) |
| 1331 | break; |
| 1332 | tell_user(stderr, "scp: reading directory %s: %s\n", |
| 1333 | fname, fxp_error()); |
| 1334 | if (must_free_fname) sfree(fname); |
| 1335 | sfree(ournames); |
| 1336 | errs++; |
| 1337 | return 1; |
| 1338 | } |
| 1339 | if (names->nnames == 0) { |
| 1340 | fxp_free_names(names); |
| 1341 | break; |
| 1342 | } |
| 1343 | if (nnames + names->nnames >= namesize) { |
| 1344 | namesize += names->nnames + 128; |
| 1345 | ournames = sresize(ournames, namesize, struct fxp_name); |
| 1346 | } |
| 1347 | for (i = 0; i < names->nnames; i++) { |
| 1348 | if (!strcmp(names->names[i].filename, ".") || |
| 1349 | !strcmp(names->names[i].filename, "..")) { |
| 1350 | /* |
| 1351 | * . and .. are normal consequences of |
| 1352 | * reading a directory, and aren't worth |
| 1353 | * complaining about. |
| 1354 | */ |
| 1355 | } else if (!vet_filename(names->names[i].filename)) { |
| 1356 | tell_user(stderr, "ignoring potentially dangerous server-" |
| 1357 | "supplied filename '%s'\n", |
| 1358 | names->names[i].filename); |
| 1359 | } else |
| 1360 | ournames[nnames++] = names->names[i]; |
| 1361 | } |
| 1362 | names->nnames = 0; /* prevent free_names */ |
| 1363 | fxp_free_names(names); |
| 1364 | } |
| 1365 | sftp_register(req = fxp_close_send(dirhandle)); |
| 1366 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1367 | assert(rreq == req); |
| 1368 | fxp_close_recv(pktin, rreq); |
| 1369 | |
| 1370 | newitem = snew(struct scp_sftp_dirstack); |
| 1371 | newitem->next = scp_sftp_dirstack_head; |
| 1372 | newitem->names = ournames; |
| 1373 | newitem->namepos = 0; |
| 1374 | newitem->namelen = nnames; |
| 1375 | if (must_free_fname) |
| 1376 | newitem->dirpath = fname; |
| 1377 | else |
| 1378 | newitem->dirpath = dupstr(fname); |
| 1379 | if (scp_sftp_wildcard) { |
| 1380 | newitem->wildcard = scp_sftp_wildcard; |
| 1381 | newitem->matched_something = 0; |
| 1382 | scp_sftp_wildcard = NULL; |
| 1383 | } else { |
| 1384 | newitem->wildcard = NULL; |
| 1385 | } |
| 1386 | scp_sftp_dirstack_head = newitem; |
| 1387 | |
| 1388 | if (newitem->wildcard) { |
| 1389 | act->action = SCP_SINK_RETRY; |
| 1390 | } else { |
| 1391 | act->action = SCP_SINK_DIR; |
| 1392 | act->buf = dupstr(stripslashes(fname, 0)); |
| 1393 | act->name = act->buf; |
| 1394 | act->size = uint64_make(0,0); /* duhh, it's a directory */ |
| 1395 | act->permissions = 07777 & attrs.permissions; |
| 1396 | if (scp_sftp_preserve && |
| 1397 | (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) { |
| 1398 | act->atime = attrs.atime; |
| 1399 | act->mtime = attrs.mtime; |
| 1400 | act->settime = 1; |
| 1401 | } else |
| 1402 | act->settime = 0; |
| 1403 | } |
| 1404 | return 0; |
| 1405 | |
| 1406 | } else { |
| 1407 | /* |
| 1408 | * It's a file. Return SCP_SINK_FILE. |
| 1409 | */ |
| 1410 | act->action = SCP_SINK_FILE; |
| 1411 | act->buf = dupstr(stripslashes(fname, 0)); |
| 1412 | act->name = act->buf; |
| 1413 | if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) { |
| 1414 | act->size = attrs.size; |
| 1415 | } else |
| 1416 | act->size = uint64_make(ULONG_MAX,ULONG_MAX); /* no idea */ |
| 1417 | act->permissions = 07777 & attrs.permissions; |
| 1418 | if (scp_sftp_preserve && |
| 1419 | (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) { |
| 1420 | act->atime = attrs.atime; |
| 1421 | act->mtime = attrs.mtime; |
| 1422 | act->settime = 1; |
| 1423 | } else |
| 1424 | act->settime = 0; |
| 1425 | if (must_free_fname) |
| 1426 | scp_sftp_currentname = fname; |
| 1427 | else |
| 1428 | scp_sftp_currentname = dupstr(fname); |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | } else { |
| 1433 | int done = 0; |
| 1434 | int i, bufsize; |
| 1435 | int action; |
| 1436 | char ch; |
| 1437 | |
| 1438 | act->settime = 0; |
| 1439 | act->buf = NULL; |
| 1440 | bufsize = 0; |
| 1441 | |
| 1442 | while (!done) { |
| 1443 | if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0) |
| 1444 | return 1; |
| 1445 | if (ch == '\n') |
| 1446 | bump("Protocol error: Unexpected newline"); |
| 1447 | i = 0; |
| 1448 | action = ch; |
| 1449 | do { |
| 1450 | if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0) |
| 1451 | bump("Lost connection"); |
| 1452 | if (i >= bufsize) { |
| 1453 | bufsize = i + 128; |
| 1454 | act->buf = sresize(act->buf, bufsize, char); |
| 1455 | } |
| 1456 | act->buf[i++] = ch; |
| 1457 | } while (ch != '\n'); |
| 1458 | act->buf[i - 1] = '\0'; |
| 1459 | switch (action) { |
| 1460 | case '\01': /* error */ |
| 1461 | tell_user(stderr, "%s\n", act->buf); |
| 1462 | errs++; |
| 1463 | continue; /* go round again */ |
| 1464 | case '\02': /* fatal error */ |
| 1465 | bump("%s", act->buf); |
| 1466 | case 'E': |
| 1467 | back->send(backhandle, "", 1); |
| 1468 | act->action = SCP_SINK_ENDDIR; |
| 1469 | return 0; |
| 1470 | case 'T': |
| 1471 | if (sscanf(act->buf, "%ld %*d %ld %*d", |
| 1472 | &act->mtime, &act->atime) == 2) { |
| 1473 | act->settime = 1; |
| 1474 | back->send(backhandle, "", 1); |
| 1475 | continue; /* go round again */ |
| 1476 | } |
| 1477 | bump("Protocol error: Illegal time format"); |
| 1478 | case 'C': |
| 1479 | case 'D': |
| 1480 | act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR); |
| 1481 | break; |
| 1482 | default: |
| 1483 | bump("Protocol error: Expected control record"); |
| 1484 | } |
| 1485 | /* |
| 1486 | * We will go round this loop only once, unless we hit |
| 1487 | * `continue' above. |
| 1488 | */ |
| 1489 | done = 1; |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * If we get here, we must have seen SCP_SINK_FILE or |
| 1494 | * SCP_SINK_DIR. |
| 1495 | */ |
| 1496 | { |
| 1497 | char sizestr[40]; |
| 1498 | |
| 1499 | if (sscanf(act->buf, "%lo %s %n", &act->permissions, |
| 1500 | sizestr, &i) != 2) |
| 1501 | bump("Protocol error: Illegal file descriptor format"); |
| 1502 | act->size = uint64_from_decimal(sizestr); |
| 1503 | act->name = act->buf + i; |
| 1504 | return 0; |
| 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | int scp_accept_filexfer(void) |
| 1510 | { |
| 1511 | if (using_sftp) { |
| 1512 | struct sftp_packet *pktin; |
| 1513 | struct sftp_request *req, *rreq; |
| 1514 | |
| 1515 | sftp_register(req = fxp_open_send(scp_sftp_currentname, SSH_FXF_READ, |
| 1516 | NULL)); |
| 1517 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1518 | assert(rreq == req); |
| 1519 | scp_sftp_filehandle = fxp_open_recv(pktin, rreq); |
| 1520 | |
| 1521 | if (!scp_sftp_filehandle) { |
| 1522 | tell_user(stderr, "pscp: unable to open %s: %s", |
| 1523 | scp_sftp_currentname, fxp_error()); |
| 1524 | errs++; |
| 1525 | return 1; |
| 1526 | } |
| 1527 | scp_sftp_fileoffset = uint64_make(0, 0); |
| 1528 | scp_sftp_xfer = xfer_download_init(scp_sftp_filehandle, |
| 1529 | scp_sftp_fileoffset); |
| 1530 | sfree(scp_sftp_currentname); |
| 1531 | return 0; |
| 1532 | } else { |
| 1533 | back->send(backhandle, "", 1); |
| 1534 | return 0; /* can't fail */ |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | int scp_recv_filedata(char *data, int len) |
| 1539 | { |
| 1540 | if (using_sftp) { |
| 1541 | struct sftp_packet *pktin; |
| 1542 | int ret, actuallen; |
| 1543 | void *vbuf; |
| 1544 | |
| 1545 | xfer_download_queue(scp_sftp_xfer); |
| 1546 | pktin = sftp_recv(); |
| 1547 | ret = xfer_download_gotpkt(scp_sftp_xfer, pktin); |
| 1548 | |
| 1549 | if (ret < 0) { |
| 1550 | tell_user(stderr, "pscp: error while reading: %s", fxp_error()); |
| 1551 | errs++; |
| 1552 | return -1; |
| 1553 | } |
| 1554 | |
| 1555 | if (xfer_download_data(scp_sftp_xfer, &vbuf, &actuallen)) { |
| 1556 | /* |
| 1557 | * This assertion relies on the fact that the natural |
| 1558 | * block size used in the xfer manager is at most that |
| 1559 | * used in this module. I don't like crossing layers in |
| 1560 | * this way, but it'll do for now. |
| 1561 | */ |
| 1562 | assert(actuallen <= len); |
| 1563 | memcpy(data, vbuf, actuallen); |
| 1564 | sfree(vbuf); |
| 1565 | } else |
| 1566 | actuallen = 0; |
| 1567 | |
| 1568 | scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, actuallen); |
| 1569 | |
| 1570 | return actuallen; |
| 1571 | } else { |
| 1572 | return ssh_scp_recv((unsigned char *) data, len); |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | int scp_finish_filerecv(void) |
| 1577 | { |
| 1578 | if (using_sftp) { |
| 1579 | struct sftp_packet *pktin; |
| 1580 | struct sftp_request *req, *rreq; |
| 1581 | |
| 1582 | /* |
| 1583 | * Ensure that xfer_done() will work correctly, so we can |
| 1584 | * clean up any outstanding requests from the file |
| 1585 | * transfer. |
| 1586 | */ |
| 1587 | xfer_set_error(scp_sftp_xfer); |
| 1588 | while (!xfer_done(scp_sftp_xfer)) { |
| 1589 | void *vbuf; |
| 1590 | int len; |
| 1591 | |
| 1592 | pktin = sftp_recv(); |
| 1593 | xfer_download_gotpkt(scp_sftp_xfer, pktin); |
| 1594 | if (xfer_download_data(scp_sftp_xfer, &vbuf, &len)) |
| 1595 | sfree(vbuf); |
| 1596 | } |
| 1597 | xfer_cleanup(scp_sftp_xfer); |
| 1598 | |
| 1599 | sftp_register(req = fxp_close_send(scp_sftp_filehandle)); |
| 1600 | rreq = sftp_find_request(pktin = sftp_recv()); |
| 1601 | assert(rreq == req); |
| 1602 | fxp_close_recv(pktin, rreq); |
| 1603 | return 0; |
| 1604 | } else { |
| 1605 | back->send(backhandle, "", 1); |
| 1606 | return response(); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* ---------------------------------------------------------------------- |
| 1611 | * Send an error message to the other side and to the screen. |
| 1612 | * Increment error counter. |
| 1613 | */ |
| 1614 | static void run_err(const char *fmt, ...) |
| 1615 | { |
| 1616 | char *str, *str2; |
| 1617 | va_list ap; |
| 1618 | va_start(ap, fmt); |
| 1619 | errs++; |
| 1620 | str = dupvprintf(fmt, ap); |
| 1621 | str2 = dupcat("scp: ", str, "\n", NULL); |
| 1622 | sfree(str); |
| 1623 | scp_send_errmsg(str2); |
| 1624 | tell_user(stderr, "%s", str2); |
| 1625 | va_end(ap); |
| 1626 | sfree(str2); |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Execute the source part of the SCP protocol. |
| 1631 | */ |
| 1632 | static void source(char *src) |
| 1633 | { |
| 1634 | uint64 size; |
| 1635 | unsigned long mtime, atime; |
| 1636 | long permissions; |
| 1637 | char *last; |
| 1638 | RFile *f; |
| 1639 | int attr; |
| 1640 | uint64 i; |
| 1641 | uint64 stat_bytes; |
| 1642 | time_t stat_starttime, stat_lasttime; |
| 1643 | |
| 1644 | attr = file_type(src); |
| 1645 | if (attr == FILE_TYPE_NONEXISTENT || |
| 1646 | attr == FILE_TYPE_WEIRD) { |
| 1647 | run_err("%s: %s file or directory", src, |
| 1648 | (attr == FILE_TYPE_WEIRD ? "Not a" : "No such")); |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | if (attr == FILE_TYPE_DIRECTORY) { |
| 1653 | if (recursive) { |
| 1654 | /* |
| 1655 | * Avoid . and .. directories. |
| 1656 | */ |
| 1657 | char *p; |
| 1658 | p = strrchr(src, '/'); |
| 1659 | if (!p) |
| 1660 | p = strrchr(src, '\\'); |
| 1661 | if (!p) |
| 1662 | p = src; |
| 1663 | else |
| 1664 | p++; |
| 1665 | if (!strcmp(p, ".") || !strcmp(p, "..")) |
| 1666 | /* skip . and .. */ ; |
| 1667 | else |
| 1668 | rsource(src); |
| 1669 | } else { |
| 1670 | run_err("%s: not a regular file", src); |
| 1671 | } |
| 1672 | return; |
| 1673 | } |
| 1674 | |
| 1675 | if ((last = strrchr(src, '/')) == NULL) |
| 1676 | last = src; |
| 1677 | else |
| 1678 | last++; |
| 1679 | if (strrchr(last, '\\') != NULL) |
| 1680 | last = strrchr(last, '\\') + 1; |
| 1681 | if (last == src && strchr(src, ':') != NULL) |
| 1682 | last = strchr(src, ':') + 1; |
| 1683 | |
| 1684 | f = open_existing_file(src, &size, &mtime, &atime, &permissions); |
| 1685 | if (f == NULL) { |
| 1686 | run_err("%s: Cannot open file", src); |
| 1687 | return; |
| 1688 | } |
| 1689 | if (preserve) { |
| 1690 | if (scp_send_filetimes(mtime, atime)) |
| 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | if (verbose) { |
| 1695 | char sizestr[40]; |
| 1696 | uint64_decimal(size, sizestr); |
| 1697 | tell_user(stderr, "Sending file %s, size=%s", last, sizestr); |
| 1698 | } |
| 1699 | if (scp_send_filename(last, size, permissions)) |
| 1700 | return; |
| 1701 | |
| 1702 | stat_bytes = uint64_make(0,0); |
| 1703 | stat_starttime = time(NULL); |
| 1704 | stat_lasttime = 0; |
| 1705 | |
| 1706 | for (i = uint64_make(0,0); |
| 1707 | uint64_compare(i,size) < 0; |
| 1708 | i = uint64_add32(i,4096)) { |
| 1709 | char transbuf[4096]; |
| 1710 | int j, k = 4096; |
| 1711 | |
| 1712 | if (uint64_compare(uint64_add32(i, k),size) > 0) /* i + k > size */ |
| 1713 | k = (uint64_subtract(size, i)).lo; /* k = size - i; */ |
| 1714 | if ((j = read_from_file(f, transbuf, k)) != k) { |
| 1715 | if (statistics) |
| 1716 | printf("\n"); |
| 1717 | bump("%s: Read error", src); |
| 1718 | } |
| 1719 | if (scp_send_filedata(transbuf, k)) |
| 1720 | bump("%s: Network error occurred", src); |
| 1721 | |
| 1722 | if (statistics) { |
| 1723 | stat_bytes = uint64_add32(stat_bytes, k); |
| 1724 | if (time(NULL) != stat_lasttime || |
| 1725 | (uint64_compare(uint64_add32(i, k), size) == 0)) { |
| 1726 | stat_lasttime = time(NULL); |
| 1727 | print_stats(last, size, stat_bytes, |
| 1728 | stat_starttime, stat_lasttime); |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | } |
| 1733 | close_rfile(f); |
| 1734 | |
| 1735 | (void) scp_send_finish(); |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * Recursively send the contents of a directory. |
| 1740 | */ |
| 1741 | static void rsource(char *src) |
| 1742 | { |
| 1743 | char *last; |
| 1744 | char *save_target; |
| 1745 | DirHandle *dir; |
| 1746 | |
| 1747 | if ((last = strrchr(src, '/')) == NULL) |
| 1748 | last = src; |
| 1749 | else |
| 1750 | last++; |
| 1751 | if (strrchr(last, '\\') != NULL) |
| 1752 | last = strrchr(last, '\\') + 1; |
| 1753 | if (last == src && strchr(src, ':') != NULL) |
| 1754 | last = strchr(src, ':') + 1; |
| 1755 | |
| 1756 | /* maybe send filetime */ |
| 1757 | |
| 1758 | save_target = scp_save_remotepath(); |
| 1759 | |
| 1760 | if (verbose) |
| 1761 | tell_user(stderr, "Entering directory: %s", last); |
| 1762 | if (scp_send_dirname(last, 0755)) |
| 1763 | return; |
| 1764 | |
| 1765 | dir = open_directory(src); |
| 1766 | if (dir != NULL) { |
| 1767 | char *filename; |
| 1768 | while ((filename = read_filename(dir)) != NULL) { |
| 1769 | char *foundfile = dupcat(src, "/", filename, NULL); |
| 1770 | source(foundfile); |
| 1771 | sfree(foundfile); |
| 1772 | sfree(filename); |
| 1773 | } |
| 1774 | } |
| 1775 | close_directory(dir); |
| 1776 | |
| 1777 | (void) scp_send_enddir(); |
| 1778 | |
| 1779 | scp_restore_remotepath(save_target); |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Execute the sink part of the SCP protocol. |
| 1784 | */ |
| 1785 | static void sink(char *targ, char *src) |
| 1786 | { |
| 1787 | char *destfname; |
| 1788 | int targisdir = 0; |
| 1789 | int exists; |
| 1790 | int attr; |
| 1791 | WFile *f; |
| 1792 | uint64 received; |
| 1793 | int wrerror = 0; |
| 1794 | uint64 stat_bytes; |
| 1795 | time_t stat_starttime, stat_lasttime; |
| 1796 | char *stat_name; |
| 1797 | |
| 1798 | attr = file_type(targ); |
| 1799 | if (attr == FILE_TYPE_DIRECTORY) |
| 1800 | targisdir = 1; |
| 1801 | |
| 1802 | if (targetshouldbedirectory && !targisdir) |
| 1803 | bump("%s: Not a directory", targ); |
| 1804 | |
| 1805 | scp_sink_init(); |
| 1806 | while (1) { |
| 1807 | struct scp_sink_action act; |
| 1808 | if (scp_get_sink_action(&act)) |
| 1809 | return; |
| 1810 | |
| 1811 | if (act.action == SCP_SINK_ENDDIR) |
| 1812 | return; |
| 1813 | |
| 1814 | if (act.action == SCP_SINK_RETRY) |
| 1815 | continue; |
| 1816 | |
| 1817 | if (targisdir) { |
| 1818 | /* |
| 1819 | * Prevent the remote side from maliciously writing to |
| 1820 | * files outside the target area by sending a filename |
| 1821 | * containing `../'. In fact, it shouldn't be sending |
| 1822 | * filenames with any slashes or colons in at all; so |
| 1823 | * we'll find the last slash, backslash or colon in the |
| 1824 | * filename and use only the part after that. (And |
| 1825 | * warn!) |
| 1826 | * |
| 1827 | * In addition, we also ensure here that if we're |
| 1828 | * copying a single file and the target is a directory |
| 1829 | * (common usage: `pscp host:filename .') the remote |
| 1830 | * can't send us a _different_ file name. We can |
| 1831 | * distinguish this case because `src' will be non-NULL |
| 1832 | * and the last component of that will fail to match |
| 1833 | * (the last component of) the name sent. |
| 1834 | * |
| 1835 | * Well, not always; if `src' is a wildcard, we do |
| 1836 | * expect to get back filenames that don't correspond |
| 1837 | * exactly to it. Ideally in this case, we would like |
| 1838 | * to ensure that the returned filename actually |
| 1839 | * matches the wildcard pattern - but one of SCP's |
| 1840 | * protocol infelicities is that wildcard matching is |
| 1841 | * done at the server end _by the server's rules_ and |
| 1842 | * so in general this is infeasible. Hence, we only |
| 1843 | * accept filenames that don't correspond to `src' if |
| 1844 | * unsafe mode is enabled or we are using SFTP (which |
| 1845 | * resolves remote wildcards on the client side and can |
| 1846 | * be trusted). |
| 1847 | */ |
| 1848 | char *striptarget, *stripsrc; |
| 1849 | |
| 1850 | striptarget = stripslashes(act.name, 1); |
| 1851 | if (striptarget != act.name) { |
| 1852 | tell_user(stderr, "warning: remote host sent a compound" |
| 1853 | " pathname '%s'", act.name); |
| 1854 | tell_user(stderr, " renaming local file to '%s'", |
| 1855 | striptarget); |
| 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | * Also check to see if the target filename is '.' or |
| 1860 | * '..', or indeed '...' and so on because Windows |
| 1861 | * appears to interpret those like '..'. |
| 1862 | */ |
| 1863 | if (is_dots(striptarget)) { |
| 1864 | bump("security violation: remote host attempted to write to" |
| 1865 | " a '.' or '..' path!"); |
| 1866 | } |
| 1867 | |
| 1868 | if (src) { |
| 1869 | stripsrc = stripslashes(src, 1); |
| 1870 | if (strcmp(striptarget, stripsrc) && |
| 1871 | !using_sftp && !scp_unsafe_mode) { |
| 1872 | tell_user(stderr, "warning: remote host tried to write " |
| 1873 | "to a file called '%s'", striptarget); |
| 1874 | tell_user(stderr, " when we requested a file " |
| 1875 | "called '%s'.", stripsrc); |
| 1876 | tell_user(stderr, " If this is a wildcard, " |
| 1877 | "consider upgrading to SSH-2 or using"); |
| 1878 | tell_user(stderr, " the '-unsafe' option. Renaming" |
| 1879 | " of this file has been disallowed."); |
| 1880 | /* Override the name the server provided with our own. */ |
| 1881 | striptarget = stripsrc; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | if (targ[0] != '\0') |
| 1886 | destfname = dir_file_cat(targ, striptarget); |
| 1887 | else |
| 1888 | destfname = dupstr(striptarget); |
| 1889 | } else { |
| 1890 | /* |
| 1891 | * In this branch of the if, the target area is a |
| 1892 | * single file with an explicitly specified name in any |
| 1893 | * case, so there's no danger. |
| 1894 | */ |
| 1895 | destfname = dupstr(targ); |
| 1896 | } |
| 1897 | attr = file_type(destfname); |
| 1898 | exists = (attr != FILE_TYPE_NONEXISTENT); |
| 1899 | |
| 1900 | if (act.action == SCP_SINK_DIR) { |
| 1901 | if (exists && attr != FILE_TYPE_DIRECTORY) { |
| 1902 | run_err("%s: Not a directory", destfname); |
| 1903 | continue; |
| 1904 | } |
| 1905 | if (!exists) { |
| 1906 | if (!create_directory(destfname)) { |
| 1907 | run_err("%s: Cannot create directory", destfname); |
| 1908 | continue; |
| 1909 | } |
| 1910 | } |
| 1911 | sink(destfname, NULL); |
| 1912 | /* can we set the timestamp for directories ? */ |
| 1913 | continue; |
| 1914 | } |
| 1915 | |
| 1916 | f = open_new_file(destfname, act.permissions); |
| 1917 | if (f == NULL) { |
| 1918 | run_err("%s: Cannot create file", destfname); |
| 1919 | continue; |
| 1920 | } |
| 1921 | |
| 1922 | if (scp_accept_filexfer()) |
| 1923 | return; |
| 1924 | |
| 1925 | stat_bytes = uint64_make(0, 0); |
| 1926 | stat_starttime = time(NULL); |
| 1927 | stat_lasttime = 0; |
| 1928 | stat_name = stripslashes(destfname, 1); |
| 1929 | |
| 1930 | received = uint64_make(0, 0); |
| 1931 | while (uint64_compare(received,act.size) < 0) { |
| 1932 | char transbuf[32768]; |
| 1933 | uint64 blksize; |
| 1934 | int read; |
| 1935 | blksize = uint64_make(0, 32768); |
| 1936 | if (uint64_compare(blksize,uint64_subtract(act.size,received)) > 0) |
| 1937 | blksize = uint64_subtract(act.size,received); |
| 1938 | read = scp_recv_filedata(transbuf, (int)blksize.lo); |
| 1939 | if (read <= 0) |
| 1940 | bump("Lost connection"); |
| 1941 | if (wrerror) |
| 1942 | continue; |
| 1943 | if (write_to_file(f, transbuf, read) != (int)read) { |
| 1944 | wrerror = 1; |
| 1945 | /* FIXME: in sftp we can actually abort the transfer */ |
| 1946 | if (statistics) |
| 1947 | printf("\r%-25.25s | %50s\n", |
| 1948 | stat_name, |
| 1949 | "Write error.. waiting for end of file"); |
| 1950 | continue; |
| 1951 | } |
| 1952 | if (statistics) { |
| 1953 | stat_bytes = uint64_add32(stat_bytes,read); |
| 1954 | if (time(NULL) > stat_lasttime || |
| 1955 | uint64_compare(uint64_add32(received, read), act.size) == 0) { |
| 1956 | stat_lasttime = time(NULL); |
| 1957 | print_stats(stat_name, act.size, stat_bytes, |
| 1958 | stat_starttime, stat_lasttime); |
| 1959 | } |
| 1960 | } |
| 1961 | received = uint64_add32(received, read); |
| 1962 | } |
| 1963 | if (act.settime) { |
| 1964 | set_file_times(f, act.mtime, act.atime); |
| 1965 | } |
| 1966 | |
| 1967 | close_wfile(f); |
| 1968 | if (wrerror) { |
| 1969 | run_err("%s: Write error", destfname); |
| 1970 | continue; |
| 1971 | } |
| 1972 | (void) scp_finish_filerecv(); |
| 1973 | sfree(destfname); |
| 1974 | sfree(act.buf); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | /* |
| 1979 | * We will copy local files to a remote server. |
| 1980 | */ |
| 1981 | static void toremote(int argc, char *argv[]) |
| 1982 | { |
| 1983 | char *src, *targ, *host, *user; |
| 1984 | char *cmd; |
| 1985 | int i, wc_type; |
| 1986 | |
| 1987 | targ = argv[argc - 1]; |
| 1988 | |
| 1989 | /* Separate host from filename */ |
| 1990 | host = targ; |
| 1991 | targ = colon(targ); |
| 1992 | if (targ == NULL) |
| 1993 | bump("targ == NULL in toremote()"); |
| 1994 | *targ++ = '\0'; |
| 1995 | if (*targ == '\0') |
| 1996 | targ = "."; |
| 1997 | /* Substitute "." for empty target */ |
| 1998 | |
| 1999 | /* Separate host and username */ |
| 2000 | user = host; |
| 2001 | host = strrchr(host, '@'); |
| 2002 | if (host == NULL) { |
| 2003 | host = user; |
| 2004 | user = NULL; |
| 2005 | } else { |
| 2006 | *host++ = '\0'; |
| 2007 | if (*user == '\0') |
| 2008 | user = NULL; |
| 2009 | } |
| 2010 | |
| 2011 | if (argc == 2) { |
| 2012 | if (colon(argv[0]) != NULL) |
| 2013 | bump("%s: Remote to remote not supported", argv[0]); |
| 2014 | |
| 2015 | wc_type = test_wildcard(argv[0], 1); |
| 2016 | if (wc_type == WCTYPE_NONEXISTENT) |
| 2017 | bump("%s: No such file or directory\n", argv[0]); |
| 2018 | else if (wc_type == WCTYPE_WILDCARD) |
| 2019 | targetshouldbedirectory = 1; |
| 2020 | } |
| 2021 | |
| 2022 | cmd = dupprintf("scp%s%s%s%s -t %s", |
| 2023 | verbose ? " -v" : "", |
| 2024 | recursive ? " -r" : "", |
| 2025 | preserve ? " -p" : "", |
| 2026 | targetshouldbedirectory ? " -d" : "", targ); |
| 2027 | do_cmd(host, user, cmd); |
| 2028 | sfree(cmd); |
| 2029 | |
| 2030 | if (scp_source_setup(targ, targetshouldbedirectory)) |
| 2031 | return; |
| 2032 | |
| 2033 | for (i = 0; i < argc - 1; i++) { |
| 2034 | src = argv[i]; |
| 2035 | if (colon(src) != NULL) { |
| 2036 | tell_user(stderr, "%s: Remote to remote not supported\n", src); |
| 2037 | errs++; |
| 2038 | continue; |
| 2039 | } |
| 2040 | |
| 2041 | wc_type = test_wildcard(src, 1); |
| 2042 | if (wc_type == WCTYPE_NONEXISTENT) { |
| 2043 | run_err("%s: No such file or directory", src); |
| 2044 | continue; |
| 2045 | } else if (wc_type == WCTYPE_FILENAME) { |
| 2046 | source(src); |
| 2047 | continue; |
| 2048 | } else { |
| 2049 | WildcardMatcher *wc; |
| 2050 | char *filename; |
| 2051 | |
| 2052 | wc = begin_wildcard_matching(src); |
| 2053 | if (wc == NULL) { |
| 2054 | run_err("%s: No such file or directory", src); |
| 2055 | continue; |
| 2056 | } |
| 2057 | |
| 2058 | while ((filename = wildcard_get_filename(wc)) != NULL) { |
| 2059 | source(filename); |
| 2060 | sfree(filename); |
| 2061 | } |
| 2062 | |
| 2063 | finish_wildcard_matching(wc); |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * We will copy files from a remote server to the local machine. |
| 2070 | */ |
| 2071 | static void tolocal(int argc, char *argv[]) |
| 2072 | { |
| 2073 | char *src, *targ, *host, *user; |
| 2074 | char *cmd; |
| 2075 | |
| 2076 | if (argc != 2) |
| 2077 | bump("More than one remote source not supported"); |
| 2078 | |
| 2079 | src = argv[0]; |
| 2080 | targ = argv[1]; |
| 2081 | |
| 2082 | /* Separate host from filename */ |
| 2083 | host = src; |
| 2084 | src = colon(src); |
| 2085 | if (src == NULL) |
| 2086 | bump("Local to local copy not supported"); |
| 2087 | *src++ = '\0'; |
| 2088 | if (*src == '\0') |
| 2089 | src = "."; |
| 2090 | /* Substitute "." for empty filename */ |
| 2091 | |
| 2092 | /* Separate username and hostname */ |
| 2093 | user = host; |
| 2094 | host = strrchr(host, '@'); |
| 2095 | if (host == NULL) { |
| 2096 | host = user; |
| 2097 | user = NULL; |
| 2098 | } else { |
| 2099 | *host++ = '\0'; |
| 2100 | if (*user == '\0') |
| 2101 | user = NULL; |
| 2102 | } |
| 2103 | |
| 2104 | cmd = dupprintf("scp%s%s%s%s -f %s", |
| 2105 | verbose ? " -v" : "", |
| 2106 | recursive ? " -r" : "", |
| 2107 | preserve ? " -p" : "", |
| 2108 | targetshouldbedirectory ? " -d" : "", src); |
| 2109 | do_cmd(host, user, cmd); |
| 2110 | sfree(cmd); |
| 2111 | |
| 2112 | if (scp_sink_setup(src, preserve, recursive)) |
| 2113 | return; |
| 2114 | |
| 2115 | sink(targ, src); |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * We will issue a list command to get a remote directory. |
| 2120 | */ |
| 2121 | static void get_dir_list(int argc, char *argv[]) |
| 2122 | { |
| 2123 | char *src, *host, *user; |
| 2124 | char *cmd, *p, *q; |
| 2125 | char c; |
| 2126 | |
| 2127 | src = argv[0]; |
| 2128 | |
| 2129 | /* Separate host from filename */ |
| 2130 | host = src; |
| 2131 | src = colon(src); |
| 2132 | if (src == NULL) |
| 2133 | bump("Local file listing not supported"); |
| 2134 | *src++ = '\0'; |
| 2135 | if (*src == '\0') |
| 2136 | src = "."; |
| 2137 | /* Substitute "." for empty filename */ |
| 2138 | |
| 2139 | /* Separate username and hostname */ |
| 2140 | user = host; |
| 2141 | host = strrchr(host, '@'); |
| 2142 | if (host == NULL) { |
| 2143 | host = user; |
| 2144 | user = NULL; |
| 2145 | } else { |
| 2146 | *host++ = '\0'; |
| 2147 | if (*user == '\0') |
| 2148 | user = NULL; |
| 2149 | } |
| 2150 | |
| 2151 | cmd = snewn(4 * strlen(src) + 100, char); |
| 2152 | strcpy(cmd, "ls -la '"); |
| 2153 | p = cmd + strlen(cmd); |
| 2154 | for (q = src; *q; q++) { |
| 2155 | if (*q == '\'') { |
| 2156 | *p++ = '\''; |
| 2157 | *p++ = '\\'; |
| 2158 | *p++ = '\''; |
| 2159 | *p++ = '\''; |
| 2160 | } else { |
| 2161 | *p++ = *q; |
| 2162 | } |
| 2163 | } |
| 2164 | *p++ = '\''; |
| 2165 | *p = '\0'; |
| 2166 | |
| 2167 | do_cmd(host, user, cmd); |
| 2168 | sfree(cmd); |
| 2169 | |
| 2170 | if (using_sftp) { |
| 2171 | scp_sftp_listdir(src); |
| 2172 | } else { |
| 2173 | while (ssh_scp_recv((unsigned char *) &c, 1) > 0) |
| 2174 | tell_char(stdout, c); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * Short description of parameters. |
| 2180 | */ |
| 2181 | static void usage(void) |
| 2182 | { |
| 2183 | printf("PuTTY Secure Copy client\n"); |
| 2184 | printf("%s\n", ver); |
| 2185 | printf("Usage: pscp [options] [user@]host:source target\n"); |
| 2186 | printf |
| 2187 | (" pscp [options] source [source...] [user@]host:target\n"); |
| 2188 | printf(" pscp [options] -ls [user@]host:filespec\n"); |
| 2189 | printf("Options:\n"); |
| 2190 | printf(" -V print version information and exit\n"); |
| 2191 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
| 2192 | printf(" -p preserve file attributes\n"); |
| 2193 | printf(" -q quiet, don't show statistics\n"); |
| 2194 | printf(" -r copy directories recursively\n"); |
| 2195 | printf(" -v show verbose messages\n"); |
| 2196 | printf(" -load sessname Load settings from saved session\n"); |
| 2197 | printf(" -P port connect to specified port\n"); |
| 2198 | printf(" -l user connect with specified username\n"); |
| 2199 | printf(" -pw passw login with specified password\n"); |
| 2200 | printf(" -1 -2 force use of particular SSH protocol version\n"); |
| 2201 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
| 2202 | printf(" -C enable compression\n"); |
| 2203 | printf(" -i key private key file for authentication\n"); |
| 2204 | printf(" -noagent disable use of Pageant\n"); |
| 2205 | printf(" -agent enable use of Pageant\n"); |
| 2206 | printf(" -batch disable all interactive prompts\n"); |
| 2207 | printf(" -unsafe allow server-side wildcards (DANGEROUS)\n"); |
| 2208 | printf(" -sftp force use of SFTP protocol\n"); |
| 2209 | printf(" -scp force use of SCP protocol\n"); |
| 2210 | #if 0 |
| 2211 | /* |
| 2212 | * -gui is an internal option, used by GUI front ends to get |
| 2213 | * pscp to pass progress reports back to them. It's not an |
| 2214 | * ordinary user-accessible option, so it shouldn't be part of |
| 2215 | * the command-line help. The only people who need to know |
| 2216 | * about it are programmers, and they can read the source. |
| 2217 | */ |
| 2218 | printf |
| 2219 | (" -gui hWnd GUI mode with the windows handle for receiving messages\n"); |
| 2220 | #endif |
| 2221 | cleanup_exit(1); |
| 2222 | } |
| 2223 | |
| 2224 | void version(void) |
| 2225 | { |
| 2226 | printf("pscp: %s\n", ver); |
| 2227 | cleanup_exit(1); |
| 2228 | } |
| 2229 | |
| 2230 | void cmdline_error(char *p, ...) |
| 2231 | { |
| 2232 | va_list ap; |
| 2233 | fprintf(stderr, "pscp: "); |
| 2234 | va_start(ap, p); |
| 2235 | vfprintf(stderr, p, ap); |
| 2236 | va_end(ap); |
| 2237 | fprintf(stderr, "\n try typing just \"pscp\" for help\n"); |
| 2238 | exit(1); |
| 2239 | } |
| 2240 | |
| 2241 | /* |
| 2242 | * Main program. (Called `psftp_main' because it gets called from |
| 2243 | * *sftp.c; bit silly, I know, but it had to be called _something_.) |
| 2244 | */ |
| 2245 | int psftp_main(int argc, char *argv[]) |
| 2246 | { |
| 2247 | int i; |
| 2248 | |
| 2249 | default_protocol = PROT_TELNET; |
| 2250 | |
| 2251 | flags = FLAG_STDERR |
| 2252 | #ifdef FLAG_SYNCAGENT |
| 2253 | | FLAG_SYNCAGENT |
| 2254 | #endif |
| 2255 | ; |
| 2256 | cmdline_tooltype = TOOLTYPE_FILETRANSFER; |
| 2257 | sk_init(); |
| 2258 | |
| 2259 | /* Load Default Settings before doing anything else. */ |
| 2260 | conf = conf_new(); |
| 2261 | do_defaults(NULL, conf); |
| 2262 | loaded_session = FALSE; |
| 2263 | |
| 2264 | for (i = 1; i < argc; i++) { |
| 2265 | int ret; |
| 2266 | if (argv[i][0] != '-') |
| 2267 | break; |
| 2268 | ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, conf); |
| 2269 | if (ret == -2) { |
| 2270 | cmdline_error("option \"%s\" requires an argument", argv[i]); |
| 2271 | } else if (ret == 2) { |
| 2272 | i++; /* skip next argument */ |
| 2273 | } else if (ret == 1) { |
| 2274 | /* We have our own verbosity in addition to `flags'. */ |
| 2275 | if (flags & FLAG_VERBOSE) |
| 2276 | verbose = 1; |
| 2277 | } else if (strcmp(argv[i], "-pgpfp") == 0) { |
| 2278 | pgp_fingerprints(); |
| 2279 | return 1; |
| 2280 | } else if (strcmp(argv[i], "-r") == 0) { |
| 2281 | recursive = 1; |
| 2282 | } else if (strcmp(argv[i], "-p") == 0) { |
| 2283 | preserve = 1; |
| 2284 | } else if (strcmp(argv[i], "-q") == 0) { |
| 2285 | statistics = 0; |
| 2286 | } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) { |
| 2287 | usage(); |
| 2288 | } else if (strcmp(argv[i], "-V") == 0) { |
| 2289 | version(); |
| 2290 | } else if (strcmp(argv[i], "-ls") == 0) { |
| 2291 | list = 1; |
| 2292 | } else if (strcmp(argv[i], "-batch") == 0) { |
| 2293 | console_batch_mode = 1; |
| 2294 | } else if (strcmp(argv[i], "-unsafe") == 0) { |
| 2295 | scp_unsafe_mode = 1; |
| 2296 | } else if (strcmp(argv[i], "-sftp") == 0) { |
| 2297 | try_scp = 0; try_sftp = 1; |
| 2298 | } else if (strcmp(argv[i], "-scp") == 0) { |
| 2299 | try_scp = 1; try_sftp = 0; |
| 2300 | } else if (strcmp(argv[i], "--") == 0) { |
| 2301 | i++; |
| 2302 | break; |
| 2303 | } else { |
| 2304 | cmdline_error("unknown option \"%s\"", argv[i]); |
| 2305 | } |
| 2306 | } |
| 2307 | argc -= i; |
| 2308 | argv += i; |
| 2309 | back = NULL; |
| 2310 | |
| 2311 | if (list) { |
| 2312 | if (argc != 1) |
| 2313 | usage(); |
| 2314 | get_dir_list(argc, argv); |
| 2315 | |
| 2316 | } else { |
| 2317 | |
| 2318 | if (argc < 2) |
| 2319 | usage(); |
| 2320 | if (argc > 2) |
| 2321 | targetshouldbedirectory = 1; |
| 2322 | |
| 2323 | if (colon(argv[argc - 1]) != NULL) |
| 2324 | toremote(argc, argv); |
| 2325 | else |
| 2326 | tolocal(argc, argv); |
| 2327 | } |
| 2328 | |
| 2329 | if (back != NULL && back->connected(backhandle)) { |
| 2330 | char ch; |
| 2331 | back->special(backhandle, TS_EOF); |
| 2332 | sent_eof = TRUE; |
| 2333 | ssh_scp_recv((unsigned char *) &ch, 1); |
| 2334 | } |
| 2335 | random_save_seed(); |
| 2336 | |
| 2337 | cmdline_cleanup(); |
| 2338 | console_provide_logctx(NULL); |
| 2339 | back->free(backhandle); |
| 2340 | backhandle = NULL; |
| 2341 | back = NULL; |
| 2342 | sk_cleanup(); |
| 2343 | return (errs == 0 ? 0 : 1); |
| 2344 | } |
| 2345 | |
| 2346 | /* end */ |