2 * scp.c - Scp (Secure Copy) client for PuTTY.
3 * Joris van Rantwijk, Simon Tatham
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.
8 * Adaptations to enable connecting a GUI by L. Gunnarsson - Sept 2000
24 /* GUI Adaptation - Sept 2000 */
28 #define PUTTY_DO_GLOBALS
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))
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 )
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;
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;
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);
78 void logevent(char *string)
82 void ldisc_send(char *buf, int len)
85 * This is only here because of the calls to ldisc_send(NULL,
86 * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
87 * as an ldisc. So if we get called with any real data, I want
93 void verify_ssh_host_key(char *host, int port, char *keytype,
94 char *keystr, char *fingerprint)
98 static const char absentmsg[] =
99 "The server's host key is not cached in the registry. You\n"
100 "have no guarantee that the server is the computer you\n"
102 "The server's key fingerprint is:\n"
104 "If you trust this host, enter \"y\" to add the key to\n"
105 "PuTTY's cache and carry on connecting.\n"
106 "If you do not trust this host, enter \"n\" to abandon the\n"
107 "connection.\n" "Continue connecting? (y/n) ";
109 static const char wrongmsg[] =
110 "WARNING - POTENTIAL SECURITY BREACH!\n"
111 "The server's host key does not match the one PuTTY has\n"
112 "cached in the registry. This means that either the\n"
113 "server administrator has changed the host key, or you\n"
114 "have actually connected to another computer pretending\n"
115 "to be the server.\n"
116 "The new key fingerprint is:\n"
118 "If you were expecting this change and trust the new key,\n"
119 "enter Yes to update PuTTY's cache and continue connecting.\n"
120 "If you want to carry on connecting but without updating\n"
121 "the cache, enter No.\n"
122 "If you want to abandon the connection completely, press\n"
123 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
125 "Update cached key? (y/n, Return cancels connection) ";
127 static const char abandoned[] = "Connection abandoned.\n";
132 * Verify the key against the registry.
134 ret = verify_host_key(host, port, keytype, keystr);
136 if (ret == 0) /* success - key matched OK */
138 if (ret == 2) { /* key was different */
139 fprintf(stderr, wrongmsg, fingerprint);
141 if (fgets(line, sizeof(line), stdin) &&
142 line[0] != '\0' && line[0] != '\n') {
143 if (line[0] == 'y' || line[0] == 'Y')
144 store_host_key(host, port, keytype, keystr);
146 fprintf(stderr, abandoned);
151 if (ret == 1) { /* key was absent */
152 fprintf(stderr, absentmsg, fingerprint);
153 if (fgets(line, sizeof(line), stdin) &&
154 (line[0] == 'y' || line[0] == 'Y'))
155 store_host_key(host, port, keytype, keystr);
157 fprintf(stderr, abandoned);
163 /* GUI Adaptation - Sept 2000 */
164 static void send_msg(HWND h, UINT message, WPARAM wParam)
166 while (!PostMessage(h, message, wParam, 0))
170 static void tell_char(FILE * stream, char c)
175 unsigned int msg_id = WM_STD_OUT_CHAR;
176 if (stream == stderr)
177 msg_id = WM_STD_ERR_CHAR;
178 send_msg((HWND) atoi(gui_hwnd), msg_id, (WPARAM) c);
182 static void tell_str(FILE * stream, char *str)
186 for (i = 0; i < strlen(str); ++i)
187 tell_char(stream, str[i]);
190 static void tell_user(FILE * stream, char *fmt, ...)
192 char str[0x100]; /* Make the size big enough */
195 vsprintf(str, fmt, ap);
198 tell_str(stream, str);
201 static void gui_update_stats(char *name, unsigned long size,
202 int percentage, unsigned long elapsed)
206 if (strcmp(name, statname) != 0) {
207 for (i = 0; i < strlen(name); ++i)
208 send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR,
210 send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM) '\n');
211 strcpy(statname, name);
213 if (statsize != size) {
214 send_msg((HWND) atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM) size);
217 if (statelapsed != elapsed) {
218 send_msg((HWND) atoi(gui_hwnd), WM_STATS_ELAPSED,
220 statelapsed = elapsed;
222 if (statperct != percentage) {
223 send_msg((HWND) atoi(gui_hwnd), WM_STATS_PERCENT,
224 (WPARAM) percentage);
225 statperct = percentage;
230 * Print an error message and perform a fatal exit.
232 void fatalbox(char *fmt, ...)
234 char str[0x100]; /* Make the size big enough */
237 strcpy(str, "Fatal:");
238 vsprintf(str + strlen(str), fmt, ap);
241 tell_str(stderr, str);
245 void connection_fatal(char *fmt, ...)
247 char str[0x100]; /* Make the size big enough */
250 strcpy(str, "Fatal:");
251 vsprintf(str + strlen(str), fmt, ap);
254 tell_str(stderr, str);
260 * Be told what socket we're supposed to be using.
262 static SOCKET scp_ssh_socket;
263 char *do_select(SOCKET skt, int startup)
266 scp_ssh_socket = skt;
268 scp_ssh_socket = INVALID_SOCKET;
271 extern int select_result(WPARAM, LPARAM);
274 * Receive a block of data from the SSH link. Block until all data
277 * To do this, we repeatedly call the SSH protocol module, with our
278 * own trap in from_backend() to catch the data that comes back. We
279 * do this until we have enough data.
282 static unsigned char *outptr; /* where to put the data */
283 static unsigned outlen; /* how much data required */
284 static unsigned char *pending = NULL; /* any spare data */
285 static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
286 void from_backend(int is_stderr, char *data, int datalen)
288 unsigned char *p = (unsigned char *) data;
289 unsigned len = (unsigned) datalen;
292 * stderr data is just spouted to local stderr and otherwise
296 fwrite(data, 1, len, stderr);
303 * If this is before the real session begins, just return.
309 unsigned used = outlen;
312 memcpy(outptr, p, used);
320 if (pendsize < pendlen + len) {
321 pendsize = pendlen + len + 4096;
322 pending = (pending ? srealloc(pending, pendsize) :
325 fatalbox("Out of memory");
327 memcpy(pending + pendlen, p, len);
331 static int ssh_scp_recv(unsigned char *buf, int len)
337 * See if the pending-input block contains some of what we
341 unsigned pendused = pendlen;
342 if (pendused > outlen)
344 memcpy(outptr, pending, pendused);
345 memmove(pending, pending + pendused, pendlen - pendused);
362 FD_SET(scp_ssh_socket, &readfds);
363 if (select(1, &readfds, NULL, NULL, NULL) < 0)
365 select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
372 * Loop through the ssh connection and authentication process.
374 static void ssh_scp_init(void)
376 if (scp_ssh_socket == INVALID_SOCKET)
378 while (!back->sendok()) {
381 FD_SET(scp_ssh_socket, &readfds);
382 if (select(1, &readfds, NULL, NULL, NULL) < 0)
384 select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
389 * Print an error message and exit after closing the SSH link.
391 static void bump(char *fmt, ...)
393 char str[0x100]; /* Make the size big enough */
396 strcpy(str, "Fatal:");
397 vsprintf(str + strlen(str), fmt, ap);
400 tell_str(stderr, str);
402 if (back != NULL && back->socket() != NULL) {
404 back->special(TS_EOF);
405 ssh_scp_recv(&ch, 1);
410 static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
413 DWORD savemode, newmode, i;
415 if (is_pw && password) {
416 static int tried_once = 0;
421 strncpy(str, password, maxlen);
422 str[maxlen - 1] = '\0';
428 /* GUI Adaptation - Sept 2000 */
433 hin = GetStdHandle(STD_INPUT_HANDLE);
434 hout = GetStdHandle(STD_OUTPUT_HANDLE);
435 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE)
436 bump("Cannot get standard input/output handles");
438 GetConsoleMode(hin, &savemode);
439 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
441 newmode &= ~ENABLE_ECHO_INPUT;
443 newmode |= ENABLE_ECHO_INPUT;
444 SetConsoleMode(hin, newmode);
446 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
447 ReadFile(hin, str, maxlen - 1, &i, NULL);
449 SetConsoleMode(hin, savemode);
451 if ((int) i > maxlen)
458 WriteFile(hout, "\r\n", 2, &i, NULL);
465 * Open an SSH connection to user@host and execute cmd.
467 static void do_cmd(char *host, char *user, char *cmd)
469 char *err, *realhost;
472 if (host == NULL || host[0] == '\0')
473 bump("Empty host name");
475 /* Try to load settings for this host */
476 do_defaults(host, &cfg);
477 if (cfg.host[0] == '\0') {
478 /* No settings for this host; use defaults */
479 do_defaults(NULL, &cfg);
480 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
481 cfg.host[sizeof(cfg.host) - 1] = '\0';
486 if (user != NULL && user[0] != '\0') {
487 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
488 cfg.username[sizeof(cfg.username) - 1] = '\0';
489 } else if (cfg.username[0] == '\0') {
491 if (GetUserName(user, &namelen) == FALSE)
492 bump("Empty user name");
493 user = smalloc(namelen * sizeof(char));
494 GetUserName(user, &namelen);
496 tell_user(stderr, "Guessing user name: %s", user);
497 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
498 cfg.username[sizeof(cfg.username) - 1] = '\0';
502 if (cfg.protocol != PROT_SSH)
506 cfg.port = portnumber;
508 strncpy(cfg.remote_cmd, cmd, sizeof(cfg.remote_cmd));
509 cfg.remote_cmd[sizeof(cfg.remote_cmd) - 1] = '\0';
514 err = back->init(cfg.host, cfg.port, &realhost);
516 bump("ssh_init: %s", err);
518 if (verbose && realhost != NULL)
519 tell_user(stderr, "Connected to %s\n", realhost);
524 * Update statistic information about current file.
526 static void print_stats(char *name, unsigned long size, unsigned long done,
527 time_t start, time_t now)
534 /* GUI Adaptation - Sept 2000 */
536 gui_update_stats(name, size, (int) (100 * (done * 1.0 / size)),
537 (unsigned long) difftime(now, start));
540 ratebs = (float) done / (now - start);
542 ratebs = (float) done;
547 eta = (unsigned long) ((size - done) / ratebs);
548 sprintf(etastr, "%02ld:%02ld:%02ld",
549 eta / 3600, (eta % 3600) / 60, eta % 60);
551 pct = (int) (100.0 * (float) done / size);
553 printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
554 name, done / 1024, ratebs / 1024.0, etastr, pct);
562 * Find a colon in str and return a pointer to the colon.
563 * This is used to separate hostname from filename.
565 static char *colon(char *str)
567 /* We ignore a leading colon, since the hostname cannot be
568 empty. We also ignore a colon as second character because
569 of filenames like f:myfile.txt. */
570 if (str[0] == '\0' || str[0] == ':' || str[1] == ':')
572 while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\')
581 * Wait for a response from the other side.
582 * Return 0 if ok, -1 if error.
584 static int response(void)
586 char ch, resp, rbuf[2048];
589 if (ssh_scp_recv(&resp, 1) <= 0)
590 bump("Lost connection");
600 case 2: /* fatal error */
602 if (ssh_scp_recv(&ch, 1) <= 0)
603 bump("Protocol error: Lost connection");
605 } while (p < sizeof(rbuf) && ch != '\n');
608 tell_user(stderr, "%s\n", rbuf);
617 * Send an error message to the other side and to the screen.
618 * Increment error counter.
620 static void run_err(const char *fmt, ...)
626 strcpy(str, "scp: ");
627 vsprintf(str + strlen(str), fmt, ap);
629 back->send("\001", 1); /* scp protocol error prefix */
630 back->send(str, strlen(str));
631 tell_user(stderr, "%s", str);
636 * Execute the source part of the SCP protocol.
638 static void source(char *src)
646 unsigned long stat_bytes;
647 time_t stat_starttime, stat_lasttime;
649 attr = GetFileAttributes(src);
650 if (attr == (DWORD) - 1) {
651 run_err("%s: No such file or directory", src);
655 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
658 * Avoid . and .. directories.
661 p = strrchr(src, '/');
663 p = strrchr(src, '\\');
668 if (!strcmp(p, ".") || !strcmp(p, ".."))
669 /* skip . and .. */ ;
673 run_err("%s: not a regular file", src);
678 if ((last = strrchr(src, '/')) == NULL)
682 if (strrchr(last, '\\') != NULL)
683 last = strrchr(last, '\\') + 1;
684 if (last == src && strchr(src, ':') != NULL)
685 last = strchr(src, ':') + 1;
687 f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL,
688 OPEN_EXISTING, 0, 0);
689 if (f == INVALID_HANDLE_VALUE) {
690 run_err("%s: Cannot open file", src);
695 FILETIME actime, wrtime;
696 unsigned long mtime, atime;
697 GetFileTime(f, NULL, &actime, &wrtime);
698 TIME_WIN_TO_POSIX(actime, atime);
699 TIME_WIN_TO_POSIX(wrtime, mtime);
700 sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
701 back->send(buf, strlen(buf));
706 size = GetFileSize(f, NULL);
707 sprintf(buf, "C0644 %lu %s\n", size, last);
709 tell_user(stderr, "Sending file modes: %s", buf);
710 back->send(buf, strlen(buf));
716 stat_starttime = time(NULL);
720 for (i = 0; i < size; i += 4096) {
725 if (!ReadFile(f, transbuf, k, &j, NULL) || j != k) {
728 bump("%s: Read error", src);
730 back->send(transbuf, k);
733 if (time(NULL) != stat_lasttime || i + k == size) {
734 stat_lasttime = time(NULL);
735 print_stats(last, size, stat_bytes,
736 stat_starttime, stat_lasttime);
747 * Recursively send the contents of a directory.
749 static void rsource(char *src)
754 WIN32_FIND_DATA fdat;
757 if ((last = strrchr(src, '/')) == NULL)
761 if (strrchr(last, '\\') != NULL)
762 last = strrchr(last, '\\') + 1;
763 if (last == src && strchr(src, ':') != NULL)
764 last = strchr(src, ':') + 1;
766 /* maybe send filetime */
768 sprintf(buf, "D0755 0 %s\n", last);
770 tell_user(stderr, "Entering directory: %s", buf);
771 back->send(buf, strlen(buf));
775 sprintf(buf, "%s/*", src);
776 dir = FindFirstFile(buf, &fdat);
777 ok = (dir != INVALID_HANDLE_VALUE);
779 if (strcmp(fdat.cFileName, ".") == 0 ||
780 strcmp(fdat.cFileName, "..") == 0) {
781 } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= sizeof(buf)) {
782 run_err("%s/%s: Name too long", src, fdat.cFileName);
784 sprintf(buf, "%s/%s", src, fdat.cFileName);
787 ok = FindNextFile(dir, &fdat);
792 back->send(buf, strlen(buf));
797 * Execute the sink part of the SCP protocol.
799 static void sink(char *targ, char *src)
809 unsigned long mtime, atime;
811 unsigned long size, i;
813 unsigned long stat_bytes;
814 time_t stat_starttime, stat_lasttime;
817 attr = GetFileAttributes(targ);
818 if (attr != (DWORD) - 1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
821 if (targetshouldbedirectory && !targisdir)
822 bump("%s: Not a directory", targ);
828 if (ssh_scp_recv(&ch, 1) <= 0)
831 bump("Protocol error: Unexpected newline");
835 if (ssh_scp_recv(&ch, 1) <= 0)
836 bump("Lost connection");
838 } while (i < sizeof(buf) && ch != '\n');
841 case '\01': /* error */
842 tell_user(stderr, "%s\n", buf + 1);
845 case '\02': /* fatal error */
851 if (sscanf(buf, "T%ld %*d %ld %*d", &mtime, &atime) == 2) {
856 bump("Protocol error: Illegal time format");
861 bump("Protocol error: Expected control record");
864 if (sscanf(buf + 1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3)
865 bump("Protocol error: Illegal file descriptor format");
866 /* Security fix: ensure the file ends up where we asked for it. */
873 p = namebuf + strlen(namebuf);
874 while (p > namebuf && p[-1] != '/' && p[-1] != '\\')
879 strcpy(namebuf, targ);
881 attr = GetFileAttributes(namebuf);
882 exists = (attr != (DWORD) - 1);
885 if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
886 run_err("%s: Not a directory", namebuf);
890 if (!CreateDirectory(namebuf, NULL)) {
891 run_err("%s: Cannot create directory", namebuf);
896 /* can we set the timestamp for directories ? */
900 f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,
901 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
902 if (f == INVALID_HANDLE_VALUE) {
903 run_err("%s: Cannot create file", namebuf);
911 stat_starttime = time(NULL);
913 if ((stat_name = strrchr(namebuf, '/')) == NULL)
917 if (strrchr(stat_name, '\\') != NULL)
918 stat_name = strrchr(stat_name, '\\') + 1;
921 for (i = 0; i < size; i += 4096) {
926 if (ssh_scp_recv(transbuf, k) == 0)
927 bump("Lost connection");
930 if (!WriteFile(f, transbuf, k, &j, NULL) || j != k) {
933 printf("\r%-25.25s | %50s\n",
935 "Write error.. waiting for end of file");
940 if (time(NULL) > stat_lasttime || i + k == size) {
941 stat_lasttime = time(NULL);
942 print_stats(stat_name, size, stat_bytes,
943 stat_starttime, stat_lasttime);
950 FILETIME actime, wrtime;
951 TIME_POSIX_TO_WIN(atime, actime);
952 TIME_POSIX_TO_WIN(mtime, wrtime);
953 SetFileTime(f, NULL, &actime, &wrtime);
958 run_err("%s: Write error", namebuf);
966 * We will copy local files to a remote server.
968 static void toremote(int argc, char *argv[])
970 char *src, *targ, *host, *user;
974 targ = argv[argc - 1];
976 /* Separate host from filename */
980 bump("targ == NULL in toremote()");
984 /* Substitute "." for emtpy target */
986 /* Separate host and username */
988 host = strrchr(host, '@');
999 /* Find out if the source filespec covers multiple files
1000 if so, we should set the targetshouldbedirectory flag */
1002 WIN32_FIND_DATA fdat;
1003 if (colon(argv[0]) != NULL)
1004 bump("%s: Remote to remote not supported", argv[0]);
1005 fh = FindFirstFile(argv[0], &fdat);
1006 if (fh == INVALID_HANDLE_VALUE)
1007 bump("%s: No such file or directory\n", argv[0]);
1008 if (FindNextFile(fh, &fdat))
1009 targetshouldbedirectory = 1;
1013 cmd = smalloc(strlen(targ) + 100);
1014 sprintf(cmd, "scp%s%s%s%s -t %s",
1015 verbose ? " -v" : "",
1016 recursive ? " -r" : "",
1017 preserve ? " -p" : "",
1018 targetshouldbedirectory ? " -d" : "", targ);
1019 do_cmd(host, user, cmd);
1024 for (i = 0; i < argc - 1; i++) {
1026 WIN32_FIND_DATA fdat;
1028 if (colon(src) != NULL) {
1029 tell_user(stderr, "%s: Remote to remote not supported\n", src);
1033 dir = FindFirstFile(src, &fdat);
1034 if (dir == INVALID_HANDLE_VALUE) {
1035 run_err("%s: No such file or directory", src);
1042 * Ensure that . and .. are never matched by wildcards,
1043 * but only by deliberate action.
1045 if (!strcmp(fdat.cFileName, ".") ||
1046 !strcmp(fdat.cFileName, "..")) {
1048 * Find*File has returned a special dir. We require
1049 * that _either_ `src' ends in a backslash followed
1050 * by that string, _or_ `src' is precisely that
1053 int len = strlen(src), dlen = strlen(fdat.cFileName);
1054 if (len == dlen && !strcmp(src, fdat.cFileName)) {
1056 } else if (len > dlen + 1 && src[len - dlen - 1] == '\\' &&
1057 !strcmp(src + len - dlen, fdat.cFileName)) {
1060 continue; /* ignore this one */
1062 if (strlen(src) + strlen(fdat.cFileName) >= sizeof(namebuf)) {
1063 tell_user(stderr, "%s: Name too long", src);
1066 strcpy(namebuf, src);
1067 if ((last = strrchr(namebuf, '/')) == NULL)
1071 if (strrchr(last, '\\') != NULL)
1072 last = strrchr(last, '\\') + 1;
1073 if (last == namebuf && strrchr(namebuf, ':') != NULL)
1074 last = strchr(namebuf, ':') + 1;
1075 strcpy(last, fdat.cFileName);
1077 } while (FindNextFile(dir, &fdat));
1083 * We will copy files from a remote server to the local machine.
1085 static void tolocal(int argc, char *argv[])
1087 char *src, *targ, *host, *user;
1091 bump("More than one remote source not supported");
1096 /* Separate host from filename */
1100 bump("Local to local copy not supported");
1104 /* Substitute "." for empty filename */
1106 /* Separate username and hostname */
1108 host = strrchr(host, '@');
1118 cmd = smalloc(strlen(src) + 100);
1119 sprintf(cmd, "scp%s%s%s%s -f %s",
1120 verbose ? " -v" : "",
1121 recursive ? " -r" : "",
1122 preserve ? " -p" : "",
1123 targetshouldbedirectory ? " -d" : "", src);
1124 do_cmd(host, user, cmd);
1131 * We will issue a list command to get a remote directory.
1133 static void get_dir_list(int argc, char *argv[])
1135 char *src, *host, *user;
1141 /* Separate host from filename */
1145 bump("Local to local copy not supported");
1149 /* Substitute "." for empty filename */
1151 /* Separate username and hostname */
1153 host = strrchr(host, '@');
1163 cmd = smalloc(4 * strlen(src) + 100);
1164 strcpy(cmd, "ls -la '");
1165 p = cmd + strlen(cmd);
1166 for (q = src; *q; q++) {
1179 do_cmd(host, user, cmd);
1182 while (ssh_scp_recv(&c, 1) > 0)
1183 tell_char(stdout, c);
1187 * Initialize the Win$ock driver.
1189 static void init_winsock(void)
1194 winsock_ver = MAKEWORD(1, 1);
1195 if (WSAStartup(winsock_ver, &wsadata))
1196 bump("Unable to initialise WinSock");
1197 if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1)
1198 bump("WinSock version is incompatible with 1.1");
1202 * Short description of parameters.
1204 static void usage(void)
1206 printf("PuTTY Secure Copy client\n");
1207 printf("%s\n", ver);
1208 printf("Usage: pscp [options] [user@]host:source target\n");
1210 (" pscp [options] source [source...] [user@]host:target\n");
1211 printf(" pscp [options] -ls user@host:filespec\n");
1212 printf("Options:\n");
1213 printf(" -p preserve file attributes\n");
1214 printf(" -q quiet, don't show statistics\n");
1215 printf(" -r copy directories recursively\n");
1216 printf(" -v show verbose messages\n");
1217 printf(" -P port connect to specified port\n");
1218 printf(" -pw passw login with specified password\n");
1221 * -gui is an internal option, used by GUI front ends to get
1222 * pscp to pass progress reports back to them. It's not an
1223 * ordinary user-accessible option, so it shouldn't be part of
1224 * the command-line help. The only people who need to know
1225 * about it are programmers, and they can read the source.
1228 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
1234 * Main program (no, really?)
1236 int main(int argc, char *argv[])
1241 default_protocol = PROT_TELNET;
1243 flags = FLAG_STDERR;
1244 ssh_get_line = &get_line;
1248 for (i = 1; i < argc; i++) {
1249 if (argv[i][0] != '-')
1251 if (strcmp(argv[i], "-v") == 0)
1252 verbose = 1, flags |= FLAG_VERBOSE;
1253 else if (strcmp(argv[i], "-r") == 0)
1255 else if (strcmp(argv[i], "-p") == 0)
1257 else if (strcmp(argv[i], "-q") == 0)
1259 else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0)
1261 else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc)
1262 portnumber = atoi(argv[++i]);
1263 else if (strcmp(argv[i], "-pw") == 0 && i + 1 < argc)
1264 password = argv[++i];
1265 else if (strcmp(argv[i], "-gui") == 0 && i + 1 < argc) {
1266 gui_hwnd = argv[++i];
1268 } else if (strcmp(argv[i], "-ls") == 0)
1270 else if (strcmp(argv[i], "--") == 0) {
1283 get_dir_list(argc, argv);
1290 targetshouldbedirectory = 1;
1292 if (colon(argv[argc - 1]) != NULL)
1293 toremote(argc, argv);
1295 tolocal(argc, argv);
1298 if (back != NULL && back->socket() != NULL) {
1300 back->special(TS_EOF);
1301 ssh_scp_recv(&ch, 1);
1306 /* GUI Adaptation - August 2000 */
1308 unsigned int msg_id = WM_RET_ERR_CNT;
1310 msg_id = WM_LS_RET_ERR_CNT;
1312 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
1313 0 /*lParam */ ))SleepEx(1000, TRUE);
1315 return (errs == 0 ? 0 : 1);