assert(rreq == req);
canonname = fxp_realpath_recv(pktin, rreq);
- if (!canonname)
- return fullname; /* even that failed; give up */
+ if (!canonname) {
+ /* Even that failed. Restore our best guess at the
+ * constructed filename and give up */
+ fullname[i] = '/'; /* restore slash and last component */
+ return fullname;
+ }
/*
* We have a canonical name for all but the last path
return strcmp(*a, *b);
}
+static void not_connected(void)
+{
+ printf("psftp: not connected to a host; use \"open host.name\"\n");
+}
+
/* ----------------------------------------------------------------------
* The meat of the `get' and `put' commands.
*/
struct sftp_request *req, *rreq;
struct fxp_xfer *xfer;
uint64 offset;
- FILE *fp;
+ WFile *file;
int ret, shown_err = FALSE;
/*
* If none of them exists, of course, we start at 0.
*/
i = 0;
- while (i < nnames) {
- char *nextoutfname;
- int ret;
- if (outfname)
- nextoutfname = dir_file_cat(outfname,
- ournames[i]->filename);
- else
- nextoutfname = dupstr(ournames[i]->filename);
- ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT);
- sfree(nextoutfname);
- if (ret)
- break;
- i++;
- }
- if (i > 0)
- i--;
+ if (restart) {
+ while (i < nnames) {
+ char *nextoutfname;
+ int ret;
+ if (outfname)
+ nextoutfname = dir_file_cat(outfname,
+ ournames[i]->filename);
+ else
+ nextoutfname = dupstr(ournames[i]->filename);
+ ret = (file_type(nextoutfname) == FILE_TYPE_NONEXISTENT);
+ sfree(nextoutfname);
+ if (ret)
+ break;
+ i++;
+ }
+ if (i > 0)
+ i--;
+ }
/*
* Now we're ready to recurse. Starting at ournames[i]
fh = fxp_open_recv(pktin, rreq);
if (!fh) {
- printf("%s: %s\n", fname, fxp_error());
+ printf("%s: open for read: %s\n", fname, fxp_error());
return 0;
}
if (restart) {
- fp = fopen(outfname, "rb+");
+ file = open_existing_wfile(outfname, NULL);
} else {
- fp = fopen(outfname, "wb");
+ file = open_new_file(outfname);
}
- if (!fp) {
+ if (!file) {
printf("local: unable to open %s\n", outfname);
sftp_register(req = fxp_close_send(fh));
}
if (restart) {
- long posn;
- fseek(fp, 0L, SEEK_END);
- posn = ftell(fp);
- printf("reget: restarting at file position %ld\n", posn);
- offset = uint64_make(0, posn);
+ char decbuf[30];
+ if (seek_file(file, uint64_make(0,0) , FROM_END) == -1) {
+ printf("reget: cannot restart %s - file too large\n",
+ outfname);
+ sftp_register(req = fxp_close_send(fh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin, rreq);
+
+ return 0;
+ }
+
+ offset = get_file_posn(file);
+ uint64_decimal(offset, decbuf);
+ printf("reget: restarting at file position %s\n", decbuf);
} else {
offset = uint64_make(0, 0);
}
wpos = 0;
while (wpos < len) {
- wlen = fwrite(buf + wpos, 1, len - wpos, fp);
+ wlen = write_to_file(file, buf + wpos, len - wpos);
if (wlen <= 0) {
printf("error while writing local file\n");
ret = 0;
xfer_set_error(xfer);
+ break;
}
wpos += wlen;
}
xfer_cleanup(xfer);
- fclose(fp);
+ close_wfile(file);
sftp_register(req = fxp_close_send(fh));
rreq = sftp_find_request(pktin = sftp_recv());
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
uint64 offset;
- FILE *fp;
+ RFile *file;
int ret, err, eof;
/*
* If none of them exists, of course, we start at 0.
*/
i = 0;
- while (i < nnames) {
- char *nextoutfname;
- nextoutfname = dupcat(outfname, "/", ournames[i], NULL);
- sftp_register(req = fxp_stat_send(nextoutfname));
- rreq = sftp_find_request(pktin = sftp_recv());
- assert(rreq == req);
- result = fxp_stat_recv(pktin, rreq, &attrs);
- sfree(nextoutfname);
- if (!result)
- break;
- i++;
- }
- if (i > 0)
- i--;
+ if (restart) {
+ while (i < nnames) {
+ char *nextoutfname;
+ nextoutfname = dupcat(outfname, "/", ournames[i], NULL);
+ sftp_register(req = fxp_stat_send(nextoutfname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_stat_recv(pktin, rreq, &attrs);
+ sfree(nextoutfname);
+ if (!result)
+ break;
+ i++;
+ }
+ if (i > 0)
+ i--;
+ }
- /*
- * Now we're ready to recurse. Starting at ournames[i]
+ /*
+ * Now we're ready to recurse. Starting at ournames[i]
* and continuing on to the end of the list, we
* construct a new source and target file name, and
* call sftp_put_file again.
return 1;
}
- fp = fopen(fname, "rb");
- if (!fp) {
+ file = open_existing_file(fname, NULL, NULL, NULL);
+ if (!file) {
printf("local: unable to open %s\n", fname);
return 0;
}
fh = fxp_open_recv(pktin, rreq);
if (!fh) {
- printf("%s: %s\n", outfname, fxp_error());
+ printf("%s: open for write: %s\n", outfname, fxp_error());
return 0;
}
offset = attrs.size;
uint64_decimal(offset, decbuf);
printf("reput: restarting at file position %s\n", decbuf);
- if (uint64_compare(offset, uint64_make(0, LONG_MAX)) > 0) {
- printf("reput: remote file is larger than we can deal with\n");
- return 0;
- }
- if (fseek(fp, offset.lo, SEEK_SET) != 0)
- fseek(fp, 0, SEEK_END); /* *shrug* */
+
+ if (seek_file((WFile *)file, offset, FROM_START) != 0)
+ seek_file((WFile *)file, uint64_make(0,0), FROM_END); /* *shrug* */
} else {
offset = uint64_make(0, 0);
}
int len, ret;
while (xfer_upload_ready(xfer) && !err && !eof) {
- len = fread(buffer, 1, sizeof(buffer), fp);
+ len = read_from_file(file, buffer, sizeof(buffer));
if (len == -1) {
printf("error while reading local file\n");
err = 1;
assert(rreq == req);
fxp_close_recv(pktin, rreq);
- fclose(fp);
+ close_rfile(file);
return ret;
}
while ( (newname = sftp_wildcard_get_filename(swcm)) != NULL ) {
cname = canonify(newname);
if (!cname) {
- printf("%s: %s\n", newname, fxp_error());
+ printf("%s: canonify: %s\n", newname, fxp_error());
ret = 0;
}
matched = TRUE;
} else {
cname = canonify(unwcfname);
if (!cname) {
- printf("%s: %s\n", filename, fxp_error());
+ printf("%s: canonify: %s\n", filename, fxp_error());
ret = 0;
}
ret = func(ctx, cname);
int sftp_cmd_close(struct sftp_command *cmd)
{
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
- if (back != NULL && back->socket(backhandle) != NULL) {
+ if (back != NULL && back->connected(backhandle)) {
char ch;
back->special(backhandle, TS_EOF);
sftp_recvdata(&ch, 1);
int i;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
cdir = canonify(dir);
if (!cdir) {
- printf("%s: %s\n", dir, fxp_error());
+ printf("%s: canonify: %s\n", dir, fxp_error());
sfree(unwcdir);
return 0;
}
char *dir;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
dir = canonify(cmd->words[1]);
if (!dir) {
- printf("%s: %s\n", dir, fxp_error());
+ printf("%s: canonify: %s\n", dir, fxp_error());
return 0;
}
int sftp_cmd_pwd(struct sftp_command *cmd)
{
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
int recurse = FALSE;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
fname = canonify(origwfname);
if (!fname) {
- printf("%s: %s\n", origwfname, fxp_error());
+ printf("%s: canonify: %s\n", origwfname, fxp_error());
sfree(unwcfname);
return 0;
}
int recurse = FALSE;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
outfname = canonify(origoutfname);
if (!outfname) {
- printf("%s: %s\n", origoutfname, fxp_error());
+ printf("%s: canonify: %s\n", origoutfname, fxp_error());
if (wcm) {
sfree(wfname);
finish_wildcard_matching(wcm);
int i, ret;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
for (i = 1; i < cmd->nwords; i++) {
dir = canonify(cmd->words[i]);
if (!dir) {
- printf("%s: %s\n", dir, fxp_error());
+ printf("%s: canonify: %s\n", dir, fxp_error());
return 0;
}
if (!result) {
printf("mkdir %s: %s\n", dir, fxp_error());
- sfree(dir);
ret = 0;
- }
+ } else
+ printf("mkdir %s: OK\n", dir);
sfree(dir);
}
return 0;
}
+ printf("rmdir %s: OK\n", dir);
+
return 1;
}
int i, ret;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
if (!result) {
printf("rm %s: %s\n", fname, fxp_error());
- sfree(fname);
return 0;
}
+ printf("rm %s: OK\n", fname);
+
return 1;
}
int i, ret;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
newname = dupcat(ctx->dstfname, "/", p, NULL);
newcanon = canonify(newname);
if (!newcanon) {
- printf("%s: %s\n", newname, fxp_error());
+ printf("%s: canonify: %s\n", newname, fxp_error());
sfree(newname);
return 0;
}
int i, ret;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
ctx->dstfname = canonify(cmd->words[cmd->nwords-1]);
if (!ctx->dstfname) {
- printf("%s: %s\n", ctx->dstfname, fxp_error());
+ printf("%s: canonify: %s\n", ctx->dstfname, fxp_error());
return 0;
}
if (!result || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
printf("get attrs for %s: %s\n", fname,
result ? "file permissions not provided" : fxp_error());
- sfree(fname);
return 0;
}
if (!result) {
printf("set attrs for %s: %s\n", fname, fxp_error());
- sfree(fname);
return 0;
}
struct sftp_context_chmod actx, *ctx = &actx;
if (back == NULL) {
- printf("psftp: not connected to a host; use \"open host.name\"\n");
+ not_connected();
return 0;
}
},
{
"cd", TRUE, "change your remote working directory",
- " [ <New working directory> ]\n"
+ " [ <new working directory> ]\n"
" Change the remote working directory for your SFTP session.\n"
" If a new working directory is not supplied, you will be\n"
" returned to your home directory.\n",
},
{
"chmod", TRUE, "change file permissions and modes",
- " ( <octal-digits> | <modifiers> ) <filename>\n"
- " Change the file permissions on a file or directory.\n"
- " <octal-digits> can be any octal Unix permission specifier.\n"
- " Alternatively, <modifiers> can include:\n"
+ " <modes> <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
+ " Change the file permissions on one or more remote files or\n"
+ " directories.\n"
+ " <modes> can be any octal Unix permission specifier.\n"
+ " Alternatively, <modes> can include the following modifiers:\n"
" u+r make file readable by owning user\n"
" u+w make file writable by owning user\n"
" u+x make file executable by owning user\n"
sftp_cmd_close
},
{
- "del", TRUE, "delete a file",
- " <filename>\n"
- " Delete a file.\n",
+ "del", TRUE, "delete files on the remote server",
+ " <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
+ " Delete a file or files from the server.\n",
sftp_cmd_rm
},
{
"delete", FALSE, "del", NULL, sftp_cmd_rm
},
{
- "dir", TRUE, "list contents of a remote directory",
+ "dir", TRUE, "list remote files",
" [ <directory-name> ]/[ <wildcard> ]\n"
" List the contents of a specified directory on the server.\n"
" If <directory-name> is not given, the current working directory\n"
sftp_cmd_mget
},
{
- "mkdir", TRUE, "create a directory on the remote server",
- " <directory-name>\n"
- " Creates a directory with the given name on the server.\n",
+ "mkdir", TRUE, "create directories on the remote server",
+ " <directory-name> [ <directory-name>... ]\n"
+ " Creates directories with the given names on the server.\n",
sftp_cmd_mkdir
},
{
sftp_cmd_mput
},
{
- "mv", TRUE, "move or rename a file on the remote server",
- " <source-filename> <destination-filename>\n"
- " Moves or renames the file <source-filename> on the server,\n"
- " so that it is accessible under the name <destination-filename>.\n",
+ "mv", TRUE, "move or rename file(s) on the remote server",
+ " <source> [ <source>... ] <destination>\n"
+ " Moves or renames <source>(s) on the server to <destination>,\n"
+ " also on the server.\n"
+ " If <destination> specifies an existing directory, then <source>\n"
+ " may be a wildcard, and multiple <source>s may be given; all\n"
+ " source files are moved into <destination>.\n"
+ " Otherwise, <source> must specify a single file, which is moved\n"
+ " or renamed so that it is accessible under the name <destination>.\n",
sftp_cmd_mv
},
{
"open", TRUE, "connect to a host",
" [<user>@]<hostname> [<port>]\n"
" Establishes an SFTP connection to a given host. Only usable\n"
- " when you did not already specify a host name on the command\n"
- " line.\n",
+ " when you are not already connected to a server.\n",
sftp_cmd_open
},
{
sftp_cmd_quit
},
{
- "reget", TRUE, "continue downloading a file",
+ "reget", TRUE, "continue downloading files",
" [ -r ] [ -- ] <filename> [ <local-filename> ]\n"
" Works exactly like the \"get\" command, but the local file\n"
" must already exist. The download will begin at the end of the\n"
sftp_cmd_mv
},
{
- "reput", TRUE, "continue uploading a file",
+ "reput", TRUE, "continue uploading files",
" [ -r ] [ -- ] <filename> [ <remote-filename> ]\n"
" Works exactly like the \"put\" command, but the remote file\n"
" must already exist. The upload will begin at the end of the\n"
sftp_cmd_rm
},
{
- "rmdir", TRUE, "remove a directory on the remote server",
- " <directory-name>\n"
+ "rmdir", TRUE, "remove directories on the remote server",
+ " <directory-name> [ <directory-name>... ]\n"
" Removes the directory with the given name on the server.\n"
- " The directory will not be removed unless it is empty.\n",
+ " The directory will not be removed unless it is empty.\n"
+ " Wildcards may be used to specify multiple directories.\n",
sftp_cmd_rmdir
}
};
return 0;
}
+int from_backend_untrusted(void *frontend_handle, const char *data, int len)
+{
+ /*
+ * No "untrusted" output should get here (the way the code is
+ * currently, it's all diverted by FLAG_STDERR).
+ */
+ assert(!"Unexpected call to from_backend_untrusted()");
+ return 0; /* not reached */
+}
int sftp_recvdata(char *buf, int len)
{
outptr = (unsigned char *) buf;
}
while (outlen > 0) {
- if (ssh_sftp_loop_iteration() < 0)
+ if (back->exitcode(backhandle) >= 0 || ssh_sftp_loop_iteration() < 0)
return 0; /* doom */
}
printf("%s\n", ver);
printf("Usage: psftp [options] [user@]host\n");
printf("Options:\n");
+ printf(" -V print version information and exit\n");
+ printf(" -pgpfp print PGP key fingerprints and exit\n");
printf(" -b file use specified batchfile\n");
printf(" -bc output batchfile commands\n");
printf(" -be don't stop batchfile processing if errors\n");
printf(" -4 -6 force use of IPv4 or IPv6\n");
printf(" -C enable compression\n");
printf(" -i key private key file for authentication\n");
+ printf(" -noagent disable use of Pageant\n");
+ printf(" -agent enable use of Pageant\n");
printf(" -batch disable all interactive prompts\n");
- printf(" -V print version information\n");
cleanup_exit(1);
}
strncpy(cfg.username, user, sizeof(cfg.username) - 1);
cfg.username[sizeof(cfg.username) - 1] = '\0';
}
- if (!cfg.username[0]) {
- if (!console_get_line("login as: ",
- cfg.username, sizeof(cfg.username), FALSE)) {
- fprintf(stderr, "psftp: no username, aborting\n");
- cleanup_exit(1);
- } else {
- int len = strlen(cfg.username);
- if (cfg.username[len - 1] == '\n')
- cfg.username[len - 1] = '\0';
- }
- }
if (portnumber)
cfg.port = portnumber;
cfg.x11_forward = 0;
cfg.agentfwd = 0;
cfg.portfwd[0] = cfg.portfwd[1] = '\0';
+ cfg.ssh_simple = TRUE;
/* Set up subsystem name. */
strcpy(cfg.remote_cmd, "sftp");
cfg.nopty = TRUE;
/*
- * Set up fallback option, for SSH1 servers or servers with the
+ * Set up fallback option, for SSH-1 servers or servers with the
* sftp subsystem not enabled but the server binary installed
* in the usual place. We only support fallback on Unix
* systems, and we use a kludgy piece of shellery which should
#endif
;
cmdline_tooltype = TOOLTYPE_FILETRANSFER;
- ssh_get_line = &console_get_line;
sk_init();
userhost = user = NULL;
} else if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "-?") == 0) {
usage();
+ } else if (strcmp(argv[i], "-pgpfp") == 0) {
+ pgp_fingerprints();
+ return 1;
} else if (strcmp(argv[i], "-V") == 0) {
version();
} else if (strcmp(argv[i], "-batch") == 0) {
do_sftp(mode, modeflags, batchfile);
- if (back != NULL && back->socket(backhandle) != NULL) {
+ if (back != NULL && back->connected(backhandle)) {
char ch;
back->special(backhandle, TS_EOF);
sftp_recvdata(&ch, 1);