4c7f0d61 |
1 | /* |
f4ff9455 |
2 | * psftp.c: (platform-independent) front end for PSFTP. |
4c7f0d61 |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
f9e162aa |
7 | #include <stdarg.h> |
4c7f0d61 |
8 | #include <assert.h> |
d92624dc |
9 | #include <limits.h> |
4c7f0d61 |
10 | |
4a8fc3c4 |
11 | #define PUTTY_DO_GLOBALS |
12 | #include "putty.h" |
d6cc41e6 |
13 | #include "psftp.h" |
4a8fc3c4 |
14 | #include "storage.h" |
15 | #include "ssh.h" |
4c7f0d61 |
16 | #include "sftp.h" |
17 | #include "int64.h" |
18 | |
5471d09a |
19 | /* |
20 | * Since SFTP is a request-response oriented protocol, it requires |
21 | * no buffer management: when we send data, we stop and wait for an |
22 | * acknowledgement _anyway_, and so we can't possibly overfill our |
23 | * send buffer. |
24 | */ |
25 | |
fa3db767 |
26 | static int psftp_connect(char *userhost, char *user, int portnumber); |
07534184 |
27 | static int do_sftp_init(void); |
679539d7 |
28 | void do_sftp_cleanup(); |
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 | |
e25883f8 |
131 | if (!canonname) { |
132 | /* Even that failed. Restore our best guess at the |
133 | * constructed filename and give up */ |
134 | fullname[i] = '/'; /* restore slash and last component */ |
135 | return fullname; |
136 | } |
32874aea |
137 | |
138 | /* |
139 | * We have a canonical name for all but the last path |
140 | * component. Concatenate the last component and return. |
141 | */ |
142 | returnname = dupcat(canonname, |
143 | canonname[strlen(canonname) - 1] == |
144 | '/' ? "" : "/", fullname + i + 1, NULL); |
145 | sfree(fullname); |
146 | sfree(canonname); |
147 | return returnname; |
50d7e054 |
148 | } |
4c7f0d61 |
149 | } |
150 | |
dcf8495c |
151 | /* |
152 | * Return a pointer to the portion of str that comes after the last |
153 | * slash (or backslash or colon, if `local' is TRUE). |
154 | */ |
155 | static char *stripslashes(char *str, int local) |
156 | { |
157 | char *p; |
158 | |
159 | if (local) { |
160 | p = strchr(str, ':'); |
161 | if (p) str = p+1; |
162 | } |
163 | |
164 | p = strrchr(str, '/'); |
165 | if (p) str = p+1; |
166 | |
167 | if (local) { |
168 | p = strrchr(str, '\\'); |
169 | if (p) str = p+1; |
170 | } |
171 | |
172 | return str; |
173 | } |
174 | |
4c7f0d61 |
175 | /* |
93e86a8b |
176 | * qsort comparison routine for fxp_name structures. Sorts by real |
177 | * file name. |
4c7f0d61 |
178 | */ |
93e86a8b |
179 | static int sftp_name_compare(const void *av, const void *bv) |
32874aea |
180 | { |
7d2c1789 |
181 | const struct fxp_name *const *a = (const struct fxp_name *const *) av; |
182 | const struct fxp_name *const *b = (const struct fxp_name *const *) bv; |
183 | return strcmp((*a)->filename, (*b)->filename); |
4c7f0d61 |
184 | } |
4c7f0d61 |
185 | |
186 | /* |
93e86a8b |
187 | * Likewise, but for a bare char *. |
4f2b387f |
188 | */ |
93e86a8b |
189 | static int bare_name_compare(const void *av, const void *bv) |
4f2b387f |
190 | { |
93e86a8b |
191 | const char **a = (const char **) av; |
192 | const char **b = (const char **) bv; |
193 | return strcmp(*a, *b); |
4f2b387f |
194 | } |
195 | |
38f0c08e |
196 | static void not_connected(void) |
197 | { |
198 | printf("psftp: not connected to a host; use \"open host.name\"\n"); |
199 | } |
200 | |
93e86a8b |
201 | /* ---------------------------------------------------------------------- |
202 | * The meat of the `get' and `put' commands. |
4c7f0d61 |
203 | */ |
5079ee6d |
204 | int sftp_get_file(char *fname, char *outfname, int recurse, int restart) |
32874aea |
205 | { |
4c7f0d61 |
206 | struct fxp_handle *fh; |
1bc24185 |
207 | struct sftp_packet *pktin; |
208 | struct sftp_request *req, *rreq; |
c606c42d |
209 | struct fxp_xfer *xfer; |
4c7f0d61 |
210 | uint64 offset; |
0ac1920c |
211 | WFile *file; |
479fe1ba |
212 | int ret, shown_err = FALSE; |
4c7f0d61 |
213 | |
93e86a8b |
214 | /* |
215 | * In recursive mode, see if we're dealing with a directory. |
216 | * (If we're not in recursive mode, we need not even check: the |
217 | * subsequent FXP_OPEN will return a usable error message.) |
218 | */ |
5079ee6d |
219 | if (recurse) { |
93e86a8b |
220 | struct fxp_attrs attrs; |
221 | int result; |
fa3db767 |
222 | |
5079ee6d |
223 | sftp_register(req = fxp_stat_send(fname)); |
224 | rreq = sftp_find_request(pktin = sftp_recv()); |
225 | assert(rreq == req); |
226 | result = fxp_stat_recv(pktin, rreq, &attrs); |
9c77ddf6 |
227 | |
5079ee6d |
228 | if (result && |
229 | (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) && |
230 | (attrs.permissions & 0040000)) { |
93e86a8b |
231 | |
232 | struct fxp_handle *dirhandle; |
233 | int nnames, namesize; |
234 | struct fxp_name **ournames; |
235 | struct fxp_names *names; |
236 | int i; |
237 | |
238 | /* |
239 | * First, attempt to create the destination directory, |
5079ee6d |
240 | * unless it already exists. |
93e86a8b |
241 | */ |
5079ee6d |
242 | if (file_type(outfname) != FILE_TYPE_DIRECTORY && |
93e86a8b |
243 | !create_directory(outfname)) { |
244 | printf("%s: Cannot create directory\n", outfname); |
245 | return 0; |
246 | } |
4c7f0d61 |
247 | |
93e86a8b |
248 | /* |
249 | * Now get the list of filenames in the remote |
250 | * directory. |
251 | */ |
252 | sftp_register(req = fxp_opendir_send(fname)); |
253 | rreq = sftp_find_request(pktin = sftp_recv()); |
254 | assert(rreq == req); |
255 | dirhandle = fxp_opendir_recv(pktin, rreq); |
256 | |
257 | if (!dirhandle) { |
258 | printf("%s: unable to open directory: %s\n", |
259 | fname, fxp_error()); |
260 | return 0; |
261 | } |
262 | nnames = namesize = 0; |
263 | ournames = NULL; |
264 | while (1) { |
265 | int i; |
266 | |
267 | sftp_register(req = fxp_readdir_send(dirhandle)); |
268 | rreq = sftp_find_request(pktin = sftp_recv()); |
269 | assert(rreq == req); |
270 | names = fxp_readdir_recv(pktin, rreq); |
271 | |
272 | if (names == NULL) { |
273 | if (fxp_error_type() == SSH_FX_EOF) |
274 | break; |
275 | printf("%s: reading directory: %s\n", fname, fxp_error()); |
276 | sfree(ournames); |
277 | return 0; |
278 | } |
279 | if (names->nnames == 0) { |
280 | fxp_free_names(names); |
281 | break; |
282 | } |
283 | if (nnames + names->nnames >= namesize) { |
284 | namesize += names->nnames + 128; |
285 | ournames = sresize(ournames, namesize, struct fxp_name *); |
286 | } |
287 | for (i = 0; i < names->nnames; i++) |
e9d14678 |
288 | if (strcmp(names->names[i].filename, ".") && |
5079ee6d |
289 | strcmp(names->names[i].filename, "..")) { |
e9d14678 |
290 | if (!vet_filename(names->names[i].filename)) { |
291 | printf("ignoring potentially dangerous server-" |
292 | "supplied filename '%s'\n", |
293 | names->names[i].filename); |
294 | } else { |
295 | ournames[nnames++] = |
296 | fxp_dup_name(&names->names[i]); |
297 | } |
298 | } |
93e86a8b |
299 | fxp_free_names(names); |
300 | } |
301 | sftp_register(req = fxp_close_send(dirhandle)); |
302 | rreq = sftp_find_request(pktin = sftp_recv()); |
303 | assert(rreq == req); |
304 | fxp_close_recv(pktin, rreq); |
305 | |
306 | /* |
307 | * Sort the names into a clear order. This ought to |
308 | * make things more predictable when we're doing a |
309 | * reget of the same directory, just in case two |
310 | * readdirs on the same remote directory return a |
311 | * different order. |
312 | */ |
313 | qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare); |
314 | |
315 | /* |
316 | * If we're in restart mode, find the last filename on |
317 | * this list that already exists. We may have to do a |
318 | * reget on _that_ file, but shouldn't have to do |
319 | * anything on the previous files. |
320 | * |
321 | * If none of them exists, of course, we start at 0. |
322 | */ |
323 | i = 0; |
60f9c221 |
324 | if (restart) { |
325 | while (i < nnames) { |
326 | char *nextoutfname; |
327 | int ret; |
328 | if (outfname) |
329 | nextoutfname = dir_file_cat(outfname, |
330 | ournames[i]->filename); |
331 | else |
332 | nextoutfname = dupstr(ournames[i]->filename); |
333 | ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT); |
334 | sfree(nextoutfname); |
335 | if (ret) |
336 | break; |
337 | i++; |
338 | } |
339 | if (i > 0) |
340 | i--; |
341 | } |
93e86a8b |
342 | |
343 | /* |
344 | * Now we're ready to recurse. Starting at ournames[i] |
345 | * and continuing on to the end of the list, we |
346 | * construct a new source and target file name, and |
347 | * call sftp_get_file again. |
348 | */ |
349 | for (; i < nnames; i++) { |
350 | char *nextfname, *nextoutfname; |
351 | int ret; |
352 | |
353 | nextfname = dupcat(fname, "/", ournames[i]->filename, NULL); |
9c77ddf6 |
354 | if (outfname) |
355 | nextoutfname = dir_file_cat(outfname, |
356 | ournames[i]->filename); |
357 | else |
358 | nextoutfname = dupstr(ournames[i]->filename); |
5079ee6d |
359 | ret = sftp_get_file(nextfname, nextoutfname, recurse, restart); |
93e86a8b |
360 | restart = FALSE; /* after first partial file, do full */ |
361 | sfree(nextoutfname); |
362 | sfree(nextfname); |
363 | if (!ret) { |
364 | for (i = 0; i < nnames; i++) { |
365 | fxp_free_name(ournames[i]); |
366 | } |
367 | sfree(ournames); |
368 | return 0; |
369 | } |
370 | } |
371 | |
372 | /* |
373 | * Done this recursion level. Free everything. |
374 | */ |
375 | for (i = 0; i < nnames; i++) { |
376 | fxp_free_name(ournames[i]); |
377 | } |
378 | sfree(ournames); |
379 | |
380 | return 1; |
381 | } |
4c7f0d61 |
382 | } |
4c7f0d61 |
383 | |
1bc24185 |
384 | sftp_register(req = fxp_open_send(fname, SSH_FXF_READ)); |
385 | rreq = sftp_find_request(pktin = sftp_recv()); |
386 | assert(rreq == req); |
7b7de4f4 |
387 | fh = fxp_open_recv(pktin, rreq); |
1bc24185 |
388 | |
4c7f0d61 |
389 | if (!fh) { |
4dc846cd |
390 | printf("%s: open for read: %s\n", fname, fxp_error()); |
4c7f0d61 |
391 | return 0; |
392 | } |
d92624dc |
393 | |
394 | if (restart) { |
0ac1920c |
395 | file = open_existing_wfile(outfname, NULL); |
d92624dc |
396 | } else { |
0ac1920c |
397 | file = open_new_file(outfname); |
d92624dc |
398 | } |
399 | |
0ac1920c |
400 | if (!file) { |
4c7f0d61 |
401 | printf("local: unable to open %s\n", outfname); |
1bc24185 |
402 | |
403 | sftp_register(req = fxp_close_send(fh)); |
404 | rreq = sftp_find_request(pktin = sftp_recv()); |
405 | assert(rreq == req); |
7b7de4f4 |
406 | fxp_close_recv(pktin, rreq); |
1bc24185 |
407 | |
4c7f0d61 |
408 | return 0; |
409 | } |
410 | |
d92624dc |
411 | if (restart) { |
0ac1920c |
412 | char decbuf[30]; |
413 | if (seek_file(file, uint64_make(0,0) , FROM_END) == -1) { |
20c2ef84 |
414 | close_wfile(file); |
0ac1920c |
415 | printf("reget: cannot restart %s - file too large\n", |
416 | outfname); |
20c2ef84 |
417 | sftp_register(req = fxp_close_send(fh)); |
418 | rreq = sftp_find_request(pktin = sftp_recv()); |
419 | assert(rreq == req); |
420 | fxp_close_recv(pktin, rreq); |
0ac1920c |
421 | |
20c2ef84 |
422 | return 0; |
0ac1920c |
423 | } |
424 | |
425 | offset = get_file_posn(file); |
426 | uint64_decimal(offset, decbuf); |
427 | printf("reget: restarting at file position %s\n", decbuf); |
d92624dc |
428 | } else { |
429 | offset = uint64_make(0, 0); |
430 | } |
4c7f0d61 |
431 | |
d92624dc |
432 | printf("remote:%s => local:%s\n", fname, outfname); |
4c7f0d61 |
433 | |
434 | /* |
435 | * FIXME: we can use FXP_FSTAT here to get the file size, and |
436 | * thus put up a progress bar. |
437 | */ |
df49ff19 |
438 | ret = 1; |
c606c42d |
439 | xfer = xfer_download_init(fh, offset); |
df0870fc |
440 | while (!xfer_done(xfer)) { |
c606c42d |
441 | void *vbuf; |
442 | int ret, len; |
4c7f0d61 |
443 | int wpos, wlen; |
444 | |
c606c42d |
445 | xfer_download_queue(xfer); |
446 | pktin = sftp_recv(); |
447 | ret = xfer_download_gotpkt(xfer, pktin); |
448 | |
449 | if (ret < 0) { |
479fe1ba |
450 | if (!shown_err) { |
451 | printf("error while reading: %s\n", fxp_error()); |
452 | shown_err = TRUE; |
453 | } |
c606c42d |
454 | ret = 0; |
4c7f0d61 |
455 | } |
32874aea |
456 | |
c606c42d |
457 | while (xfer_download_data(xfer, &vbuf, &len)) { |
458 | unsigned char *buf = (unsigned char *)vbuf; |
459 | |
460 | wpos = 0; |
461 | while (wpos < len) { |
0ac1920c |
462 | wlen = write_to_file(file, buf + wpos, len - wpos); |
c606c42d |
463 | if (wlen <= 0) { |
464 | printf("error while writing local file\n"); |
465 | ret = 0; |
466 | xfer_set_error(xfer); |
51618e4b |
467 | break; |
c606c42d |
468 | } |
469 | wpos += wlen; |
470 | } |
471 | if (wpos < len) { /* we had an error */ |
df49ff19 |
472 | ret = 0; |
c606c42d |
473 | xfer_set_error(xfer); |
4c7f0d61 |
474 | } |
9ff4e23d |
475 | |
476 | sfree(vbuf); |
df49ff19 |
477 | } |
4c7f0d61 |
478 | } |
479 | |
c606c42d |
480 | xfer_cleanup(xfer); |
481 | |
0ac1920c |
482 | close_wfile(file); |
1bc24185 |
483 | |
484 | sftp_register(req = fxp_close_send(fh)); |
485 | rreq = sftp_find_request(pktin = sftp_recv()); |
486 | assert(rreq == req); |
7b7de4f4 |
487 | fxp_close_recv(pktin, rreq); |
1bc24185 |
488 | |
df49ff19 |
489 | return ret; |
4c7f0d61 |
490 | } |
491 | |
5079ee6d |
492 | int sftp_put_file(char *fname, char *outfname, int recurse, int restart) |
32874aea |
493 | { |
4c7f0d61 |
494 | struct fxp_handle *fh; |
df0870fc |
495 | struct fxp_xfer *xfer; |
1bc24185 |
496 | struct sftp_packet *pktin; |
497 | struct sftp_request *req, *rreq; |
4c7f0d61 |
498 | uint64 offset; |
0ac1920c |
499 | RFile *file; |
df0870fc |
500 | int ret, err, eof; |
4c7f0d61 |
501 | |
93e86a8b |
502 | /* |
503 | * In recursive mode, see if we're dealing with a directory. |
504 | * (If we're not in recursive mode, we need not even check: the |
505 | * subsequent fopen will return an error message.) |
506 | */ |
5079ee6d |
507 | if (recurse && file_type(fname) == FILE_TYPE_DIRECTORY) { |
93e86a8b |
508 | struct fxp_attrs attrs; |
509 | int result; |
510 | int nnames, namesize; |
511 | char *name, **ournames; |
512 | DirHandle *dh; |
513 | int i; |
4c7f0d61 |
514 | |
5079ee6d |
515 | /* |
516 | * First, attempt to create the destination directory, |
517 | * unless it already exists. |
518 | */ |
519 | sftp_register(req = fxp_stat_send(outfname)); |
520 | rreq = sftp_find_request(pktin = sftp_recv()); |
521 | assert(rreq == req); |
522 | result = fxp_stat_recv(pktin, rreq, &attrs); |
523 | if (!result || |
524 | !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) || |
525 | !(attrs.permissions & 0040000)) { |
526 | sftp_register(req = fxp_mkdir_send(outfname)); |
93e86a8b |
527 | rreq = sftp_find_request(pktin = sftp_recv()); |
528 | assert(rreq == req); |
5079ee6d |
529 | result = fxp_mkdir_recv(pktin, rreq); |
93e86a8b |
530 | |
5079ee6d |
531 | if (!result) { |
532 | printf("%s: create directory: %s\n", |
533 | outfname, fxp_error()); |
534 | return 0; |
93e86a8b |
535 | } |
536 | } |
537 | |
538 | /* |
539 | * Now get the list of filenames in the local directory. |
540 | */ |
93e86a8b |
541 | nnames = namesize = 0; |
542 | ournames = NULL; |
9c77ddf6 |
543 | |
5079ee6d |
544 | dh = open_directory(fname); |
545 | if (!dh) { |
546 | printf("%s: unable to open directory\n", fname); |
547 | return 0; |
9c77ddf6 |
548 | } |
5079ee6d |
549 | while ((name = read_filename(dh)) != NULL) { |
550 | if (nnames >= namesize) { |
551 | namesize += 128; |
552 | ournames = sresize(ournames, namesize, char *); |
553 | } |
554 | ournames[nnames++] = name; |
93e86a8b |
555 | } |
5079ee6d |
556 | close_directory(dh); |
93e86a8b |
557 | |
558 | /* |
559 | * Sort the names into a clear order. This ought to make |
560 | * things more predictable when we're doing a reput of the |
561 | * same directory, just in case two readdirs on the same |
562 | * local directory return a different order. |
563 | */ |
564 | qsort(ournames, nnames, sizeof(*ournames), bare_name_compare); |
565 | |
566 | /* |
567 | * If we're in restart mode, find the last filename on this |
568 | * list that already exists. We may have to do a reput on |
569 | * _that_ file, but shouldn't have to do anything on the |
570 | * previous files. |
571 | * |
572 | * If none of them exists, of course, we start at 0. |
573 | */ |
574 | i = 0; |
60f9c221 |
575 | if (restart) { |
576 | while (i < nnames) { |
577 | char *nextoutfname; |
578 | nextoutfname = dupcat(outfname, "/", ournames[i], NULL); |
579 | sftp_register(req = fxp_stat_send(nextoutfname)); |
580 | rreq = sftp_find_request(pktin = sftp_recv()); |
581 | assert(rreq == req); |
582 | result = fxp_stat_recv(pktin, rreq, &attrs); |
583 | sfree(nextoutfname); |
584 | if (!result) |
585 | break; |
586 | i++; |
587 | } |
588 | if (i > 0) |
589 | i--; |
590 | } |
591 | |
592 | /* |
593 | * Now we're ready to recurse. Starting at ournames[i] |
93e86a8b |
594 | * and continuing on to the end of the list, we |
595 | * construct a new source and target file name, and |
596 | * call sftp_put_file again. |
597 | */ |
598 | for (; i < nnames; i++) { |
599 | char *nextfname, *nextoutfname; |
600 | int ret; |
601 | |
9c77ddf6 |
602 | if (fname) |
603 | nextfname = dir_file_cat(fname, ournames[i]); |
604 | else |
605 | nextfname = dupstr(ournames[i]); |
93e86a8b |
606 | nextoutfname = dupcat(outfname, "/", ournames[i], NULL); |
5079ee6d |
607 | ret = sftp_put_file(nextfname, nextoutfname, recurse, restart); |
93e86a8b |
608 | restart = FALSE; /* after first partial file, do full */ |
609 | sfree(nextoutfname); |
610 | sfree(nextfname); |
611 | if (!ret) { |
612 | for (i = 0; i < nnames; i++) { |
613 | sfree(ournames[i]); |
614 | } |
615 | sfree(ournames); |
616 | return 0; |
617 | } |
618 | } |
619 | |
620 | /* |
621 | * Done this recursion level. Free everything. |
622 | */ |
623 | for (i = 0; i < nnames; i++) { |
624 | sfree(ournames[i]); |
625 | } |
626 | sfree(ournames); |
627 | |
628 | return 1; |
4c7f0d61 |
629 | } |
630 | |
0ac1920c |
631 | file = open_existing_file(fname, NULL, NULL, NULL); |
632 | if (!file) { |
4c7f0d61 |
633 | printf("local: unable to open %s\n", fname); |
4c7f0d61 |
634 | return 0; |
635 | } |
d92624dc |
636 | if (restart) { |
1bc24185 |
637 | sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE)); |
d92624dc |
638 | } else { |
1bc24185 |
639 | sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE | |
640 | SSH_FXF_CREAT | SSH_FXF_TRUNC)); |
d92624dc |
641 | } |
1bc24185 |
642 | rreq = sftp_find_request(pktin = sftp_recv()); |
643 | assert(rreq == req); |
7b7de4f4 |
644 | fh = fxp_open_recv(pktin, rreq); |
1bc24185 |
645 | |
4c7f0d61 |
646 | if (!fh) { |
20c2ef84 |
647 | close_rfile(file); |
4dc846cd |
648 | printf("%s: open for write: %s\n", outfname, fxp_error()); |
4c7f0d61 |
649 | return 0; |
650 | } |
651 | |
d92624dc |
652 | if (restart) { |
653 | char decbuf[30]; |
654 | struct fxp_attrs attrs; |
1bc24185 |
655 | int ret; |
656 | |
657 | sftp_register(req = fxp_fstat_send(fh)); |
658 | rreq = sftp_find_request(pktin = sftp_recv()); |
659 | assert(rreq == req); |
7b7de4f4 |
660 | ret = fxp_fstat_recv(pktin, rreq, &attrs); |
1bc24185 |
661 | |
662 | if (!ret) { |
20c2ef84 |
663 | close_rfile(file); |
d92624dc |
664 | printf("read size of %s: %s\n", outfname, fxp_error()); |
d92624dc |
665 | return 0; |
666 | } |
667 | if (!(attrs.flags & SSH_FILEXFER_ATTR_SIZE)) { |
20c2ef84 |
668 | close_rfile(file); |
d92624dc |
669 | printf("read size of %s: size was not given\n", outfname); |
d92624dc |
670 | return 0; |
671 | } |
672 | offset = attrs.size; |
673 | uint64_decimal(offset, decbuf); |
674 | printf("reput: restarting at file position %s\n", decbuf); |
0ac1920c |
675 | |
676 | if (seek_file((WFile *)file, offset, FROM_START) != 0) |
677 | seek_file((WFile *)file, uint64_make(0,0), FROM_END); /* *shrug* */ |
d92624dc |
678 | } else { |
679 | offset = uint64_make(0, 0); |
680 | } |
4c7f0d61 |
681 | |
d92624dc |
682 | printf("local:%s => remote:%s\n", fname, outfname); |
4c7f0d61 |
683 | |
684 | /* |
685 | * FIXME: we can use FXP_FSTAT here to get the file size, and |
686 | * thus put up a progress bar. |
687 | */ |
df49ff19 |
688 | ret = 1; |
df0870fc |
689 | xfer = xfer_upload_init(fh, offset); |
690 | err = eof = 0; |
691 | while ((!err && !eof) || !xfer_done(xfer)) { |
4c7f0d61 |
692 | char buffer[4096]; |
1bc24185 |
693 | int len, ret; |
4c7f0d61 |
694 | |
df0870fc |
695 | while (xfer_upload_ready(xfer) && !err && !eof) { |
0ac1920c |
696 | len = read_from_file(file, buffer, sizeof(buffer)); |
df0870fc |
697 | if (len == -1) { |
698 | printf("error while reading local file\n"); |
699 | err = 1; |
700 | } else if (len == 0) { |
701 | eof = 1; |
702 | } else { |
703 | xfer_upload_data(xfer, buffer, len); |
704 | } |
4c7f0d61 |
705 | } |
1bc24185 |
706 | |
2dbd915a |
707 | if (!xfer_done(xfer)) { |
708 | pktin = sftp_recv(); |
709 | ret = xfer_upload_gotpkt(xfer, pktin); |
0bd57c0f |
710 | if (ret <= 0 && !err) { |
2dbd915a |
711 | printf("error while writing: %s\n", fxp_error()); |
712 | err = 1; |
713 | } |
4c7f0d61 |
714 | } |
4c7f0d61 |
715 | } |
716 | |
df0870fc |
717 | xfer_cleanup(xfer); |
718 | |
1bc24185 |
719 | sftp_register(req = fxp_close_send(fh)); |
720 | rreq = sftp_find_request(pktin = sftp_recv()); |
721 | assert(rreq == req); |
7b7de4f4 |
722 | fxp_close_recv(pktin, rreq); |
1bc24185 |
723 | |
0ac1920c |
724 | close_rfile(file); |
93e86a8b |
725 | |
726 | return ret; |
727 | } |
728 | |
729 | /* ---------------------------------------------------------------------- |
5079ee6d |
730 | * A remote wildcard matcher, providing a similar interface to the |
731 | * local one in psftp.h. |
732 | */ |
733 | |
734 | typedef struct SftpWildcardMatcher { |
735 | struct fxp_handle *dirh; |
736 | struct fxp_names *names; |
737 | int namepos; |
738 | char *wildcard, *prefix; |
739 | } SftpWildcardMatcher; |
740 | |
741 | SftpWildcardMatcher *sftp_begin_wildcard_matching(char *name) |
742 | { |
743 | struct sftp_packet *pktin; |
744 | struct sftp_request *req, *rreq; |
745 | char *wildcard; |
746 | char *unwcdir, *tmpdir, *cdir; |
747 | int len, check; |
748 | SftpWildcardMatcher *swcm; |
749 | struct fxp_handle *dirh; |
750 | |
751 | /* |
752 | * We don't handle multi-level wildcards; so we expect to find |
753 | * a fully specified directory part, followed by a wildcard |
754 | * after that. |
755 | */ |
756 | wildcard = stripslashes(name, 0); |
757 | |
758 | unwcdir = dupstr(name); |
759 | len = wildcard - name; |
760 | unwcdir[len] = '\0'; |
761 | if (len > 0 && unwcdir[len-1] == '/') |
762 | unwcdir[len-1] = '\0'; |
763 | tmpdir = snewn(1 + len, char); |
764 | check = wc_unescape(tmpdir, unwcdir); |
765 | sfree(tmpdir); |
766 | |
767 | if (!check) { |
768 | printf("Multiple-level wildcards are not supported\n"); |
769 | sfree(unwcdir); |
770 | return NULL; |
771 | } |
772 | |
773 | cdir = canonify(unwcdir); |
774 | |
775 | sftp_register(req = fxp_opendir_send(cdir)); |
776 | rreq = sftp_find_request(pktin = sftp_recv()); |
777 | assert(rreq == req); |
778 | dirh = fxp_opendir_recv(pktin, rreq); |
779 | |
780 | if (dirh) { |
781 | swcm = snew(SftpWildcardMatcher); |
782 | swcm->dirh = dirh; |
783 | swcm->names = NULL; |
784 | swcm->wildcard = dupstr(wildcard); |
785 | swcm->prefix = unwcdir; |
786 | } else { |
787 | printf("Unable to open %s: %s\n", cdir, fxp_error()); |
788 | swcm = NULL; |
789 | sfree(unwcdir); |
790 | } |
791 | |
792 | sfree(cdir); |
793 | |
794 | return swcm; |
795 | } |
796 | |
797 | char *sftp_wildcard_get_filename(SftpWildcardMatcher *swcm) |
798 | { |
799 | struct fxp_name *name; |
800 | struct sftp_packet *pktin; |
801 | struct sftp_request *req, *rreq; |
802 | |
803 | while (1) { |
804 | if (swcm->names && swcm->namepos >= swcm->names->nnames) { |
805 | fxp_free_names(swcm->names); |
806 | swcm->names = NULL; |
807 | } |
808 | |
809 | if (!swcm->names) { |
810 | sftp_register(req = fxp_readdir_send(swcm->dirh)); |
811 | rreq = sftp_find_request(pktin = sftp_recv()); |
812 | assert(rreq == req); |
813 | swcm->names = fxp_readdir_recv(pktin, rreq); |
814 | |
815 | if (!swcm->names) { |
816 | if (fxp_error_type() != SSH_FX_EOF) |
817 | printf("%s: reading directory: %s\n", swcm->prefix, |
818 | fxp_error()); |
819 | return NULL; |
820 | } |
821 | |
822 | swcm->namepos = 0; |
823 | } |
824 | |
825 | assert(swcm->names && swcm->namepos < swcm->names->nnames); |
826 | |
827 | name = &swcm->names->names[swcm->namepos++]; |
828 | |
829 | if (!strcmp(name->filename, ".") || !strcmp(name->filename, "..")) |
830 | continue; /* expected bad filenames */ |
831 | |
832 | if (!vet_filename(name->filename)) { |
833 | printf("ignoring potentially dangerous server-" |
834 | "supplied filename '%s'\n", name->filename); |
835 | continue; /* unexpected bad filename */ |
836 | } |
837 | |
838 | if (!wc_match(swcm->wildcard, name->filename)) |
839 | continue; /* doesn't match the wildcard */ |
840 | |
841 | /* |
842 | * We have a working filename. Return it. |
843 | */ |
844 | return dupprintf("%s%s%s", swcm->prefix, |
83567e43 |
845 | (!swcm->prefix[0] || |
846 | swcm->prefix[strlen(swcm->prefix)-1]=='/' ? |
847 | "" : "/"), |
5079ee6d |
848 | name->filename); |
849 | } |
850 | } |
851 | |
852 | void sftp_finish_wildcard_matching(SftpWildcardMatcher *swcm) |
853 | { |
854 | struct sftp_packet *pktin; |
855 | struct sftp_request *req, *rreq; |
856 | |
857 | sftp_register(req = fxp_close_send(swcm->dirh)); |
858 | rreq = sftp_find_request(pktin = sftp_recv()); |
859 | assert(rreq == req); |
860 | fxp_close_recv(pktin, rreq); |
861 | |
862 | if (swcm->names) |
863 | fxp_free_names(swcm->names); |
864 | |
865 | sfree(swcm->prefix); |
866 | sfree(swcm->wildcard); |
867 | |
868 | sfree(swcm); |
869 | } |
870 | |
83567e43 |
871 | /* |
872 | * General function to match a potential wildcard in a filename |
873 | * argument and iterate over every matching file. Used in several |
874 | * PSFTP commands (rmdir, rm, chmod, mv). |
875 | */ |
876 | int wildcard_iterate(char *filename, int (*func)(void *, char *), void *ctx) |
877 | { |
878 | char *unwcfname, *newname, *cname; |
879 | int is_wc, ret; |
880 | |
881 | unwcfname = snewn(strlen(filename)+1, char); |
882 | is_wc = !wc_unescape(unwcfname, filename); |
883 | |
884 | if (is_wc) { |
885 | SftpWildcardMatcher *swcm = sftp_begin_wildcard_matching(filename); |
886 | int matched = FALSE; |
887 | sfree(unwcfname); |
888 | |
889 | if (!swcm) |
890 | return 0; |
891 | |
892 | ret = 1; |
893 | |
894 | while ( (newname = sftp_wildcard_get_filename(swcm)) != NULL ) { |
895 | cname = canonify(newname); |
896 | if (!cname) { |
4dc846cd |
897 | printf("%s: canonify: %s\n", newname, fxp_error()); |
83567e43 |
898 | ret = 0; |
899 | } |
900 | matched = TRUE; |
901 | ret &= func(ctx, cname); |
902 | sfree(cname); |
903 | } |
904 | |
905 | if (!matched) { |
906 | /* Politely warn the user that nothing matched. */ |
907 | printf("%s: nothing matched\n", filename); |
908 | } |
909 | |
910 | sftp_finish_wildcard_matching(swcm); |
911 | } else { |
912 | cname = canonify(unwcfname); |
913 | if (!cname) { |
4dc846cd |
914 | printf("%s: canonify: %s\n", filename, fxp_error()); |
83567e43 |
915 | ret = 0; |
916 | } |
917 | ret = func(ctx, cname); |
918 | sfree(cname); |
919 | sfree(unwcfname); |
920 | } |
921 | |
922 | return ret; |
923 | } |
924 | |
925 | /* |
926 | * Handy helper function. |
927 | */ |
928 | int is_wildcard(char *name) |
929 | { |
930 | char *unwcfname = snewn(strlen(name)+1, char); |
931 | int is_wc = !wc_unescape(unwcfname, name); |
932 | sfree(unwcfname); |
933 | return is_wc; |
934 | } |
935 | |
5079ee6d |
936 | /* ---------------------------------------------------------------------- |
93e86a8b |
937 | * Actual sftp commands. |
938 | */ |
939 | struct sftp_command { |
940 | char **words; |
941 | int nwords, wordssize; |
942 | int (*obey) (struct sftp_command *); /* returns <0 to quit */ |
943 | }; |
944 | |
945 | int sftp_cmd_null(struct sftp_command *cmd) |
946 | { |
947 | return 1; /* success */ |
948 | } |
949 | |
950 | int sftp_cmd_unknown(struct sftp_command *cmd) |
951 | { |
952 | printf("psftp: unknown command \"%s\"\n", cmd->words[0]); |
953 | return 0; /* failure */ |
954 | } |
955 | |
956 | int sftp_cmd_quit(struct sftp_command *cmd) |
957 | { |
958 | return -1; |
959 | } |
960 | |
b614ce89 |
961 | int sftp_cmd_close(struct sftp_command *cmd) |
962 | { |
963 | if (back == NULL) { |
38f0c08e |
964 | not_connected(); |
b614ce89 |
965 | return 0; |
966 | } |
967 | |
6226c939 |
968 | if (back != NULL && back->connected(backhandle)) { |
b614ce89 |
969 | char ch; |
970 | back->special(backhandle, TS_EOF); |
971 | sftp_recvdata(&ch, 1); |
972 | } |
973 | do_sftp_cleanup(); |
974 | |
975 | return 0; |
976 | } |
977 | |
93e86a8b |
978 | /* |
979 | * List a directory. If no arguments are given, list pwd; otherwise |
980 | * list the directory given in words[1]. |
981 | */ |
982 | int sftp_cmd_ls(struct sftp_command *cmd) |
983 | { |
984 | struct fxp_handle *dirh; |
985 | struct fxp_names *names; |
986 | struct fxp_name **ournames; |
987 | int nnames, namesize; |
3394416c |
988 | char *dir, *cdir, *unwcdir, *wildcard; |
93e86a8b |
989 | struct sftp_packet *pktin; |
990 | struct sftp_request *req, *rreq; |
991 | int i; |
992 | |
993 | if (back == NULL) { |
38f0c08e |
994 | not_connected(); |
93e86a8b |
995 | return 0; |
996 | } |
997 | |
998 | if (cmd->nwords < 2) |
999 | dir = "."; |
1000 | else |
1001 | dir = cmd->words[1]; |
1002 | |
3394416c |
1003 | unwcdir = snewn(1 + strlen(dir), char); |
1004 | if (wc_unescape(unwcdir, dir)) { |
1005 | dir = unwcdir; |
1006 | wildcard = NULL; |
1007 | } else { |
1008 | char *tmpdir; |
1009 | int len, check; |
1010 | |
1011 | wildcard = stripslashes(dir, 0); |
1012 | unwcdir = dupstr(dir); |
1013 | len = wildcard - dir; |
1014 | unwcdir[len] = '\0'; |
1015 | if (len > 0 && unwcdir[len-1] == '/') |
1016 | unwcdir[len-1] = '\0'; |
1017 | tmpdir = snewn(1 + len, char); |
1018 | check = wc_unescape(tmpdir, unwcdir); |
1019 | sfree(tmpdir); |
1020 | if (!check) { |
1021 | printf("Multiple-level wildcards are not supported\n"); |
1022 | sfree(unwcdir); |
1023 | return 0; |
1024 | } |
1025 | dir = unwcdir; |
1026 | } |
1027 | |
93e86a8b |
1028 | cdir = canonify(dir); |
1029 | if (!cdir) { |
4dc846cd |
1030 | printf("%s: canonify: %s\n", dir, fxp_error()); |
3394416c |
1031 | sfree(unwcdir); |
93e86a8b |
1032 | return 0; |
1033 | } |
1034 | |
1035 | printf("Listing directory %s\n", cdir); |
1036 | |
1037 | sftp_register(req = fxp_opendir_send(cdir)); |
1038 | rreq = sftp_find_request(pktin = sftp_recv()); |
1039 | assert(rreq == req); |
1040 | dirh = fxp_opendir_recv(pktin, rreq); |
1041 | |
1042 | if (dirh == NULL) { |
1043 | printf("Unable to open %s: %s\n", dir, fxp_error()); |
1044 | } else { |
1045 | nnames = namesize = 0; |
1046 | ournames = NULL; |
1047 | |
1048 | while (1) { |
1049 | |
1050 | sftp_register(req = fxp_readdir_send(dirh)); |
1051 | rreq = sftp_find_request(pktin = sftp_recv()); |
1052 | assert(rreq == req); |
1053 | names = fxp_readdir_recv(pktin, rreq); |
1054 | |
1055 | if (names == NULL) { |
1056 | if (fxp_error_type() == SSH_FX_EOF) |
1057 | break; |
1058 | printf("Reading directory %s: %s\n", dir, fxp_error()); |
1059 | break; |
1060 | } |
1061 | if (names->nnames == 0) { |
1062 | fxp_free_names(names); |
1063 | break; |
1064 | } |
1065 | |
1066 | if (nnames + names->nnames >= namesize) { |
1067 | namesize += names->nnames + 128; |
1068 | ournames = sresize(ournames, namesize, struct fxp_name *); |
1069 | } |
1070 | |
1071 | for (i = 0; i < names->nnames; i++) |
3394416c |
1072 | if (!wildcard || wc_match(wildcard, names->names[i].filename)) |
1073 | ournames[nnames++] = fxp_dup_name(&names->names[i]); |
93e86a8b |
1074 | |
1075 | fxp_free_names(names); |
1076 | } |
1077 | sftp_register(req = fxp_close_send(dirh)); |
1078 | rreq = sftp_find_request(pktin = sftp_recv()); |
1079 | assert(rreq == req); |
1080 | fxp_close_recv(pktin, rreq); |
1081 | |
1082 | /* |
1083 | * Now we have our filenames. Sort them by actual file |
1084 | * name, and then output the longname parts. |
1085 | */ |
1086 | qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare); |
1087 | |
1088 | /* |
1089 | * And print them. |
1090 | */ |
1091 | for (i = 0; i < nnames; i++) { |
1092 | printf("%s\n", ournames[i]->longname); |
1093 | fxp_free_name(ournames[i]); |
1094 | } |
1095 | sfree(ournames); |
1096 | } |
1097 | |
1098 | sfree(cdir); |
3394416c |
1099 | sfree(unwcdir); |
93e86a8b |
1100 | |
1101 | return 1; |
1102 | } |
1103 | |
1104 | /* |
1105 | * Change directories. We do this by canonifying the new name, then |
1106 | * trying to OPENDIR it. Only if that succeeds do we set the new pwd. |
1107 | */ |
1108 | int sftp_cmd_cd(struct sftp_command *cmd) |
1109 | { |
1110 | struct fxp_handle *dirh; |
1111 | struct sftp_packet *pktin; |
1112 | struct sftp_request *req, *rreq; |
1113 | char *dir; |
1114 | |
1115 | if (back == NULL) { |
38f0c08e |
1116 | not_connected(); |
93e86a8b |
1117 | return 0; |
1118 | } |
1119 | |
1120 | if (cmd->nwords < 2) |
1121 | dir = dupstr(homedir); |
1122 | else |
1123 | dir = canonify(cmd->words[1]); |
1124 | |
1125 | if (!dir) { |
4dc846cd |
1126 | printf("%s: canonify: %s\n", dir, fxp_error()); |
93e86a8b |
1127 | return 0; |
1128 | } |
1129 | |
1130 | sftp_register(req = fxp_opendir_send(dir)); |
1131 | rreq = sftp_find_request(pktin = sftp_recv()); |
1132 | assert(rreq == req); |
1133 | dirh = fxp_opendir_recv(pktin, rreq); |
1134 | |
1135 | if (!dirh) { |
1136 | printf("Directory %s: %s\n", dir, fxp_error()); |
1137 | sfree(dir); |
1138 | return 0; |
1139 | } |
1140 | |
1141 | sftp_register(req = fxp_close_send(dirh)); |
1142 | rreq = sftp_find_request(pktin = sftp_recv()); |
1143 | assert(rreq == req); |
1144 | fxp_close_recv(pktin, rreq); |
1145 | |
1146 | sfree(pwd); |
1147 | pwd = dir; |
1148 | printf("Remote directory is now %s\n", pwd); |
1149 | |
1150 | return 1; |
1151 | } |
1152 | |
1153 | /* |
1154 | * Print current directory. Easy as pie. |
1155 | */ |
1156 | int sftp_cmd_pwd(struct sftp_command *cmd) |
1157 | { |
1158 | if (back == NULL) { |
38f0c08e |
1159 | not_connected(); |
93e86a8b |
1160 | return 0; |
1161 | } |
1162 | |
1163 | printf("Remote directory is %s\n", pwd); |
1164 | return 1; |
1165 | } |
1166 | |
1167 | /* |
9c77ddf6 |
1168 | * Get a file and save it at the local end. We have three very |
1169 | * similar commands here. The basic one is `get'; `reget' differs |
1170 | * in that it checks for the existence of the destination file and |
1171 | * starts from where a previous aborted transfer left off; `mget' |
1172 | * differs in that it interprets all its arguments as files to |
1173 | * transfer (never as a different local name for a remote file) and |
1174 | * can handle wildcards. |
93e86a8b |
1175 | */ |
9c77ddf6 |
1176 | int sftp_general_get(struct sftp_command *cmd, int restart, int multiple) |
93e86a8b |
1177 | { |
5079ee6d |
1178 | char *fname, *unwcfname, *origfname, *origwfname, *outfname; |
93e86a8b |
1179 | int i, ret; |
1180 | int recurse = FALSE; |
1181 | |
1182 | if (back == NULL) { |
38f0c08e |
1183 | not_connected(); |
93e86a8b |
1184 | return 0; |
1185 | } |
1186 | |
1187 | i = 1; |
1188 | while (i < cmd->nwords && cmd->words[i][0] == '-') { |
1189 | if (!strcmp(cmd->words[i], "--")) { |
1190 | /* finish processing options */ |
1191 | i++; |
1192 | break; |
1193 | } else if (!strcmp(cmd->words[i], "-r")) { |
1194 | recurse = TRUE; |
1195 | } else { |
9033711a |
1196 | printf("%s: unrecognised option '%s'\n", cmd->words[0], cmd->words[i]); |
93e86a8b |
1197 | return 0; |
1198 | } |
1199 | i++; |
1200 | } |
1201 | |
1202 | if (i >= cmd->nwords) { |
9033711a |
1203 | printf("%s: expects a filename\n", cmd->words[0]); |
93e86a8b |
1204 | return 0; |
1205 | } |
1206 | |
5079ee6d |
1207 | ret = 1; |
9c77ddf6 |
1208 | do { |
5079ee6d |
1209 | SftpWildcardMatcher *swcm; |
1210 | |
9c77ddf6 |
1211 | origfname = cmd->words[i++]; |
5079ee6d |
1212 | unwcfname = snewn(strlen(origfname)+1, char); |
93e86a8b |
1213 | |
5079ee6d |
1214 | if (multiple && !wc_unescape(unwcfname, origfname)) { |
1215 | swcm = sftp_begin_wildcard_matching(origfname); |
1216 | if (!swcm) { |
1217 | sfree(unwcfname); |
1218 | continue; |
1219 | } |
1220 | origwfname = sftp_wildcard_get_filename(swcm); |
1221 | if (!origwfname) { |
1222 | /* Politely warn the user that nothing matched. */ |
1223 | printf("%s: nothing matched\n", origfname); |
1224 | sftp_finish_wildcard_matching(swcm); |
1225 | sfree(unwcfname); |
1226 | continue; |
1227 | } |
9c77ddf6 |
1228 | } else { |
5079ee6d |
1229 | origwfname = origfname; |
1230 | swcm = NULL; |
1231 | } |
1232 | |
1233 | while (origwfname) { |
1234 | fname = canonify(origwfname); |
1235 | |
9c77ddf6 |
1236 | if (!fname) { |
4dc846cd |
1237 | printf("%s: canonify: %s\n", origwfname, fxp_error()); |
9c77ddf6 |
1238 | sfree(unwcfname); |
1239 | return 0; |
1240 | } |
93e86a8b |
1241 | |
9c77ddf6 |
1242 | if (!multiple && i < cmd->nwords) |
1243 | outfname = cmd->words[i++]; |
1244 | else |
5079ee6d |
1245 | outfname = stripslashes(origwfname, 0); |
93e86a8b |
1246 | |
5079ee6d |
1247 | ret = sftp_get_file(fname, outfname, recurse, restart); |
9c77ddf6 |
1248 | |
1249 | sfree(fname); |
5079ee6d |
1250 | |
1251 | if (swcm) { |
1252 | sfree(origwfname); |
1253 | origwfname = sftp_wildcard_get_filename(swcm); |
1254 | } else { |
1255 | origwfname = NULL; |
1256 | } |
9c77ddf6 |
1257 | } |
1258 | sfree(unwcfname); |
5079ee6d |
1259 | if (swcm) |
1260 | sftp_finish_wildcard_matching(swcm); |
9c77ddf6 |
1261 | if (!ret) |
1262 | return ret; |
1263 | |
1264 | } while (multiple && i < cmd->nwords); |
93e86a8b |
1265 | |
1266 | return ret; |
1267 | } |
1268 | int sftp_cmd_get(struct sftp_command *cmd) |
1269 | { |
9c77ddf6 |
1270 | return sftp_general_get(cmd, 0, 0); |
1271 | } |
1272 | int sftp_cmd_mget(struct sftp_command *cmd) |
1273 | { |
1274 | return sftp_general_get(cmd, 0, 1); |
93e86a8b |
1275 | } |
1276 | int sftp_cmd_reget(struct sftp_command *cmd) |
1277 | { |
9c77ddf6 |
1278 | return sftp_general_get(cmd, 1, 0); |
93e86a8b |
1279 | } |
1280 | |
1281 | /* |
9c77ddf6 |
1282 | * Send a file and store it at the remote end. We have three very |
1283 | * similar commands here. The basic one is `put'; `reput' differs |
1284 | * in that it checks for the existence of the destination file and |
1285 | * starts from where a previous aborted transfer left off; `mput' |
1286 | * differs in that it interprets all its arguments as files to |
1287 | * transfer (never as a different remote name for a local file) and |
1288 | * can handle wildcards. |
93e86a8b |
1289 | */ |
9c77ddf6 |
1290 | int sftp_general_put(struct sftp_command *cmd, int restart, int multiple) |
93e86a8b |
1291 | { |
5079ee6d |
1292 | char *fname, *wfname, *origoutfname, *outfname; |
93e86a8b |
1293 | int i, ret; |
1294 | int recurse = FALSE; |
1295 | |
1296 | if (back == NULL) { |
38f0c08e |
1297 | not_connected(); |
93e86a8b |
1298 | return 0; |
1299 | } |
1300 | |
1301 | i = 1; |
1302 | while (i < cmd->nwords && cmd->words[i][0] == '-') { |
1303 | if (!strcmp(cmd->words[i], "--")) { |
1304 | /* finish processing options */ |
1305 | i++; |
1306 | break; |
1307 | } else if (!strcmp(cmd->words[i], "-r")) { |
1308 | recurse = TRUE; |
1309 | } else { |
9033711a |
1310 | printf("%s: unrecognised option '%s'\n", cmd->words[0], cmd->words[i]); |
93e86a8b |
1311 | return 0; |
1312 | } |
1313 | i++; |
1314 | } |
1315 | |
1316 | if (i >= cmd->nwords) { |
9033711a |
1317 | printf("%s: expects a filename\n", cmd->words[0]); |
93e86a8b |
1318 | return 0; |
1319 | } |
1320 | |
5079ee6d |
1321 | ret = 1; |
9c77ddf6 |
1322 | do { |
5079ee6d |
1323 | WildcardMatcher *wcm; |
9c77ddf6 |
1324 | fname = cmd->words[i++]; |
93e86a8b |
1325 | |
9c77ddf6 |
1326 | if (multiple && test_wildcard(fname, FALSE) == WCTYPE_WILDCARD) { |
5079ee6d |
1327 | wcm = begin_wildcard_matching(fname); |
1328 | wfname = wildcard_get_filename(wcm); |
1329 | if (!wfname) { |
1330 | /* Politely warn the user that nothing matched. */ |
1331 | printf("%s: nothing matched\n", fname); |
1332 | finish_wildcard_matching(wcm); |
1333 | continue; |
1334 | } |
9c77ddf6 |
1335 | } else { |
5079ee6d |
1336 | wfname = fname; |
1337 | wcm = NULL; |
1338 | } |
1339 | |
1340 | while (wfname) { |
9c77ddf6 |
1341 | if (!multiple && i < cmd->nwords) |
1342 | origoutfname = cmd->words[i++]; |
1343 | else |
5079ee6d |
1344 | origoutfname = stripslashes(wfname, 1); |
9c77ddf6 |
1345 | |
1346 | outfname = canonify(origoutfname); |
1347 | if (!outfname) { |
4dc846cd |
1348 | printf("%s: canonify: %s\n", origoutfname, fxp_error()); |
5079ee6d |
1349 | if (wcm) { |
1350 | sfree(wfname); |
1351 | finish_wildcard_matching(wcm); |
1352 | } |
9c77ddf6 |
1353 | return 0; |
1354 | } |
5079ee6d |
1355 | ret = sftp_put_file(wfname, outfname, recurse, restart); |
9c77ddf6 |
1356 | sfree(outfname); |
5079ee6d |
1357 | |
1358 | if (wcm) { |
1359 | sfree(wfname); |
1360 | wfname = wildcard_get_filename(wcm); |
1361 | } else { |
1362 | wfname = NULL; |
1363 | } |
9c77ddf6 |
1364 | } |
5079ee6d |
1365 | |
1366 | if (wcm) |
1367 | finish_wildcard_matching(wcm); |
1368 | |
9c77ddf6 |
1369 | if (!ret) |
1370 | return ret; |
93e86a8b |
1371 | |
9c77ddf6 |
1372 | } while (multiple && i < cmd->nwords); |
4c7f0d61 |
1373 | |
df49ff19 |
1374 | return ret; |
4c7f0d61 |
1375 | } |
d92624dc |
1376 | int sftp_cmd_put(struct sftp_command *cmd) |
1377 | { |
9c77ddf6 |
1378 | return sftp_general_put(cmd, 0, 0); |
1379 | } |
1380 | int sftp_cmd_mput(struct sftp_command *cmd) |
1381 | { |
1382 | return sftp_general_put(cmd, 0, 1); |
d92624dc |
1383 | } |
1384 | int sftp_cmd_reput(struct sftp_command *cmd) |
1385 | { |
9c77ddf6 |
1386 | return sftp_general_put(cmd, 1, 0); |
d92624dc |
1387 | } |
4c7f0d61 |
1388 | |
9954aaa3 |
1389 | int sftp_cmd_mkdir(struct sftp_command *cmd) |
1390 | { |
1391 | char *dir; |
1bc24185 |
1392 | struct sftp_packet *pktin; |
1393 | struct sftp_request *req, *rreq; |
9954aaa3 |
1394 | int result; |
83567e43 |
1395 | int i, ret; |
9954aaa3 |
1396 | |
fa3db767 |
1397 | if (back == NULL) { |
38f0c08e |
1398 | not_connected(); |
fa3db767 |
1399 | return 0; |
1400 | } |
9954aaa3 |
1401 | |
1402 | if (cmd->nwords < 2) { |
1403 | printf("mkdir: expects a directory\n"); |
1404 | return 0; |
1405 | } |
1406 | |
83567e43 |
1407 | ret = 1; |
1408 | for (i = 1; i < cmd->nwords; i++) { |
1409 | dir = canonify(cmd->words[i]); |
1410 | if (!dir) { |
4dc846cd |
1411 | printf("%s: canonify: %s\n", dir, fxp_error()); |
83567e43 |
1412 | return 0; |
1413 | } |
1414 | |
1415 | sftp_register(req = fxp_mkdir_send(dir)); |
1416 | rreq = sftp_find_request(pktin = sftp_recv()); |
1417 | assert(rreq == req); |
1418 | result = fxp_mkdir_recv(pktin, rreq); |
1419 | |
1420 | if (!result) { |
1421 | printf("mkdir %s: %s\n", dir, fxp_error()); |
83567e43 |
1422 | ret = 0; |
f68363d2 |
1423 | } else |
1424 | printf("mkdir %s: OK\n", dir); |
83567e43 |
1425 | |
1426 | sfree(dir); |
9954aaa3 |
1427 | } |
1428 | |
83567e43 |
1429 | return ret; |
1430 | } |
1431 | |
1432 | static int sftp_action_rmdir(void *vctx, char *dir) |
1433 | { |
1434 | struct sftp_packet *pktin; |
1435 | struct sftp_request *req, *rreq; |
1436 | int result; |
1437 | |
1438 | sftp_register(req = fxp_rmdir_send(dir)); |
1bc24185 |
1439 | rreq = sftp_find_request(pktin = sftp_recv()); |
1440 | assert(rreq == req); |
83567e43 |
1441 | result = fxp_rmdir_recv(pktin, rreq); |
1bc24185 |
1442 | |
9954aaa3 |
1443 | if (!result) { |
83567e43 |
1444 | printf("rmdir %s: %s\n", dir, fxp_error()); |
9954aaa3 |
1445 | return 0; |
1446 | } |
1447 | |
f68363d2 |
1448 | printf("rmdir %s: OK\n", dir); |
1449 | |
df49ff19 |
1450 | return 1; |
9954aaa3 |
1451 | } |
1452 | |
1453 | int sftp_cmd_rmdir(struct sftp_command *cmd) |
1454 | { |
83567e43 |
1455 | int i, ret; |
9954aaa3 |
1456 | |
fa3db767 |
1457 | if (back == NULL) { |
38f0c08e |
1458 | not_connected(); |
fa3db767 |
1459 | return 0; |
1460 | } |
9954aaa3 |
1461 | |
1462 | if (cmd->nwords < 2) { |
1463 | printf("rmdir: expects a directory\n"); |
1464 | return 0; |
1465 | } |
1466 | |
83567e43 |
1467 | ret = 1; |
1468 | for (i = 1; i < cmd->nwords; i++) |
1469 | ret &= wildcard_iterate(cmd->words[i], sftp_action_rmdir, NULL); |
9954aaa3 |
1470 | |
83567e43 |
1471 | return ret; |
1472 | } |
1473 | |
1474 | static int sftp_action_rm(void *vctx, char *fname) |
1475 | { |
1476 | struct sftp_packet *pktin; |
1477 | struct sftp_request *req, *rreq; |
1478 | int result; |
1479 | |
1480 | sftp_register(req = fxp_remove_send(fname)); |
1bc24185 |
1481 | rreq = sftp_find_request(pktin = sftp_recv()); |
1482 | assert(rreq == req); |
83567e43 |
1483 | result = fxp_remove_recv(pktin, rreq); |
1bc24185 |
1484 | |
9954aaa3 |
1485 | if (!result) { |
83567e43 |
1486 | printf("rm %s: %s\n", fname, fxp_error()); |
9954aaa3 |
1487 | return 0; |
1488 | } |
1489 | |
f68363d2 |
1490 | printf("rm %s: OK\n", fname); |
1491 | |
df49ff19 |
1492 | return 1; |
9954aaa3 |
1493 | } |
1494 | |
1495 | int sftp_cmd_rm(struct sftp_command *cmd) |
1496 | { |
83567e43 |
1497 | int i, ret; |
9954aaa3 |
1498 | |
fa3db767 |
1499 | if (back == NULL) { |
38f0c08e |
1500 | not_connected(); |
fa3db767 |
1501 | return 0; |
1502 | } |
1503 | |
9954aaa3 |
1504 | if (cmd->nwords < 2) { |
1505 | printf("rm: expects a filename\n"); |
1506 | return 0; |
1507 | } |
1508 | |
83567e43 |
1509 | ret = 1; |
1510 | for (i = 1; i < cmd->nwords; i++) |
1511 | ret &= wildcard_iterate(cmd->words[i], sftp_action_rm, NULL); |
1512 | |
1513 | return ret; |
1514 | } |
1515 | |
1516 | static int check_is_dir(char *dstfname) |
1517 | { |
1518 | struct sftp_packet *pktin; |
1519 | struct sftp_request *req, *rreq; |
1520 | struct fxp_attrs attrs; |
1521 | int result; |
1522 | |
1523 | sftp_register(req = fxp_stat_send(dstfname)); |
1524 | rreq = sftp_find_request(pktin = sftp_recv()); |
1525 | assert(rreq == req); |
1526 | result = fxp_stat_recv(pktin, rreq, &attrs); |
1527 | |
1528 | if (result && |
1529 | (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) && |
1530 | (attrs.permissions & 0040000)) |
1531 | return TRUE; |
1532 | else |
1533 | return FALSE; |
1534 | } |
1535 | |
1536 | struct sftp_context_mv { |
1537 | char *dstfname; |
1538 | int dest_is_dir; |
1539 | }; |
1540 | |
1541 | static int sftp_action_mv(void *vctx, char *srcfname) |
1542 | { |
1543 | struct sftp_context_mv *ctx = (struct sftp_context_mv *)vctx; |
1544 | struct sftp_packet *pktin; |
1545 | struct sftp_request *req, *rreq; |
1546 | const char *error; |
1547 | char *finalfname, *newcanon = NULL; |
1548 | int ret, result; |
1549 | |
1550 | if (ctx->dest_is_dir) { |
1551 | char *p; |
1552 | char *newname; |
1553 | |
1554 | p = srcfname + strlen(srcfname); |
1555 | while (p > srcfname && p[-1] != '/') p--; |
1556 | newname = dupcat(ctx->dstfname, "/", p, NULL); |
1557 | newcanon = canonify(newname); |
1558 | if (!newcanon) { |
4dc846cd |
1559 | printf("%s: canonify: %s\n", newname, fxp_error()); |
83567e43 |
1560 | sfree(newname); |
1561 | return 0; |
1562 | } |
1563 | sfree(newname); |
1564 | |
1565 | finalfname = newcanon; |
1566 | } else { |
1567 | finalfname = ctx->dstfname; |
9954aaa3 |
1568 | } |
1569 | |
83567e43 |
1570 | sftp_register(req = fxp_rename_send(srcfname, finalfname)); |
1bc24185 |
1571 | rreq = sftp_find_request(pktin = sftp_recv()); |
1572 | assert(rreq == req); |
83567e43 |
1573 | result = fxp_rename_recv(pktin, rreq); |
1bc24185 |
1574 | |
83567e43 |
1575 | error = result ? NULL : fxp_error(); |
1576 | |
1577 | if (error) { |
1578 | printf("mv %s %s: %s\n", srcfname, finalfname, error); |
1579 | ret = 0; |
1580 | } else { |
1581 | printf("%s -> %s\n", srcfname, finalfname); |
1582 | ret = 1; |
9954aaa3 |
1583 | } |
1584 | |
83567e43 |
1585 | sfree(newcanon); |
1586 | return ret; |
d92624dc |
1587 | } |
1588 | |
1589 | int sftp_cmd_mv(struct sftp_command *cmd) |
1590 | { |
83567e43 |
1591 | struct sftp_context_mv actx, *ctx = &actx; |
1592 | int i, ret; |
d92624dc |
1593 | |
fa3db767 |
1594 | if (back == NULL) { |
38f0c08e |
1595 | not_connected(); |
fa3db767 |
1596 | return 0; |
1597 | } |
1598 | |
d92624dc |
1599 | if (cmd->nwords < 3) { |
1600 | printf("mv: expects two filenames\n"); |
9954aaa3 |
1601 | return 0; |
d92624dc |
1602 | } |
83567e43 |
1603 | |
1604 | ctx->dstfname = canonify(cmd->words[cmd->nwords-1]); |
1605 | if (!ctx->dstfname) { |
4dc846cd |
1606 | printf("%s: canonify: %s\n", ctx->dstfname, fxp_error()); |
d92624dc |
1607 | return 0; |
1608 | } |
1609 | |
83567e43 |
1610 | /* |
1611 | * If there's more than one source argument, or one source |
1612 | * argument which is a wildcard, we _require_ that the |
1613 | * destination is a directory. |
1614 | */ |
1615 | ctx->dest_is_dir = check_is_dir(ctx->dstfname); |
1616 | if ((cmd->nwords > 3 || is_wildcard(cmd->words[1])) && !ctx->dest_is_dir) { |
1617 | printf("mv: multiple or wildcard arguments require the destination" |
1618 | " to be a directory\n"); |
c4acc08c |
1619 | sfree(ctx->dstfname); |
d92624dc |
1620 | return 0; |
1621 | } |
9954aaa3 |
1622 | |
83567e43 |
1623 | /* |
1624 | * Now iterate over the source arguments. |
1625 | */ |
1626 | ret = 1; |
1627 | for (i = 1; i < cmd->nwords-1; i++) |
1628 | ret &= wildcard_iterate(cmd->words[i], sftp_action_mv, ctx); |
1629 | |
c4acc08c |
1630 | sfree(ctx->dstfname); |
83567e43 |
1631 | return ret; |
1632 | } |
1633 | |
1634 | struct sftp_context_chmod { |
1635 | unsigned attrs_clr, attrs_xor; |
1636 | }; |
1637 | |
1638 | static int sftp_action_chmod(void *vctx, char *fname) |
1639 | { |
1640 | struct fxp_attrs attrs; |
1641 | struct sftp_packet *pktin; |
1642 | struct sftp_request *req, *rreq; |
1643 | int result; |
1644 | unsigned oldperms, newperms; |
1645 | struct sftp_context_chmod *ctx = (struct sftp_context_chmod *)vctx; |
1646 | |
1647 | sftp_register(req = fxp_stat_send(fname)); |
1bc24185 |
1648 | rreq = sftp_find_request(pktin = sftp_recv()); |
1649 | assert(rreq == req); |
83567e43 |
1650 | result = fxp_stat_recv(pktin, rreq, &attrs); |
1bc24185 |
1651 | |
83567e43 |
1652 | if (!result || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) { |
1653 | printf("get attrs for %s: %s\n", fname, |
1654 | result ? "file permissions not provided" : fxp_error()); |
83567e43 |
1655 | return 0; |
1656 | } |
d92624dc |
1657 | |
83567e43 |
1658 | attrs.flags = SSH_FILEXFER_ATTR_PERMISSIONS; /* perms _only_ */ |
1659 | oldperms = attrs.permissions & 07777; |
1660 | attrs.permissions &= ~ctx->attrs_clr; |
1661 | attrs.permissions ^= ctx->attrs_xor; |
1662 | newperms = attrs.permissions & 07777; |
1bc24185 |
1663 | |
83567e43 |
1664 | if (oldperms == newperms) |
1665 | return 1; /* no need to do anything! */ |
1bc24185 |
1666 | |
83567e43 |
1667 | sftp_register(req = fxp_setstat_send(fname, attrs)); |
1668 | rreq = sftp_find_request(pktin = sftp_recv()); |
1669 | assert(rreq == req); |
1670 | result = fxp_setstat_recv(pktin, rreq); |
1bc24185 |
1671 | |
83567e43 |
1672 | if (!result) { |
1673 | printf("set attrs for %s: %s\n", fname, fxp_error()); |
83567e43 |
1674 | return 0; |
d92624dc |
1675 | } |
d92624dc |
1676 | |
83567e43 |
1677 | printf("%s: %04o -> %04o\n", fname, oldperms, newperms); |
1678 | |
df49ff19 |
1679 | return 1; |
9954aaa3 |
1680 | } |
1681 | |
d92624dc |
1682 | int sftp_cmd_chmod(struct sftp_command *cmd) |
1683 | { |
83567e43 |
1684 | char *mode; |
1685 | int i, ret; |
1686 | struct sftp_context_chmod actx, *ctx = &actx; |
d92624dc |
1687 | |
fa3db767 |
1688 | if (back == NULL) { |
38f0c08e |
1689 | not_connected(); |
fa3db767 |
1690 | return 0; |
1691 | } |
1692 | |
d92624dc |
1693 | if (cmd->nwords < 3) { |
1694 | printf("chmod: expects a mode specifier and a filename\n"); |
1695 | return 0; |
1696 | } |
1697 | |
1698 | /* |
1699 | * Attempt to parse the mode specifier in cmd->words[1]. We |
1700 | * don't support the full horror of Unix chmod; instead we |
1701 | * support a much simpler syntax in which the user can either |
1702 | * specify an octal number, or a comma-separated sequence of |
1703 | * [ugoa]*[-+=][rwxst]+. (The initial [ugoa] sequence may |
1704 | * _only_ be omitted if the only attribute mentioned is t, |
1705 | * since all others require a user/group/other specification. |
1706 | * Additionally, the s attribute may not be specified for any |
1707 | * [ugoa] specifications other than exactly u or exactly g. |
1708 | */ |
83567e43 |
1709 | ctx->attrs_clr = ctx->attrs_xor = 0; |
d92624dc |
1710 | mode = cmd->words[1]; |
1711 | if (mode[0] >= '0' && mode[0] <= '9') { |
1712 | if (mode[strspn(mode, "01234567")]) { |
1713 | printf("chmod: numeric file modes should" |
1714 | " contain digits 0-7 only\n"); |
1715 | return 0; |
1716 | } |
83567e43 |
1717 | ctx->attrs_clr = 07777; |
1718 | sscanf(mode, "%o", &ctx->attrs_xor); |
1719 | ctx->attrs_xor &= ctx->attrs_clr; |
d92624dc |
1720 | } else { |
1721 | while (*mode) { |
1722 | char *modebegin = mode; |
1723 | unsigned subset, perms; |
1724 | int action; |
1725 | |
1726 | subset = 0; |
1727 | while (*mode && *mode != ',' && |
1728 | *mode != '+' && *mode != '-' && *mode != '=') { |
1729 | switch (*mode) { |
1730 | case 'u': subset |= 04700; break; /* setuid, user perms */ |
1731 | case 'g': subset |= 02070; break; /* setgid, group perms */ |
1732 | case 'o': subset |= 00007; break; /* just other perms */ |
1733 | case 'a': subset |= 06777; break; /* all of the above */ |
1734 | default: |
1735 | printf("chmod: file mode '%.*s' contains unrecognised" |
1736 | " user/group/other specifier '%c'\n", |
b51259f6 |
1737 | (int)strcspn(modebegin, ","), modebegin, *mode); |
d92624dc |
1738 | return 0; |
1739 | } |
1740 | mode++; |
1741 | } |
1742 | if (!*mode || *mode == ',') { |
1743 | printf("chmod: file mode '%.*s' is incomplete\n", |
b51259f6 |
1744 | (int)strcspn(modebegin, ","), modebegin); |
d92624dc |
1745 | return 0; |
1746 | } |
1747 | action = *mode++; |
1748 | if (!*mode || *mode == ',') { |
1749 | printf("chmod: file mode '%.*s' is incomplete\n", |
b51259f6 |
1750 | (int)strcspn(modebegin, ","), modebegin); |
d92624dc |
1751 | return 0; |
1752 | } |
1753 | perms = 0; |
1754 | while (*mode && *mode != ',') { |
1755 | switch (*mode) { |
1756 | case 'r': perms |= 00444; break; |
1757 | case 'w': perms |= 00222; break; |
1758 | case 'x': perms |= 00111; break; |
1759 | case 't': perms |= 01000; subset |= 01000; break; |
1760 | case 's': |
1761 | if ((subset & 06777) != 04700 && |
1762 | (subset & 06777) != 02070) { |
1763 | printf("chmod: file mode '%.*s': set[ug]id bit should" |
1764 | " be used with exactly one of u or g only\n", |
b51259f6 |
1765 | (int)strcspn(modebegin, ","), modebegin); |
d92624dc |
1766 | return 0; |
1767 | } |
1768 | perms |= 06000; |
1769 | break; |
1770 | default: |
1771 | printf("chmod: file mode '%.*s' contains unrecognised" |
1772 | " permission specifier '%c'\n", |
b51259f6 |
1773 | (int)strcspn(modebegin, ","), modebegin, *mode); |
d92624dc |
1774 | return 0; |
1775 | } |
1776 | mode++; |
1777 | } |
1778 | if (!(subset & 06777) && (perms &~ subset)) { |
1779 | printf("chmod: file mode '%.*s' contains no user/group/other" |
1780 | " specifier and permissions other than 't' \n", |
b51259f6 |
1781 | (int)strcspn(modebegin, ","), modebegin); |
d92624dc |
1782 | return 0; |
1783 | } |
1784 | perms &= subset; |
1785 | switch (action) { |
1786 | case '+': |
83567e43 |
1787 | ctx->attrs_clr |= perms; |
1788 | ctx->attrs_xor |= perms; |
d92624dc |
1789 | break; |
1790 | case '-': |
83567e43 |
1791 | ctx->attrs_clr |= perms; |
1792 | ctx->attrs_xor &= ~perms; |
d92624dc |
1793 | break; |
1794 | case '=': |
83567e43 |
1795 | ctx->attrs_clr |= subset; |
1796 | ctx->attrs_xor |= perms; |
d92624dc |
1797 | break; |
1798 | } |
1799 | if (*mode) mode++; /* eat comma */ |
1800 | } |
1801 | } |
1802 | |
83567e43 |
1803 | ret = 1; |
1804 | for (i = 2; i < cmd->nwords; i++) |
1805 | ret &= wildcard_iterate(cmd->words[i], sftp_action_chmod, ctx); |
d92624dc |
1806 | |
83567e43 |
1807 | return ret; |
d92624dc |
1808 | } |
9954aaa3 |
1809 | |
fa3db767 |
1810 | static int sftp_cmd_open(struct sftp_command *cmd) |
1811 | { |
f11233cb |
1812 | int portnumber; |
1813 | |
fa3db767 |
1814 | if (back != NULL) { |
1815 | printf("psftp: already connected\n"); |
1816 | return 0; |
1817 | } |
1818 | |
1819 | if (cmd->nwords < 2) { |
1820 | printf("open: expects a host name\n"); |
1821 | return 0; |
1822 | } |
1823 | |
f11233cb |
1824 | if (cmd->nwords > 2) { |
1825 | portnumber = atoi(cmd->words[2]); |
1826 | if (portnumber == 0) { |
1827 | printf("open: invalid port number\n"); |
1828 | return 0; |
1829 | } |
1830 | } else |
1831 | portnumber = 0; |
1832 | |
1833 | if (psftp_connect(cmd->words[1], NULL, portnumber)) { |
fa3db767 |
1834 | back = NULL; /* connection is already closed */ |
1835 | return -1; /* this is fatal */ |
1836 | } |
1837 | do_sftp_init(); |
df49ff19 |
1838 | return 1; |
fa3db767 |
1839 | } |
1840 | |
3af97463 |
1841 | static int sftp_cmd_lcd(struct sftp_command *cmd) |
1842 | { |
d6cc41e6 |
1843 | char *currdir, *errmsg; |
3af97463 |
1844 | |
1845 | if (cmd->nwords < 2) { |
1846 | printf("lcd: expects a local directory name\n"); |
1847 | return 0; |
1848 | } |
1849 | |
d6cc41e6 |
1850 | errmsg = psftp_lcd(cmd->words[1]); |
1851 | if (errmsg) { |
1852 | printf("lcd: unable to change directory: %s\n", errmsg); |
1853 | sfree(errmsg); |
3af97463 |
1854 | return 0; |
1855 | } |
1856 | |
d6cc41e6 |
1857 | currdir = psftp_getcwd(); |
3af97463 |
1858 | printf("New local directory is %s\n", currdir); |
1859 | sfree(currdir); |
1860 | |
1861 | return 1; |
1862 | } |
1863 | |
1864 | static int sftp_cmd_lpwd(struct sftp_command *cmd) |
1865 | { |
1866 | char *currdir; |
3af97463 |
1867 | |
d6cc41e6 |
1868 | currdir = psftp_getcwd(); |
3af97463 |
1869 | printf("Current local directory is %s\n", currdir); |
1870 | sfree(currdir); |
1871 | |
1872 | return 1; |
1873 | } |
1874 | |
1875 | static int sftp_cmd_pling(struct sftp_command *cmd) |
1876 | { |
1877 | int exitcode; |
1878 | |
1879 | exitcode = system(cmd->words[1]); |
1880 | return (exitcode == 0); |
1881 | } |
1882 | |
bf5240cd |
1883 | static int sftp_cmd_help(struct sftp_command *cmd); |
1884 | |
4c7f0d61 |
1885 | static struct sftp_cmd_lookup { |
1886 | char *name; |
bf5240cd |
1887 | /* |
1888 | * For help purposes, there are two kinds of command: |
1889 | * |
1890 | * - primary commands, in which `longhelp' is non-NULL. In |
1891 | * this case `shorthelp' is descriptive text, and `longhelp' |
1892 | * is longer descriptive text intended to be printed after |
1893 | * the command name. |
1894 | * |
1895 | * - alias commands, in which `longhelp' is NULL. In this case |
1896 | * `shorthelp' is the name of a primary command, which |
1897 | * contains the help that should double up for this command. |
1898 | */ |
3af97463 |
1899 | int listed; /* do we list this in primary help? */ |
bf5240cd |
1900 | char *shorthelp; |
1901 | char *longhelp; |
32874aea |
1902 | int (*obey) (struct sftp_command *); |
4c7f0d61 |
1903 | } sftp_lookup[] = { |
1904 | /* |
1905 | * List of sftp commands. This is binary-searched so it MUST be |
1906 | * in ASCII order. |
1907 | */ |
32874aea |
1908 | { |
d6cc41e6 |
1909 | "!", TRUE, "run a local command", |
3af97463 |
1910 | "<command>\n" |
d6cc41e6 |
1911 | /* FIXME: this example is crap for non-Windows. */ |
1912 | " Runs a local command. For example, \"!del myfile\".\n", |
3af97463 |
1913 | sftp_cmd_pling |
1914 | }, |
1915 | { |
1916 | "bye", TRUE, "finish your SFTP session", |
bf5240cd |
1917 | "\n" |
1918 | " Terminates your SFTP session and quits the PSFTP program.\n", |
1919 | sftp_cmd_quit |
1920 | }, |
1921 | { |
3af97463 |
1922 | "cd", TRUE, "change your remote working directory", |
c1b8799b |
1923 | " [ <new working directory> ]\n" |
bf5240cd |
1924 | " Change the remote working directory for your SFTP session.\n" |
1925 | " If a new working directory is not supplied, you will be\n" |
1926 | " returned to your home directory.\n", |
1927 | sftp_cmd_cd |
1928 | }, |
1929 | { |
3af97463 |
1930 | "chmod", TRUE, "change file permissions and modes", |
c1b8799b |
1931 | " <modes> <filename-or-wildcard> [ <filename-or-wildcard>... ]\n" |
1932 | " Change the file permissions on one or more remote files or\n" |
1933 | " directories.\n" |
1934 | " <modes> can be any octal Unix permission specifier.\n" |
1935 | " Alternatively, <modes> can include the following modifiers:\n" |
bf5240cd |
1936 | " u+r make file readable by owning user\n" |
1937 | " u+w make file writable by owning user\n" |
1938 | " u+x make file executable by owning user\n" |
1939 | " u-r make file not readable by owning user\n" |
1940 | " [also u-w, u-x]\n" |
1941 | " g+r make file readable by members of owning group\n" |
1942 | " [also g+w, g+x, g-r, g-w, g-x]\n" |
1943 | " o+r make file readable by all other users\n" |
1944 | " [also o+w, o+x, o-r, o-w, o-x]\n" |
1945 | " a+r make file readable by absolutely everybody\n" |
1946 | " [also a+w, a+x, a-r, a-w, a-x]\n" |
1947 | " u+s enable the Unix set-user-ID bit\n" |
1948 | " u-s disable the Unix set-user-ID bit\n" |
1949 | " g+s enable the Unix set-group-ID bit\n" |
1950 | " g-s disable the Unix set-group-ID bit\n" |
1951 | " +t enable the Unix \"sticky bit\"\n" |
1952 | " You can give more than one modifier for the same user (\"g-rwx\"), and\n" |
1953 | " more than one user for the same modifier (\"ug+w\"). You can\n" |
1954 | " use commas to separate different modifiers (\"u+rwx,g+s\").\n", |
1955 | sftp_cmd_chmod |
1956 | }, |
1957 | { |
b614ce89 |
1958 | "close", TRUE, "finish your SFTP session but do not quit PSFTP", |
1959 | "\n" |
1960 | " Terminates your SFTP session, but does not quit the PSFTP\n" |
1961 | " program. You can then use \"open\" to start another SFTP\n" |
1962 | " session, to the same server or to a different one.\n", |
1963 | sftp_cmd_close |
1964 | }, |
1965 | { |
c1b8799b |
1966 | "del", TRUE, "delete files on the remote server", |
1967 | " <filename-or-wildcard> [ <filename-or-wildcard>... ]\n" |
1968 | " Delete a file or files from the server.\n", |
bf5240cd |
1969 | sftp_cmd_rm |
1970 | }, |
1971 | { |
3af97463 |
1972 | "delete", FALSE, "del", NULL, sftp_cmd_rm |
bf5240cd |
1973 | }, |
1974 | { |
c1b8799b |
1975 | "dir", TRUE, "list remote files", |
9033711a |
1976 | " [ <directory-name> ]/[ <wildcard> ]\n" |
bf5240cd |
1977 | " List the contents of a specified directory on the server.\n" |
1978 | " If <directory-name> is not given, the current working directory\n" |
9033711a |
1979 | " is assumed.\n" |
1980 | " If <wildcard> is given, it is treated as a set of files to\n" |
1981 | " list; otherwise, all files are listed.\n", |
bf5240cd |
1982 | sftp_cmd_ls |
1983 | }, |
1984 | { |
3af97463 |
1985 | "exit", TRUE, "bye", NULL, sftp_cmd_quit |
bf5240cd |
1986 | }, |
1987 | { |
3af97463 |
1988 | "get", TRUE, "download a file from the server to your local machine", |
9033711a |
1989 | " [ -r ] [ -- ] <filename> [ <local-filename> ]\n" |
bf5240cd |
1990 | " Downloads a file on the server and stores it locally under\n" |
1991 | " the same name, or under a different one if you supply the\n" |
9033711a |
1992 | " argument <local-filename>.\n" |
1993 | " If -r specified, recursively fetch a directory.\n", |
bf5240cd |
1994 | sftp_cmd_get |
1995 | }, |
1996 | { |
3af97463 |
1997 | "help", TRUE, "give help", |
bf5240cd |
1998 | " [ <command> [ <command> ... ] ]\n" |
1999 | " Give general help if no commands are specified.\n" |
2000 | " If one or more commands are specified, give specific help on\n" |
2001 | " those particular commands.\n", |
2002 | sftp_cmd_help |
2003 | }, |
2004 | { |
3af97463 |
2005 | "lcd", TRUE, "change local working directory", |
2006 | " <local-directory-name>\n" |
2007 | " Change the local working directory of the PSFTP program (the\n" |
2008 | " default location where the \"get\" command will save files).\n", |
2009 | sftp_cmd_lcd |
2010 | }, |
2011 | { |
2012 | "lpwd", TRUE, "print local working directory", |
2013 | "\n" |
2014 | " Print the local working directory of the PSFTP program (the\n" |
2015 | " default location where the \"get\" command will save files).\n", |
2016 | sftp_cmd_lpwd |
2017 | }, |
2018 | { |
2019 | "ls", TRUE, "dir", NULL, |
bf5240cd |
2020 | sftp_cmd_ls |
2021 | }, |
2022 | { |
9c77ddf6 |
2023 | "mget", TRUE, "download multiple files at once", |
9033711a |
2024 | " [ -r ] [ -- ] <filename-or-wildcard> [ <filename-or-wildcard>... ]\n" |
9c77ddf6 |
2025 | " Downloads many files from the server, storing each one under\n" |
2026 | " the same name it has on the server side. You can use wildcards\n" |
9033711a |
2027 | " such as \"*.c\" to specify lots of files at once.\n" |
2028 | " If -r specified, recursively fetch files and directories.\n", |
9c77ddf6 |
2029 | sftp_cmd_mget |
2030 | }, |
2031 | { |
c1b8799b |
2032 | "mkdir", TRUE, "create directories on the remote server", |
2033 | " <directory-name> [ <directory-name>... ]\n" |
2034 | " Creates directories with the given names on the server.\n", |
bf5240cd |
2035 | sftp_cmd_mkdir |
2036 | }, |
2037 | { |
9c77ddf6 |
2038 | "mput", TRUE, "upload multiple files at once", |
96515f61 |
2039 | " [ -r ] [ -- ] <filename-or-wildcard> [ <filename-or-wildcard>... ]\n" |
9c77ddf6 |
2040 | " Uploads many files to the server, storing each one under the\n" |
2041 | " same name it has on the client side. You can use wildcards\n" |
9033711a |
2042 | " such as \"*.c\" to specify lots of files at once.\n" |
2043 | " If -r specified, recursively store files and directories.\n", |
9c77ddf6 |
2044 | sftp_cmd_mput |
2045 | }, |
2046 | { |
c1b8799b |
2047 | "mv", TRUE, "move or rename file(s) on the remote server", |
2048 | " <source> [ <source>... ] <destination>\n" |
2049 | " Moves or renames <source>(s) on the server to <destination>,\n" |
2050 | " also on the server.\n" |
2051 | " If <destination> specifies an existing directory, then <source>\n" |
2052 | " may be a wildcard, and multiple <source>s may be given; all\n" |
2053 | " source files are moved into <destination>.\n" |
2054 | " Otherwise, <source> must specify a single file, which is moved\n" |
2055 | " or renamed so that it is accessible under the name <destination>.\n", |
bf5240cd |
2056 | sftp_cmd_mv |
2057 | }, |
2058 | { |
3af97463 |
2059 | "open", TRUE, "connect to a host", |
f11233cb |
2060 | " [<user>@]<hostname> [<port>]\n" |
fa3db767 |
2061 | " Establishes an SFTP connection to a given host. Only usable\n" |
c1b8799b |
2062 | " when you are not already connected to a server.\n", |
fa3db767 |
2063 | sftp_cmd_open |
2064 | }, |
2065 | { |
56542985 |
2066 | "put", TRUE, "upload a file from your local machine to the server", |
9033711a |
2067 | " [ -r ] [ -- ] <filename> [ <remote-filename> ]\n" |
56542985 |
2068 | " Uploads a file to the server and stores it there under\n" |
2069 | " the same name, or under a different one if you supply the\n" |
9033711a |
2070 | " argument <remote-filename>.\n" |
2071 | " If -r specified, recursively store a directory.\n", |
56542985 |
2072 | sftp_cmd_put |
2073 | }, |
2074 | { |
3af97463 |
2075 | "pwd", TRUE, "print your remote working directory", |
4f2b387f |
2076 | "\n" |
2077 | " Print the current remote working directory for your SFTP session.\n", |
2078 | sftp_cmd_pwd |
2079 | }, |
2080 | { |
3af97463 |
2081 | "quit", TRUE, "bye", NULL, |
bf5240cd |
2082 | sftp_cmd_quit |
2083 | }, |
2084 | { |
c1b8799b |
2085 | "reget", TRUE, "continue downloading files", |
9033711a |
2086 | " [ -r ] [ -- ] <filename> [ <local-filename> ]\n" |
bf5240cd |
2087 | " Works exactly like the \"get\" command, but the local file\n" |
2088 | " must already exist. The download will begin at the end of the\n" |
9033711a |
2089 | " file. This is for resuming a download that was interrupted.\n" |
2090 | " If -r specified, resume interrupted \"get -r\".\n", |
bf5240cd |
2091 | sftp_cmd_reget |
2092 | }, |
2093 | { |
3af97463 |
2094 | "ren", TRUE, "mv", NULL, |
bf5240cd |
2095 | sftp_cmd_mv |
2096 | }, |
2097 | { |
3af97463 |
2098 | "rename", FALSE, "mv", NULL, |
bf5240cd |
2099 | sftp_cmd_mv |
2100 | }, |
2101 | { |
c1b8799b |
2102 | "reput", TRUE, "continue uploading files", |
9033711a |
2103 | " [ -r ] [ -- ] <filename> [ <remote-filename> ]\n" |
bf5240cd |
2104 | " Works exactly like the \"put\" command, but the remote file\n" |
2105 | " must already exist. The upload will begin at the end of the\n" |
9033711a |
2106 | " file. This is for resuming an upload that was interrupted.\n" |
2107 | " If -r specified, resume interrupted \"put -r\".\n", |
bf5240cd |
2108 | sftp_cmd_reput |
2109 | }, |
2110 | { |
3af97463 |
2111 | "rm", TRUE, "del", NULL, |
bf5240cd |
2112 | sftp_cmd_rm |
2113 | }, |
2114 | { |
c1b8799b |
2115 | "rmdir", TRUE, "remove directories on the remote server", |
2116 | " <directory-name> [ <directory-name>... ]\n" |
bf5240cd |
2117 | " Removes the directory with the given name on the server.\n" |
c1b8799b |
2118 | " The directory will not be removed unless it is empty.\n" |
2119 | " Wildcards may be used to specify multiple directories.\n", |
bf5240cd |
2120 | sftp_cmd_rmdir |
2121 | } |
2122 | }; |
2123 | |
2124 | const struct sftp_cmd_lookup *lookup_command(char *name) |
2125 | { |
2126 | int i, j, k, cmp; |
2127 | |
2128 | i = -1; |
2129 | j = sizeof(sftp_lookup) / sizeof(*sftp_lookup); |
2130 | while (j - i > 1) { |
2131 | k = (j + i) / 2; |
2132 | cmp = strcmp(name, sftp_lookup[k].name); |
2133 | if (cmp < 0) |
2134 | j = k; |
2135 | else if (cmp > 0) |
2136 | i = k; |
2137 | else { |
2138 | return &sftp_lookup[k]; |
2139 | } |
2140 | } |
2141 | return NULL; |
2142 | } |
2143 | |
2144 | static int sftp_cmd_help(struct sftp_command *cmd) |
2145 | { |
2146 | int i; |
2147 | if (cmd->nwords == 1) { |
2148 | /* |
2149 | * Give short help on each command. |
2150 | */ |
2151 | int maxlen; |
2152 | maxlen = 0; |
2153 | for (i = 0; i < sizeof(sftp_lookup) / sizeof(*sftp_lookup); i++) { |
3af97463 |
2154 | int len; |
2155 | if (!sftp_lookup[i].listed) |
2156 | continue; |
2157 | len = strlen(sftp_lookup[i].name); |
bf5240cd |
2158 | if (maxlen < len) |
2159 | maxlen = len; |
2160 | } |
2161 | for (i = 0; i < sizeof(sftp_lookup) / sizeof(*sftp_lookup); i++) { |
2162 | const struct sftp_cmd_lookup *lookup; |
3af97463 |
2163 | if (!sftp_lookup[i].listed) |
2164 | continue; |
bf5240cd |
2165 | lookup = &sftp_lookup[i]; |
2166 | printf("%-*s", maxlen+2, lookup->name); |
2167 | if (lookup->longhelp == NULL) |
2168 | lookup = lookup_command(lookup->shorthelp); |
2169 | printf("%s\n", lookup->shorthelp); |
2170 | } |
2171 | } else { |
2172 | /* |
2173 | * Give long help on specific commands. |
2174 | */ |
2175 | for (i = 1; i < cmd->nwords; i++) { |
2176 | const struct sftp_cmd_lookup *lookup; |
2177 | lookup = lookup_command(cmd->words[i]); |
2178 | if (!lookup) { |
2179 | printf("help: %s: command not found\n", cmd->words[i]); |
2180 | } else { |
2181 | printf("%s", lookup->name); |
2182 | if (lookup->longhelp == NULL) |
2183 | lookup = lookup_command(lookup->shorthelp); |
2184 | printf("%s", lookup->longhelp); |
2185 | } |
2186 | } |
2187 | } |
df49ff19 |
2188 | return 1; |
bf5240cd |
2189 | } |
4c7f0d61 |
2190 | |
2191 | /* ---------------------------------------------------------------------- |
2192 | * Command line reading and parsing. |
2193 | */ |
9954aaa3 |
2194 | struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) |
32874aea |
2195 | { |
4c7f0d61 |
2196 | char *line; |
4c7f0d61 |
2197 | struct sftp_command *cmd; |
2198 | char *p, *q, *r; |
2199 | int quoting; |
2200 | |
3d88e64d |
2201 | cmd = snew(struct sftp_command); |
4c7f0d61 |
2202 | cmd->words = NULL; |
2203 | cmd->nwords = 0; |
2204 | cmd->wordssize = 0; |
2205 | |
2206 | line = NULL; |
39934deb |
2207 | |
2208 | if (fp) { |
2209 | if (modeflags & 1) |
2210 | printf("psftp> "); |
2211 | line = fgetline(fp); |
2212 | } else { |
65857773 |
2213 | line = ssh_sftp_get_cmdline("psftp> ", back == NULL); |
4c7f0d61 |
2214 | } |
39934deb |
2215 | |
2216 | if (!line || !*line) { |
2217 | cmd->obey = sftp_cmd_quit; |
2218 | if ((mode == 0) || (modeflags & 1)) |
2219 | printf("quit\n"); |
2220 | return cmd; /* eof */ |
2221 | } |
2222 | |
2223 | line[strcspn(line, "\r\n")] = '\0'; |
2224 | |
df49ff19 |
2225 | if (modeflags & 1) { |
2226 | printf("%s\n", line); |
2227 | } |
4c7f0d61 |
2228 | |
4c7f0d61 |
2229 | p = line; |
3af97463 |
2230 | while (*p && (*p == ' ' || *p == '\t')) |
2231 | p++; |
2232 | |
2233 | if (*p == '!') { |
2234 | /* |
2235 | * Special case: the ! command. This is always parsed as |
2236 | * exactly two words: one containing the !, and the second |
2237 | * containing everything else on the line. |
2238 | */ |
2239 | cmd->nwords = cmd->wordssize = 2; |
3d88e64d |
2240 | cmd->words = sresize(cmd->words, cmd->wordssize, char *); |
679539d7 |
2241 | cmd->words[0] = dupstr("!"); |
2242 | cmd->words[1] = dupstr(p+1); |
1ef4dce9 |
2243 | } else if (*p == '#') { |
2244 | /* |
2245 | * Special case: comment. Entire line is ignored. |
2246 | */ |
2247 | cmd->nwords = cmd->wordssize = 0; |
3af97463 |
2248 | } else { |
2249 | |
2250 | /* |
2251 | * Parse the command line into words. The syntax is: |
2252 | * - double quotes are removed, but cause spaces within to be |
2253 | * treated as non-separating. |
2254 | * - a double-doublequote pair is a literal double quote, inside |
2255 | * _or_ outside quotes. Like this: |
2256 | * |
2257 | * firstword "second word" "this has ""quotes"" in" and""this"" |
2258 | * |
2259 | * becomes |
2260 | * |
2261 | * >firstword< |
2262 | * >second word< |
2263 | * >this has "quotes" in< |
2264 | * >and"this"< |
2265 | */ |
4c7f0d61 |
2266 | while (*p) { |
3af97463 |
2267 | /* skip whitespace */ |
2268 | while (*p && (*p == ' ' || *p == '\t')) |
2269 | p++; |
2270 | /* mark start of word */ |
2271 | q = r = p; /* q sits at start, r writes word */ |
2272 | quoting = 0; |
2273 | while (*p) { |
2274 | if (!quoting && (*p == ' ' || *p == '\t')) |
2275 | break; /* reached end of word */ |
2276 | else if (*p == '"' && p[1] == '"') |
2277 | p += 2, *r++ = '"'; /* a literal quote */ |
2278 | else if (*p == '"') |
2279 | p++, quoting = !quoting; |
2280 | else |
2281 | *r++ = *p++; |
2282 | } |
2283 | if (*p) |
2284 | p++; /* skip over the whitespace */ |
2285 | *r = '\0'; |
2286 | if (cmd->nwords >= cmd->wordssize) { |
2287 | cmd->wordssize = cmd->nwords + 16; |
3d88e64d |
2288 | cmd->words = sresize(cmd->words, cmd->wordssize, char *); |
3af97463 |
2289 | } |
679539d7 |
2290 | cmd->words[cmd->nwords++] = dupstr(q); |
4c7f0d61 |
2291 | } |
4c7f0d61 |
2292 | } |
2293 | |
39934deb |
2294 | sfree(line); |
2295 | |
4c7f0d61 |
2296 | /* |
2297 | * Now parse the first word and assign a function. |
2298 | */ |
2299 | |
2300 | if (cmd->nwords == 0) |
2301 | cmd->obey = sftp_cmd_null; |
2302 | else { |
bf5240cd |
2303 | const struct sftp_cmd_lookup *lookup; |
2304 | lookup = lookup_command(cmd->words[0]); |
2305 | if (!lookup) |
2306 | cmd->obey = sftp_cmd_unknown; |
2307 | else |
2308 | cmd->obey = lookup->obey; |
4c7f0d61 |
2309 | } |
2310 | |
2311 | return cmd; |
2312 | } |
2313 | |
774204f5 |
2314 | static int do_sftp_init(void) |
32874aea |
2315 | { |
1bc24185 |
2316 | struct sftp_packet *pktin; |
2317 | struct sftp_request *req, *rreq; |
2318 | |
4c7f0d61 |
2319 | /* |
2320 | * Do protocol initialisation. |
2321 | */ |
2322 | if (!fxp_init()) { |
2323 | fprintf(stderr, |
32874aea |
2324 | "Fatal: unable to initialise SFTP: %s\n", fxp_error()); |
774204f5 |
2325 | return 1; /* failure */ |
4c7f0d61 |
2326 | } |
2327 | |
2328 | /* |
2329 | * Find out where our home directory is. |
2330 | */ |
1bc24185 |
2331 | sftp_register(req = fxp_realpath_send(".")); |
2332 | rreq = sftp_find_request(pktin = sftp_recv()); |
2333 | assert(rreq == req); |
7b7de4f4 |
2334 | homedir = fxp_realpath_recv(pktin, rreq); |
1bc24185 |
2335 | |
4c7f0d61 |
2336 | if (!homedir) { |
2337 | fprintf(stderr, |
2338 | "Warning: failed to resolve home directory: %s\n", |
2339 | fxp_error()); |
2340 | homedir = dupstr("."); |
2341 | } else { |
2342 | printf("Remote working directory is %s\n", homedir); |
2343 | } |
2344 | pwd = dupstr(homedir); |
774204f5 |
2345 | return 0; |
fa3db767 |
2346 | } |
2347 | |
679539d7 |
2348 | void do_sftp_cleanup() |
2349 | { |
2350 | char ch; |
f11233cb |
2351 | if (back) { |
2352 | back->special(backhandle, TS_EOF); |
2353 | sftp_recvdata(&ch, 1); |
2354 | back->free(backhandle); |
2355 | sftp_cleanup_request(); |
65857773 |
2356 | back = NULL; |
2357 | backhandle = NULL; |
f11233cb |
2358 | } |
679539d7 |
2359 | if (pwd) { |
2360 | sfree(pwd); |
2361 | pwd = NULL; |
2362 | } |
2363 | if (homedir) { |
2364 | sfree(homedir); |
2365 | homedir = NULL; |
2366 | } |
2367 | } |
2368 | |
fa3db767 |
2369 | void do_sftp(int mode, int modeflags, char *batchfile) |
2370 | { |
2371 | FILE *fp; |
df49ff19 |
2372 | int ret; |
4c7f0d61 |
2373 | |
9954aaa3 |
2374 | /* |
2375 | * Batch mode? |
4c7f0d61 |
2376 | */ |
9954aaa3 |
2377 | if (mode == 0) { |
2378 | |
2379 | /* ------------------------------------------------------------------ |
2380 | * Now we're ready to do Real Stuff. |
2381 | */ |
2382 | while (1) { |
df49ff19 |
2383 | struct sftp_command *cmd; |
39934deb |
2384 | cmd = sftp_getcmd(NULL, 0, 0); |
df49ff19 |
2385 | if (!cmd) |
2386 | break; |
679539d7 |
2387 | ret = cmd->obey(cmd); |
2388 | if (cmd->words) { |
2389 | int i; |
2390 | for(i = 0; i < cmd->nwords; i++) |
2391 | sfree(cmd->words[i]); |
2392 | sfree(cmd->words); |
2393 | } |
2394 | sfree(cmd); |
2395 | if (ret < 0) |
df49ff19 |
2396 | break; |
bf5240cd |
2397 | } |
9954aaa3 |
2398 | } else { |
2399 | fp = fopen(batchfile, "r"); |
2400 | if (!fp) { |
bf5240cd |
2401 | printf("Fatal: unable to open %s\n", batchfile); |
2402 | return; |
9954aaa3 |
2403 | } |
2404 | while (1) { |
bf5240cd |
2405 | struct sftp_command *cmd; |
2406 | cmd = sftp_getcmd(fp, mode, modeflags); |
2407 | if (!cmd) |
2408 | break; |
df49ff19 |
2409 | ret = cmd->obey(cmd); |
2410 | if (ret < 0) |
bf5240cd |
2411 | break; |
df49ff19 |
2412 | if (ret == 0) { |
bf5240cd |
2413 | if (!(modeflags & 2)) |
9954aaa3 |
2414 | break; |
bf5240cd |
2415 | } |
9954aaa3 |
2416 | } |
bf5240cd |
2417 | fclose(fp); |
9954aaa3 |
2418 | |
4c7f0d61 |
2419 | } |
4a8fc3c4 |
2420 | } |
4c7f0d61 |
2421 | |
4a8fc3c4 |
2422 | /* ---------------------------------------------------------------------- |
2423 | * Dirty bits: integration with PuTTY. |
2424 | */ |
2425 | |
2426 | static int verbose = 0; |
2427 | |
7bedb13c |
2428 | /* |
4a8fc3c4 |
2429 | * Print an error message and perform a fatal exit. |
2430 | */ |
2431 | void fatalbox(char *fmt, ...) |
2432 | { |
57356d63 |
2433 | char *str, *str2; |
4a8fc3c4 |
2434 | va_list ap; |
2435 | va_start(ap, fmt); |
57356d63 |
2436 | str = dupvprintf(fmt, ap); |
2437 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
2438 | sfree(str); |
4a8fc3c4 |
2439 | va_end(ap); |
57356d63 |
2440 | fputs(str2, stderr); |
2441 | sfree(str2); |
4a8fc3c4 |
2442 | |
93b581bd |
2443 | cleanup_exit(1); |
4a8fc3c4 |
2444 | } |
1709795f |
2445 | void modalfatalbox(char *fmt, ...) |
2446 | { |
57356d63 |
2447 | char *str, *str2; |
1709795f |
2448 | va_list ap; |
2449 | va_start(ap, fmt); |
57356d63 |
2450 | str = dupvprintf(fmt, ap); |
2451 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
2452 | sfree(str); |
1709795f |
2453 | va_end(ap); |
57356d63 |
2454 | fputs(str2, stderr); |
2455 | sfree(str2); |
1709795f |
2456 | |
2457 | cleanup_exit(1); |
2458 | } |
a8327734 |
2459 | void connection_fatal(void *frontend, char *fmt, ...) |
4a8fc3c4 |
2460 | { |
57356d63 |
2461 | char *str, *str2; |
4a8fc3c4 |
2462 | va_list ap; |
2463 | va_start(ap, fmt); |
57356d63 |
2464 | str = dupvprintf(fmt, ap); |
2465 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
2466 | sfree(str); |
4a8fc3c4 |
2467 | va_end(ap); |
57356d63 |
2468 | fputs(str2, stderr); |
2469 | sfree(str2); |
4a8fc3c4 |
2470 | |
93b581bd |
2471 | cleanup_exit(1); |
4a8fc3c4 |
2472 | } |
2473 | |
6b78788a |
2474 | void ldisc_send(void *handle, char *buf, int len, int interactive) |
32874aea |
2475 | { |
4a8fc3c4 |
2476 | /* |
2477 | * This is only here because of the calls to ldisc_send(NULL, |
2478 | * 0) in ssh.c. Nothing in PSFTP actually needs to use the |
2479 | * ldisc as an ldisc. So if we get called with any real data, I |
2480 | * want to know about it. |
4c7f0d61 |
2481 | */ |
4a8fc3c4 |
2482 | assert(len == 0); |
2483 | } |
2484 | |
2485 | /* |
c44bf5bd |
2486 | * In psftp, all agent requests should be synchronous, so this is a |
2487 | * never-called stub. |
2488 | */ |
2489 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
2490 | void *callback_ctx, void *data, int len) |
2491 | { |
2492 | assert(!"We shouldn't be here"); |
2493 | } |
2494 | |
2495 | /* |
4a8fc3c4 |
2496 | * Receive a block of data from the SSH link. Block until all data |
2497 | * is available. |
2498 | * |
2499 | * To do this, we repeatedly call the SSH protocol module, with our |
2500 | * own trap in from_backend() to catch the data that comes back. We |
2501 | * do this until we have enough data. |
2502 | */ |
2503 | |
32874aea |
2504 | static unsigned char *outptr; /* where to put the data */ |
2505 | static unsigned outlen; /* how much data required */ |
4a8fc3c4 |
2506 | static unsigned char *pending = NULL; /* any spare data */ |
32874aea |
2507 | static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ |
9fab77dc |
2508 | int from_backend(void *frontend, int is_stderr, const char *data, int datalen) |
32874aea |
2509 | { |
2510 | unsigned char *p = (unsigned char *) data; |
2511 | unsigned len = (unsigned) datalen; |
4a8fc3c4 |
2512 | |
2513 | /* |
2514 | * stderr data is just spouted to local stderr and otherwise |
2515 | * ignored. |
2516 | */ |
2517 | if (is_stderr) { |
bfa5400d |
2518 | if (len > 0) |
2519 | fwrite(data, 1, len, stderr); |
5471d09a |
2520 | return 0; |
4a8fc3c4 |
2521 | } |
2522 | |
2523 | /* |
2524 | * If this is before the real session begins, just return. |
2525 | */ |
2526 | if (!outptr) |
5471d09a |
2527 | return 0; |
4a8fc3c4 |
2528 | |
bfa5400d |
2529 | if ((outlen > 0) && (len > 0)) { |
32874aea |
2530 | unsigned used = outlen; |
2531 | if (used > len) |
2532 | used = len; |
2533 | memcpy(outptr, p, used); |
2534 | outptr += used; |
2535 | outlen -= used; |
2536 | p += used; |
2537 | len -= used; |
4a8fc3c4 |
2538 | } |
2539 | |
2540 | if (len > 0) { |
32874aea |
2541 | if (pendsize < pendlen + len) { |
2542 | pendsize = pendlen + len + 4096; |
3d88e64d |
2543 | pending = sresize(pending, pendsize, unsigned char); |
32874aea |
2544 | } |
2545 | memcpy(pending + pendlen, p, len); |
2546 | pendlen += len; |
4a8fc3c4 |
2547 | } |
5471d09a |
2548 | |
2549 | return 0; |
4a8fc3c4 |
2550 | } |
edd0cb8a |
2551 | int from_backend_untrusted(void *frontend_handle, const char *data, int len) |
2552 | { |
2553 | /* |
2554 | * No "untrusted" output should get here (the way the code is |
2555 | * currently, it's all diverted by FLAG_STDERR). |
2556 | */ |
2557 | assert(!"Unexpected call to from_backend_untrusted()"); |
2558 | return 0; /* not reached */ |
2559 | } |
32874aea |
2560 | int sftp_recvdata(char *buf, int len) |
2561 | { |
2562 | outptr = (unsigned char *) buf; |
4a8fc3c4 |
2563 | outlen = len; |
2564 | |
2565 | /* |
2566 | * See if the pending-input block contains some of what we |
2567 | * need. |
2568 | */ |
2569 | if (pendlen > 0) { |
32874aea |
2570 | unsigned pendused = pendlen; |
2571 | if (pendused > outlen) |
2572 | pendused = outlen; |
4a8fc3c4 |
2573 | memcpy(outptr, pending, pendused); |
32874aea |
2574 | memmove(pending, pending + pendused, pendlen - pendused); |
4a8fc3c4 |
2575 | outptr += pendused; |
2576 | outlen -= pendused; |
32874aea |
2577 | pendlen -= pendused; |
2578 | if (pendlen == 0) { |
2579 | pendsize = 0; |
2580 | sfree(pending); |
2581 | pending = NULL; |
2582 | } |
2583 | if (outlen == 0) |
2584 | return 1; |
4a8fc3c4 |
2585 | } |
2586 | |
2587 | while (outlen > 0) { |
34580230 |
2588 | if (back->exitcode(backhandle) >= 0 || ssh_sftp_loop_iteration() < 0) |
32874aea |
2589 | return 0; /* doom */ |
4a8fc3c4 |
2590 | } |
2591 | |
2592 | return 1; |
2593 | } |
32874aea |
2594 | int sftp_senddata(char *buf, int len) |
2595 | { |
776792d7 |
2596 | back->send(backhandle, buf, len); |
4a8fc3c4 |
2597 | return 1; |
2598 | } |
2599 | |
2600 | /* |
4a8fc3c4 |
2601 | * Short description of parameters. |
2602 | */ |
2603 | static void usage(void) |
2604 | { |
2605 | printf("PuTTY Secure File Transfer (SFTP) client\n"); |
2606 | printf("%s\n", ver); |
90767715 |
2607 | printf("Usage: psftp [options] [user@]host\n"); |
4a8fc3c4 |
2608 | printf("Options:\n"); |
2285d016 |
2609 | printf(" -V print version information and exit\n"); |
2610 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
9954aaa3 |
2611 | printf(" -b file use specified batchfile\n"); |
2612 | printf(" -bc output batchfile commands\n"); |
2613 | printf(" -be don't stop batchfile processing if errors\n"); |
4a8fc3c4 |
2614 | printf(" -v show verbose messages\n"); |
e2a197cf |
2615 | printf(" -load sessname Load settings from saved session\n"); |
2616 | printf(" -l user connect with specified username\n"); |
4a8fc3c4 |
2617 | printf(" -P port connect to specified port\n"); |
2618 | printf(" -pw passw login with specified password\n"); |
e2a197cf |
2619 | printf(" -1 -2 force use of particular SSH protocol version\n"); |
05581745 |
2620 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
e2a197cf |
2621 | printf(" -C enable compression\n"); |
2622 | printf(" -i key private key file for authentication\n"); |
e5708bc7 |
2623 | printf(" -noagent disable use of Pageant\n"); |
2624 | printf(" -agent enable use of Pageant\n"); |
e2a197cf |
2625 | printf(" -batch disable all interactive prompts\n"); |
93b581bd |
2626 | cleanup_exit(1); |
4a8fc3c4 |
2627 | } |
2628 | |
dc108ebc |
2629 | static void version(void) |
2630 | { |
2631 | printf("psftp: %s\n", ver); |
2632 | cleanup_exit(1); |
2633 | } |
2634 | |
4a8fc3c4 |
2635 | /* |
fa3db767 |
2636 | * Connect to a host. |
4a8fc3c4 |
2637 | */ |
fa3db767 |
2638 | static int psftp_connect(char *userhost, char *user, int portnumber) |
4a8fc3c4 |
2639 | { |
fa3db767 |
2640 | char *host, *realhost; |
cbe2d68f |
2641 | const char *err; |
b51259f6 |
2642 | void *logctx; |
4a8fc3c4 |
2643 | |
2644 | /* Separate host and username */ |
2645 | host = userhost; |
2646 | host = strrchr(host, '@'); |
2647 | if (host == NULL) { |
2648 | host = userhost; |
2649 | } else { |
2650 | *host++ = '\0'; |
2651 | if (user) { |
32874aea |
2652 | printf("psftp: multiple usernames specified; using \"%s\"\n", |
2653 | user); |
4a8fc3c4 |
2654 | } else |
2655 | user = userhost; |
2656 | } |
2657 | |
18e62ad8 |
2658 | /* |
2659 | * If we haven't loaded session details already (e.g., from -load), |
2660 | * try looking for a session called "host". |
2661 | */ |
2662 | if (!loaded_session) { |
2663 | /* Try to load settings for `host' into a temporary config */ |
2664 | Config cfg2; |
2665 | cfg2.host[0] = '\0'; |
2666 | do_defaults(host, &cfg2); |
2667 | if (cfg2.host[0] != '\0') { |
2668 | /* Settings present and include hostname */ |
2669 | /* Re-load data into the real config. */ |
2670 | do_defaults(host, &cfg); |
2671 | } else { |
2672 | /* Session doesn't exist or mention a hostname. */ |
2673 | /* Use `host' as a bare hostname. */ |
2674 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
2675 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
2676 | } |
2677 | } else { |
2678 | /* Patch in hostname `host' to session details. */ |
32874aea |
2679 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
2680 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
f133db8e |
2681 | } |
2682 | |
2683 | /* |
2684 | * Force use of SSH. (If they got the protocol wrong we assume the |
2685 | * port is useless too.) |
2686 | */ |
2687 | if (cfg.protocol != PROT_SSH) { |
2688 | cfg.protocol = PROT_SSH; |
2689 | cfg.port = 22; |
4a8fc3c4 |
2690 | } |
2691 | |
449925a6 |
2692 | /* |
4123fa9a |
2693 | * If saved session / Default Settings says SSH-1 (`1 only' or `1'), |
2694 | * then change it to SSH-2, on the grounds that that's more likely to |
2695 | * work for SFTP. (Can be overridden with `-1' option.) |
2696 | * But if it says `2 only' or `2', respect which. |
2697 | */ |
2698 | if (cfg.sshprot != 2 && cfg.sshprot != 3) |
2699 | cfg.sshprot = 2; |
2700 | |
2701 | /* |
c0a81592 |
2702 | * Enact command-line overrides. |
2703 | */ |
5555d393 |
2704 | cmdline_run_saved(&cfg); |
c0a81592 |
2705 | |
2706 | /* |
449925a6 |
2707 | * Trim leading whitespace off the hostname if it's there. |
2708 | */ |
2709 | { |
2710 | int space = strspn(cfg.host, " \t"); |
2711 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
2712 | } |
2713 | |
2714 | /* See if host is of the form user@host */ |
2715 | if (cfg.host[0] != '\0') { |
5dd103a8 |
2716 | char *atsign = strrchr(cfg.host, '@'); |
449925a6 |
2717 | /* Make sure we're not overflowing the user field */ |
2718 | if (atsign) { |
2719 | if (atsign - cfg.host < sizeof cfg.username) { |
2720 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
2721 | cfg.username[atsign - cfg.host] = '\0'; |
2722 | } |
2723 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
2724 | } |
2725 | } |
2726 | |
2727 | /* |
2728 | * Trim a colon suffix off the hostname if it's there. |
2729 | */ |
2730 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
2731 | |
cae0c023 |
2732 | /* |
2733 | * Remove any remaining whitespace from the hostname. |
2734 | */ |
2735 | { |
2736 | int p1 = 0, p2 = 0; |
2737 | while (cfg.host[p2] != '\0') { |
2738 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
2739 | cfg.host[p1] = cfg.host[p2]; |
2740 | p1++; |
2741 | } |
2742 | p2++; |
2743 | } |
2744 | cfg.host[p1] = '\0'; |
2745 | } |
2746 | |
4a8fc3c4 |
2747 | /* Set username */ |
2748 | if (user != NULL && user[0] != '\0') { |
32874aea |
2749 | strncpy(cfg.username, user, sizeof(cfg.username) - 1); |
2750 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
4a8fc3c4 |
2751 | } |
4a8fc3c4 |
2752 | |
4a8fc3c4 |
2753 | if (portnumber) |
2754 | cfg.port = portnumber; |
2755 | |
d27b4a18 |
2756 | /* |
2757 | * Disable scary things which shouldn't be enabled for simple |
2758 | * things like SCP and SFTP: agent forwarding, port forwarding, |
2759 | * X forwarding. |
2760 | */ |
2761 | cfg.x11_forward = 0; |
2762 | cfg.agentfwd = 0; |
2763 | cfg.portfwd[0] = cfg.portfwd[1] = '\0'; |
9b53e9b8 |
2764 | cfg.ssh_simple = TRUE; |
d27b4a18 |
2765 | |
bebf22d0 |
2766 | /* Set up subsystem name. */ |
4a8fc3c4 |
2767 | strcpy(cfg.remote_cmd, "sftp"); |
2768 | cfg.ssh_subsys = TRUE; |
2769 | cfg.nopty = TRUE; |
2770 | |
bebf22d0 |
2771 | /* |
2e85c969 |
2772 | * Set up fallback option, for SSH-1 servers or servers with the |
bebf22d0 |
2773 | * sftp subsystem not enabled but the server binary installed |
2774 | * in the usual place. We only support fallback on Unix |
248c0c5a |
2775 | * systems, and we use a kludgy piece of shellery which should |
2776 | * try to find sftp-server in various places (the obvious |
2777 | * systemwide spots /usr/lib and /usr/local/lib, and then the |
2778 | * user's PATH) and finally give up. |
bebf22d0 |
2779 | * |
248c0c5a |
2780 | * test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server |
2781 | * test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server |
2782 | * exec sftp-server |
bebf22d0 |
2783 | * |
2784 | * the idea being that this will attempt to use either of the |
2785 | * obvious pathnames and then give up, and when it does give up |
2786 | * it will print the preferred pathname in the error messages. |
2787 | */ |
2788 | cfg.remote_cmd_ptr2 = |
248c0c5a |
2789 | "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" |
2790 | "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n" |
2791 | "exec sftp-server"; |
bebf22d0 |
2792 | cfg.ssh_subsys2 = FALSE; |
2793 | |
4a8fc3c4 |
2794 | back = &ssh_backend; |
2795 | |
79bf227b |
2796 | err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost, |
2797 | 0, cfg.tcp_keepalives); |
4a8fc3c4 |
2798 | if (err != NULL) { |
fa3db767 |
2799 | fprintf(stderr, "ssh_init: %s\n", err); |
4a8fc3c4 |
2800 | return 1; |
2801 | } |
c229ef97 |
2802 | logctx = log_init(NULL, &cfg); |
a8327734 |
2803 | back->provide_logctx(backhandle, logctx); |
d3fef4a5 |
2804 | console_provide_logctx(logctx); |
d6cc41e6 |
2805 | while (!back->sendok(backhandle)) { |
2e528220 |
2806 | if (back->exitcode(backhandle) >= 0) |
2807 | return 1; |
d6cc41e6 |
2808 | if (ssh_sftp_loop_iteration() < 0) { |
2809 | fprintf(stderr, "ssh_init: error during SSH connection setup\n"); |
2810 | return 1; |
2811 | } |
2812 | } |
4a8fc3c4 |
2813 | if (verbose && realhost != NULL) |
2814 | printf("Connected to %s\n", realhost); |
679539d7 |
2815 | if (realhost != NULL) |
2816 | sfree(realhost); |
fa3db767 |
2817 | return 0; |
2818 | } |
2819 | |
c0a81592 |
2820 | void cmdline_error(char *p, ...) |
2821 | { |
2822 | va_list ap; |
86256dc6 |
2823 | fprintf(stderr, "psftp: "); |
c0a81592 |
2824 | va_start(ap, p); |
2825 | vfprintf(stderr, p, ap); |
2826 | va_end(ap); |
86256dc6 |
2827 | fprintf(stderr, "\n try typing \"psftp -h\" for help\n"); |
c0a81592 |
2828 | exit(1); |
2829 | } |
2830 | |
fa3db767 |
2831 | /* |
2832 | * Main program. Parse arguments etc. |
2833 | */ |
d6cc41e6 |
2834 | int psftp_main(int argc, char *argv[]) |
fa3db767 |
2835 | { |
2836 | int i; |
2837 | int portnumber = 0; |
2838 | char *userhost, *user; |
2839 | int mode = 0; |
2840 | int modeflags = 0; |
2841 | char *batchfile = NULL; |
86256dc6 |
2842 | int errors = 0; |
fa3db767 |
2843 | |
b51259f6 |
2844 | flags = FLAG_STDERR | FLAG_INTERACTIVE |
2845 | #ifdef FLAG_SYNCAGENT |
2846 | | FLAG_SYNCAGENT |
2847 | #endif |
2848 | ; |
c0a81592 |
2849 | cmdline_tooltype = TOOLTYPE_FILETRANSFER; |
fa3db767 |
2850 | sk_init(); |
2851 | |
2852 | userhost = user = NULL; |
2853 | |
18e62ad8 |
2854 | /* Load Default Settings before doing anything else. */ |
2855 | do_defaults(NULL, &cfg); |
2856 | loaded_session = FALSE; |
2857 | |
86256dc6 |
2858 | errors = 0; |
fa3db767 |
2859 | for (i = 1; i < argc; i++) { |
c0a81592 |
2860 | int ret; |
fa3db767 |
2861 | if (argv[i][0] != '-') { |
c0a81592 |
2862 | if (userhost) |
2863 | usage(); |
2864 | else |
2865 | userhost = dupstr(argv[i]); |
2866 | continue; |
2867 | } |
5555d393 |
2868 | ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, &cfg); |
c0a81592 |
2869 | if (ret == -2) { |
2870 | cmdline_error("option \"%s\" requires an argument", argv[i]); |
2871 | } else if (ret == 2) { |
2872 | i++; /* skip next argument */ |
2873 | } else if (ret == 1) { |
2874 | /* We have our own verbosity in addition to `flags'. */ |
2875 | if (flags & FLAG_VERBOSE) |
2876 | verbose = 1; |
fa3db767 |
2877 | } else if (strcmp(argv[i], "-h") == 0 || |
2878 | strcmp(argv[i], "-?") == 0) { |
2879 | usage(); |
2285d016 |
2880 | } else if (strcmp(argv[i], "-pgpfp") == 0) { |
2881 | pgp_fingerprints(); |
2882 | return 1; |
dc108ebc |
2883 | } else if (strcmp(argv[i], "-V") == 0) { |
2884 | version(); |
c0a81592 |
2885 | } else if (strcmp(argv[i], "-batch") == 0) { |
2886 | console_batch_mode = 1; |
fa3db767 |
2887 | } else if (strcmp(argv[i], "-b") == 0 && i + 1 < argc) { |
2888 | mode = 1; |
2889 | batchfile = argv[++i]; |
d13c2ee9 |
2890 | } else if (strcmp(argv[i], "-bc") == 0) { |
fa3db767 |
2891 | modeflags = modeflags | 1; |
d13c2ee9 |
2892 | } else if (strcmp(argv[i], "-be") == 0) { |
fa3db767 |
2893 | modeflags = modeflags | 2; |
2894 | } else if (strcmp(argv[i], "--") == 0) { |
2895 | i++; |
2896 | break; |
2897 | } else { |
86256dc6 |
2898 | cmdline_error("unknown option \"%s\"", argv[i]); |
fa3db767 |
2899 | } |
2900 | } |
2901 | argc -= i; |
2902 | argv += i; |
2903 | back = NULL; |
2904 | |
2905 | /* |
e1bb41d1 |
2906 | * If the loaded session provides a hostname, and a hostname has not |
2907 | * otherwise been specified, pop it in `userhost' so that |
2908 | * `psftp -load sessname' is sufficient to start a session. |
2909 | */ |
2910 | if (!userhost && cfg.host[0] != '\0') { |
2911 | userhost = dupstr(cfg.host); |
2912 | } |
2913 | |
2914 | /* |
fa3db767 |
2915 | * If a user@host string has already been provided, connect to |
2916 | * it now. |
2917 | */ |
2918 | if (userhost) { |
679539d7 |
2919 | int ret; |
2920 | ret = psftp_connect(userhost, user, portnumber); |
2921 | sfree(userhost); |
2922 | if (ret) |
fa3db767 |
2923 | return 1; |
774204f5 |
2924 | if (do_sftp_init()) |
2925 | return 1; |
fa3db767 |
2926 | } else { |
2927 | printf("psftp: no hostname specified; use \"open host.name\"" |
679539d7 |
2928 | " to connect\n"); |
fa3db767 |
2929 | } |
4c7f0d61 |
2930 | |
9954aaa3 |
2931 | do_sftp(mode, modeflags, batchfile); |
4a8fc3c4 |
2932 | |
6226c939 |
2933 | if (back != NULL && back->connected(backhandle)) { |
4a8fc3c4 |
2934 | char ch; |
51470298 |
2935 | back->special(backhandle, TS_EOF); |
4a8fc3c4 |
2936 | sftp_recvdata(&ch, 1); |
2937 | } |
b614ce89 |
2938 | do_sftp_cleanup(); |
4a8fc3c4 |
2939 | random_save_seed(); |
679539d7 |
2940 | cmdline_cleanup(); |
2941 | console_provide_logctx(NULL); |
679539d7 |
2942 | sk_cleanup(); |
4a8fc3c4 |
2943 | |
4c7f0d61 |
2944 | return 0; |
2945 | } |