History log of /openbsd-current/usr.bin/ssh/sftp-client.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.176 17-May-2024 jsg

spelling; ok djm@


Revision tags: OPENBSD_7_5_BASE
# 1.175 13-Nov-2023 tobhe

Make sure sftp_get_limits() only returns 0 if 'limits' was initialized.
This fixes a potential uninitialized use of 'limits' in sftp_init() if
sftp_get_limits() returned early because of an unexpected message type.

ok djm@


Revision tags: OPENBSD_7_4_BASE
# 1.174 08-Sep-2023 djm

fix recursive remote-remote copies of directories that contain symlinks
to other directories (similar to bz3611)


# 1.173 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.172 08-Sep-2023 djm

fix scp in SFTP mode recursive upload and download of directories
that contain symlinks to other directories. In scp mode, the links
would be followed, but in SFTP mode they were not. bz3611, ok dtucker@


# 1.171 30-Apr-2023 djm

adjust ftruncate() logic to handle servers that reorder requests.

sftp/scp will ftruncate the destination file after a transfer completes,
to deal with the case where a longer destination file already existed.
We tracked the highest contiguous block transferred to deal with this
case, but our naive tracking doesn't deal with servers that reorder
requests - a misfeature strictly permitted by the protocol but seldom
implemented.

Adjust the logic to ftruncate() at the highest absolute block received
when the transfer is successful. feedback deraadt@ ok markus@

prompted by https://github.com/openssh/openssh-portable/commit/9b733#commitcomment-110679778


# 1.170 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.169 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.175 13-Nov-2023 tobhe

Make sure sftp_get_limits() only returns 0 if 'limits' was initialized.
This fixes a potential uninitialized use of 'limits' in sftp_init() if
sftp_get_limits() returned early because of an unexpected message type.

ok djm@


Revision tags: OPENBSD_7_4_BASE
# 1.174 08-Sep-2023 djm

fix recursive remote-remote copies of directories that contain symlinks
to other directories (similar to bz3611)


# 1.173 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.172 08-Sep-2023 djm

fix scp in SFTP mode recursive upload and download of directories
that contain symlinks to other directories. In scp mode, the links
would be followed, but in SFTP mode they were not. bz3611, ok dtucker@


# 1.171 30-Apr-2023 djm

adjust ftruncate() logic to handle servers that reorder requests.

sftp/scp will ftruncate the destination file after a transfer completes,
to deal with the case where a longer destination file already existed.
We tracked the highest contiguous block transferred to deal with this
case, but our naive tracking doesn't deal with servers that reorder
requests - a misfeature strictly permitted by the protocol but seldom
implemented.

Adjust the logic to ftruncate() at the highest absolute block received
when the transfer is successful. feedback deraadt@ ok markus@

prompted by https://github.com/openssh/openssh-portable/commit/9b733#commitcomment-110679778


# 1.170 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.169 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.174 08-Sep-2023 djm

fix recursive remote-remote copies of directories that contain symlinks
to other directories (similar to bz3611)


# 1.173 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.172 08-Sep-2023 djm

fix scp in SFTP mode recursive upload and download of directories
that contain symlinks to other directories. In scp mode, the links
would be followed, but in SFTP mode they were not. bz3611, ok dtucker@


# 1.171 30-Apr-2023 djm

adjust ftruncate() logic to handle servers that reorder requests.

sftp/scp will ftruncate the destination file after a transfer completes,
to deal with the case where a longer destination file already existed.
We tracked the highest contiguous block transferred to deal with this
case, but our naive tracking doesn't deal with servers that reorder
requests - a misfeature strictly permitted by the protocol but seldom
implemented.

Adjust the logic to ftruncate() at the highest absolute block received
when the transfer is successful. feedback deraadt@ ok markus@

prompted by https://github.com/openssh/openssh-portable/commit/9b733#commitcomment-110679778


# 1.170 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.169 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.171 30-Apr-2023 djm

adjust ftruncate() logic to handle servers that reorder requests.

sftp/scp will ftruncate the destination file after a transfer completes,
to deal with the case where a longer destination file already existed.
We tracked the highest contiguous block transferred to deal with this
case, but our naive tracking doesn't deal with servers that reorder
requests - a misfeature strictly permitted by the protocol but seldom
implemented.

