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

Start the process of splitting sshd into separate binaries. This step
splits sshd into a listener and a session binary. More splits are
planned.

After this changes, the listener binary will validate the configuration,
load the hostkeys, listen on port 22 and manage MaxStartups only. All
session handling will be performed by a new sshd-session binary that the
listener fork+execs.

This reduces the listener process to the minimum necessary and sets us
up for future work on the sshd-session binary.

feedback/ok markus@ deraadt@

NB. if you're updating via source, please restart sshd after installing,
otherwise you run the risk of locking yourself out.


Revision tags: OPENBSD_7_5_BASE
# 1.437 06-Mar-2024 djm

fix memory leak in mux proxy mode when requesting forwarding.

found by RASU JSC, reported by Maks Mishin in GHPR#467


# 1.436 09-Jan-2024 djm

add a "global" ChannelTimeout type to ssh(1) and sshd(8) that watches
all open channels and will close all open channels if there is no
traffic on any of them for the specified interval. This is in addition
to the existing per-channel timeouts added a few releases ago.

This supports use-cases like having a session + x11 forwarding channel
open where one may be idle for an extended period but the other is
actively used. The global timeout would allow closing both channels when
both have been idle for too long.

ok dtucker@


# 1.435 18-Dec-2023 djm

stricter handling of channel window limits

This makes ssh/sshd more strict in handling non-compliant peers that
send more data than the advertised channel window allows. Previously
the additional data would be silently discarded. This change will
cause ssh/sshd to terminate the connection if the channel window is
exceeded by more than a small grace allowance.

ok markus@


# 1.434 15-Nov-2023 djm

when deciding whether to enable keystroke timing obfuscation,
only consider enabling it when a channel with a tty is open.

Avoids turning on the obfucation when X11 forwarding only is in use,
which slows it right down. Reported by Roger Marsh


Revision tags: OPENBSD_7_4_BASE
# 1.433 04-Sep-2023 djm

make channel_output_poll() return a flag indicating whether channel
data was enqueued. Will be used to improve keystroke timing
obfuscation. Problem spotted by / tested by naddy@


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


Revision tags: OPENBSD_7_5_BASE
# 1.437 06-Mar-2024 djm

fix memory leak in mux proxy mode when requesting forwarding.

found by RASU JSC, reported by Maks Mishin in GHPR#467


# 1.436 09-Jan-2024 djm

add a "global" ChannelTimeout type to ssh(1) and sshd(8) that watches
all open channels and will close all open channels if there is no
traffic on any of them for the specified interval. This is in addition
to the existing per-channel timeouts added a few releases ago.

This supports use-cases like having a session + x11 forwarding channel
open where one may be idle for an extended period but the other is
actively used. The global timeout would allow closing both channels when
both have been idle for too long.

ok dtucker@


# 1.435 18-Dec-2023 djm

stricter handling of channel window limits

This makes ssh/sshd more strict in handling non-compliant peers that
send more data than the advertised channel window allows. Previously
the additional data would be silently discarded. This change will
cause ssh/sshd to terminate the connection if the channel window is
exceeded by more than a small grace allowance.

ok markus@


# 1.434 15-Nov-2023 djm

when deciding whether to enable keystroke timing obfuscation,
only consider enabling it when a channel with a tty is open.

Avoids turning on the obfucation when X11 forwarding only is in use,
which slows it right down. Reported by Roger Marsh


Revision tags: OPENBSD_7_4_BASE
# 1.433 04-Sep-2023 djm

make channel_output_poll() return a flag indicating whether channel
data was enqueued. Will be used to improve keystroke timing
obfuscation. Problem spotted by / tested by naddy@


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.436 09-Jan-2024 djm

add a "global" ChannelTimeout type to ssh(1) and sshd(8) that watches
all open channels and will close all open channels if there is no
traffic on any of them for the specified interval. This is in addition
to the existing per-channel timeouts added a few releases ago.

This supports use-cases like having a session + x11 forwarding channel
open where one may be idle for an extended period but the other is
actively used. The global timeout would allow closing both channels when
both have been idle for too long.

ok dtucker@


# 1.435 18-Dec-2023 djm

stricter handling of channel window limits

This makes ssh/sshd more strict in handling non-compliant peers that
send more data than the advertised channel window allows. Previously
the additional data would be silently discarded. This change will
cause ssh/sshd to terminate the connection if the channel window is
exceeded by more than a small grace allowance.

ok markus@


# 1.434 15-Nov-2023 djm

when deciding whether to enable keystroke timing obfuscation,
only consider enabling it when a channel with a tty is open.

Avoids turning on the obfucation when X11 forwarding only is in use,
which slows it right down. Reported by Roger Marsh


Revision tags: OPENBSD_7_4_BASE
# 1.433 04-Sep-2023 djm

make channel_output_poll() return a flag indicating whether channel
data was enqueued. Will be used to improve keystroke timing
obfuscation. Problem spotted by / tested by naddy@


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.435 18-Dec-2023 djm

stricter handling of channel window limits

This makes ssh/sshd more strict in handling non-compliant peers that
send more data than the advertised channel window allows. Previously
the additional data would be silently discarded. This change will
cause ssh/sshd to terminate the connection if the channel window is
exceeded by more than a small grace allowance.

ok markus@


# 1.434 15-Nov-2023 djm

when deciding whether to enable keystroke timing obfuscation,
only consider enabling it when a channel with a tty is open.

Avoids turning on the obfucation when X11 forwarding only is in use,
which slows it right down. Reported by Roger Marsh


Revision tags: OPENBSD_7_4_BASE
# 1.433 04-Sep-2023 djm

make channel_output_poll() return a flag indicating whether channel
data was enqueued. Will be used to improve keystroke timing
obfuscation. Problem spotted by / tested by naddy@


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.434 15-Nov-2023 djm

when deciding whether to enable keystroke timing obfuscation,
only consider enabling it when a channel with a tty is open.

Avoids turning on the obfucation when X11 forwarding only is in use,
which slows it right down. Reported by Roger Marsh


Revision tags: OPENBSD_7_4_BASE
# 1.433 04-Sep-2023 djm

make channel_output_poll() return a flag indicating whether channel
data was enqueued. Will be used to improve keystroke timing
obfuscation. Problem spotted by / tested by naddy@


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.433 04-Sep-2023 djm

make channel_output_poll() return a flag indicating whether channel
data was enqueued. Will be used to improve keystroke timing
obfuscation. Problem spotted by / tested by naddy@


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.432 04-Jul-2023 dlg

add support for unix domain sockets to ssh -W

ok djm@ dtucker@


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.431 05-Jun-2023 millert

Store timeouts as int, not u_int as they are limited to INT_MAX.
Fixes sign compare warnings systems with 32-bit time_t due to type
promotion. OK djm@


Revision tags: OPENBSD_7_3_BASE
# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.430 10-Mar-2023 dtucker

Expliticly ignore return code from fcntl(.. FD_CLOEXEC) since there's
not much we can do anyway. From Coverity CID 291857, ok djm@


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.429 07-Mar-2023 djm

refactor to be more readable top to bottom. Prompted by Coverity CID
405048 which was a false-positive fd leak; ok dtucker@


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.428 04-Mar-2023 dtucker

Use time_t instead of u_int for remaining x11 timeout checks for 64bit
time_t safety. From Coverity CIDs 405197 and 405028, ok djm@


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.427 18-Jan-2023 djm

when restoring non-blocking mode to stdio fds, restore exactly
the flags that ssh started with and don't just clobber them with
zero, as this could also remove the append flag from the set;

bz3523; ok dtucker@


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.426 06-Jan-2023 djm

Implement channel inactivity timeouts

This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.

Note: this only affects channels over an opened SSH connection and not
the connection itself. Most clients close the connection when their channels
go away, with a notable exception being ssh(1) in multiplexing mode.

ok markus dtucker


# 1.425 06-Jan-2023 djm

Add channel_set_xtype()

This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker


# 1.424 06-Jan-2023 djm

tweak channel ctype names

These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.

ok dtucker markus


# 1.423 06-Jan-2023 djm

Add channel_force_close()

This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.

ok markus dtucker


# 1.422 06-Jan-2023 djm

replace manual poll/ppoll timeout math with ptimeout API

feedback markus / ok markus dtucker


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.421 18-Nov-2022 mbuhl

In channel_request_remote_forwarding the parameters for permission_set_add
are leaked as they are also duplicated in the call.
Found by CodeChecker.
ok djm


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

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.420 19-Sep-2022 djm

better debugging for connect_next()


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.419 05-May-2022 djm

channel_new no longer frees remote_name. So update the comment
accordingly. As remote_name is not modified, it can be const
as well. From Martin Vahlensieck


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.418 04-May-2022 markus

make sure stdout is non-blocking; ok djm@


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.417 20-Apr-2022 djm

Try to continue running local I/O for channels in state OPEN during
SSH transport rekeying. The most visible benefit is that it should make
~-escapes work in the client (e.g. to exit) if the connection happened
to have stalled during a rekey event. Based work by and ok dtucker@


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


Revision tags: OPENBSD_7_1_BASE
# 1.416 11-Apr-2022 djm

clear io_want/io_ready flags at start of poll() cycle;
avoids plausible spin during rekeying if channel io_want flags are
reused across cycles. ok markus@ deraadt@


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.415 30-Mar-2022 djm

