2 * psftp.c: (platform-independent) front end for PSFTP.
\r
11 #define PUTTY_DO_GLOBALS
\r
14 #include "storage.h"
\r
19 const char *const appname = "PSFTP";
\r
22 * Since SFTP is a request-response oriented protocol, it requires
\r
23 * no buffer management: when we send data, we stop and wait for an
\r
24 * acknowledgement _anyway_, and so we can't possibly overfill our
\r
28 static int psftp_connect(char *userhost, char *user, int portnumber);
\r
29 static int do_sftp_init(void);
\r
30 void do_sftp_cleanup();
\r
32 /* ----------------------------------------------------------------------
\r
33 * sftp client state.
\r
36 char *pwd, *homedir;
\r
37 static Backend *back;
\r
38 static void *backhandle;
\r
41 /* ----------------------------------------------------------------------
\r
42 * Higher-level helper functions used in commands.
\r
46 * Attempt to canonify a pathname starting from the pwd. If
\r
47 * canonification fails, at least fall back to returning a _valid_
\r
48 * pathname (though it may be ugly, eg /home/simon/../foobar).
\r
50 char *canonify(char *name)
\r
52 char *fullname, *canonname;
\r
53 struct sftp_packet *pktin;
\r
54 struct sftp_request *req, *rreq;
\r
56 if (name[0] == '/') {
\r
57 fullname = dupstr(name);
\r
60 if (pwd[strlen(pwd) - 1] == '/')
\r
64 fullname = dupcat(pwd, slash, name, NULL);
\r
67 sftp_register(req = fxp_realpath_send(fullname));
\r
68 rreq = sftp_find_request(pktin = sftp_recv());
\r
69 assert(rreq == req);
\r
70 canonname = fxp_realpath_recv(pktin, rreq);
\r
77 * Attempt number 2. Some FXP_REALPATH implementations
\r
78 * (glibc-based ones, in particular) require the _whole_
\r
79 * path to point to something that exists, whereas others
\r
80 * (BSD-based) only require all but the last component to
\r
81 * exist. So if the first call failed, we should strip off
\r
82 * everything from the last slash onwards and try again,
\r
83 * then put the final component back on.
\r
87 * - if the last component is "/." or "/..", then we don't
\r
88 * bother trying this because there's no way it can work.
\r
90 * - if the thing actually ends with a "/", we remove it
\r
91 * before we start. Except if the string is "/" itself
\r
92 * (although I can't see why we'd have got here if so,
\r
93 * because surely "/" would have worked the first
\r
94 * time?), in which case we don't bother.
\r
96 * - if there's no slash in the string at all, give up in
\r
97 * confusion (we expect at least one because of the way
\r
98 * we constructed the string).
\r
104 i = strlen(fullname);
\r
105 if (i > 2 && fullname[i - 1] == '/')
\r
106 fullname[--i] = '\0'; /* strip trailing / unless at pos 0 */
\r
107 while (i > 0 && fullname[--i] != '/');
\r
110 * Give up on special cases.
\r
112 if (fullname[i] != '/' || /* no slash at all */
\r
113 !strcmp(fullname + i, "/.") || /* ends in /. */
\r
114 !strcmp(fullname + i, "/..") || /* ends in /.. */
\r
115 !strcmp(fullname, "/")) {
\r
120 * Now i points at the slash. Deal with the final special
\r
121 * case i==0 (ie the whole path was "/nonexistentfile").
\r
123 fullname[i] = '\0'; /* separate the string */
\r
125 sftp_register(req = fxp_realpath_send("/"));
\r
127 sftp_register(req = fxp_realpath_send(fullname));
\r
129 rreq = sftp_find_request(pktin = sftp_recv());
\r
130 assert(rreq == req);
\r
131 canonname = fxp_realpath_recv(pktin, rreq);
\r
134 /* Even that failed. Restore our best guess at the
\r
135 * constructed filename and give up */
\r
136 fullname[i] = '/'; /* restore slash and last component */
\r
141 * We have a canonical name for all but the last path
\r
142 * component. Concatenate the last component and return.
\r
144 returnname = dupcat(canonname,
\r
145 canonname[strlen(canonname) - 1] ==
\r
146 '/' ? "" : "/", fullname + i + 1, NULL);
\r
154 * Return a pointer to the portion of str that comes after the last
\r
155 * slash (or backslash or colon, if `local' is TRUE).
\r
157 static char *stripslashes(char *str, int local)
\r
162 p = strchr(str, ':');
\r
166 p = strrchr(str, '/');
\r
170 p = strrchr(str, '\\');
\r
178 * qsort comparison routine for fxp_name structures. Sorts by real
\r
181 static int sftp_name_compare(const void *av, const void *bv)
\r
183 const struct fxp_name *const *a = (const struct fxp_name *const *) av;
\r
184 const struct fxp_name *const *b = (const struct fxp_name *const *) bv;
\r
185 return strcmp((*a)->filename, (*b)->filename);
\r
189 * Likewise, but for a bare char *.
\r
191 static int bare_name_compare(const void *av, const void *bv)
\r
193 const char **a = (const char **) av;
\r
194 const char **b = (const char **) bv;
\r
195 return strcmp(*a, *b);
\r
198 static void not_connected(void)
\r
200 printf("psftp: not connected to a host; use \"open host.name\"\n");
\r
203 /* ----------------------------------------------------------------------
\r
204 * The meat of the `get' and `put' commands.
\r
206 int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
\r
208 struct fxp_handle *fh;
\r
209 struct sftp_packet *pktin;
\r
210 struct sftp_request *req, *rreq;
\r
211 struct fxp_xfer *xfer;
\r
214 int ret, shown_err = FALSE;
\r
217 * In recursive mode, see if we're dealing with a directory.
\r
218 * (If we're not in recursive mode, we need not even check: the
\r
219 * subsequent FXP_OPEN will return a usable error message.)
\r
222 struct fxp_attrs attrs;
\r
225 sftp_register(req = fxp_stat_send(fname));
\r
226 rreq = sftp_find_request(pktin = sftp_recv());
\r
227 assert(rreq == req);
\r
228 result = fxp_stat_recv(pktin, rreq, &attrs);
\r
231 (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) &&
\r
232 (attrs.permissions & 0040000)) {
\r
234 struct fxp_handle *dirhandle;
\r
235 int nnames, namesize;
\r
236 struct fxp_name **ournames;
\r
237 struct fxp_names *names;
\r
241 * First, attempt to create the destination directory,
\r
242 * unless it already exists.
\r
244 if (file_type(outfname) != FILE_TYPE_DIRECTORY &&
\r
245 !create_directory(outfname)) {
\r
246 printf("%s: Cannot create directory\n", outfname);
\r
251 * Now get the list of filenames in the remote
\r
254 sftp_register(req = fxp_opendir_send(fname));
\r
255 rreq = sftp_find_request(pktin = sftp_recv());
\r
256 assert(rreq == req);
\r
257 dirhandle = fxp_opendir_recv(pktin, rreq);
\r
260 printf("%s: unable to open directory: %s\n",
\r
261 fname, fxp_error());
\r
264 nnames = namesize = 0;
\r
269 sftp_register(req = fxp_readdir_send(dirhandle));
\r
270 rreq = sftp_find_request(pktin = sftp_recv());
\r
271 assert(rreq == req);
\r
272 names = fxp_readdir_recv(pktin, rreq);
\r
274 if (names == NULL) {
\r
275 if (fxp_error_type() == SSH_FX_EOF)
\r
277 printf("%s: reading directory: %s\n", fname, fxp_error());
\r
281 if (names->nnames == 0) {
\r
282 fxp_free_names(names);
\r
285 if (nnames + names->nnames >= namesize) {
\r
286 namesize += names->nnames + 128;
\r
287 ournames = sresize(ournames, namesize, struct fxp_name *);
\r
289 for (i = 0; i < names->nnames; i++)
\r
290 if (strcmp(names->names[i].filename, ".") &&
\r
291 strcmp(names->names[i].filename, "..")) {
\r
292 if (!vet_filename(names->names[i].filename)) {
\r
293 printf("ignoring potentially dangerous server-"
\r
294 "supplied filename '%s'\n",
\r
295 names->names[i].filename);
\r
297 ournames[nnames++] =
\r
298 fxp_dup_name(&names->names[i]);
\r
301 fxp_free_names(names);
\r
303 sftp_register(req = fxp_close_send(dirhandle));
\r
304 rreq = sftp_find_request(pktin = sftp_recv());
\r
305 assert(rreq == req);
\r
306 fxp_close_recv(pktin, rreq);
\r
309 * Sort the names into a clear order. This ought to
\r
310 * make things more predictable when we're doing a
\r
311 * reget of the same directory, just in case two
\r
312 * readdirs on the same remote directory return a
\r
315 qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare);
\r
318 * If we're in restart mode, find the last filename on
\r
319 * this list that already exists. We may have to do a
\r
320 * reget on _that_ file, but shouldn't have to do
\r
321 * anything on the previous files.
\r
323 * If none of them exists, of course, we start at 0.
\r
327 while (i < nnames) {
\r
328 char *nextoutfname;
\r
331 nextoutfname = dir_file_cat(outfname,
\r
332 ournames[i]->filename);
\r
334 nextoutfname = dupstr(ournames[i]->filename);
\r
335 ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT);
\r
336 sfree(nextoutfname);
\r
346 * Now we're ready to recurse. Starting at ournames[i]
\r
347 * and continuing on to the end of the list, we
\r
348 * construct a new source and target file name, and
\r
349 * call sftp_get_file again.
\r
351 for (; i < nnames; i++) {
\r
352 char *nextfname, *nextoutfname;
\r
355 nextfname = dupcat(fname, "/", ournames[i]->filename, NULL);
\r
357 nextoutfname = dir_file_cat(outfname,
\r
358 ournames[i]->filename);
\r
360 nextoutfname = dupstr(ournames[i]->filename);
\r
361 ret = sftp_get_file(nextfname, nextoutfname, recurse, restart);
\r
362 restart = FALSE; /* after first partial file, do full */
\r
363 sfree(nextoutfname);
\r
366 for (i = 0; i < nnames; i++) {
\r
367 fxp_free_name(ournames[i]);
\r
375 * Done this recursion level. Free everything.
\r
377 for (i = 0; i < nnames; i++) {
\r
378 fxp_free_name(ournames[i]);
\r
386 sftp_register(req = fxp_open_send(fname, SSH_FXF_READ));
\r
387 rreq = sftp_find_request(pktin = sftp_recv());
\r
388 assert(rreq == req);
\r
389 fh = fxp_open_recv(pktin, rreq);
\r
392 printf("%s: open for read: %s\n", fname, fxp_error());
\r
397 file = open_existing_wfile(outfname, NULL);
\r
399 file = open_new_file(outfname);
\r
403 printf("local: unable to open %s\n", outfname);
\r
405 sftp_register(req = fxp_close_send(fh));
\r
406 rreq = sftp_find_request(pktin = sftp_recv());
\r
407 assert(rreq == req);
\r
408 fxp_close_recv(pktin, rreq);
\r
415 if (seek_file(file, uint64_make(0,0) , FROM_END) == -1) {
\r
417 printf("reget: cannot restart %s - file too large\n",
\r
419 sftp_register(req = fxp_close_send(fh));
\r
420 rreq = sftp_find_request(pktin = sftp_recv());
\r
421 assert(rreq == req);
\r
422 fxp_close_recv(pktin, rreq);
\r
427 offset = get_file_posn(file);
\r
428 uint64_decimal(offset, decbuf);
\r
429 printf("reget: restarting at file position %s\n", decbuf);
\r
431 offset = uint64_make(0, 0);
\r
434 printf("remote:%s => local:%s\n", fname, outfname);
\r
437 * FIXME: we can use FXP_FSTAT here to get the file size, and
\r
438 * thus put up a progress bar.
\r
441 xfer = xfer_download_init(fh, offset);
\r
442 while (!xfer_done(xfer)) {
\r
447 xfer_download_queue(xfer);
\r
448 pktin = sftp_recv();
\r
449 ret = xfer_download_gotpkt(xfer, pktin);
\r
453 printf("error while reading: %s\n", fxp_error());
\r
459 while (xfer_download_data(xfer, &vbuf, &len)) {
\r
460 unsigned char *buf = (unsigned char *)vbuf;
\r
463 while (wpos < len) {
\r
464 wlen = write_to_file(file, buf + wpos, len - wpos);
\r
466 printf("error while writing local file\n");
\r
468 xfer_set_error(xfer);
\r
473 if (wpos < len) { /* we had an error */
\r
475 xfer_set_error(xfer);
\r
482 xfer_cleanup(xfer);
\r
486 sftp_register(req = fxp_close_send(fh));
\r
487 rreq = sftp_find_request(pktin = sftp_recv());
\r
488 assert(rreq == req);
\r
489 fxp_close_recv(pktin, rreq);
\r
494 int sftp_put_file(char *fname, char *outfname, int recurse, int restart)
\r
496 struct fxp_handle *fh;
\r
497 struct fxp_xfer *xfer;
\r
498 struct sftp_packet *pktin;
\r
499 struct sftp_request *req, *rreq;
\r
505 * In recursive mode, see if we're dealing with a directory.
\r
506 * (If we're not in recursive mode, we need not even check: the
\r
507 * subsequent fopen will return an error message.)
\r
509 if (recurse && file_type(fname) == FILE_TYPE_DIRECTORY) {
\r
510 struct fxp_attrs attrs;
\r
512 int nnames, namesize;
\r
513 char *name, **ournames;
\r
518 * First, attempt to create the destination directory,
\r
519 * unless it already exists.
\r
521 sftp_register(req = fxp_stat_send(outfname));
\r
522 rreq = sftp_find_request(pktin = sftp_recv());
\r
523 assert(rreq == req);
\r
524 result = fxp_stat_recv(pktin, rreq, &attrs);
\r
526 !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) ||
\r
527 !(attrs.permissions & 0040000)) {
\r
528 sftp_register(req = fxp_mkdir_send(outfname));
\r
529 rreq = sftp_find_request(pktin = sftp_recv());
\r
530 assert(rreq == req);
\r
531 result = fxp_mkdir_recv(pktin, rreq);
\r
534 printf("%s: create directory: %s\n",
\r
535 outfname, fxp_error());
\r
541 * Now get the list of filenames in the local directory.
\r
543 nnames = namesize = 0;
\r
546 dh = open_directory(fname);
\r
548 printf("%s: unable to open directory\n", fname);
\r
551 while ((name = read_filename(dh)) != NULL) {
\r
552 if (nnames >= namesize) {
\r
554 ournames = sresize(ournames, namesize, char *);
\r
556 ournames[nnames++] = name;
\r
558 close_directory(dh);
\r
561 * Sort the names into a clear order. This ought to make
\r
562 * things more predictable when we're doing a reput of the
\r
563 * same directory, just in case two readdirs on the same
\r
564 * local directory return a different order.
\r
566 qsort(ournames, nnames, sizeof(*ournames), bare_name_compare);
\r
569 * If we're in restart mode, find the last filename on this
\r
570 * list that already exists. We may have to do a reput on
\r
571 * _that_ file, but shouldn't have to do anything on the
\r
574 * If none of them exists, of course, we start at 0.
\r
578 while (i < nnames) {
\r
579 char *nextoutfname;
\r
580 nextoutfname = dupcat(outfname, "/", ournames[i], NULL);
\r
581 sftp_register(req = fxp_stat_send(nextoutfname));
\r
582 rreq = sftp_find_request(pktin = sftp_recv());
\r
583 assert(rreq == req);
\r
584 result = fxp_stat_recv(pktin, rreq, &attrs);
\r
585 sfree(nextoutfname);
\r
595 * Now we're ready to recurse. Starting at ournames[i]
\r
596 * and continuing on to the end of the list, we
\r
597 * construct a new source and target file name, and
\r
598 * call sftp_put_file again.
\r
600 for (; i < nnames; i++) {
\r
601 char *nextfname, *nextoutfname;
\r
605 nextfname = dir_file_cat(fname, ournames[i]);
\r
607 nextfname = dupstr(ournames[i]);
\r
608 nextoutfname = dupcat(outfname, "/", ournames[i], NULL);
\r
609 ret = sftp_put_file(nextfname, nextoutfname, recurse, restart);
\r
610 restart = FALSE; /* after first partial file, do full */
\r
611 sfree(nextoutfname);
\r
614 for (i = 0; i < nnames; i++) {
\r
615 sfree(ournames[i]);
\r
623 * Done this recursion level. Free everything.
\r
625 for (i = 0; i < nnames; i++) {
\r
626 sfree(ournames[i]);
\r
633 file = open_existing_file(fname, NULL, NULL, NULL);
\r
635 printf("local: unable to open %s\n", fname);
\r
639 sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE));
\r
641 sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE |
\r
642 SSH_FXF_CREAT | SSH_FXF_TRUNC));
\r
644 rreq = sftp_find_request(pktin = sftp_recv());
\r
645 assert(rreq == req);
\r
646 fh = fxp_open_recv(pktin, rreq);
\r
650 printf("%s: open for write: %s\n", outfname, fxp_error());
\r
656 struct fxp_attrs attrs;
\r
659 sftp_register(req = fxp_fstat_send(fh));
\r
660 rreq = sftp_find_request(pktin = sftp_recv());
\r
661 assert(rreq == req);
\r
662 ret = fxp_fstat_recv(pktin, rreq, &attrs);
\r
666 printf("read size of %s: %s\n", outfname, fxp_error());
\r
669 if (!(attrs.flags & SSH_FILEXFER_ATTR_SIZE)) {
\r
671 printf("read size of %s: size was not given\n", outfname);
\r
674 offset = attrs.size;
\r
675 uint64_decimal(offset, decbuf);
\r
676 printf("reput: restarting at file position %s\n", decbuf);
\r
678 if (seek_file((WFile *)file, offset, FROM_START) != 0)
\r
679 seek_file((WFile *)file, uint64_make(0,0), FROM_END); /* *shrug* */
\r
681 offset = uint64_make(0, 0);
\r
684 printf("local:%s => remote:%s\n", fname, outfname);
\r
687 * FIXME: we can use FXP_FSTAT here to get the file size, and
\r
688 * thus put up a progress bar.
\r
691 xfer = xfer_upload_init(fh, offset);
\r
693 while ((!err && !eof) || !xfer_done(xfer)) {
\r
697 while (xfer_upload_ready(xfer) && !err && !eof) {
\r
698 len = read_from_file(file, buffer, sizeof(buffer));
\r
700 printf("error while reading local file\n");
\r
702 } else if (len == 0) {
\r
705 xfer_upload_data(xfer, buffer, len);
\r
709 if (!xfer_done(xfer)) {
\r
710 pktin = sftp_recv();
\r
711 ret = xfer_upload_gotpkt(xfer, pktin);
\r
712 if (ret <= 0 && !err) {
\r
713 printf("error while writing: %s\n", fxp_error());
\r
719 xfer_cleanup(xfer);
\r
721 sftp_register(req = fxp_close_send(fh));
\r
722 rreq = sftp_find_request(pktin = sftp_recv());
\r
723 assert(rreq == req);
\r
724 fxp_close_recv(pktin, rreq);
\r
731 /* ----------------------------------------------------------------------
\r
732 * A remote wildcard matcher, providing a similar interface to the
\r
733 * local one in psftp.h.
\r
736 typedef struct SftpWildcardMatcher {
\r
737 struct fxp_handle *dirh;
\r
738 struct fxp_names *names;
\r
740 char *wildcard, *prefix;
\r
741 } SftpWildcardMatcher;
\r
743 SftpWildcardMatcher *sftp_begin_wildcard_matching(char *name)
\r
745 struct sftp_packet *pktin;
\r
746 struct sftp_request *req, *rreq;
\r
748 char *unwcdir, *tmpdir, *cdir;
\r
750 SftpWildcardMatcher *swcm;
\r
751 struct fxp_handle *dirh;
\r
754 * We don't handle multi-level wildcards; so we expect to find
\r
755 * a fully specified directory part, followed by a wildcard
\r
758 wildcard = stripslashes(name, 0);
\r
760 unwcdir = dupstr(name);
\r
761 len = wildcard - name;
\r
762 unwcdir[len] = '\0';
\r
763 if (len > 0 && unwcdir[len-1] == '/')
\r
764 unwcdir[len-1] = '\0';
\r
765 tmpdir = snewn(1 + len, char);
\r
766 check = wc_unescape(tmpdir, unwcdir);
\r
770 printf("Multiple-level wildcards are not supported\n");
\r
775 cdir = canonify(unwcdir);
\r
777 sftp_register(req = fxp_opendir_send(cdir));
\r
778 rreq = sftp_find_request(pktin = sftp_recv());
\r
779 assert(rreq == req);
\r
780 dirh = fxp_opendir_recv(pktin, rreq);
\r
783 swcm = snew(SftpWildcardMatcher);
\r
785 swcm->names = NULL;
\r
786 swcm->wildcard = dupstr(wildcard);
\r
787 swcm->prefix = unwcdir;
\r
789 printf("Unable to open %s: %s\n", cdir, fxp_error());
\r
799 char *sftp_wildcard_get_filename(SftpWildcardMatcher *swcm)
\r
801 struct fxp_name *name;
\r
802 struct sftp_packet *pktin;
\r
803 struct sftp_request *req, *rreq;
\r
806 if (swcm->names && swcm->namepos >= swcm->names->nnames) {
\r
807 fxp_free_names(swcm->names);
\r
808 swcm->names = NULL;
\r
811 if (!swcm->names) {
\r
812 sftp_register(req = fxp_readdir_send(swcm->dirh));
\r
813 rreq = sftp_find_request(pktin = sftp_recv());
\r
814 assert(rreq == req);
\r
815 swcm->names = fxp_readdir_recv(pktin, rreq);
\r
817 if (!swcm->names) {
\r
818 if (fxp_error_type() != SSH_FX_EOF)
\r
819 printf("%s: reading directory: %s\n", swcm->prefix,
\r
827 assert(swcm->names && swcm->namepos < swcm->names->nnames);
\r
829 name = &swcm->names->names[swcm->namepos++];
\r
831 if (!strcmp(name->filename, ".") || !strcmp(name->filename, ".."))
\r
832 continue; /* expected bad filenames */
\r
834 if (!vet_filename(name->filename)) {
\r
835 printf("ignoring potentially dangerous server-"
\r
836 "supplied filename '%s'\n", name->filename);
\r
837 continue; /* unexpected bad filename */
\r
840 if (!wc_match(swcm->wildcard, name->filename))
\r
841 continue; /* doesn't match the wildcard */
\r
844 * We have a working filename. Return it.
\r
846 return dupprintf("%s%s%s", swcm->prefix,
\r
847 (!swcm->prefix[0] ||
\r
848 swcm->prefix[strlen(swcm->prefix)-1]=='/' ?
\r
854 void sftp_finish_wildcard_matching(SftpWildcardMatcher *swcm)
\r
856 struct sftp_packet *pktin;
\r
857 struct sftp_request *req, *rreq;
\r
859 sftp_register(req = fxp_close_send(swcm->dirh));
\r
860 rreq = sftp_find_request(pktin = sftp_recv());
\r
861 assert(rreq == req);
\r
862 fxp_close_recv(pktin, rreq);
\r
865 fxp_free_names(swcm->names);
\r
867 sfree(swcm->prefix);
\r
868 sfree(swcm->wildcard);
\r
874 * General function to match a potential wildcard in a filename
\r
875 * argument and iterate over every matching file. Used in several
\r
876 * PSFTP commands (rmdir, rm, chmod, mv).
\r
878 int wildcard_iterate(char *filename, int (*func)(void *, char *), void *ctx)
\r
880 char *unwcfname, *newname, *cname;
\r
883 unwcfname = snewn(strlen(filename)+1, char);
\r
884 is_wc = !wc_unescape(unwcfname, filename);
\r
887 SftpWildcardMatcher *swcm = sftp_begin_wildcard_matching(filename);
\r
888 int matched = FALSE;
\r
896 while ( (newname = sftp_wildcard_get_filename(swcm)) != NULL ) {
\r
897 cname = canonify(newname);
\r
899 printf("%s: canonify: %s\n", newname, fxp_error());
\r
903 ret &= func(ctx, cname);
\r
908 /* Politely warn the user that nothing matched. */
\r
909 printf("%s: nothing matched\n", filename);
\r
912 sftp_finish_wildcard_matching(swcm);
\r
914 cname = canonify(unwcfname);
\r
916 printf("%s: canonify: %s\n", filename, fxp_error());
\r
919 ret = func(ctx, cname);
\r
928 * Handy helper function.
\r
930 int is_wildcard(char *name)
\r
932 char *unwcfname = snewn(strlen(name)+1, char);
\r
933 int is_wc = !wc_unescape(unwcfname, name);
\r
938 /* ----------------------------------------------------------------------
\r
939 * Actual sftp commands.
\r
941 struct sftp_command {
\r
943 int nwords, wordssize;
\r
944 int (*obey) (struct sftp_command *); /* returns <0 to quit */
\r
947 int sftp_cmd_null(struct sftp_command *cmd)
\r
949 return 1; /* success */
\r
952 int sftp_cmd_unknown(struct sftp_command *cmd)
\r
954 printf("psftp: unknown command \"%s\"\n", cmd->words[0]);
\r
955 return 0; /* failure */
\r
958 int sftp_cmd_quit(struct sftp_command *cmd)
\r
963 int sftp_cmd_close(struct sftp_command *cmd)
\r
965 if (back == NULL) {
\r
970 if (back != NULL && back->connected(backhandle)) {
\r
972 back->special(backhandle, TS_EOF);
\r
973 sftp_recvdata(&ch, 1);
\r
981 * List a directory. If no arguments are given, list pwd; otherwise
\r
982 * list the directory given in words[1].
\r
984 int sftp_cmd_ls(struct sftp_command *cmd)
\r
986 struct fxp_handle *dirh;
\r
987 struct fxp_names *names;
\r
988 struct fxp_name **ournames;
\r
989 int nnames, namesize;
\r
990 char *dir, *cdir, *unwcdir, *wildcard;
\r
991 struct sftp_packet *pktin;
\r
992 struct sftp_request *req, *rreq;
\r
995 if (back == NULL) {
\r
1000 if (cmd->nwords < 2)
\r
1003 dir = cmd->words[1];
\r
1005 unwcdir = snewn(1 + strlen(dir), char);
\r
1006 if (wc_unescape(unwcdir, dir)) {
\r
1013 wildcard = stripslashes(dir, 0);
\r
1014 unwcdir = dupstr(dir);
\r
1015 len = wildcard - dir;
\r
1016 unwcdir[len] = '\0';
\r
1017 if (len > 0 && unwcdir[len-1] == '/')
\r
1018 unwcdir[len-1] = '\0';
\r
1019 tmpdir = snewn(1 + len, char);
\r
1020 check = wc_unescape(tmpdir, unwcdir);
\r
1023 printf("Multiple-level wildcards are not supported\n");
\r
1030 cdir = canonify(dir);
\r
1032 printf("%s: canonify: %s\n", dir, fxp_error());
\r
1037 printf("Listing directory %s\n", cdir);
\r
1039 sftp_register(req = fxp_opendir_send(cdir));
\r
1040 rreq = sftp_find_request(pktin = sftp_recv());
\r
1041 assert(rreq == req);
\r
1042 dirh = fxp_opendir_recv(pktin, rreq);
\r
1044 if (dirh == NULL) {
\r
1045 printf("Unable to open %s: %s\n", dir, fxp_error());
\r
1047 nnames = namesize = 0;
\r
1052 sftp_register(req = fxp_readdir_send(dirh));
\r
1053 rreq = sftp_find_request(pktin = sftp_recv());
\r
1054 assert(rreq == req);
\r
1055 names = fxp_readdir_recv(pktin, rreq);
\r
1057 if (names == NULL) {
\r
1058 if (fxp_error_type() == SSH_FX_EOF)
\r
1060 printf("Reading directory %s: %s\n", dir, fxp_error());
\r
1063 if (names->nnames == 0) {
\r
1064 fxp_free_names(names);
\r
1068 if (nnames + names->nnames >= namesize) {
\r
1069 namesize += names->nnames + 128;
\r
1070 ournames = sresize(ournames, namesize, struct fxp_name *);
\r
1073 for (i = 0; i < names->nnames; i++)
\r
1074 if (!wildcard || wc_match(wildcard, names->names[i].filename))
\r
1075 ournames[nnames++] = fxp_dup_name(&names->names[i]);
\r
1077 fxp_free_names(names);
\r
1079 sftp_register(req = fxp_close_send(dirh));
\r
1080 rreq = sftp_find_request(pktin = sftp_recv());
\r
1081 assert(rreq == req);
\r
1082 fxp_close_recv(pktin, rreq);
\r
1085 * Now we have our filenames. Sort them by actual file
\r
1086 * name, and then output the longname parts.
\r
1088 qsort(ournames, nnames, sizeof(*ournames), sftp_name_compare);
\r
1093 for (i = 0; i < nnames; i++) {
\r
1094 printf("%s\n", ournames[i]->longname);
\r
1095 fxp_free_name(ournames[i]);
\r
1107 * Change directories. We do this by canonifying the new name, then
\r
1108 * trying to OPENDIR it. Only if that succeeds do we set the new pwd.
\r
1110 int sftp_cmd_cd(struct sftp_command *cmd)
\r
1112 struct fxp_handle *dirh;
\r
1113 struct sftp_packet *pktin;
\r
1114 struct sftp_request *req, *rreq;
\r
1117 if (back == NULL) {
\r
1122 if (cmd->nwords < 2)
\r
1123 dir = dupstr(homedir);
\r
1125 dir = canonify(cmd->words[1]);
\r
1128 printf("%s: canonify: %s\n", dir, fxp_error());
\r
1132 sftp_register(req = fxp_opendir_send(dir));
\r
1133 rreq = sftp_find_request(pktin = sftp_recv());
\r
1134 assert(rreq == req);
\r
1135 dirh = fxp_opendir_recv(pktin, rreq);
\r
1138 printf("Directory %s: %s\n", dir, fxp_error());
\r
1143 sftp_register(req = fxp_close_send(dirh));
\r
1144 rreq = sftp_find_request(pktin = sftp_recv());
\r
1145 assert(rreq == req);
\r
1146 fxp_close_recv(pktin, rreq);
\r
1150 printf("Remote directory is now %s\n", pwd);
\r
1156 * Print current directory. Easy as pie.
\r
1158 int sftp_cmd_pwd(struct sftp_command *cmd)
\r
1160 if (back == NULL) {
\r
1165 printf("Remote directory is %s\n", pwd);
\r
1170 * Get a file and save it at the local end. We have three very
\r
1171 * similar commands here. The basic one is `get'; `reget' differs
\r
1172 * in that it checks for the existence of the destination file and
\r
1173 * starts from where a previous aborted transfer left off; `mget'
\r
1174 * differs in that it interprets all its arguments as files to
\r
1175 * transfer (never as a different local name for a remote file) and
\r
1176 * can handle wildcards.
\r
1178 int sftp_general_get(struct sftp_command *cmd, int restart, int multiple)
\r
1180 char *fname, *unwcfname, *origfname, *origwfname, *outfname;
\r
1182 int recurse = FALSE;
\r
1184 if (back == NULL) {
\r
1190 while (i < cmd->nwords && cmd->words[i][0] == '-') {
\r
1191 if (!strcmp(cmd->words[i], "--")) {
\r
1192 /* finish processing options */
\r
1195 } else if (!strcmp(cmd->words[i], "-r")) {
\r
1198 printf("%s: unrecognised option '%s'\n", cmd->words[0], cmd->words[i]);
\r
1204 if (i >= cmd->nwords) {
\r
1205 printf("%s: expects a filename\n", cmd->words[0]);
\r
1211 SftpWildcardMatcher *swcm;
\r
1213 origfname = cmd->words[i++];
\r
1214 unwcfname = snewn(strlen(origfname)+1, char);
\r
1216 if (multiple && !wc_unescape(unwcfname, origfname)) {
\r
1217 swcm = sftp_begin_wildcard_matching(origfname);
\r
1222 origwfname = sftp_wildcard_get_filename(swcm);
\r
1223 if (!origwfname) {
\r
1224 /* Politely warn the user that nothing matched. */
\r
1225 printf("%s: nothing matched\n", origfname);
\r
1226 sftp_finish_wildcard_matching(swcm);
\r
1231 origwfname = origfname;
\r
1235 while (origwfname) {
\r
1236 fname = canonify(origwfname);
\r
1239 printf("%s: canonify: %s\n", origwfname, fxp_error());
\r
1244 if (!multiple && i < cmd->nwords)
\r
1245 outfname = cmd->words[i++];
\r
1247 outfname = stripslashes(origwfname, 0);
\r
1249 ret = sftp_get_file(fname, outfname, recurse, restart);
\r
1254 sfree(origwfname);
\r
1255 origwfname = sftp_wildcard_get_filename(swcm);
\r
1257 origwfname = NULL;
\r
1262 sftp_finish_wildcard_matching(swcm);
\r
1266 } while (multiple && i < cmd->nwords);
\r
1270 int sftp_cmd_get(struct sftp_command *cmd)
\r
1272 return sftp_general_get(cmd, 0, 0);
\r
1274 int sftp_cmd_mget(struct sftp_command *cmd)
\r
1276 return sftp_general_get(cmd, 0, 1);
\r
1278 int sftp_cmd_reget(struct sftp_command *cmd)
\r
1280 return sftp_general_get(cmd, 1, 0);
\r
1284 * Send a file and store it at the remote end. We have three very
\r
1285 * similar commands here. The basic one is `put'; `reput' differs
\r
1286 * in that it checks for the existence of the destination file and
\r
1287 * starts from where a previous aborted transfer left off; `mput'
\r
1288 * differs in that it interprets all its arguments as files to
\r
1289 * transfer (never as a different remote name for a local file) and
\r
1290 * can handle wildcards.
\r
1292 int sftp_general_put(struct sftp_command *cmd, int restart, int multiple)
\r
1294 char *fname, *wfname, *origoutfname, *outfname;
\r
1296 int recurse = FALSE;
\r
1298 if (back == NULL) {
\r
1304 while (i < cmd->nwords && cmd->words[i][0] == '-') {
\r
1305 if (!strcmp(cmd->words[i], "--")) {
\r
1306 /* finish processing options */
\r
1309 } else if (!strcmp(cmd->words[i], "-r")) {
\r
1312 printf("%s: unrecognised option '%s'\n", cmd->words[0], cmd->words[i]);
\r
1318 if (i >= cmd->nwords) {
\r
1319 printf("%s: expects a filename\n", cmd->words[0]);
\r
1325 WildcardMatcher *wcm;
\r
1326 fname = cmd->words[i++];
\r
1328 if (multiple && test_wildcard(fname, FALSE) == WCTYPE_WILDCARD) {
\r
1329 wcm = begin_wildcard_matching(fname);
\r
1330 wfname = wildcard_get_filename(wcm);
\r
1332 /* Politely warn the user that nothing matched. */
\r
1333 printf("%s: nothing matched\n", fname);
\r
1334 finish_wildcard_matching(wcm);
\r
1343 if (!multiple && i < cmd->nwords)
\r
1344 origoutfname = cmd->words[i++];
\r
1346 origoutfname = stripslashes(wfname, 1);
\r
1348 outfname = canonify(origoutfname);
\r
1350 printf("%s: canonify: %s\n", origoutfname, fxp_error());
\r
1353 finish_wildcard_matching(wcm);
\r
1357 ret = sftp_put_file(wfname, outfname, recurse, restart);
\r
1362 wfname = wildcard_get_filename(wcm);
\r
1369 finish_wildcard_matching(wcm);
\r
1374 } while (multiple && i < cmd->nwords);
\r
1378 int sftp_cmd_put(struct sftp_command *cmd)
\r
1380 return sftp_general_put(cmd, 0, 0);
\r
1382 int sftp_cmd_mput(struct sftp_command *cmd)
\r
1384 return sftp_general_put(cmd, 0, 1);
\r
1386 int sftp_cmd_reput(struct sftp_command *cmd)
\r
1388 return sftp_general_put(cmd, 1, 0);
\r
1391 int sftp_cmd_mkdir(struct sftp_command *cmd)
\r
1394 struct sftp_packet *pktin;
\r
1395 struct sftp_request *req, *rreq;
\r
1399 if (back == NULL) {
\r
1404 if (cmd->nwords < 2) {
\r
1405 printf("mkdir: expects a directory\n");
\r
1410 for (i = 1; i < cmd->nwords; i++) {
\r
1411 dir = canonify(cmd->words[i]);
\r
1413 printf("%s: canonify: %s\n", dir, fxp_error());
\r
1417 sftp_register(req = fxp_mkdir_send(dir));
\r
1418 rreq = sftp_find_request(pktin = sftp_recv());
\r
1419 assert(rreq == req);
\r
1420 result = fxp_mkdir_recv(pktin, rreq);
\r
1423 printf("mkdir %s: %s\n", dir, fxp_error());
\r
1426 printf("mkdir %s: OK\n", dir);
\r
1434 static int sftp_action_rmdir(void *vctx, char *dir)
\r
1436 struct sftp_packet *pktin;
\r
1437 struct sftp_request *req, *rreq;
\r
1440 sftp_register(req = fxp_rmdir_send(dir));
\r
1441 rreq = sftp_find_request(pktin = sftp_recv());
\r
1442 assert(rreq == req);
\r
1443 result = fxp_rmdir_recv(pktin, rreq);
\r
1446 printf("rmdir %s: %s\n", dir, fxp_error());
\r
1450 printf("rmdir %s: OK\n", dir);
\r
1455 int sftp_cmd_rmdir(struct sftp_command *cmd)
\r
1459 if (back == NULL) {
\r
1464 if (cmd->nwords < 2) {
\r
1465 printf("rmdir: expects a directory\n");
\r
1470 for (i = 1; i < cmd->nwords; i++)
\r
1471 ret &= wildcard_iterate(cmd->words[i], sftp_action_rmdir, NULL);
\r
1476 static int sftp_action_rm(void *vctx, char *fname)
\r
1478 struct sftp_packet *pktin;
\r
1479 struct sftp_request *req, *rreq;
\r
1482 sftp_register(req = fxp_remove_send(fname));
\r
1483 rreq = sftp_find_request(pktin = sftp_recv());
\r
1484 assert(rreq == req);
\r
1485 result = fxp_remove_recv(pktin, rreq);
\r
1488 printf("rm %s: %s\n", fname, fxp_error());
\r
1492 printf("rm %s: OK\n", fname);
\r
1497 int sftp_cmd_rm(struct sftp_command *cmd)
\r
1501 if (back == NULL) {
\r
1506 if (cmd->nwords < 2) {
\r
1507 printf("rm: expects a filename\n");
\r
1512 for (i = 1; i < cmd->nwords; i++)
\r
1513 ret &= wildcard_iterate(cmd->words[i], sftp_action_rm, NULL);
\r
1518 static int check_is_dir(char *dstfname)
\r
1520 struct sftp_packet *pktin;
\r
1521 struct sftp_request *req, *rreq;
\r
1522 struct fxp_attrs attrs;
\r
1525 sftp_register(req = fxp_stat_send(dstfname));
\r
1526 rreq = sftp_find_request(pktin = sftp_recv());
\r
1527 assert(rreq == req);
\r
1528 result = fxp_stat_recv(pktin, rreq, &attrs);
\r
1531 (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) &&
\r
1532 (attrs.permissions & 0040000))
\r
1538 struct sftp_context_mv {
\r
1543 static int sftp_action_mv(void *vctx, char *srcfname)
\r
1545 struct sftp_context_mv *ctx = (struct sftp_context_mv *)vctx;
\r
1546 struct sftp_packet *pktin;
\r
1547 struct sftp_request *req, *rreq;
\r
1548 const char *error;
\r
1549 char *finalfname, *newcanon = NULL;
\r
1552 if (ctx->dest_is_dir) {
\r
1556 p = srcfname + strlen(srcfname);
\r
1557 while (p > srcfname && p[-1] != '/') p--;
\r
1558 newname = dupcat(ctx->dstfname, "/", p, NULL);
\r
1559 newcanon = canonify(newname);
\r
1561 printf("%s: canonify: %s\n", newname, fxp_error());
\r
1567 finalfname = newcanon;
\r
1569 finalfname = ctx->dstfname;
\r
1572 sftp_register(req = fxp_rename_send(srcfname, finalfname));
\r
1573 rreq = sftp_find_request(pktin = sftp_recv());
\r
1574 assert(rreq == req);
\r
1575 result = fxp_rename_recv(pktin, rreq);
\r
1577 error = result ? NULL : fxp_error();
\r
1580 printf("mv %s %s: %s\n", srcfname, finalfname, error);
\r
1583 printf("%s -> %s\n", srcfname, finalfname);
\r
1591 int sftp_cmd_mv(struct sftp_command *cmd)
\r
1593 struct sftp_context_mv actx, *ctx = &actx;
\r
1596 if (back == NULL) {
\r
1601 if (cmd->nwords < 3) {
\r
1602 printf("mv: expects two filenames\n");
\r
1606 ctx->dstfname = canonify(cmd->words[cmd->nwords-1]);
\r
1607 if (!ctx->dstfname) {
\r
1608 printf("%s: canonify: %s\n", ctx->dstfname, fxp_error());
\r
1613 * If there's more than one source argument, or one source
\r
1614 * argument which is a wildcard, we _require_ that the
\r
1615 * destination is a directory.
\r
1617 ctx->dest_is_dir = check_is_dir(ctx->dstfname);
\r
1618 if ((cmd->nwords > 3 || is_wildcard(cmd->words[1])) && !ctx->dest_is_dir) {
\r
1619 printf("mv: multiple or wildcard arguments require the destination"
\r
1620 " to be a directory\n");
\r
1621 sfree(ctx->dstfname);
\r
1626 * Now iterate over the source arguments.
\r
1629 for (i = 1; i < cmd->nwords-1; i++)
\r
1630 ret &= wildcard_iterate(cmd->words[i], sftp_action_mv, ctx);
\r
1632 sfree(ctx->dstfname);
\r
1636 struct sftp_context_chmod {
\r
1637 unsigned attrs_clr, attrs_xor;
\r
1640 static int sftp_action_chmod(void *vctx, char *fname)
\r
1642 struct fxp_attrs attrs;
\r
1643 struct sftp_packet *pktin;
\r
1644 struct sftp_request *req, *rreq;
\r
1646 unsigned oldperms, newperms;
\r
1647 struct sftp_context_chmod *ctx = (struct sftp_context_chmod *)vctx;
\r
1649 sftp_register(req = fxp_stat_send(fname));
\r
1650 rreq = sftp_find_request(pktin = sftp_recv());
\r
1651 assert(rreq == req);
\r
1652 result = fxp_stat_recv(pktin, rreq, &attrs);
\r
1654 if (!result || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
\r
1655 printf("get attrs for %s: %s\n", fname,
\r
1656 result ? "file permissions not provided" : fxp_error());
\r
1660 attrs.flags = SSH_FILEXFER_ATTR_PERMISSIONS; /* perms _only_ */
\r
1661 oldperms = attrs.permissions & 07777;
\r
1662 attrs.permissions &= ~ctx->attrs_clr;
\r
1663 attrs.permissions ^= ctx->attrs_xor;
\r
1664 newperms = attrs.permissions & 07777;
\r
1666 if (oldperms == newperms)
\r
1667 return 1; /* no need to do anything! */
\r
1669 sftp_register(req = fxp_setstat_send(fname, attrs));
\r
1670 rreq = sftp_find_request(pktin = sftp_recv());
\r
1671 assert(rreq == req);
\r
1672 result = fxp_setstat_recv(pktin, rreq);
\r
1675 printf("set attrs for %s: %s\n", fname, fxp_error());
\r
1679 printf("%s: %04o -> %04o\n", fname, oldperms, newperms);
\r
1684 int sftp_cmd_chmod(struct sftp_command *cmd)
\r
1688 struct sftp_context_chmod actx, *ctx = &actx;
\r
1690 if (back == NULL) {
\r
1695 if (cmd->nwords < 3) {
\r
1696 printf("chmod: expects a mode specifier and a filename\n");
\r
1701 * Attempt to parse the mode specifier in cmd->words[1]. We
\r
1702 * don't support the full horror of Unix chmod; instead we
\r
1703 * support a much simpler syntax in which the user can either
\r
1704 * specify an octal number, or a comma-separated sequence of
\r
1705 * [ugoa]*[-+=][rwxst]+. (The initial [ugoa] sequence may
\r
1706 * _only_ be omitted if the only attribute mentioned is t,
\r
1707 * since all others require a user/group/other specification.
\r
1708 * Additionally, the s attribute may not be specified for any
\r
1709 * [ugoa] specifications other than exactly u or exactly g.
\r
1711 ctx->attrs_clr = ctx->attrs_xor = 0;
\r
1712 mode = cmd->words[1];
\r
1713 if (mode[0] >= '0' && mode[0] <= '9') {
\r
1714 if (mode[strspn(mode, "01234567")]) {
\r
1715 printf("chmod: numeric file modes should"
\r
1716 " contain digits 0-7 only\n");
\r
1719 ctx->attrs_clr = 07777;
\r
1720 sscanf(mode, "%o", &ctx->attrs_xor);
\r
1721 ctx->attrs_xor &= ctx->attrs_clr;
\r
1724 char *modebegin = mode;
\r
1725 unsigned subset, perms;
\r
1729 while (*mode && *mode != ',' &&
\r
1730 *mode != '+' && *mode != '-' && *mode != '=') {
\r
1732 case 'u': subset |= 04700; break; /* setuid, user perms */
\r
1733 case 'g': subset |= 02070; break; /* setgid, group perms */
\r
1734 case 'o': subset |= 00007; break; /* just other perms */
\r
1735 case 'a': subset |= 06777; break; /* all of the above */
\r
1737 printf("chmod: file mode '%.*s' contains unrecognised"
\r
1738 " user/group/other specifier '%c'\n",
\r
1739 (int)strcspn(modebegin, ","), modebegin, *mode);
\r
1744 if (!*mode || *mode == ',') {
\r
1745 printf("chmod: file mode '%.*s' is incomplete\n",
\r
1746 (int)strcspn(modebegin, ","), modebegin);
\r
1750 if (!*mode || *mode == ',') {
\r
1751 printf("chmod: file mode '%.*s' is incomplete\n",
\r
1752 (int)strcspn(modebegin, ","), modebegin);
\r
1756 while (*mode && *mode != ',') {
\r
1758 case 'r': perms |= 00444; break;
\r
1759 case 'w': perms |= 00222; break;
\r
1760 case 'x': perms |= 00111; break;
\r
1761 case 't': perms |= 01000; subset |= 01000; break;
\r
1763 if ((subset & 06777) != 04700 &&
\r
1764 (subset & 06777) != 02070) {
\r
1765 printf("chmod: file mode '%.*s': set[ug]id bit should"
\r
1766 " be used with exactly one of u or g only\n",
\r
1767 (int)strcspn(modebegin, ","), modebegin);
\r
1773 printf("chmod: file mode '%.*s' contains unrecognised"
\r
1774 " permission specifier '%c'\n",
\r
1775 (int)strcspn(modebegin, ","), modebegin, *mode);
\r
1780 if (!(subset & 06777) && (perms &~ subset)) {
\r
1781 printf("chmod: file mode '%.*s' contains no user/group/other"
\r
1782 " specifier and permissions other than 't' \n",
\r
1783 (int)strcspn(modebegin, ","), modebegin);
\r
1789 ctx->attrs_clr |= perms;
\r
1790 ctx->attrs_xor |= perms;
\r
1793 ctx->attrs_clr |= perms;
\r
1794 ctx->attrs_xor &= ~perms;
\r
1797 ctx->attrs_clr |= subset;
\r
1798 ctx->attrs_xor |= perms;
\r
1801 if (*mode) mode++; /* eat comma */
\r
1806 for (i = 2; i < cmd->nwords; i++)
\r
1807 ret &= wildcard_iterate(cmd->words[i], sftp_action_chmod, ctx);
\r
1812 static int sftp_cmd_open(struct sftp_command *cmd)
\r
1816 if (back != NULL) {
\r
1817 printf("psftp: already connected\n");
\r
1821 if (cmd->nwords < 2) {
\r
1822 printf("open: expects a host name\n");
\r
1826 if (cmd->nwords > 2) {
\r
1827 portnumber = atoi(cmd->words[2]);
\r
1828 if (portnumber == 0) {
\r
1829 printf("open: invalid port number\n");
\r
1835 if (psftp_connect(cmd->words[1], NULL, portnumber)) {
\r
1836 back = NULL; /* connection is already closed */
\r
1837 return -1; /* this is fatal */
\r
1843 static int sftp_cmd_lcd(struct sftp_command *cmd)
\r
1845 char *currdir, *errmsg;
\r
1847 if (cmd->nwords < 2) {
\r
1848 printf("lcd: expects a local directory name\n");
\r
1852 errmsg = psftp_lcd(cmd->words[1]);
\r
1854 printf("lcd: unable to change directory: %s\n", errmsg);
\r
1859 currdir = psftp_getcwd();
\r
1860 printf("New local directory is %s\n", currdir);
\r
1866 static int sftp_cmd_lpwd(struct sftp_command *cmd)
\r
1870 currdir = psftp_getcwd();
\r
1871 printf("Current local directory is %s\n", currdir);
\r
1877 static int sftp_cmd_pling(struct sftp_command *cmd)
\r
1881 exitcode = system(cmd->words[1]);
\r
1882 return (exitcode == 0);
\r
1885 static int sftp_cmd_help(struct sftp_command *cmd);
\r
1887 static struct sftp_cmd_lookup {
\r
1890 * For help purposes, there are two kinds of command:
\r
1892 * - primary commands, in which `longhelp' is non-NULL. In
\r
1893 * this case `shorthelp' is descriptive text, and `longhelp'
\r
1894 * is longer descriptive text intended to be printed after
\r
1895 * the command name.
\r
1897 * - alias commands, in which `longhelp' is NULL. In this case
\r
1898 * `shorthelp' is the name of a primary command, which
\r
1899 * contains the help that should double up for this command.
\r
1901 int listed; /* do we list this in primary help? */
\r
1904 int (*obey) (struct sftp_command *);
\r
1905 } sftp_lookup[] = {
\r
1907 * List of sftp commands. This is binary-searched so it MUST be
\r
1911 "!", TRUE, "run a local command",
\r
1913 /* FIXME: this example is crap for non-Windows. */
\r
1914 " Runs a local command. For example, \"!del myfile\".\n",
\r
1918 "bye", TRUE, "finish your SFTP session",
\r
1920 " Terminates your SFTP session and quits the PSFTP program.\n",
\r
1924 "cd", TRUE, "change your remote working directory",
\r
1925 " [ <new working directory> ]\n"
\r
1926 " Change the remote working directory for your SFTP session.\n"
\r
1927 " If a new working directory is not supplied, you will be\n"
\r
1928 " returned to your home directory.\n",
\r
1932 "chmod", TRUE, "change file permissions and modes",
\r
1933 " <modes> <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
\r
1934 " Change the file permissions on one or more remote files or\n"
\r
1936 " <modes> can be any octal Unix permission specifier.\n"
\r
1937 " Alternatively, <modes> can include the following modifiers:\n"
\r
1938 " u+r make file readable by owning user\n"
\r
1939 " u+w make file writable by owning user\n"
\r
1940 " u+x make file executable by owning user\n"
\r
1941 " u-r make file not readable by owning user\n"
\r
1942 " [also u-w, u-x]\n"
\r
1943 " g+r make file readable by members of owning group\n"
\r
1944 " [also g+w, g+x, g-r, g-w, g-x]\n"
\r
1945 " o+r make file readable by all other users\n"
\r
1946 " [also o+w, o+x, o-r, o-w, o-x]\n"
\r
1947 " a+r make file readable by absolutely everybody\n"
\r
1948 " [also a+w, a+x, a-r, a-w, a-x]\n"
\r
1949 " u+s enable the Unix set-user-ID bit\n"
\r
1950 " u-s disable the Unix set-user-ID bit\n"
\r
1951 " g+s enable the Unix set-group-ID bit\n"
\r
1952 " g-s disable the Unix set-group-ID bit\n"
\r
1953 " +t enable the Unix \"sticky bit\"\n"
\r
1954 " You can give more than one modifier for the same user (\"g-rwx\"), and\n"
\r
1955 " more than one user for the same modifier (\"ug+w\"). You can\n"
\r
1956 " use commas to separate different modifiers (\"u+rwx,g+s\").\n",
\r
1960 "close", TRUE, "finish your SFTP session but do not quit PSFTP",
\r
1962 " Terminates your SFTP session, but does not quit the PSFTP\n"
\r
1963 " program. You can then use \"open\" to start another SFTP\n"
\r
1964 " session, to the same server or to a different one.\n",
\r
1968 "del", TRUE, "delete files on the remote server",
\r
1969 " <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
\r
1970 " Delete a file or files from the server.\n",
\r
1974 "delete", FALSE, "del", NULL, sftp_cmd_rm
\r
1977 "dir", TRUE, "list remote files",
\r
1978 " [ <directory-name> ]/[ <wildcard> ]\n"
\r
1979 " List the contents of a specified directory on the server.\n"
\r
1980 " If <directory-name> is not given, the current working directory\n"
\r
1982 " If <wildcard> is given, it is treated as a set of files to\n"
\r
1983 " list; otherwise, all files are listed.\n",
\r
1987 "exit", TRUE, "bye", NULL, sftp_cmd_quit
\r
1990 "get", TRUE, "download a file from the server to your local machine",
\r
1991 " [ -r ] [ -- ] <filename> [ <local-filename> ]\n"
\r
1992 " Downloads a file on the server and stores it locally under\n"
\r
1993 " the same name, or under a different one if you supply the\n"
\r
1994 " argument <local-filename>.\n"
\r
1995 " If -r specified, recursively fetch a directory.\n",
\r
1999 "help", TRUE, "give help",
\r
2000 " [ <command> [ <command> ... ] ]\n"
\r
2001 " Give general help if no commands are specified.\n"
\r
2002 " If one or more commands are specified, give specific help on\n"
\r
2003 " those particular commands.\n",
\r
2007 "lcd", TRUE, "change local working directory",
\r
2008 " <local-directory-name>\n"
\r
2009 " Change the local working directory of the PSFTP program (the\n"
\r
2010 " default location where the \"get\" command will save files).\n",
\r
2014 "lpwd", TRUE, "print local working directory",
\r
2016 " Print the local working directory of the PSFTP program (the\n"
\r
2017 " default location where the \"get\" command will save files).\n",
\r
2021 "ls", TRUE, "dir", NULL,
\r
2025 "mget", TRUE, "download multiple files at once",
\r
2026 " [ -r ] [ -- ] <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
\r
2027 " Downloads many files from the server, storing each one under\n"
\r
2028 " the same name it has on the server side. You can use wildcards\n"
\r
2029 " such as \"*.c\" to specify lots of files at once.\n"
\r
2030 " If -r specified, recursively fetch files and directories.\n",
\r
2034 "mkdir", TRUE, "create directories on the remote server",
\r
2035 " <directory-name> [ <directory-name>... ]\n"
\r
2036 " Creates directories with the given names on the server.\n",
\r
2040 "mput", TRUE, "upload multiple files at once",
\r
2041 " [ -r ] [ -- ] <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
\r
2042 " Uploads many files to the server, storing each one under the\n"
\r
2043 " same name it has on the client side. You can use wildcards\n"
\r
2044 " such as \"*.c\" to specify lots of files at once.\n"
\r
2045 " If -r specified, recursively store files and directories.\n",
\r
2049 "mv", TRUE, "move or rename file(s) on the remote server",
\r
2050 " <source> [ <source>... ] <destination>\n"
\r
2051 " Moves or renames <source>(s) on the server to <destination>,\n"
\r
2052 " also on the server.\n"
\r
2053 " If <destination> specifies an existing directory, then <source>\n"
\r
2054 " may be a wildcard, and multiple <source>s may be given; all\n"
\r
2055 " source files are moved into <destination>.\n"
\r
2056 " Otherwise, <source> must specify a single file, which is moved\n"
\r
2057 " or renamed so that it is accessible under the name <destination>.\n",
\r
2061 "open", TRUE, "connect to a host",
\r
2062 " [<user>@]<hostname> [<port>]\n"
\r
2063 " Establishes an SFTP connection to a given host. Only usable\n"
\r
2064 " when you are not already connected to a server.\n",
\r
2068 "put", TRUE, "upload a file from your local machine to the server",
\r
2069 " [ -r ] [ -- ] <filename> [ <remote-filename> ]\n"
\r
2070 " Uploads a file to the server and stores it there under\n"
\r
2071 " the same name, or under a different one if you supply the\n"
\r
2072 " argument <remote-filename>.\n"
\r
2073 " If -r specified, recursively store a directory.\n",
\r
2077 "pwd", TRUE, "print your remote working directory",
\r
2079 " Print the current remote working directory for your SFTP session.\n",
\r
2083 "quit", TRUE, "bye", NULL,
\r
2087 "reget", TRUE, "continue downloading files",
\r
2088 " [ -r ] [ -- ] <filename> [ <local-filename> ]\n"
\r
2089 " Works exactly like the \"get\" command, but the local file\n"
\r
2090 " must already exist. The download will begin at the end of the\n"
\r
2091 " file. This is for resuming a download that was interrupted.\n"
\r
2092 " If -r specified, resume interrupted \"get -r\".\n",
\r
2096 "ren", TRUE, "mv", NULL,
\r
2100 "rename", FALSE, "mv", NULL,
\r
2104 "reput", TRUE, "continue uploading files",
\r
2105 " [ -r ] [ -- ] <filename> [ <remote-filename> ]\n"
\r
2106 " Works exactly like the \"put\" command, but the remote file\n"
\r
2107 " must already exist. The upload will begin at the end of the\n"
\r
2108 " file. This is for resuming an upload that was interrupted.\n"
\r
2109 " If -r specified, resume interrupted \"put -r\".\n",
\r
2113 "rm", TRUE, "del", NULL,
\r
2117 "rmdir", TRUE, "remove directories on the remote server",
\r
2118 " <directory-name> [ <directory-name>... ]\n"
\r
2119 " Removes the directory with the given name on the server.\n"
\r
2120 " The directory will not be removed unless it is empty.\n"
\r
2121 " Wildcards may be used to specify multiple directories.\n",
\r
2126 const struct sftp_cmd_lookup *lookup_command(char *name)
\r
2131 j = sizeof(sftp_lookup) / sizeof(*sftp_lookup);
\r
2132 while (j - i > 1) {
\r
2134 cmp = strcmp(name, sftp_lookup[k].name);
\r
2140 return &sftp_lookup[k];
\r
2146 static int sftp_cmd_help(struct sftp_command *cmd)
\r
2149 if (cmd->nwords == 1) {
\r
2151 * Give short help on each command.
\r
2155 for (i = 0; i < sizeof(sftp_lookup) / sizeof(*sftp_lookup); i++) {
\r
2157 if (!sftp_lookup[i].listed)
\r
2159 len = strlen(sftp_lookup[i].name);
\r
2163 for (i = 0; i < sizeof(sftp_lookup) / sizeof(*sftp_lookup); i++) {
\r
2164 const struct sftp_cmd_lookup *lookup;
\r
2165 if (!sftp_lookup[i].listed)
\r
2167 lookup = &sftp_lookup[i];
\r
2168 printf("%-*s", maxlen+2, lookup->name);
\r
2169 if (lookup->longhelp == NULL)
\r
2170 lookup = lookup_command(lookup->shorthelp);
\r
2171 printf("%s\n", lookup->shorthelp);
\r
2175 * Give long help on specific commands.
\r
2177 for (i = 1; i < cmd->nwords; i++) {
\r
2178 const struct sftp_cmd_lookup *lookup;
\r
2179 lookup = lookup_command(cmd->words[i]);
\r
2181 printf("help: %s: command not found\n", cmd->words[i]);
\r
2183 printf("%s", lookup->name);
\r
2184 if (lookup->longhelp == NULL)
\r
2185 lookup = lookup_command(lookup->shorthelp);
\r
2186 printf("%s", lookup->longhelp);
\r
2193 /* ----------------------------------------------------------------------
\r
2194 * Command line reading and parsing.
\r
2196 struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
\r
2199 struct sftp_command *cmd;
\r
2203 cmd = snew(struct sftp_command);
\r
2204 cmd->words = NULL;
\r
2206 cmd->wordssize = 0;
\r
2211 if (modeflags & 1)
\r
2212 printf("psftp> ");
\r
2213 line = fgetline(fp);
\r
2215 line = ssh_sftp_get_cmdline("psftp> ", back == NULL);
\r
2218 if (!line || !*line) {
\r
2219 cmd->obey = sftp_cmd_quit;
\r
2220 if ((mode == 0) || (modeflags & 1))
\r
2222 return cmd; /* eof */
\r
2225 line[strcspn(line, "\r\n")] = '\0';
\r
2227 if (modeflags & 1) {
\r
2228 printf("%s\n", line);
\r
2232 while (*p && (*p == ' ' || *p == '\t'))
\r
2237 * Special case: the ! command. This is always parsed as
\r
2238 * exactly two words: one containing the !, and the second
\r
2239 * containing everything else on the line.
\r
2241 cmd->nwords = cmd->wordssize = 2;
\r
2242 cmd->words = sresize(cmd->words, cmd->wordssize, char *);
\r
2243 cmd->words[0] = dupstr("!");
\r
2244 cmd->words[1] = dupstr(p+1);
\r
2245 } else if (*p == '#') {
\r
2247 * Special case: comment. Entire line is ignored.
\r
2249 cmd->nwords = cmd->wordssize = 0;
\r
2253 * Parse the command line into words. The syntax is:
\r
2254 * - double quotes are removed, but cause spaces within to be
\r
2255 * treated as non-separating.
\r
2256 * - a double-doublequote pair is a literal double quote, inside
\r
2257 * _or_ outside quotes. Like this:
\r
2259 * firstword "second word" "this has ""quotes"" in" and""this""
\r
2265 * >this has "quotes" in<
\r
2269 /* skip whitespace */
\r
2270 while (*p && (*p == ' ' || *p == '\t'))
\r
2272 /* mark start of word */
\r
2273 q = r = p; /* q sits at start, r writes word */
\r
2276 if (!quoting && (*p == ' ' || *p == '\t'))
\r
2277 break; /* reached end of word */
\r
2278 else if (*p == '"' && p[1] == '"')
\r
2279 p += 2, *r++ = '"'; /* a literal quote */
\r
2280 else if (*p == '"')
\r
2281 p++, quoting = !quoting;
\r
2286 p++; /* skip over the whitespace */
\r
2288 if (cmd->nwords >= cmd->wordssize) {
\r
2289 cmd->wordssize = cmd->nwords + 16;
\r
2290 cmd->words = sresize(cmd->words, cmd->wordssize, char *);
\r
2292 cmd->words[cmd->nwords++] = dupstr(q);
\r
2299 * Now parse the first word and assign a function.
\r
2302 if (cmd->nwords == 0)
\r
2303 cmd->obey = sftp_cmd_null;
\r
2305 const struct sftp_cmd_lookup *lookup;
\r
2306 lookup = lookup_command(cmd->words[0]);
\r
2308 cmd->obey = sftp_cmd_unknown;
\r
2310 cmd->obey = lookup->obey;
\r
2316 static int do_sftp_init(void)
\r
2318 struct sftp_packet *pktin;
\r
2319 struct sftp_request *req, *rreq;
\r
2322 * Do protocol initialisation.
\r
2324 if (!fxp_init()) {
\r
2326 "Fatal: unable to initialise SFTP: %s\n", fxp_error());
\r
2327 return 1; /* failure */
\r
2331 * Find out where our home directory is.
\r
2333 sftp_register(req = fxp_realpath_send("."));
\r
2334 rreq = sftp_find_request(pktin = sftp_recv());
\r
2335 assert(rreq == req);
\r
2336 homedir = fxp_realpath_recv(pktin, rreq);
\r
2340 "Warning: failed to resolve home directory: %s\n",
\r
2342 homedir = dupstr(".");
\r
2344 printf("Remote working directory is %s\n", homedir);
\r
2346 pwd = dupstr(homedir);
\r
2350 void do_sftp_cleanup()
\r
2354 back->special(backhandle, TS_EOF);
\r
2355 sftp_recvdata(&ch, 1);
\r
2356 back->free(backhandle);
\r
2357 sftp_cleanup_request();
\r
2359 backhandle = NULL;
\r
2371 void do_sftp(int mode, int modeflags, char *batchfile)
\r
2381 /* ------------------------------------------------------------------
\r
2382 * Now we're ready to do Real Stuff.
\r
2385 struct sftp_command *cmd;
\r
2386 cmd = sftp_getcmd(NULL, 0, 0);
\r
2389 ret = cmd->obey(cmd);
\r
2392 for(i = 0; i < cmd->nwords; i++)
\r
2393 sfree(cmd->words[i]);
\r
2394 sfree(cmd->words);
\r
2401 fp = fopen(batchfile, "r");
\r
2403 printf("Fatal: unable to open %s\n", batchfile);
\r
2407 struct sftp_command *cmd;
\r
2408 cmd = sftp_getcmd(fp, mode, modeflags);
\r
2411 ret = cmd->obey(cmd);
\r
2415 if (!(modeflags & 2))
\r
2424 /* ----------------------------------------------------------------------
\r
2425 * Dirty bits: integration with PuTTY.
\r
2428 static int verbose = 0;
\r
2431 * Print an error message and perform a fatal exit.
\r
2433 void fatalbox(char *fmt, ...)
\r
2437 va_start(ap, fmt);
\r
2438 str = dupvprintf(fmt, ap);
\r
2439 str2 = dupcat("Fatal: ", str, "\n", NULL);
\r
2442 fputs(str2, stderr);
\r
2447 void modalfatalbox(char *fmt, ...)
\r
2451 va_start(ap, fmt);
\r
2452 str = dupvprintf(fmt, ap);
\r
2453 str2 = dupcat("Fatal: ", str, "\n", NULL);
\r
2456 fputs(str2, stderr);
\r
2461 void connection_fatal(void *frontend, char *fmt, ...)
\r
2465 va_start(ap, fmt);
\r
2466 str = dupvprintf(fmt, ap);
\r
2467 str2 = dupcat("Fatal: ", str, "\n", NULL);
\r
2470 fputs(str2, stderr);
\r
2476 void ldisc_send(void *handle, char *buf, int len, int interactive)
\r
2479 * This is only here because of the calls to ldisc_send(NULL,
\r
2480 * 0) in ssh.c. Nothing in PSFTP actually needs to use the
\r
2481 * ldisc as an ldisc. So if we get called with any real data, I
\r
2482 * want to know about it.
\r
2488 * In psftp, all agent requests should be synchronous, so this is a
\r
2489 * never-called stub.
\r
2491 void agent_schedule_callback(void (*callback)(void *, void *, int),
\r
2492 void *callback_ctx, void *data, int len)
\r
2494 assert(!"We shouldn't be here");
\r
2498 * Receive a block of data from the SSH link. Block until all data
\r
2501 * To do this, we repeatedly call the SSH protocol module, with our
\r
2502 * own trap in from_backend() to catch the data that comes back. We
\r
2503 * do this until we have enough data.
\r
2506 static unsigned char *outptr; /* where to put the data */
\r
2507 static unsigned outlen; /* how much data required */
\r
2508 static unsigned char *pending = NULL; /* any spare data */
\r
2509 static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
\r
2510 int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
\r
2512 unsigned char *p = (unsigned char *) data;
\r
2513 unsigned len = (unsigned) datalen;
\r
2516 * stderr data is just spouted to local stderr and otherwise
\r
2521 if (fwrite(data, 1, len, stderr) < len)
\r
2527 * If this is before the real session begins, just return.
\r
2532 if ((outlen > 0) && (len > 0)) {
\r
2533 unsigned used = outlen;
\r
2536 memcpy(outptr, p, used);
\r
2544 if (pendsize < pendlen + len) {
\r
2545 pendsize = pendlen + len + 4096;
\r
2546 pending = sresize(pending, pendsize, unsigned char);
\r
2548 memcpy(pending + pendlen, p, len);
\r
2554 int from_backend_untrusted(void *frontend_handle, const char *data, int len)
\r
2557 * No "untrusted" output should get here (the way the code is
\r
2558 * currently, it's all diverted by FLAG_STDERR).
\r
2560 assert(!"Unexpected call to from_backend_untrusted()");
\r
2561 return 0; /* not reached */
\r
2563 int sftp_recvdata(char *buf, int len)
\r
2565 outptr = (unsigned char *) buf;
\r
2569 * See if the pending-input block contains some of what we
\r
2572 if (pendlen > 0) {
\r
2573 unsigned pendused = pendlen;
\r
2574 if (pendused > outlen)
\r
2575 pendused = outlen;
\r
2576 memcpy(outptr, pending, pendused);
\r
2577 memmove(pending, pending + pendused, pendlen - pendused);
\r
2578 outptr += pendused;
\r
2579 outlen -= pendused;
\r
2580 pendlen -= pendused;
\r
2581 if (pendlen == 0) {
\r
2590 while (outlen > 0) {
\r
2591 if (back->exitcode(backhandle) >= 0 || ssh_sftp_loop_iteration() < 0)
\r
2592 return 0; /* doom */
\r
2597 int sftp_senddata(char *buf, int len)
\r
2599 back->send(backhandle, buf, len);
\r
2604 * Short description of parameters.
\r
2606 static void usage(void)
\r
2608 printf("PuTTY Secure File Transfer (SFTP) client\n");
\r
2609 printf("%s\n", ver);
\r
2610 printf("Usage: psftp [options] [user@]host\n");
\r
2611 printf("Options:\n");
\r
2612 printf(" -V print version information and exit\n");
\r
2613 printf(" -pgpfp print PGP key fingerprints and exit\n");
\r
2614 printf(" -b file use specified batchfile\n");
\r
2615 printf(" -bc output batchfile commands\n");
\r
2616 printf(" -be don't stop batchfile processing if errors\n");
\r
2617 printf(" -v show verbose messages\n");
\r
2618 printf(" -load sessname Load settings from saved session\n");
\r
2619 printf(" -l user connect with specified username\n");
\r
2620 printf(" -P port connect to specified port\n");
\r
2621 printf(" -pw passw login with specified password\n");
\r
2622 printf(" -1 -2 force use of particular SSH protocol version\n");
\r
2623 printf(" -4 -6 force use of IPv4 or IPv6\n");
\r
2624 printf(" -C enable compression\n");
\r
2625 printf(" -i key private key file for authentication\n");
\r
2626 printf(" -noagent disable use of Pageant\n");
\r
2627 printf(" -agent enable use of Pageant\n");
\r
2628 printf(" -batch disable all interactive prompts\n");
\r
2632 static void version(void)
\r
2634 printf("psftp: %s\n", ver);
\r
2639 * Connect to a host.
\r
2641 static int psftp_connect(char *userhost, char *user, int portnumber)
\r
2643 char *host, *realhost;
\r
2647 /* Separate host and username */
\r
2649 host = strrchr(host, '@');
\r
2650 if (host == NULL) {
\r
2655 printf("psftp: multiple usernames specified; using \"%s\"\n",
\r
2662 * If we haven't loaded session details already (e.g., from -load),
\r
2663 * try looking for a session called "host".
\r
2665 if (!loaded_session) {
\r
2666 /* Try to load settings for `host' into a temporary config */
\r
2668 cfg2.host[0] = '\0';
\r
2669 do_defaults(host, &cfg2);
\r
2670 if (cfg2.host[0] != '\0') {
\r
2671 /* Settings present and include hostname */
\r
2672 /* Re-load data into the real config. */
\r
2673 do_defaults(host, &cfg);
\r
2675 /* Session doesn't exist or mention a hostname. */
\r
2676 /* Use `host' as a bare hostname. */
\r
2677 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
\r
2678 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
2681 /* Patch in hostname `host' to session details. */
\r
2682 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
\r
2683 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
2687 * Force use of SSH. (If they got the protocol wrong we assume the
\r
2688 * port is useless too.)
\r
2690 if (cfg.protocol != PROT_SSH) {
\r
2691 cfg.protocol = PROT_SSH;
\r
2696 * If saved session / Default Settings says SSH-1 (`1 only' or `1'),
\r
2697 * then change it to SSH-2, on the grounds that that's more likely to
\r
2698 * work for SFTP. (Can be overridden with `-1' option.)
\r
2699 * But if it says `2 only' or `2', respect which.
\r
2701 if (cfg.sshprot != 2 && cfg.sshprot != 3)
\r
2705 * Enact command-line overrides.
\r
2707 cmdline_run_saved(&cfg);
\r
2710 * Trim leading whitespace off the hostname if it's there.
\r
2713 int space = strspn(cfg.host, " \t");
\r
2714 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
\r
2717 /* See if host is of the form user@host */
\r
2718 if (cfg.host[0] != '\0') {
\r
2719 char *atsign = strrchr(cfg.host, '@');
\r
2720 /* Make sure we're not overflowing the user field */
\r
2722 if (atsign - cfg.host < sizeof cfg.username) {
\r
2723 strncpy(cfg.username, cfg.host, atsign - cfg.host);
\r
2724 cfg.username[atsign - cfg.host] = '\0';
\r
2726 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
\r
2731 * Trim a colon suffix off the hostname if it's there.
\r
2733 cfg.host[strcspn(cfg.host, ":")] = '\0';
\r
2736 * Remove any remaining whitespace from the hostname.
\r
2739 int p1 = 0, p2 = 0;
\r
2740 while (cfg.host[p2] != '\0') {
\r
2741 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
\r
2742 cfg.host[p1] = cfg.host[p2];
\r
2747 cfg.host[p1] = '\0';
\r
2750 /* Set username */
\r
2751 if (user != NULL && user[0] != '\0') {
\r
2752 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
\r
2753 cfg.username[sizeof(cfg.username) - 1] = '\0';
\r
2757 cfg.port = portnumber;
\r
2760 * Disable scary things which shouldn't be enabled for simple
\r
2761 * things like SCP and SFTP: agent forwarding, port forwarding,
\r
2764 cfg.x11_forward = 0;
\r
2766 cfg.portfwd[0] = cfg.portfwd[1] = '\0';
\r
2767 cfg.ssh_simple = TRUE;
\r
2769 /* Set up subsystem name. */
\r
2770 strcpy(cfg.remote_cmd, "sftp");
\r
2771 cfg.ssh_subsys = TRUE;
\r
2775 * Set up fallback option, for SSH-1 servers or servers with the
\r
2776 * sftp subsystem not enabled but the server binary installed
\r
2777 * in the usual place. We only support fallback on Unix
\r
2778 * systems, and we use a kludgy piece of shellery which should
\r
2779 * try to find sftp-server in various places (the obvious
\r
2780 * systemwide spots /usr/lib and /usr/local/lib, and then the
\r
2781 * user's PATH) and finally give up.
\r
2783 * test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server
\r
2784 * test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server
\r
2785 * exec sftp-server
\r
2787 * the idea being that this will attempt to use either of the
\r
2788 * obvious pathnames and then give up, and when it does give up
\r
2789 * it will print the preferred pathname in the error messages.
\r
2791 cfg.remote_cmd_ptr2 =
\r
2792 "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
\r
2793 "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
\r
2794 "exec sftp-server";
\r
2795 cfg.ssh_subsys2 = FALSE;
\r
2797 back = &ssh_backend;
\r
2799 err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,
\r
2800 0, cfg.tcp_keepalives);
\r
2801 if (err != NULL) {
\r
2802 fprintf(stderr, "ssh_init: %s\n", err);
\r
2805 logctx = log_init(NULL, &cfg);
\r
2806 back->provide_logctx(backhandle, logctx);
\r
2807 console_provide_logctx(logctx);
\r
2808 while (!back->sendok(backhandle)) {
\r
2809 if (back->exitcode(backhandle) >= 0)
\r
2811 if (ssh_sftp_loop_iteration() < 0) {
\r
2812 fprintf(stderr, "ssh_init: error during SSH connection setup\n");
\r
2816 if (verbose && realhost != NULL)
\r
2817 printf("Connected to %s\n", realhost);
\r
2818 if (realhost != NULL)
\r
2823 void cmdline_error(char *p, ...)
\r
2826 fprintf(stderr, "psftp: ");
\r
2828 vfprintf(stderr, p, ap);
\r
2830 fprintf(stderr, "\n try typing \"psftp -h\" for help\n");
\r
2835 * Main program. Parse arguments etc.
\r
2837 int psftp_main(int argc, char *argv[])
\r
2840 int portnumber = 0;
\r
2841 char *userhost, *user;
\r
2843 int modeflags = 0;
\r
2844 char *batchfile = NULL;
\r
2846 flags = FLAG_STDERR | FLAG_INTERACTIVE
\r
2847 #ifdef FLAG_SYNCAGENT
\r
2851 cmdline_tooltype = TOOLTYPE_FILETRANSFER;
\r
2854 userhost = user = NULL;
\r
2856 /* Load Default Settings before doing anything else. */
\r
2857 do_defaults(NULL, &cfg);
\r
2858 loaded_session = FALSE;
\r
2860 for (i = 1; i < argc; i++) {
\r
2862 if (argv[i][0] != '-') {
\r
2866 userhost = dupstr(argv[i]);
\r
2869 ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, &cfg);
\r
2871 cmdline_error("option \"%s\" requires an argument", argv[i]);
\r
2872 } else if (ret == 2) {
\r
2873 i++; /* skip next argument */
\r
2874 } else if (ret == 1) {
\r
2875 /* We have our own verbosity in addition to `flags'. */
\r
2876 if (flags & FLAG_VERBOSE)
\r
2878 } else if (strcmp(argv[i], "-h") == 0 ||
\r
2879 strcmp(argv[i], "-?") == 0) {
\r
2881 } else if (strcmp(argv[i], "-pgpfp") == 0) {
\r
2882 pgp_fingerprints();
\r
2884 } else if (strcmp(argv[i], "-V") == 0) {
\r
2886 } else if (strcmp(argv[i], "-batch") == 0) {
\r
2887 console_batch_mode = 1;
\r
2888 } else if (strcmp(argv[i], "-b") == 0 && i + 1 < argc) {
\r
2890 batchfile = argv[++i];
\r
2891 } else if (strcmp(argv[i], "-bc") == 0) {
\r
2892 modeflags = modeflags | 1;
\r
2893 } else if (strcmp(argv[i], "-be") == 0) {
\r
2894 modeflags = modeflags | 2;
\r
2895 } else if (strcmp(argv[i], "--") == 0) {
\r
2899 cmdline_error("unknown option \"%s\"", argv[i]);
\r
2907 * If the loaded session provides a hostname, and a hostname has not
\r
2908 * otherwise been specified, pop it in `userhost' so that
\r
2909 * `psftp -load sessname' is sufficient to start a session.
\r
2911 if (!userhost && cfg.host[0] != '\0') {
\r
2912 userhost = dupstr(cfg.host);
\r
2916 * If a user@host string has already been provided, connect to
\r
2921 ret = psftp_connect(userhost, user, portnumber);
\r
2925 if (do_sftp_init())
\r
2928 printf("psftp: no hostname specified; use \"open host.name\""
\r
2932 do_sftp(mode, modeflags, batchfile);
\r
2934 if (back != NULL && back->connected(backhandle)) {
\r
2936 back->special(backhandle, TS_EOF);
\r
2937 sftp_recvdata(&ch, 1);
\r
2939 do_sftp_cleanup();
\r
2940 random_save_seed();
\r
2941 cmdline_cleanup();
\r
2942 console_provide_logctx(NULL);
\r