diff options
author | Damien Miller <djm@cvs.openbsd.org> | 2017-11-03 03:46:53 +0000 |
---|---|---|
committer | Damien Miller <djm@cvs.openbsd.org> | 2017-11-03 03:46:53 +0000 |
commit | ccbeea5c3a0f8b6736e0c93bf23095c5b36f29d0 (patch) | |
tree | 4192f1ca1cdaba91217a2a64abea0e7f2a6fb2c3 /usr.bin | |
parent | a597fff09493e4e550bdc1ae5ff1b8069f6a754a (diff) |
allow "cd" and "lcd" commands with no explicit path argument.
lcd will change to the local user's home directory as usual.
cd will change to the starting directory for session (because the
protocol offers no way to obtain the remote user's home directory).
bz#2760 ok dtucker@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/ssh/sftp.1 | 14 | ||||
-rw-r--r-- | usr.bin/ssh/sftp.c | 32 |
2 files changed, 31 insertions, 15 deletions
diff --git a/usr.bin/ssh/sftp.1 b/usr.bin/ssh/sftp.1 index 9b03280be0f..529be7fc6e8 100644 --- a/usr.bin/ssh/sftp.1 +++ b/usr.bin/ssh/sftp.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: sftp.1,v 1.112 2017/10/25 06:19:46 jmc Exp $ +.\" $OpenBSD: sftp.1,v 1.113 2017/11/03 03:46:52 djm Exp $ .\" .\" Copyright (c) 2001 Damien Miller. All rights reserved. .\" @@ -22,7 +22,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: October 25 2017 $ +.Dd $Mdocdate: November 3 2017 $ .Dt SFTP 1 .Os .Sh NAME @@ -301,9 +301,12 @@ must be escaped with backslashes .It Ic bye Quit .Nm sftp . -.It Ic cd Ar path +.It Ic cd Op Ar path Change remote directory to .Ar path . +If +.Ar path +is not specified, then change directory to the one the session started in. .It Ic chgrp Ar grp Ar path Change group of file .Ar path @@ -407,9 +410,12 @@ Note that does not follow symbolic links when performing recursive transfers. .It Ic help Display help text. -.It Ic lcd Ar path +.It Ic lcd Op Ar path Change local directory to .Ar path . +If +.Ar path +is not specified, then change directory to the local user's home directory. .It Ic lls Op Ar ls-options Op Ar path Display local directory listing of either .Ar path diff --git a/usr.bin/ssh/sftp.c b/usr.bin/ssh/sftp.c index 7e9d02968f5..f72e321359f 100644 --- a/usr.bin/ssh/sftp.c +++ b/usr.bin/ssh/sftp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sftp.c,v 1.181 2017/10/21 23:06:24 millert Exp $ */ +/* $OpenBSD: sftp.c,v 1.182 2017/11/03 03:46:52 djm Exp $ */ /* * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> * @@ -195,8 +195,6 @@ static const struct CMD cmds[] = { { NULL, -1, -1 } }; -int interactive_loop(struct sftp_conn *, char *file1, char *file2); - /* ARGSUSED */ static void killchild(int signo) @@ -1260,7 +1258,7 @@ parse_args(const char **cpp, int *ignore_errors, int *aflag, char *cp2, **argv; int base = 0; long l; - int i, cmdnum, optidx, argc; + int path1_mandatory = 0, i, cmdnum, optidx, argc; /* Skip leading whitespace */ cp = cp + strspn(cp, WHITESPACE); @@ -1350,13 +1348,17 @@ parse_args(const char **cpp, int *ignore_errors, int *aflag, case I_RM: case I_MKDIR: case I_RMDIR: + case I_LMKDIR: + path1_mandatory = 1; + /* FALLTHROUGH */ case I_CHDIR: case I_LCHDIR: - case I_LMKDIR: if ((optidx = parse_no_flags(cmd, argv, argc)) == -1) return -1; /* Get pathname (mandatory) */ if (argc - optidx < 1) { + if (!path1_mandatory) + break; /* return a NULL path1 */ error("You must specify a path after a %s command.", cmd); return -1; @@ -1441,7 +1443,7 @@ parse_args(const char **cpp, int *ignore_errors, int *aflag, static int parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, - int err_abort) + const char *startdir, int err_abort) { char *path1, *path2, *tmp; int ignore_errors = 0, aflag = 0, fflag = 0, hflag = 0, @@ -1521,6 +1523,8 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, err = do_rmdir(conn, path1); break; case I_CHDIR: + if (path1 == NULL || *path1 == '\0') + path1 = xstrdup(startdir); path1 = make_absolute(path1, *pwd); if ((tmp = do_realpath(conn, path1)) == NULL) { err = 1; @@ -1569,6 +1573,8 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, err = do_df(conn, path1, hflag, iflag); break; case I_LCHDIR: + if (path1 == NULL || *path1 == '\0') + path1 = xstrdup("~"); tmp = tilde_expand_filename(path1, getuid()); free(path1); path1 = tmp; @@ -2053,11 +2059,11 @@ complete(EditLine *el, int ch) return ret; } -int +static int interactive_loop(struct sftp_conn *conn, char *file1, char *file2) { char *remote_path; - char *dir = NULL; + char *dir = NULL, *startdir = NULL; char cmd[2048]; int err, interactive; EditLine *el = NULL; @@ -2099,6 +2105,7 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2) remote_path = do_realpath(conn, "."); if (remote_path == NULL) fatal("Need cwd"); + startdir = xstrdup(remote_path); if (file1 != NULL) { dir = xstrdup(file1); @@ -2109,8 +2116,9 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2) mprintf("Changing to: %s\n", dir); snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); if (parse_dispatch_command(conn, cmd, - &remote_path, 1) != 0) { + &remote_path, startdir, 1) != 0) { free(dir); + free(startdir); free(remote_path); free(conn); return (-1); @@ -2122,8 +2130,9 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2) file2 == NULL ? "" : " ", file2 == NULL ? "" : file2); err = parse_dispatch_command(conn, cmd, - &remote_path, 1); + &remote_path, startdir, 1); free(dir); + free(startdir); free(remote_path); free(conn); return (err); @@ -2179,11 +2188,12 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2) signal(SIGINT, cmd_interrupt); err = parse_dispatch_command(conn, cmd, &remote_path, - batchmode); + startdir, batchmode); if (err != 0) break; } free(remote_path); + free(startdir); free(conn); if (el != NULL) |