| 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 | * Adaptations to enable connecting a GUI by L. Gunnarsson - Sept 2000 |
| 9 | */ |
| 10 | |
| 11 | #include <windows.h> |
| 12 | #ifndef AUTO_WINSOCK |
| 13 | #ifdef WINSOCK_TWO |
| 14 | #include <winsock2.h> |
| 15 | #else |
| 16 | #include <winsock.h> |
| 17 | #endif |
| 18 | #endif |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | #include <assert.h> |
| 24 | /* GUI Adaptation - Sept 2000 */ |
| 25 | #include <winuser.h> |
| 26 | #include <winbase.h> |
| 27 | |
| 28 | #define PUTTY_DO_GLOBALS |
| 29 | #include "putty.h" |
| 30 | #include "winstuff.h" |
| 31 | #include "storage.h" |
| 32 | |
| 33 | #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \ |
| 34 | ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000) |
| 35 | #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \ |
| 36 | ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600)) |
| 37 | |
| 38 | /* GUI Adaptation - Sept 2000 */ |
| 39 | #define WM_APP_BASE 0x8000 |
| 40 | #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 ) |
| 41 | #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 ) |
| 42 | #define WM_STATS_CHAR ( WM_APP_BASE+402 ) |
| 43 | #define WM_STATS_SIZE ( WM_APP_BASE+403 ) |
| 44 | #define WM_STATS_PERCENT ( WM_APP_BASE+404 ) |
| 45 | #define WM_STATS_ELAPSED ( WM_APP_BASE+405 ) |
| 46 | #define WM_RET_ERR_CNT ( WM_APP_BASE+406 ) |
| 47 | #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 ) |
| 48 | |
| 49 | static int verbose = 0; |
| 50 | static int recursive = 0; |
| 51 | static int preserve = 0; |
| 52 | static int targetshouldbedirectory = 0; |
| 53 | static int statistics = 1; |
| 54 | static int portnumber = 0; |
| 55 | static char *password = NULL; |
| 56 | static int errs = 0; |
| 57 | /* GUI Adaptation - Sept 2000 */ |
| 58 | #define NAME_STR_MAX 2048 |
| 59 | static char statname[NAME_STR_MAX+1]; |
| 60 | static unsigned long statsize = 0; |
| 61 | static int statperct = 0; |
| 62 | static unsigned long statelapsed = 0; |
| 63 | static int gui_mode = 0; |
| 64 | static char *gui_hwnd = NULL; |
| 65 | |
| 66 | static void source(char *src); |
| 67 | static void rsource(char *src); |
| 68 | static void sink(char *targ, char *src); |
| 69 | /* GUI Adaptation - Sept 2000 */ |
| 70 | static void tell_char(FILE *stream, char c); |
| 71 | static void tell_str(FILE *stream, char *str); |
| 72 | static void tell_user(FILE *stream, char *fmt, ...); |
| 73 | static void send_char_msg(unsigned int msg_id, char c); |
| 74 | static void send_str_msg(unsigned int msg_id, char *str); |
| 75 | static void gui_update_stats(char *name, unsigned long size, |
| 76 | int percentage, unsigned long elapsed); |
| 77 | |
| 78 | void logevent(char *string) { } |
| 79 | |
| 80 | void ldisc_send(char *buf, int len) { |
| 81 | /* |
| 82 | * This is only here because of the calls to ldisc_send(NULL, |
| 83 | * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc |
| 84 | * as an ldisc. So if we get called with any real data, I want |
| 85 | * to know about it. |
| 86 | */ |
| 87 | assert(len == 0); |
| 88 | } |
| 89 | |
| 90 | void verify_ssh_host_key(char *host, int port, char *keytype, |
| 91 | char *keystr, char *fingerprint) { |
| 92 | int ret; |
| 93 | |
| 94 | static const char absentmsg[] = |
| 95 | "The server's host key is not cached in the registry. You\n" |
| 96 | "have no guarantee that the server is the computer you\n" |
| 97 | "think it is.\n" |
| 98 | "The server's key fingerprint is:\n" |
| 99 | "%s\n" |
| 100 | "If you trust this host, enter \"y\" to add the key to\n" |
| 101 | "PuTTY's cache and carry on connecting.\n" |
| 102 | "If you do not trust this host, enter \"n\" to abandon the\n" |
| 103 | "connection.\n" |
| 104 | "Continue connecting? (y/n) "; |
| 105 | |
| 106 | static const char wrongmsg[] = |
| 107 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
| 108 | "The server's host key does not match the one PuTTY has\n" |
| 109 | "cached in the registry. This means that either the\n" |
| 110 | "server administrator has changed the host key, or you\n" |
| 111 | "have actually connected to another computer pretending\n" |
| 112 | "to be the server.\n" |
| 113 | "The new key fingerprint is:\n" |
| 114 | "%s\n" |
| 115 | "If you were expecting this change and trust the new key,\n" |
| 116 | "enter Yes to update PuTTY's cache and continue connecting.\n" |
| 117 | "If you want to carry on connecting but without updating\n" |
| 118 | "the cache, enter No.\n" |
| 119 | "If you want to abandon the connection completely, press\n" |
| 120 | "Return to cancel. Pressing Return is the ONLY guaranteed\n" |
| 121 | "safe choice.\n" |
| 122 | "Update cached key? (y/n, Return cancels connection) "; |
| 123 | |
| 124 | static const char abandoned[] = "Connection abandoned.\n"; |
| 125 | |
| 126 | char line[32]; |
| 127 | |
| 128 | /* |
| 129 | * Verify the key against the registry. |
| 130 | */ |
| 131 | ret = verify_host_key(host, port, keytype, keystr); |
| 132 | |
| 133 | if (ret == 0) /* success - key matched OK */ |
| 134 | return; |
| 135 | if (ret == 2) { /* key was different */ |
| 136 | fprintf(stderr, wrongmsg, fingerprint); |
| 137 | if (fgets(line, sizeof(line), stdin) && |
| 138 | line[0] != '\0' && line[0] != '\n') { |
| 139 | if (line[0] == 'y' || line[0] == 'Y') |
| 140 | store_host_key(host, port, keytype, keystr); |
| 141 | } else { |
| 142 | fprintf(stderr, abandoned); |
| 143 | exit(0); |
| 144 | } |
| 145 | } |
| 146 | if (ret == 1) { /* key was absent */ |
| 147 | fprintf(stderr, absentmsg, fingerprint); |
| 148 | if (fgets(line, sizeof(line), stdin) && |
| 149 | (line[0] == 'y' || line[0] == 'Y')) |
| 150 | store_host_key(host, port, keytype, keystr); |
| 151 | else { |
| 152 | fprintf(stderr, abandoned); |
| 153 | exit(0); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* GUI Adaptation - Sept 2000 */ |
| 159 | static void send_msg(HWND h, UINT message, WPARAM wParam) |
| 160 | { |
| 161 | while (!PostMessage( h, message, wParam, 0)) |
| 162 | SleepEx(1000,TRUE); |
| 163 | } |
| 164 | |
| 165 | static void tell_char(FILE *stream, char c) |
| 166 | { |
| 167 | if (!gui_mode) |
| 168 | fputc(c, stream); |
| 169 | else |
| 170 | { |
| 171 | unsigned int msg_id = WM_STD_OUT_CHAR; |
| 172 | if (stream == stderr) msg_id = WM_STD_ERR_CHAR; |
| 173 | send_msg( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)c ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void tell_str(FILE *stream, char *str) |
| 178 | { |
| 179 | unsigned int i; |
| 180 | |
| 181 | for( i = 0; i < strlen(str); ++i ) |
| 182 | tell_char(stream, str[i]); |
| 183 | } |
| 184 | |
| 185 | static void tell_user(FILE *stream, char *fmt, ...) |
| 186 | { |
| 187 | char str[0x100]; /* Make the size big enough */ |
| 188 | va_list ap; |
| 189 | va_start(ap, fmt); |
| 190 | vsprintf(str, fmt, ap); |
| 191 | va_end(ap); |
| 192 | strcat(str, "\n"); |
| 193 | tell_str(stream, str); |
| 194 | } |
| 195 | |
| 196 | static void gui_update_stats(char *name, unsigned long size, int percentage, unsigned long elapsed) |
| 197 | { |
| 198 | unsigned int i; |
| 199 | |
| 200 | if (strcmp(name,statname) != 0) |
| 201 | { |
| 202 | for( i = 0; i < strlen(name); ++i ) |
| 203 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)name[i]); |
| 204 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)'\n' ); |
| 205 | strcpy(statname,name); |
| 206 | } |
| 207 | if (statsize != size) |
| 208 | { |
| 209 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM)size ); |
| 210 | statsize = size; |
| 211 | } |
| 212 | if (statelapsed != elapsed) |
| 213 | { |
| 214 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_ELAPSED, (WPARAM)elapsed ); |
| 215 | statelapsed = elapsed; |
| 216 | } |
| 217 | if (statperct != percentage) |
| 218 | { |
| 219 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_PERCENT, (WPARAM)percentage ); |
| 220 | statperct = percentage; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Print an error message and perform a fatal exit. |
| 226 | */ |
| 227 | void fatalbox(char *fmt, ...) |
| 228 | { |
| 229 | char str[0x100]; /* Make the size big enough */ |
| 230 | va_list ap; |
| 231 | va_start(ap, fmt); |
| 232 | strcpy(str, "Fatal:"); |
| 233 | vsprintf(str+strlen(str), fmt, ap); |
| 234 | va_end(ap); |
| 235 | strcat(str, "\n"); |
| 236 | tell_str(stderr, str); |
| 237 | |
| 238 | exit(1); |
| 239 | } |
| 240 | void connection_fatal(char *fmt, ...) |
| 241 | { |
| 242 | char str[0x100]; /* Make the size big enough */ |
| 243 | va_list ap; |
| 244 | va_start(ap, fmt); |
| 245 | strcpy(str, "Fatal:"); |
| 246 | vsprintf(str+strlen(str), fmt, ap); |
| 247 | va_end(ap); |
| 248 | strcat(str, "\n"); |
| 249 | tell_str(stderr, str); |
| 250 | |
| 251 | exit(1); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Be told what socket we're supposed to be using. |
| 256 | */ |
| 257 | static SOCKET scp_ssh_socket; |
| 258 | char *do_select(SOCKET skt, int startup) { |
| 259 | if (startup) |
| 260 | scp_ssh_socket = skt; |
| 261 | else |
| 262 | scp_ssh_socket = INVALID_SOCKET; |
| 263 | return NULL; |
| 264 | } |
| 265 | extern int select_result(WPARAM, LPARAM); |
| 266 | |
| 267 | /* |
| 268 | * Receive a block of data from the SSH link. Block until all data |
| 269 | * is available. |
| 270 | * |
| 271 | * To do this, we repeatedly call the SSH protocol module, with our |
| 272 | * own trap in from_backend() to catch the data that comes back. We |
| 273 | * do this until we have enough data. |
| 274 | */ |
| 275 | |
| 276 | static unsigned char *outptr; /* where to put the data */ |
| 277 | static unsigned outlen; /* how much data required */ |
| 278 | static unsigned char *pending = NULL; /* any spare data */ |
| 279 | static unsigned pendlen=0, pendsize=0; /* length and phys. size of buffer */ |
| 280 | void from_backend(int is_stderr, char *data, int datalen) { |
| 281 | unsigned char *p = (unsigned char *)data; |
| 282 | unsigned len = (unsigned)datalen; |
| 283 | |
| 284 | /* |
| 285 | * stderr data is just spouted to local stderr and otherwise |
| 286 | * ignored. |
| 287 | */ |
| 288 | if (is_stderr) { |
| 289 | fwrite(data, 1, len, stderr); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | inbuf_head = 0; |
| 294 | |
| 295 | /* |
| 296 | * If this is before the real session begins, just return. |
| 297 | */ |
| 298 | if (!outptr) |
| 299 | return; |
| 300 | |
| 301 | if (outlen > 0) { |
| 302 | unsigned used = outlen; |
| 303 | if (used > len) used = len; |
| 304 | memcpy(outptr, p, used); |
| 305 | outptr += used; outlen -= used; |
| 306 | p += used; len -= used; |
| 307 | } |
| 308 | |
| 309 | if (len > 0) { |
| 310 | if (pendsize < pendlen + len) { |
| 311 | pendsize = pendlen + len + 4096; |
| 312 | pending = (pending ? srealloc(pending, pendsize) : |
| 313 | smalloc(pendsize)); |
| 314 | if (!pending) |
| 315 | fatalbox("Out of memory"); |
| 316 | } |
| 317 | memcpy(pending+pendlen, p, len); |
| 318 | pendlen += len; |
| 319 | } |
| 320 | } |
| 321 | static int ssh_scp_recv(unsigned char *buf, int len) { |
| 322 | outptr = buf; |
| 323 | outlen = len; |
| 324 | |
| 325 | /* |
| 326 | * See if the pending-input block contains some of what we |
| 327 | * need. |
| 328 | */ |
| 329 | if (pendlen > 0) { |
| 330 | unsigned pendused = pendlen; |
| 331 | if (pendused > outlen) |
| 332 | pendused = outlen; |
| 333 | memcpy(outptr, pending, pendused); |
| 334 | memmove(pending, pending+pendused, pendlen-pendused); |
| 335 | outptr += pendused; |
| 336 | outlen -= pendused; |
| 337 | pendlen -= pendused; |
| 338 | if (pendlen == 0) { |
| 339 | pendsize = 0; |
| 340 | sfree(pending); |
| 341 | pending = NULL; |
| 342 | } |
| 343 | if (outlen == 0) |
| 344 | return len; |
| 345 | } |
| 346 | |
| 347 | while (outlen > 0) { |
| 348 | fd_set readfds; |
| 349 | |
| 350 | FD_ZERO(&readfds); |
| 351 | FD_SET(scp_ssh_socket, &readfds); |
| 352 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
| 353 | return 0; /* doom */ |
| 354 | select_result((WPARAM)scp_ssh_socket, (LPARAM)FD_READ); |
| 355 | } |
| 356 | |
| 357 | return len; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Loop through the ssh connection and authentication process. |
| 362 | */ |
| 363 | static void ssh_scp_init(void) { |
| 364 | if (scp_ssh_socket == INVALID_SOCKET) |
| 365 | return; |
| 366 | while (!back->sendok()) { |
| 367 | fd_set readfds; |
| 368 | FD_ZERO(&readfds); |
| 369 | FD_SET(scp_ssh_socket, &readfds); |
| 370 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
| 371 | return; /* doom */ |
| 372 | select_result((WPARAM)scp_ssh_socket, (LPARAM)FD_READ); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Print an error message and exit after closing the SSH link. |
| 378 | */ |
| 379 | static void bump(char *fmt, ...) |
| 380 | { |
| 381 | char str[0x100]; /* Make the size big enough */ |
| 382 | va_list ap; |
| 383 | va_start(ap, fmt); |
| 384 | strcpy(str, "Fatal:"); |
| 385 | vsprintf(str+strlen(str), fmt, ap); |
| 386 | va_end(ap); |
| 387 | strcat(str, "\n"); |
| 388 | tell_str(stderr, str); |
| 389 | |
| 390 | if (back != NULL && back->socket() != NULL) { |
| 391 | char ch; |
| 392 | back->special(TS_EOF); |
| 393 | ssh_scp_recv(&ch, 1); |
| 394 | } |
| 395 | exit(1); |
| 396 | } |
| 397 | |
| 398 | static int get_line(const char *prompt, char *str, int maxlen, int is_pw) |
| 399 | { |
| 400 | HANDLE hin, hout; |
| 401 | DWORD savemode, newmode, i; |
| 402 | |
| 403 | if (is_pw && password) { |
| 404 | static int tried_once = 0; |
| 405 | |
| 406 | if (tried_once) { |
| 407 | return 0; |
| 408 | } else { |
| 409 | strncpy(str, password, maxlen); |
| 410 | str[maxlen-1] = '\0'; |
| 411 | tried_once = 1; |
| 412 | return 1; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* GUI Adaptation - Sept 2000 */ |
| 417 | if (gui_mode) { |
| 418 | if (maxlen>0) str[0] = '\0'; |
| 419 | } else { |
| 420 | hin = GetStdHandle(STD_INPUT_HANDLE); |
| 421 | hout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 422 | if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) |
| 423 | bump("Cannot get standard input/output handles"); |
| 424 | |
| 425 | GetConsoleMode(hin, &savemode); |
| 426 | newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT; |
| 427 | if (is_pw) |
| 428 | newmode &= ~ENABLE_ECHO_INPUT; |
| 429 | else |
| 430 | newmode |= ENABLE_ECHO_INPUT; |
| 431 | SetConsoleMode(hin, newmode); |
| 432 | |
| 433 | WriteFile(hout, prompt, strlen(prompt), &i, NULL); |
| 434 | ReadFile(hin, str, maxlen-1, &i, NULL); |
| 435 | |
| 436 | SetConsoleMode(hin, savemode); |
| 437 | |
| 438 | if ((int)i > maxlen) i = maxlen-1; else i = i - 2; |
| 439 | str[i] = '\0'; |
| 440 | |
| 441 | if (is_pw) |
| 442 | WriteFile(hout, "\r\n", 2, &i, NULL); |
| 443 | } |
| 444 | |
| 445 | return 1; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Open an SSH connection to user@host and execute cmd. |
| 450 | */ |
| 451 | static void do_cmd(char *host, char *user, char *cmd) |
| 452 | { |
| 453 | char *err, *realhost; |
| 454 | DWORD namelen; |
| 455 | |
| 456 | if (host == NULL || host[0] == '\0') |
| 457 | bump("Empty host name"); |
| 458 | |
| 459 | /* Try to load settings for this host */ |
| 460 | do_defaults(host, &cfg); |
| 461 | if (cfg.host[0] == '\0') { |
| 462 | /* No settings for this host; use defaults */ |
| 463 | do_defaults(NULL, &cfg); |
| 464 | strncpy(cfg.host, host, sizeof(cfg.host)-1); |
| 465 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
| 466 | cfg.port = 22; |
| 467 | } |
| 468 | |
| 469 | /* Set username */ |
| 470 | if (user != NULL && user[0] != '\0') { |
| 471 | strncpy(cfg.username, user, sizeof(cfg.username)-1); |
| 472 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
| 473 | } else if (cfg.username[0] == '\0') { |
| 474 | namelen = 0; |
| 475 | if (GetUserName(user, &namelen) == FALSE) |
| 476 | bump("Empty user name"); |
| 477 | user = smalloc(namelen * sizeof(char)); |
| 478 | GetUserName(user, &namelen); |
| 479 | if (verbose) tell_user(stderr, "Guessing user name: %s", user); |
| 480 | strncpy(cfg.username, user, sizeof(cfg.username)-1); |
| 481 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
| 482 | free(user); |
| 483 | } |
| 484 | |
| 485 | if (cfg.protocol != PROT_SSH) |
| 486 | cfg.port = 22; |
| 487 | |
| 488 | if (portnumber) |
| 489 | cfg.port = portnumber; |
| 490 | |
| 491 | strncpy(cfg.remote_cmd, cmd, sizeof(cfg.remote_cmd)); |
| 492 | cfg.remote_cmd[sizeof(cfg.remote_cmd)-1] = '\0'; |
| 493 | cfg.nopty = TRUE; |
| 494 | |
| 495 | back = &ssh_backend; |
| 496 | |
| 497 | err = back->init(cfg.host, cfg.port, &realhost); |
| 498 | if (err != NULL) |
| 499 | bump("ssh_init: %s", err); |
| 500 | ssh_scp_init(); |
| 501 | if (verbose && realhost != NULL) |
| 502 | tell_user(stderr, "Connected to %s\n", realhost); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Update statistic information about current file. |
| 507 | */ |
| 508 | static void print_stats(char *name, unsigned long size, unsigned long done, |
| 509 | time_t start, time_t now) |
| 510 | { |
| 511 | float ratebs; |
| 512 | unsigned long eta; |
| 513 | char etastr[10]; |
| 514 | int pct; |
| 515 | |
| 516 | /* GUI Adaptation - Sept 2000 */ |
| 517 | if (gui_mode) |
| 518 | gui_update_stats(name, size, (int)(100 * (done*1.0/size)), |
| 519 | (unsigned long)difftime(now, start)); |
| 520 | else { |
| 521 | if (now > start) |
| 522 | ratebs = (float) done / (now - start); |
| 523 | else |
| 524 | ratebs = (float) done; |
| 525 | |
| 526 | if (ratebs < 1.0) |
| 527 | eta = size - done; |
| 528 | else |
| 529 | eta = (unsigned long) ((size - done) / ratebs); |
| 530 | sprintf(etastr, "%02ld:%02ld:%02ld", |
| 531 | eta / 3600, (eta % 3600) / 60, eta % 60); |
| 532 | |
| 533 | pct = (int) (100.0 * (float) done / size); |
| 534 | |
| 535 | printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%", |
| 536 | name, done / 1024, ratebs / 1024.0, |
| 537 | etastr, pct); |
| 538 | |
| 539 | if (done == size) |
| 540 | printf("\n"); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Find a colon in str and return a pointer to the colon. |
| 546 | * This is used to separate hostname from filename. |
| 547 | */ |
| 548 | static char * colon(char *str) |
| 549 | { |
| 550 | /* We ignore a leading colon, since the hostname cannot be |
| 551 | empty. We also ignore a colon as second character because |
| 552 | of filenames like f:myfile.txt. */ |
| 553 | if (str[0] == '\0' || |
| 554 | str[0] == ':' || |
| 555 | str[1] == ':') |
| 556 | return (NULL); |
| 557 | while (*str != '\0' && |
| 558 | *str != ':' && |
| 559 | *str != '/' && |
| 560 | *str != '\\') |
| 561 | str++; |
| 562 | if (*str == ':') |
| 563 | return (str); |
| 564 | else |
| 565 | return (NULL); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Wait for a response from the other side. |
| 570 | * Return 0 if ok, -1 if error. |
| 571 | */ |
| 572 | static int response(void) |
| 573 | { |
| 574 | char ch, resp, rbuf[2048]; |
| 575 | int p; |
| 576 | |
| 577 | if (ssh_scp_recv(&resp, 1) <= 0) |
| 578 | bump("Lost connection"); |
| 579 | |
| 580 | p = 0; |
| 581 | switch (resp) { |
| 582 | case 0: /* ok */ |
| 583 | return (0); |
| 584 | default: |
| 585 | rbuf[p++] = resp; |
| 586 | /* fallthrough */ |
| 587 | case 1: /* error */ |
| 588 | case 2: /* fatal error */ |
| 589 | do { |
| 590 | if (ssh_scp_recv(&ch, 1) <= 0) |
| 591 | bump("Protocol error: Lost connection"); |
| 592 | rbuf[p++] = ch; |
| 593 | } while (p < sizeof(rbuf) && ch != '\n'); |
| 594 | rbuf[p-1] = '\0'; |
| 595 | if (resp == 1) |
| 596 | tell_user(stderr, "%s\n", rbuf); |
| 597 | else |
| 598 | bump("%s", rbuf); |
| 599 | errs++; |
| 600 | return (-1); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Send an error message to the other side and to the screen. |
| 606 | * Increment error counter. |
| 607 | */ |
| 608 | static void run_err(const char *fmt, ...) |
| 609 | { |
| 610 | char str[2048]; |
| 611 | va_list ap; |
| 612 | va_start(ap, fmt); |
| 613 | errs++; |
| 614 | strcpy(str, "scp: "); |
| 615 | vsprintf(str+strlen(str), fmt, ap); |
| 616 | strcat(str, "\n"); |
| 617 | back->send(str, strlen(str)); |
| 618 | tell_user(stderr, "%s",str); |
| 619 | va_end(ap); |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Execute the source part of the SCP protocol. |
| 624 | */ |
| 625 | static void source(char *src) |
| 626 | { |
| 627 | char buf[2048]; |
| 628 | unsigned long size; |
| 629 | char *last; |
| 630 | HANDLE f; |
| 631 | DWORD attr; |
| 632 | unsigned long i; |
| 633 | unsigned long stat_bytes; |
| 634 | time_t stat_starttime, stat_lasttime; |
| 635 | |
| 636 | attr = GetFileAttributes(src); |
| 637 | if (attr == (DWORD)-1) { |
| 638 | run_err("%s: No such file or directory", src); |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 643 | if (recursive) { |
| 644 | /* |
| 645 | * Avoid . and .. directories. |
| 646 | */ |
| 647 | char *p; |
| 648 | p = strrchr(src, '/'); |
| 649 | if (!p) |
| 650 | p = strrchr(src, '\\'); |
| 651 | if (!p) |
| 652 | p = src; |
| 653 | else |
| 654 | p++; |
| 655 | if (!strcmp(p, ".") || !strcmp(p, "..")) |
| 656 | /* skip . and .. */; |
| 657 | else |
| 658 | rsource(src); |
| 659 | } else { |
| 660 | run_err("%s: not a regular file", src); |
| 661 | } |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | if ((last = strrchr(src, '/')) == NULL) |
| 666 | last = src; |
| 667 | else |
| 668 | last++; |
| 669 | if (strrchr(last, '\\') != NULL) |
| 670 | last = strrchr(last, '\\') + 1; |
| 671 | if (last == src && strchr(src, ':') != NULL) |
| 672 | last = strchr(src, ':') + 1; |
| 673 | |
| 674 | f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 675 | OPEN_EXISTING, 0, 0); |
| 676 | if (f == INVALID_HANDLE_VALUE) { |
| 677 | run_err("%s: Cannot open file", src); |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | if (preserve) { |
| 682 | FILETIME actime, wrtime; |
| 683 | unsigned long mtime, atime; |
| 684 | GetFileTime(f, NULL, &actime, &wrtime); |
| 685 | TIME_WIN_TO_POSIX(actime, atime); |
| 686 | TIME_WIN_TO_POSIX(wrtime, mtime); |
| 687 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
| 688 | back->send(buf, strlen(buf)); |
| 689 | if (response()) |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | size = GetFileSize(f, NULL); |
| 694 | sprintf(buf, "C0644 %lu %s\n", size, last); |
| 695 | if (verbose) |
| 696 | tell_user(stderr, "Sending file modes: %s", buf); |
| 697 | back->send(buf, strlen(buf)); |
| 698 | if (response()) |
| 699 | return; |
| 700 | |
| 701 | if (statistics) { |
| 702 | stat_bytes = 0; |
| 703 | stat_starttime = time(NULL); |
| 704 | stat_lasttime = 0; |
| 705 | } |
| 706 | |
| 707 | for (i = 0; i < size; i += 4096) { |
| 708 | char transbuf[4096]; |
| 709 | DWORD j, k = 4096; |
| 710 | if (i + k > size) k = size - i; |
| 711 | if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) { |
| 712 | if (statistics) printf("\n"); |
| 713 | bump("%s: Read error", src); |
| 714 | } |
| 715 | back->send(transbuf, k); |
| 716 | if (statistics) { |
| 717 | stat_bytes += k; |
| 718 | if (time(NULL) != stat_lasttime || |
| 719 | i + k == size) { |
| 720 | stat_lasttime = time(NULL); |
| 721 | print_stats(last, size, stat_bytes, |
| 722 | stat_starttime, stat_lasttime); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | CloseHandle(f); |
| 727 | |
| 728 | back->send("", 1); |
| 729 | (void) response(); |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Recursively send the contents of a directory. |
| 734 | */ |
| 735 | static void rsource(char *src) |
| 736 | { |
| 737 | char buf[2048]; |
| 738 | char *last; |
| 739 | HANDLE dir; |
| 740 | WIN32_FIND_DATA fdat; |
| 741 | int ok; |
| 742 | |
| 743 | if ((last = strrchr(src, '/')) == NULL) |
| 744 | last = src; |
| 745 | else |
| 746 | last++; |
| 747 | if (strrchr(last, '\\') != NULL) |
| 748 | last = strrchr(last, '\\') + 1; |
| 749 | if (last == src && strchr(src, ':') != NULL) |
| 750 | last = strchr(src, ':') + 1; |
| 751 | |
| 752 | /* maybe send filetime */ |
| 753 | |
| 754 | sprintf(buf, "D0755 0 %s\n", last); |
| 755 | if (verbose) |
| 756 | tell_user(stderr, "Entering directory: %s", buf); |
| 757 | back->send(buf, strlen(buf)); |
| 758 | if (response()) |
| 759 | return; |
| 760 | |
| 761 | sprintf(buf, "%s/*", src); |
| 762 | dir = FindFirstFile(buf, &fdat); |
| 763 | ok = (dir != INVALID_HANDLE_VALUE); |
| 764 | while (ok) { |
| 765 | if (strcmp(fdat.cFileName, ".") == 0 || |
| 766 | strcmp(fdat.cFileName, "..") == 0) { |
| 767 | } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= |
| 768 | sizeof(buf)) { |
| 769 | run_err("%s/%s: Name too long", src, fdat.cFileName); |
| 770 | } else { |
| 771 | sprintf(buf, "%s/%s", src, fdat.cFileName); |
| 772 | source(buf); |
| 773 | } |
| 774 | ok = FindNextFile(dir, &fdat); |
| 775 | } |
| 776 | FindClose(dir); |
| 777 | |
| 778 | sprintf(buf, "E\n"); |
| 779 | back->send(buf, strlen(buf)); |
| 780 | (void) response(); |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Execute the sink part of the SCP protocol. |
| 785 | */ |
| 786 | static void sink(char *targ, char *src) |
| 787 | { |
| 788 | char buf[2048]; |
| 789 | char namebuf[2048]; |
| 790 | char ch; |
| 791 | int targisdir = 0; |
| 792 | int settime; |
| 793 | int exists; |
| 794 | DWORD attr; |
| 795 | HANDLE f; |
| 796 | unsigned long mtime, atime; |
| 797 | unsigned int mode; |
| 798 | unsigned long size, i; |
| 799 | int wrerror = 0; |
| 800 | unsigned long stat_bytes; |
| 801 | time_t stat_starttime, stat_lasttime; |
| 802 | char *stat_name; |
| 803 | |
| 804 | attr = GetFileAttributes(targ); |
| 805 | if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
| 806 | targisdir = 1; |
| 807 | |
| 808 | if (targetshouldbedirectory && !targisdir) |
| 809 | bump("%s: Not a directory", targ); |
| 810 | |
| 811 | back->send("", 1); |
| 812 | while (1) { |
| 813 | settime = 0; |
| 814 | gottime: |
| 815 | if (ssh_scp_recv(&ch, 1) <= 0) |
| 816 | return; |
| 817 | if (ch == '\n') |
| 818 | bump("Protocol error: Unexpected newline"); |
| 819 | i = 0; |
| 820 | buf[i++] = ch; |
| 821 | do { |
| 822 | if (ssh_scp_recv(&ch, 1) <= 0) |
| 823 | bump("Lost connection"); |
| 824 | buf[i++] = ch; |
| 825 | } while (i < sizeof(buf) && ch != '\n'); |
| 826 | buf[i-1] = '\0'; |
| 827 | switch (buf[0]) { |
| 828 | case '\01': /* error */ |
| 829 | tell_user(stderr, "%s\n", buf+1); |
| 830 | errs++; |
| 831 | continue; |
| 832 | case '\02': /* fatal error */ |
| 833 | bump("%s", buf+1); |
| 834 | case 'E': |
| 835 | back->send("", 1); |
| 836 | return; |
| 837 | case 'T': |
| 838 | if (sscanf(buf, "T%ld %*d %ld %*d", |
| 839 | &mtime, &atime) == 2) { |
| 840 | settime = 1; |
| 841 | back->send("", 1); |
| 842 | goto gottime; |
| 843 | } |
| 844 | bump("Protocol error: Illegal time format"); |
| 845 | case 'C': |
| 846 | case 'D': |
| 847 | break; |
| 848 | default: |
| 849 | bump("Protocol error: Expected control record"); |
| 850 | } |
| 851 | |
| 852 | if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3) |
| 853 | bump("Protocol error: Illegal file descriptor format"); |
| 854 | /* Security fix: ensure the file ends up where we asked for it. */ |
| 855 | if (targisdir) { |
| 856 | char t[2048]; |
| 857 | char *p; |
| 858 | strcpy(t, targ); |
| 859 | if (targ[0] != '\0') |
| 860 | strcat(t, "/"); |
| 861 | p = namebuf + strlen(namebuf); |
| 862 | while (p > namebuf && p[-1] != '/' && p[-1] != '\\') |
| 863 | p--; |
| 864 | strcat(t, p); |
| 865 | strcpy(namebuf, t); |
| 866 | } else { |
| 867 | strcpy(namebuf, targ); |
| 868 | } |
| 869 | attr = GetFileAttributes(namebuf); |
| 870 | exists = (attr != (DWORD)-1); |
| 871 | |
| 872 | if (buf[0] == 'D') { |
| 873 | if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 874 | run_err("%s: Not a directory", namebuf); |
| 875 | continue; |
| 876 | } |
| 877 | if (!exists) { |
| 878 | if (! CreateDirectory(namebuf, NULL)) { |
| 879 | run_err("%s: Cannot create directory", |
| 880 | namebuf); |
| 881 | continue; |
| 882 | } |
| 883 | } |
| 884 | sink(namebuf, NULL); |
| 885 | /* can we set the timestamp for directories ? */ |
| 886 | continue; |
| 887 | } |
| 888 | |
| 889 | f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL, |
| 890 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
| 891 | if (f == INVALID_HANDLE_VALUE) { |
| 892 | run_err("%s: Cannot create file", namebuf); |
| 893 | continue; |
| 894 | } |
| 895 | |
| 896 | back->send("", 1); |
| 897 | |
| 898 | if (statistics) { |
| 899 | stat_bytes = 0; |
| 900 | stat_starttime = time(NULL); |
| 901 | stat_lasttime = 0; |
| 902 | if ((stat_name = strrchr(namebuf, '/')) == NULL) |
| 903 | stat_name = namebuf; |
| 904 | else |
| 905 | stat_name++; |
| 906 | if (strrchr(stat_name, '\\') != NULL) |
| 907 | stat_name = strrchr(stat_name, '\\') + 1; |
| 908 | } |
| 909 | |
| 910 | for (i = 0; i < size; i += 4096) { |
| 911 | char transbuf[4096]; |
| 912 | DWORD j, k = 4096; |
| 913 | if (i + k > size) k = size - i; |
| 914 | if (ssh_scp_recv(transbuf, k) == 0) |
| 915 | bump("Lost connection"); |
| 916 | if (wrerror) continue; |
| 917 | if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) { |
| 918 | wrerror = 1; |
| 919 | if (statistics) |
| 920 | printf("\r%-25.25s | %50s\n", |
| 921 | stat_name, |
| 922 | "Write error.. waiting for end of file"); |
| 923 | continue; |
| 924 | } |
| 925 | if (statistics) { |
| 926 | stat_bytes += k; |
| 927 | if (time(NULL) > stat_lasttime || |
| 928 | i + k == size) { |
| 929 | stat_lasttime = time(NULL); |
| 930 | print_stats(stat_name, size, stat_bytes, |
| 931 | stat_starttime, stat_lasttime); |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | (void) response(); |
| 936 | |
| 937 | if (settime) { |
| 938 | FILETIME actime, wrtime; |
| 939 | TIME_POSIX_TO_WIN(atime, actime); |
| 940 | TIME_POSIX_TO_WIN(mtime, wrtime); |
| 941 | SetFileTime(f, NULL, &actime, &wrtime); |
| 942 | } |
| 943 | |
| 944 | CloseHandle(f); |
| 945 | if (wrerror) { |
| 946 | run_err("%s: Write error", namebuf); |
| 947 | continue; |
| 948 | } |
| 949 | back->send("", 1); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * We will copy local files to a remote server. |
| 955 | */ |
| 956 | static void toremote(int argc, char *argv[]) |
| 957 | { |
| 958 | char *src, *targ, *host, *user; |
| 959 | char *cmd; |
| 960 | int i; |
| 961 | |
| 962 | targ = argv[argc-1]; |
| 963 | |
| 964 | /* Separate host from filename */ |
| 965 | host = targ; |
| 966 | targ = colon(targ); |
| 967 | if (targ == NULL) |
| 968 | bump("targ == NULL in toremote()"); |
| 969 | *targ++ = '\0'; |
| 970 | if (*targ == '\0') |
| 971 | targ = "."; |
| 972 | /* Substitute "." for emtpy target */ |
| 973 | |
| 974 | /* Separate host and username */ |
| 975 | user = host; |
| 976 | host = strrchr(host, '@'); |
| 977 | if (host == NULL) { |
| 978 | host = user; |
| 979 | user = NULL; |
| 980 | } else { |
| 981 | *host++ = '\0'; |
| 982 | if (*user == '\0') |
| 983 | user = NULL; |
| 984 | } |
| 985 | |
| 986 | if (argc == 2) { |
| 987 | /* Find out if the source filespec covers multiple files |
| 988 | if so, we should set the targetshouldbedirectory flag */ |
| 989 | HANDLE fh; |
| 990 | WIN32_FIND_DATA fdat; |
| 991 | if (colon(argv[0]) != NULL) |
| 992 | bump("%s: Remote to remote not supported", argv[0]); |
| 993 | fh = FindFirstFile(argv[0], &fdat); |
| 994 | if (fh == INVALID_HANDLE_VALUE) |
| 995 | bump("%s: No such file or directory\n", argv[0]); |
| 996 | if (FindNextFile(fh, &fdat)) |
| 997 | targetshouldbedirectory = 1; |
| 998 | FindClose(fh); |
| 999 | } |
| 1000 | |
| 1001 | cmd = smalloc(strlen(targ) + 100); |
| 1002 | sprintf(cmd, "scp%s%s%s%s -t %s", |
| 1003 | verbose ? " -v" : "", |
| 1004 | recursive ? " -r" : "", |
| 1005 | preserve ? " -p" : "", |
| 1006 | targetshouldbedirectory ? " -d" : "", |
| 1007 | targ); |
| 1008 | do_cmd(host, user, cmd); |
| 1009 | sfree(cmd); |
| 1010 | |
| 1011 | (void) response(); |
| 1012 | |
| 1013 | for (i = 0; i < argc - 1; i++) { |
| 1014 | HANDLE dir; |
| 1015 | WIN32_FIND_DATA fdat; |
| 1016 | src = argv[i]; |
| 1017 | if (colon(src) != NULL) { |
| 1018 | tell_user(stderr, "%s: Remote to remote not supported\n", src); |
| 1019 | errs++; |
| 1020 | continue; |
| 1021 | } |
| 1022 | dir = FindFirstFile(src, &fdat); |
| 1023 | if (dir == INVALID_HANDLE_VALUE) { |
| 1024 | run_err("%s: No such file or directory", src); |
| 1025 | continue; |
| 1026 | } |
| 1027 | do { |
| 1028 | char *last; |
| 1029 | char namebuf[2048]; |
| 1030 | /* |
| 1031 | * Ensure that . and .. are never matched by wildcards, |
| 1032 | * but only by deliberate action. |
| 1033 | */ |
| 1034 | if (!strcmp(fdat.cFileName, ".") || |
| 1035 | !strcmp(fdat.cFileName, "..")) { |
| 1036 | /* |
| 1037 | * Find*File has returned a special dir. We require |
| 1038 | * that _either_ `src' ends in a backslash followed |
| 1039 | * by that string, _or_ `src' is precisely that |
| 1040 | * string. |
| 1041 | */ |
| 1042 | int len = strlen(src), dlen = strlen(fdat.cFileName); |
| 1043 | if (len == dlen && !strcmp(src, fdat.cFileName)) { |
| 1044 | /* ok */; |
| 1045 | } else if (len > dlen+1 && src[len-dlen-1] == '\\' && |
| 1046 | !strcmp(src+len-dlen, fdat.cFileName)) { |
| 1047 | /* ok */; |
| 1048 | } else |
| 1049 | continue; /* ignore this one */ |
| 1050 | } |
| 1051 | if (strlen(src) + strlen(fdat.cFileName) >= |
| 1052 | sizeof(namebuf)) { |
| 1053 | tell_user(stderr, "%s: Name too long", src); |
| 1054 | continue; |
| 1055 | } |
| 1056 | strcpy(namebuf, src); |
| 1057 | if ((last = strrchr(namebuf, '/')) == NULL) |
| 1058 | last = namebuf; |
| 1059 | else |
| 1060 | last++; |
| 1061 | if (strrchr(last, '\\') != NULL) |
| 1062 | last = strrchr(last, '\\') + 1; |
| 1063 | if (last == namebuf && strrchr(namebuf, ':') != NULL) |
| 1064 | last = strchr(namebuf, ':') + 1; |
| 1065 | strcpy(last, fdat.cFileName); |
| 1066 | source(namebuf); |
| 1067 | } while (FindNextFile(dir, &fdat)); |
| 1068 | FindClose(dir); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * We will copy files from a remote server to the local machine. |
| 1074 | */ |
| 1075 | static void tolocal(int argc, char *argv[]) |
| 1076 | { |
| 1077 | char *src, *targ, *host, *user; |
| 1078 | char *cmd; |
| 1079 | |
| 1080 | if (argc != 2) |
| 1081 | bump("More than one remote source not supported"); |
| 1082 | |
| 1083 | src = argv[0]; |
| 1084 | targ = argv[1]; |
| 1085 | |
| 1086 | /* Separate host from filename */ |
| 1087 | host = src; |
| 1088 | src = colon(src); |
| 1089 | if (src == NULL) |
| 1090 | bump("Local to local copy not supported"); |
| 1091 | *src++ = '\0'; |
| 1092 | if (*src == '\0') |
| 1093 | src = "."; |
| 1094 | /* Substitute "." for empty filename */ |
| 1095 | |
| 1096 | /* Separate username and hostname */ |
| 1097 | user = host; |
| 1098 | host = strrchr(host, '@'); |
| 1099 | if (host == NULL) { |
| 1100 | host = user; |
| 1101 | user = NULL; |
| 1102 | } else { |
| 1103 | *host++ = '\0'; |
| 1104 | if (*user == '\0') |
| 1105 | user = NULL; |
| 1106 | } |
| 1107 | |
| 1108 | cmd = smalloc(strlen(src) + 100); |
| 1109 | sprintf(cmd, "scp%s%s%s%s -f %s", |
| 1110 | verbose ? " -v" : "", |
| 1111 | recursive ? " -r" : "", |
| 1112 | preserve ? " -p" : "", |
| 1113 | targetshouldbedirectory ? " -d" : "", |
| 1114 | src); |
| 1115 | do_cmd(host, user, cmd); |
| 1116 | sfree(cmd); |
| 1117 | |
| 1118 | sink(targ, src); |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * We will issue a list command to get a remote directory. |
| 1123 | */ |
| 1124 | static void get_dir_list(int argc, char *argv[]) |
| 1125 | { |
| 1126 | char *src, *host, *user; |
| 1127 | char *cmd, *p, *q; |
| 1128 | char c; |
| 1129 | |
| 1130 | src = argv[0]; |
| 1131 | |
| 1132 | /* Separate host from filename */ |
| 1133 | host = src; |
| 1134 | src = colon(src); |
| 1135 | if (src == NULL) |
| 1136 | bump("Local to local copy not supported"); |
| 1137 | *src++ = '\0'; |
| 1138 | if (*src == '\0') |
| 1139 | src = "."; |
| 1140 | /* Substitute "." for empty filename */ |
| 1141 | |
| 1142 | /* Separate username and hostname */ |
| 1143 | user = host; |
| 1144 | host = strrchr(host, '@'); |
| 1145 | if (host == NULL) { |
| 1146 | host = user; |
| 1147 | user = NULL; |
| 1148 | } else { |
| 1149 | *host++ = '\0'; |
| 1150 | if (*user == '\0') |
| 1151 | user = NULL; |
| 1152 | } |
| 1153 | |
| 1154 | cmd = smalloc(4*strlen(src) + 100); |
| 1155 | strcpy(cmd, "ls -la '"); |
| 1156 | p = cmd + strlen(cmd); |
| 1157 | for (q = src; *q; q++) { |
| 1158 | if (*q == '\'') { |
| 1159 | *p++ = '\''; *p++ = '\\'; *p++ = '\''; *p++ = '\''; |
| 1160 | } else { |
| 1161 | *p++ = *q; |
| 1162 | } |
| 1163 | } |
| 1164 | *p++ = '\''; |
| 1165 | *p = '\0'; |
| 1166 | |
| 1167 | do_cmd(host, user, cmd); |
| 1168 | sfree(cmd); |
| 1169 | |
| 1170 | while (ssh_scp_recv(&c, 1) > 0) |
| 1171 | tell_char(stdout, c); |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Initialize the Win$ock driver. |
| 1176 | */ |
| 1177 | static void init_winsock(void) |
| 1178 | { |
| 1179 | WORD winsock_ver; |
| 1180 | WSADATA wsadata; |
| 1181 | |
| 1182 | winsock_ver = MAKEWORD(1, 1); |
| 1183 | if (WSAStartup(winsock_ver, &wsadata)) |
| 1184 | bump("Unable to initialise WinSock"); |
| 1185 | if (LOBYTE(wsadata.wVersion) != 1 || |
| 1186 | HIBYTE(wsadata.wVersion) != 1) |
| 1187 | bump("WinSock version is incompatible with 1.1"); |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * Short description of parameters. |
| 1192 | */ |
| 1193 | static void usage(void) |
| 1194 | { |
| 1195 | printf("PuTTY Secure Copy client\n"); |
| 1196 | printf("%s\n", ver); |
| 1197 | printf("Usage: pscp [options] [user@]host:source target\n"); |
| 1198 | printf(" pscp [options] source [source...] [user@]host:target\n"); |
| 1199 | printf(" pscp [options] -ls user@host:filespec\n"); |
| 1200 | printf("Options:\n"); |
| 1201 | printf(" -p preserve file attributes\n"); |
| 1202 | printf(" -q quiet, don't show statistics\n"); |
| 1203 | printf(" -r copy directories recursively\n"); |
| 1204 | printf(" -v show verbose messages\n"); |
| 1205 | printf(" -P port connect to specified port\n"); |
| 1206 | printf(" -pw passw login with specified password\n"); |
| 1207 | #if 0 |
| 1208 | /* |
| 1209 | * -gui is an internal option, used by GUI front ends to get |
| 1210 | * pscp to pass progress reports back to them. It's not an |
| 1211 | * ordinary user-accessible option, so it shouldn't be part of |
| 1212 | * the command-line help. The only people who need to know |
| 1213 | * about it are programmers, and they can read the source. |
| 1214 | */ |
| 1215 | printf(" -gui hWnd GUI mode with the windows handle for receiving messages\n"); |
| 1216 | #endif |
| 1217 | exit(1); |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * Main program (no, really?) |
| 1222 | */ |
| 1223 | int main(int argc, char *argv[]) |
| 1224 | { |
| 1225 | int i; |
| 1226 | int list = 0; |
| 1227 | |
| 1228 | default_protocol = PROT_TELNET; |
| 1229 | |
| 1230 | flags = FLAG_STDERR; |
| 1231 | ssh_get_line = &get_line; |
| 1232 | init_winsock(); |
| 1233 | sk_init(); |
| 1234 | |
| 1235 | for (i = 1; i < argc; i++) { |
| 1236 | if (argv[i][0] != '-') |
| 1237 | break; |
| 1238 | if (strcmp(argv[i], "-v") == 0) |
| 1239 | verbose = 1, flags |= FLAG_VERBOSE; |
| 1240 | else if (strcmp(argv[i], "-r") == 0) |
| 1241 | recursive = 1; |
| 1242 | else if (strcmp(argv[i], "-p") == 0) |
| 1243 | preserve = 1; |
| 1244 | else if (strcmp(argv[i], "-q") == 0) |
| 1245 | statistics = 0; |
| 1246 | else if (strcmp(argv[i], "-h") == 0 || |
| 1247 | strcmp(argv[i], "-?") == 0) |
| 1248 | usage(); |
| 1249 | else if (strcmp(argv[i], "-P") == 0 && i+1 < argc) |
| 1250 | portnumber = atoi(argv[++i]); |
| 1251 | else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc) |
| 1252 | password = argv[++i]; |
| 1253 | else if (strcmp(argv[i], "-gui") == 0 && i+1 < argc) { |
| 1254 | gui_hwnd = argv[++i]; |
| 1255 | gui_mode = 1; |
| 1256 | } else if (strcmp(argv[i], "-ls") == 0) |
| 1257 | list = 1; |
| 1258 | else if (strcmp(argv[i], "--") == 0) |
| 1259 | { i++; break; } |
| 1260 | else |
| 1261 | usage(); |
| 1262 | } |
| 1263 | argc -= i; |
| 1264 | argv += i; |
| 1265 | back = NULL; |
| 1266 | |
| 1267 | if (list) { |
| 1268 | if (argc != 1) |
| 1269 | usage(); |
| 1270 | get_dir_list(argc, argv); |
| 1271 | |
| 1272 | } else { |
| 1273 | |
| 1274 | if (argc < 2) |
| 1275 | usage(); |
| 1276 | if (argc > 2) |
| 1277 | targetshouldbedirectory = 1; |
| 1278 | |
| 1279 | if (colon(argv[argc-1]) != NULL) |
| 1280 | toremote(argc, argv); |
| 1281 | else |
| 1282 | tolocal(argc, argv); |
| 1283 | } |
| 1284 | |
| 1285 | if (back != NULL && back->socket() != NULL) { |
| 1286 | char ch; |
| 1287 | back->special(TS_EOF); |
| 1288 | ssh_scp_recv(&ch, 1); |
| 1289 | } |
| 1290 | WSACleanup(); |
| 1291 | random_save_seed(); |
| 1292 | |
| 1293 | /* GUI Adaptation - August 2000 */ |
| 1294 | if (gui_mode) { |
| 1295 | unsigned int msg_id = WM_RET_ERR_CNT; |
| 1296 | if (list) msg_id = WM_LS_RET_ERR_CNT; |
| 1297 | while (!PostMessage( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)errs, 0/*lParam*/ ) ) |
| 1298 | SleepEx(1000,TRUE); |
| 1299 | } |
| 1300 | return (errs == 0 ? 0 : 1); |
| 1301 | } |
| 1302 | |
| 1303 | /* end */ |