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); |
167 | sprintf(etastr, "%02d:%02d:%02d", |
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) { |
280 | if (recursive) |
281 | rsource(src); |
07d9aa13 |
282 | else |
c51a56e2 |
283 | run_err("%s: not a regular file", src); |
284 | return; |
285 | } |
286 | |
287 | if ((last = strrchr(src, '/')) == NULL) |
288 | last = src; |
289 | else |
290 | last++; |
291 | if (strrchr(last, '\\') != NULL) |
292 | last = strrchr(last, '\\') + 1; |
293 | if (last == src && strchr(src, ':') != NULL) |
294 | last = strchr(src, ':') + 1; |
295 | |
296 | f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL, |
297 | OPEN_EXISTING, 0, 0); |
298 | if (f == INVALID_HANDLE_VALUE) { |
299 | run_err("%s: Cannot open file"); |
300 | return; |
301 | } |
302 | |
303 | if (preserve) { |
304 | FILETIME actime, wrtime; |
305 | unsigned long mtime, atime; |
306 | GetFileTime(f, NULL, &actime, &wrtime); |
307 | TIME_WIN_TO_POSIX(actime, atime); |
308 | TIME_WIN_TO_POSIX(wrtime, mtime); |
309 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
07d9aa13 |
310 | ssh_send(buf, strlen(buf)); |
311 | if (response()) |
c51a56e2 |
312 | return; |
313 | } |
314 | |
315 | size = GetFileSize(f, NULL); |
316 | sprintf(buf, "C0644 %lu %s\n", size, last); |
317 | if (verbose) |
318 | fprintf(stderr, "Sending file modes: %s", buf); |
319 | ssh_send(buf, strlen(buf)); |
320 | if (response()) |
321 | return; |
322 | |
323 | if (statistics) { |
324 | stat_bytes = 0; |
325 | stat_starttime = time(NULL); |
326 | stat_lasttime = 0; |
327 | } |
328 | |
329 | for (i = 0; i < size; i += 4096) { |
330 | char transbuf[4096]; |
331 | DWORD j, k = 4096; |
332 | if (i + k > size) k = size - i; |
333 | if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) { |
334 | if (statistics) printf("\n"); |
335 | bump("%s: Read error", src); |
07d9aa13 |
336 | } |
c51a56e2 |
337 | ssh_send(transbuf, k); |
338 | if (statistics) { |
339 | stat_bytes += k; |
340 | if (time(NULL) != stat_lasttime || |
341 | i + k == size) { |
342 | stat_lasttime = time(NULL); |
343 | print_stats(last, size, stat_bytes, |
344 | stat_starttime, stat_lasttime); |
345 | } |
07d9aa13 |
346 | } |
c51a56e2 |
347 | } |
348 | CloseHandle(f); |
07d9aa13 |
349 | |
c51a56e2 |
350 | ssh_send("", 1); |
351 | (void) response(); |
07d9aa13 |
352 | } |
353 | |
07d9aa13 |
354 | /* |
355 | * Recursively send the contents of a directory. |
356 | */ |
357 | static void rsource(char *src) |
358 | { |
c51a56e2 |
359 | char buf[2048]; |
360 | char *last; |
361 | HANDLE dir; |
362 | WIN32_FIND_DATA fdat; |
363 | int ok; |
364 | |
365 | if ((last = strrchr(src, '/')) == NULL) |
366 | last = src; |
367 | else |
368 | last++; |
369 | if (strrchr(last, '\\') != NULL) |
370 | last = strrchr(last, '\\') + 1; |
371 | if (last == src && strchr(src, ':') != NULL) |
372 | last = strchr(src, ':') + 1; |
373 | |
374 | /* maybe send filetime */ |
375 | |
376 | sprintf(buf, "D0755 0 %s\n", last); |
377 | if (verbose) |
378 | fprintf(stderr, "Entering directory: %s", buf); |
379 | ssh_send(buf, strlen(buf)); |
380 | if (response()) |
381 | return; |
382 | |
383 | sprintf(buf, "%s/*", src); |
384 | dir = FindFirstFile(buf, &fdat); |
385 | ok = (dir != INVALID_HANDLE_VALUE); |
386 | while (ok) { |
387 | if (strcmp(fdat.cFileName, ".") == 0 || |
388 | strcmp(fdat.cFileName, "..") == 0) { |
389 | } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= |
390 | sizeof(buf)) { |
391 | run_err("%s/%s: Name too long", src, fdat.cFileName); |
392 | } else { |
393 | sprintf(buf, "%s/%s", src, fdat.cFileName); |
394 | source(buf); |
07d9aa13 |
395 | } |
c51a56e2 |
396 | ok = FindNextFile(dir, &fdat); |
397 | } |
398 | FindClose(dir); |
07d9aa13 |
399 | |
c51a56e2 |
400 | sprintf(buf, "E\n"); |
401 | ssh_send(buf, strlen(buf)); |
402 | (void) response(); |
07d9aa13 |
403 | } |
404 | |
07d9aa13 |
405 | /* |
406 | * Execute the sink part of the SCP protocol. |
407 | */ |
408 | static void sink(char *targ) |
409 | { |
c51a56e2 |
410 | char buf[2048]; |
411 | char namebuf[2048]; |
412 | char ch; |
413 | int targisdir = 0; |
414 | int settime = 0; |
415 | int exists; |
416 | DWORD attr; |
417 | HANDLE f; |
418 | unsigned long mtime, atime; |
419 | unsigned int mode; |
420 | unsigned long size, i; |
421 | int wrerror = 0; |
422 | unsigned long stat_bytes; |
423 | time_t stat_starttime, stat_lasttime; |
424 | char *stat_name; |
425 | |
426 | attr = GetFileAttributes(targ); |
427 | if (attr != -1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
428 | targisdir = 1; |
429 | |
430 | if (targetshouldbedirectory && !targisdir) |
431 | bump("%s: Not a directory", targ); |
432 | |
433 | ssh_send("", 1); |
434 | while (1) { |
435 | settime = 0; |
436 | gottime: |
437 | if (ssh_recv(&ch, 1) <= 0) |
438 | return; |
439 | if (ch == '\n') |
440 | bump("Protocol error: Unexpected newline"); |
441 | i = 0; |
442 | buf[i++] = ch; |
443 | do { |
444 | if (ssh_recv(&ch, 1) <= 0) |
445 | bump("Lost connection"); |
446 | buf[i++] = ch; |
447 | } while (i < sizeof(buf) && ch != '\n'); |
448 | buf[i-1] = '\0'; |
449 | switch (buf[0]) { |
450 | case '\01': /* error */ |
451 | fprintf(stderr, "%s\n", buf+1); |
452 | errs++; |
453 | continue; |
454 | case '\02': /* fatal error */ |
455 | bump("%s", buf+1); |
456 | case 'E': |
457 | ssh_send("", 1); |
458 | return; |
459 | case 'T': |
460 | if (sscanf(buf, "T%d %*d %d %*d", |
461 | &mtime, &atime) == 2) { |
462 | settime = 1; |
463 | ssh_send("", 1); |
464 | goto gottime; |
465 | } |
466 | bump("Protocol error: Illegal time format"); |
467 | case 'C': |
468 | case 'D': |
469 | break; |
470 | default: |
471 | bump("Protocol error: Expected control record"); |
472 | } |
07d9aa13 |
473 | |
c51a56e2 |
474 | if (sscanf(buf+1, "%u %u %[^\n]", &mode, &size, namebuf) != 3) |
475 | bump("Protocol error: Illegal file descriptor format"); |
476 | if (targisdir) { |
477 | char t[2048]; |
478 | strcpy(t, targ); |
479 | if (targ[0] != '\0') |
480 | strcat(t, "/"); |
481 | strcat(t, namebuf); |
482 | strcpy(namebuf, t); |
483 | } else { |
484 | strcpy(namebuf, targ); |
485 | } |
486 | attr = GetFileAttributes(namebuf); |
487 | exists = (attr != -1); |
488 | |
489 | if (buf[0] == 'D') { |
490 | if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
491 | run_err("%s: Not a directory", namebuf); |
492 | continue; |
493 | } |
494 | if (!exists) { |
495 | if (! CreateDirectory(namebuf, NULL)) { |
496 | run_err("%s: Cannot create directory", |
497 | namebuf); |
498 | continue; |
499 | } |
500 | } |
501 | sink(namebuf); |
502 | /* can we set the timestamp for directories ? */ |
503 | continue; |
504 | } |
07d9aa13 |
505 | |
c51a56e2 |
506 | f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL, |
507 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
508 | if (f == INVALID_HANDLE_VALUE) { |
509 | run_err("%s: Cannot create file", namebuf); |
510 | continue; |
511 | } |
07d9aa13 |
512 | |
513 | ssh_send("", 1); |
07d9aa13 |
514 | |
c51a56e2 |
515 | if (statistics) { |
516 | stat_bytes = 0; |
517 | stat_starttime = time(NULL); |
518 | stat_lasttime = 0; |
519 | if ((stat_name = strrchr(namebuf, '/')) == NULL) |
520 | stat_name = namebuf; |
521 | else |
522 | stat_name++; |
523 | if (strrchr(stat_name, '\\') != NULL) |
524 | stat_name = strrchr(stat_name, '\\') + 1; |
525 | } |
07d9aa13 |
526 | |
c51a56e2 |
527 | for (i = 0; i < size; i += 4096) { |
528 | char transbuf[4096]; |
529 | int j, k = 4096; |
530 | if (i + k > size) k = size - i; |
531 | if (ssh_recv(transbuf, k) == 0) |
532 | bump("Lost connection"); |
533 | if (wrerror) continue; |
534 | if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) { |
535 | wrerror = 1; |
536 | if (statistics) |
537 | printf("\r%-25.25s | %50s\n", |
538 | stat_name, |
539 | "Write error.. waiting for end of file"); |
540 | continue; |
541 | } |
542 | if (statistics) { |
543 | stat_bytes += k; |
544 | if (time(NULL) > stat_lasttime || |
545 | i + k == size) { |
546 | stat_lasttime = time(NULL); |
547 | print_stats(stat_name, size, stat_bytes, |
548 | stat_starttime, stat_lasttime); |
07d9aa13 |
549 | } |
c51a56e2 |
550 | } |
551 | } |
552 | (void) response(); |
07d9aa13 |
553 | |
c51a56e2 |
554 | if (settime) { |
555 | FILETIME actime, wrtime; |
556 | TIME_POSIX_TO_WIN(atime, actime); |
557 | TIME_POSIX_TO_WIN(mtime, wrtime); |
558 | SetFileTime(f, NULL, &actime, &wrtime); |
07d9aa13 |
559 | } |
07d9aa13 |
560 | |
c51a56e2 |
561 | CloseHandle(f); |
562 | if (wrerror) { |
563 | run_err("%s: Write error", namebuf); |
564 | continue; |
565 | } |
566 | ssh_send("", 1); |
567 | } |
568 | } |
07d9aa13 |
569 | |
570 | /* |
571 | * We will copy local files to a remote server. |
572 | */ |
573 | static void toremote(int argc, char *argv[]) |
574 | { |
c51a56e2 |
575 | char *src, *targ, *host, *user; |
576 | char *cmd; |
577 | int i; |
578 | |
579 | targ = argv[argc-1]; |
580 | |
581 | /* Seperate host from filename */ |
582 | host = targ; |
583 | targ = colon(targ); |
584 | if (targ == NULL) |
585 | bump("targ == NULL in toremote()"); |
586 | *targ++ = '\0'; |
587 | if (*targ == '\0') |
588 | targ = "."; |
589 | /* Substitute "." for emtpy target */ |
590 | |
591 | /* Seperate host and username */ |
592 | user = host; |
593 | host = strrchr(host, '@'); |
594 | if (host == NULL) { |
595 | host = user; |
596 | user = NULL; |
597 | } else { |
598 | *host++ = '\0'; |
599 | if (*user == '\0') |
600 | user = NULL; |
601 | } |
602 | |
603 | if (argc == 2) { |
604 | /* Find out if the source filespec covers multiple files |
605 | if so, we should set the targetshouldbedirectory flag */ |
606 | HANDLE fh; |
607 | WIN32_FIND_DATA fdat; |
608 | if (colon(argv[0]) != NULL) |
609 | bump("%s: Remote to remote not supported", argv[0]); |
610 | fh = FindFirstFile(argv[0], &fdat); |
611 | if (fh == INVALID_HANDLE_VALUE) |
612 | bump("%s: No such file or directory\n", argv[0]); |
613 | if (FindNextFile(fh, &fdat)) |
614 | targetshouldbedirectory = 1; |
615 | FindClose(fh); |
616 | } |
617 | |
618 | cmd = smalloc(strlen(targ) + 100); |
619 | sprintf(cmd, "scp%s%s%s%s -t %s", |
620 | verbose ? " -v" : "", |
621 | recursive ? " -r" : "", |
622 | preserve ? " -p" : "", |
623 | targetshouldbedirectory ? " -d" : "", |
624 | targ); |
625 | do_cmd(host, user, cmd); |
626 | sfree(cmd); |
627 | |
628 | (void) response(); |
629 | |
630 | for (i = 0; i < argc - 1; i++) { |
631 | HANDLE dir; |
632 | WIN32_FIND_DATA fdat; |
633 | src = argv[i]; |
634 | if (colon(src) != NULL) { |
635 | fprintf(stderr, |
636 | "%s: Remote to remote not supported\n", src); |
637 | errs++; |
638 | continue; |
07d9aa13 |
639 | } |
c51a56e2 |
640 | dir = FindFirstFile(src, &fdat); |
641 | if (dir == INVALID_HANDLE_VALUE) { |
642 | run_err("%s: No such file or directory", src); |
643 | continue; |
07d9aa13 |
644 | } |
c51a56e2 |
645 | do { |
646 | char *last; |
647 | char namebuf[2048]; |
648 | if (strlen(src) + strlen(fdat.cFileName) >= |
649 | sizeof(namebuf)) { |
650 | fprintf(stderr, "%s: Name too long", src); |
651 | continue; |
652 | } |
653 | strcpy(namebuf, src); |
654 | if ((last = strrchr(namebuf, '/')) == NULL) |
655 | last = namebuf; |
656 | else |
657 | last++; |
658 | if (strrchr(last, '\\') != NULL) |
659 | last = strrchr(last, '\\') + 1; |
660 | if (last == namebuf && strrchr(namebuf, ':') != NULL) |
661 | last = strchr(namebuf, ':') + 1; |
662 | strcpy(last, fdat.cFileName); |
663 | source(namebuf); |
664 | } while (FindNextFile(dir, &fdat)); |
665 | FindClose(dir); |
666 | } |
07d9aa13 |
667 | } |
668 | |
07d9aa13 |
669 | /* |
670 | * We will copy files from a remote server to the local machine. |
671 | */ |
672 | static void tolocal(int argc, char *argv[]) |
673 | { |
c51a56e2 |
674 | char *src, *targ, *host, *user; |
675 | char *cmd; |
676 | |
677 | if (argc != 2) |
678 | bump("More than one remote source not supported"); |
679 | |
680 | src = argv[0]; |
681 | targ = argv[1]; |
682 | |
683 | /* Seperate host from filename */ |
684 | host = src; |
685 | src = colon(src); |
686 | if (src == NULL) |
687 | bump("Local to local copy not supported"); |
688 | *src++ = '\0'; |
689 | if (*src == '\0') |
690 | src = "."; |
691 | /* Substitute "." for empty filename */ |
692 | |
693 | /* Seperate username and hostname */ |
694 | user = host; |
695 | host = strrchr(host, '@'); |
696 | if (host == NULL) { |
697 | host = user; |
698 | user = NULL; |
699 | } else { |
700 | *host++ = '\0'; |
701 | if (*user == '\0') |
702 | user = NULL; |
703 | } |
704 | |
705 | cmd = smalloc(strlen(src) + 100); |
706 | sprintf(cmd, "scp%s%s%s%s -f %s", |
707 | verbose ? " -v" : "", |
708 | recursive ? " -r" : "", |
709 | preserve ? " -p" : "", |
710 | targetshouldbedirectory ? " -d" : "", |
711 | src); |
712 | do_cmd(host, user, cmd); |
713 | sfree(cmd); |
714 | |
715 | sink(targ); |
07d9aa13 |
716 | } |
717 | |
07d9aa13 |
718 | /* |
719 | * Initialize the Win$ock driver. |
720 | */ |
721 | static void init_winsock() |
722 | { |
c51a56e2 |
723 | WORD winsock_ver; |
724 | WSADATA wsadata; |
725 | |
726 | winsock_ver = MAKEWORD(1, 1); |
727 | if (WSAStartup(winsock_ver, &wsadata)) |
728 | bump("Unable to initialise WinSock"); |
729 | if (LOBYTE(wsadata.wVersion) != 1 || |
730 | HIBYTE(wsadata.wVersion) != 1) |
731 | bump("WinSock version is incompatible with 1.1"); |
07d9aa13 |
732 | } |
733 | |
07d9aa13 |
734 | /* |
735 | * Short description of parameters. |
736 | */ |
737 | static void usage() |
738 | { |
c51a56e2 |
739 | printf("PuTTY Secure Copy client\n"); |
740 | printf("%s\n", ver); |
b8a19193 |
741 | printf("Usage: scp [options] [user@]host:source target\n"); |
742 | printf(" scp [options] source [source...] [user@]host:target\n"); |
743 | printf("Options:\n"); |
744 | printf(" -p preserve file attributes\n"); |
745 | printf(" -q quiet, don't show statistics\n"); |
746 | printf(" -r copy directories recursively\n"); |
747 | printf(" -v show verbose messages\n"); |
748 | printf(" -P port connect to specified port\n"); |
749 | printf(" -pw passw login with specified password\n"); |
c51a56e2 |
750 | exit(1); |
07d9aa13 |
751 | } |
752 | |
07d9aa13 |
753 | /* |
754 | * Main program (no, really?) |
755 | */ |
756 | int main(int argc, char *argv[]) |
757 | { |
c51a56e2 |
758 | int i; |
759 | |
760 | init_winsock(); |
761 | |
762 | for (i = 1; i < argc; i++) { |
763 | if (argv[i][0] != '-') |
764 | break; |
765 | if (strcmp(argv[i], "-v") == 0) |
766 | verbose = 1; |
767 | else if (strcmp(argv[i], "-r") == 0) |
768 | recursive = 1; |
769 | else if (strcmp(argv[i], "-p") == 0) |
770 | preserve = 1; |
771 | else if (strcmp(argv[i], "-q") == 0) |
772 | statistics = 0; |
773 | else if (strcmp(argv[i], "-h") == 0 || |
774 | strcmp(argv[i], "-?") == 0) |
775 | usage(); |
b8a19193 |
776 | else if (strcmp(argv[i], "-P") == 0 && i+1 < argc) |
777 | portnumber = atoi(argv[++i]); |
778 | else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc) |
779 | password = argv[++i]; |
c51a56e2 |
780 | else if (strcmp(argv[i], "--") == 0) |
781 | { i++; break; } |
07d9aa13 |
782 | else |
c51a56e2 |
783 | usage(); |
784 | } |
785 | argc -= i; |
786 | argv += i; |
787 | |
788 | if (argc < 2) |
789 | usage(); |
790 | if (argc > 2) |
791 | targetshouldbedirectory = 1; |
792 | |
793 | if (colon(argv[argc-1]) != NULL) |
794 | toremote(argc, argv); |
795 | else |
796 | tolocal(argc, argv); |
797 | |
798 | if (connection_open) { |
799 | char ch; |
800 | ssh_send_eof(); |
801 | ssh_recv(&ch, 1); |
802 | } |
803 | WSACleanup(); |
804 | random_save_seed(); |
07d9aa13 |
805 | |
c51a56e2 |
806 | return (errs == 0 ? 0 : 1); |
07d9aa13 |
807 | } |
808 | |
809 | /* end */ |