fix poll() spin when a channel's output fd closes without data in the
channel buffer. Introduce more exact packing of channel fds into the
pollfd array. fixes bz3405 and bz3411; ok deraadt@ markus@


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.414 15-Mar-2022 djm

improve DEBUG_CHANNEL_POLL debugging message


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.413 17-Feb-2022 djm

check for EINTR/EAGAIN failures in the rfd fast-path;
caught by dtucker's minix3 vm :) ok dtucker@


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.412 22-Jan-2022 djm

Use sshbuf_read() to read directly into the channel input buffer
rather than into a stack buffer that needs to be copied again;
Improves performance by about 1% on cipher-speed.sh
feedback dtucker@ ok markus@


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.411 06-Jan-2022 djm

convert ssh, sshd mainloops from select() to poll();
feedback & ok deraadt@ and markus@
has been in snaps for a few months


# 1.410 06-Jan-2022 djm

prepare for conversion of ssh, sshd mainloop from select() to poll()
by moving FD_SET construction out of channel handlers into separate
functions. ok markus


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.409 01-Jan-2022 jsg

spelling
ok dtucker@


Revision tags: OPENBSD_7_0_BASE
# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.408 14-Sep-2021 mbuhl

put back the mux_ctx memleak fix for SSH_CHANNEL_MUX_CLIENT
OK mfriedl@


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.407 19-May-2021 djm

restore blocking status on stdio fds before close

ssh(1) needs to set file descriptors to non-blocking mode to operate
but it was not restoring the original state on exit. This could cause
problems with fds shared with other programs via the shell, e.g.

> $ cat > test.sh << _EOF
> #!/bin/sh
> {
> ssh -Fnone -oLogLevel=verbose ::1 hostname
> cat /usr/share/dict/words
> } | sleep 10
> _EOF
> $ ./test.sh
> Authenticated to ::1 ([::1]:22).
> Transferred: sent 2352, received 2928 bytes, in 0.1 seconds
> Bytes per second: sent 44338.9, received 55197.4
> cat: stdout: Resource temporarily unavailable

This restores the blocking status for fds 0,1,2 (stdio) before ssh(1)
abandons/closes them.

This was reported as bz3280 and GHPR246; ok dtucker@


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

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.406 03-Apr-2021 djm

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


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.405 15-Feb-2021 markus

ssh: add PermitRemoteOpen for remote dynamic forwarding with SOCKS
ok djm@, dtucker@


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.404 27-Jan-2021 djm

remove global variable used to stash compat flags and use the
purpose-built ssh->compat variable instead; feedback/ok markus@


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

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


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


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

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

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


# 1.233 07-Feb-2006 stevesk

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


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

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

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


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

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


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

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


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

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


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

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


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

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


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

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


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

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


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.403 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.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

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


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

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


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

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


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

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


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

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


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

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


# 1.262 26-Jul-2006 stevesk

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


# 1.261 25-Jul-2006 stevesk

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


# 1.260 22-Jul-2006 stevesk

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


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

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


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.402 20-Sep-2020 djm

cap channel input buffer size at 16MB; avoids high memory use when
peer advertises a large window but is slow to consume the data we
send (e.g. because of a slow network)

reported by Pierre-Yves David

fix with & ok markus@


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.401 03-Jul-2020 djm

put back the mux_ctx memleak fix, but only for channels of type
SSH_CHANNEL_MUX_LISTENER; Specifically SSH_CHANNEL_MUX_PROXY channels
should not have this structure freed.


# 1.400 03-Jul-2020 djm

revert r1.399 - the lifetime of c->mux_ctx is more complex; simply freeing
it here causes other problems


# 1.399 03-Jul-2020 djm

fix memory leak of mux_ctx; patch from Sergiy Lozovsky via bz3189
ok dtucker


Revision tags: OPENBSD_6_7_BASE
# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.398 25-Apr-2020 dtucker

We've standardized on memset over bzero, replace a couple that had slipped
in. ok deraadt markus djm.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.397 06-Mar-2020 markus

fix uninitialized pointers for forward_cancel; ok djm


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.396 26-Feb-2020 jsg

change explicit_bzero();free() to freezero()

While freezero() returns early if the pointer is NULL the tests for
NULL in callers are left to avoid warnings about passing an
uninitialised size argument across a function boundry.

ok deraadt@ djm@


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.395 25-Jan-2020 djm

the GatewayPorts vs -R listen address selection logic is still
confusing people, so add another comment explaining the special
handling of "localhost"; bz#3258


Revision tags: OPENBSD_6_6_BASE
# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.394 07-Jul-2019 dtucker

Remove some set but never used variables. ok daraadt@


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.393 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.392 07-Jun-2019 dtucker

Typo and spelling fixes in comments and error messages. Patch from
knweiss at gmail.com via -portable.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.391 10-May-2019 florian

For PermitOpen violations add the remote host and port to
be able to find out from where the request was comming.

Add the same logging for PermitListen violations which where not
logged at all.

Pointed out by Robert Kisteleki (robert AT ripe.net)

input markus
OK deraadt


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.390 03-May-2019 dtucker

Free channel objects on exit path. Patch from markus at blueflash.cc,
ok deraadt


Revision tags: OPENBSD_6_5_BASE
# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.389 19-Jan-2019 djm

convert channels.c to new packet API

with & ok markus@


# 1.388 19-Jan-2019 djm

begin landing remaining refactoring of packet parsing API, started
almost exactly six years ago.

This change stops including the old packet_* API by default and makes
each file that requires the old API include it explicitly. We will
commit file-by-file refactoring to remove the old API in consistent
steps.

with & ok markus@


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.387 07-Dec-2018 djm

no need to allocate channels_pre/channels_post in channel_init_channels()
as we do it anyway in channel_handler_init() that we call at the end of
the function. Fix from Markus Schmidt via bz#2938


Revision tags: OPENBSD_6_4_BASE
# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.386 04-Oct-2018 djm

factor out channel status formatting from channel_open_message() so
we can use it in other debug messages


# 1.385 04-Oct-2018 djm

include a little more information about the status and disposition of
channel's extended (stderr) fd; makes debugging some things a bit easier.
No behaviour change.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.384 27-Jul-2018 markus

avoid expensive channel_open_message() calls; ok djm@


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.383 11-Jul-2018 markus

remove legacy key emulation layer; ok djm@


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.382 25-Jun-2018 djm

fix NULL dereference in open_listen_match_tcpip()


# 1.381 06-Jun-2018 djm

Add a PermitListen directive to control which server-side addresses
may be listened on when the client requests remote forwarding (ssh -R).

This is the converse of the existing PermitOpen directive and this
includes some refactoring to share much of its implementation.

feedback and ok markus@


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.380 10-Apr-2018 djm

