History log of /openbsd-current/usr.bin/ssh/sftp.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.238 30-Apr-2024 djm

flush stdout after writing "sftp>" prompt when not using editline.

From Alpine Linux via GHPR480


Revision tags: OPENBSD_7_5_BASE
# 1.237 01-Feb-2024 djm

whitespace


Revision tags: OPENBSD_7_4_BASE
# 1.236 10-Sep-2023 djm

rename remote_glob() -> sftp_glob() to match other API


# 1.235 08-Sep-2023 djm

the sftp code was one of my first contributions to OpenSSH and it
shows - the function names are terrible.

Rename do_blah() to sftp_blah() to make them less so.

Completely mechanical except for sftp_stat() and sftp_lstat() which
change from returning a pointer to a static variable (error-prone) to
taking a pointer to a caller-provided receiver.


# 1.234 12-Apr-2023 jsg

fix double words
ok dtucker@


# 1.233 06-Apr-2023 djm

don't care about glob() return value here.


# 1.232 31-Mar-2023 dtucker

Explictly ignore return codes where we don't check them. From Dmitry
Belyavskiy via github PR#238, ok djm@


# 1.231 29-Mar-2023 dtucker

Plug another potential mem leak in process_put. It allocates abs_dst
inside a loop but only frees it on exit, so free inside the loop if
necessary. Coverity CID 291837, ok djm@


# 1.230 28-Mar-2023 dtucker

Plug more mem leaks in sftp by making make_absolute_pwd_glob work in
the same way as make_absolute: you pass it a dynamically allocated string
and it either returns it, or frees it and allocates a new one.
Patch from emaste at freebsd.org and https://reviews.freebsd.org/D37253
ok djm@


Revision tags: OPENBSD_7_3_BASE
# 1.229 12-Mar-2023 dtucker

calloc can return NULL but xcalloc cannot. From Coverity CID 291881,
ok djm@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.237 01-Feb-2024 djm

whitespace


Revision tags: OPENBSD_7_4_BASE
# 1.236 10-Sep-2023 djm

rename remote_glob() -> sftp_glob() to match other API


# 1.235 08-Sep-2023 djm

the sftp code was one of my first contributions to OpenSSH and it
shows - the function names are terrible.

Rename do_blah() to sftp_blah() to make them less so.

Completely mechanical except for sftp_stat() and sftp_lstat() which
change from returning a pointer to a static variable (error-prone) to
taking a pointer to a caller-provided receiver.


# 1.234 12-Apr-2023 jsg

fix double words
ok dtucker@


# 1.233 06-Apr-2023 djm

don't care about glob() return value here.


# 1.232 31-Mar-2023 dtucker

Explictly ignore return codes where we don't check them. From Dmitry
Belyavskiy via github PR#238, ok djm@


# 1.231 29-Mar-2023 dtucker

Plug another potential mem leak in process_put. It allocates abs_dst
inside a loop but only frees it on exit, so free inside the loop if
necessary. Coverity CID 291837, ok djm@


# 1.230 28-Mar-2023 dtucker

Plug more mem leaks in sftp by making make_absolute_pwd_glob work in
the same way as make_absolute: you pass it a dynamically allocated string
and it either returns it, or frees it and allocates a new one.
Patch from emaste at freebsd.org and https://reviews.freebsd.org/D37253
ok djm@


Revision tags: OPENBSD_7_3_BASE
# 1.229 12-Mar-2023 dtucker

calloc can return NULL but xcalloc cannot. From Coverity CID 291881,
ok djm@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.236 10-Sep-2023 djm

rename remote_glob() -> sftp_glob() to match other API


# 1.235 08-Sep-2023 djm

the sftp code was one of my first contributions to OpenSSH and it
shows - the function names are terrible.

Rename do_blah() to sftp_blah() to make them less so.

Completely mechanical except for sftp_stat() and sftp_lstat() which
change from returning a pointer to a static variable (error-prone) to
taking a pointer to a caller-provided receiver.


# 1.234 12-Apr-2023 jsg

fix double words
ok dtucker@


# 1.233 06-Apr-2023 djm

don't care about glob() return value here.


# 1.232 31-Mar-2023 dtucker

Explictly ignore return codes where we don't check them. From Dmitry
Belyavskiy via github PR#238, ok djm@


# 1.231 29-Mar-2023 dtucker

Plug another potential mem leak in process_put. It allocates abs_dst
inside a loop but only frees it on exit, so free inside the loop if
necessary. Coverity CID 291837, ok djm@


# 1.230 28-Mar-2023 dtucker

Plug more mem leaks in sftp by making make_absolute_pwd_glob work in
the same way as make_absolute: you pass it a dynamically allocated string
and it either returns it, or frees it and allocates a new one.
Patch from emaste at freebsd.org and https://reviews.freebsd.org/D37253
ok djm@


Revision tags: OPENBSD_7_3_BASE
# 1.229 12-Mar-2023 dtucker

calloc can return NULL but xcalloc cannot. From Coverity CID 291881,
ok djm@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.235 08-Sep-2023 djm

the sftp code was one of my first contributions to OpenSSH and it
shows - the function names are terrible.

Rename do_blah() to sftp_blah() to make them less so.

Completely mechanical except for sftp_stat() and sftp_lstat() which
change from returning a pointer to a static variable (error-prone) to
taking a pointer to a caller-provided receiver.


# 1.234 12-Apr-2023 jsg

fix double words
ok dtucker@


# 1.233 06-Apr-2023 djm