Adjust the logic to ftruncate() at the highest absolute block received
when the transfer is successful. feedback deraadt@ ok markus@

prompted by https://github.com/openssh/openssh-portable/commit/9b733#commitcomment-110679778


# 1.170 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.169 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.170 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.169 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.169 08-Mar-2023 guenther

Delete obsolete /* ARGSUSED */ lint comments.

ok miod@ millert@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.168 11-Jan-2023 djm

clamp the minimum buffer lengths and number of inflight requests too


# 1.167 11-Jan-2023 djm

ignore bogus upload/download buffer lengths in the limits extension


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.166 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.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.165 19-Sep-2022 djm

sftp client library support for users-groups-by-id@openssh.com;
ok markus@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.164 15-May-2022 djm

fix in-place copies; r1.163 incorrectly skipped truncation in all cases,
not just at the start of a transfer. This could cause overwrites of larger
files to leave junk at the end. Spotted by tb@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.163 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@


Revision tags: OPENBSD_7_1_BASE
# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.162 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.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.161 17-Jan-2022 djm

make most of the sftp errors more idiomatic, following the general form
of "[local/remote] operation path: error message"; ok markus


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.160 08-Jan-2022 djm

use status error message to communicate ~user expansion failures;
provides better experience for scp in sftp mode, where ~user paths
are more likely to be used; spotted jsg, feedback jsg & deraadt
ok jsg & markus


# 1.159 08-Jan-2022 djm

more idiomatic error messages; spotted by jsg & deraadt
ok jsg & markus


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.158 01-Jan-2022 jsg

spelling
ok dtucker@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.157 06-Nov-2021 dtucker

Plug a couple of minor mem leaks. From beldmit at gmail.com via github
PR#283, ok markus@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.156 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_7_0_BASE
# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.155 03-Sep-2021 dtucker

Remove redundant attrib_clear in upload_dir_internal. The subsequent
call to stat_to_attrib clears the struct as its first step anyway.
From pmeinhardt via github PR#220, ok djm@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.154 09-Aug-2021 djm

SFTP protocol extension to allow the server to expand ~-prefixed
paths, in particular ~user ones. Allows scp in sftp mode to accept
these paths, like scp in rcp mode does.

prompted by and much discussion deraadt@
ok markus@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.153 09-Aug-2021 djm

show only the final path component in the progress meter;
more useful with long paths (that may truncate) and better
matches traditional scp behaviour; spotted by naddy@
ok deraadt@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.152 07-Aug-2021 djm

sftp-client.c needs poll.h

remove unused variable


# 1.151 07-Aug-2021 djm

do_upload() used a near-identical structure for tracking
expected status replies from the server to what do_download()
was using.

Refactor it to use the same structure and factor out some common
code into helper functions.


# 1.150 07-Aug-2021 djm

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


# 1.149 07-Aug-2021 djm

fix incorrect directory permissions on scp -3 transfers;
ok markus@


# 1.148 07-Aug-2021 djm

a bit more debugging of file attributes being sent/received
over the wire


# 1.147 07-Aug-2021 djm

make scp(1) in SFTP mode output better match original scp(1) by
suppressing "Retrieving [path]" lines that were emitted to support
the interactive sftp(1) client. ok markus@


# 1.146 07-Aug-2021 djm

factor out a structure duplicated between downloading and
crossloading; ok markus@


# 1.145 07-Aug-2021 djm

support for "cross"-loading files/directories, i.e. downloading
from one SFTP server while simultaneously uploading to another.

feedback & ok markus@


# 1.144 07-Aug-2021 djm

factor our SSH2_FXP_OPEN calls into their own function;
"looks fine" markus@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.143 06-Jun-2021 djm

degrade gracefully if a sftp-server offers the limits@openssh.com
extension but fails when the client tries to invoke it.
Reported by Hector Martin via bz3318


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

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.142 03-Apr-2021 djm

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


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.141 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.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.140 10-Mar-2021 djm

no need to reset buffer after send_msg() as that is done for us;
patch from Mike Frysinger


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.139 04-Dec-2020 djm

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


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.138 20-Nov-2020 dtucker