lots of typos in comments/docs. Patch from Karsten Weiss after checking
with codespell tool (https://github.com/lucasdemarchi/codespell)


Revision tags: OPENBSD_6_3_BASE
# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.379 05-Feb-2018 tb

The file descriptors for socket, stdin, stdout and stderr aren't
necessarily distinct, so check if they are the same to avoid closing
the same fd several times.

ok djm


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.378 23-Jan-2018 djm

Drop compatibility hacks for some ancient SSH implementations, including
ssh.com <=2.* and OpenSSH <= 3.*.

These versions were all released in or before 2001 and predate the
final SSH RFCs. The hacks in question aren't necessary for RFC-
compliant SSH implementations.

ok markus@


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.


# 1.377 05-Dec-2017 djm

include the addr:port in bind/listen failure messages


# 1.376 25-Oct-2017 djm

Add optional rdomain qualifier to sshd_config's ListenAddress option
to allow listening on a different rdomain(4), e.g.

ListenAddress 0.0.0.0 rdomain 4


Revision tags: OPENBSD_6_2_BASE
# 1.375 24-Sep-2017 djm

fix inverted test on channel open failure path that "upgraded" a
transient failure into a fatal error; reported by sthen and also seen
by benno@; ok sthen@


# 1.374 24-Sep-2017 djm

write the correct buffer when tunnel forwarding; doesn't matter
on OpenBSD (they are the same) but does matter on portable where
we use an output filter to translate os-specific tun/tap headers


# 1.373 23-Sep-2017 djm

fix tunnel forwarding problem introduced in refactor; reported by
stsp@ ok markus@


# 1.372 21-Sep-2017 markus

Add 'reverse' dynamic forwarding which combines dynamic forwarding
(-D) with remote forwarding (-R) where the remote-forwarded port
expects SOCKS-requests.

The SSH server code is unchanged and the parsing happens at the SSH
clients side. Thus the full SOCKS-request is sent over the forwarded
channel and the client parses c->output. Parsing happens in
channel_before_prepare_select(), _before_ the select bitmask is
computed in the pre[] handlers, but after network input processing
in the post[] handlers.

help and ok djm@


# 1.371 19-Sep-2017 millert

Use explicit_bzero() instead of bzero() before free() to prevent
the compiler from optimizing away the bzero() call. OK djm@


# 1.370 12-Sep-2017 djm

unused variable


# 1.369 12-Sep-2017 djm

fix tun/tap forwarding case in previous


# 1.368 12-Sep-2017 djm

Make remote channel ID a u_int

Previously we tracked the remote channel IDs in an int, but this is
strictly incorrect: the wire protocol uses uint32 and there is nothing
in-principle stopping a SSH implementation from sending, say, 0xffff0000.

In practice everyone numbers their channels sequentially, so this has
never been a problem.

ok markus@


# 1.367 12-Sep-2017 djm

refactor channels.c

Move static state to a "struct ssh_channels" that is allocated at
runtime and tracked as a member of struct ssh.

Explicitly pass "struct ssh" to all channels functions.

Replace use of the legacy packet APIs in channels.c.

Rework sshd_config PermitOpen handling: previously the configuration
parser would call directly into the channels layer. After the refactor
this is not possible, as the channels structures are allocated at
connection time and aren't available when the configuration is parsed.
The server config parser now tracks PermitOpen itself and explicitly
configures the channels code later.

ok markus@


# 1.366 30-Aug-2017 djm

pass packet state down to some of the channels function (more
to come...); ok markus@


# 1.365 31-May-2017 deraadt

These shutdown() SHUT_RDWR are not needed before close()
ok djm markus claudio


# 1.364 31-May-2017 djm

fix possible OOB strlen() in SOCKS4A hostname parsing; ok markus@


# 1.363 30-May-2017 markus

protocol handlers all get struct ssh passed; ok djm@


# 1.362 30-May-2017 markus

remove ssh1 references; ok djm@


# 1.361 26-May-2017 markus

remove SSH_CHANNEL_XXX_DRAINING (ssh1 only); ok djm@


# 1.360 26-May-2017 markus

remove channel_input_close_confirmation (ssh1 only); ok djm@


# 1.359 30-Apr-2017 djm

obliterate ssh1.h and some dead code that used it

ok markus@


# 1.358 30-Apr-2017 djm

remove compat20/compat13/compat15 variables

ok markus@


Revision tags: OPENBSD_6_1_BASE
# 1.357 01-Feb-2017 dtucker

Return true reason for port forwarding failures where feasible rather
than always "administratively prohibited". bz#2674, ok djm@


# 1.356 18-Oct-2016 dtucker

Remove channel_input_port_forward_request(); the only caller was the
recently-removed SSH1 server code so it's now dead code. ok markus@


# 1.355 30-Sep-2016 djm

fix some -Wpointer-sign warnings in the new mux proxy; ok markus@


# 1.354 30-Sep-2016 markus

ssh proxy mux mode (-O proxy; idea from Simon Tatham):
- mux client speaks the ssh-packet protocol directly over unix-domain socket.
- mux server acts as a proxy, translates channel IDs and relays to the server.
- no filedescriptor passing necessary.
- combined with unix-domain forwarding it's even possible to run mux client
and server on different machines.
feedback & ok djm@


# 1.353 19-Sep-2016 natano

Replace two more arc4random() loops with arc4random_buf().

tweaks and ok dtucker
ok deraadt


# 1.352 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.351 19-Jul-2016 dtucker

Allow wildcard for PermitOpen hosts as well as ports. bz#2582, patch from
openssh at mzpqnxow.com and jjelen at redhat.com. ok markus@


# 1.350 07-Mar-2016 djm

refactor canohost.c: move functions that cache results closer to the
places that use them (authn and session code). After this, no state is
cached in canohost.c

feedback and ok markus@


Revision tags: OPENBSD_5_9_BASE
# 1.349 05-Feb-2016 naddy

Only check errno if read() has returned an error. EOF is not an error.
This fixes a problem where the mux master would sporadically fail to
notice that the client had exited.
ok mikeb@ djm@


# 1.348 15-Oct-2015 djm

fix some signed/unsigned integer type mismatches in format
strings; reported by Nicholas Lemonias


Revision tags: OPENBSD_5_8_BASE
# 1.347 01-Jul-2015 djm

better refuse ForwardX11Trusted=no connections attempted after
ForwardX11Timeout expires; reported by Jann Horn


# 1.346 30-Jun-2015 djm

fatal() when a remote window update causes the window value to
overflow. Reported by Georg Wicherski, ok markus@


# 1.345 30-Jun-2015 djm

Fix math error in remote window calculations that causes eventual stalls
for datagram channels. Reported by Georg Wicherski, ok markus@


# 1.344 05-Jun-2015 millert

For "ssh -L 12345:/tmp/sock" don't fail with "No forward host name."
(we have a path, not a host name). Based on a diff from Jared Yanovich.
OK djm@


# 1.343 08-May-2015 dtucker

Use xcalloc for permitted_adm_opens instead of xmalloc to ensure it's zeroed.
Fixes post-auth crash with permitopen=none. bz#2355, ok djm@


# 1.342 24-Apr-2015 deraadt

rename xrealloc() to xreallocarray() since it follows that form.
ok djm


Revision tags: OPENBSD_5_7_BASE
# 1.341 06-Feb-2015 millert

SIZE_MAX is standard, we should be using it in preference to the
obsolete SIZE_T_MAX. OK miod@ beck@


# 1.340 20-Jan-2015 deraadt

Reduce use of <sys/param.h> and transition to <limits.h> throughout.
ok djm markus


# 1.339 19-Jan-2015 markus

move dispatch to struct ssh; ok djm@


# 1.338 11-Dec-2014 djm

explicitly include sys/param.h in files that use the howmany() macro;
from portable


# 1.337 08-Oct-2014 djm

fix a few -Wpointer-sign warnings from clang


Revision tags: OPENBSD_5_6_BASE
# 1.336 15-Jul-2014 millert

Add support for Unix domain socket forwarding. A remote TCP port
may be forwarded to a local Unix domain socket and vice versa or
both ends may be a Unix domain socket. This is a reimplementation
of the streamlocal patches by William Ahern from:
http://www.25thandclement.com/~william/projects/streamlocal.html
OK djm@ markus@


# 1.335 05-Jul-2014 djm

fix remote-forward cancel regression; ok markus@


# 1.334 03-Jul-2014 djm

allow explicit ::1 and 127.0.0.1 forwarding bind addresses when
GatewayPorts=no; allows client to choose address family;
bz#2222 ok markus@


# 1.333 27-Jun-2014 markus

fix remote fwding with same listen port but different listen address
with gerhard@, ok djm@


# 1.332 28-Apr-2014 djm

buffer_get_string_ptr's return should be const to remind
callers that futzing with it will futz with the actual buffer
contents


Revision tags: OPENBSD_5_5_BASE
# 1.331 26-Feb-2014 djm

don't assume that the socks4 username is \0 terminated;
spotted by Ben Hawkes; ok markus@


# 1.330 15-Feb-2014 djm

avoid spurious "getsockname failed: Bad file descriptor" errors in ssh -W;
bz#2200, debian#738692 via Colin Watson; ok dtucker@


# 1.329 31-Jan-2014 tedu

replace most bzero with explicit_bzero, except a few that cna be memset
ok djm dtucker


# 1.328 19-Dec-2013 djm

bz#2147: fix multiple remote forwardings with dynamically assigned
listen ports. In the s->c message to open the channel we were sending
zero (the magic number to request a dynamic port) instead of the actual
listen port. The client therefore had no way of discriminating between
them.

Diagnosis and fix by ronf AT timeheart.net


# 1.327 08-Nov-2013 djm

use calloc for all structure allocations; from markus@


# 1.326 19-Sep-2013 djm

bz#1297 - tell the client (via packet_send_debug) when their preferred
listen address has been overridden by the server's GatewayPorts;
ok dtucker@


# 1.325 13-Sep-2013 djm

avoid unaligned access in code that reused a buffer to send a
struct in_addr in a reply; simpler just use use buffer_put_int();
from portable; spotted by and ok dtucker@


Revision tags: OPENBSD_5_4_BASE
# 1.324 12-Jul-2013 djm

branches: 1.324.2;
fix pointer-signedness warnings from clang/llvm-3.3; "seems nice" deraadt@


# 1.323 07-Jun-2013 dtucker

Add an "ABANDONED" channel state and use for mux sessions that are
disconnected via the ~. escape sequence. Channels in this state will
be able to close if the server responds, but do not count as active channels.
This means that if you ~. all of the mux clients when using ControlPersist
on a broken network, the backgrounded mux master will exit when the
Control Persist time expires rather than hanging around indefinitely.
bz#1917, also reported and tested by tedu@. ok djm@ markus@.


# 1.322 01-Jun-2013 dtucker

Use clock_gettime(CLOCK_MONOTONIC ...) for ssh timers so that things like
keepalives and rekeying will work properly over clock steps. Suggested by
markus@, "looks good" djm@.


# 1.321 17-May-2013 djm

bye, bye xfree(); ok markus@


# 1.320 06-Apr-2013 markus

handle ECONNABORTED for accept(); ok deraadt some time ago...


Revision tags: OPENBSD_5_3_BASE
# 1.319 02-Dec-2012 djm

branches: 1.319.2;
make AllowTcpForwarding accept "local" and "remote" in addition to its
current "yes"/"no" to allow the server to specify whether just local or
remote TCP forwarding is enabled. ok markus@


Revision tags: OPENBSD_5_2_BASE
# 1.318 23-Apr-2012 djm

fix function proto/source mismatch


# 1.317 11-Apr-2012 djm

don't spin in accept() when out of fds (ENFILE/ENFILE) - back off for a
while; ok deraadt@ markus@


# 1.316 29-Mar-2012 dtucker

Add PermitOpen none option based on patch from Loganaden Velvindron
(bz #1949). ok djm@


Revision tags: OPENBSD_5_1_BASE
# 1.315 23-Sep-2011 markus

unbreak remote portforwarding with dynamic allocated listen ports:
1) send the actual listen port in the open message (instead of 0).
this allows multiple forwardings with a dynamic listen port
2) update the matching permit-open entry, so we can identify where
to connect to
report: den at skbkontur.ru and P. Szczygielski
feedback and ok djm@