don't care about glob() return value here.


# 1.232 31-Mar-2023 dtucker

Explictly ignore return codes where we don't check them. From Dmitry
Belyavskiy via github PR#238, ok djm@


# 1.231 29-Mar-2023 dtucker

Plug another potential mem leak in process_put. It allocates abs_dst
inside a loop but only frees it on exit, so free inside the loop if
necessary. Coverity CID 291837, ok djm@


# 1.230 28-Mar-2023 dtucker

Plug more mem leaks in sftp by making make_absolute_pwd_glob work in
the same way as make_absolute: you pass it a dynamically allocated string
and it either returns it, or frees it and allocates a new one.
Patch from emaste at freebsd.org and https://reviews.freebsd.org/D37253
ok djm@


Revision tags: OPENBSD_7_3_BASE
# 1.229 12-Mar-2023 dtucker

calloc can return NULL but xcalloc cannot. From Coverity CID 291881,
ok djm@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.234 12-Apr-2023 jsg

fix double words
ok dtucker@


# 1.233 06-Apr-2023 djm

don't care about glob() return value here.


# 1.232 31-Mar-2023 dtucker

Explictly ignore return codes where we don't check them. From Dmitry
Belyavskiy via github PR#238, ok djm@


# 1.231 29-Mar-2023 dtucker

Plug another potential mem leak in process_put. It allocates abs_dst
inside a loop but only frees it on exit, so free inside the loop if
necessary. Coverity CID 291837, ok djm@


# 1.230 28-Mar-2023 dtucker

Plug more mem leaks in sftp by making make_absolute_pwd_glob work in
the same way as make_absolute: you pass it a dynamically allocated string
and it either returns it, or frees it and allocates a new one.
Patch from emaste at freebsd.org and https://reviews.freebsd.org/D37253
ok djm@


Revision tags: OPENBSD_7_3_BASE
# 1.229 12-Mar-2023 dtucker

calloc can return NULL but xcalloc cannot. From Coverity CID 291881,
ok djm@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.229 12-Mar-2023 dtucker

calloc can return NULL but xcalloc cannot. From Coverity CID 291881,
ok djm@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.228 08-Mar-2023 dtucker

Plug mem leak. Coverity CID 405196, ok djm@


# 1.227 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.226 27-Feb-2023 dtucker

Plug mem leak on globbed ls error path. Spotted by Coverity, ok deraadt@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.225 05-Jan-2023 djm

suppress "Connection closed" message when in quiet mode


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.224 16-Dec-2022 jmc

add -X to usage();


# 1.223 16-Dec-2022 djm

add a -X option to both scp(1) and sftp(1) to allow control over
some SFTP protocol knobs: the copy buffer length and the number of
inflight requests, both of which are used during upload/download.

Previously these could be controlled in sftp(1) using the -b/-R options.
This makes them available in both SFTP protocol clients using the same
option character sequence.

ok dtucker@


Revision tags: OPENBSD_7_2_BASE
# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.222 19-Sep-2022 djm

use users-groups-by-id@openssh.com sftp-server extension (when
available) to fill in user/group names for directory listings.
Implement a client-side cache of see uid/gid=>user/group names.
ok markus@


# 1.221 19-Sep-2022 djm

extend sftp-common.c:extend ls_file() to support supplied user/group
names; ok markus@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.220 16-Sep-2022 djm

sftp: Be a bit more clever about completions

There are commands (e.g. "get" or "put") that accept two
arguments, a local path and a remote path. However, the way
current completion is written doesn't take this distinction into
account and always completes remote or local paths.

By expanding CMD struct and "cmds" array this distinction can be
reflected and with small adjustment to completer code the correct
path can be completed.

By Michal Privoznik, ok dtucker@


# 1.219 16-Sep-2022 djm

sftp: Don't attempt to complete arguments for non-existent commands

If user entered a non-existent command (e.g. because they made a
typo) there is no point in trying to complete its arguments. Skip
calling complete_match() if that's the case.

From Michal Privoznik


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.218 28-Jun-2022 jmc

reflect the update to -D arg name in usage();


# 1.217 27-Jun-2022 djm

allow arguments to sftp -D option, e.g.
sftp -D "/usr/libexec/sftp-server -el debug3"

ok markus@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.216 13-May-2022 djm

arrange for scp, when in sftp mode, to not ftruncate(3) files early

previous behavious of unconditionally truncating the destination file
would cause "scp ~/foo localhost:" and "scp localhost:foo ~/" to
delete all the contents of their destination.

spotted by solene@ sthen@, also bz3431; ok dtucker@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.215 08-May-2022 djm

When performing operations that glob(3) a remote path, ensure that the
implicit working directory used to construct that path escapes glob(3)
characters.

This prevents glob characters from being processed in places they
shouldn't, e.g. "cd /tmp/a*/", "get *.txt" should have the get operation
treat the path "/tmp/a*" literally and not attempt to expand it.

Reported by Lusia Kundel; ok markus@


Revision tags: OPENBSD_7_1_BASE
# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.214 31-Mar-2022 djm

add a sftp client "cp" command that supports server-side copying
of files. Useful for this task and for testing the copy-data
extension. Patch from Mike Frysinger; ok dtucker@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.213 18-Mar-2022 djm

remove blank line


Revision tags: OPENBSD_7_0_BASE
# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.212 11-Sep-2021 schwarze