When doing an sftp recursive upload or download of a read-only directory,
ensure that the directory is created with write and execute permissions
in the interim so that we can actually complete the transfer, then set
the directory permission as the final step. (The execute bit is only
likely to be an issue with a non-POSIX server). bz#3222, ok djm@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.137 18-Oct-2020 djm

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


Revision tags: OPENBSD_6_8_BASE
# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

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


# 1.67 11-Jul-2006 stevesk

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


# 1.66 09-Jul-2006 stevesk

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


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


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

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


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

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


# 1.55 17-Jun-2005 djm

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


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


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

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


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

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

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


# 1.48 30-Mar-2004 djm

sync comment with reality


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

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

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


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

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


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

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


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

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


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

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


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.136 15-May-2020 djm

fix off-by-one error that caused sftp downloads to make one more
concurrent request that desired. This prevented using sftp(1) in
unpipelined request/response mode, which is useful when debugging.
Patch from Stephen Goetze in bz#3054


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

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


# 1.116 14-Jan-2015 djm

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


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


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

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


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

branches: 1.74.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.73 01-Aug-2006 stevesk

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


# 1.72 26-Jul-2006 stevesk

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


# 1.71 25-Jul-2006 stevesk

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


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

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


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.135 04-Oct-2019 djm

fix memory leak in error path; bz#3074
patch from krishnaiah.bommu@intel.com, ok dtucker


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.134 12-Jul-2019 djm

print explicit "not modified" message if a file was requested for
resumed download but was considered already complete.

bz#2978 ok dtucker


Revision tags: OPENBSD_6_5_BASE
# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.133 24-Jan-2019 dtucker

Have progressmeter force an update at the beginning and end of each
transfer. Fixes the problem recently introduces where very quick
transfers do not display the progressmeter at all. Spotted by naddy@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.132 23-Jan-2019 dtucker

Sanitize scp filenames via snmprintf. To do this we move the
progressmeter formatting outside of signal handler context and have
the atomicio callback called for EINTR too. bz#2434 with contributions
from djm and jjelen at redhat.com, ok djm@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.131 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@


Revision tags: OPENBSD_6_4_BASE
# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.130 31-Jul-2018 djm

fix some memory leaks spotted by Coverity via Jakub Jelen in bz#2366
feedback and ok dtucker@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.129 25-May-2018 djm

Fix return value confusion in several functions (readdir, download and
fsync). These should return -1 on error, not a sftp status code.

patch from Petr Cerny in bz#2871


Revision tags: OPENBSD_6_3_BASE
# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@


# 1.128 28-Nov-2017 dtucker

Have sftp print a warning about shell cleanliness when decoding the first
packet fails, which is usually caused by shells polluting stdout of
non-interactive starups. bz#2800, ok markus@ deraadt@.


Revision tags: OPENBSD_6_2_BASE
# 1.127 11-Aug-2017 djm

don't print verbose error message when ssh disconnects under sftp;
bz#2750; ok dtucker@


Revision tags: OPENBSD_6_1_BASE
# 1.126 03-Jan-2017 djm

check number of entries in SSH2_FXP_NAME response; avoids
unreachable overflow later. Reported by Jann Horn


# 1.125 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.124 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.123 02-May-2016 djm

fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of
strdup(sshbuf_ptr()) with better safety checking;
feedback and ok markus@


# 1.122 08-Apr-2016 djm

whitespace at EOL


Revision tags: OPENBSD_5_9_BASE
# 1.121 11-Feb-2016 djm

fix regression in openssh-6.8 sftp client: existing destination
directories would incorrectly terminate recursive uploads;
bz#2528


Revision tags: OPENBSD_5_8_BASE
# 1.120 28-May-2015 djm

add error message on ftruncate failure; bz#2176


# 1.119 23-May-2015 jsg

fix a memory leak in an error path
ok markus@ dtucker@


# 1.118 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.117 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.116 14-Jan-2015 djm

update sftp client and server to new buffer API.
pretty much just mechanical changes; with & ok markus


Revision tags: OPENBSD_5_6_BASE
# 1.115 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.114 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.113 17-Jan-2014 djm