# 1.314 23-Sep-2011 dtucker

Add wildcard support to PermitOpen, allowing things like "PermitOpen
localhost:*". bz #1857, ok djm markus.


# 1.313 10-Sep-2011 markus

support cancellation of local/dynamic forwardings from ~C commandline;
ok & feedback djm@


# 1.312 09-Sep-2011 djm

support for cancelling local and remote port forwards via the multiplex
socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request
the cancellation of the specified forwardings; ok markus@


Revision tags: OPENBSD_5_0_BASE
# 1.311 22-Jun-2011 djm

hook up a channel confirm callback to warn the user then requested X11
forwarding was refused by the server; ok markus@


Revision tags: OPENBSD_4_9_BASE
# 1.310 24-Nov-2010 djm

remove a debug() that pollutes stderr on client connecting to a server
in debug mode (channel_close_fds is called transitively from the session
code post-fork); bz#1719, ok dtucker


Revision tags: OPENBSD_4_8_BASE
# 1.309 05-Aug-2010 djm

Fix a trio of bugs in the local/remote window calculation for datagram
data channels (i.e. TunnelForward):

Calculate local_consumed correctly in channel_handle_wfd() by measuring
the delta to buffer_len(c->output) from when we start to when we finish.
The proximal problem here is that the output_filter we use in portable
modified the length of the dequeued datagram (to futz with the headers
for !OpenBSD).

In channel_output_poll(), don't enqueue datagrams that won't fit in the
peer's advertised packet size (highly unlikely to ever occur) or which
won't fit in the peer's remaining window (more likely).

In channel_input_data(), account for the 4-byte string header in
datagram packets that we accept from the peer and enqueue in c->output.

report, analysis and testing 2/3 cases from wierbows AT us.ibm.com;
"looks good" markus@


# 1.308 13-Jul-2010 djm

s/timing_safe_cmp/timingsafe_bcmp/g


# 1.307 13-Jul-2010 djm

implement a timing_safe_cmp() function to compare memory without leaking
timing information by short-circuiting like memcmp() and use it for
some of the more sensitive comparisons (though nothing high-value was
readily attackable anyway); "looks ok" markus@


# 1.306 25-Jun-2010 djm

bz#1750: fix requirement for /dev/null inside ChrootDirectory for
internal-sftp accidentally introduced in r1.253 by removing the code
that opens and dup /dev/null to stderr and modifying the channels code
to read stderr but discard it instead; ok markus@


# 1.305 25-Jun-2010 djm

bz#1327: remove hardcoded limit of 100 permitopen clauses and port
forwards per direction; ok markus@ stevesk@


# 1.304 14-May-2010 djm

Pause the mux channel while waiting for reply from aynch callbacks.
Prevents misordering of replies if new requests arrive while waiting.

Extend channel open confirm callback to allow signalling failure
conditions as well as success. Use this to 1) fix a memory leak, 2)
start using the above pause mechanism and 3) delay sending a success/
failure message on mux slave session open until we receive a reply from
the server.

motivated by and with feedback from markus@


Revision tags: OPENBSD_4_7_BASE
# 1.303 30-Jan-2010 djm

fake local addr:port when stdio fowarding as some servers (Tectia at
least) validate that they are well-formed;
reported by imorgan AT nas.nasa.gov
ok dtucker


# 1.302 26-Jan-2010 djm

rewrite ssh(1) multiplexing code to a more sensible protocol.

The new multiplexing code uses channels for the listener and
accepted control sockets to make the mux master non-blocking, so
no stalls when processing messages from a slave.

avoid use of fatal() in mux master protocol parsing so an errant slave
process cannot take down a running master.

implement requesting of port-forwards over multiplexed sessions. Any
port forwards requested by the slave are added to those the master has
established.

add support for stdio forwarding ("ssh -W host:port ...") in mux slaves.

document master/slave mux protocol so that other tools can use it to
control a running ssh(1). Note: there are no guarantees that this
protocol won't be incompatibly changed (though it is versioned).

feedback Salvador Fandino, dtucker@
channel changes ok markus@


# 1.301 11-Jan-2010 dtucker

Add a 'netcat mode' (ssh -W). This connects stdio on the client to a single
port forward on the server. This allows, for example, using ssh as
a ProxyCommand to route connections via intermediate servers.
bz #1618, man page help from jmc@, ok markus@


# 1.300 09-Jan-2010 dtucker

Remove RoutingDomain from ssh since it's now not needed. It can be replaced
with "route exec" or "nc -V" as a proxycommand. "route exec" also ensures
that trafic such as DNS lookups stays withing the specified routingdomain.

For example (from reyk):
# route -T 2 exec /usr/sbin/sshd
or inherited from the parent process
$ route -T 2 exec sh
$ ssh 10.1.2.3

ok deraadt@ markus@ stevesk@ reyk@


# 1.299 11-Nov-2009 markus

fix race condition in x11/agent channel allocation: don't read after
the end of the select read/write fdset and make sure a reused FD
is not touched before the pre-handlers are called.
with and ok djm@


# 1.298 10-Nov-2009 dtucker

Set close-on-exec on various descriptors so they don't get leaked to
child processes. bz #1643, patch from jchadima at redhat, ok deraadt.


# 1.297 28-Oct-2009 reyk

Allow to set the rdomain in ssh/sftp/scp/sshd and ssh-keyscan.

ok markus@


Revision tags: OPENBSD_4_6_BASE
# 1.296 25-May-2009 andreas

Put the globals in packet.c into a struct and don't access it directly
from other files. No functional changes.
ok markus@ djm@


Revision tags: OPENBSD_4_5_BASE
# 1.295 12-Feb-2009 djm

support remote port forwarding with a zero listen port (-R0:...) to
dyamically allocate a listen port at runtime (this is actually
specified in rfc4254); bz#1003 ok markus@


# 1.294 22-Jan-2009 djm

oops! I committed the wrong version of the Channel->path diff,
it was missing some tweaks suggested by stevesk@


# 1.293 22-Jan-2009 djm

make Channel->path an allocated string, saving a few bytes here and
there and fixing bz#1380 in the process; ok markus@


# 1.292 14-Jan-2009 djm

support SOCKS4A protocol, from dwmw2 AT infradead.org via bz#1482;
"looks ok" markus@


# 1.291 01-Jan-2009 djm

call channel destroy callbacks on receipt of open failure messages.
fixes client hangs when connecting to a server that has MaxSessions=0
set spotted by imorgan AT nas.nasa.gov; ok markus@


# 1.290 09-Dec-2008 stevesk

channel_print_adm_permitted_opens() should deal with all the printing
for that config option. suggested by markus@; ok markus@ djm@
dtucker@


# 1.289 02-Dec-2008 markus

s/remote_id/id/ to be more consistent with other code; ok djm@


# 1.288 11-Nov-2008 stevesk

for sshd -T print 'permitopen any' vs. 'permitopen' for case of no
permitopen's; ok and input dtucker@


# 1.287 01-Nov-2008 stevesk

fix some typos in log messages; ok djm@


Revision tags: OPENBSD_4_4_BASE
# 1.286 16-Jul-2008 djm

this loop index should be automatic, not static


# 1.285 13-Jul-2008 djm

use struct sockaddr_storage instead of struct sockaddr for accept(2)
address argument. from visibilis AT yahoo.com in bz#1485; ok markus@


# 1.284 12-Jul-2008 djm

unbreak; move clearing of cctx struct to before first use
reported by dkrause@


# 1.283 10-Jul-2008 markus

missing bzero; from mickey; ok djm@


# 1.282 16-Jun-2008 dtucker

Rename the isatty argument to is_tty so we don't shadow isatty(3).
ok markus@


# 1.281 15-Jun-2008 djm

