07d9aa13 |
1 | /* |
2 | * scp.c - Scp (Secure Copy) client for PuTTY. |
b8a19193 |
3 | * Joris van Rantwijk, Aug 1999, Nov 1999. |
07d9aa13 |
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 | |
07d9aa13 |
9 | #include <windows.h> |
10 | #include <winsock.h> |
11 | #include <stdlib.h> |
12 | #include <stdio.h> |
13 | #include <string.h> |
14 | #include <time.h> |
15 | |
16 | #define PUTTY_DO_GLOBALS |
17 | #include "putty.h" |
18 | #include "scp.h" |
19 | |
07d9aa13 |
20 | #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \ |
c51a56e2 |
21 | ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000) |
07d9aa13 |
22 | #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \ |
c51a56e2 |
23 | ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600)) |
07d9aa13 |
24 | |
25 | int verbose = 0; |
26 | static int recursive = 0; |
27 | static int preserve = 0; |
28 | static int targetshouldbedirectory = 0; |
29 | static int statistics = 1; |
b8a19193 |
30 | static int portnumber = 0; |
31 | static char *password = NULL; |
07d9aa13 |
32 | static int errs = 0; |
33 | static int connection_open = 0; |
34 | |
35 | static void source(char *src); |
36 | static void rsource(char *src); |
37 | static void sink(char *targ); |
38 | |
07d9aa13 |
39 | /* |
40 | * Print an error message and perform a fatal exit. |
41 | */ |
42 | void fatalbox(char *fmt, ...) |
43 | { |
c51a56e2 |
44 | va_list ap; |
45 | va_start(ap, fmt); |
46 | fprintf(stderr, "Fatal: "); |
47 | vfprintf(stderr, fmt, ap); |
48 | fprintf(stderr, "\n"); |
49 | va_end(ap); |
50 | exit(1); |
07d9aa13 |
51 | } |
52 | |
07d9aa13 |
53 | /* |
54 | * Print an error message and exit after closing the SSH link. |
55 | */ |
56 | static void bump(char *fmt, ...) |
57 | { |
c51a56e2 |
58 | va_list ap; |
59 | va_start(ap, fmt); |
60 | fprintf(stderr, "Fatal: "); |
61 | vfprintf(stderr, fmt, ap); |
62 | fprintf(stderr, "\n"); |
63 | va_end(ap); |
64 | if (connection_open) { |
65 | char ch; |
66 | ssh_send_eof(); |
67 | ssh_recv(&ch, 1); |
68 | } |
69 | exit(1); |
07d9aa13 |
70 | } |
71 | |
07d9aa13 |
72 | void ssh_get_password(char *prompt, char *str, int maxlen) |
73 | { |
c51a56e2 |
74 | HANDLE hin, hout; |
b8a19193 |
75 | DWORD savemode, i; |
76 | |
77 | if (password) { |
78 | strncpy(str, password, maxlen); |
79 | str[maxlen-1] = '\0'; |
80 | password = NULL; |
81 | return; |
82 | } |
07d9aa13 |
83 | |
c51a56e2 |
84 | hin = GetStdHandle(STD_INPUT_HANDLE); |
85 | hout = GetStdHandle(STD_OUTPUT_HANDLE); |
86 | if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) |
87 | bump("Cannot get standard input/output handles"); |
07d9aa13 |
88 | |
c51a56e2 |
89 | GetConsoleMode(hin, &savemode); |
90 | SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | |
91 | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); |
07d9aa13 |
92 | |
c51a56e2 |
93 | WriteFile(hout, prompt, strlen(prompt), &i, NULL); |
94 | ReadFile(hin, str, maxlen-1, &i, NULL); |
07d9aa13 |
95 | |
c51a56e2 |
96 | SetConsoleMode(hin, savemode); |
07d9aa13 |
97 | |
b65b8aae |
98 | if ((int)i > maxlen) i = maxlen-1; else i = i - 2; |
c51a56e2 |
99 | str[i] = '\0'; |
07d9aa13 |
100 | |
c51a56e2 |
101 | WriteFile(hout, "\r\n", 2, &i, NULL); |
07d9aa13 |
102 | } |
103 | |
07d9aa13 |
104 | /* |
105 | * Open an SSH connection to user@host and execute cmd. |
106 | */ |
107 | static void do_cmd(char *host, char *user, char *cmd) |
108 | { |
c51a56e2 |
109 | char *err, *realhost; |
110 | |
111 | if (host == NULL || host[0] == '\0') |
112 | bump("Empty host name"); |
113 | |
114 | /* Try to load settings for this host */ |
115 | do_defaults(host); |
116 | if (cfg.host[0] == '\0') { |
117 | /* No settings for this host; use defaults */ |
118 | strncpy(cfg.host, host, sizeof(cfg.host)-1); |
119 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
120 | cfg.port = 22; |
121 | } |
122 | |
123 | /* Set username */ |
124 | if (user != NULL && user[0] != '\0') { |
125 | strncpy(cfg.username, user, sizeof(cfg.username)-1); |
126 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
c51a56e2 |
127 | } else if (cfg.username[0] == '\0') { |
128 | bump("Empty user name"); |
129 | } |
130 | |
131 | if (cfg.protocol != PROT_SSH) |
132 | cfg.port = 22; |
133 | |
ed89e8a5 |
134 | if (portnumber) |
135 | cfg.port = portnumber; |
136 | |
c51a56e2 |
137 | err = ssh_init(cfg.host, cfg.port, cmd, &realhost); |
138 | if (err != NULL) |
139 | bump("ssh_init: %s", err); |
140 | if (verbose && realhost != NULL) |
141 | fprintf(stderr, "Connected to %s\n", realhost); |
142 | |
143 | connection_open = 1; |
07d9aa13 |
144 | } |
145 | |
07d9aa13 |
146 | /* |
147 | * Update statistic information about current file. |
148 | */ |
149 | static void print_stats(char *name, unsigned long size, unsigned long done, |
c51a56e2 |
150 | time_t start, time_t now) |
07d9aa13 |
151 | { |
c51a56e2 |
152 | float ratebs; |
153 | unsigned long eta; |
154 | char etastr[10]; |
155 | int pct; |
156 | |
157 | if (now > start) |
158 | ratebs = (float) done / (now - start); |
159 | else |
160 | ratebs = (float) done; |
161 | |
162 | if (ratebs < 1.0) |
163 | eta = size - done; |
164 | else |
165 | eta = (unsigned long) ((size - done) / ratebs); |
1d470ad2 |
166 | sprintf(etastr, "%02ld:%02ld:%02ld", |
c51a56e2 |
167 | eta / 3600, (eta % 3600) / 60, eta % 60); |
168 | |
169 | pct = (int) (100.0 * (float) done / size); |
170 | |
171 | printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%", |
172 | name, done / 1024, ratebs / 1024.0, |
173 | etastr, pct); |
174 | |
175 | if (done == size) |
176 | printf("\n"); |
07d9aa13 |
177 | } |
178 | |
07d9aa13 |
179 | /* |
180 | * Find a colon in str and return a pointer to the colon. |
181 | * This is used to seperate hostname from filename. |
182 | */ |
183 | static char * colon(char *str) |
184 | { |
c51a56e2 |
185 | /* We ignore a leading colon, since the hostname cannot be |
186 | empty. We also ignore a colon as second character because |
187 | of filenames like f:myfile.txt. */ |
188 | if (str[0] == '\0' || |
189 | str[0] == ':' || |
190 | str[1] == ':') |
191 | return (NULL); |
192 | while (*str != '\0' && |
193 | *str != ':' && |
194 | *str != '/' && |
195 | *str != '\\') |
196 | str++; |
197 | if (*str == ':') |
198 | return (str); |
199 | else |
200 | return (NULL); |
07d9aa13 |
201 | } |
202 | |
07d9aa13 |
203 | /* |
204 | * Wait for a response from the other side. |
205 | * Return 0 if ok, -1 if error. |
206 | */ |
207 | static int response(void) |
208 | { |
c51a56e2 |
209 | char ch, resp, rbuf[2048]; |
210 | int p; |
211 | |
212 | if (ssh_recv(&resp, 1) <= 0) |
213 | bump("Lost connection"); |
214 | |
215 | p = 0; |
216 | switch (resp) { |
217 | case 0: /* ok */ |
218 | return (0); |
219 | default: |
220 | rbuf[p++] = resp; |
221 | /* fallthrough */ |
222 | case 1: /* error */ |
223 | case 2: /* fatal error */ |
224 | do { |
225 | if (ssh_recv(&ch, 1) <= 0) |
226 | bump("Protocol error: Lost connection"); |
227 | rbuf[p++] = ch; |
228 | } while (p < sizeof(rbuf) && ch != '\n'); |
229 | rbuf[p-1] = '\0'; |
230 | if (resp == 1) |
231 | fprintf(stderr, "%s\n", rbuf); |
232 | else |
233 | bump("%s", rbuf); |
234 | errs++; |
235 | return (-1); |
236 | } |
07d9aa13 |
237 | } |
238 | |
07d9aa13 |
239 | /* |
240 | * Send an error message to the other side and to the screen. |
241 | * Increment error counter. |
242 | */ |
243 | static void run_err(const char *fmt, ...) |
244 | { |
c51a56e2 |
245 | char str[2048]; |
246 | va_list ap; |
247 | va_start(ap, fmt); |
248 | errs++; |
249 | strcpy(str, "\01scp: "); |
250 | vsprintf(str+strlen(str), fmt, ap); |
251 | strcat(str, "\n"); |
252 | ssh_send(str, strlen(str)); |
253 | vfprintf(stderr, fmt, ap); |
254 | fprintf(stderr, "\n"); |
255 | va_end(ap); |
07d9aa13 |
256 | } |
257 | |
07d9aa13 |
258 | /* |
259 | * Execute the source part of the SCP protocol. |
260 | */ |
261 | static void source(char *src) |
262 | { |
c51a56e2 |
263 | char buf[2048]; |
264 | unsigned long size; |
265 | char *last; |
266 | HANDLE f; |
267 | DWORD attr; |
268 | unsigned long i; |
269 | unsigned long stat_bytes; |
270 | time_t stat_starttime, stat_lasttime; |
271 | |
272 | attr = GetFileAttributes(src); |
996c8c3b |
273 | if (attr == (DWORD)-1) { |
c51a56e2 |
274 | run_err("%s: No such file or directory", src); |
275 | return; |
276 | } |
277 | |
278 | if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
7f1f80de |
279 | if (recursive) { |
280 | /* |
281 | * Avoid . and .. directories. |
282 | */ |
283 | char *p; |
284 | p = strrchr(src, '/'); |
285 | if (!p) |
286 | p = strrchr(src, '\\'); |
287 | if (!p) |
288 | p = src; |
289 | else |
290 | p++; |
291 | if (!strcmp(p, ".") || !strcmp(p, "..")) |
292 | /* skip . and .. */; |
293 | else |
294 | rsource(src); |
295 | } else { |
c51a56e2 |
296 | run_err("%s: not a regular file", src); |
7f1f80de |
297 | } |
c51a56e2 |
298 | return; |
299 | } |
300 | |
301 | if ((last = strrchr(src, '/')) == NULL) |
302 | last = src; |
303 | else |
304 | last++; |
305 | if (strrchr(last, '\\') != NULL) |
306 | last = strrchr(last, '\\') + 1; |
307 | if (last == src && strchr(src, ':') != NULL) |
308 | last = strchr(src, ':') + 1; |
309 | |
310 | f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL, |
311 | OPEN_EXISTING, 0, 0); |
312 | if (f == INVALID_HANDLE_VALUE) { |
486543a1 |
313 | run_err("%s: Cannot open file", src); |
c51a56e2 |
314 | return; |
315 | } |
316 | |
317 | if (preserve) { |
318 | FILETIME actime, wrtime; |
319 | unsigned long mtime, atime; |
320 | GetFileTime(f, NULL, &actime, &wrtime); |
321 | TIME_WIN_TO_POSIX(actime, atime); |
322 | TIME_WIN_TO_POSIX(wrtime, mtime); |
323 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
07d9aa13 |
324 | ssh_send(buf, strlen(buf)); |
325 | if (response()) |
c51a56e2 |
326 | return; |
327 | } |
328 | |
329 | size = GetFileSize(f, NULL); |
330 | sprintf(buf, "C0644 %lu %s\n", size, last); |
331 | if (verbose) |
332 | fprintf(stderr, "Sending file modes: %s", buf); |
333 | ssh_send(buf, strlen(buf)); |
334 | if (response()) |
335 | return; |
336 | |
337 | if (statistics) { |
338 | stat_bytes = 0; |
339 | stat_starttime = time(NULL); |
340 | stat_lasttime = 0; |
341 | } |
342 | |
343 | for (i = 0; i < size; i += 4096) { |
344 | char transbuf[4096]; |
345 | DWORD j, k = 4096; |
346 | if (i + k > size) k = size - i; |
347 | if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) { |
348 | if (statistics) printf("\n"); |
349 | bump("%s: Read error", src); |
07d9aa13 |
350 | } |
c51a56e2 |
351 | ssh_send(transbuf, k); |
352 | if (statistics) { |
353 | stat_bytes += k; |
354 | if (time(NULL) != stat_lasttime || |
355 | i + k == size) { |
356 | stat_lasttime = time(NULL); |
357 | print_stats(last, size, stat_bytes, |
358 | stat_starttime, stat_lasttime); |
359 | } |
07d9aa13 |
360 | } |
c51a56e2 |
361 | } |
362 | CloseHandle(f); |
07d9aa13 |
363 | |
c51a56e2 |
364 | ssh_send("", 1); |
365 | (void) response(); |
07d9aa13 |
366 | } |
367 | |
07d9aa13 |
368 | /* |
369 | * Recursively send the contents of a directory. |
370 | */ |
371 | static void rsource(char *src) |
372 | { |
c51a56e2 |
373 | char buf[2048]; |
374 | char *last; |
375 | HANDLE dir; |
376 | WIN32_FIND_DATA fdat; |
377 | int ok; |
378 | |
379 | if ((last = strrchr(src, '/')) == NULL) |
380 | last = src; |
381 | else |
382 | last++; |
383 | if (strrchr(last, '\\') != NULL) |
384 | last = strrchr(last, '\\') + 1; |
385 | if (last == src && strchr(src, ':') != NULL) |
386 | last = strchr(src, ':') + 1; |
387 | |
388 | /* maybe send filetime */ |
389 | |
390 | sprintf(buf, "D0755 0 %s\n", last); |
391 | if (verbose) |
392 | fprintf(stderr, "Entering directory: %s", buf); |
393 | ssh_send(buf, strlen(buf)); |
394 | if (response()) |
395 | return; |
396 | |
397 | sprintf(buf, "%s/*", src); |
398 | dir = FindFirstFile(buf, &fdat); |
399 | ok = (dir != INVALID_HANDLE_VALUE); |
400 | while (ok) { |
401 | if (strcmp(fdat.cFileName, ".") == 0 || |
402 | strcmp(fdat.cFileName, "..") == 0) { |
403 | } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= |
404 | sizeof(buf)) { |
405 | run_err("%s/%s: Name too long", src, fdat.cFileName); |
406 | } else { |
407 | sprintf(buf, "%s/%s", src, fdat.cFileName); |
408 | source(buf); |
07d9aa13 |
409 | } |
c51a56e2 |
410 | ok = FindNextFile(dir, &fdat); |
411 | } |
412 | FindClose(dir); |
07d9aa13 |
413 | |
c51a56e2 |
414 | sprintf(buf, "E\n"); |
415 | ssh_send(buf, strlen(buf)); |
416 | (void) response(); |
07d9aa13 |
417 | } |
418 | |
07d9aa13 |
419 | /* |
420 | * Execute the sink part of the SCP protocol. |
421 | */ |
422 | static void sink(char *targ) |
423 | { |
c51a56e2 |
424 | char buf[2048]; |
425 | char namebuf[2048]; |
426 | char ch; |
427 | int targisdir = 0; |
996c8c3b |
428 | int settime; |
c51a56e2 |
429 | int exists; |
430 | DWORD attr; |
431 | HANDLE f; |
432 | unsigned long mtime, atime; |
433 | unsigned int mode; |
434 | unsigned long size, i; |
435 | int wrerror = 0; |
436 | unsigned long stat_bytes; |
437 | time_t stat_starttime, stat_lasttime; |
438 | char *stat_name; |
439 | |
440 | attr = GetFileAttributes(targ); |
996c8c3b |
441 | if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
c51a56e2 |
442 | targisdir = 1; |
443 | |
444 | if (targetshouldbedirectory && !targisdir) |
445 | bump("%s: Not a directory", targ); |
446 | |
447 | ssh_send("", 1); |
448 | while (1) { |
449 | settime = 0; |
450 | gottime: |
451 | if (ssh_recv(&ch, 1) <= 0) |
452 | return; |
453 | if (ch == '\n') |
454 | bump("Protocol error: Unexpected newline"); |
455 | i = 0; |
456 | buf[i++] = ch; |
457 | do { |
458 | if (ssh_recv(&ch, 1) <= 0) |
459 | bump("Lost connection"); |
460 | buf[i++] = ch; |
461 | } while (i < sizeof(buf) && ch != '\n'); |
462 | buf[i-1] = '\0'; |
463 | switch (buf[0]) { |
464 | case '\01': /* error */ |
465 | fprintf(stderr, "%s\n", buf+1); |
466 | errs++; |
467 | continue; |
468 | case '\02': /* fatal error */ |
469 | bump("%s", buf+1); |
470 | case 'E': |
471 | ssh_send("", 1); |
472 | return; |
473 | case 'T': |
1d470ad2 |
474 | if (sscanf(buf, "T%ld %*d %ld %*d", |
c51a56e2 |
475 | &mtime, &atime) == 2) { |
476 | settime = 1; |
477 | ssh_send("", 1); |
478 | goto gottime; |
479 | } |
480 | bump("Protocol error: Illegal time format"); |
481 | case 'C': |
482 | case 'D': |
483 | break; |
484 | default: |
485 | bump("Protocol error: Expected control record"); |
486 | } |
07d9aa13 |
487 | |
1d470ad2 |
488 | if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3) |
c51a56e2 |
489 | bump("Protocol error: Illegal file descriptor format"); |
490 | if (targisdir) { |
491 | char t[2048]; |
492 | strcpy(t, targ); |
493 | if (targ[0] != '\0') |
494 | strcat(t, "/"); |
495 | strcat(t, namebuf); |
496 | strcpy(namebuf, t); |
497 | } else { |
498 | strcpy(namebuf, targ); |
499 | } |
500 | attr = GetFileAttributes(namebuf); |
996c8c3b |
501 | exists = (attr != (DWORD)-1); |
c51a56e2 |
502 | |
503 | if (buf[0] == 'D') { |
504 | if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
505 | run_err("%s: Not a directory", namebuf); |
506 | continue; |
507 | } |
508 | if (!exists) { |
509 | if (! CreateDirectory(namebuf, NULL)) { |
510 | run_err("%s: Cannot create directory", |
511 | namebuf); |
512 | continue; |
513 | } |
514 | } |
515 | sink(namebuf); |
516 | /* can we set the timestamp for directories ? */ |
517 | continue; |
518 | } |
07d9aa13 |
519 | |
c51a56e2 |
520 | f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL, |
521 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
522 | if (f == INVALID_HANDLE_VALUE) { |
523 | run_err("%s: Cannot create file", namebuf); |
524 | continue; |
525 | } |
07d9aa13 |
526 | |
527 | ssh_send("", 1); |
07d9aa13 |
528 | |
c51a56e2 |
529 | if (statistics) { |
530 | stat_bytes = 0; |
531 | stat_starttime = time(NULL); |
532 | stat_lasttime = 0; |
533 | if ((stat_name = strrchr(namebuf, '/')) == NULL) |
534 | stat_name = namebuf; |
535 | else |
536 | stat_name++; |
537 | if (strrchr(stat_name, '\\') != NULL) |
538 | stat_name = strrchr(stat_name, '\\') + 1; |
539 | } |
07d9aa13 |
540 | |
c51a56e2 |
541 | for (i = 0; i < size; i += 4096) { |
542 | char transbuf[4096]; |
996c8c3b |
543 | DWORD j, k = 4096; |
c51a56e2 |
544 | if (i + k > size) k = size - i; |
545 | if (ssh_recv(transbuf, k) == 0) |
546 | bump("Lost connection"); |
547 | if (wrerror) continue; |
548 | if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) { |
549 | wrerror = 1; |
550 | if (statistics) |
551 | printf("\r%-25.25s | %50s\n", |
552 | stat_name, |
553 | "Write error.. waiting for end of file"); |
554 | continue; |
555 | } |
556 | if (statistics) { |
557 | stat_bytes += k; |
558 | if (time(NULL) > stat_lasttime || |
559 | i + k == size) { |
560 | stat_lasttime = time(NULL); |
561 | print_stats(stat_name, size, stat_bytes, |
562 | stat_starttime, stat_lasttime); |
07d9aa13 |
563 | } |
c51a56e2 |
564 | } |
565 | } |
566 | (void) response(); |
07d9aa13 |
567 | |
c51a56e2 |
568 | if (settime) { |
569 | FILETIME actime, wrtime; |
570 | TIME_POSIX_TO_WIN(atime, actime); |
571 | TIME_POSIX_TO_WIN(mtime, wrtime); |
572 | SetFileTime(f, NULL, &actime, &wrtime); |
07d9aa13 |
573 | } |
07d9aa13 |
574 | |
c51a56e2 |
575 | CloseHandle(f); |
576 | if (wrerror) { |
577 | run_err("%s: Write error", namebuf); |
578 | continue; |
579 | } |
580 | ssh_send("", 1); |
581 | } |
582 | } |
07d9aa13 |
583 | |
584 | /* |
585 | * We will copy local files to a remote server. |
586 | */ |
587 | static void toremote(int argc, char *argv[]) |
588 | { |
c51a56e2 |
589 | char *src, *targ, *host, *user; |
590 | char *cmd; |
591 | int i; |
592 | |
593 | targ = argv[argc-1]; |
594 | |
595 | /* Seperate host from filename */ |
596 | host = targ; |
597 | targ = colon(targ); |
598 | if (targ == NULL) |
599 | bump("targ == NULL in toremote()"); |
600 | *targ++ = '\0'; |
601 | if (*targ == '\0') |
602 | targ = "."; |
603 | /* Substitute "." for emtpy target */ |
604 | |
605 | /* Seperate host and username */ |
606 | user = host; |
607 | host = strrchr(host, '@'); |
608 | if (host == NULL) { |
609 | host = user; |
610 | user = NULL; |
611 | } else { |
612 | *host++ = '\0'; |
613 | if (*user == '\0') |
614 | user = NULL; |
615 | } |
616 | |
617 | if (argc == 2) { |
618 | /* Find out if the source filespec covers multiple files |
619 | if so, we should set the targetshouldbedirectory flag */ |
620 | HANDLE fh; |
621 | WIN32_FIND_DATA fdat; |
622 | if (colon(argv[0]) != NULL) |
623 | bump("%s: Remote to remote not supported", argv[0]); |
624 | fh = FindFirstFile(argv[0], &fdat); |
625 | if (fh == INVALID_HANDLE_VALUE) |
626 | bump("%s: No such file or directory\n", argv[0]); |
627 | if (FindNextFile(fh, &fdat)) |
628 | targetshouldbedirectory = 1; |
629 | FindClose(fh); |
630 | } |
631 | |
632 | cmd = smalloc(strlen(targ) + 100); |
633 | sprintf(cmd, "scp%s%s%s%s -t %s", |
634 | verbose ? " -v" : "", |
635 | recursive ? " -r" : "", |
636 | preserve ? " -p" : "", |
637 | targetshouldbedirectory ? " -d" : "", |
638 | targ); |
639 | do_cmd(host, user, cmd); |
640 | sfree(cmd); |
641 | |
642 | (void) response(); |
643 | |
644 | for (i = 0; i < argc - 1; i++) { |
645 | HANDLE dir; |
646 | WIN32_FIND_DATA fdat; |
647 | src = argv[i]; |
648 | if (colon(src) != NULL) { |
649 | fprintf(stderr, |
650 | "%s: Remote to remote not supported\n", src); |
651 | errs++; |
652 | continue; |
07d9aa13 |
653 | } |
c51a56e2 |
654 | dir = FindFirstFile(src, &fdat); |
655 | if (dir == INVALID_HANDLE_VALUE) { |
656 | run_err("%s: No such file or directory", src); |
657 | continue; |
07d9aa13 |
658 | } |
c51a56e2 |
659 | do { |
660 | char *last; |
661 | char namebuf[2048]; |
662 | if (strlen(src) + strlen(fdat.cFileName) >= |
663 | sizeof(namebuf)) { |
664 | fprintf(stderr, "%s: Name too long", src); |
665 | continue; |
666 | } |
667 | strcpy(namebuf, src); |
668 | if ((last = strrchr(namebuf, '/')) == NULL) |
669 | last = namebuf; |
670 | else |
671 | last++; |
672 | if (strrchr(last, '\\') != NULL) |
673 | last = strrchr(last, '\\') + 1; |
674 | if (last == namebuf && strrchr(namebuf, ':') != NULL) |
675 | last = strchr(namebuf, ':') + 1; |
676 | strcpy(last, fdat.cFileName); |
677 | source(namebuf); |
678 | } while (FindNextFile(dir, &fdat)); |
679 | FindClose(dir); |
680 | } |
07d9aa13 |
681 | } |
682 | |
07d9aa13 |
683 | /* |
684 | * We will copy files from a remote server to the local machine. |
685 | */ |
686 | static void tolocal(int argc, char *argv[]) |
687 | { |
c51a56e2 |
688 | char *src, *targ, *host, *user; |
689 | char *cmd; |
690 | |
691 | if (argc != 2) |
692 | bump("More than one remote source not supported"); |
693 | |
694 | src = argv[0]; |
695 | targ = argv[1]; |
696 | |
697 | /* Seperate host from filename */ |
698 | host = src; |
699 | src = colon(src); |
700 | if (src == NULL) |
701 | bump("Local to local copy not supported"); |
702 | *src++ = '\0'; |
703 | if (*src == '\0') |
704 | src = "."; |
705 | /* Substitute "." for empty filename */ |
706 | |
707 | /* Seperate username and hostname */ |
708 | user = host; |
709 | host = strrchr(host, '@'); |
710 | if (host == NULL) { |
711 | host = user; |
712 | user = NULL; |
713 | } else { |
714 | *host++ = '\0'; |
715 | if (*user == '\0') |
716 | user = NULL; |
717 | } |
718 | |
719 | cmd = smalloc(strlen(src) + 100); |
720 | sprintf(cmd, "scp%s%s%s%s -f %s", |
721 | verbose ? " -v" : "", |
722 | recursive ? " -r" : "", |
723 | preserve ? " -p" : "", |
724 | targetshouldbedirectory ? " -d" : "", |
725 | src); |
726 | do_cmd(host, user, cmd); |
727 | sfree(cmd); |
728 | |
729 | sink(targ); |
07d9aa13 |
730 | } |
731 | |
07d9aa13 |
732 | /* |
733 | * Initialize the Win$ock driver. |
734 | */ |
996c8c3b |
735 | static void init_winsock(void) |
07d9aa13 |
736 | { |
c51a56e2 |
737 | WORD winsock_ver; |
738 | WSADATA wsadata; |
739 | |
740 | winsock_ver = MAKEWORD(1, 1); |
741 | if (WSAStartup(winsock_ver, &wsadata)) |
742 | bump("Unable to initialise WinSock"); |
743 | if (LOBYTE(wsadata.wVersion) != 1 || |
744 | HIBYTE(wsadata.wVersion) != 1) |
745 | bump("WinSock version is incompatible with 1.1"); |
07d9aa13 |
746 | } |
747 | |
07d9aa13 |
748 | /* |
749 | * Short description of parameters. |
750 | */ |
996c8c3b |
751 | static void usage(void) |
07d9aa13 |
752 | { |
c51a56e2 |
753 | printf("PuTTY Secure Copy client\n"); |
754 | printf("%s\n", ver); |
b8a19193 |
755 | printf("Usage: scp [options] [user@]host:source target\n"); |
756 | printf(" scp [options] source [source...] [user@]host:target\n"); |
757 | printf("Options:\n"); |
758 | printf(" -p preserve file attributes\n"); |
759 | printf(" -q quiet, don't show statistics\n"); |
760 | printf(" -r copy directories recursively\n"); |
761 | printf(" -v show verbose messages\n"); |
762 | printf(" -P port connect to specified port\n"); |
763 | printf(" -pw passw login with specified password\n"); |
c51a56e2 |
764 | exit(1); |
07d9aa13 |
765 | } |
766 | |
07d9aa13 |
767 | /* |
768 | * Main program (no, really?) |
769 | */ |
770 | int main(int argc, char *argv[]) |
771 | { |
c51a56e2 |
772 | int i; |
773 | |
774 | init_winsock(); |
775 | |
776 | for (i = 1; i < argc; i++) { |
777 | if (argv[i][0] != '-') |
778 | break; |
779 | if (strcmp(argv[i], "-v") == 0) |
780 | verbose = 1; |
781 | else if (strcmp(argv[i], "-r") == 0) |
782 | recursive = 1; |
783 | else if (strcmp(argv[i], "-p") == 0) |
784 | preserve = 1; |
785 | else if (strcmp(argv[i], "-q") == 0) |
786 | statistics = 0; |
787 | else if (strcmp(argv[i], "-h") == 0 || |
788 | strcmp(argv[i], "-?") == 0) |
789 | usage(); |
b8a19193 |
790 | else if (strcmp(argv[i], "-P") == 0 && i+1 < argc) |
791 | portnumber = atoi(argv[++i]); |
792 | else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc) |
793 | password = argv[++i]; |
c51a56e2 |
794 | else if (strcmp(argv[i], "--") == 0) |
795 | { i++; break; } |
07d9aa13 |
796 | else |
c51a56e2 |
797 | usage(); |
798 | } |
799 | argc -= i; |
800 | argv += i; |
801 | |
802 | if (argc < 2) |
803 | usage(); |
804 | if (argc > 2) |
805 | targetshouldbedirectory = 1; |
806 | |
807 | if (colon(argv[argc-1]) != NULL) |
808 | toremote(argc, argv); |
809 | else |
810 | tolocal(argc, argv); |
811 | |
812 | if (connection_open) { |
813 | char ch; |
814 | ssh_send_eof(); |
815 | ssh_recv(&ch, 1); |
816 | } |
817 | WSACleanup(); |
818 | random_save_seed(); |
07d9aa13 |
819 | |
c51a56e2 |
820 | return (errs == 0 ? 0 : 1); |
07d9aa13 |
821 | } |
822 | |
823 | /* end */ |