From 4f1cfb20a98c85a4cd741f34bc69662bc62ff510 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 2 Feb 2007 23:23:36 +0000 Subject: [PATCH] 1 - new public function libssh2_trace() that enables tracing/debug output on various stuff if libssh2 was built with debug. If built without debug, the function does nothing. 2 - configure --enable-debug is now enough to build a debug version (including picky compiler options) 3 - internally, we no longer need/use #ifdef/#endif around all uses of the _libssh2_debug() function/macro. The scp.c example is the first application to test this new debug logging. --- configure.in | 48 ++----------------------- example/simple/scp.c | 5 ++- include/libssh2.h | 14 ++++++++ src/channel.c | 85 ++++++++++++-------------------------------- src/kex.c | 49 ++++++------------------- src/libssh2_priv.h | 44 ++++++++++++++--------- src/misc.c | 51 +++++++++++++++++--------- src/packet.c | 52 +++++++-------------------- src/publickey.c | 32 +++++------------ src/scp.c | 16 ++------- src/session.c | 19 +--------- src/sftp.c | 50 +------------------------- src/transport.c | 22 +++++++----- src/userauth.c | 35 ++---------------- 14 files changed, 158 insertions(+), 364 deletions(-) diff --git a/configure.in b/configure.in index ca568ff..02da03b 100644 --- a/configure.in +++ b/configure.in @@ -195,55 +195,13 @@ if test "$GEX_NEW" != "no"; then AC_DEFINE(LIBSSH2_DH_GEX_NEW, 1, [Enable newer diffie-hellman-group-exchange-sha1 syntax]) fi -# -# Optional debugging -- Meant for developer maintenance only -# When enabled, the relevant debugging information will be written on stderr -# -AC_ARG_ENABLE(debug-transport, - AC_HELP_STRING([--enable-debug-transport],[Output transport layer debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_TRANSPORT, 1, [Output transport layer debugging info to stderr])]) -AC_ARG_ENABLE(debug-kex, - AC_HELP_STRING([--enable-debug-kex],[Output Key Exchange debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_KEX, 1, [Output Key Exchange debugging info to stderr])]) -AC_ARG_ENABLE(debug-userauth, - AC_HELP_STRING([--enable-debug-userauth],[Output userauth debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_USERAUTH, 1, [Output userauth layer debugging info to stderr])]) -AC_ARG_ENABLE(debug-channel, - AC_HELP_STRING([--enable-debug-connection],[Output connection layer debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_CONNECTION, 1, [Output connection layer debugging info to stderr])]) -AC_ARG_ENABLE(debug-scp, - AC_HELP_STRING([--enable-debug-scp],[Output scp subsystem debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_SCP, 1, [Output scp subsystem debugging info to stderr])]) -AC_ARG_ENABLE(debug-sftp, - AC_HELP_STRING([--enable-debug-sftp],[Output sftp subsystem debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_SFTP, 1, [Output sftp subsystem debugging info to stderr])]) -AC_ARG_ENABLE(debug-publickey, - AC_HELP_STRING([--enable-debug-publickey],[Output publickey subsystem debugging info to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_PUBLICKEY, 1, [Output publickey subsystem debugging info to stderr])]) -AC_ARG_ENABLE(debug-errors, - AC_HELP_STRING([--enable-debug-errors],[Output failure events to stderr]), - [AC_DEFINE(LIBSSH2_DEBUG_ERRORS, 1, [Output failure events to stderr])]) -AC_ARG_ENABLE(debug-all, - AC_HELP_STRING([--enable-debug],[Enable debug]), - [ - AC_DEFINE(LIBSSH2_DEBUG_TRANSPORT, 1, [Output transport layer debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_KEX, 1, [Output Key Exchange debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_USERAUTH, 1, [Output userauth layer debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_CONNECTION, 1, [Output connection layer debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_SCP, 1, [Output scp subsystem debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_SFTP, 1, [Output sftp subsystem debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_PUBLICKEY, 1, [Output publickey subsystem debugging info to stderr]) - AC_DEFINE(LIBSSH2_DEBUG_ERRORS, 1, [Output failure events to stderr]) - ]) - - dnl ************************************************************ dnl option to switch on compiler debug options dnl AC_MSG_CHECKING([whether to enable pedantic and debug compiler options]) -AC_ARG_ENABLE(debug-build, -AC_HELP_STRING([--enable-debug-build],[Enable pedantic debug options]) -AC_HELP_STRING([--disable-debug-build],[Disable debug options]), +AC_ARG_ENABLE(debug, +AC_HELP_STRING([--enable-debug],[Enable pedantic and debug options]) +AC_HELP_STRING([--disable-debug],[Disable debug options]), [ case "$enableval" in no) AC_MSG_RESULT(no) diff --git a/example/simple/scp.c b/example/simple/scp.c index d787a6e..784c67b 100644 --- a/example/simple/scp.c +++ b/example/simple/scp.c @@ -1,5 +1,5 @@ /* - * $Id: scp.c,v 1.3 2007/02/01 22:39:45 bagder Exp $ + * $Id: scp.c,v 1.4 2007/02/02 23:23:36 bagder Exp $ * * Sample showing how to do a simple SCP transfer. */ @@ -61,6 +61,9 @@ int main(int argc, char *argv[]) if(!session) return -1; + /* trace transport layer stuff*/ + libssh2_trace(session, LIBSSH2_TRACE_TRANS); + /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ diff --git a/include/libssh2.h b/include/libssh2.h index 6bb3f73..54b91cd 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -417,6 +417,20 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session, const LIBSSH2_API int libssh2_base64_decode(LIBSSH2_SESSION *session, char **dest, unsigned int *dest_len, char *src, unsigned int src_len); +/* NOTE NOTE NOTE + libssh2_trace() has no function in builds that aren't built with debug + enabled + */ +LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask); +#define LIBSSH2_TRACE_TRANS (1<<1) +#define LIBSSH2_TRACE_KEX (1<<2) +#define LIBSSH2_TRACE_AUTH (1<<3) +#define LIBSSH2_TRACE_CONN (1<<4) +#define LIBSSH2_TRACE_SCP (1<<5) +#define LIBSSH2_TRACE_SFTP (1<<6) +#define LIBSSH2_TRACE_ERROR (1<<7) +#define LIBSSH2_TRACE_PUBLICKEY (1<<8) + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/channel.c b/src/channel.c index 131fe21..1dddd6c 100644 --- a/src/channel.c +++ b/src/channel.c @@ -66,9 +66,7 @@ unsigned long libssh2_channel_nextid(LIBSSH2_SESSION *session) * Gets picked up by the new one.... Pretty unlikely all told... */ session->next_channel = id + 1; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Allocated new channel ID#%lu", id); -#endif return id; } /* }}} */ @@ -131,11 +129,9 @@ libssh2_channel_open_ex(LIBSSH2_SESSION *session, const char *channel_type, unsigned char *data = NULL; unsigned long data_len; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Opening Channel - win %d pack %d", window_size, packet_size); -#endif channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); if (!channel) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, @@ -206,13 +202,11 @@ libssh2_channel_open_ex(LIBSSH2_SESSION *session, const char *channel_type, channel->local.window_size = libssh2_ntohu32(data + 9); channel->local.window_size_initial = libssh2_ntohu32(data + 9); channel->local.packet_size = libssh2_ntohu32(data + 13); -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Connection Established - ID: %lu/%lu win: %lu/%lu pack: %lu/%lu", channel->local.id, channel->remote.id, channel->local.window_size, channel->remote.window_size, channel->local.packet_size, channel->remote.packet_size); -#endif LIBSSH2_FREE(session, packet); LIBSSH2_FREE(session, data); @@ -277,9 +271,7 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *se unsigned long host_len = strlen(host), shost_len = strlen(shost); unsigned long message_len = host_len + shost_len + 16; /* host_len(4) + port(4) + shost_len(4) + sport(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Requesting direct-tcpip session to from %s:%d to %s:%d", shost, sport, host, port); -#endif s = message = LIBSSH2_ALLOC(session, message_len); if (!message) { @@ -316,9 +308,7 @@ LIBSSH2_API LIBSSH2_LISTENER *libssh2_channel_forward_listen_ex(LIBSSH2_SESSION unsigned long packet_len = host_len + (sizeof("tcpip-forward") - 1) + 14; /* packet_type(1) + request_len(4) + want_replay(1) + host_len(4) + port(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Requesting tcpip-forward session for %s:%d", host, port); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { @@ -368,9 +358,9 @@ LIBSSH2_API LIBSSH2_LISTENER *libssh2_channel_forward_listen_ex(LIBSSH2_SESSION listener->host[host_len] = 0; if (data_len >= 5 && !port) { listener->port = libssh2_ntohu32(data + 1); -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(session, LIBSSH2_DBG_CONN, "Dynamic tcpip-forward port allocated: %d", listener->port); -#endif + _libssh2_debug(session, LIBSSH2_DBG_CONN, + "Dynamic tcpip-forward port allocated: %d", + listener->port); } else { listener->port = port; } @@ -416,9 +406,7 @@ LIBSSH2_API int libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener) unsigned long packet_len = host_len + 14 + sizeof("cancel-tcpip-forward") - 1; /* packet_type(1) + request_len(4) + want_replay(1) + host_len(4) + port(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Cancelling tcpip-forward session for %s:%d", listener->host, listener->port); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { @@ -521,9 +509,7 @@ LIBSSH2_API int libssh2_channel_setenv_ex(LIBSSH2_CHANNEL *channel, char *varnam unsigned long packet_len = varname_len + value_len + 21; /* packet_type(1) + channel_id(4) + request_len(4) + request(3)"env" + want_reply(1) + varname_len(4) + value_len(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Setting remote environment variable: %s=%s on channel %lu/%lu", varname, value, channel->local.id, channel->remote.id); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { @@ -582,9 +568,7 @@ LIBSSH2_API int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel, char *t unsigned long packet_len = term_len + modes_len + 41; /* packet_type(1) + channel(4) + pty_req_len(4) + "pty_req"(7) + want_reply(1) + term_len(4) + width(4) + height(4) + width_px(4) + height_px(4) + modes_len(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Allocating tty on channel %lu/%lu", channel->local.id, channel->remote.id); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { @@ -652,12 +636,10 @@ LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel, int single_ unsigned long packet_len = proto_len + cookie_len + 30; /* packet_type(1) + channel(4) + x11_req_len(4) + "x11-req"(7) + want_reply(1) + single_cnx(1) + proto_len(4) + cookie_len(4) + screen_num(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Requesting x11-req for channel %lu/%lu: single=%d proto=%s cookie=%s screen=%d", channel->local.id, channel->remote.id, single_connection, auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", auth_cookie ? auth_cookie : "", screen_number); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { @@ -732,10 +714,7 @@ LIBSSH2_API int libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, const packet_len += message_len + 4; } -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "starting request(%s) on channel %lu/%lu, message=%s", request, channel->local.id, channel->remote.id, message); -#endif - s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for channel-process request", 0); @@ -787,11 +766,9 @@ int _libssh2_channel_set_blocking(LIBSSH2_CHANNEL *channel, { int rc; int bl = channel->blocking; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(channel->session, LIBSSH2_DBG_CONN, "Setting blocking mode on channel %lu/%lu to %d", channel->local.id, channel->remote.id, blocking); -#endif if(blocking == channel->blocking) { /* avoid if already correct */ return bl; @@ -864,10 +841,14 @@ LIBSSH2_API int libssh2_channel_flush_ex(LIBSSH2_CHANNEL *channel, int streamid) (streamid == 0))) { int bytes_to_flush = packet->data_len - packet->data_head; -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(channel->session, LIBSSH2_DBG_CONN, "Flushing %d bytes of data from stream %lu on channel %lu/%lu", bytes_to_flush, - packet_stream_id, channel->local.id, channel->remote.id); -#endif + _libssh2_debug(channel->session, + LIBSSH2_DBG_CONN, + "Flushing %d bytes of data from " + "stream %lu on channel %lu/%lu", + bytes_to_flush, + packet_stream_id, + channel->local.id, + channel->remote.id); /* It's one of the streams we wanted to flush */ refund_bytes += packet->data_len - 13; @@ -920,9 +901,7 @@ LIBSSH2_API unsigned long libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL unsigned char adjust[9]; /* packet_type(1) + channel(4) + adjustment(4) */ if (!force && (adjustment + channel->adjust_queue < LIBSSH2_CHANNEL_MINADJUST)) { -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(channel->session, LIBSSH2_DBG_CONN, "Queing %lu bytes for receive window adjustment for channel %lu/%lu", adjustment, channel->local.id, channel->remote.id); -#endif channel->adjust_queue += adjustment; return channel->remote.window_size; } @@ -939,9 +918,7 @@ LIBSSH2_API unsigned long libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL adjust[0] = SSH_MSG_CHANNEL_WINDOW_ADJUST; libssh2_htonu32(adjust + 1, channel->remote.id); libssh2_htonu32(adjust + 5, adjustment); -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(channel->session, LIBSSH2_DBG_CONN, "Adjusting window %lu bytes for data flushed from channel %lu/%lu", adjustment, channel->local.id, channel->remote.id); -#endif if (libssh2_packet_write(channel->session, adjust, 9)) { libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send transfer-window adjustment packet, deferring", 0); @@ -962,9 +939,7 @@ LIBSSH2_API unsigned long libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL */ LIBSSH2_API void libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode) { -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(channel->session, LIBSSH2_DBG_CONN, "Setting channel %lu/%lu handle_extended_data mode to %d", channel->local.id, channel->remote.id, ignore_mode); -#endif channel->remote.extended_data_ignore_mode = ignore_mode; if (ignore_mode == LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE) { @@ -991,12 +966,10 @@ ssize_t _libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, int bl; int block=0; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Attempting to read %d bytes from channel %lu/%lu stream #%d", (int)buflen, channel->local.id, channel->remote.id, stream_id); -#endif /* set non-blocking and remember previous state */ bl = _libssh2_channel_set_blocking(channel, 0); @@ -1060,9 +1033,10 @@ ssize_t _libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, unlink_packet = 1; } -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(session, LIBSSH2_DBG_CONN, "Reading %d of buffered data from %lu/%lu/%d", want, channel->local.id, channel->remote.id, stream_id); -#endif + _libssh2_debug(session, LIBSSH2_DBG_CONN, + "Reading %d of buffered data from %lu/%lu/%d", + want, channel->local.id, + channel->remote.id, stream_id); memcpy(buf + bytes_read, packet->data + packet->data_head, want); packet->data_head += want; bytes_read += want; @@ -1080,9 +1054,12 @@ ssize_t _libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, } LIBSSH2_FREE(session, packet->data); -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(session, LIBSSH2_DBG_CONN, "Unlinking empty packet buffer from channel %lu/%lu", channel->local.id, channel->remote.id); -#endif + + _libssh2_debug(session, + LIBSSH2_DBG_CONN, + "Unlinking empty packet buffer from channel %lu/%lu", + channel->local.id, channel->remote.id); + libssh2_channel_receive_window_adjust(channel, packet->data_len - (stream_id ? 13 : 9), 0); LIBSSH2_FREE(session, packet); } @@ -1166,9 +1143,7 @@ int _libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel, unsigned char *packet; unsigned long packet_len, bufwrote = 0; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Writing %d bytes on channel %lu/%lu, stream #%d", (int)buflen, channel->local.id, channel->remote.id, stream_id); -#endif if (channel->local.close) { libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED, "We've already closed this channel", 0); return -1; @@ -1220,23 +1195,17 @@ int _libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel, /* Don't exceed the remote end's limits */ /* REMEMBER local means local as the SOURCE of the data */ if (bufwrite > channel->local.window_size) { -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(session, LIBSSH2_DBG_CONN, "Splitting write block due to %lu byte window_size on %lu/%lu/%d", channel->local.window_size, channel->local.id, channel->remote.id, stream_id); -#endif + _libssh2_debug(session, LIBSSH2_DBG_CONN, "Splitting write block due to %lu byte window_size on %lu/%lu/%d", channel->local.window_size, channel->local.id, channel->remote.id, stream_id); bufwrite = channel->local.window_size; } if (bufwrite > channel->local.packet_size) { -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(session, LIBSSH2_DBG_CONN, "Splitting write block due to %lu byte packet_size on %lu/%lu/%d", channel->local.packet_size, channel->local.id, channel->remote.id, stream_id); -#endif + _libssh2_debug(session, LIBSSH2_DBG_CONN, "Splitting write block due to %lu byte packet_size on %lu/%lu/%d", channel->local.packet_size, channel->local.id, channel->remote.id, stream_id); bufwrite = channel->local.packet_size; } libssh2_htonu32(s, bufwrite); s += 4; memcpy(s, buf, bufwrite); s += bufwrite; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Sending %d bytes on channel %lu/%lu, stream_id=%d", (int)bufwrite, channel->local.id, channel->remote.id, stream_id); -#endif rc = libssh2_packet_write(session, packet, s - packet); if(rc) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send channel data", 0); @@ -1314,9 +1283,7 @@ LIBSSH2_API int libssh2_channel_send_eof(LIBSSH2_CHANNEL *channel) LIBSSH2_SESSION *session = channel->session; unsigned char packet[5]; /* packet_type(1) + channelno(4) */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Sending EOF on channel %lu/%lu",channel->local.id, channel->remote.id); -#endif packet[0] = SSH_MSG_CHANNEL_EOF; libssh2_htonu32(packet + 1, channel->remote.id); if (libssh2_packet_write(session, packet, 5)) { @@ -1363,9 +1330,7 @@ LIBSSH2_API int libssh2_channel_close(LIBSSH2_CHANNEL *channel) return 0; } -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Closing channel %lu/%lu", channel->local.id, channel->remote.id); -#endif if (channel->close_cb) { LIBSSH2_CHANNEL_CLOSE(session, channel); @@ -1397,9 +1362,7 @@ LIBSSH2_API int libssh2_channel_wait_closed(LIBSSH2_CHANNEL *channel) return -1; } -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Awaiting close of channel %lu/%lu", channel->local.id, channel->remote.id); -#endif /* while channel is not closed, read more * packets from the network. @@ -1423,9 +1386,7 @@ LIBSSH2_API int libssh2_channel_free(LIBSSH2_CHANNEL *channel) unsigned char channel_id[4], *data; unsigned long data_len; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Freeing channel %lu/%lu resources", channel->local.id, channel->remote.id); -#endif /* Allow channel freeing even when the socket has lost its connection */ if (!channel->local.close && (session->socket_state == LIBSSH2_SOCKET_CONNECTED) && libssh2_channel_close(channel)) { diff --git a/src/kex.c b/src/kex.c index 4a0f138..0e9290c 100644 --- a/src/kex.c +++ b/src/kex.c @@ -106,9 +106,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S _libssh2_bn_to_bin(e, e_packet + 6); } -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Sending KEX packet %d", (int)packet_type_init); -#endif rc = libssh2_packet_write(session, e_packet, e_packet_len); if (rc) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send KEX init message", 0); @@ -120,9 +118,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S /* The first KEX packet to come along will be the guess initially sent by the server * That guess turned out to be wrong so we need to silently ignore it */ int burn_type; -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Waiting for badly guessed KEX packet (to be ignored)"); -#endif burn_type = libssh2_packet_burn(session); if (burn_type <= 0) { /* Failed to receive a packet */ @@ -131,9 +127,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S } session->burn_optimistic_kexinit = 0; -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Burnt packet of type: %02x", (unsigned int)burn_type); -#endif } /* Wait for KEX reply */ @@ -167,7 +161,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S libssh2_md5_update(fingerprint_ctx, session->server_hostkey, session->server_hostkey_len); libssh2_md5_final(fingerprint_ctx, session->server_hostkey_md5); } -#ifdef LIBSSH2_DEBUG_KEX +#ifdef LIBSSH2DEBUG { char fingerprint[50], *fprint = fingerprint; int i; @@ -177,7 +171,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S *(--fprint) = '\0'; _libssh2_debug(session, LIBSSH2_DBG_KEX, "Server's MD5 Fingerprint: %s", fingerprint); } -#endif /* LIBSSH2_DEBUG_KEX */ +#endif /* LIBSSH2DEBUG */ #endif /* ! LIBSSH2_MD5 */ { @@ -187,7 +181,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S libssh2_sha1_update (fingerprint_ctx, session->server_hostkey, session->server_hostkey_len); libssh2_sha1_final(fingerprint_ctx, session->server_hostkey_sha1); } -#ifdef LIBSSH2_DEBUG_KEX +#ifdef LIBSSH2DEBUG { char fingerprint[64], *fprint = fingerprint; int i; @@ -197,7 +191,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S *(--fprint) = '\0'; _libssh2_debug(session, LIBSSH2_DBG_KEX, "Server's SHA1 Fingerprint: %s", fingerprint); } -#endif /* LIBSSH2_DEBUG_KEX */ +#endif /* LIBSSH2DEBUG */ if (session->hostkey->init(session, session->server_hostkey, session->server_hostkey_len, &session->server_hostkey_abstract)) { libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, "Unable to initialize hostkey importer", 0); @@ -297,9 +291,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S goto clean_exit; } -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Sending NEWKEYS message"); -#endif c = SSH_MSG_NEWKEYS; if (libssh2_packet_write(session, &c, 1)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send NEWKEYS message", 0); @@ -314,9 +306,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S } /* The first key exchange has been performed, switch to active crypt/comp/mac mode */ session->state |= LIBSSH2_STATE_NEWKEYS; -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Received NEWKEYS message"); -#endif /* This will actually end up being just packet_type(1) for this packet type anyway */ LIBSSH2_FREE(session, tmp); @@ -329,9 +319,8 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S } memcpy(session->session_id, h_sig_comp, SHA_DIGEST_LENGTH); session->session_id_len = SHA_DIGEST_LENGTH; -#ifdef LIBSSH2_DEBUG_KEX - _libssh2_debug(session, LIBSSH2_DBG_KEX, "session_id calculated"); -#endif + _libssh2_debug(session, LIBSSH2_DBG_KEX, + "session_id calculated"); } /* Cleanup any existing cipher */ @@ -363,9 +352,8 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S LIBSSH2_FREE(session, secret); } } -#ifdef LIBSSH2_DEBUG_KEX - _libssh2_debug(session, LIBSSH2_DBG_KEX, "Client to Server IV and Key calculated"); -#endif + _libssh2_debug(session, LIBSSH2_DBG_KEX, + "Client to Server IV and Key calculated"); if (session->remote.crypt->dtor) { /* Cleanup any existing cipher */ @@ -395,9 +383,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S LIBSSH2_FREE(session, secret); } } -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Server to Client IV and Key calculated"); -#endif if (session->local.mac->dtor) { session->local.mac->dtor(session, &session->local.mac_abstract); @@ -415,9 +401,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S LIBSSH2_FREE(session, key); } } -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Client to Server HMAC Key calculated"); -#endif if (session->remote.mac->dtor) { session->remote.mac->dtor(session, &session->remote.mac_abstract); @@ -435,9 +419,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S LIBSSH2_FREE(session, key); } } -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Server to Client HMAC Key calculated"); -#endif clean_exit: _libssh2_bn_free(x); @@ -498,9 +480,8 @@ static int libssh2_kex_method_diffie_hellman_group1_sha1_key_exchange(LIBSSH2_SE _libssh2_bn_set_word(g, 2); _libssh2_bn_from_bin(p, 128, p_value); -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Initiating Diffie-Hellman Group1 Key Exchange"); -#endif + ret = libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(session, g, p, 128, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY, NULL, 0); _libssh2_bn_free(p); @@ -557,9 +538,7 @@ static int libssh2_kex_method_diffie_hellman_group14_sha1_key_exchange(LIBSSH2_S _libssh2_bn_set_word(g, 2); _libssh2_bn_from_bin(p, 256, p_value); -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Initiating Diffie-Hellman Group14 Key Exchange"); -#endif ret = libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(session, g, p, 256, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY, NULL, 0); _libssh2_bn_free(p); @@ -588,16 +567,12 @@ static int libssh2_kex_method_diffie_hellman_group_exchange_sha1_key_exchange(LI libssh2_htonu32(request + 5, LIBSSH2_DH_GEX_OPTGROUP); libssh2_htonu32(request + 9, LIBSSH2_DH_GEX_MAXGROUP); request_len = 13; -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Initiating Diffie-Hellman Group-Exchange (New Method)"); -#endif #else request[0] = SSH_MSG_KEX_DH_GEX_REQUEST_OLD; libssh2_htonu32(request + 1, LIBSSH2_DH_GEX_OPTGROUP); request_len = 5; -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Initiating Diffie-Hellman Group-Exchange (Old Method)"); -#endif #endif if (libssh2_packet_write(session, request, request_len)) { @@ -786,7 +761,7 @@ static int libssh2_kexinit(LIBSSH2_SESSION *session) *(s++) = 0; *(s++) = 0; -#ifdef LIBSSH2_DEBUG_KEX +#ifdef LIBSSH2DEBUG { /* Funnily enough, they'll all "appear" to be '\0' terminated */ char *p = data + 21; /* type(1) + cookie(16) + len(4) */ @@ -802,7 +777,7 @@ static int libssh2_kexinit(LIBSSH2_SESSION *session) _libssh2_debug(session, LIBSSH2_DBG_KEX, "Sent LANG_CS: %s", p); p += lang_cs_len + 4; _libssh2_debug(session, LIBSSH2_DBG_KEX, "Sent LANG_SC: %s", p); p += lang_sc_len + 4; } -#endif /* LIBSSH2_DEBUG_KEX */ +#endif /* LIBSSH2DEBUG */ if (libssh2_packet_write(session, data, data_len)) { LIBSSH2_FREE(session, data); libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send KEXINIT packet to remote host", 0); @@ -1209,7 +1184,6 @@ static int libssh2_kex_agree_methods(LIBSSH2_SESSION *session, unsigned char *da return -1; } -#ifdef LIBSSH2_DEBUG_KEX _libssh2_debug(session, LIBSSH2_DBG_KEX, "Agreed on KEX method: %s", session->kex->name); _libssh2_debug(session, LIBSSH2_DBG_KEX, "Agreed on HOSTKEY method: %s", session->hostkey->name); _libssh2_debug(session, LIBSSH2_DBG_KEX, "Agreed on CRYPT_CS method: %s", session->local.crypt->name); @@ -1220,7 +1194,6 @@ static int libssh2_kex_agree_methods(LIBSSH2_SESSION *session, unsigned char *da _libssh2_debug(session, LIBSSH2_DBG_KEX, "Agreed on COMP_SC method: %s", session->remote.comp->name); _libssh2_debug(session, LIBSSH2_DBG_KEX, "Agreed on LANG_CS method:"); /* None yet */ _libssh2_debug(session, LIBSSH2_DBG_KEX, "Agreed on LANG_SC method:"); /* None yet */ -#endif /* Initialize compression layer */ if (session->local.comp && session->local.comp->init && diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index 8891e84..2376615 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -289,6 +289,9 @@ struct _LIBSSH2_SESSION { /* struct members for packet-level reading */ struct transportpacket packet; +#ifdef LIBSSH2DEBUG + int showmask; /* what debug/trace messages to display */ +#endif }; /* session.state bits */ @@ -371,24 +374,31 @@ struct _LIBSSH2_MAC_METHOD { int (*dtor)(LIBSSH2_SESSION *session, void **abstract); }; -#if defined(LIBSSH2_DEBUG_TRANSPORT) || defined(LIBSSH2_DEBUG_KEX) || defined(LIBSSH2_DEBUG_USERAUTH) || defined(LIBSSH2_DEBUG_CONNECTION) || defined(LIBSSH2_DEBUG_SCP) || defined(LIBSSH2_DEBUG_SFTP) || defined(LIBSSH2_DEBUG_ERRORS) -#define LIBSSH2_DEBUG_ENABLED - -/* Internal debugging contexts -- Used with --enable-debug-* */ -#define LIBSSH2_DBG_TRANS 1 -#define LIBSSH2_DBG_KEX 2 -#define LIBSSH2_DBG_AUTH 3 -#define LIBSSH2_DBG_CONN 4 -#define LIBSSH2_DBG_SCP 5 -#define LIBSSH2_DBG_SFTP 6 -#define LIBSSH2_DBG_ERROR 7 -#define LIBSSH2_DBG_PUBLICKEY 8 - +#ifdef LIBSSH2DEBUG void _libssh2_debug(LIBSSH2_SESSION *session, int context, const char *format, ...); +#define LIBSSH2_DBG_TRANS 1 +#define LIBSSH2_DBG_KEX 2 +#define LIBSSH2_DBG_AUTH 3 +#define LIBSSH2_DBG_CONN 4 +#define LIBSSH2_DBG_SCP 5 +#define LIBSSH2_DBG_SFTP 6 +#define LIBSSH2_DBG_ERROR 7 +#define LIBSSH2_DBG_PUBLICKEY 8 +#else +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +/* C99 style */ +#define _libssh2_debug(x,y,z, __VA_ARGS__) do {} while (0) +#elif defined(__GNUC__) +/* GNU style */ +#define _libssh2_debug(x,y,z,...) do {} while (0) +#else +/* no gcc and not C99, do static and hopefully inline */ +static inline void _libssh2_debug(LIBSSH2_SESSION *session, int context, + const char *format, ...) {} +#endif +#endif -#endif /* LIBSSH2_DEBUG_ENABLED */ - -#ifdef LIBSSH2_DEBUG_ERRORS +#ifdef LIBSSH2DEBUG #define libssh2_error(session, errcode, errmsg, should_free) \ { \ if (session->err_msg && session->err_should_free) { \ @@ -401,7 +411,7 @@ void _libssh2_debug(LIBSSH2_SESSION *session, int context, const char *format, . _libssh2_debug(session, LIBSSH2_DBG_ERROR, "%d - %s", session->err_code, session->err_msg); \ } -#else /* ! LIBSSH2_DEBUG_ERRORS */ +#else /* ! LIBSSH2DEBUG */ #define libssh2_error(session, errcode, errmsg, should_free) \ { \ diff --git a/src/misc.c b/src/misc.c index 5d22876..bd69a45 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2004-2006, Sara Golemon +/* Copyright (c) 2004-2007, Sara Golemon * All rights reserved. * * Redistribution and use in source and binary forms, @@ -36,6 +36,7 @@ */ #include "libssh2_priv.h" +#include /* {{{ libssh2_ntohu32 */ @@ -170,30 +171,38 @@ LIBSSH2_API int libssh2_base64_decode(LIBSSH2_SESSION *session, char **data, uns } /* }}} */ -#ifdef LIBSSH2_DEBUG_ENABLED -/* {{{ _libssh2_debug - * Internal debug logging facility - * Just writes to stderr until a good reason comes up to support anything else - */ -void _libssh2_debug(LIBSSH2_SESSION *session, int context, const char *format, ...) +#ifdef LIBSSH2DEBUG +LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask) +{ + session->showmask = bitmask; + return 0; +} + +void _libssh2_debug(LIBSSH2_SESSION *session, int context, + const char *format, ...) { char buffer[1536]; int len; va_list vargs; - char *contexts[9] = { "Unknown", - "Transport", - "Key Exhange", - "Userauth", - "Connection", - "scp", - "SFTP Subsystem", - "Failure Event", - "Publickey Subsystem", + static const char *contexts[9] = { + "Unknown", + "Transport", + "Key Exhange", + "Userauth", + "Connection", + "scp", + "SFTP Subsystem", + "Failure Event", + "Publickey Subsystem", }; if (context < 1 || context > 8) { context = 0; } + if(!(session->showmask & (1<port == (int)port) && @@ -115,9 +113,7 @@ inline int libssh2_packet_queue_listener(LIBSSH2_SESSION *session, (l->queue_maxsize <= l->queue_size)) { /* Queue is full */ failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Listener queue full, ignoring"); -#endif break; } @@ -150,12 +146,10 @@ inline int libssh2_packet_queue_listener(LIBSSH2_SESSION *session, channel->local.window_size = initial_window_size; channel->local.packet_size = packet_size; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Connection queued: channel %lu/%lu win %lu/%lu packet %lu/%lu", channel->local.id, channel->remote.id, channel->local.window_size, channel->remote.window_size, channel->local.packet_size, channel->remote.packet_size); -#endif p = packet; *(p++) = SSH_MSG_CHANNEL_OPEN_CONFIRMATION; @@ -233,9 +227,7 @@ inline int libssh2_packet_x11_open(LIBSSH2_SESSION *session, shost = s; s += shost_len; sport = libssh2_ntohu32(s); s += 4; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "X11 Connection Received from %s:%ld on channel %lu", shost, sport, sender_channel); -#endif if (session->x11) { channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); if (!channel) { @@ -266,12 +258,10 @@ inline int libssh2_packet_x11_open(LIBSSH2_SESSION *session, channel->local.window_size = initial_window_size; channel->local.packet_size = packet_size; -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "X11 Connection established: channel %lu/%lu win %lu/%lu packet %lu/%lu", channel->local.id, channel->remote.id, channel->local.window_size, channel->remote.window_size, channel->local.packet_size, channel->remote.packet_size); -#endif p = packet; *(p++) = SSH_MSG_CHANNEL_OPEN_CONFIRMATION; libssh2_htonu32(p, channel->remote.id); p += 4; @@ -331,9 +321,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat LIBSSH2_PACKET *packet; unsigned long data_head = 0; -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Packet type %d received, length=%d", (int)data[0], (int)datalen); -#endif if (macstate == LIBSSH2_MAC_INVALID) { if (session->macerror) { if (LIBSSH2_MACERROR(session, (char *)data, datalen) == 0) { @@ -382,9 +370,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat if (session->ssh_msg_disconnect) { LIBSSH2_DISCONNECT(session, reason, message, message_len, language, language_len); } -#ifdef LIBSSH2_DEBUG_TRANSPORT - _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Disconnect(%d): %s(%s)", reason, message, language); -#endif + _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Disconnect(%d): %s(%s)", reason, message, language); LIBSSH2_FREE(session, data); session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; return -1; @@ -425,10 +411,8 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat if (session->ssh_msg_debug) { LIBSSH2_DEBUG(session, always_display, message, message_len, language, language_len); } -#ifdef LIBSSH2_DEBUG_TRANSPORT /* _libssh2_debug will actually truncate this for us so that it's not an inordinate about of data */ _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Debug Packet: %s", message); -#endif LIBSSH2_FREE(session, data); return 0; } @@ -445,7 +429,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat LIBSSH2_FREE(session, data); return 0; } -#ifdef LIBSSH2_DEBUG_CONNECTION +#ifdef LIBSSH2DEBUG { unsigned long stream_id = 0; @@ -460,9 +444,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat /* Pretend we didn't receive this */ LIBSSH2_FREE(session, data); -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Ignoring extended data and refunding %d bytes", (int)(datalen - 13)); -#endif /* Adjust the window based on the block we just freed */ libssh2_channel_receive_window_adjust(channel, datalen - 13, 0); @@ -503,9 +485,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat return 0; } -#ifdef LIBSSH2_DEBUG_CONNECTION - _libssh2_debug(session, LIBSSH2_DBG_CONN, "EOF received for channel %lu/%lu", channel->local.id, channel->remote.id); -#endif + _libssh2_debug(session, LIBSSH2_DBG_CONN, "EOF received for channel %lu/%lu", channel->local.id, channel->remote.id); channel->remote.eof = 1; LIBSSH2_FREE(session, data); @@ -522,9 +502,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat if (channel) { channel->exit_status = libssh2_ntohu32(data + 9 + sizeof("exit-status")); -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Exit status %lu received for channel %lu/%lu", channel->exit_status, channel->local.id, channel->remote.id); -#endif } LIBSSH2_FREE(session, data); @@ -541,9 +519,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat LIBSSH2_FREE(session, data); return 0; } -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Close received for channel %lu/%lu", channel->local.id, channel->remote.id); -#endif channel->remote.close = 1; channel->remote.eof = 1; @@ -579,9 +555,7 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat if (channel && bytestoadd) { channel->local.window_size += bytestoadd; } -#ifdef LIBSSH2_DEBUG_CONNECTION _libssh2_debug(session, LIBSSH2_DBG_CONN, "Window adjust received for channel %lu/%lu, adding %lu bytes, new window_size=%lu", channel->local.id, channel->remote.id, bytestoadd, channel->local.window_size); -#endif LIBSSH2_FREE(session, data); return 0; @@ -614,9 +588,8 @@ int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, size_t dat * Well, it's already in the brigade, * let's just call back into ourselves */ -#ifdef LIBSSH2_DEBUG_TRANSPORT - _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Renegotiating Keys"); -#endif + _libssh2_debug(session, LIBSSH2_DBG_TRANS, + "Renegotiating Keys"); libssh2_kex_exchange(session, 1); /* If there was a key reexchange failure, let's just hope we didn't send NEWKEYS yet, otherwise remote will drop us like a rock */ } @@ -643,9 +616,9 @@ int libssh2_packet_ask_ex(LIBSSH2_SESSION *session, unsigned char packet_type, return rc; } } -#ifdef LIBSSH2_DEBUG_TRANSPORT - _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Looking for packet of type: %d", (int)packet_type); -#endif + _libssh2_debug(session, LIBSSH2_DBG_TRANS, + "Looking for packet of type: %d", (int)packet_type); + while (packet) { if (packet->data[0] == packet_type && (packet->data_len >= (match_ofs + match_len)) && @@ -754,11 +727,10 @@ int libssh2_packet_require_ex(LIBSSH2_SESSION *session, return 0; } -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Blocking until packet of type %d becomes available", (int)packet_type); -#endif + while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) { libssh2pack_t ret = libssh2_packet_read(session); if ((ret < 0) && (ret != PACKET_EAGAIN)) { @@ -811,9 +783,9 @@ int libssh2_packet_burn(LIBSSH2_SESSION *session) return i; } -#ifdef LIBSSH2_DEBUG_TRANSPORT - _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Blocking until packet becomes available to burn"); -#endif + _libssh2_debug(session, LIBSSH2_DBG_TRANS, + "Blocking until packet becomes available to burn"); + while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) { int ret = libssh2_packet_read(session); if (ret < 0) { diff --git a/src/publickey.c b/src/publickey.c index 524d7ca..64737c1 100644 --- a/src/publickey.c +++ b/src/publickey.c @@ -289,9 +289,7 @@ LIBSSH2_API LIBSSH2_PUBLICKEY *libssh2_publickey_init(LIBSSH2_SESSION *session) unsigned long data_len; int response; -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Initializing publickey subsystem"); -#endif channel = libssh2_channel_open_session(session); if (!channel) { @@ -320,9 +318,7 @@ LIBSSH2_API LIBSSH2_PUBLICKEY *libssh2_publickey_init(LIBSSH2_SESSION *session) memcpy(s, "version", sizeof("version") - 1); s += sizeof("version") - 1; libssh2_htonu32(s, LIBSSH2_PUBLICKEY_VERSION); s += 4; -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Sending publickey version packet advertising version %d support", (int)LIBSSH2_PUBLICKEY_VERSION); -#endif if ((s - buffer) != libssh2_channel_write(channel, (char*)buffer, (s - buffer))) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send publickey version packet", 0); goto err_exit; @@ -365,14 +361,10 @@ LIBSSH2_API LIBSSH2_PUBLICKEY *libssh2_publickey_init(LIBSSH2_SESSION *session) /* What we want */ pkey->version = libssh2_ntohu32(s); if (pkey->version > LIBSSH2_PUBLICKEY_VERSION) { -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Truncating remote publickey version from %lu", pkey->version); -#endif pkey->version = LIBSSH2_PUBLICKEY_VERSION; } -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Enabling publickey subsystem version %lu", pkey->version); -#endif LIBSSH2_FREE(session, data); return pkey; default: @@ -419,9 +411,7 @@ LIBSSH2_API int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, const unsigned blob_len(4) + {blob} */ -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Adding %s pubickey", name); -#endif if (pkey->version == 1) { for(i = 0; i < num_attrs; i++) { @@ -480,9 +470,9 @@ LIBSSH2_API int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, const unsigned } } -#ifdef LIBSSH2_DEBUG_PUBLICKEY - _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Sending publickey \"add\" packet: type=%s blob_len=%ld num_attrs=%ld", name, blob_len, num_attrs); -#endif + _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, + "Sending publickey \"add\" packet: type=%s blob_len=%ld num_attrs=%ld", + name, blob_len, num_attrs); if ((s - packet) != libssh2_channel_write(channel, (char *)packet, (s - packet))) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send publickey add packet", 0); LIBSSH2_FREE(session, packet); @@ -528,14 +518,12 @@ LIBSSH2_API int libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY *pkey, const unsig libssh2_htonu32(s, blob_len); s += 4; memcpy(s, blob, blob_len); s += blob_len; -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Sending publickey \"remove\" packet: type=%s blob_len=%ld", name, blob_len); -#endif - if ((s - packet) != libssh2_channel_write(channel, (char *)packet, (s - packet))) { - libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send publickey remove packet", 0); + if ((s - packet) != libssh2_channel_write(channel, (char *)packet, (s - packet))) { + libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send publickey remove packet", 0); LIBSSH2_FREE(session, packet); return -1; - } + } LIBSSH2_FREE(session, packet); packet = NULL; @@ -563,13 +551,11 @@ LIBSSH2_API int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, unsigned l libssh2_htonu32(s, sizeof("list") - 1); s += 4; memcpy(s, "list", sizeof("list") - 1); s += sizeof("list") - 1; -#ifdef LIBSSH2_DEBUG_PUBLICKEY _libssh2_debug(session, LIBSSH2_DBG_PUBLICKEY, "Sending publickey \"list\" packet"); -#endif - if ((s - buffer) != libssh2_channel_write(channel, (char *)buffer, (s - buffer))) { - libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send publickey list packet", 0); + if ((s - buffer) != libssh2_channel_write(channel, (char *)buffer, (s - buffer))) { + libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send publickey list packet", 0); return -1; - } + } while (1) { if (libssh2_publickey_packet_receive(pkey, &data, &data_len)) { diff --git a/src/scp.c b/src/scp.c index 8866b06..1046cd0 100644 --- a/src/scp.c +++ b/src/scp.c @@ -73,9 +73,7 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session, } command[command_len - 1] = '\0'; -#ifdef LIBSSH2_DEBUG_SCP _libssh2_debug(session, LIBSSH2_DBG_SCP, "Opening channel for SCP receive"); -#endif /* Allocate a channel */ if ((channel = libssh2_channel_open_session(session)) == NULL) { LIBSSH2_FREE(session, command); @@ -92,9 +90,7 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session, } LIBSSH2_FREE(session, command); -#ifdef LIBSSH2_DEBUG_SCP _libssh2_debug(session, LIBSSH2_DBG_SCP, "Sending initial wakeup"); -#endif /* SCP ACK */ response[0] = '\0'; if (libssh2_channel_write(channel, response, 1) != 1) { @@ -208,9 +204,8 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session, libssh2_channel_free(channel); return NULL; } -#ifdef LIBSSH2_DEBUG_SCP + _libssh2_debug(session, LIBSSH2_DBG_SCP, "mtime = %ld, atime = %ld", mtime, atime); -#endif /* We *should* check that atime.usec is valid, but why let that stop use? */ break; @@ -309,9 +304,7 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session, libssh2_channel_free(channel); return NULL; } -#ifdef LIBSSH2_DEBUG_SCP _libssh2_debug(session, LIBSSH2_DBG_SCP, "mod = 0%lo size = %ld", mode, size); -#endif /* We *should* check that basename is valid, but why let that stop us? */ break; @@ -362,9 +355,7 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session, const } command[command_len - 1] = '\0'; -#ifdef LIBSSH2_DEBUG_SCP _libssh2_debug(session, LIBSSH2_DBG_SCP, "Opening channel for SCP send"); -#endif /* Allocate a channel */ if ((channel = libssh2_channel_open_session(session)) == NULL) { /* previous call set libssh2_session_last_error(), pass it through */ @@ -393,9 +384,8 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session, const /* Send mtime and atime to be used for file */ if (mtime || atime) { response_len = snprintf(response, LIBSSH2_SCP_RESPONSE_BUFLEN, "T%ld 0 %ld 0\n", mtime, atime); -#ifdef LIBSSH2_DEBUG_SCP _libssh2_debug(session, LIBSSH2_DBG_SCP, "Sent %s", response); -#endif + if (libssh2_channel_write(channel, response, response_len) != response_len) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send time data for SCP file", 0); libssh2_channel_free(channel); @@ -418,9 +408,7 @@ LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session, const } response_len = snprintf(response, LIBSSH2_SCP_RESPONSE_BUFLEN, "C0%o %lu %s\n", mode, (unsigned long)size, base); -#ifdef LIBSSH2_DEBUG_SCP _libssh2_debug(session, LIBSSH2_DBG_SCP, "Sent %s", response); -#endif if (libssh2_channel_write(channel, response, response_len) != response_len) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send core file data for SCP file", 0); libssh2_channel_free(channel); diff --git a/src/session.c b/src/session.c index 14bd382..fee9fe9 100644 --- a/src/session.c +++ b/src/session.c @@ -150,9 +150,7 @@ static int libssh2_banner_receive(LIBSSH2_SESSION *session) session->remote.banner = LIBSSH2_ALLOC(session, banner_len + 1); memcpy(session->remote.banner, banner, banner_len); session->remote.banner[banner_len] = '\0'; -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Received Banner: %s", session->remote.banner); -#endif return 0; } /* }}} */ @@ -170,7 +168,7 @@ static int libssh2_banner_send(LIBSSH2_SESSION *session) banner_len = strlen((char *)session->local.banner); banner = (char *)session->local.banner; } -#ifdef LIBSSH2_DEBUG_TRANSPORT +#ifdef LIBSSH2DEBUG { /* Hack and slash to avoid sending CRLF in debug output */ char banner_dup[256]; @@ -215,11 +213,9 @@ LIBSSH2_API int libssh2_banner_set(LIBSSH2_SESSION *session, const char *banner) } memcpy(session->local.banner, banner, banner_len); -#ifdef LIBSSH2_DEBUG_TRANSPORT session->local.banner[banner_len] = '\0'; _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Setting local Banner: %s", session->local.banner); -#endif session->local.banner[banner_len++] = '\r'; session->local.banner[banner_len++] = '\n'; session->local.banner[banner_len++] = '\0'; @@ -258,11 +254,8 @@ LIBSSH2_API LIBSSH2_SESSION *libssh2_session_init_ex( session->free = local_free; session->realloc = local_realloc; session->abstract = abstract; -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "New session resource allocated"); -#endif - libssh2_crypto_init (); return session; @@ -306,9 +299,7 @@ LIBSSH2_API void* libssh2_session_callback_set(LIBSSH2_SESSION *session, return oldcb; } -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Setting Callback %d", cbtype); -#endif return NULL; } @@ -329,10 +320,8 @@ LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int socket) unsigned long service_length; int rc; -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "session_startup for socket %d", socket); -#endif /* FIXME: on some platforms (like win32) sockets are unsigned */ if (socket < 0) { /* Did we forget something? */ @@ -364,10 +353,8 @@ LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int socket) return LIBSSH2_ERROR_KEX_FAILURE; } -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Requesting userauth service"); -#endif /* Request the userauth service */ service[0] = SSH_MSG_SERVICE_REQUEST; libssh2_htonu32(service + 1, sizeof("ssh-userauth") - 1); @@ -405,9 +392,7 @@ LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int socket) */ LIBSSH2_API void libssh2_session_free(LIBSSH2_SESSION *session) { -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Freeing session resource", session->remote.banner); -#endif while (session->channels.head) { LIBSSH2_CHANNEL *tmp = session->channels.head; @@ -535,9 +520,7 @@ LIBSSH2_API int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session, int reas unsigned char *s, *data; unsigned long data_len, descr_len = 0, lang_len = 0; -#ifdef LIBSSH2_DEBUG_TRANSPORT _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Disconnecting: reason=%d, desc=%s, lang=%s", reason, description, lang); -#endif if (description) { descr_len = strlen(description); } diff --git a/src/sftp.c b/src/sftp.c index a6a801c..e930950 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -123,9 +123,7 @@ static int libssh2_sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data, unsi LIBSSH2_SESSION *session = sftp->channel->session; LIBSSH2_PACKET *packet; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Received packet %d", (int)data[0]); -#endif packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PACKET)); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate datablock for SFTP packet", 0); @@ -161,9 +159,7 @@ static int libssh2_sftp_packet_read(LIBSSH2_SFTP *sftp, int should_block) unsigned char *packet; unsigned long packet_len, packet_received; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Waiting for packet: %s block", should_block ? "will" : "willnot"); -#endif libssh2_channel_set_blocking(channel, should_block); if (4 != _libssh2_channel_read(channel, (char *)buffer, 4)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, @@ -172,10 +168,8 @@ static int libssh2_sftp_packet_read(LIBSSH2_SFTP *sftp, int should_block) } packet_len = libssh2_ntohu32(buffer); -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Data begin - Packet Length: %lu", packet_len); -#endif if (packet_len > LIBSSH2_SFTP_PACKET_MAXLEN) { libssh2_error(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, "SFTP packet too large", 0); return -1; @@ -221,9 +215,7 @@ static int libssh2_sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type unsigned char match_buf[5]; int match_len = 5; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Asking for %d packet", (int)packet_type); -#endif if (poll_channel) { if (libssh2_sftp_packet_read(sftp, 0) < 0) { return -1; @@ -272,10 +264,8 @@ static int libssh2_sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type static int libssh2_sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type, unsigned long request_id, unsigned char **data, unsigned long *data_len) { LIBSSH2_SESSION *session = sftp->channel->session; - -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Requiring %d packet", (int)packet_type); -#endif + if (libssh2_sftp_packet_ask(sftp, packet_type, request_id, data, data_len, 0) == 0) { /* A packet was available in the packet brigade */ return 0; @@ -470,9 +460,7 @@ LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session) unsigned char *data, *s, buffer[9]; /* sftp_header(5){excludes request_id} + version_id(4) */ unsigned long data_len; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Initializing SFTP subsystem"); -#endif channel = libssh2_channel_open_session(session); if (!channel) { libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, "Unable to startup channel", 0); @@ -503,9 +491,7 @@ LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session) buffer[4] = SSH_FXP_INIT; libssh2_htonu32(buffer + 5, LIBSSH2_SFTP_VERSION); -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Sending FXP_INIT packet advertising version %d support", (int)LIBSSH2_SFTP_VERSION); -#endif if (9 != libssh2_channel_write(channel, (char *)buffer, 9)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send SSH_FXP_INIT", 0); @@ -532,14 +518,10 @@ LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session) s = data + 1; sftp->version = libssh2_ntohu32(s); s += 4; if (sftp->version > LIBSSH2_SFTP_VERSION) { -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Truncating remote SFTP version from %lu", sftp->version); -#endif sftp->version = LIBSSH2_SFTP_VERSION; } -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Enabling SFTP version %lu compatability", sftp->version); -#endif while (s < (data + data_len)) { unsigned char *extension_name, *extension_data; unsigned long extname_len, extdata_len; @@ -612,9 +594,7 @@ LIBSSH2_API LIBSSH2_SFTP_HANDLE *libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, char * s += libssh2_sftp_attr2bin(s, &attrs); } -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Sending %s open request", (open_type == LIBSSH2_SFTP_OPENFILE) ? "file" : "directory"); -#endif if (packet_len != _libssh2_channel_write(channel, (char *)packet, packet_len)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send FXP_OPEN or FXP_OPENDIR command", 0); @@ -668,9 +648,7 @@ LIBSSH2_API LIBSSH2_SFTP_HANDLE *libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, char * fp->u.file.offset = 0; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Open command successful"); -#endif return fp; } /* }}} */ @@ -691,9 +669,7 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle, unsigned char read_responses[2] = { SSH_FXP_DATA, SSH_FXP_STATUS }; size_t bytes_read = 0; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Reading %lu bytes from SFTP handle", (unsigned long)buffer_maxlen); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_CLOSE packet", 0); @@ -746,11 +722,9 @@ static ssize_t _libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle, if (bytes_read > (data_len - 9)) { return -1; } -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "%lu bytes returned", (unsigned long)bytes_read); -#endif memcpy(buffer, data + 9, bytes_read); handle->u.file.offset += bytes_read; LIBSSH2_FREE(session, data); @@ -870,9 +844,7 @@ LIBSSH2_API int libssh2_sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, libssh2_htonu32(s, handle->handle_len); s += 4; memcpy(s, handle->handle, handle->handle_len); s += handle->handle_len; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Reading entries from directory handle"); -#endif if (packet_len != libssh2_channel_write(channel, (char *)packet, packet_len)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send FXP_READ command", 0); @@ -901,9 +873,7 @@ LIBSSH2_API int libssh2_sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, } num_names = libssh2_ntohu32(data + 5); -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "%lu entries returned", num_names); -#endif if (num_names <= 0) { LIBSSH2_FREE(session, data); return (num_names == 0) ? 0 : -1; @@ -955,9 +925,7 @@ static ssize_t _libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *handle, ssize_t packet_len = handle->handle_len + count + 25; /* packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) + offset(8) + count(4) */ unsigned char *packet, *s, *data; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Writing %lu bytes", (unsigned long)count); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_WRITE packet", 0); @@ -1058,9 +1026,7 @@ LIBSSH2_API int libssh2_sftp_fstat_ex(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_ unsigned char *packet, *s, *data; unsigned char fstat_responses[2] = { SSH_FXP_ATTRS, SSH_FXP_STATUS }; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Issuing %s command", setstat ? "set-stat" : "stat"); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FSTAT/FSETSTAT packet", 0); @@ -1141,9 +1107,7 @@ LIBSSH2_API int libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle) ssize_t packet_len = handle->handle_len + 13; /* packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */ unsigned char *packet, *s, *data; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Closing handle"); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_CLOSE packet", 0); @@ -1212,9 +1176,7 @@ LIBSSH2_API int libssh2_sftp_unlink_ex(LIBSSH2_SFTP *sftp, char *filename, unsig ssize_t packet_len = filename_len + 13; /* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) */ unsigned char *packet, *s, *data; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Unlinking %s", filename); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_REMOVE packet", 0); @@ -1274,9 +1236,7 @@ LIBSSH2_API int libssh2_sftp_rename_ex(LIBSSH2_SFTP *sftp, char *source_filenam return -1; } -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Renaming %s to %s", source_filename, dest_filename); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_RENAME packet", 0); @@ -1351,9 +1311,7 @@ LIBSSH2_API int libssh2_sftp_mkdir_ex(LIBSSH2_SFTP *sftp, char *path, unsigned i /* packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */ unsigned char *packet, *s, *data; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Creating directory %s with mode 0%lo", path, mode); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_MKDIR packet", 0); @@ -1407,9 +1365,7 @@ LIBSSH2_API int libssh2_sftp_rmdir_ex(LIBSSH2_SFTP *sftp, char *path, unsigned i ssize_t packet_len = path_len + 13; /* packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */ unsigned char *packet, *s, *data; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "Removing directory: %s", path); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_MKDIR packet", 0); @@ -1461,9 +1417,7 @@ LIBSSH2_API int libssh2_sftp_stat_ex(LIBSSH2_SFTP *sftp, char *path, unsigned in unsigned char *packet, *s, *data; unsigned char stat_responses[2] = { SSH_FXP_ATTRS, SSH_FXP_STATUS }; -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "%s %s", (stat_type == LIBSSH2_SFTP_SETSTAT) ? "Set-statting" : (stat_type == LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), path); -#endif s = packet = LIBSSH2_ALLOC(session, packet_len); if (!packet) { libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for FXP_MKDIR packet", 0); @@ -1550,10 +1504,8 @@ LIBSSH2_API int libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp, const char *path, un return -1; } -#ifdef LIBSSH2_DEBUG_SFTP _libssh2_debug(session, LIBSSH2_DBG_SFTP, "%s %s on %s", (link_type == LIBSSH2_SFTP_SYMLINK) ? "Creating" : "Reading", (link_type == LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", path); -#endif libssh2_htonu32(s, packet_len - 4); s += 4; switch (link_type) { case LIBSSH2_SFTP_REALPATH: diff --git a/src/transport.c b/src/transport.c index 607362f..64d2b7f 100644 --- a/src/transport.c +++ b/src/transport.c @@ -48,14 +48,20 @@ #ifdef LIBSSH2DEBUG #define UNPRINTABLE_CHAR '.' -static void debugdump(const char *desc, unsigned char *ptr, unsigned long size) +static void debugdump(LIBSSH2_SESSION *session, + const char *desc, unsigned char *ptr, + unsigned long size) { size_t i; size_t c; FILE *stream = stdout; - unsigned int width=0x10; + if(!(session->showmask & (1<< LIBSSH2_DBG_TRANS))) { + /* not asked for, bail out */ + return; + } + fprintf(stream, "=> %s (%d bytes)\n", desc, (int)size); for(i=0; ipayload[0]; - debugdump("libssh2_packet_read() plain", + debugdump(session, "libssh2_packet_read() plain", p->payload, payload_len); libssh2_packet_add(session, p->payload, payload_len, macstate); @@ -303,7 +309,7 @@ libssh2pack_t libssh2_packet_read(LIBSSH2_SESSION *session) } return PACKET_FAIL; } - debugdump("libssh2_packet_read() raw", + debugdump(session, "libssh2_packet_read() raw", &p->buf[remainbuf], nread); /* advance write pointer */ p->writeidx += nread; @@ -530,7 +536,7 @@ static libssh2pack_t send_existing(LIBSSH2_SESSION *session, return PACKET_EAGAIN; } - debugdump("libssh2_packet_write send()", + debugdump(session, "libssh2_packet_write send()", &p->outbuf[p->osent], length); p->osent += length; /* we sent away this much data */ @@ -568,7 +574,7 @@ int libssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, unsigned char *orgdata = data; unsigned long orgdata_len = data_len; - debugdump("libssh2_packet_write plain", data, data_len); + debugdump(session, "libssh2_packet_write plain", data, data_len); /* FIRST, check if we have a pending write to complete */ rc = send_existing(session, data, data_len, &ret); @@ -677,7 +683,7 @@ int libssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, total_length, LIBSSH2_SOCKET_SEND_FLAGS(session)); if(ret != -1) { - debugdump("libssh2_packet_write send()", + debugdump(session, "libssh2_packet_write send()", p->outbuf, ret); } if(ret != total_length) { diff --git a/src/userauth.c b/src/userauth.c index 181a19b..7b1247c 100644 --- a/src/userauth.c +++ b/src/userauth.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2004-2006, Sara Golemon +/* Copyright (c) 2004-2007, Sara Golemon * All rights reserved. * * Redistribution and use in source and binary forms, @@ -99,9 +99,7 @@ LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session, const char *us methods_len = libssh2_ntohu32(data + 1); memcpy(data, data + 5, methods_len); data[methods_len] = '\0'; -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Permitted auth methods: %s", data); -#endif return data; } /* }}} */ @@ -148,9 +146,7 @@ LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const cha libssh2_htonu32(s, password_len); s += 4; memcpy(s, password, password_len); s += password_len; -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Attempting to login using password authentication"); -#endif if (libssh2_packet_write(session, data, data_len)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send userauth-password request", 0); LIBSSH2_FREE(session, data); @@ -164,9 +160,7 @@ LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const cha } if (data[0] == SSH_MSG_USERAUTH_SUCCESS) { -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Password authentication successful"); -#endif LIBSSH2_FREE(session, data); session->state |= LIBSSH2_STATE_AUTHENTICATED; return 0; @@ -176,9 +170,7 @@ LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const cha char *newpw = NULL; int newpw_len = 0; -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Password change required"); -#endif LIBSSH2_FREE(session, data); if (passwd_change_cb) { passwd_change_cb(session, &newpw, &newpw_len, &session->abstract); @@ -245,9 +237,7 @@ static int libssh2_file_read_publickey(LIBSSH2_SESSION *session, unsigned char * size_t pubkey_len = 0; unsigned int tmp_len; -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Loading public key file: %s", pubkeyfile); -#endif /* Read Public Key */ fd = fopen(pubkeyfile, "r"); if (!fd) { @@ -330,9 +320,7 @@ static int libssh2_file_read_privatekey(LIBSSH2_SESSION *session, LIBSSH2_HOSTKE { LIBSSH2_HOSTKEY_METHOD **hostkey_methods_avail = libssh2_hostkey_methods(); -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Loading private key file: %s", privkeyfile); -#endif *hostkey_method = NULL; *hostkey_abstract = NULL; while (*hostkey_methods_avail && (*hostkey_methods_avail)->name) { @@ -457,9 +445,7 @@ LIBSSH2_API int libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION *session, memcpy(s, sig, sig_len); s += sig_len; LIBSSH2_FREE(session, sig); -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Attempting hostbased authentication"); -#endif if (libssh2_packet_write(session, packet, s - packet)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send userauth-hostbased request", 0); LIBSSH2_FREE(session, packet); @@ -472,9 +458,8 @@ LIBSSH2_API int libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION *session, } if (data[0] == SSH_MSG_USERAUTH_SUCCESS) { -#ifdef LIBSSH2_DEBUG_USERAUTH - _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Hostbased authentication successful"); -#endif + _libssh2_debug(session, LIBSSH2_DBG_AUTH, + "Hostbased authentication successful"); /* We are us and we've proved it. */ LIBSSH2_FREE(session, data); session->state |= LIBSSH2_STATE_AUTHENTICATED; @@ -534,9 +519,7 @@ LIBSSH2_API int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session, libssh2_htonu32(s, pubkeydata_len); s += 4; memcpy(s, pubkeydata, pubkeydata_len); s += pubkeydata_len; -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Attempting publickey authentication"); -#endif if (libssh2_packet_write(session, packet, packet_len)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send userauth-publickey request", 0); LIBSSH2_FREE(session, packet); @@ -553,9 +536,7 @@ LIBSSH2_API int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session, } if (data[0] == SSH_MSG_USERAUTH_SUCCESS) { -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Pubkey authentication prematurely successful"); -#endif /* God help any SSH server that allows an UNVERIFIED public key to validate the user */ LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, packet); @@ -630,9 +611,7 @@ LIBSSH2_API int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session, memcpy(s, sig, sig_len); s += sig_len; LIBSSH2_FREE(session, sig); -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Attempting publickey authentication -- phase 2"); -#endif if (libssh2_packet_write(session, packet, s - packet)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send userauth-publickey request", 0); LIBSSH2_FREE(session, packet); @@ -648,9 +627,7 @@ LIBSSH2_API int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session, } if (data[0] == SSH_MSG_USERAUTH_SUCCESS) { -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Publickey authentication successful"); -#endif /* We are us and we've proved it. */ LIBSSH2_FREE(session, data); session->state |= LIBSSH2_STATE_AUTHENTICATED; @@ -706,9 +683,7 @@ LIBSSH2_API int libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION *sessio /* submethods */ libssh2_htonu32(s, 0); s += 4; -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Attempting keyboard-interactive authentication"); -#endif if (libssh2_packet_write(session, data, packet_len)) { libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, "Unable to send keyboard-interactive request", 0); LIBSSH2_FREE(session, data); @@ -735,9 +710,7 @@ LIBSSH2_API int libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION *sessio } if (data[0] == SSH_MSG_USERAUTH_SUCCESS) { -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Keyboard-interactive authentication successful"); -#endif LIBSSH2_FREE(session, data); session->state |= LIBSSH2_STATE_AUTHENTICATED; return 0; @@ -804,9 +777,7 @@ LIBSSH2_API int libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION *sessio response_callback(auth_name, auth_name_len, auth_instruction, auth_instruction_len, num_prompts, prompts, responses, &session->abstract); -#ifdef LIBSSH2_DEBUG_USERAUTH _libssh2_debug(session, LIBSSH2_DBG_AUTH, "Keyboard-interactive response callback function invoked"); -#endif packet_len = 1 /* byte SSH_MSG_USERAUTH_INFO_RESPONSE */ + 4 /* int num-responses */