don't call isatty() on a pty master, instead pass a flag down to
channel_set_fds() indicating that te fds refer to a tty. Fixes a
hang on exit on Solaris (bz#1463) in portable but is actually
a generic bug; ok dtucker deraadt markus


# 1.280 12-Jun-2008 djm

The multiplexing escape char handler commit last night introduced a
small memory leak per session; plug it.


# 1.279 12-Jun-2008 djm

Enable ~ escapes for multiplex slave sessions; give each channel
its own escape state and hook the escape filters up to muxed
channels. bz #1331

Mux slaves do not currently support the ~^Z and ~& escapes.

NB. this change cranks the mux protocol version, so a new ssh
mux client will not be able to connect to a running old ssh
mux master.

ok dtucker@


# 1.278 10-Jun-2008 dtucker

Add extended test mode (-T) and connection parameters for test mode (-C).
-T causes sshd to write its effective configuration to stdout and exit.
-C causes any relevant Match rules to be applied before output. The
combination allows tesing of the parser and config files. ok deraadt djm


# 1.277 09-May-2008 markus

error-fd race: don't enable the error fd in the select bitmask
for channels with both in- and output closed, since the channel
will go away before we call select();
report, lots of debugging help and ok djm@


# 1.276 09-May-2008 djm

Try additional addresses when connecting to a port forward destination
whose DNS name resolves to more than one address. The previous behaviour
was to try the first address and give up.

Reported by stig AT venaas.com in bz#343

great feedback and ok markus@


# 1.275 08-May-2008 djm

Implement a channel success/failure status confirmation callback
mechanism. Each channel maintains a queue of callbacks, which will
be drained in order (RFC4253 guarantees confirm messages are not
reordered within an channel).

Also includes a abandonment callback to clean up if a channel is
closed without sending confirmation messages. This probably
shouldn't happen in compliant implementations, but it could be
abused to leak memory.

ok markus@ (as part of a larger diff)


# 1.274 08-May-2008 markus

avoid extra malloc/copy/free when receiving data over the net;
~10% speedup for localhost-scp; ok djm@


# 1.273 02-Apr-2008 markus

avoid possible hijacking of x11-forwarded connections (back out 1.183)
CVE-2008-1483; ok djm@


Revision tags: OPENBSD_4_3_BASE
# 1.272 19-Jan-2008 djm

branches: 1.272.2;
When we added support for specified bind addresses for port forwards, we
added a quirk SSH_OLD_FORWARD_ADDR. There is a bug in our handling of
this for -L port forwards that causes the client to listen on both v4
and v6 addresses when connected to a server with this quirk, despite
having set 0.0.0.0 as a bind_address.

report and patch from Jan.Pechanec AT Sun.COM; ok dtucker@


# 1.271 27-Dec-2007 dtucker

Add a small helper function to consistently handle the EAI_SYSTEM error
code of getaddrinfo. Prompted by vgiffin at apple com via bz #1417.
ok markus@ stevesk@


Revision tags: OPENBSD_4_2_BASE
# 1.270 25-Jun-2007 dtucker

branches: 1.270.2;
Correct test for window updates every three packets; prevents sending
window updates for every single packet. ok markus@


# 1.269 11-Jun-2007 markus

send 'window adjust' messages every tree packets and do not wait
until 50% of the window is consumed. ok djm dtucker


Revision tags: OPENBSD_4_1_BASE
# 1.268 03-Jan-2007 stevesk

branches: 1.268.2;
spaces


# 1.267 12-Dec-2006 djm

bz #1019: some ssh.com versions apparently can't cope with the remote port
forwarding bind_address being a hostname, so send them an address for cases
where they are not explicitly specified (wildcard or localhost bind).
reported by daveroth AT acm.org; ok dtucker@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.266 29-Aug-2006 djm

normalise some inconsistent (but harmless) NULL pointer checks
spotted by the Stanford SATURN tool, via Isil Dillig;
ok markus@ deraadt@


# 1.265 03-Aug-2006 deraadt

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.264 01-Aug-2006 stevesk

clean extra spaces


# 1.263 01-Aug-2006 stevesk

move #include <stdio.h> out of includes.h


# 1.262 26-Jul-2006 stevesk

move #include <stdlib.h> out of includes.h


# 1.261 25-Jul-2006 stevesk

move #include <sys/time.h> out of includes.h


# 1.260 22-Jul-2006 stevesk

move #include <string.h> out of includes.h


# 1.259 21-Jul-2006 stevesk

more ARGSUSED (lint) for dispatch table-driven functions; ok djm@


# 1.258 21-Jul-2006 dtucker

Make PermitOpen take a list of permitted ports and act more like most other
keywords (ie the first match is the effective setting). This also makes it
easier to override a previously set PermitOpen. ok djm@


# 1.257 17-Jul-2006 dtucker

Add PermitOpen directive to sshd_config which is equivalent to the
"permitopen" key option. Allows server admin to allow TCP port forwarding
only two specific host/port pairs. Useful when combined with Match.

If permitopen is used in both sshd_config and a key option, both must allow
a given connection before it will be permitted.

Note that users can still use external forwarders such as netcat, so to be
those must be controlled too for the limits to be effective.

Feedback & ok djm@, man page corrections & ok jmc@.


# 1.256 17-Jul-2006 stevesk

move #include <unistd.h> out of includes.h


# 1.255 12-Jul-2006 stevesk

move #include <netdb.h> out of includes.h; ok djm@


# 1.254 11-Jul-2006 stevesk

move #include <errno.h> out of includes.h; ok markus@


# 1.253 11-Jul-2006 markus

add ExitOnForwardFailure: terminate the connection if ssh(1)
cannot set up all requested dynamic, local, and remote port
forwardings. ok djm, dtucker, stevesk, jmc


# 1.252 10-Jul-2006 djm

fix misparsing of SOCKS 5 packets that could result in a crash;
reported by mk@ ok markus@


# 1.251 03-Jul-2006 stevesk

move #include <arpa/inet.h> out of includes.h; old ok djm@


# 1.250 16-Apr-2006 djm

Fix condition where we could exit with a fatal error when an input
buffer became too large and the remote end had advertised a big window.
The problem was a mismatch in the backoff math between the channels code
and the buffer code, so make a buffer_check_alloc() function that the
channels code can use to propsectivly check whether an incremental
allocation will succeed. bz #1131, debugged with the assistance of
cove AT wildpackets.com; ok dtucker@ deraadt@


# 1.249 30-Mar-2006 djm

ARGSUSED for dispatch table-driven functions


# 1.248 28-Mar-2006 deraadt

do not accept unreasonable X ports numbers; ok djm


# 1.247 25-Mar-2006 deraadt

delete cast not required


# 1.246 25-Mar-2006 deraadt

remove (char *) casts to a function that accepts void * for the arg


# 1.245 25-Mar-2006 deraadt

use strtonum() instead of atoi() [limit X screens to 400, sorry]


# 1.244 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.243 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.242 25-Mar-2006 djm

introduce xcalloc() and xasprintf() failure-checked allocations functions
and use them throughout openssh

xcalloc is particularly important because malloc(nmemb * size) is a
dangerous idiom (subject to integer overflow) and it is time for it to
die

feedback and ok deraadt@


# 1.241 20-Mar-2006 deraadt

spacing


# 1.240 20-Mar-2006 deraadt

x11_fake_data is only ever used as u_char *


# 1.239 20-Mar-2006 deraadt

annoying spacing fixes getting in the way of real diffs


# 1.238 20-Mar-2006 deraadt

sprinkle u_int throughout pty subsystem, ok markus


# 1.237 19-Mar-2006 deraadt

spacing


# 1.236 19-Mar-2006 deraadt

RCSID() can die


Revision tags: OPENBSD_3_9_BASE
# 1.235 20-Feb-2006 stevesk

branches: 1.235.2;
move #include <sys/un.h> out of includes.h; ok djm@


# 1.234 10-Feb-2006 stevesk

move #include <sys/ioctl.h> out of includes.h; ok markus@


# 1.233 07-Feb-2006 stevesk

move #include <termios.h> out of includes.h; ok markus@


# 1.232 30-Jan-2006 reyk

mark channel as write failed or dead instead of read failed on error
of the channel output filter.

ok markus@


# 1.231 30-Dec-2005 reyk

add channel output filter interface.

ok djm@, suggested by markus@


# 1.230 28-Dec-2005 stevesk

use 'break-in' for consistency; ok deraadt@ ok and input jmc@


# 1.229 12-Dec-2005 markus

make sure protocol messages for internal channels are ignored.
allow adjust messages for non-open channels; with and ok djm@


# 1.228 06-Dec-2005 reyk

Add support for tun(4) forwarding over OpenSSH, based on an idea and
initial channel code bits by markus@. This is a simple and easy way to
use OpenSSH for ad hoc virtual private network connections, e.g.
administrative tunnels or secure wireless access. It's based on a new
ssh channel and works similar to the existing TCP forwarding support,
except that it depends on the tun(4) network interface on both ends of
the connection for layer 2 or layer 3 tunneling. This diff also adds
support for LocalCommand in the ssh(1) client.

ok djm@, markus@, jmc@ (manpages), tested and discussed with others


# 1.227 14-Oct-2005 stevesk

free()->xfree(); ok djm@


# 1.226 11-Oct-2005 djm

bz #1076 set SO_REUSEADDR on X11 forwarding listner sockets, preventing
bind() failure when a previous connection's listeners are in TIME_WAIT,
reported by plattner AT inf.ethz.ch; ok dtucker@


# 1.225 10-Oct-2005 djm

fix regression I introduced in 4.2: X11 forwardings initiated after
a session has exited (e.g. "(sleep 5; xterm) &") would not start.
bz #1086 reported by t8m AT centrum.cz; ok markus@ dtucker@


# 1.224 07-Sep-2005 markus

enforce chanid != NULL; ok djm


Revision tags: OPENBSD_3_8_BASE
# 1.223 17-Jul-2005 djm

branches: 1.223.2;
knf says that a 2nd level indent is four (not three or five) spaces


# 1.222 17-Jul-2005 djm

Fix a number of X11 forwarding channel leaks:
1. Refuse multiple X11 forwarding requests on the same session
2. Clean up all listeners after a single_connection X11 forward, not just
the one that made the single connection
3. Destroy X11 listeners when the session owning them goes away
testing and ok dtucker@


# 1.221 16-Jul-2005 djm

spacing


# 1.220 04-Jul-2005 markus

don't forget to set x11_saved_display


# 1.219 04-Jul-2005 djm

implement support for X11 and agent forwarding over multiplex slave
connections. Because of protocol limitations, the slave connections inherit
the master's DISPLAY and SSH_AUTH_SOCK rather than distinctly forwarding
their own.

ok dtucker@ "put it in" deraadt@


# 1.218 01-Jul-2005 markus

don't free() if getaddrinfo() fails; report mpech@


# 1.217 17-Jun-2005 djm

make this -Wsign-compare clean; ok avsm@ markus@


# 1.216 16-Jun-2005 markus

don't exit if getpeername fails for forwarded ports; bugzilla #1054; ok djm


# 1.215 16-Jun-2005 djm

move x11_get_proto from ssh.c to clientloop.c, to make muliplexed xfwd easier
later; ok deraadt@


Revision tags: OPENBSD_3_7_BASE
# 1.214 14-Mar-2005 markus

branches: 1.214.2;
limit input buffer size for channels; bugzilla #896; with and ok dtucker@


# 1.213 10-Mar-2005 deraadt

spacing


# 1.212 01-Mar-2005 djm

bz#413: allow optional specification of bind address for port forwardings.
Patch originally by Dan Astorian, but worked on by several people
Adds GatewayPorts=clientspecified option on server to allow remote forwards
to bind to client-specified ports.

ok markus@


# 1.211 29-Oct-2004 djm

fix some window size change bugs for multiplexed connections: windows sizes
were not being updated if they had changed after ~^Z suspends and SIGWINCH
was not being processed unless the first connection had requested a tty;
ok markus


Revision tags: OPENBSD_3_6_BASE
# 1.210 23-Aug-2004 djm

branches: 1.210.2;
typo, spotted by Martin.Kraemer AT Fujitsu-Siemens.com; ok markus


# 1.209 11-Aug-2004 avsm

some signed/unsigned int comparison cleanups; markus@ ok


# 1.208 11-Jul-2004 deraadt

spaces


# 1.207 21-Jun-2004 avsm

make ssh -Wshadow clean, no functional changes
markus@ ok


# 1.206 18-Jun-2004 djm

clientloop.c


# 1.205 14-Jun-2004 djm

set_nonblock() instead of fnctl(...,O_NONBLOCK); "looks sane" deraadt@


# 1.204 13-Jun-2004 djm

implement session multiplexing in the client (the server has supported this
since 2.0); ok markus@


# 1.203 26-May-2004 markus

missing freeaddrinfo; Andrey Matveev


# 1.202 21-May-2004 djm

bz #756: add support for the cancel-tcpip-forward request for the server and
the client (through the ~C commandline). reported by z3p AT twistedmatrix.com;
ok markus@


# 1.201 11-May-2004 deraadt

improve some code lint did not like; djm millert ok


Revision tags: OPENBSD_3_5_BASE
# 1.200 19-Jan-2004 markus

branches: 1.200.2;
fake consumption for half closed channels since the peer is waiting for
window adjust messages; bugzilla #790 Matthew Dillon; test + ok dtucker@
reproduce with sh -c 'ulimit -f 10; ssh host -n od /bsd | cat > foo'


# 1.199 02-Dec-2003 markus

use SSH_LISTEN_BACKLOG (=128) in listen(2).


# 1.198 21-Nov-2003 djm

unexpand and delete whitespace at EOL; ok markus@


# 1.197 23-Sep-2003 markus

move client only agent code to clientloop.c


# 1.196 19-Sep-2003 markus

do not call channel_free_all on fatal; ok deraadt


Revision tags: OPENBSD_3_4_BASE
# 1.195 16-Sep-2003 markus

branches: 1.195.2;
more malloc/fatal fixes; ok millert/deraadt; ghudson at MIT.EDU


# 1.194 29-Aug-2003 markus

be less chatty; debug -> debug2, cleanup; ok henning@


# 1.193 02-Jul-2003 markus

(re)add socks5 suppport to -D; ok djm@
now ssh(1) can act both as a socks 4 and socks 5 server and
dynamically forward ports.


# 1.192 02-Jul-2003 markus

deny dynamic forwarding with -R for v1, too; ok djm@


# 1.191 24-Jun-2003 markus

int -> u_int; ok djm@, deraadt@, mouring@


# 1.190 11-May-2003 markus

make channel_new() strdup the 'remote_name' (not the caller); ok theo


# 1.189 14-Apr-2003 markus

avoid hardcoded SOCK_xx; with itojun@; should allow ssh over SCTP


# 1.188 08-Apr-2003 itojun

rename log() into logit() to avoid name conflict. markus ok, from netbsd


Revision tags: OPENBSD_3_3_BASE
# 1.187 05-Mar-2003 markus

branches: 1.187.2;
fix memory leaks; from dlheine@suif.Stanford.EDU/CLOUSEAU; ok djm@


# 1.186 10-Jan-2003 djm

hush socket() errors, except last. Fixes mindrot bug #408; ok markus@


# 1.185 01-Jan-2003 markus

move big output buffer messages to debug2


# 1.184 13-Dec-2002 markus

cleanup debug messages, more useful information for the client user.


Revision tags: OPENBSD_3_2_BASE
# 1.183 17-Sep-2002 itojun

branches: 1.183.2;
don't quit while creating X11 listening socket.

http://mail-index.netbsd.org/current-users/2002/09/16/0005.html
got from portable. markus ok


# 1.182 13-Sep-2002 stevesk

remove use of SO_LINGER, it should not be needed. error check
SO_REUSEADDR. fixup comments. ok markus@


# 1.181 09-Sep-2002 markus

signed vs unsigned from -pedantic; ok henning@


# 1.180 04-Jul-2002 deraadt

blah blah minor nothing as i read and re-read and re-read...


# 1.179 26-Jun-2002 markus

limit # of channels to 10000


# 1.178 24-Jun-2002 markus

move channel counter to u_int


# 1.177 23-Jun-2002 markus

tcode is u_int


# 1.176 23-Jun-2002 deraadt

display, screen, row, col, xpixel, ypixel are u_int; markus ok


# 1.175 10-Jun-2002 markus

move creation of agent socket to session.c; no need for uidswapping
in channel.c.


# 1.174 09-Jun-2002 markus

use tab not spaces (|unexpand)


# 1.173 22-Apr-2002 markus

request reply (success/failure) for -R style fwd in protocol v2,
depends on ordered replies.
fixes http://bugzilla.mindrot.org/show_bug.cgi?id=215; ok provos@


Revision tags: OPENBSD_3_1_BASE
# 1.172 25-Mar-2002 markus

branches: 1.172.2;
don't send stderr data after EOF, accept this from older known (broken)
sshd servers only, fixes http://bugzilla.mindrot.org/show_bug.cgi?id=179


# 1.171 04-Mar-2002 markus

off by one; thanks to joost@pine.nl


# 1.170 27-Feb-2002 stevesk

remove unneeded casts in [gs]etsockopt(); ok markus@


# 1.169 24-Feb-2002 stevesk

disable Nagle in connect_to() and channel_post_port_listener() (port
forwarding endpoints). the intention is to preserve the on-the-wire
appearance to applications at either end; the applications can then
enable TCP_NODELAY according to their requirements. ok markus@


# 1.168 14-Feb-2002 markus

increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.


# 1.167 06-Feb-2002 markus

channel_new never returns NULL, mouring@; ok djm@


# 1.166 05-Feb-2002 markus

merge channel_request() into channel_request_start()


# 1.165 03-Feb-2002 markus

generic callbacks are not really used, remove and
add a callback for msg of type SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
ok djm@


# 1.164 03-Feb-2002 markus

remove unused channel_input_channel_request


# 1.163 27-Jan-2002 stevesk

add X11UseLocalhost; ok markus@


# 1.162 24-Jan-2002 stevesk

add set_nodelay() to set TCP_NODELAY on a socket (prep for nagle tuning).
no nagle changes just yet; ok djm@ markus@


# 1.161 21-Jan-2002 markus

cleanup channels faster if the are empty and we are in drain-state; ok deraadt@


# 1.160 16-Jan-2002 markus

wrapper for channel_setup_fwd_listener


# 1.159 14-Jan-2002 markus

remove function pointers for events, remove chan_init*; ok provos@


# 1.158 09-Jan-2002 markus

replace buffer_consume(b, buffer_len(b)) with buffer_clear(b); ok provos@


# 1.157 09-Jan-2002 markus

merge channel_pre_open_15/channel_pre_open_20; ok provos@


# 1.156 05-Jan-2002 markus

fix hanging x11 channels for rejected cookies (e.g. XAUTHORITY=/dev/null xbiff)
bug #36, based on patch from djast@cs.toronto.edu


# 1.155 29-Dec-2001 stevesk

remove unneeded casts and some char->u_char cleanup; ok markus@


# 1.154 28-Dec-2001 markus

remove plen from the dispatch fn. it's no longer used.


# 1.153 28-Dec-2001 markus

packet_read* no longer return the packet length, since it's not used.


# 1.152 28-Dec-2001 markus

s/packet_done/packet_check_eom/ (end-of-message); ok djm@


# 1.151 27-Dec-2001 markus

get rid of packet_integrity_check, use packet_done() instead.


# 1.150 20-Dec-2001 djm

Conformance fix: we should send failing packet sequence number when
responding with a SSH_MSG_UNIMPLEMENTED message. Spotted by
yakk@yakk.dot.net; ok markus@


# 1.149 20-Dec-2001 markus

setup x11 listen socket for just one connect if the client requests so.
(v2 only, but the openssh client does not support this feature).


# 1.148 19-Dec-2001 deraadt

basic KNF done while i was looking for something else


# 1.147 08-Dec-2001 stevesk

use only one path to X11 UNIX domain socket vs. an array of paths
to try. report from djast@cs.toronto.edu. ok markus@


# 1.146 06-Dec-2001 stevesk

disable nagle for X11 fake server and client TCPs. from netbsd.
ok markus@


# 1.145 06-Dec-2001 stevesk

strncpy->strlcpy. remaining strncpy's are necessary. ok markus@


# 1.144 06-Dec-2001 stevesk

shutdown(sock, SHUT_RDWR) not needed here; ok markus@


# 1.143 05-Dec-2001 deraadt

minor KNF


# 1.142 05-Dec-2001 itojun

make it compile with more strict prototype checking


# 1.141 29-Nov-2001 stevesk

sshd X11 fake server will now listen on localhost by default:
$ echo $DISPLAY
localhost:12.0
$ netstat -an|grep 6012
tcp 0 0 127.0.0.1.6012 *.* LISTEN
tcp6 0 0 ::1.6012 *.* LISTEN
sshd_config gatewayports=yes can be used to revert back to the old
behavior. will control this with another option later. ok markus@


Revision tags: OPENBSD_3_0_BASE
# 1.140 10-Oct-2001 markus

branches: 1.140.2;
try to keep channels open until an exit-status message is sent.
don't kill the login shells if the shells stdin/out/err is closed.
this should now work:
ssh -2n localhost 'exec > /dev/null 2>&1; sleep 10; exit 5'; echo ?


# 1.139 09-Oct-2001 markus

simplify session close: no more delayed session_close, no more blocking wait() calls.


# 1.138 08-Oct-2001 markus

better debug


# 1.137 07-Oct-2001 markus

avoid possible FD_ISSET overflow for channels established
during channnel_after_select() (used for dynamic channels).


# 1.136 04-Oct-2001 markus

comment out bogus conditions for selecting on connection_in


# 1.135 01-Oct-2001 markus

remove ugliness; vp@drexel.edu via angelos


# 1.134 17-Sep-2001 markus

don't send fake dummy packets on CR (\r)
bugreport from yyua@cs.sfu.ca via solar@@openwall.com


# 1.133 17-Sep-2001 markus

try to fix agent-forwarding-backconnection-bug, as seen on HPUX, for example;
with Lutz.Jaenicke@aet.TU-Cottbus.DE,


# 1.132 17-Jul-2001 markus

keep track of both maxfd and the size of the malloc'ed fdsets.
update maxfd if maxfd gets closed.


# 1.131 02-Jul-2001 markus

improve cleanup/exit logic in ssh2:
stop listening to channels, detach channel users (e.g. sessions).
wait for children (i.e. dying sessions), send exit messages,
cleanup all channels.


# 1.130 30-Jun-2001 stevesk

adress -> address; ok markus@


# 1.129 29-Jun-2001 stevesk

use socklen_t for getsockopt arg #5; ok markus@


# 1.128 25-Jun-2001 markus

update copyright for 2001


# 1.127 23-Jun-2001 itojun

more strict prototypes. raise warning level in Makefile.inc. markus ok'ed
TODO; cleanup headers


# 1.126 20-Jun-2001 markus

move from channel_stop_listening to channel_free_all,
call channel_free_all before calling waitpid() in serverloop.
fixes the utmp handling; report from Lutz.Jaenicke@aet.TU-Cottbus.DE


# 1.125 07-Jun-2001 markus

use xxx_put_cstring()


# 1.124 05-Jun-2001 markus

don't delete the auth socket in channel_stop_listening()
auth_sock_cleanup_proc() will take care of this.


# 1.123 04-Jun-2001 markus

switch uid when cleaning up tmp files and sockets; reported by zen-parse@gmx.net on bugtraq


# 1.122 03-Jun-2001 markus

use fatal_register_cleanup instead of atexit, sync with x11 authdir handling


# 1.121 31-May-2001 markus

undo the .c file split, just merge the header and keep the cvs history


# 1.120 30-May-2001 markus

channel layer cleanup: merge header files and split .c files


# 1.119 28-May-2001 markus

cleanup, typo


# 1.118 28-May-2001 markus

undo broken channel fix and try a different one. there
should be still some select errors...


# 1.117 19-May-2001 stevesk

typo in error message


# 1.116 16-May-2001 markus

more select() error fixes (don't set rfd/wfd to -1).


# 1.115 09-May-2001 markus

fix -R for protocol 2, noticed by greg@nest.cx.
bug was introduced with experimental dynamic forwarding.


# 1.114 08-May-2001 markus

adds correct error reporting to async connect()s
fixes the server-discards-data-before-connected-bug found by onoe@sm.sony.co.jp


# 1.113 04-May-2001 markus

move to Channel **channels (instead of Channel *channels), fixes realloc problems.
channel_new now returns a Channel *, favour Channel * over channel id.
remove old channel_allocate interface.


# 1.112 04-May-2001 markus

channel_new() reallocs channels[], we cannot use Channel *c after calling
channel_new(), XXX fix this in the future...


# 1.111 03-May-2001 stevesk

typo in debug() string


# 1.110 29-Apr-2001 markus

more ssh.com-2.0.x bug-compat; from per@appgate.com


Revision tags: OPENBSD_2_9_BASE
# 1.109 17-Apr-2001 markus

branches: 1.109.2;
undo socks5 and https support since they are not really used and
only bloat ssh. remove -D from usage(), since '-D' is experimental.


# 1.108 14-Apr-2001 markus

remove some channels that are not appropriate for keepalive.


# 1.107 13-Apr-2001 beck

Add options ClientAliveInterval and ClientAliveCountMax to sshd.
This gives the ability to do a "keepalive" via the encrypted channel
which can't be spoofed (unlike TCP keepalives). Useful for when you want
to use ssh connections to authenticate people for something, and know
relatively quickly when they are no longer authenticated. Disabled
by default (of course). ok markus@


# 1.106 11-Apr-2001 markus

https-connect and socks5 support. i feel so bad.


# 1.105 10-Apr-2001 markus

debug cleanup


# 1.104 10-Apr-2001 markus

cleanup socks4 handling


# 1.103 07-Apr-2001 markus

allow the ssh client act as a SOCKS4 proxy (dynamic local portforwarding).
work by Dan Kaminsky <dankamin@cisco.com> and me. thanks to Dan for this
great patch: use 'ssh -D 1080 host' and make netscape use localhost:1080 as
a socks proxy.


# 1.102 06-Apr-2001 markus

do gid/groups-swap in addition to uid-swap, should help if /home/group
is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
to olar@openwall.com is comments. we had many requests for this.


# 1.101 05-Apr-2001 markus

fix whitespace: unexpand + trailing spaces.


# 1.100 04-Apr-2001 markus

more robust rekeying
don't send channel data after rekeying is started.


# 1.99 16-Mar-2001 markus

implement "permitopen" key option, restricts -L style forwarding to
to specified host:port pairs. based on work by harlan@genua.de


# 1.98 04-Mar-2001 millert

log functions should not be passed strings that end in newline as they
get passed on to syslog() and when logging to stderr, do_log() appends
its own newline.


# 1.97 04-Mar-2001 markus

debug1->2


# 1.96 28-Feb-2001 markus

typo


# 1.95 28-Feb-2001 markus

use ignore message to simulate a SSH2_MSG_CHANNEL_DATA message
use random content in ignore messages.


# 1.94 28-Feb-2001 markus

unify debug messages


# 1.93 28-Feb-2001 markus

make sure remote stderr does not get truncated.
remove closed fd's from the select mask.


# 1.92 16-Feb-2001 markus

remove debug


# 1.91 15-Feb-2001 markus

genericize password padding function for SSH1 and SSH2.
add stylized echo to 2, too.


# 1.90 08-Feb-2001 markus

nuke sprintf, ok deraadt@


# 1.89 04-Feb-2001 stevesk

unexpand and remove end-of-line whitespace; ok markus@


# 1.88 01-Feb-2001 markus

use ipaddr in channel messages, ietf-secsh wants this


# 1.87 31-Jan-2001 markus

do not disconnect if local port forwarding fails, e.g. if port is already in use


# 1.86 31-Jan-2001 markus

ssh.com-2.0.1x does not send additional info in CHANNEL_OPEN_FAILURE messages; bug report from edmundo@rano.org


# 1.85 29-Jan-2001 markus

add get_peer_ipaddr(socket), x11-fwd in ssh2 requires ipaddr, not DNS


# 1.84 29-Jan-2001 markus

fix select overflow; ok deraadt@ and stevesk@


# 1.83 24-Jan-2001 stevesk

missing freeaddrinfo(); ok markus@


# 1.82 21-Jan-2001 markus

split ssh.h and try to cleanup the #include mess. remove unnecessary #includes.
rename util.[ch] -> misc.[ch]


# 1.81 19-Jan-2001 markus

move ssh1 definitions to ssh1.h, pathnames to pathnames.h


# 1.80 08-Jan-2001 markus

O_NDELAY -> O_NONBLOCK; thanks stevesk@pobox.com


# 1.79 29-Dec-2000 markus

missing xfree; from vaughan99@yahoo.com


# 1.78 29-Dec-2000 markus

remove->unlink; stevesk@pobox.com


# 1.77 19-Dec-2000 markus

replace 'unsigned bla' with 'u_bla' everywhere. also, replace 'char unsigned'
with u_char.


# 1.76 19-Dec-2000 markus

remove() -> unlink() for consistency


# 1.75 05-Dec-2000 markus

async connects for -R/-L; ok deraadt@


# 1.74 30-Nov-2000 markus

debug -> warn if server tries to do -R style fwd w/o client requesting this; ok niels@


# 1.73 06-Nov-2000 markus

agent forwarding and -R for ssh2, based on work from jhuuskon@messi.uku.fi


Revision tags: OPENBSD_2_8_BASE
# 1.72 27-Oct-2000 markus

branches: 1.72.2;
deny agent/x11 forwarding unless requested; thanks to jwl@pobox.com


# 1.71 27-Oct-2000 markus

enable non-blocking IO on channels, and tty's (except for the client ttys).


# 1.70 28-Sep-2000 markus

debug -> debug2 cleanup


# 1.69 21-Sep-2000 markus

add context to dispatch_run


# 1.68 07-Sep-2000 markus

cleanup window and packet sizes for ssh2 flow control; ok niels


# 1.67 07-Sep-2000 deraadt

cleanup copyright notices on all files. I have attempted to be accurate with
the details. everything is now under Tatu's licence (which I copied from his
readme), and/or the core-sdi bsd-ish thing for deattack, or various openbsd
developers under a 2-term bsd licence. We're not changing any rules, just
being accurate.


# 1.66 19-Aug-2000 markus

more ~ support for ssh2


# 1.65 19-Aug-2000 markus

support for ~. in ssh2


# 1.64 16-Jul-2000 markus

make ssh-add accept dsa keys (the agent does not)


# 1.63 25-Jun-2000 provos

correct check for bad channel ids; from Wei Dai <weidai@eskimo.com>


# 1.62 20-Jun-2000 markus

OpenBSD tag


# 1.61 17-Jun-2000 deraadt

a real nix


# 1.60 17-Jun-2000 deraadt

everyone says "nix it"


# 1.59 30-May-2000 markus

don't panic if mkdtemp fails for authfwd; jkb@yahoo-inc.com via kris@FreeBSD.org


# 1.58 17-May-2000 markus

enable nonblocking IO for sshd w/ proto 1, too; split out common code


Revision tags: OPENBSD_2_7_BASE
# 1.57 08-May-2000 markus

branches: 1.57.2;
bug compat w/ ssh-2.0.13 x11, split out bugs


# 1.56 03-May-2000 markus

GatewayPorts for sshd, ok deraadt@


# 1.55 02-May-2000 markus

set O_NONBLOCK


# 1.54 01-May-2000 markus

unbreak, ok niels@


# 1.53 01-May-2000 markus

EINTR


# 1.52 01-May-2000 markus

init all fds, close all fds.


# 1.51 28-Apr-2000 markus

support for x11-fwding, client+server


# 1.50 16-Apr-2000 markus

fix pr 1196, listen_port and port_to_connect interchanged


# 1.49 14-Apr-2000 markus

whitespace cleanup


# 1.48 14-Apr-2000 markus

check payload for (illegal) extra data


# 1.47 10-Apr-2000 markus

repair x11-fwd


# 1.46 06-Apr-2000 markus

no adjust after close


# 1.45 04-Apr-2000 markus

close efd on eof


# 1.44 03-Apr-2000 markus

channel layer support for ssh2


# 1.43 28-Mar-2000 markus

typo


# 1.42 28-Mar-2000 markus

missing close


# 1.41 28-Mar-2000 markus

replace big switch() with function tables (prepare for ssh2)


# 1.40 27-Mar-2000 markus

allow bigger packets


# 1.39 16-Mar-2000 markus

-pedantic: signed vs. unsigned, void*-arithm, etc


# 1.38 24-Jan-2000 markus

do not connect() if request has been denied.


# 1.37 10-Jan-2000 markus

discard data for channel if state != CHAN_OUTPUT_OPEN, fixes lockup


# 1.36 04-Jan-2000 markus

listen on _all_ interfaces for X11-Fwd (hints.ai_flags = AI_PASSIVE)


# 1.35 04-Jan-2000 markus

ipv6 support: mostly gethostbyname->getaddrinfo/getnameinfo, new features:
sshd allows multiple ListenAddress and Port options. note that libwrap is
not IPv6-ready. (based on patches from <kick@kyoto.wide.ad.jp> and
fujiwara@rcac.tdi.co.jp)


# 1.34 27-Dec-1999 markus

use packet_get_maxsize for channels. consistence.


# 1.33 12-Dec-1999 markus

type conflict for 'extern Type *options' in channels.c; dot@dotat.at


# 1.32 06-Dec-1999 deraadt

display great hatred towards strcpy


# 1.31 01-Dec-1999 markus

ports are u_short


# 1.30 25-Nov-1999 deraadt

fix type


# 1.29 24-Nov-1999 markus

fix packet_integrity_check() for !have_hostname_in_open.
report from mrwizard@psu.edu via djm@ibs.com.au


# 1.28 24-Nov-1999 markus

set SO_REUSEADDR and SO_LINGER for forwarded ports.
chip@valinux.com via damien@ibs.com.au


# 1.27 24-Nov-1999 markus

KNF, final part 3


# 1.26 24-Nov-1999 deraadt

much more KNF


# 1.25 23-Nov-1999 markus

KNF part 1


# 1.24 22-Nov-1999 markus

syslog changes:
* Unified Logmessage for all auth-types, for success and for failed
* Standard connections get only ONE line in the LOG when level==LOG:
Auth-attempts are logged only, if authentication is:
a) successfull or
b) with passwd or
c) we had more than AUTH_FAIL_LOG failues
* many log() became verbose()
* old behaviour with level=VERBOSE