signed/unsigned comparison warning fix; from portable


# 1.112 16-Jan-2014 djm

needless and incorrect cast to size_t can break resumption of
large download; patch from tobias@


# 1.111 05-Dec-2013 djm

fix memory leak in error path in do_readdir(); pointed out by
Loganaden Velvindron @ AfriNIC in bz#2163


# 1.110 04-Dec-2013 djm

bz#2171: don't leak local_fd on error; from Loganaden Velvindron @
AfriNIC


# 1.109 08-Nov-2013 dtucker

Include stdlib.h for free() as per the man page.


# 1.108 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.107 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.106 11-Oct-2013 djm

missed one arg reorder


# 1.105 11-Oct-2013 djm

rename flag arguments to be more clear and consistent.
reorder some internal function arguments to make adding additional flags
easier.

no functional change


# 1.104 19-Sep-2013 djm

fix swapped pflag and printflag in sftp upload_dir; from Iain Morgan


# 1.103 09-Aug-2013 djm

two problems found by a to-be-committed regress test: 1) msg_id was not
being initialised so was starting at a random value from the heap
(harmless, but confusing). 2) some error conditions were not being
propagated back to the caller


# 1.102 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.


Revision tags: OPENBSD_5_4_BASE
# 1.101 25-Jul-2013 djm

branches: 1.101.2;
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.100 01-Jun-2013 dtucker

Update progressmeter when data is acked, not when it's sent. bz#2108, from
Debian via Colin Watson, ok djm@


# 1.99 01-Jun-2013 dtucker

Replace S_IWRITE, which isn't standardized, with S_IWUSR, which is. Patch
from Nathan Osman via bz#2085. ok deraadt.


# 1.98 17-May-2013 djm

bye, bye xfree(); ok markus@


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.97 02-Jul-2012 dtucker

branches: 1.97.4;
fix a couple of "assigned but not used" warnings. ok markus@


Revision tags: OPENBSD_5_1_BASE
# 1.96 12-Sep-2011 markus

fix leak in do_lsreaddir(); ok djm


# 1.95 11-Sep-2011 markus

fix leaks in do_hardlink() and do_readlink(); bz#1921
from Loganaden Velvindron


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.94 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.93 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.92 19-Jul-2010 djm

bz#1797: fix swapped args in upload_dir_internal(), breaking recursive
upload depth checks and causing verbose printing of transfers to always
be turned on; patch from imorgan AT nas.nasa.gov


# 1.91 18-Jun-2010 djm

fix memory leak in do_realpath() error path; bz#1771, patch from
anicka AT suse.cz


Revision tags: OPENBSD_4_7_BASE
# 1.90 11-Oct-2009 dtucker

d_type isn't portable so use lstat to get dirent modes. Suggested by and
"looks sane" deraadt@


# 1.89 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.88 14-Aug-2009 djm

make the "get_handle: ..." error messages vaguely useful by allowing
callers to specify their own error message strings.


Revision tags: OPENBSD_4_6_BASE
# 1.87 22-Jun-2009 dtucker

alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm


Revision tags: OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.86 26-Jun-2008 djm

