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