Do not ignore SIGINT while waiting for input if editline(3) is not used.
Instead, in non-interactive mode, exit sftp(1), like for other serious errors.
As pointed out by dtucker@, when compiled without editline(3) support in
portable OpenSSH, the el == NULL branch is also used for interactive mode.
In that case, discard the input line and provide a fresh prompt to the user
just like in the case where editline(3) is used.
OK djm@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.211 12-Aug-2021 schwarze

In the editline(3) branch of the sftp(1) event loop, handle SIGINT
rather than ignoring it, such that the user can use Ctrl-C to discard
the currently edited command line and get a fresh prompt, just like
in ftp(1), bc(1), and in shells.

It is critical to not use ssl_signal() for this particular case
because that function unconditionally sets SA_RESTART, but here we
need the signal to interrupt the read(2) in the el_gets(3) event loop.

OK dtucker@ deraadt@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.210 07-Aug-2021 djm

make scp(1) in SFTP mode follow symlinks like traditional scp(1)
ok markus@


Revision tags: OPENBSD_6_9_BASE
# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.209 03-Apr-2021 djm

typos in comments; GHPR#180 from Ville Skytt��


# 1.208 03-Apr-2021 djm

highly polished whitespace, mostly fixing spaces-for-tab and bad
indentation on continuation lines. Prompted by GHPR#185


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.207 31-Mar-2021 djm

Use new limits@openssh.com protocol extension to let the client select
good limits based on what the server supports. Split the download and
upload buffer sizes to allow them to be chosen independently.

In practice (and assuming upgraded sftp/sftp-server at each end), this
increases the download buffer 32->64KiB and the upload buffer
32->255KiB.

Patches from Mike Frysinger; ok dtucker@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.206 08-Jan-2021 djm

don't try to use timespeccmp(3) directly as a qsort(3) comparison
function - it returns 0/1 and not the -1/0/1 that qsort expectes.

fixes sftp "ls -ltr" under some circumstances.

Based on patch by Masahiro Matsuya via bz3248.


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.205 04-Dec-2020 djm

shuffle a few utility functions into sftp-client.c; from Jakub Jelen


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.204 29-Oct-2020 djm

whitespace; no code change


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.203 18-Oct-2020 djm

use the new variant log macros instead of prepending __func__ and
appending ssh_err(r) manually; ok markus@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.202 04-Oct-2020 dtucker

Allow full range of UIDs and GIDs for sftp chown and chgrp on 32bit
platforms instead of being limited by LONG_MAX. bz#3206, found by
booking00 at sina.cn, ok markus@


Revision tags: OPENBSD_6_8_BASE
# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.201 03-Aug-2020 djm

allow -A to explicitly enable agent forwarding in scp and sftp. The
default remains to not forward an agent, even when ssh_config enables
it. ok jmc dtucker markus


Revision tags: OPENBSD_6_7_BASE
# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.200 03-Apr-2020 jmc

sort -N and add it to usage();


# 1.199 03-Apr-2020 djm

Add a flag to re-enable verbose output when in batch mode;
requested in bz3135; ok dtucker


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.198 26-Feb-2020 dtucker

Have sftp reject "-1" in the same way as ssh(1) and scp(1) do instead
of accepting and silently ignoring it since protocol 1 support has
been removed. Spotted by shivakumar2696 at gmail.com, ok deraadt@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.197 23-Jan-2020 dtucker

Replace all calls to signal(2) with a wrapper around sigaction(2).
This wrapper blocks all other signals during the handler preventing
races between handlers, and sets SA_RESTART which should reduce the
potential for short read/write operations.


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.196 01-Nov-2019 djm

fix a race condition in the SIGCHILD handler that could turn in
to a kill(-1); bz3084, reported by Gao Rui, ok dtucker@


Revision tags: OPENBSD_6_6_BASE
# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.195 02-Oct-2019 djm

remove some duplicate #includes


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.194 10-Jul-2019 tb

Fix a typo and make <esc><right> move right to the closest end of a word
just like <esc><left> moves left to the closest beginning of a word.

ok djm


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.193 19-Jun-2019 jmc

from tim:
- for reput, it is remote-path which is optional, not local-path
- sync help

from deraadt:
- prefer -R and undocument -r (but add a comment for future editors)