allow the sftp chmod(2)-equivalent operation to set set[ug]id/sticky
bits. Note that this only affects explicit setting of modes (e.g. via
sftp(1)'s chmod command) and not file transfers. (bz#1310)
ok deraadt@ at c2k8


# 1.85 12-Jun-2008 djm

print extension revisions for extensions that we understand


# 1.84 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.83 07-Jun-2008 djm

statvfs member fsid needs to be wider, increase it to 64 bits and
crank extension revision number to 2; prodded and ok dtucker@


# 1.82 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@


# 1.81 23-Mar-2008 djm

prefer POSIX-style file renaming over filexfer rename behaviour if the server
supports the posix-rename@openssh.com extension.

Note that the old (filexfer) behaviour would refuse to clobber an existing
file. Users who depended on this should adjust their sftp(1) usage.

ok deraadt@ markus@


Revision tags: OPENBSD_4_3_BASE
# 1.80 21-Jan-2008 djm

when a remote write error occurs during an upload, ensure that ACKs for all
issued requests are properly drained. patch from t8m AT centrum.cz


# 1.79 19-Jan-2008 djm

fix remote handle leak in do_download() local file open error path;
report and fix from sworley AT chkno.net


# 1.78 11-Jan-2008 chl

disable unused functions

initially from tobias@, but disabled them by placing them in "#ifdef notyet" which was asked by djm@

ok djm@ tobias@


# 1.77 16-Sep-2007 djm

use off_t instead of u_int64_t for file offsets, matching what the
progressmeter code expects; bz #842


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.76 22-Jan-2007 djm

return error from do_upload() when a write fails. fixes bz#1252: zero
exit status from sftp when uploading to a full device. report from
jirkat AT atlas.cz; ok dtucker@


# 1.75 22-Oct-2006 djm

cancel progress meter when upload write fails; ok deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.74 03-Aug-2006 deraadt

branches: 1.74.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.73 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.72 26-Jul-2006 stevesk

move #include <sys/param.h> out of includes.h


# 1.71 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.70 23-Jul-2006 stevesk

#include <signal.h> for sig_atomic_t; need this prior to <sys/param.h>
move


# 1.69 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.68 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.67 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.66 09-Jul-2006 stevesk

move #include <fcntl.h> out of includes.h


# 1.65 16-Apr-2006 djm

avoid making a tiny 4-byte write to send the packet length of sftp
commands, which would result in a separate tiny packet on the wire by
using atomiciov(writev, ...) to write the length and the command in one
pass; ok deraadt@


# 1.64 30-Mar-2006 djm

replace {GET,PUT}_XXBIT macros with functionally similar functions,
silencing a heap of lint warnings. also allows them to use
__bounded__ checking which can't be applied to macros; requested
by and feedback from deraadt@


# 1.63 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.62 25-Mar-2006 djm

change OpenSSH's xrealloc() function from being xrealloc(p, new_size) to
xrealloc(p, new_nmemb, new_itemsize).

realloc is particularly prone to integer overflows because it is almost
always allocating "n * size" bytes, so this is a far safer API;
ok deraadt@


# 1.61 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.60 20-Feb-2006 stevesk

branches: 1.60.2;
move #include <sys/stat.h> out of includes.h; ok markus@


# 1.59 15-Feb-2006 david

typo in comment; ok djm@


# 1.58 02-Jan-2006 djm

use a common max. packet length, no binary change


Revision tags: OPENBSD_3_8_BASE
# 1.57 27-Jul-2005 dtucker

branches: 1.57.2;
Silence bogus -Wuninitialized warnings; ok djm@


# 1.56 17-Jul-2005 djm

knf says that a 2nd level indent is four (not three or five) spaces


# 1.55 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.54 24-May-2005 avsm

Switch atomicio to use a simpler interface; it now returns a size_t
(containing number of bytes read/written), and indicates error by
returning 0. EOF is signalled by errno==EPIPE.
Typical use now becomes:

if (atomicio(read, ..., len) != len)
err(1,"read");

ok deraadt@, cloder@, djm@


Revision tags: OPENBSD_3_7_BASE
# 1.53 10-Mar-2005 deraadt

branches: 1.53.2;
spacing


# 1.52 25-Nov-2004 markus

leak; from mpech


Revision tags: OPENBSD_3_6_BASE
# 1.51 11-Jul-2004 deraadt

branches: 1.51.2;
spaces


# 1.50 03-Jun-2004 pedro

initialize pointers, ok markus@


# 1.49 19-May-2004 djm

gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@


# 1.48 30-Mar-2004 djm

sync comment with reality


Revision tags: OPENBSD_3_5_BASE
# 1.47 03-Mar-2004 djm

branches: 1.47.2;
Don't print duplicate messages when progressmeter is off
Spotted by job317 AT mailvault.com; ok markus@


# 1.46 17-Feb-2004 djm

switch to license.template for code written by me (belated, I know...)


# 1.45 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


Revision tags: OPENBSD_3_4_BASE
# 1.44 28-Jun-2003 deraadt

branches: 1.44.2;
deal with typing of write vs read in atomicio


# 1.43 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.42 05-Mar-2003 markus

branches: 1.42.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.41 14-Jan-2003 djm

Don't try to upload or download non-regular files. Report from
apoloval@pantuflo.escet.urjc.es; ok markus@


# 1.40 10-Jan-2003 djm

Simplify and avoid redundancy in packet send and receive function; ok fgs@


# 1.39 10-Jan-2003 fgsch

sftp progress meter support.
original diffs by Nils Nordman <nino at nforced dot com> via markus@, merged
to -current by me, djm@ ok.


# 1.38 06-Jan-2003 djm

Fix "get -p" download to not add user-write perm. mindrot bug #426 reported
by gfernandez@livevault.com; ok markus@


# 1.37 21-Nov-2002 deraadt

KNF


# 1.36 05-Nov-2002 markus

typo; GaryF@livevault.com


Revision tags: OPENBSD_3_2_BASE
# 1.35 11-Sep-2002 djm

branches: 1.35.2;
support for short/long listings and globbing in "ls"; ok markus@


# 1.34 27-Jun-2002 deraadt

use xfree()


# 1.33 23-Jun-2002 deraadt

bunch of u_int vs int stuff


# 1.32 09-Jun-2002 markus

use tab not spaces (|unexpand)


Revision tags: OPENBSD_3_1_BASE
# 1.31 06-Apr-2002 djm

branches: 1.31.2;
Fix occasional corruption on upload due to bad reuse of request id, spotted
by chombier@mac.com; ok markus@


# 1.30 01-Apr-2002 markus

fallback to stat if server does not support lstat


# 1.29 01-Apr-2002 markus

20480 is an upper limit for older server


# 1.28 19-Mar-2002 markus

KNF whitespace


# 1.27 11-Mar-2002 itojun

indent


# 1.26 11-Mar-2002 itojun

correct type mismatches (u_int64_t != unsigned long long)


# 1.25 08-Mar-2002 itojun

printf type mismatch


# 1.24 24-Feb-2002 markus

early close(), missing free; ok stevesk@


# 1.23 13-Feb-2002 djm

API cleanup and backwards compat for filexfer v.0 servers; ok markus@


# 1.22 12-Feb-2002 djm

Let overlapped upload path handle servers which reorder ACKs. This may be
permitted by the protocol spec; ok markus@


# 1.21 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.20 05-Feb-2002 djm

Add "-B" option to specify copy buffer length (default 32k); ok markus@


# 1.19 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


Revision tags: OPENBSD_3_0_BASE
# 1.18 14-Jul-2001 stevesk

branches: 1.18.2;
delete spurious #includes; ok deraadt@ markus@


# 1.17 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


Revision tags: OPENBSD_2_9_BASE
# 1.16 05-Apr-2001 markus

branches: 1.16.2;
fix whitespace: unexpand + trailing spaces.


# 1.15 19-Mar-2001 djm

Report ssh connection closing correctly; ok deraadt@


# 1.14 16-Mar-2001 djm

Revise globbing for get/put to be more shell-like. In particular,
"get/put file* directory/" now works. ok markus@


# 1.13 14-Mar-2001 markus

Wall


# 1.12 13-Mar-2001 djm

sftp client filename globbing for get, put, ch{mod,grp,own}. ok markus@


# 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 14-Feb-2001 djm

branches: 1.10.2;
Fix and document 'preserve modes & times' option ('-p' flag in sftp);
ok markus@


# 1.9 10-Feb-2001 djm

typo; ok Niels


# 1.8 08-Feb-2001 stevesk

%i -> %d


# 1.7 07-Feb-2001 markus

older servers reply with SSH2_FXP_NAME + count==0 instead of EOF


# 1.6 07-Feb-2001 itojun

unsigned long long -> %llu, not %qu. markus ok


# 1.5 07-Feb-2001 djm

Don't free handles before we are done with them. Based on work from
Corinna Vinschen <vinschen@redhat.com>. ok markus@


# 1.4 06-Feb-2001 djm

replace arc4random with counter for request ids; ok markus@


# 1.3 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.2 04-Feb-2001 deraadt

make gcc on the alpha even happier


# 1.1 04-Feb-2001 djm

Basic interactive sftp client; ok theo@