# 1.23 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@
[hope this time my ISP stays alive during commit]


# 1.22 19-Nov-1999 deraadt

make this compile, bad markus


# 1.21 19-Nov-1999 markus

SSH_CMSG_MAX_PACKET_SIZE, some clients use this, some need this, niels@


# 1.20 11-Nov-1999 markus

make all access to options via 'extern Options options'
and 'extern ServerOptions options' respectively;
options are no longer passed as arguments:
* make options handling more consistent
* remove #include "readconf.h" from ssh.h
* readconf.h is only included if necessary


# 1.19 02-Nov-1999 markus

replace assert() with error, fatal or packet_disconnect


# 1.18 28-Oct-1999 markus

remove broken x11 fix and document istate/ostate


# 1.17 26-Oct-1999 markus

more usefull debug messages and simplify channel alloc code


Revision tags: OPENBSD_2_6_BASE
# 1.16 17-Oct-1999 markus

re-implement the proto-1.5 channel close protocol, see nchan.ms.


# 1.15 16-Oct-1999 deraadt

snprintf


# 1.14 16-Oct-1999 markus

support for SSH protocol 1.5 which is poorly documented, the RFC.troff lies.
interops (x11,agent,etc) with 1.2.27 and protocol 1.3


# 1.13 14-Oct-1999 markus