from schwarze:
- prefer -p and undocument -P (as above. the comment was schwarze's too)

more:
- add the -f flag to reput and reget
- sort help (i can;t remember who suggested this originally)

djm and deraadt were ok with earlier versions of this;
tim and schwarze ok


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.192 07-Jun-2019 dtucker

Check for user@host when parsing sftp target. This allows user@[1.2.3.4]
to work without a path in addition to with one. bz#2999, ok djm@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.191 06-Jun-2019 otto

Replace calls to ssh_malloc_init() by a static init of malloc_options.
Prepares for changes in the way malloc is initialized. ok guenther@ dtucker@


Revision tags: OPENBSD_6_5_BASE
# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.190 21-Jan-2019 tb

Add a -J option as a shortcut for -o Proxyjump= to scp(1) and sftp(1)
to match ssh(1)'s interface.

ok djm


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.189 16-Jan-2019 djm

Add "-h" flag to sftp chown/chgrp/chmod commands to request they do
not follow symlinks. Requires recently-committed lsetstat@openssh.com
extension on the server side.

ok markus@ dtucker@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.188 16-Nov-2018 djm

use path_absolute() for pathname checks; from Manoj Ampalam


# 1.187 16-Nov-2018 djm

support a prefix of '@' to suppress echo of sftp batch commands;
bz#2926; ok dtucker@


Revision tags: OPENBSD_6_4_BASE
# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.186 07-Sep-2018 dtucker

Add FALLTHROUGH comments where appropriate. Patch from jjelen at redhat
via bz#2687.


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.185 26-Apr-2018 bluhm

Since the previous commit, ssh regress test sftp-chroot was failing.
The sftp program terminated with the wrong exit code as sftp called
fatal() instad of exit(0). So when the sigchld handler waits for
the child, remember that it was found. Then don't expect that
main() can wait again.
OK dtucker@


# 1.184 13-Apr-2018 djm

notify user immediately when underlying ssh process dies;
patch from Thomas Kuthan in bz2719; ok dtucker@


# 1.183 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.182 03-Nov-2017 djm

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@


# 1.181 21-Oct-2017 millert

Add URI support to ssh, sftp and scp. For example ssh://user@host
or sftp://user@host/path. The connection parameters described in
draft-ietf-secsh-scp-sftp-ssh-uri-04 are not implemented since the
ssh fingerprint format in the draft uses md5 with no way to specify
the hash function type. OK djm@


Revision tags: OPENBSD_6_2_BASE
# 1.180 10-Jun-2017 djm

implement sorting for globbed ls; bz#2649 ok dtucker@


# 1.179 02-May-2017 djm

remove -1 / -2 options; pointed out by jmc@


Revision tags: OPENBSD_6_1_BASE
# 1.178 15-Feb-2017 djm

fix division by zero crash in "df" output when server returns zero
total filesystem blocks/inodes. Spotted by Guido Vranken; ok dtucker@


# 1.177 18-Oct-2016 millert

Install a signal handler for tty-generated signals and wait for the
ssh child to suspend before suspending sftp. This lets ssh restore
the terminal mode as needed when it is suspended at the password
prompt. OK dtucker@


# 1.176 12-Sep-2016 deraadt

Add MAXIMUM(), MINIMUM(), and ROUNDUP() to misc.h, then use those definitions
rather than pulling <sys/param.h> and unknown namespace pollution.
ok djm markus dtucker


Revision tags: OPENBSD_6_0_BASE
# 1.175 22-Jul-2016 djm

constify a few functions' arguments; patch from Jakub Jelen bz#2581


# 1.174 25-May-2016 schwarze

To prevent screwing up terminal settings when printing to the
terminal, for ASCII and UTF-8, escape bytes not forming characters
and bytes forming non-printable characters with vis(3) VIS_OCTAL.
For other character sets, abort printing of the current string in
these cases. In particular,
* let scp(1) respect the local user's LC_CTYPE locale(1);
* sanitize data received from the remote host;
* sanitize filenames, usernames, and similar data even locally;
* take character display widths into account for the progressmeter.

This is believed to be sufficient to keep the local terminal safe
on OpenBSD, but bad things can still happen on other systems with
state-dependent locales because many places in the code print
unencoded ASCII characters into the output stream.

Using feedback from djm@ and martijn@,
various aspects discussed with many others.

deraadt@ says it should go in now, i probably already hesitated too long


# 1.173 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.172 15-Feb-2016 dtucker

Add a function to enable security-related malloc_options. With and ok
deraadt@, something similar has been in the snaps for a while.


# 1.171 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.170 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.169 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


# 1.168 26-Nov-2014 millert

Prefer setvbuf() to setlinebuf() for portability; ok deraadt@


# 1.167 06-Oct-2014 djm

correct options in usage(); from mancha1 AT zoho.com


# 1.166 20-Aug-2014 deraadt

djm how did you make a typo like that...


# 1.165 19-Aug-2014 djm

~-expand lcd paths


Revision tags: OPENBSD_5_6_BASE
# 1.164 09-Jul-2014 djm

more useful error message when GLOB_NOSPACE occurs;
bz#2254, patch from Orion Poplawski


# 1.163 05-May-2014 logan

Zap extra whitespace.

OK from djm@ and dtucker@


# 1.162 29-Apr-2014 dtucker

Don't attempt to append a nul quote char to the filename. Should prevent
fatal'ing with "el_insertstr failed" when there's a single quote char
somewhere in the string. bz#2238, ok markus@


# 1.161 29-Apr-2014 dtucker

Move nulling of variable next to where it's freed. ok markus@


# 1.160 22-Apr-2014 logan

Sort the sftp command list.

OK from djm@


# 1.159 21-Apr-2014 logan

Implement sftp upload resume support.

OK from djm@, with input from guenther@, mlarkin@ and
okan@


Revision tags: OPENBSD_5_5_BASE
# 1.158 20-Nov-2013 deraadt

unsigned casts for ctype macros where neccessary
ok guenther millert markus


# 1.157 17-Oct-2013 jmc

tweak previous;


# 1.156 17-Oct-2013 djm

fsync@openssh.com protocol extension for sftp-server
client support to allow calling fsync() faster successful transfer
patch mostly by imorgan AT nas.nasa.gov; bz#1798
"fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@


# 1.155 31-Aug-2013 djm

make ^w match ksh behaviour (delete previous word instead of entire line)


# 1.154 09-Aug-2013 djm

enable ctrl-left-arrow and ctrl-right-arrow to move forward/back a word;
matching ksh's relatively recent change.


# 1.153 09-Aug-2013 djm

do getopt parsing for all sftp commands (with an empty optstring for
commands without arguments) to ensure consistent behaviour


# 1.152 08-Aug-2013 djm

add a "-l" flag for the rename command to force it to use the silly
standard SSH_FXP_RENAME command instead of the POSIX-rename- like
posix-rename@openssh.com extension.

intended for use in regress tests, so no documentation.


# 1.151 08-Aug-2013 djm

fix two year old regression: symlinking a file would incorrectly
canonicalise the target path. bz#2129 report from delphij AT freebsd.org


# 1.150 07-Aug-2013 jmc

sort -a;


# 1.149 06-Aug-2013 djm

fix some whitespace at EOL

make list of commands an enum rather than a long list of defines

add -a to usage()


Revision tags: OPENBSD_5_4_BASE
# 1.148 25-Jul-2013 djm

sftp support for resuming partial downloads; patch mostly by Loganaden
Velvindron/AfriNIC with some tweaks by me; feedback and ok dtucker@
"Just be careful" deraadt@


# 1.147 12-Jul-2013 djm

fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.146 04-Jun-2013 dtucker

Make sftp's libedit interface marginally multibyte aware by building up the
quoted string by character instead of by byte. Prevents failures when linked
against a libedit built with wide character support (bz#1990). "looks ok" djm


# 1.145 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.144 16-May-2013 dtucker

Fix some "unused result" warnings found via clang and -portable. ok markus@


# 1.143 18-Apr-2013 djm

make "sftp -q" do what it says on the sticker: hush everything but errors;
ok dtucker@


Revision tags: OPENBSD_5_3_BASE
# 1.142 08-Feb-2013 djm

fix NULL deref when built without libedit and control characters
entered as command; debugging and patch from Iain Morgan an
Loganaden Velvindron in bz#1956


# 1.141 05-Oct-2012 markus

fix signed vs unsigned warning; feedback & ok: djm@


# 1.140 21-Sep-2012 dtucker

Fix handling of filenames containing escaped globbing characters and escape
"#" and "*". Patch from Jean-Marc Robert via tech@, ok djm.


# 1.139 21-Sep-2012 dtucker

Fix improper handling of absolute paths when PWD is part of the completed
path. Patch from Jean-Marc Robert via tech@, ok djm.


# 1.138 18-Sep-2012 dtucker

Add bounds check on sftp tab-completion. Part of a patch from from Jean-Marc
Robert via tech@, ok djm


# 1.137 17-Sep-2012 djm

an XXX for later


Revision tags: OPENBSD_5_2_BASE
# 1.136 22-Jun-2012 dtucker

Remove unused variable leftover from tab-completion changes.
From Steve.McClellan at radisys com, ok markus@


# 1.135 20-Apr-2012 djm

setlinebuf(3) is more readable than setvbuf(.., _IOLBF, ...)


Revision tags: OPENBSD_5_1_BASE
# 1.134 16-Nov-2011 oga

Don't leak list in complete_cmd_parse if there are no commands found.

Discovered when I was ``borrowing'' this code for something else.

ok djm@


# 1.133 22-Sep-2011 djm

don't let remote_glob() implicitly sort its results in do_globbed_ls() -
in all likelihood, they will be resorted anyway


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.132 04-Dec-2010 djm

add a protocol extension to support a hard link operation. It is
available through the "ln" command in the client. The old "ln"
behaviour of creating a symlink is available using its "-s" option
or through the preexisting "symlink" command; based on a patch from
miklos AT szeredi.hu in bz#1555; ok markus@


# 1.131 23-Oct-2010 sthen

escape '[' in filename tab-completion; fix a type while there.
ok djm@


# 1.130 05-Oct-2010 djm

use default shell /bin/sh if $SHELL is ""; ok markus@


# 1.129 26-Sep-2010 djm

when performing an "ls" in columnated (short) mode, only call
ioctl(TIOCGWINSZ) once to get the window width instead of per-
filename


# 1.128 25-Sep-2010 djm

make use of new glob(3) GLOB_KEEPSTAT extension to save extra server
rountrips to fetch per-file stat(2) information.


# 1.127 23-Sep-2010 jmc

add [-l limit] to usage();


# 1.126 22-Sep-2010 djm

add an option per-read/write callback to atomicio

factor out bandwidth limiting code from scp(1) into a generic bandwidth
limiter that can be attached using the atomicio callback mechanism

add a bandwidth limit option to sftp(1) using the above

"very nice" markus@


Revision tags: OPENBSD_4_8_BASE
# 1.125 18-Jun-2010 djm

unbreak ls in working directories that contains globbing characters in
their pathnames. bz#1655 reported by vgiffin AT apple.com


# 1.124 05-May-2010 dtucker

restore mput and mget which got lost in the tab-completion changes.
found by Kenneth Whitaker, ok djm@


Revision tags: OPENBSD_4_7_BASE
# 1.123 27-Jan-2010 djm

add missing "p" flag to getopt optstring;
bz#1704 from imorgan AT nas.nasa.gov


# 1.122 15-Jan-2010 guenther

Reset SIGTERM to SIG_DFL before executing ssh, so that even if sftp
inherited SIGTERM as ignored it will still be able to kill the ssh it
starts.

ok dtucker@


# 1.121 13-Jan-2010 jmc

sftp.1: put ls -h in the right place
sftp.c: as above, plus add -p to get/put, and shorten their arg names
to keep the help usage nicely aligned

ok djm


# 1.120 13-Jan-2010 djm

don't append a space after inserting a completion of a directory (i.e.
a path ending in '/') for a slightly better user experience; ok dtucker@


# 1.119 13-Jan-2010 djm

support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@


# 1.118 09-Jan-2010 dtucker

Prevent sftp from derefing a null pointer when given a "-" without a command.
Also, allow whitespace to follow a "-". bz#1691, path from Colin Watson via
Debian. ok djm@ deraadt@


# 1.117 08-Jan-2010 dtucker

Fix two warnings: possibly used unitialized and use a nul byte instead of
NULL pointer. ok djm@


# 1.116 04-Jan-2010 djm

Implement tab-completion of commands, local and remote filenames for sftp.
Hacked on and off for some time by myself, mouring, Carlos Silva (via 2009
Google Summer of Code) and polished to a fine sheen by myself again.
It should deal more-or-less correctly with the ikky corner-cases presented
by quoted filenames, but the UI could still be slightly improved.
In particular, it is quite slow for remote completion on large directories.
bz#200; ok markus@


# 1.115 20-Dec-2009 guenther

When passing user-controlled options with arguments to other programs,
pass the option and option argument as separate argv entries and
not smashed into one (e.g., as -l foo and not -lfoo). Also, always
pass a "--" argument to stop option parsing, so that a positional
argument that starts with a '-' isn't treated as an option. This
fixes some error cases as well as the handling of hostnames and
filenames that start with a '-'.

Based on a diff by halex@
ok halex@ djm@ deraadt@


# 1.114 06-Dec-2009 dtucker

fix potential divide-by-zero in sftp's "df" output when talking to a server
that reports zero files on the filesystem (Unix filesystems always have at
least the root inode). From Steve McClellan at radisys, ok djm@


# 1.113 22-Nov-2009 halex

make passing of zero-length arguments to ssh safe by
passing "-<switch>" "<value>" rather than "-<switch><value>"

ok dtucker@, guenther@, djm@


# 1.112 20-Nov-2009 djm

bz#1588 change "Connecting to host..." message to "Connected to host."
and delay it until after the sftp protocol connection has been established.
Avoids confusing sequence of messages when the underlying ssh connection
experiences problems. ok dtucker@


# 1.111 18-Aug-2009 djm

recursive transfer support for get/put and on the commandline
work mostly by carlosvsilvapt@gmail.com for the Google Summer of Code
with some tweaks by me; "go for it" deraadt@


# 1.110 13-Aug-2009 jmc

sync synopsis and usage();


# 1.109 13-Aug-2009 djm

Swizzle options: "-P sftp_server_path" moves to "-D sftp_server_path",
add "-P port" to match scp(1). Fortunately, the -P option is only really
used by our regression scripts.

part of larger patch from carlosvsilvapt@gmail.com for his Google Summer
of Code work; ok deraadt markus


# 1.108 12-Aug-2009 djm

support most of scp(1)'s commandline arguments in sftp(1), as a first
step towards making sftp(1) a drop-in replacement for scp(1).
One conflicting option (-P) has not been changed, pending further
discussion.

Patch from carlosvsilvapt@gmail.com as part of his work in the
Google Summer of Code


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.107 02-Feb-2009 dtucker

Initialize a few variables to prevent spurious "may be used uninitialized"
warnings from newer gcc's. ok djm@


# 1.106 09-Dec-2008 sobrado

update for the synopses displayed by the 'help' command, there are a
few missing flags; add 'bye' to the output of 'help'; sorting and spacing.

jmc@ suggested replacing .Oo/.Oc with a single .Op macro.

ok jmc@


# 1.105 09-Dec-2008 djm

correct sftp(1) and corresponding usage syntax;
bz#1518 patch from imorgan AT nas.nasa.gov; ok deraadt@ improved diff jmc@


# 1.104 09-Dec-2008 djm

Deal correctly with failures in remote stat() operation in sftp,
correcting fail-on-error behaviour in batchmode. bz#1541 report and
fix from anedvedicky AT gmail.com; ok markus@


Revision tags: OPENBSD_4_4_BASE
# 1.103 13-Jul-2008 djm

increase number of piplelined requests so they properly fill the
(recently increased) channel window. prompted by rapier AT psc.edu;
ok markus@


# 1.102 21-Jun-2008 martynas

use optopt to get invalid flag, instead of return value of getopt,
which is always '?'; ok djm@


# 1.101 08-Jun-2008 dtucker

Have the sftp client store the statvfs replies in wire format,
which prevents problems when the server's native sizes exceed the
client's.

Also extends the sizes of the remaining 32bit wire format to 64bit,
they're specified as unsigned long in the standard.


# 1.100 18-Apr-2008 djm

introduce sftp extension methods statvfs@openssh.com and
fstatvfs@openssh.com that implement statvfs(2)-like operations,
based on a patch from miklos AT szeredi.hu (bz#1399)

also add a "df" command to the sftp client that uses the
statvfs@openssh.com to produce a df(1)-like display of filesystem
space and inode utilisation

ok markus@


Revision tags: OPENBSD_4_3_BASE
# 1.99 20-Jan-2008 djm

When uploading, correctly handle the case of an unquoted filename with
glob metacharacters that match a file exactly but not as a glob, e.g. a
file called "[abcd]". report and test cases from duncan2nd AT gmx.de


# 1.98 12-Dec-2007 djm

unbreak lls command and add a regress test that would have caught the
breakage; spotted by mouring@


# 1.97 24-Oct-2007 djm

rework argument splitting and parsing to cope correctly with common shell
escapes and make handling of escaped characters consistent with sh(1) and
between sftp commands (especially between ones that glob their arguments
and ones that don't).
parse command flags using getopt(3) rather than hand-rolled parsers.
ok dtucker@


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.96 03-Jan-2007 stevesk

ARGSUSED for lint


# 1.95 03-Jan-2007 stevesk

spaces


# 1.94 23-Nov-2006 ray

Don't access buf[strlen(buf) - 1] for zero-length strings.

``ok by me'' djm@.


# 1.93 30-Sep-2006 ray

Clear errno before calling the strtol functions.

From Paul Stoeber <x0001 at x dot de1 dot cc>.

OK deraadt@.


# 1.92 19-Sep-2006 otto

Use S_IS* macros insted of masking with S_IF* flags. The latter may
have multiple bits set, which lead to surprising results. Spotted by
Paul Stoeber, more to come. ok millert@ pedro@ jaredy@ djm@


Revision tags: OPENBSD_4_0_BASE
# 1.91 03-Aug-2006 deraadt

branches: 1.91.4;
almost entirely get rid of the culture of ".h files that include .h files"
ok djm, sort of ok stevesk
makes the pain stop in one easy step


# 1.90 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.89 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.88 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.87 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.86 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.85 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.84 10-Jul-2006 stevesk

buffer.h only needed in sftp-common.h and remove some unneeded
user includes; ok djm@


# 1.83 08-Jul-2006 stevesk

move #include <sys/socket.h> out of includes.h


# 1.82 17-May-2006 markus

fix leak; coverity via Kylene Jo Hall


# 1.81 20-Apr-2006 djm

Switch from using pipes to socketpairs for communication between
sftp/scp and ssh, and between sshd and its subprocesses. This saves
a file descriptor per session and apparently makes userland ppp over
ssh work; ok markus@ deraadt@


# 1.80 27-Mar-2006 djm

always use a format string for addargs; spotted by mouring@


# 1.79 25-Mar-2006 djm

Put $OpenBSD$ tags back (as comments) to replace the RCSID()s that
Theo nuked - our scripts to sync -portable need them in the files


# 1.78 25-Mar-2006 djm

"abormally" is a perfectly cromulent word, but "abnormally" is better


# 1.77 19-Mar-2006 deraadt

RCSID() can die


# 1.76 19-Mar-2006 djm

more memory leaks detected by Coverity via elad AT netbsd.org;
deraadt@ ok


Revision tags: OPENBSD_3_9_BASE
# 1.75 20-Feb-2006 stevesk

branches: 1.75.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.74 20-Feb-2006 stevesk

move #include <signal.h> out of includes.h; ok markus@


# 1.73 10-Feb-2006 stevesk

move #include <sys/wait.h> out of includes.h; ok markus@


# 1.72 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.71 08-Feb-2006 stevesk

move #include <paths.h> out of includes.h; ok markus@


# 1.70 31-Jan-2006 djm

fix local arbitrary command execution vulnerability on local/local and
remote/remote copies (CVE-2006-0225, bz #1094), patch by
t8m AT centrum.cz, polished by dtucker@ and myself; ok markus@


# 1.69 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.68 31-Oct-2005 dtucker

Fix sorting with "ls -1" command. From Robert Tsai, "looks right" deraadt@


# 1.67 13-Sep-2005 djm

ensure that stdio fds are attached; ok deraadt@


Revision tags: OPENBSD_3_8_BASE
# 1.66 08-Aug-2005 jaredy

branches: 1.66.2;
sftp prompt enhancements:
- in non-interactive mode, do not print an empty prompt at the end
before finishing
- print newline after EOF in editline mode
- call el_end() in editline mode

ok dtucker djm


# 1.65 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.64 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


Revision tags: OPENBSD_3_7_BASE
# 1.63 10-Mar-2005 deraadt

branches: 1.63.2;
spacing


# 1.62 20-Feb-2005 djm

turn on ssh batch mode when in sftp batch mode, patch from jdmossh AT nand.net;
ok markus@


# 1.61 24-Jan-2005 dtucker

Have scp and sftp wait for the spawned ssh to exit before they exit
themselves. This prevents ssh from being unable to restore terminal
modes (not normally a problem on OpenBSD but common with -Portable
on POSIX platforms). From peak at argo.troja.mff.cuni.cz (bz#950);
ok djm@ markus@


# 1.60 10-Dec-2004 fgsch

- fix globbed ls for paths the same lenght as the globbed path when
we have a unique matching.
- fix globbed ls in case of a directory when we have a unique matching.
- as a side effect, if the path does not exist error (used to silently ignore).
- don't do extra do_lstat() if we only have one matching file.

djm@ ok


# 1.59 29-Nov-2004 djm

Some small fixes from moritz@jodeit.org. ok deraadt@


# 1.58 25-Nov-2004 markus

leak; from mpech


# 1.57 05-Nov-2004 djm

command editing and history support via libedit; ok markus@
thanks to hshoexer@ and many testers on tech@ too


Revision tags: OPENBSD_3_6_BASE
# 1.56 11-Jul-2004 deraadt

branches: 1.56.2;
spaces


# 1.55 25-Jun-2004 djm

bz #875: fix bad escape char error message; reported by f_mohr AT yahoo.de


# 1.54 22-Jun-2004 djm

don't show .files by default in ls, add -a option to turn them back on;
ok markus


# 1.53 21-Jun-2004 djm

prefix ls option flags with LS_


# 1.52 21-Jun-2004 djm

introduce sorting for ls, same options as /bin/ls; ok markus@


# 1.51 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.50 20-Jun-2004 djm

make "ls -l" listings print user/group names, add "ls -n" to show uid/gid
(like /bin/ls); idea & ok markus@


# 1.49 18-Jun-2004 dtucker

Use execvp instead of execv so sftp -S ssh works. "makes sense" markus@


# 1.48 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.47 26-May-2004 djm

exit -> _exit in forked child on error; from andrushock AT korovino.net


# 1.46 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


Revision tags: OPENBSD_3_5_BASE
# 1.45 03-Mar-2004 djm

branches: 1.45.2;
Fix initialisation of progress meter; ok markus@


# 1.44 17-Feb-2004 djm

sftp.c and sftp-int.c, together at last; ok markus@


# 1.43 17-Feb-2004 djm

Remove useless headers; ok deraadt@


# 1.42 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.41 27-Jan-2004 djm

reorder parsing so user:skey@host:file works (bugzilla #777)
patch from admorten AT umich.edu; ok markus@


# 1.40 21-Jan-2004 djm

initialise infile in main, rather than statically - from portable


# 1.39 13-Jan-2004 djm

Tidy sftp batchmode handling, eliminate junk to stderr (bugzilla #754) and
enable use of "-b -" to accept batchfile from stdin; ok markus@


# 1.38 08-Oct-2003 jmc

scp and sftp: add options list and sort options. options list requested
by deraadt@
sshd: use same format as ssh
ssh: remove wrong option from list
sftp-server: Subsystem is documented in ssh_config(5), not sshd(8)

ok deraadt@ markus@


Revision tags: OPENBSD_3_4_BASE
# 1.37 10-Jul-2003 markus

branches: 1.37.2;
sync usage with manpage, add missing -R


# 1.36 04-Jun-2003 djm

kill ssh process on receipt of signal; ok markus@


# 1.35 15-May-2003 mouring

Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm


Revision tags: OPENBSD_3_3_BASE
# 1.34 10-Jan-2003 fgsch

branches: 1.34.2;
sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.33 08-Jan-2003 djm

Cleanup error handling for batchmode
Allow blank lines and comments in input
Ability to suppress abort on error in batchmode ("-put blah")

Fixes mindrot bug #452; markus@ ok


# 1.32 27-Nov-2002 markus

allow usernames with embedded '@', e.g. scp user@vhost@realhost:file /tmp;
http://bugzilla.mindrot.org/show_bug.cgi?id=447; ok mouring@, millert@


Revision tags: OPENBSD_3_2_BASE
# 1.31 25-Jul-2002 mouring

branches: 1.31.2;
FallBackToRsh does not exist anywhere else. Remove it from here. OK deraadt.


# 1.30 23-Jun-2002 deraadt

bunch of u_int vs int stuff


Revision tags: OPENBSD_3_1_BASE
# 1.29 02-Apr-2002 markus

branches: 1.29.2;
always call log_init()


# 1.28 30-Mar-2002 markus

check waitpid for EINTR; based on patch from peter@ifm.liu.se


# 1.27 19-Mar-2002 markus

KNF whitespace


# 1.26 12-Feb-2002 djm

Perform multiple overlapping read/write requests in file transfer. Mostly
done by Tobias Ringstrom <tori@ringstrom.mine.nu>; ok markus@


# 1.25 06-Feb-2002 mpech

sync usage() with manual.

markus@ ok


# 1.24 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.23 04-Feb-2002 djm

Add "-P" option to directly connect to a local sftp-server. Should be useful
for regression testing; ok markus@


# 1.22 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.21 19-Sep-2001 stevesk

branches: 1.21.2;
add ClearAllForwardings ssh option and set it in scp and sftp; ok markus@


# 1.20 17-Sep-2001 stevesk

cleanup and document -1, -s and -S; ok markus@


# 1.19 17-Sep-2001 stevesk

add -Fssh_config option; ok markus@


# 1.18 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.17 08-May-2001 mouring

Use addargs() in sftp plus some clean up of addargs(). OK Markus


# 1.16 03-May-2001 mouring

Move colon() and cleanhost() to misc.c where I should I have put it in
the first place


Revision tags: OPENBSD_2_9_BASE
# 1.15 16-Apr-2001 mouring

branches: 1.15.2;
IPv6 support for sftp (which I bungled in my last patch) which is
borrowed from scp.c. Thanks to Markus@ for pointing it out.


# 1.14 12-Apr-2001 mouring

Add support for:
sftp [user@]host[:file [file]] - Fetch remote file(s)
sftp [user@]host[:dir[/]] - Start in remote dir/
OK deraadt@


# 1.13 08-Apr-2001 deraadt

do not modify an actual argv[] entry


# 1.12 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.11 07-Mar-2001 djm

branches: 1.11.2;
Support for new draft (draft-ietf-secsh-filexfer-01). New symlink handling
functions and small protocol change.


# 1.10 06-Mar-2001 deraadt

sftp -b batchfile; mouring@etoh.eviladmin.org


# 1.9 03-Mar-2001 markus

clean up arg processing. based on work by Christophe_Moret@hp.com


# 1.8 28-Feb-2001 deraadt

do not kill the subprocess on termination (we will see if this helps things
or hurts things)


# 1.7 08-Feb-2001 markus

branches: 1.7.2;
allow sftp over ssh protocol 1; ok djm@


# 1.6 07-Feb-2001 djm

Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov
<roumen.petrov@skalasoft.com>


# 1.5 06-Feb-2001 djm

Fix -o option under getopt; ok markus@


# 1.4 06-Feb-2001 djm

Wait for ssh subprocess at exit


# 1.3 06-Feb-2001 djm

Use getopt to process commandline arguments


# 1.2 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@