tests: introduce pkd_hello

Introduce a sample public-key testing daemon to the 'pkd' test directory,
and add support code for cycling through various combinations of different
key exchange, cipher, and MAC algorithms.

The goal of the 'pkd_hello' test is to make it easy to test interactions
between non-libssh clients and a libssh-server, and to provide a starting
point for testing new implementations for key types, ciphers, MACs, and
so on.  The thinking is that testing a new algorithm should be as simple
as adding a new line for it in the PKDTESTS_* lists.

Macros are used to generate the tests and helper functions for a couple of
clients -- here, OpenSSH and dropbear are included for the first cut.  If
binaries are found for these clients, their test lists will be enabled;
when binaries are not found for a given client, those tests are skipped.

Tests are run in one large batch by default, but can also be run individually
to help with tracking down things like signature bugs that may take many
iterations to reproduce.

Each test logs its stdout and stderr to its own file, which is cleaned up
when a test succeeds.  For failures, those logs can be combined with verbose
libssh output from pkd itself to start debugging things.

Some example usages:

  pkd_hello
    Run all tests with default number of iterations.

  pkd_hello --list
    List available individual test names.

  pkd_hello -i 1000 -t torture_pkd_openssh_ecdsa_256_ecdh_sha2_nistp256
    Run only the torture_pkd_openssh_ecdsa_256_ecdh_sha2_nistp256
    testcase 1000 times.

  pkd_hello -v -v -v -v -e -o
    Run all tests with maximum libssh and pkd logging.

Included in the tests are passes for all existing kex, cipher, and MAC
algorithms.

BUG: https://red.libssh.org/issues/144

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Check return code of connect(2).

Signed-off-by: William Orr <will@worrbase.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
gssapi: ssh_gssapi_set_creds() is a client side function

It should not be guarded by the WITH_SERVER #ifdef

Signed-off-by: Stef Walter <stefw@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
messages: emit pubkey failures to callbacks

With this change, errors encountered while processing public keys
in 'ssh_packet_userauth_request' will be propagated back up to the
user-provided 'auth_pubkey_function' callback.

For example, if signature validation fails, the user callback will
be invoked with a signature state of SSH_PUBLICKEY_STATE_WRONG.

Before this change, a failure in signature validation would not be
made explicit via the 'ssh_server_callbacks'.

Conflicts:
	src/messages.c
channel: add ssh_channel_write timeout family

Conflicts:
	include/libssh/channels.h
	src/channels.c
buffer: always start off with BUFFER_INIT_SIZE

Initialize all new buffers with a BUFFER_INIT_SIZE allocation right
off the bat, to avoid later byte-by-byte repeated realloc calls.
buffer: never realloc down in buffer_reinit

Never realloc buffers down to 127 bytes in buffer_reinit: during large
transmits, this causes a "realloc ping-pong" with the session out buffer
constantly being realloc'd down to 127 bytes, just to be realloc'd back
up to 32 kilobytes for the next packet send (for example).

Were the realloc behavior desired in buffer_reinit, it would be better
to issue a free followed by malloc (as-is, 127 bytes that we don't care
about are memmove'd to the new buffer).
bind: add selective use of XCLOSE
socket: add selective use of XCLOSE
channel: is_local_eof|closed getters

Given an invocation to 'ssh_channel_close', upon SSH_AGAIN, there
is currently no way to know whether the SSH_AGAIN means that EOF
has been totally sent and CLOSE message bytes have been enqueued,
or whether sending and flushing EOF needs to be retried, and CLOSE
message bytes are yet to be queued.

Expose getters for 'local_eof' and the new field 'local_closed'.
Until the latter is set, it is not the case the CLOSE message
bytes have been enqueued to the outgoing session socket.

Motivation: when unable to distinguish the above ssh_channel_close
cases, clients either have to resort to missing a channel close, or
possibly sending more than one CLOSE message for a given channel.
Both of those options are bad for the case of session multiplexing
(for example, OpenSSH might indefinitely hang if it never sees a
corresponding CLOSE response for a closed channel; likewise, it
may abruptly disconnect if it receives a duplicate CLOSE or a
CLOSE for an unknown channel).
channel: DISABLE_AUTH_AGENT_REQ test

Conflicts:
	src/channels.c
session: check session error in blocking_flush
session: add get_in|outbuffer_rest_len helpers
pki_crypto.c: plug ecdsa_sig->[r,s] bignum leaks

Per ecdsa(3ssl), ECDSA_SIG_new does allocate its 'r' and 's' bignum fields.
Fix a bug where the initial 'r' and 's' bignums were being overwritten with
newly-allocated bignums, resulting in a memory leak.  Observable by running
the pkd tests with 'valgrind --leak-check=full'; with the fix, the leak is
absent.
ConfigureChecks.cmake: fix htonll/ntohll checks

Fixes the build on OSX Yosemite, Linux build remains OK.

See also:
 * https://red.libssh.org/issues/164
 * https://red.libssh.org/issues/174
Revert "Check return code of connect(2)."

This reverts commit 250f50648755415a138b1347a17a367ad384b66d.