fix old connect() race security-bug for ssh-agent and agent-forwarding
by removing the connect() junk, with the following restrictions:
1) change the version to "OpenSSH-1.1":
agent-forwarding will work only between OpenSSH-1.1 client and
OpenSSH-1.1 server
2) renamed the environment variable of OpenSSH-1.1 to
"SSH_AUTH_SOCKET", since useing OpenSSH-1.0 ssh-add against the new
ssh-agent does not work


# 1.12 05-Oct-1999 markus

move auth-sockets to private dir
delete minfd residua


# 1.11 04-Oct-1999 markus

nuke genminfd/AUTH_FD


# 1.10 03-Oct-1999 deraadt

use SHUT_* symbols


# 1.9 30-Sep-1999 deraadt

even smaller


# 1.8 30-Sep-1999 deraadt

IPPORT_RESERVED


# 1.7 30-Sep-1999 deraadt

do not bother with dinosaur pacification


# 1.6 29-Sep-1999 deraadt

numerous sprintf, strncpy, strcpy cleanups


# 1.5 29-Sep-1999 dugsong

update krb4/AFS support to ssh-1.2.27-afs-kerberos-pl1 level, clean up unused variables, update manpages


# 1.4 29-Sep-1999 deraadt

GatewayPorts and ssh -g; markus.friedl@informatik.uni-erlangen.de


# 1.3 28-Sep-1999 deraadt

X11DisplayOffset; aaron


# 1.2 28-Sep-1999 provos

convert all uses of gmp to SSL bignum
convert all used of rsa to SSL rsa functions
remove all use of randomstate to OpenBSD arc4random() and arc4_stir()
all this done at a long long night in Canada.


# 1.1 26-Sep-1999 deraadt

i bet a lot of people didn't know what ssh 1.2.16 had a nice license.
well, except for the patent issues. someone in sweden (forget their
name at the moment) cleaned out most of the patented code, and now
this code removes rsa code. when this is done, it will link against
libssl, but the work isn't completely done yet. then we need to bring
this up to modern days, featurewise.