4c7f0d61 |
1 | /* |
2 | * psftp.c: front end for PSFTP. |
3 | */ |
4 | |
4a8fc3c4 |
5 | #include <windows.h> |
6 | |
4c7f0d61 |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
f9e162aa |
9 | #include <stdarg.h> |
4c7f0d61 |
10 | #include <assert.h> |
d92624dc |
11 | #include <limits.h> |
4c7f0d61 |
12 | |
4a8fc3c4 |
13 | #define PUTTY_DO_GLOBALS |
14 | #include "putty.h" |
15 | #include "storage.h" |
16 | #include "ssh.h" |
4c7f0d61 |
17 | #include "sftp.h" |
18 | #include "int64.h" |
19 | |
5471d09a |
20 | /* |
21 | * Since SFTP is a request-response oriented protocol, it requires |
22 | * no buffer management: when we send data, we stop and wait for an |
23 | * acknowledgement _anyway_, and so we can't possibly overfill our |
24 | * send buffer. |
25 | */ |
26 | |
fa3db767 |
27 | static int psftp_connect(char *userhost, char *user, int portnumber); |
07534184 |
28 | static int do_sftp_init(void); |
fa3db767 |
29 | |
4c7f0d61 |
30 | /* ---------------------------------------------------------------------- |
4c7f0d61 |
31 | * sftp client state. |
32 | */ |
33 | |
34 | char *pwd, *homedir; |
6b78788a |
35 | static Backend *back; |
36 | static void *backhandle; |
3ea863a3 |
37 | static Config cfg; |
4c7f0d61 |
38 | |
39 | /* ---------------------------------------------------------------------- |
40 | * Higher-level helper functions used in commands. |
41 | */ |
42 | |
43 | /* |
f9e162aa |
44 | * Attempt to canonify a pathname starting from the pwd. If |
45 | * canonification fails, at least fall back to returning a _valid_ |
46 | * pathname (though it may be ugly, eg /home/simon/../foobar). |
4c7f0d61 |
47 | */ |
32874aea |
48 | char *canonify(char *name) |
49 | { |
f9e162aa |
50 | char *fullname, *canonname; |
1bc24185 |
51 | struct sftp_packet *pktin; |
52 | struct sftp_request *req, *rreq; |
4a8fc3c4 |
53 | |
f9e162aa |
54 | if (name[0] == '/') { |
55 | fullname = dupstr(name); |
56 | } else { |
4a8fc3c4 |
57 | char *slash; |
32874aea |
58 | if (pwd[strlen(pwd) - 1] == '/') |
4a8fc3c4 |
59 | slash = ""; |
60 | else |
61 | slash = "/"; |
62 | fullname = dupcat(pwd, slash, name, NULL); |
f9e162aa |
63 | } |
4a8fc3c4 |
64 | |
1bc24185 |
65 | sftp_register(req = fxp_realpath_send(fullname)); |
66 | rreq = sftp_find_request(pktin = sftp_recv()); |
67 | assert(rreq == req); |
7b7de4f4 |
68 | canonname = fxp_realpath_recv(pktin, rreq); |
4a8fc3c4 |
69 | |
f9e162aa |
70 | if (canonname) { |
71 | sfree(fullname); |
72 | return canonname; |
50d7e054 |
73 | } else { |
32874aea |
74 | /* |
75 | * Attempt number 2. Some FXP_REALPATH implementations |
76 | * (glibc-based ones, in particular) require the _whole_ |
77 | * path to point to something that exists, whereas others |
78 | * (BSD-based) only require all but the last component to |
79 | * exist. So if the first call failed, we should strip off |
80 | * everything from the last slash onwards and try again, |
81 | * then put the final component back on. |
82 | * |
83 | * Special cases: |
84 | * |
85 | * - if the last component is "/." or "/..", then we don't |
86 | * bother trying this because there's no way it can work. |
87 | * |
88 | * - if the thing actually ends with a "/", we remove it |
89 | * before we start. Except if the string is "/" itself |
90 | * (although I can't see why we'd have got here if so, |
91 | * because surely "/" would have worked the first |
92 | * time?), in which case we don't bother. |
93 | * |
94 | * - if there's no slash in the string at all, give up in |
95 | * confusion (we expect at least one because of the way |
96 | * we constructed the string). |
97 | */ |
98 | |
99 | int i; |
100 | char *returnname; |
101 | |
102 | i = strlen(fullname); |
103 | if (i > 2 && fullname[i - 1] == '/') |
104 | fullname[--i] = '\0'; /* strip trailing / unless at pos 0 */ |
105 | while (i > 0 && fullname[--i] != '/'); |
106 | |
107 | /* |
108 | * Give up on special cases. |
109 | */ |
110 | if (fullname[i] != '/' || /* no slash at all */ |
111 | !strcmp(fullname + i, "/.") || /* ends in /. */ |
112 | !strcmp(fullname + i, "/..") || /* ends in /.. */ |
113 | !strcmp(fullname, "/")) { |
114 | return fullname; |
115 | } |
116 | |
117 | /* |
118 | * Now i points at the slash. Deal with the final special |
119 | * case i==0 (ie the whole path was "/nonexistentfile"). |
120 | */ |
121 | fullname[i] = '\0'; /* separate the string */ |
122 | if (i == 0) { |
1bc24185 |
123 | sftp_register(req = fxp_realpath_send("/")); |
32874aea |
124 | } else { |
1bc24185 |
125 | sftp_register(req = fxp_realpath_send(fullname)); |
32874aea |
126 | } |
1bc24185 |
127 | rreq = sftp_find_request(pktin = sftp_recv()); |
128 | assert(rreq == req); |
7b7de4f4 |
129 | canonname = fxp_realpath_recv(pktin, rreq); |
32874aea |
130 | |
131 | if (!canonname) |
132 | return fullname; /* even that failed; give up */ |
133 | |
134 | /* |
135 | * We have a canonical name for all but the last path |
136 | * component. Concatenate the last component and return. |
137 | */ |
138 | returnname = dupcat(canonname, |
139 | canonname[strlen(canonname) - 1] == |
140 | '/' ? "" : "/", fullname + i + 1, NULL); |
141 | sfree(fullname); |
142 | sfree(canonname); |
143 | return returnname; |
50d7e054 |
144 | } |
4c7f0d61 |
145 | } |
146 | |
dcf8495c |
147 | /* |
148 | * Return a pointer to the portion of str that comes after the last |
149 | * slash (or backslash or colon, if `local' is TRUE). |
150 | */ |
151 | static char *stripslashes(char *str, int local) |
152 | { |
153 | char *p; |
154 | |
155 | if (local) { |
156 | p = strchr(str, ':'); |
157 | if (p) str = p+1; |
158 | } |
159 | |
160 | p = strrchr(str, '/'); |
161 | if (p) str = p+1; |
162 | |
163 | if (local) { |
164 | p = strrchr(str, '\\'); |
165 | if (p) str = p+1; |
166 | } |
167 | |
168 | return str; |
169 | } |
170 | |
4c7f0d61 |
171 | /* ---------------------------------------------------------------------- |
172 | * Actual sftp commands. |
173 | */ |
174 | struct sftp_command { |
175 | char **words; |
176 | int nwords, wordssize; |
32874aea |
177 | int (*obey) (struct sftp_command *); /* returns <0 to quit */ |
4c7f0d61 |
178 | }; |
179 | |
32874aea |
180 | int sftp_cmd_null(struct sftp_command *cmd) |
181 | { |
df49ff19 |
182 | return 1; /* success */ |
4c7f0d61 |
183 | } |
184 | |
32874aea |
185 | int sftp_cmd_unknown(struct sftp_command *cmd) |
186 | { |
4c7f0d61 |
187 | printf("psftp: unknown command \"%s\"\n", cmd->words[0]); |
df49ff19 |
188 | return 0; /* failure */ |
4c7f0d61 |
189 | } |
190 | |
32874aea |
191 | int sftp_cmd_quit(struct sftp_command *cmd) |
192 | { |
4c7f0d61 |
193 | return -1; |
194 | } |
195 | |
196 | /* |
197 | * List a directory. If no arguments are given, list pwd; otherwise |
198 | * list the directory given in words[1]. |
199 | */ |
32874aea |
200 | static int sftp_ls_compare(const void *av, const void *bv) |
201 | { |
7d2c1789 |
202 | const struct fxp_name *const *a = (const struct fxp_name *const *) av; |
203 | const struct fxp_name *const *b = (const struct fxp_name *const *) bv; |
204 | return strcmp((*a)->filename, (*b)->filename); |
4c7f0d61 |
205 | } |
32874aea |
206 | int sftp_cmd_ls(struct sftp_command *cmd) |
207 | { |
4c7f0d61 |
208 | struct fxp_handle *dirh; |
209 | struct fxp_names *names; |
7d2c1789 |
210 | struct fxp_name **ournames; |
4c7f0d61 |
211 | int nnames, namesize; |
212 | char *dir, *cdir; |
1bc24185 |
213 | struct sftp_packet *pktin; |
214 | struct sftp_request *req, *rreq; |
4c7f0d61 |
215 | int i; |
216 | |
fa3db767 |
217 | if (back == NULL) { |
218 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
219 | return 0; |
220 | } |
221 | |
4c7f0d61 |
222 | if (cmd->nwords < 2) |
223 | dir = "."; |
224 | else |
225 | dir = cmd->words[1]; |
226 | |
227 | cdir = canonify(dir); |
228 | if (!cdir) { |
229 | printf("%s: %s\n", dir, fxp_error()); |
230 | return 0; |
231 | } |
232 | |
233 | printf("Listing directory %s\n", cdir); |
234 | |
1bc24185 |
235 | sftp_register(req = fxp_opendir_send(cdir)); |
236 | rreq = sftp_find_request(pktin = sftp_recv()); |
237 | assert(rreq == req); |
7b7de4f4 |
238 | dirh = fxp_opendir_recv(pktin, rreq); |
1bc24185 |
239 | |
4c7f0d61 |
240 | if (dirh == NULL) { |
241 | printf("Unable to open %s: %s\n", dir, fxp_error()); |
242 | } else { |
243 | nnames = namesize = 0; |
244 | ournames = NULL; |
245 | |
246 | while (1) { |
247 | |
1bc24185 |
248 | sftp_register(req = fxp_readdir_send(dirh)); |
249 | rreq = sftp_find_request(pktin = sftp_recv()); |
250 | assert(rreq == req); |
7b7de4f4 |
251 | names = fxp_readdir_recv(pktin, rreq); |
1bc24185 |
252 | |
4c7f0d61 |
253 | if (names == NULL) { |
254 | if (fxp_error_type() == SSH_FX_EOF) |
255 | break; |
256 | printf("Reading directory %s: %s\n", dir, fxp_error()); |
257 | break; |
258 | } |
259 | if (names->nnames == 0) { |
260 | fxp_free_names(names); |
261 | break; |
262 | } |
263 | |
264 | if (nnames + names->nnames >= namesize) { |
265 | namesize += names->nnames + 128; |
3d88e64d |
266 | ournames = sresize(ournames, namesize, struct fxp_name *); |
4c7f0d61 |
267 | } |
268 | |
269 | for (i = 0; i < names->nnames; i++) |
7d2c1789 |
270 | ournames[nnames++] = fxp_dup_name(&names->names[i]); |
4c7f0d61 |
271 | |
4c7f0d61 |
272 | fxp_free_names(names); |
273 | } |
1bc24185 |
274 | sftp_register(req = fxp_close_send(dirh)); |
275 | rreq = sftp_find_request(pktin = sftp_recv()); |
276 | assert(rreq == req); |
7b7de4f4 |
277 | fxp_close_recv(pktin, rreq); |
4c7f0d61 |
278 | |
279 | /* |
280 | * Now we have our filenames. Sort them by actual file |
281 | * name, and then output the longname parts. |
282 | */ |
283 | qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare); |
284 | |
285 | /* |
286 | * And print them. |
287 | */ |
7d2c1789 |
288 | for (i = 0; i < nnames; i++) { |
289 | printf("%s\n", ournames[i]->longname); |
290 | fxp_free_name(ournames[i]); |
291 | } |
292 | sfree(ournames); |
4c7f0d61 |
293 | } |
294 | |
295 | sfree(cdir); |
296 | |
df49ff19 |
297 | return 1; |
4c7f0d61 |
298 | } |
299 | |
300 | /* |
301 | * Change directories. We do this by canonifying the new name, then |
302 | * trying to OPENDIR it. Only if that succeeds do we set the new pwd. |
303 | */ |
32874aea |
304 | int sftp_cmd_cd(struct sftp_command *cmd) |
305 | { |
4c7f0d61 |
306 | struct fxp_handle *dirh; |
1bc24185 |
307 | struct sftp_packet *pktin; |
308 | struct sftp_request *req, *rreq; |
4c7f0d61 |
309 | char *dir; |
310 | |
fa3db767 |
311 | if (back == NULL) { |
312 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
313 | return 0; |
314 | } |
315 | |
4c7f0d61 |
316 | if (cmd->nwords < 2) |
f9e162aa |
317 | dir = dupstr(homedir); |
4c7f0d61 |
318 | else |
319 | dir = canonify(cmd->words[1]); |
320 | |
321 | if (!dir) { |
322 | printf("%s: %s\n", dir, fxp_error()); |
323 | return 0; |
324 | } |
325 | |
1bc24185 |
326 | sftp_register(req = fxp_opendir_send(dir)); |
327 | rreq = sftp_find_request(pktin = sftp_recv()); |
328 | assert(rreq == req); |
7b7de4f4 |
329 | dirh = fxp_opendir_recv(pktin, rreq); |
1bc24185 |
330 | |
4c7f0d61 |
331 | if (!dirh) { |
332 | printf("Directory %s: %s\n", dir, fxp_error()); |
333 | sfree(dir); |
334 | return 0; |
335 | } |
336 | |
1bc24185 |
337 | sftp_register(req = fxp_close_send(dirh)); |
338 | rreq = sftp_find_request(pktin = sftp_recv()); |
339 | assert(rreq == req); |
7b7de4f4 |
340 | fxp_close_recv(pktin, rreq); |
4c7f0d61 |
341 | |
342 | sfree(pwd); |
343 | pwd = dir; |
344 | printf("Remote directory is now %s\n", pwd); |
345 | |
df49ff19 |
346 | return 1; |
4c7f0d61 |
347 | } |
348 | |
349 | /* |
4f2b387f |
350 | * Print current directory. Easy as pie. |
351 | */ |
352 | int sftp_cmd_pwd(struct sftp_command *cmd) |
353 | { |
fa3db767 |
354 | if (back == NULL) { |
355 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
356 | return 0; |
357 | } |
358 | |
4f2b387f |
359 | printf("Remote directory is %s\n", pwd); |
df49ff19 |
360 | return 1; |
4f2b387f |
361 | } |
362 | |
363 | /* |
d92624dc |
364 | * Get a file and save it at the local end. We have two very |
365 | * similar commands here: `get' and `reget', which differ in that |
366 | * `reget' checks for the existence of the destination file and |
367 | * starts from where a previous aborted transfer left off. |
4c7f0d61 |
368 | */ |
d92624dc |
369 | int sftp_general_get(struct sftp_command *cmd, int restart) |
32874aea |
370 | { |
4c7f0d61 |
371 | struct fxp_handle *fh; |
1bc24185 |
372 | struct sftp_packet *pktin; |
373 | struct sftp_request *req, *rreq; |
4c7f0d61 |
374 | char *fname, *outfname; |
375 | uint64 offset; |
376 | FILE *fp; |
df49ff19 |
377 | int ret; |
4c7f0d61 |
378 | |
fa3db767 |
379 | if (back == NULL) { |
380 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
381 | return 0; |
382 | } |
383 | |
4c7f0d61 |
384 | if (cmd->nwords < 2) { |
385 | printf("get: expects a filename\n"); |
386 | return 0; |
387 | } |
388 | |
389 | fname = canonify(cmd->words[1]); |
390 | if (!fname) { |
391 | printf("%s: %s\n", cmd->words[1], fxp_error()); |
392 | return 0; |
393 | } |
dcf8495c |
394 | outfname = (cmd->nwords == 2 ? |
395 | stripslashes(cmd->words[1], 0) : cmd->words[2]); |
4c7f0d61 |
396 | |
1bc24185 |
397 | sftp_register(req = fxp_open_send(fname, SSH_FXF_READ)); |
398 | rreq = sftp_find_request(pktin = sftp_recv()); |
399 | assert(rreq == req); |
7b7de4f4 |
400 | fh = fxp_open_recv(pktin, rreq); |
1bc24185 |
401 | |
4c7f0d61 |
402 | if (!fh) { |
403 | printf("%s: %s\n", fname, fxp_error()); |
404 | sfree(fname); |
405 | return 0; |
406 | } |
d92624dc |
407 | |
408 | if (restart) { |
409 | fp = fopen(outfname, "rb+"); |
410 | } else { |
411 | fp = fopen(outfname, "wb"); |
412 | } |
413 | |
4c7f0d61 |
414 | if (!fp) { |
415 | printf("local: unable to open %s\n", outfname); |
1bc24185 |
416 | |
417 | sftp_register(req = fxp_close_send(fh)); |
418 | rreq = sftp_find_request(pktin = sftp_recv()); |
419 | assert(rreq == req); |
7b7de4f4 |
420 | fxp_close_recv(pktin, rreq); |
1bc24185 |
421 | |
4c7f0d61 |
422 | sfree(fname); |
423 | return 0; |
424 | } |
425 | |
d92624dc |
426 | if (restart) { |
427 | long posn; |
428 | fseek(fp, 0L, SEEK_END); |
429 | posn = ftell(fp); |
430 | printf("reget: restarting at file position %ld\n", posn); |
431 | offset = uint64_make(0, posn); |
432 | } else { |
433 | offset = uint64_make(0, 0); |
434 | } |
4c7f0d61 |
435 | |
d92624dc |
436 | printf("remote:%s => local:%s\n", fname, outfname); |
4c7f0d61 |
437 | |
438 | /* |
439 | * FIXME: we can use FXP_FSTAT here to get the file size, and |
440 | * thus put up a progress bar. |
441 | */ |
df49ff19 |
442 | ret = 1; |
4c7f0d61 |
443 | while (1) { |
444 | char buffer[4096]; |
445 | int len; |
446 | int wpos, wlen; |
447 | |
1bc24185 |
448 | sftp_register(req = fxp_read_send(fh, offset, sizeof(buffer))); |
449 | rreq = sftp_find_request(pktin = sftp_recv()); |
450 | assert(rreq == req); |
7b7de4f4 |
451 | len = fxp_read_recv(pktin, rreq, buffer, sizeof(buffer)); |
1bc24185 |
452 | |
32874aea |
453 | if ((len == -1 && fxp_error_type() == SSH_FX_EOF) || len == 0) |
4c7f0d61 |
454 | break; |
455 | if (len == -1) { |
456 | printf("error while reading: %s\n", fxp_error()); |
df49ff19 |
457 | ret = 0; |
4c7f0d61 |
458 | break; |
459 | } |
32874aea |
460 | |
4c7f0d61 |
461 | wpos = 0; |
462 | while (wpos < len) { |
32874aea |
463 | wlen = fwrite(buffer, 1, len - wpos, fp); |
4c7f0d61 |
464 | if (wlen <= 0) { |
465 | printf("error while writing local file\n"); |
df49ff19 |
466 | ret = 0; |
4c7f0d61 |
467 | break; |
468 | } |
469 | wpos += wlen; |
470 | } |
df49ff19 |
471 | if (wpos < len) { /* we had an error */ |
472 | ret = 0; |
4c7f0d61 |
473 | break; |
df49ff19 |
474 | } |
4c7f0d61 |
475 | offset = uint64_add32(offset, len); |
476 | } |
477 | |
478 | fclose(fp); |
1bc24185 |
479 | |
480 | sftp_register(req = fxp_close_send(fh)); |
481 | rreq = sftp_find_request(pktin = sftp_recv()); |
482 | assert(rreq == req); |
7b7de4f4 |
483 | fxp_close_recv(pktin, rreq); |
1bc24185 |
484 | |
4c7f0d61 |
485 | sfree(fname); |
486 | |
df49ff19 |
487 | return ret; |
4c7f0d61 |
488 | } |
d92624dc |
489 | int sftp_cmd_get(struct sftp_command *cmd) |
490 | { |
491 | return sftp_general_get(cmd, 0); |
492 | } |
493 | int sftp_cmd_reget(struct sftp_command *cmd) |
494 | { |
495 | return sftp_general_get(cmd, 1); |
496 | } |
4c7f0d61 |
497 | |
498 | /* |
d92624dc |
499 | * Send a file and store it at the remote end. We have two very |
500 | * similar commands here: `put' and `reput', which differ in that |
501 | * `reput' checks for the existence of the destination file and |
502 | * starts from where a previous aborted transfer left off. |
4c7f0d61 |
503 | */ |
d92624dc |
504 | int sftp_general_put(struct sftp_command *cmd, int restart) |
32874aea |
505 | { |
4c7f0d61 |
506 | struct fxp_handle *fh; |
507 | char *fname, *origoutfname, *outfname; |
1bc24185 |
508 | struct sftp_packet *pktin; |
509 | struct sftp_request *req, *rreq; |
4c7f0d61 |
510 | uint64 offset; |
511 | FILE *fp; |
df49ff19 |
512 | int ret; |
4c7f0d61 |
513 | |
fa3db767 |
514 | if (back == NULL) { |
515 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
516 | return 0; |
517 | } |
518 | |
4c7f0d61 |
519 | if (cmd->nwords < 2) { |
520 | printf("put: expects a filename\n"); |
521 | return 0; |
522 | } |
523 | |
524 | fname = cmd->words[1]; |
dcf8495c |
525 | origoutfname = (cmd->nwords == 2 ? |
526 | stripslashes(cmd->words[1], 1) : cmd->words[2]); |
4c7f0d61 |
527 | outfname = canonify(origoutfname); |
f9e162aa |
528 | if (!outfname) { |
4c7f0d61 |
529 | printf("%s: %s\n", origoutfname, fxp_error()); |
530 | return 0; |
531 | } |
532 | |
533 | fp = fopen(fname, "rb"); |
534 | if (!fp) { |
535 | printf("local: unable to open %s\n", fname); |
4c7f0d61 |
536 | sfree(outfname); |
537 | return 0; |
538 | } |
d92624dc |
539 | if (restart) { |
1bc24185 |
540 | sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE)); |
d92624dc |
541 | } else { |
1bc24185 |
542 | sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE | |
543 | SSH_FXF_CREAT | SSH_FXF_TRUNC)); |
d92624dc |
544 | } |
1bc24185 |
545 | rreq = sftp_find_request(pktin = sftp_recv()); |
546 | assert(rreq == req); |
7b7de4f4 |
547 | fh = fxp_open_recv(pktin, rreq); |
1bc24185 |
548 | |
4c7f0d61 |
549 | if (!fh) { |
550 | printf("%s: %s\n", outfname, fxp_error()); |
551 | sfree(outfname); |
552 | return 0; |
553 | } |
554 | |
d92624dc |
555 | if (restart) { |
556 | char decbuf[30]; |
557 | struct fxp_attrs attrs; |
1bc24185 |
558 | int ret; |
559 | |
560 | sftp_register(req = fxp_fstat_send(fh)); |
561 | rreq = sftp_find_request(pktin = sftp_recv()); |
562 | assert(rreq == req); |
7b7de4f4 |
563 | ret = fxp_fstat_recv(pktin, rreq, &attrs); |
1bc24185 |
564 | |
565 | if (!ret) { |
d92624dc |
566 | printf("read size of %s: %s\n", outfname, fxp_error()); |
567 | sfree(outfname); |
568 | return 0; |
569 | } |
570 | if (!(attrs.flags & SSH_FILEXFER_ATTR_SIZE)) { |
571 | printf("read size of %s: size was not given\n", outfname); |
572 | sfree(outfname); |
573 | return 0; |
574 | } |
575 | offset = attrs.size; |
576 | uint64_decimal(offset, decbuf); |
577 | printf("reput: restarting at file position %s\n", decbuf); |
578 | if (uint64_compare(offset, uint64_make(0, LONG_MAX)) > 0) { |
579 | printf("reput: remote file is larger than we can deal with\n"); |
580 | sfree(outfname); |
581 | return 0; |
582 | } |
583 | if (fseek(fp, offset.lo, SEEK_SET) != 0) |
584 | fseek(fp, 0, SEEK_END); /* *shrug* */ |
585 | } else { |
586 | offset = uint64_make(0, 0); |
587 | } |
4c7f0d61 |
588 | |
d92624dc |
589 | printf("local:%s => remote:%s\n", fname, outfname); |
4c7f0d61 |
590 | |
591 | /* |
592 | * FIXME: we can use FXP_FSTAT here to get the file size, and |
593 | * thus put up a progress bar. |
594 | */ |
df49ff19 |
595 | ret = 1; |
4c7f0d61 |
596 | while (1) { |
597 | char buffer[4096]; |
1bc24185 |
598 | int len, ret; |
4c7f0d61 |
599 | |
f9e162aa |
600 | len = fread(buffer, 1, sizeof(buffer), fp); |
4c7f0d61 |
601 | if (len == -1) { |
602 | printf("error while reading local file\n"); |
df49ff19 |
603 | ret = 0; |
4c7f0d61 |
604 | break; |
605 | } else if (len == 0) { |
606 | break; |
607 | } |
1bc24185 |
608 | |
609 | sftp_register(req = fxp_write_send(fh, buffer, offset, len)); |
610 | rreq = sftp_find_request(pktin = sftp_recv()); |
611 | assert(rreq == req); |
7b7de4f4 |
612 | ret = fxp_write_recv(pktin, rreq); |
1bc24185 |
613 | |
614 | if (!ret) { |
4c7f0d61 |
615 | printf("error while writing: %s\n", fxp_error()); |
df49ff19 |
616 | ret = 0; |
4c7f0d61 |
617 | break; |
618 | } |
619 | offset = uint64_add32(offset, len); |
620 | } |
621 | |
1bc24185 |
622 | sftp_register(req = fxp_close_send(fh)); |
623 | rreq = sftp_find_request(pktin = sftp_recv()); |
624 | assert(rreq == req); |
7b7de4f4 |
625 | fxp_close_recv(pktin, rreq); |
1bc24185 |
626 | |
4c7f0d61 |
627 | fclose(fp); |
628 | sfree(outfname); |
629 | |
df49ff19 |
630 | return ret; |
4c7f0d61 |
631 | } |
d92624dc |
632 | int sftp_cmd_put(struct sftp_command *cmd) |
633 | { |
634 | return sftp_general_put(cmd, 0); |
635 | } |
636 | int sftp_cmd_reput(struct sftp_command *cmd) |
637 | { |
638 | return sftp_general_put(cmd, 1); |
639 | } |
4c7f0d61 |
640 | |
9954aaa3 |
641 | int sftp_cmd_mkdir(struct sftp_command *cmd) |
642 | { |
643 | char *dir; |
1bc24185 |
644 | struct sftp_packet *pktin; |
645 | struct sftp_request *req, *rreq; |
9954aaa3 |
646 | int result; |
647 | |
fa3db767 |
648 | if (back == NULL) { |
649 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
650 | return 0; |
651 | } |
9954aaa3 |
652 | |
653 | if (cmd->nwords < 2) { |
654 | printf("mkdir: expects a directory\n"); |
655 | return 0; |
656 | } |
657 | |
658 | dir = canonify(cmd->words[1]); |
659 | if (!dir) { |
660 | printf("%s: %s\n", dir, fxp_error()); |
661 | return 0; |
662 | } |
663 | |
1bc24185 |
664 | sftp_register(req = fxp_mkdir_send(dir)); |
665 | rreq = sftp_find_request(pktin = sftp_recv()); |
666 | assert(rreq == req); |
7b7de4f4 |
667 | result = fxp_mkdir_recv(pktin, rreq); |
1bc24185 |
668 | |
9954aaa3 |
669 | if (!result) { |
670 | printf("mkdir %s: %s\n", dir, fxp_error()); |
671 | sfree(dir); |
672 | return 0; |
673 | } |
674 | |
d92624dc |
675 | sfree(dir); |
df49ff19 |
676 | return 1; |
9954aaa3 |
677 | } |
678 | |
679 | int sftp_cmd_rmdir(struct sftp_command *cmd) |
680 | { |
681 | char *dir; |
1bc24185 |
682 | struct sftp_packet *pktin; |
683 | struct sftp_request *req, *rreq; |
9954aaa3 |
684 | int result; |
685 | |
fa3db767 |
686 | if (back == NULL) { |
687 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
688 | return 0; |
689 | } |
9954aaa3 |
690 | |
691 | if (cmd->nwords < 2) { |
692 | printf("rmdir: expects a directory\n"); |
693 | return 0; |
694 | } |
695 | |
696 | dir = canonify(cmd->words[1]); |
697 | if (!dir) { |
698 | printf("%s: %s\n", dir, fxp_error()); |
699 | return 0; |
700 | } |
701 | |
1bc24185 |
702 | sftp_register(req = fxp_rmdir_send(dir)); |
703 | rreq = sftp_find_request(pktin = sftp_recv()); |
704 | assert(rreq == req); |
7b7de4f4 |
705 | result = fxp_rmdir_recv(pktin, rreq); |
1bc24185 |
706 | |
9954aaa3 |
707 | if (!result) { |
708 | printf("rmdir %s: %s\n", dir, fxp_error()); |
709 | sfree(dir); |
710 | return 0; |
711 | } |
712 | |
d92624dc |
713 | sfree(dir); |
df49ff19 |
714 | return 1; |
9954aaa3 |
715 | } |
716 | |
717 | int sftp_cmd_rm(struct sftp_command *cmd) |
718 | { |
719 | char *fname; |
1bc24185 |
720 | struct sftp_packet *pktin; |
721 | struct sftp_request *req, *rreq; |
9954aaa3 |
722 | int result; |
723 | |
fa3db767 |
724 | if (back == NULL) { |
725 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
726 | return 0; |
727 | } |
728 | |
9954aaa3 |
729 | if (cmd->nwords < 2) { |
730 | printf("rm: expects a filename\n"); |
731 | return 0; |
732 | } |
733 | |
734 | fname = canonify(cmd->words[1]); |
735 | if (!fname) { |
736 | printf("%s: %s\n", fname, fxp_error()); |
737 | return 0; |
738 | } |
739 | |
1bc24185 |
740 | sftp_register(req = fxp_remove_send(fname)); |
741 | rreq = sftp_find_request(pktin = sftp_recv()); |
742 | assert(rreq == req); |
7b7de4f4 |
743 | result = fxp_remove_recv(pktin, rreq); |
1bc24185 |
744 | |
9954aaa3 |
745 | if (!result) { |
746 | printf("rm %s: %s\n", fname, fxp_error()); |
747 | sfree(fname); |
748 | return 0; |
749 | } |
750 | |
d92624dc |
751 | sfree(fname); |
df49ff19 |
752 | return 1; |
d92624dc |
753 | } |
754 | |
755 | int sftp_cmd_mv(struct sftp_command *cmd) |
756 | { |
757 | char *srcfname, *dstfname; |
1bc24185 |
758 | struct sftp_packet *pktin; |
759 | struct sftp_request *req, *rreq; |
d92624dc |
760 | int result; |
761 | |
fa3db767 |
762 | if (back == NULL) { |
763 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
764 | return 0; |
765 | } |
766 | |
d92624dc |
767 | if (cmd->nwords < 3) { |
768 | printf("mv: expects two filenames\n"); |
9954aaa3 |
769 | return 0; |
d92624dc |
770 | } |
771 | srcfname = canonify(cmd->words[1]); |
772 | if (!srcfname) { |
773 | printf("%s: %s\n", srcfname, fxp_error()); |
774 | return 0; |
775 | } |
776 | |
777 | dstfname = canonify(cmd->words[2]); |
778 | if (!dstfname) { |
779 | printf("%s: %s\n", dstfname, fxp_error()); |
780 | return 0; |
781 | } |
9954aaa3 |
782 | |
1bc24185 |
783 | sftp_register(req = fxp_rename_send(srcfname, dstfname)); |
784 | rreq = sftp_find_request(pktin = sftp_recv()); |
785 | assert(rreq == req); |
7b7de4f4 |
786 | result = fxp_rename_recv(pktin, rreq); |
1bc24185 |
787 | |
d92624dc |
788 | if (!result) { |
789 | char const *error = fxp_error(); |
790 | struct fxp_attrs attrs; |
791 | |
792 | /* |
793 | * The move might have failed because dstfname pointed at a |
794 | * directory. We check this possibility now: if dstfname |
795 | * _is_ a directory, we re-attempt the move by appending |
796 | * the basename of srcfname to dstfname. |
797 | */ |
1bc24185 |
798 | sftp_register(req = fxp_stat_send(dstfname)); |
799 | rreq = sftp_find_request(pktin = sftp_recv()); |
800 | assert(rreq == req); |
7b7de4f4 |
801 | result = fxp_stat_recv(pktin, rreq, &attrs); |
1bc24185 |
802 | |
d92624dc |
803 | if (result && |
804 | (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) && |
805 | (attrs.permissions & 0040000)) { |
806 | char *p; |
807 | char *newname, *newcanon; |
808 | printf("(destination %s is a directory)\n", dstfname); |
809 | p = srcfname + strlen(srcfname); |
810 | while (p > srcfname && p[-1] != '/') p--; |
811 | newname = dupcat(dstfname, "/", p, NULL); |
812 | newcanon = canonify(newname); |
813 | sfree(newname); |
814 | if (newcanon) { |
815 | sfree(dstfname); |
816 | dstfname = newcanon; |
1bc24185 |
817 | |
818 | sftp_register(req = fxp_rename_send(srcfname, dstfname)); |
819 | rreq = sftp_find_request(pktin = sftp_recv()); |
820 | assert(rreq == req); |
7b7de4f4 |
821 | result = fxp_rename_recv(pktin, rreq); |
1bc24185 |
822 | |
d92624dc |
823 | error = result ? NULL : fxp_error(); |
824 | } |
825 | } |
826 | if (error) { |
827 | printf("mv %s %s: %s\n", srcfname, dstfname, error); |
828 | sfree(srcfname); |
829 | sfree(dstfname); |
830 | return 0; |
831 | } |
832 | } |
833 | printf("%s -> %s\n", srcfname, dstfname); |
834 | |
835 | sfree(srcfname); |
836 | sfree(dstfname); |
df49ff19 |
837 | return 1; |
9954aaa3 |
838 | } |
839 | |
d92624dc |
840 | int sftp_cmd_chmod(struct sftp_command *cmd) |
841 | { |
842 | char *fname, *mode; |
843 | int result; |
844 | struct fxp_attrs attrs; |
845 | unsigned attrs_clr, attrs_xor, oldperms, newperms; |
1bc24185 |
846 | struct sftp_packet *pktin; |
847 | struct sftp_request *req, *rreq; |
d92624dc |
848 | |
fa3db767 |
849 | if (back == NULL) { |
850 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
851 | return 0; |
852 | } |
853 | |
d92624dc |
854 | if (cmd->nwords < 3) { |
855 | printf("chmod: expects a mode specifier and a filename\n"); |
856 | return 0; |
857 | } |
858 | |
859 | /* |
860 | * Attempt to parse the mode specifier in cmd->words[1]. We |
861 | * don't support the full horror of Unix chmod; instead we |
862 | * support a much simpler syntax in which the user can either |
863 | * specify an octal number, or a comma-separated sequence of |
864 | * [ugoa]*[-+=][rwxst]+. (The initial [ugoa] sequence may |
865 | * _only_ be omitted if the only attribute mentioned is t, |
866 | * since all others require a user/group/other specification. |
867 | * Additionally, the s attribute may not be specified for any |
868 | * [ugoa] specifications other than exactly u or exactly g. |
869 | */ |
870 | attrs_clr = attrs_xor = 0; |
871 | mode = cmd->words[1]; |
872 | if (mode[0] >= '0' && mode[0] <= '9') { |
873 | if (mode[strspn(mode, "01234567")]) { |
874 | printf("chmod: numeric file modes should" |
875 | " contain digits 0-7 only\n"); |
876 | return 0; |
877 | } |
878 | attrs_clr = 07777; |
879 | sscanf(mode, "%o", &attrs_xor); |
880 | attrs_xor &= attrs_clr; |
881 | } else { |
882 | while (*mode) { |
883 | char *modebegin = mode; |
884 | unsigned subset, perms; |
885 | int action; |
886 | |
887 | subset = 0; |
888 | while (*mode && *mode != ',' && |
889 | *mode != '+' && *mode != '-' && *mode != '=') { |
890 | switch (*mode) { |
891 | case 'u': subset |= 04700; break; /* setuid, user perms */ |
892 | case 'g': subset |= 02070; break; /* setgid, group perms */ |
893 | case 'o': subset |= 00007; break; /* just other perms */ |
894 | case 'a': subset |= 06777; break; /* all of the above */ |
895 | default: |
896 | printf("chmod: file mode '%.*s' contains unrecognised" |
897 | " user/group/other specifier '%c'\n", |
898 | strcspn(modebegin, ","), modebegin, *mode); |
899 | return 0; |
900 | } |
901 | mode++; |
902 | } |
903 | if (!*mode || *mode == ',') { |
904 | printf("chmod: file mode '%.*s' is incomplete\n", |
905 | strcspn(modebegin, ","), modebegin); |
906 | return 0; |
907 | } |
908 | action = *mode++; |
909 | if (!*mode || *mode == ',') { |
910 | printf("chmod: file mode '%.*s' is incomplete\n", |
911 | strcspn(modebegin, ","), modebegin); |
912 | return 0; |
913 | } |
914 | perms = 0; |
915 | while (*mode && *mode != ',') { |
916 | switch (*mode) { |
917 | case 'r': perms |= 00444; break; |
918 | case 'w': perms |= 00222; break; |
919 | case 'x': perms |= 00111; break; |
920 | case 't': perms |= 01000; subset |= 01000; break; |
921 | case 's': |
922 | if ((subset & 06777) != 04700 && |
923 | (subset & 06777) != 02070) { |
924 | printf("chmod: file mode '%.*s': set[ug]id bit should" |
925 | " be used with exactly one of u or g only\n", |
926 | strcspn(modebegin, ","), modebegin); |
927 | return 0; |
928 | } |
929 | perms |= 06000; |
930 | break; |
931 | default: |
932 | printf("chmod: file mode '%.*s' contains unrecognised" |
933 | " permission specifier '%c'\n", |
934 | strcspn(modebegin, ","), modebegin, *mode); |
935 | return 0; |
936 | } |
937 | mode++; |
938 | } |
939 | if (!(subset & 06777) && (perms &~ subset)) { |
940 | printf("chmod: file mode '%.*s' contains no user/group/other" |
941 | " specifier and permissions other than 't' \n", |
7a4b5114 |
942 | strcspn(modebegin, ","), modebegin); |
d92624dc |
943 | return 0; |
944 | } |
945 | perms &= subset; |
946 | switch (action) { |
947 | case '+': |
948 | attrs_clr |= perms; |
949 | attrs_xor |= perms; |
950 | break; |
951 | case '-': |
952 | attrs_clr |= perms; |
953 | attrs_xor &= ~perms; |
954 | break; |
955 | case '=': |
956 | attrs_clr |= subset; |
957 | attrs_xor |= perms; |
958 | break; |
959 | } |
960 | if (*mode) mode++; /* eat comma */ |
961 | } |
962 | } |
963 | |
964 | fname = canonify(cmd->words[2]); |
965 | if (!fname) { |
966 | printf("%s: %s\n", fname, fxp_error()); |
967 | return 0; |
968 | } |
969 | |
1bc24185 |
970 | sftp_register(req = fxp_stat_send(fname)); |
971 | rreq = sftp_find_request(pktin = sftp_recv()); |
972 | assert(rreq == req); |
7b7de4f4 |
973 | result = fxp_stat_recv(pktin, rreq, &attrs); |
1bc24185 |
974 | |
d92624dc |
975 | if (!result || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) { |
976 | printf("get attrs for %s: %s\n", fname, |
977 | result ? "file permissions not provided" : fxp_error()); |
978 | sfree(fname); |
979 | return 0; |
980 | } |
981 | |
982 | attrs.flags = SSH_FILEXFER_ATTR_PERMISSIONS; /* perms _only_ */ |
983 | oldperms = attrs.permissions & 07777; |
984 | attrs.permissions &= ~attrs_clr; |
985 | attrs.permissions ^= attrs_xor; |
986 | newperms = attrs.permissions & 07777; |
987 | |
1bc24185 |
988 | sftp_register(req = fxp_setstat_send(fname, attrs)); |
989 | rreq = sftp_find_request(pktin = sftp_recv()); |
990 | assert(rreq == req); |
7b7de4f4 |
991 | result = fxp_setstat_recv(pktin, rreq); |
d92624dc |
992 | |
993 | if (!result) { |
994 | printf("set attrs for %s: %s\n", fname, fxp_error()); |
995 | sfree(fname); |
996 | return 0; |
997 | } |
998 | |
999 | printf("%s: %04o -> %04o\n", fname, oldperms, newperms); |
1000 | |
1001 | sfree(fname); |
df49ff19 |
1002 | return 1; |
d92624dc |
1003 | } |
9954aaa3 |
1004 | |
fa3db767 |
1005 | static int sftp_cmd_open(struct sftp_command *cmd) |
1006 | { |
1007 | if (back != NULL) { |
1008 | printf("psftp: already connected\n"); |
1009 | return 0; |
1010 | } |
1011 | |
1012 | if (cmd->nwords < 2) { |
1013 | printf("open: expects a host name\n"); |
1014 | return 0; |
1015 | } |
1016 | |
1017 | if (psftp_connect(cmd->words[1], NULL, 0)) { |
1018 | back = NULL; /* connection is already closed */ |
1019 | return -1; /* this is fatal */ |
1020 | } |
1021 | do_sftp_init(); |
df49ff19 |
1022 | return 1; |
fa3db767 |
1023 | } |
1024 | |
3af97463 |
1025 | static int sftp_cmd_lcd(struct sftp_command *cmd) |
1026 | { |
1027 | char *currdir; |
1028 | int len; |
1029 | |
1030 | if (cmd->nwords < 2) { |
1031 | printf("lcd: expects a local directory name\n"); |
1032 | return 0; |
1033 | } |
1034 | |
1035 | if (!SetCurrentDirectory(cmd->words[1])) { |
1036 | LPVOID message; |
1037 | int i; |
1038 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
1039 | FORMAT_MESSAGE_FROM_SYSTEM | |
1040 | FORMAT_MESSAGE_IGNORE_INSERTS, |
1041 | NULL, GetLastError(), |
1042 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
1043 | (LPTSTR)&message, 0, NULL); |
1044 | i = strcspn((char *)message, "\n"); |
1045 | printf("lcd: unable to change directory: %.*s\n", i, (LPCTSTR)message); |
1046 | LocalFree(message); |
1047 | return 0; |
1048 | } |
1049 | |
3d88e64d |
1050 | currdir = snewn(256, char); |
3af97463 |
1051 | len = GetCurrentDirectory(256, currdir); |
1052 | if (len > 256) |
3d88e64d |
1053 | currdir = sresize(currdir, len, char); |
3af97463 |
1054 | GetCurrentDirectory(len, currdir); |
1055 | printf("New local directory is %s\n", currdir); |
1056 | sfree(currdir); |
1057 | |
1058 | return 1; |
1059 | } |
1060 | |
1061 | static int sftp_cmd_lpwd(struct sftp_command *cmd) |
1062 | { |
1063 | char *currdir; |
1064 | int len; |
1065 | |
3d88e64d |
1066 | currdir = snewn(256, char); |
3af97463 |
1067 | len = GetCurrentDirectory(256, currdir); |
1068 | if (len > 256) |
3d88e64d |
1069 | currdir = sresize(currdir, len, char); |
3af97463 |
1070 | GetCurrentDirectory(len, currdir); |
1071 | printf("Current local directory is %s\n", currdir); |
1072 | sfree(currdir); |
1073 | |
1074 | return 1; |
1075 | } |
1076 | |
1077 | static int sftp_cmd_pling(struct sftp_command *cmd) |
1078 | { |
1079 | int exitcode; |
1080 | |
1081 | exitcode = system(cmd->words[1]); |
1082 | return (exitcode == 0); |
1083 | } |
1084 | |
bf5240cd |
1085 | static int sftp_cmd_help(struct sftp_command *cmd); |
1086 | |
4c7f0d61 |
1087 | static struct sftp_cmd_lookup { |
1088 | char *name; |
bf5240cd |
1089 | /* |
1090 | * For help purposes, there are two kinds of command: |
1091 | * |
1092 | * - primary commands, in which `longhelp' is non-NULL. In |
1093 | * this case `shorthelp' is descriptive text, and `longhelp' |
1094 | * is longer descriptive text intended to be printed after |
1095 | * the command name. |
1096 | * |
1097 | * - alias commands, in which `longhelp' is NULL. In this case |
1098 | * `shorthelp' is the name of a primary command, which |
1099 | * contains the help that should double up for this command. |
1100 | */ |
3af97463 |
1101 | int listed; /* do we list this in primary help? */ |
bf5240cd |
1102 | char *shorthelp; |
1103 | char *longhelp; |
32874aea |
1104 | int (*obey) (struct sftp_command *); |
4c7f0d61 |
1105 | } sftp_lookup[] = { |
1106 | /* |
1107 | * List of sftp commands. This is binary-searched so it MUST be |
1108 | * in ASCII order. |
1109 | */ |
32874aea |
1110 | { |
3af97463 |
1111 | "!", TRUE, "run a local Windows command", |
1112 | "<command>\n" |
1113 | " Runs a local Windows command. For example, \"!del myfile\".\n", |
1114 | sftp_cmd_pling |
1115 | }, |
1116 | { |
1117 | "bye", TRUE, "finish your SFTP session", |
bf5240cd |
1118 | "\n" |
1119 | " Terminates your SFTP session and quits the PSFTP program.\n", |
1120 | sftp_cmd_quit |
1121 | }, |
1122 | { |
3af97463 |
1123 | "cd", TRUE, "change your remote working directory", |
bf5240cd |
1124 | " [ <New working directory> ]\n" |
1125 | " Change the remote working directory for your SFTP session.\n" |
1126 | " If a new working directory is not supplied, you will be\n" |
1127 | " returned to your home directory.\n", |
1128 | sftp_cmd_cd |
1129 | }, |
1130 | { |
3af97463 |
1131 | "chmod", TRUE, "change file permissions and modes", |
bf5240cd |
1132 | " ( <octal-digits> | <modifiers> ) <filename>\n" |
1133 | " Change the file permissions on a file or directory.\n" |
1134 | " <octal-digits> can be any octal Unix permission specifier.\n" |
1135 | " Alternatively, <modifiers> can include:\n" |
1136 | " u+r make file readable by owning user\n" |
1137 | " u+w make file writable by owning user\n" |
1138 | " u+x make file executable by owning user\n" |
1139 | " u-r make file not readable by owning user\n" |
1140 | " [also u-w, u-x]\n" |
1141 | " g+r make file readable by members of owning group\n" |
1142 | " [also g+w, g+x, g-r, g-w, g-x]\n" |
1143 | " o+r make file readable by all other users\n" |
1144 | " [also o+w, o+x, o-r, o-w, o-x]\n" |
1145 | " a+r make file readable by absolutely everybody\n" |
1146 | " [also a+w, a+x, a-r, a-w, a-x]\n" |
1147 | " u+s enable the Unix set-user-ID bit\n" |
1148 | " u-s disable the Unix set-user-ID bit\n" |
1149 | " g+s enable the Unix set-group-ID bit\n" |
1150 | " g-s disable the Unix set-group-ID bit\n" |
1151 | " +t enable the Unix \"sticky bit\"\n" |
1152 | " You can give more than one modifier for the same user (\"g-rwx\"), and\n" |
1153 | " more than one user for the same modifier (\"ug+w\"). You can\n" |
1154 | " use commas to separate different modifiers (\"u+rwx,g+s\").\n", |
1155 | sftp_cmd_chmod |
1156 | }, |
1157 | { |
3af97463 |
1158 | "del", TRUE, "delete a file", |
bf5240cd |
1159 | " <filename>\n" |
1160 | " Delete a file.\n", |
1161 | sftp_cmd_rm |
1162 | }, |
1163 | { |
3af97463 |
1164 | "delete", FALSE, "del", NULL, sftp_cmd_rm |
bf5240cd |
1165 | }, |
1166 | { |
3af97463 |
1167 | "dir", TRUE, "list contents of a remote directory", |
bf5240cd |
1168 | " [ <directory-name> ]\n" |
1169 | " List the contents of a specified directory on the server.\n" |
1170 | " If <directory-name> is not given, the current working directory\n" |
1171 | " will be listed.\n", |
1172 | sftp_cmd_ls |
1173 | }, |
1174 | { |
3af97463 |
1175 | "exit", TRUE, "bye", NULL, sftp_cmd_quit |
bf5240cd |
1176 | }, |
1177 | { |
3af97463 |
1178 | "get", TRUE, "download a file from the server to your local machine", |
bf5240cd |
1179 | " <filename> [ <local-filename> ]\n" |
1180 | " Downloads a file on the server and stores it locally under\n" |
1181 | " the same name, or under a different one if you supply the\n" |
1182 | " argument <local-filename>.\n", |
1183 | sftp_cmd_get |
1184 | }, |
1185 | { |
3af97463 |
1186 | "help", TRUE, "give help", |
bf5240cd |
1187 | " [ <command> [ <command> ... ] ]\n" |
1188 | " Give general help if no commands are specified.\n" |
1189 | " If one or more commands are specified, give specific help on\n" |
1190 | " those particular commands.\n", |
1191 | sftp_cmd_help |
1192 | }, |
1193 | { |
3af97463 |
1194 | "lcd", TRUE, "change local working directory", |
1195 | " <local-directory-name>\n" |
1196 | " Change the local working directory of the PSFTP program (the\n" |
1197 | " default location where the \"get\" command will save files).\n", |
1198 | sftp_cmd_lcd |
1199 | }, |
1200 | { |
1201 | "lpwd", TRUE, "print local working directory", |
1202 | "\n" |
1203 | " Print the local working directory of the PSFTP program (the\n" |
1204 | " default location where the \"get\" command will save files).\n", |
1205 | sftp_cmd_lpwd |
1206 | }, |
1207 | { |
1208 | "ls", TRUE, "dir", NULL, |
bf5240cd |
1209 | sftp_cmd_ls |
1210 | }, |
1211 | { |
3af97463 |
1212 | "mkdir", TRUE, "create a directory on the remote server", |
bf5240cd |
1213 | " <directory-name>\n" |
1214 | " Creates a directory with the given name on the server.\n", |
1215 | sftp_cmd_mkdir |
1216 | }, |
1217 | { |
3af97463 |
1218 | "mv", TRUE, "move or rename a file on the remote server", |
bf5240cd |
1219 | " <source-filename> <destination-filename>\n" |
1220 | " Moves or renames the file <source-filename> on the server,\n" |
1221 | " so that it is accessible under the name <destination-filename>.\n", |
1222 | sftp_cmd_mv |
1223 | }, |
1224 | { |
3af97463 |
1225 | "open", TRUE, "connect to a host", |
fa3db767 |
1226 | " [<user>@]<hostname>\n" |
1227 | " Establishes an SFTP connection to a given host. Only usable\n" |
1228 | " when you did not already specify a host name on the command\n" |
1229 | " line.\n", |
1230 | sftp_cmd_open |
1231 | }, |
1232 | { |
56542985 |
1233 | "put", TRUE, "upload a file from your local machine to the server", |
1234 | " <filename> [ <remote-filename> ]\n" |
1235 | " Uploads a file to the server and stores it there under\n" |
1236 | " the same name, or under a different one if you supply the\n" |
1237 | " argument <remote-filename>.\n", |
1238 | sftp_cmd_put |
1239 | }, |
1240 | { |
3af97463 |
1241 | "pwd", TRUE, "print your remote working directory", |
4f2b387f |
1242 | "\n" |
1243 | " Print the current remote working directory for your SFTP session.\n", |
1244 | sftp_cmd_pwd |
1245 | }, |
1246 | { |
3af97463 |
1247 | "quit", TRUE, "bye", NULL, |
bf5240cd |
1248 | sftp_cmd_quit |
1249 | }, |
1250 | { |
3af97463 |
1251 | "reget", TRUE, "continue downloading a file", |
bf5240cd |
1252 | " <filename> [ <local-filename> ]\n" |
1253 | " Works exactly like the \"get\" command, but the local file\n" |
1254 | " must already exist. The download will begin at the end of the\n" |
1255 | " file. This is for resuming a download that was interrupted.\n", |
1256 | sftp_cmd_reget |
1257 | }, |
1258 | { |
3af97463 |
1259 | "ren", TRUE, "mv", NULL, |
bf5240cd |
1260 | sftp_cmd_mv |
1261 | }, |
1262 | { |
3af97463 |
1263 | "rename", FALSE, "mv", NULL, |
bf5240cd |
1264 | sftp_cmd_mv |
1265 | }, |
1266 | { |
3af97463 |
1267 | "reput", TRUE, "continue uploading a file", |
bf5240cd |
1268 | " <filename> [ <remote-filename> ]\n" |
1269 | " Works exactly like the \"put\" command, but the remote file\n" |
1270 | " must already exist. The upload will begin at the end of the\n" |
1271 | " file. This is for resuming an upload that was interrupted.\n", |
1272 | sftp_cmd_reput |
1273 | }, |
1274 | { |
3af97463 |
1275 | "rm", TRUE, "del", NULL, |
bf5240cd |
1276 | sftp_cmd_rm |
1277 | }, |
1278 | { |
3af97463 |
1279 | "rmdir", TRUE, "remove a directory on the remote server", |
bf5240cd |
1280 | " <directory-name>\n" |
1281 | " Removes the directory with the given name on the server.\n" |
1282 | " The directory will not be removed unless it is empty.\n", |
1283 | sftp_cmd_rmdir |
1284 | } |
1285 | }; |
1286 | |
1287 | const struct sftp_cmd_lookup *lookup_command(char *name) |
1288 | { |
1289 | int i, j, k, cmp; |
1290 | |
1291 | i = -1; |
1292 | j = sizeof(sftp_lookup) / sizeof(*sftp_lookup); |
1293 | while (j - i > 1) { |
1294 | k = (j + i) / 2; |
1295 | cmp = strcmp(name, sftp_lookup[k].name); |
1296 | if (cmp < 0) |
1297 | j = k; |
1298 | else if (cmp > 0) |
1299 | i = k; |
1300 | else { |
1301 | return &sftp_lookup[k]; |
1302 | } |
1303 | } |
1304 | return NULL; |
1305 | } |
1306 | |
1307 | static int sftp_cmd_help(struct sftp_command *cmd) |
1308 | { |
1309 | int i; |
1310 | if (cmd->nwords == 1) { |
1311 | /* |
1312 | * Give short help on each command. |
1313 | */ |
1314 | int maxlen; |
1315 | maxlen = 0; |
1316 | for (i = 0; i < sizeof(sftp_lookup) / sizeof(*sftp_lookup); i++) { |
3af97463 |
1317 | int len; |
1318 | if (!sftp_lookup[i].listed) |
1319 | continue; |
1320 | len = strlen(sftp_lookup[i].name); |
bf5240cd |
1321 | if (maxlen < len) |
1322 | maxlen = len; |
1323 | } |
1324 | for (i = 0; i < sizeof(sftp_lookup) / sizeof(*sftp_lookup); i++) { |
1325 | const struct sftp_cmd_lookup *lookup; |
3af97463 |
1326 | if (!sftp_lookup[i].listed) |
1327 | continue; |
bf5240cd |
1328 | lookup = &sftp_lookup[i]; |
1329 | printf("%-*s", maxlen+2, lookup->name); |
1330 | if (lookup->longhelp == NULL) |
1331 | lookup = lookup_command(lookup->shorthelp); |
1332 | printf("%s\n", lookup->shorthelp); |
1333 | } |
1334 | } else { |
1335 | /* |
1336 | * Give long help on specific commands. |
1337 | */ |
1338 | for (i = 1; i < cmd->nwords; i++) { |
1339 | const struct sftp_cmd_lookup *lookup; |
1340 | lookup = lookup_command(cmd->words[i]); |
1341 | if (!lookup) { |
1342 | printf("help: %s: command not found\n", cmd->words[i]); |
1343 | } else { |
1344 | printf("%s", lookup->name); |
1345 | if (lookup->longhelp == NULL) |
1346 | lookup = lookup_command(lookup->shorthelp); |
1347 | printf("%s", lookup->longhelp); |
1348 | } |
1349 | } |
1350 | } |
df49ff19 |
1351 | return 1; |
bf5240cd |
1352 | } |
4c7f0d61 |
1353 | |
1354 | /* ---------------------------------------------------------------------- |
1355 | * Command line reading and parsing. |
1356 | */ |
9954aaa3 |
1357 | struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) |
32874aea |
1358 | { |
4c7f0d61 |
1359 | char *line; |
1360 | int linelen, linesize; |
1361 | struct sftp_command *cmd; |
1362 | char *p, *q, *r; |
1363 | int quoting; |
1364 | |
df49ff19 |
1365 | if ((mode == 0) || (modeflags & 1)) { |
1366 | printf("psftp> "); |
1367 | } |
4c7f0d61 |
1368 | fflush(stdout); |
1369 | |
3d88e64d |
1370 | cmd = snew(struct sftp_command); |
4c7f0d61 |
1371 | cmd->words = NULL; |
1372 | cmd->nwords = 0; |
1373 | cmd->wordssize = 0; |
1374 | |
1375 | line = NULL; |
1376 | linesize = linelen = 0; |
1377 | while (1) { |
1378 | int len; |
1379 | char *ret; |
1380 | |
1381 | linesize += 512; |
3d88e64d |
1382 | line = sresize(line, linesize, char); |
9954aaa3 |
1383 | ret = fgets(line + linelen, linesize - linelen, fp); |
4c7f0d61 |
1384 | |
1385 | if (!ret || (linelen == 0 && line[0] == '\0')) { |
1386 | cmd->obey = sftp_cmd_quit; |
d13c2ee9 |
1387 | if ((mode == 0) || (modeflags & 1)) |
1388 | printf("quit\n"); |
4c7f0d61 |
1389 | return cmd; /* eof */ |
1390 | } |
32874aea |
1391 | len = linelen + strlen(line + linelen); |
4c7f0d61 |
1392 | linelen += len; |
32874aea |
1393 | if (line[linelen - 1] == '\n') { |
4c7f0d61 |
1394 | linelen--; |
1395 | line[linelen] = '\0'; |
1396 | break; |
1397 | } |
1398 | } |
df49ff19 |
1399 | if (modeflags & 1) { |
1400 | printf("%s\n", line); |
1401 | } |
4c7f0d61 |
1402 | |
4c7f0d61 |
1403 | p = line; |
3af97463 |
1404 | while (*p && (*p == ' ' || *p == '\t')) |
1405 | p++; |
1406 | |
1407 | if (*p == '!') { |
1408 | /* |
1409 | * Special case: the ! command. This is always parsed as |
1410 | * exactly two words: one containing the !, and the second |
1411 | * containing everything else on the line. |
1412 | */ |
1413 | cmd->nwords = cmd->wordssize = 2; |
3d88e64d |
1414 | cmd->words = sresize(cmd->words, cmd->wordssize, char *); |
3af97463 |
1415 | cmd->words[0] = "!"; |
1416 | cmd->words[1] = p+1; |
1417 | } else { |
1418 | |
1419 | /* |
1420 | * Parse the command line into words. The syntax is: |
1421 | * - double quotes are removed, but cause spaces within to be |
1422 | * treated as non-separating. |
1423 | * - a double-doublequote pair is a literal double quote, inside |
1424 | * _or_ outside quotes. Like this: |
1425 | * |
1426 | * firstword "second word" "this has ""quotes"" in" and""this"" |
1427 | * |
1428 | * becomes |
1429 | * |
1430 | * >firstword< |
1431 | * >second word< |
1432 | * >this has "quotes" in< |
1433 | * >and"this"< |
1434 | */ |
4c7f0d61 |
1435 | while (*p) { |
3af97463 |
1436 | /* skip whitespace */ |
1437 | while (*p && (*p == ' ' || *p == '\t')) |
1438 | p++; |
1439 | /* mark start of word */ |
1440 | q = r = p; /* q sits at start, r writes word */ |
1441 | quoting = 0; |
1442 | while (*p) { |
1443 | if (!quoting && (*p == ' ' || *p == '\t')) |
1444 | break; /* reached end of word */ |
1445 | else if (*p == '"' && p[1] == '"') |
1446 | p += 2, *r++ = '"'; /* a literal quote */ |
1447 | else if (*p == '"') |
1448 | p++, quoting = !quoting; |
1449 | else |
1450 | *r++ = *p++; |
1451 | } |
1452 | if (*p) |
1453 | p++; /* skip over the whitespace */ |
1454 | *r = '\0'; |
1455 | if (cmd->nwords >= cmd->wordssize) { |
1456 | cmd->wordssize = cmd->nwords + 16; |
3d88e64d |
1457 | cmd->words = sresize(cmd->words, cmd->wordssize, char *); |
3af97463 |
1458 | } |
1459 | cmd->words[cmd->nwords++] = q; |
4c7f0d61 |
1460 | } |
4c7f0d61 |
1461 | } |
1462 | |
1463 | /* |
1464 | * Now parse the first word and assign a function. |
1465 | */ |
1466 | |
1467 | if (cmd->nwords == 0) |
1468 | cmd->obey = sftp_cmd_null; |
1469 | else { |
bf5240cd |
1470 | const struct sftp_cmd_lookup *lookup; |
1471 | lookup = lookup_command(cmd->words[0]); |
1472 | if (!lookup) |
1473 | cmd->obey = sftp_cmd_unknown; |
1474 | else |
1475 | cmd->obey = lookup->obey; |
4c7f0d61 |
1476 | } |
1477 | |
1478 | return cmd; |
1479 | } |
1480 | |
774204f5 |
1481 | static int do_sftp_init(void) |
32874aea |
1482 | { |
1bc24185 |
1483 | struct sftp_packet *pktin; |
1484 | struct sftp_request *req, *rreq; |
1485 | |
4c7f0d61 |
1486 | /* |
1487 | * Do protocol initialisation. |
1488 | */ |
1489 | if (!fxp_init()) { |
1490 | fprintf(stderr, |
32874aea |
1491 | "Fatal: unable to initialise SFTP: %s\n", fxp_error()); |
774204f5 |
1492 | return 1; /* failure */ |
4c7f0d61 |
1493 | } |
1494 | |
1495 | /* |
1496 | * Find out where our home directory is. |
1497 | */ |
1bc24185 |
1498 | sftp_register(req = fxp_realpath_send(".")); |
1499 | rreq = sftp_find_request(pktin = sftp_recv()); |
1500 | assert(rreq == req); |
7b7de4f4 |
1501 | homedir = fxp_realpath_recv(pktin, rreq); |
1bc24185 |
1502 | |
4c7f0d61 |
1503 | if (!homedir) { |
1504 | fprintf(stderr, |
1505 | "Warning: failed to resolve home directory: %s\n", |
1506 | fxp_error()); |
1507 | homedir = dupstr("."); |
1508 | } else { |
1509 | printf("Remote working directory is %s\n", homedir); |
1510 | } |
1511 | pwd = dupstr(homedir); |
774204f5 |
1512 | return 0; |
fa3db767 |
1513 | } |
1514 | |
1515 | void do_sftp(int mode, int modeflags, char *batchfile) |
1516 | { |
1517 | FILE *fp; |
df49ff19 |
1518 | int ret; |
4c7f0d61 |
1519 | |
9954aaa3 |
1520 | /* |
1521 | * Batch mode? |
4c7f0d61 |
1522 | */ |
9954aaa3 |
1523 | if (mode == 0) { |
1524 | |
1525 | /* ------------------------------------------------------------------ |
1526 | * Now we're ready to do Real Stuff. |
1527 | */ |
1528 | while (1) { |
df49ff19 |
1529 | struct sftp_command *cmd; |
1530 | cmd = sftp_getcmd(stdin, 0, 0); |
1531 | if (!cmd) |
1532 | break; |
1533 | if (cmd->obey(cmd) < 0) |
1534 | break; |
bf5240cd |
1535 | } |
9954aaa3 |
1536 | } else { |
1537 | fp = fopen(batchfile, "r"); |
1538 | if (!fp) { |
bf5240cd |
1539 | printf("Fatal: unable to open %s\n", batchfile); |
1540 | return; |
9954aaa3 |
1541 | } |
1542 | while (1) { |
bf5240cd |
1543 | struct sftp_command *cmd; |
1544 | cmd = sftp_getcmd(fp, mode, modeflags); |
1545 | if (!cmd) |
1546 | break; |
df49ff19 |
1547 | ret = cmd->obey(cmd); |
1548 | if (ret < 0) |
bf5240cd |
1549 | break; |
df49ff19 |
1550 | if (ret == 0) { |
bf5240cd |
1551 | if (!(modeflags & 2)) |
9954aaa3 |
1552 | break; |
bf5240cd |
1553 | } |
9954aaa3 |
1554 | } |
bf5240cd |
1555 | fclose(fp); |
9954aaa3 |
1556 | |
4c7f0d61 |
1557 | } |
4a8fc3c4 |
1558 | } |
4c7f0d61 |
1559 | |
4a8fc3c4 |
1560 | /* ---------------------------------------------------------------------- |
1561 | * Dirty bits: integration with PuTTY. |
1562 | */ |
1563 | |
1564 | static int verbose = 0; |
1565 | |
7bedb13c |
1566 | /* |
4a8fc3c4 |
1567 | * Print an error message and perform a fatal exit. |
1568 | */ |
1569 | void fatalbox(char *fmt, ...) |
1570 | { |
57356d63 |
1571 | char *str, *str2; |
4a8fc3c4 |
1572 | va_list ap; |
1573 | va_start(ap, fmt); |
57356d63 |
1574 | str = dupvprintf(fmt, ap); |
1575 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
1576 | sfree(str); |
4a8fc3c4 |
1577 | va_end(ap); |
57356d63 |
1578 | fputs(str2, stderr); |
1579 | sfree(str2); |
4a8fc3c4 |
1580 | |
93b581bd |
1581 | cleanup_exit(1); |
4a8fc3c4 |
1582 | } |
1709795f |
1583 | void modalfatalbox(char *fmt, ...) |
1584 | { |
57356d63 |
1585 | char *str, *str2; |
1709795f |
1586 | va_list ap; |
1587 | va_start(ap, fmt); |
57356d63 |
1588 | str = dupvprintf(fmt, ap); |
1589 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
1590 | sfree(str); |
1709795f |
1591 | va_end(ap); |
57356d63 |
1592 | fputs(str2, stderr); |
1593 | sfree(str2); |
1709795f |
1594 | |
1595 | cleanup_exit(1); |
1596 | } |
a8327734 |
1597 | void connection_fatal(void *frontend, char *fmt, ...) |
4a8fc3c4 |
1598 | { |
57356d63 |
1599 | char *str, *str2; |
4a8fc3c4 |
1600 | va_list ap; |
1601 | va_start(ap, fmt); |
57356d63 |
1602 | str = dupvprintf(fmt, ap); |
1603 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
1604 | sfree(str); |
4a8fc3c4 |
1605 | va_end(ap); |
57356d63 |
1606 | fputs(str2, stderr); |
1607 | sfree(str2); |
4a8fc3c4 |
1608 | |
93b581bd |
1609 | cleanup_exit(1); |
4a8fc3c4 |
1610 | } |
1611 | |
6b78788a |
1612 | void ldisc_send(void *handle, char *buf, int len, int interactive) |
32874aea |
1613 | { |
4a8fc3c4 |
1614 | /* |
1615 | * This is only here because of the calls to ldisc_send(NULL, |
1616 | * 0) in ssh.c. Nothing in PSFTP actually needs to use the |
1617 | * ldisc as an ldisc. So if we get called with any real data, I |
1618 | * want to know about it. |
4c7f0d61 |
1619 | */ |
4a8fc3c4 |
1620 | assert(len == 0); |
1621 | } |
1622 | |
1623 | /* |
1624 | * Be told what socket we're supposed to be using. |
1625 | */ |
1626 | static SOCKET sftp_ssh_socket; |
32874aea |
1627 | char *do_select(SOCKET skt, int startup) |
1628 | { |
4a8fc3c4 |
1629 | if (startup) |
1630 | sftp_ssh_socket = skt; |
1631 | else |
1632 | sftp_ssh_socket = INVALID_SOCKET; |
1633 | return NULL; |
4c7f0d61 |
1634 | } |
4a8fc3c4 |
1635 | extern int select_result(WPARAM, LPARAM); |
1636 | |
1637 | /* |
c44bf5bd |
1638 | * In psftp, all agent requests should be synchronous, so this is a |
1639 | * never-called stub. |
1640 | */ |
1641 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
1642 | void *callback_ctx, void *data, int len) |
1643 | { |
1644 | assert(!"We shouldn't be here"); |
1645 | } |
1646 | |
1647 | /* |
4a8fc3c4 |
1648 | * Receive a block of data from the SSH link. Block until all data |
1649 | * is available. |
1650 | * |
1651 | * To do this, we repeatedly call the SSH protocol module, with our |
1652 | * own trap in from_backend() to catch the data that comes back. We |
1653 | * do this until we have enough data. |
1654 | */ |
1655 | |
32874aea |
1656 | static unsigned char *outptr; /* where to put the data */ |
1657 | static unsigned outlen; /* how much data required */ |
4a8fc3c4 |
1658 | static unsigned char *pending = NULL; /* any spare data */ |
32874aea |
1659 | static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ |
9fab77dc |
1660 | int from_backend(void *frontend, int is_stderr, const char *data, int datalen) |
32874aea |
1661 | { |
1662 | unsigned char *p = (unsigned char *) data; |
1663 | unsigned len = (unsigned) datalen; |
4a8fc3c4 |
1664 | |
2b0c045b |
1665 | assert(len > 0); |
1666 | |
4a8fc3c4 |
1667 | /* |
1668 | * stderr data is just spouted to local stderr and otherwise |
1669 | * ignored. |
1670 | */ |
1671 | if (is_stderr) { |
1672 | fwrite(data, 1, len, stderr); |
5471d09a |
1673 | return 0; |
4a8fc3c4 |
1674 | } |
1675 | |
1676 | /* |
1677 | * If this is before the real session begins, just return. |
1678 | */ |
1679 | if (!outptr) |
5471d09a |
1680 | return 0; |
4a8fc3c4 |
1681 | |
1682 | if (outlen > 0) { |
32874aea |
1683 | unsigned used = outlen; |
1684 | if (used > len) |
1685 | used = len; |
1686 | memcpy(outptr, p, used); |
1687 | outptr += used; |
1688 | outlen -= used; |
1689 | p += used; |
1690 | len -= used; |
4a8fc3c4 |
1691 | } |
1692 | |
1693 | if (len > 0) { |
32874aea |
1694 | if (pendsize < pendlen + len) { |
1695 | pendsize = pendlen + len + 4096; |
3d88e64d |
1696 | pending = sresize(pending, pendsize, unsigned char); |
32874aea |
1697 | } |
1698 | memcpy(pending + pendlen, p, len); |
1699 | pendlen += len; |
4a8fc3c4 |
1700 | } |
5471d09a |
1701 | |
1702 | return 0; |
4a8fc3c4 |
1703 | } |
32874aea |
1704 | int sftp_recvdata(char *buf, int len) |
1705 | { |
1706 | outptr = (unsigned char *) buf; |
4a8fc3c4 |
1707 | outlen = len; |
1708 | |
1709 | /* |
1710 | * See if the pending-input block contains some of what we |
1711 | * need. |
1712 | */ |
1713 | if (pendlen > 0) { |
32874aea |
1714 | unsigned pendused = pendlen; |
1715 | if (pendused > outlen) |
1716 | pendused = outlen; |
4a8fc3c4 |
1717 | memcpy(outptr, pending, pendused); |
32874aea |
1718 | memmove(pending, pending + pendused, pendlen - pendused); |
4a8fc3c4 |
1719 | outptr += pendused; |
1720 | outlen -= pendused; |
32874aea |
1721 | pendlen -= pendused; |
1722 | if (pendlen == 0) { |
1723 | pendsize = 0; |
1724 | sfree(pending); |
1725 | pending = NULL; |
1726 | } |
1727 | if (outlen == 0) |
1728 | return 1; |
4a8fc3c4 |
1729 | } |
1730 | |
1731 | while (outlen > 0) { |
32874aea |
1732 | fd_set readfds; |
4a8fc3c4 |
1733 | |
32874aea |
1734 | FD_ZERO(&readfds); |
1735 | FD_SET(sftp_ssh_socket, &readfds); |
1736 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
1737 | return 0; /* doom */ |
1738 | select_result((WPARAM) sftp_ssh_socket, (LPARAM) FD_READ); |
4a8fc3c4 |
1739 | } |
1740 | |
1741 | return 1; |
1742 | } |
32874aea |
1743 | int sftp_senddata(char *buf, int len) |
1744 | { |
51470298 |
1745 | back->send(backhandle, (unsigned char *) buf, len); |
4a8fc3c4 |
1746 | return 1; |
1747 | } |
1748 | |
1749 | /* |
1750 | * Loop through the ssh connection and authentication process. |
1751 | */ |
32874aea |
1752 | static void ssh_sftp_init(void) |
1753 | { |
4a8fc3c4 |
1754 | if (sftp_ssh_socket == INVALID_SOCKET) |
1755 | return; |
51470298 |
1756 | while (!back->sendok(backhandle)) { |
32874aea |
1757 | fd_set readfds; |
1758 | FD_ZERO(&readfds); |
1759 | FD_SET(sftp_ssh_socket, &readfds); |
1760 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
1761 | return; /* doom */ |
1762 | select_result((WPARAM) sftp_ssh_socket, (LPARAM) FD_READ); |
4a8fc3c4 |
1763 | } |
1764 | } |
1765 | |
4a8fc3c4 |
1766 | /* |
1767 | * Initialize the Win$ock driver. |
1768 | */ |
1769 | static void init_winsock(void) |
1770 | { |
1771 | WORD winsock_ver; |
1772 | WSADATA wsadata; |
1773 | |
1774 | winsock_ver = MAKEWORD(1, 1); |
1775 | if (WSAStartup(winsock_ver, &wsadata)) { |
1776 | fprintf(stderr, "Unable to initialise WinSock"); |
93b581bd |
1777 | cleanup_exit(1); |
4a8fc3c4 |
1778 | } |
32874aea |
1779 | if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) { |
4a8fc3c4 |
1780 | fprintf(stderr, "WinSock version is incompatible with 1.1"); |
93b581bd |
1781 | cleanup_exit(1); |
4a8fc3c4 |
1782 | } |
1783 | } |
1784 | |
1785 | /* |
1786 | * Short description of parameters. |
1787 | */ |
1788 | static void usage(void) |
1789 | { |
1790 | printf("PuTTY Secure File Transfer (SFTP) client\n"); |
1791 | printf("%s\n", ver); |
1792 | printf("Usage: psftp [options] user@host\n"); |
1793 | printf("Options:\n"); |
9954aaa3 |
1794 | printf(" -b file use specified batchfile\n"); |
1795 | printf(" -bc output batchfile commands\n"); |
1796 | printf(" -be don't stop batchfile processing if errors\n"); |
4a8fc3c4 |
1797 | printf(" -v show verbose messages\n"); |
e2a197cf |
1798 | printf(" -load sessname Load settings from saved session\n"); |
1799 | printf(" -l user connect with specified username\n"); |
4a8fc3c4 |
1800 | printf(" -P port connect to specified port\n"); |
1801 | printf(" -pw passw login with specified password\n"); |
e2a197cf |
1802 | printf(" -1 -2 force use of particular SSH protocol version\n"); |
1803 | printf(" -C enable compression\n"); |
1804 | printf(" -i key private key file for authentication\n"); |
1805 | printf(" -batch disable all interactive prompts\n"); |
93b581bd |
1806 | cleanup_exit(1); |
4a8fc3c4 |
1807 | } |
1808 | |
1809 | /* |
fa3db767 |
1810 | * Connect to a host. |
4a8fc3c4 |
1811 | */ |
fa3db767 |
1812 | static int psftp_connect(char *userhost, char *user, int portnumber) |
4a8fc3c4 |
1813 | { |
fa3db767 |
1814 | char *host, *realhost; |
cbe2d68f |
1815 | const char *err; |
4a8fc3c4 |
1816 | |
1817 | /* Separate host and username */ |
1818 | host = userhost; |
1819 | host = strrchr(host, '@'); |
1820 | if (host == NULL) { |
1821 | host = userhost; |
1822 | } else { |
1823 | *host++ = '\0'; |
1824 | if (user) { |
32874aea |
1825 | printf("psftp: multiple usernames specified; using \"%s\"\n", |
1826 | user); |
4a8fc3c4 |
1827 | } else |
1828 | user = userhost; |
1829 | } |
1830 | |
1831 | /* Try to load settings for this host */ |
1832 | do_defaults(host, &cfg); |
1833 | if (cfg.host[0] == '\0') { |
1834 | /* No settings for this host; use defaults */ |
32874aea |
1835 | do_defaults(NULL, &cfg); |
1836 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
1837 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
f133db8e |
1838 | } |
1839 | |
1840 | /* |
1841 | * Force use of SSH. (If they got the protocol wrong we assume the |
1842 | * port is useless too.) |
1843 | */ |
1844 | if (cfg.protocol != PROT_SSH) { |
1845 | cfg.protocol = PROT_SSH; |
1846 | cfg.port = 22; |
4a8fc3c4 |
1847 | } |
1848 | |
449925a6 |
1849 | /* |
c0a81592 |
1850 | * Enact command-line overrides. |
1851 | */ |
5555d393 |
1852 | cmdline_run_saved(&cfg); |
c0a81592 |
1853 | |
1854 | /* |
449925a6 |
1855 | * Trim leading whitespace off the hostname if it's there. |
1856 | */ |
1857 | { |
1858 | int space = strspn(cfg.host, " \t"); |
1859 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
1860 | } |
1861 | |
1862 | /* See if host is of the form user@host */ |
1863 | if (cfg.host[0] != '\0') { |
1864 | char *atsign = strchr(cfg.host, '@'); |
1865 | /* Make sure we're not overflowing the user field */ |
1866 | if (atsign) { |
1867 | if (atsign - cfg.host < sizeof cfg.username) { |
1868 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
1869 | cfg.username[atsign - cfg.host] = '\0'; |
1870 | } |
1871 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
1872 | } |
1873 | } |
1874 | |
1875 | /* |
1876 | * Trim a colon suffix off the hostname if it's there. |
1877 | */ |
1878 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
1879 | |
cae0c023 |
1880 | /* |
1881 | * Remove any remaining whitespace from the hostname. |
1882 | */ |
1883 | { |
1884 | int p1 = 0, p2 = 0; |
1885 | while (cfg.host[p2] != '\0') { |
1886 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
1887 | cfg.host[p1] = cfg.host[p2]; |
1888 | p1++; |
1889 | } |
1890 | p2++; |
1891 | } |
1892 | cfg.host[p1] = '\0'; |
1893 | } |
1894 | |
4a8fc3c4 |
1895 | /* Set username */ |
1896 | if (user != NULL && user[0] != '\0') { |
32874aea |
1897 | strncpy(cfg.username, user, sizeof(cfg.username) - 1); |
1898 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
4a8fc3c4 |
1899 | } |
1900 | if (!cfg.username[0]) { |
1901 | printf("login as: "); |
f55eceed |
1902 | fflush(stdout); |
4a8fc3c4 |
1903 | if (!fgets(cfg.username, sizeof(cfg.username), stdin)) { |
1904 | fprintf(stderr, "psftp: aborting\n"); |
93b581bd |
1905 | cleanup_exit(1); |
4a8fc3c4 |
1906 | } else { |
1907 | int len = strlen(cfg.username); |
32874aea |
1908 | if (cfg.username[len - 1] == '\n') |
1909 | cfg.username[len - 1] = '\0'; |
4a8fc3c4 |
1910 | } |
1911 | } |
1912 | |
4a8fc3c4 |
1913 | if (portnumber) |
1914 | cfg.port = portnumber; |
1915 | |
1916 | /* SFTP uses SSH2 by default always */ |
1917 | cfg.sshprot = 2; |
1918 | |
d27b4a18 |
1919 | /* |
1920 | * Disable scary things which shouldn't be enabled for simple |
1921 | * things like SCP and SFTP: agent forwarding, port forwarding, |
1922 | * X forwarding. |
1923 | */ |
1924 | cfg.x11_forward = 0; |
1925 | cfg.agentfwd = 0; |
1926 | cfg.portfwd[0] = cfg.portfwd[1] = '\0'; |
1927 | |
bebf22d0 |
1928 | /* Set up subsystem name. */ |
4a8fc3c4 |
1929 | strcpy(cfg.remote_cmd, "sftp"); |
1930 | cfg.ssh_subsys = TRUE; |
1931 | cfg.nopty = TRUE; |
1932 | |
bebf22d0 |
1933 | /* |
1934 | * Set up fallback option, for SSH1 servers or servers with the |
1935 | * sftp subsystem not enabled but the server binary installed |
1936 | * in the usual place. We only support fallback on Unix |
248c0c5a |
1937 | * systems, and we use a kludgy piece of shellery which should |
1938 | * try to find sftp-server in various places (the obvious |
1939 | * systemwide spots /usr/lib and /usr/local/lib, and then the |
1940 | * user's PATH) and finally give up. |
bebf22d0 |
1941 | * |
248c0c5a |
1942 | * test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server |
1943 | * test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server |
1944 | * exec sftp-server |
bebf22d0 |
1945 | * |
1946 | * the idea being that this will attempt to use either of the |
1947 | * obvious pathnames and then give up, and when it does give up |
1948 | * it will print the preferred pathname in the error messages. |
1949 | */ |
1950 | cfg.remote_cmd_ptr2 = |
248c0c5a |
1951 | "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" |
1952 | "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n" |
1953 | "exec sftp-server"; |
bebf22d0 |
1954 | cfg.ssh_subsys2 = FALSE; |
1955 | |
4a8fc3c4 |
1956 | back = &ssh_backend; |
1957 | |
86916870 |
1958 | err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,0); |
4a8fc3c4 |
1959 | if (err != NULL) { |
fa3db767 |
1960 | fprintf(stderr, "ssh_init: %s\n", err); |
4a8fc3c4 |
1961 | return 1; |
1962 | } |
c229ef97 |
1963 | logctx = log_init(NULL, &cfg); |
a8327734 |
1964 | back->provide_logctx(backhandle, logctx); |
d3fef4a5 |
1965 | console_provide_logctx(logctx); |
4a8fc3c4 |
1966 | ssh_sftp_init(); |
1967 | if (verbose && realhost != NULL) |
1968 | printf("Connected to %s\n", realhost); |
fa3db767 |
1969 | return 0; |
1970 | } |
1971 | |
c0a81592 |
1972 | void cmdline_error(char *p, ...) |
1973 | { |
1974 | va_list ap; |
86256dc6 |
1975 | fprintf(stderr, "psftp: "); |
c0a81592 |
1976 | va_start(ap, p); |
1977 | vfprintf(stderr, p, ap); |
1978 | va_end(ap); |
86256dc6 |
1979 | fprintf(stderr, "\n try typing \"psftp -h\" for help\n"); |
c0a81592 |
1980 | exit(1); |
1981 | } |
1982 | |
fa3db767 |
1983 | /* |
1984 | * Main program. Parse arguments etc. |
1985 | */ |
1986 | int main(int argc, char *argv[]) |
1987 | { |
1988 | int i; |
1989 | int portnumber = 0; |
1990 | char *userhost, *user; |
1991 | int mode = 0; |
1992 | int modeflags = 0; |
1993 | char *batchfile = NULL; |
86256dc6 |
1994 | int errors = 0; |
fa3db767 |
1995 | |
c44bf5bd |
1996 | flags = FLAG_STDERR | FLAG_INTERACTIVE | FLAG_SYNCAGENT; |
c0a81592 |
1997 | cmdline_tooltype = TOOLTYPE_FILETRANSFER; |
ff2ae367 |
1998 | ssh_get_line = &console_get_line; |
fa3db767 |
1999 | init_winsock(); |
2000 | sk_init(); |
2001 | |
2002 | userhost = user = NULL; |
2003 | |
86256dc6 |
2004 | errors = 0; |
fa3db767 |
2005 | for (i = 1; i < argc; i++) { |
c0a81592 |
2006 | int ret; |
fa3db767 |
2007 | if (argv[i][0] != '-') { |
c0a81592 |
2008 | if (userhost) |
2009 | usage(); |
2010 | else |
2011 | userhost = dupstr(argv[i]); |
2012 | continue; |
2013 | } |
5555d393 |
2014 | ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, &cfg); |
c0a81592 |
2015 | if (ret == -2) { |
2016 | cmdline_error("option \"%s\" requires an argument", argv[i]); |
2017 | } else if (ret == 2) { |
2018 | i++; /* skip next argument */ |
2019 | } else if (ret == 1) { |
2020 | /* We have our own verbosity in addition to `flags'. */ |
2021 | if (flags & FLAG_VERBOSE) |
2022 | verbose = 1; |
fa3db767 |
2023 | } else if (strcmp(argv[i], "-h") == 0 || |
2024 | strcmp(argv[i], "-?") == 0) { |
2025 | usage(); |
c0a81592 |
2026 | } else if (strcmp(argv[i], "-batch") == 0) { |
2027 | console_batch_mode = 1; |
fa3db767 |
2028 | } else if (strcmp(argv[i], "-b") == 0 && i + 1 < argc) { |
2029 | mode = 1; |
2030 | batchfile = argv[++i]; |
d13c2ee9 |
2031 | } else if (strcmp(argv[i], "-bc") == 0) { |
fa3db767 |
2032 | modeflags = modeflags | 1; |
d13c2ee9 |
2033 | } else if (strcmp(argv[i], "-be") == 0) { |
fa3db767 |
2034 | modeflags = modeflags | 2; |
2035 | } else if (strcmp(argv[i], "--") == 0) { |
2036 | i++; |
2037 | break; |
2038 | } else { |
86256dc6 |
2039 | cmdline_error("unknown option \"%s\"", argv[i]); |
fa3db767 |
2040 | } |
2041 | } |
2042 | argc -= i; |
2043 | argv += i; |
2044 | back = NULL; |
2045 | |
2046 | /* |
2047 | * If a user@host string has already been provided, connect to |
2048 | * it now. |
2049 | */ |
2050 | if (userhost) { |
2051 | if (psftp_connect(userhost, user, portnumber)) |
2052 | return 1; |
774204f5 |
2053 | if (do_sftp_init()) |
2054 | return 1; |
fa3db767 |
2055 | } else { |
2056 | printf("psftp: no hostname specified; use \"open host.name\"" |
2057 | " to connect\n"); |
2058 | } |
4c7f0d61 |
2059 | |
9954aaa3 |
2060 | do_sftp(mode, modeflags, batchfile); |
4a8fc3c4 |
2061 | |
51470298 |
2062 | if (back != NULL && back->socket(backhandle) != NULL) { |
4a8fc3c4 |
2063 | char ch; |
51470298 |
2064 | back->special(backhandle, TS_EOF); |
4a8fc3c4 |
2065 | sftp_recvdata(&ch, 1); |
2066 | } |
2067 | WSACleanup(); |
2068 | random_save_seed(); |
2069 | |
4c7f0d61 |
2070 | return 0; |
2071 | } |