removed libssh2_error()'s forth argument

libssh2_error() no longer allocates a string and only accepts a const
error string. I also made a lot of functions use the construct of
return libssh2_error(...) instead of having one call to
libssh2_error() and then a separate return call. In several of those
cases I then also changed the former -1 return code to a more
detailed one - something that I think will not change behaviors
anywhere but it's worth keeping an eye open for any such.
This commit is contained in:
Daniel Stenberg 2010-03-03 23:04:05 +01:00
parent d377c3065a
commit d4a768af4e
15 changed files with 693 additions and 980 deletions

View File

@ -619,7 +619,7 @@ libssh2_agent_init(LIBSSH2_SESSION *session)
agent = LIBSSH2_ALLOC(session, sizeof *agent); agent = LIBSSH2_ALLOC(session, sizeof *agent);
if (!agent) { if (!agent) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for agent connection", 0); "Unable to allocate space for agent connection");
return NULL; return NULL;
} }
memset(agent, 0, sizeof *agent); memset(agent, 0, sizeof *agent);

View File

@ -156,7 +156,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!session->open_channel) { if (!session->open_channel) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for channel data", 0); "Unable to allocate space for channel data");
return NULL; return NULL;
} }
memset(session->open_channel, 0, sizeof(LIBSSH2_CHANNEL)); memset(session->open_channel, 0, sizeof(LIBSSH2_CHANNEL));
@ -166,7 +166,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
LIBSSH2_ALLOC(session, channel_type_len); LIBSSH2_ALLOC(session, channel_type_len);
if (!session->open_channel->channel_type) { if (!session->open_channel->channel_type) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating memory for channel type name", 0); "Failed allocating memory for channel type name");
LIBSSH2_FREE(session, session->open_channel); LIBSSH2_FREE(session, session->open_channel);
session->open_channel = NULL; session->open_channel = NULL;
return NULL; return NULL;
@ -188,7 +188,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
LIBSSH2_ALLOC(session, session->open_packet_len); LIBSSH2_ALLOC(session, session->open_packet_len);
if (!session->open_packet) { if (!session->open_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate temporary space for packet", 0); "Unable to allocate temporary space for packet");
goto channel_error; goto channel_error;
} }
*(s++) = SSH_MSG_CHANNEL_OPEN; *(s++) = SSH_MSG_CHANNEL_OPEN;
@ -220,11 +220,11 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
session->open_packet_len); session->open_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending channel-open request", 0); "Would block sending channel-open request");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel-open request", 0); "Unable to send channel-open request");
goto channel_error; goto channel_error;
} }
@ -239,7 +239,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
channel_type_len, 4, channel_type_len, 4,
&session->open_packet_requirev_state); &session->open_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
goto channel_error; goto channel_error;
@ -274,7 +274,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
if (session->open_data[0] == SSH_MSG_CHANNEL_OPEN_FAILURE) { if (session->open_data[0] == SSH_MSG_CHANNEL_OPEN_FAILURE) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Channel open failure", 0); "Channel open failure");
} }
} }
@ -369,8 +369,7 @@ channel_direct_tcpip(LIBSSH2_SESSION * session, const char *host,
LIBSSH2_ALLOC(session, session->direct_message_len); LIBSSH2_ALLOC(session, session->direct_message_len);
if (!session->direct_message) { if (!session->direct_message) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for direct-tcpip connection", "Unable to allocate memory for direct-tcpip connection");
0);
return NULL; return NULL;
} }
_libssh2_htonu32(s, session->direct_host_len); _libssh2_htonu32(s, session->direct_host_len);
@ -463,7 +462,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
LIBSSH2_ALLOC(session, session->fwdLstn_packet_len); LIBSSH2_ALLOC(session, session->fwdLstn_packet_len);
if (!session->fwdLstn_packet) { if (!session->fwdLstn_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memeory for setenv packet", 0); "Unable to allocate memeory for setenv packet");
return NULL; return NULL;
} }
@ -490,14 +489,12 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending global-request packet for " "Would block sending global-request packet for "
"forward listen request", "forward listen request");
0);
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send global-request packet for forward " "Unable to send global-request packet for forward "
"listen request", "listen request");
0);
LIBSSH2_FREE(session, session->fwdLstn_packet); LIBSSH2_FREE(session, session->fwdLstn_packet);
session->fwdLstn_packet = NULL; session->fwdLstn_packet = NULL;
session->fwdLstn_state = libssh2_NB_state_idle; session->fwdLstn_state = libssh2_NB_state_idle;
@ -514,10 +511,10 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
0, NULL, 0, 0, NULL, 0,
&session->fwdLstn_packet_requirev_state); &session->fwdLstn_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_PROTO, "Unknown", 0); libssh2_error(session, LIBSSH2_ERROR_PROTO, "Unknown");
session->fwdLstn_state = libssh2_NB_state_idle; session->fwdLstn_state = libssh2_NB_state_idle;
return NULL; return NULL;
} }
@ -528,8 +525,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER)); listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER));
if (!listener) { if (!listener) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue", "Unable to allocate memory for listener queue");
0);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->fwdLstn_state = libssh2_NB_state_idle; session->fwdLstn_state = libssh2_NB_state_idle;
return NULL; return NULL;
@ -540,8 +536,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1); LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1);
if (!listener->host) { if (!listener->host) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue", "Unable to allocate memory for listener queue");
0);
LIBSSH2_FREE(session, listener); LIBSSH2_FREE(session, listener);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->fwdLstn_state = libssh2_NB_state_idle; session->fwdLstn_state = libssh2_NB_state_idle;
@ -577,7 +572,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
if (data[0] == SSH_MSG_REQUEST_FAILURE) { if (data[0] == SSH_MSG_REQUEST_FAILURE) {
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
libssh2_error(session, LIBSSH2_ERROR_REQUEST_DENIED, libssh2_error(session, LIBSSH2_ERROR_REQUEST_DENIED,
"Unable to complete request for forward-listen", 0); "Unable to complete request for forward-listen");
session->fwdLstn_state = libssh2_NB_state_idle; session->fwdLstn_state = libssh2_NB_state_idle;
return NULL; return NULL;
} }
@ -632,7 +627,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
s = packet = LIBSSH2_ALLOC(session, packet_len); s = packet = LIBSSH2_ALLOC(session, packet_len);
if (!packet) { if (!packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memeory for setenv packet", 0); "Unable to allocate memeory for setenv packet");
return LIBSSH2_ERROR_ALLOC; return LIBSSH2_ERROR_ALLOC;
} }
@ -664,8 +659,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
else if (rc) { else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send global-request packet for forward " "Unable to send global-request packet for forward "
"listen request", "listen request");
0);
LIBSSH2_FREE(session, packet); LIBSSH2_FREE(session, packet);
listener->chanFwdCncl_state = libssh2_NB_state_idle; listener->chanFwdCncl_state = libssh2_NB_state_idle;
return LIBSSH2_ERROR_SOCKET_SEND; return LIBSSH2_ERROR_SOCKET_SEND;
@ -743,11 +737,11 @@ channel_forward_accept(LIBSSH2_LISTENER *listener)
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN, libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for packet", 0); "Would block waiting for packet");
} }
else else
libssh2_error(listener->session, LIBSSH2_ERROR_CHANNEL_UNKNOWN, libssh2_error(listener->session, LIBSSH2_ERROR_CHANNEL_UNKNOWN,
"Channel not found", 0); "Channel not found");
return NULL; return NULL;
} }
@ -799,9 +793,9 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
s = channel->setenv_packet = s = channel->setenv_packet =
LIBSSH2_ALLOC(session, channel->setenv_packet_len); LIBSSH2_ALLOC(session, channel->setenv_packet_len);
if (!channel->setenv_packet) { if (!channel->setenv_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memeory for setenv packet", 0); "Unable to allocate memeory "
return LIBSSH2_ERROR_ALLOC; "for setenv packet");
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -833,14 +827,12 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel-request packet for "
"setenv request",
0);
LIBSSH2_FREE(session, channel->setenv_packet); LIBSSH2_FREE(session, channel->setenv_packet);
channel->setenv_packet = NULL; channel->setenv_packet = NULL;
channel->setenv_state = libssh2_NB_state_idle; channel->setenv_state = libssh2_NB_state_idle;
return LIBSSH2_ERROR_SOCKET_SEND; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel-request packet for "
"setenv request");
} }
LIBSSH2_FREE(session, channel->setenv_packet); LIBSSH2_FREE(session, channel->setenv_packet);
channel->setenv_packet = NULL; channel->setenv_packet = NULL;
@ -872,10 +864,9 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
} }
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel-setenv", 0);
channel->setenv_state = libssh2_NB_state_idle; channel->setenv_state = libssh2_NB_state_idle;
return LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED; return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel-setenv");
} }
/* /*
@ -929,9 +920,8 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
s = channel->reqPTY_packet = s = channel->reqPTY_packet =
LIBSSH2_ALLOC(session, channel->reqPTY_packet_len); LIBSSH2_ALLOC(session, channel->reqPTY_packet_len);
if (!channel->reqPTY_packet) { if (!channel->reqPTY_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request", 0); "Unable to allocate memory for pty-request");
return -1;
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -976,12 +966,11 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc,
"Unable to send pty-request packet", 0);
LIBSSH2_FREE(session, channel->reqPTY_packet); LIBSSH2_FREE(session, channel->reqPTY_packet);
channel->reqPTY_packet = NULL; channel->reqPTY_packet = NULL;
channel->reqPTY_state = libssh2_NB_state_idle; channel->reqPTY_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send pty-request packet");
} }
LIBSSH2_FREE(session, channel->reqPTY_packet); LIBSSH2_FREE(session, channel->reqPTY_packet);
channel->reqPTY_packet = NULL; channel->reqPTY_packet = NULL;
@ -1010,10 +999,9 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
} }
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel request-pty", 0);
channel->reqPTY_state = libssh2_NB_state_idle; channel->reqPTY_state = libssh2_NB_state_idle;
return LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED; return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel request-pty");
} }
/* /*
@ -1058,9 +1046,8 @@ channel_request_pty_size(LIBSSH2_CHANNEL * channel, int width,
LIBSSH2_ALLOC(session, channel->reqPTY_packet_len); LIBSSH2_ALLOC(session, channel->reqPTY_packet_len);
if (!channel->reqPTY_packet) { if (!channel->reqPTY_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request", 0); "Unable to allocate memory for pty-request");
return -1;
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -1090,12 +1077,11 @@ channel_request_pty_size(LIBSSH2_CHANNEL * channel, int width,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc,
"Unable to send window-change packet", 0);
LIBSSH2_FREE(session, channel->reqPTY_packet); LIBSSH2_FREE(session, channel->reqPTY_packet);
channel->reqPTY_packet = NULL; channel->reqPTY_packet = NULL;
channel->reqPTY_state = libssh2_NB_state_idle; channel->reqPTY_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send window-change packet");
} }
LIBSSH2_FREE(session, channel->reqPTY_packet); LIBSSH2_FREE(session, channel->reqPTY_packet);
channel->reqPTY_packet = NULL; channel->reqPTY_packet = NULL;
@ -1164,9 +1150,8 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
s = channel->reqX11_packet = s = channel->reqX11_packet =
LIBSSH2_ALLOC(session, channel->reqX11_packet_len); LIBSSH2_ALLOC(session, channel->reqX11_packet_len);
if (!channel->reqX11_packet) { if (!channel->reqX11_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request", 0); "Unable to allocate memory for pty-request");
return -1;
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -1217,12 +1202,11 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
return rc; return rc;
} }
if (rc) { if (rc) {
libssh2_error(session, rc,
"Unable to send x11-req packet", 0);
LIBSSH2_FREE(session, channel->reqX11_packet); LIBSSH2_FREE(session, channel->reqX11_packet);
channel->reqX11_packet = NULL; channel->reqX11_packet = NULL;
channel->reqX11_state = libssh2_NB_state_idle; channel->reqX11_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send x11-req packet");
} }
LIBSSH2_FREE(session, channel->reqX11_packet); LIBSSH2_FREE(session, channel->reqX11_packet);
channel->reqX11_packet = NULL; channel->reqX11_packet = NULL;
@ -1239,9 +1223,9 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
/* TODO: call libssh2_error() here! */
channel->reqX11_state = libssh2_NB_state_idle; channel->reqX11_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"waiting for x11-req response packet");
} }
if (data[0] == SSH_MSG_CHANNEL_SUCCESS) { if (data[0] == SSH_MSG_CHANNEL_SUCCESS) {
@ -1252,9 +1236,8 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
} }
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel x11-req", 0); "Unable to complete request for channel x11-req");
return -1;
} }
/* /*
@ -1310,10 +1293,9 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
s = channel->process_packet = s = channel->process_packet =
LIBSSH2_ALLOC(session, channel->process_packet_len); LIBSSH2_ALLOC(session, channel->process_packet_len);
if (!channel->process_packet) { if (!channel->process_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for channel-process request", "Unable to allocate memory "
0); "for channel-process request");
return LIBSSH2_ERROR_ALLOC;
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -1343,12 +1325,11 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
return rc; return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, rc,
"Unable to send channel request", 0);
LIBSSH2_FREE(session, channel->process_packet); LIBSSH2_FREE(session, channel->process_packet);
channel->process_packet = NULL; channel->process_packet = NULL;
channel->process_state = libssh2_NB_state_idle; channel->process_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send channel request");
} }
LIBSSH2_FREE(session, channel->process_packet); LIBSSH2_FREE(session, channel->process_packet);
channel->process_packet = NULL; channel->process_packet = NULL;
@ -1366,9 +1347,8 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
return rc; return rc;
} else if (rc) { } else if (rc) {
channel->process_state = libssh2_NB_state_idle; channel->process_state = libssh2_NB_state_idle;
libssh2_error(session, rc, return libssh2_error(session, rc,
"Failed waiting for channel success", 0); "Failed waiting for channel success");
return rc;
} }
if (data[0] == SSH_MSG_CHANNEL_SUCCESS) { if (data[0] == SSH_MSG_CHANNEL_SUCCESS) {
@ -1379,10 +1359,10 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
} }
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel-process-startup", 0);
channel->process_state = libssh2_NB_state_idle; channel->process_state = libssh2_NB_state_idle;
return LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED; return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for "
"channel-process-startup");
} }
/* /*
@ -1569,11 +1549,10 @@ _libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL * channel,
return rc; return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send transfer-window adjustment packet, "
"deferring", 0);
channel->adjust_queue = adjustment; channel->adjust_queue = adjustment;
return rc; return libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send transfer-window adjustment "
"packet, deferring");
} }
else { else {
channel->remote.window_size += adjustment; channel->remote.window_size += adjustment;
@ -1736,8 +1715,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
rc = _libssh2_transport_read(session); rc = _libssh2_transport_read(session);
if ((rc < 0) && (rc != PACKET_EAGAIN)) { if ((rc < 0) && (rc != PACKET_EAGAIN)) {
libssh2_error(session, rc, "tranport read", 0); return libssh2_error(session, rc, "tranport read");
return rc;
} }
/* /*
@ -1972,15 +1950,14 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
stream_id); stream_id);
if (channel->local.close) { if (channel->local.close) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED, return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED,
"We've already closed this channel", 0); "We've already closed this channel");
return LIBSSH2_ERROR_CHANNEL_CLOSED;
} }
if (channel->local.eof) { if (channel->local.eof) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_EOF_SENT, return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_EOF_SENT,
"EOF has already been sight, data might be ignored", "EOF has already been sight, "
0); "data might be ignored");
} }
/* [13] 9 = packet_type(1) + channelno(4) [ + streamid(4) ] + /* [13] 9 = packet_type(1) + channelno(4) [ + streamid(4) ] +
@ -1989,10 +1966,9 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
channel->write_packet = channel->write_packet =
LIBSSH2_ALLOC(session, channel->write_packet_len); LIBSSH2_ALLOC(session, channel->write_packet_len);
if (!channel->write_packet) { if (!channel->write_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocte space for data transmission packet", "Unable to allocte space "
0); "for data transmission packet");
return LIBSSH2_ERROR_ALLOC;
} }
channel->write_state = libssh2_NB_state_allocated; channel->write_state = libssh2_NB_state_allocated;
@ -2063,17 +2039,15 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
channel->write_s - channel->write_s -
channel->write_packet); channel->write_packet);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, rc, return libssh2_error(session, rc,
"Unable to send channel data", 0); "Unable to send channel data");
return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, rc,
"Unable to send channel data", 0);
LIBSSH2_FREE(session, channel->write_packet); LIBSSH2_FREE(session, channel->write_packet);
channel->write_packet = NULL; channel->write_packet = NULL;
channel->write_state = libssh2_NB_state_idle; channel->write_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send channel data");
} }
/* Shrink local window size */ /* Shrink local window size */
channel->local.window_size -= channel->write_bufwrite; channel->local.window_size -= channel->write_bufwrite;
@ -2131,9 +2105,8 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel)
return rc; return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send EOF on channel", 0); "Unable to send EOF on channel");
return LIBSSH2_ERROR_SOCKET_SEND;
} }
channel->local.eof = 1; channel->local.eof = 1;
@ -2268,10 +2241,9 @@ channel_close(LIBSSH2_CHANNEL * channel)
if (retcode == PACKET_EAGAIN) { if (retcode == PACKET_EAGAIN) {
return retcode; return retcode;
} else if (retcode) { } else if (retcode) {
libssh2_error(session, retcode,
"Unable to send close-channel request", 0);
channel->close_state = libssh2_NB_state_idle; channel->close_state = libssh2_NB_state_idle;
return retcode; return libssh2_error(session, retcode,
"Unable to send close-channel request");
} }
channel->close_state = libssh2_NB_state_sent; channel->close_state = libssh2_NB_state_sent;
@ -2327,11 +2299,9 @@ static int channel_wait_closed(LIBSSH2_CHANNEL *channel)
int rc; int rc;
if (!libssh2_channel_eof(channel)) { if (!libssh2_channel_eof(channel)) {
libssh2_error(session, LIBSSH2_ERROR_INVAL, return libssh2_error(session, LIBSSH2_ERROR_INVAL,
"libssh2_channel_wait_closed() invoked when channel is " "libssh2_channel_wait_closed() invoked when "
"not in EOF state", "channel is not in EOF state");
0);
return -1;
} }
if (channel->wait_closed_state == libssh2_NB_state_idle) { if (channel->wait_closed_state == libssh2_NB_state_idle) {

View File

@ -122,10 +122,9 @@ comp_method_zlib_init(LIBSSH2_SESSION * session, int compress,
strm = LIBSSH2_ALLOC(session, sizeof(z_stream)); strm = LIBSSH2_ALLOC(session, sizeof(z_stream));
if (!strm) { if (!strm) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for zlib compression/decompression", "Unable to allocate memory for "
0); "zlib compression/decompression");
return -1;
} }
memset(strm, 0, sizeof(z_stream)); memset(strm, 0, sizeof(z_stream));
@ -186,10 +185,9 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
out = (char *) strm->next_out; out = (char *) strm->next_out;
strm->avail_out = out_maxlen; strm->avail_out = out_maxlen;
if (!strm->next_out) { if (!strm->next_out) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate compression/decompression buffer", "Unable to allocate compression/decompression "
0); "buffer");
return -1;
} }
while (strm->avail_in) { while (strm->avail_in) {
int status; int status;
@ -200,10 +198,9 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
status = inflate(strm, Z_PARTIAL_FLUSH); status = inflate(strm, Z_PARTIAL_FLUSH);
} }
if (status != Z_OK) { if (status != Z_OK) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure", 0);
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure");
} }
if (strm->avail_in) { if (strm->avail_in) {
unsigned long out_ofs = out_maxlen - strm->avail_out; unsigned long out_ofs = out_maxlen - strm->avail_out;
@ -213,19 +210,17 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
compress ? (strm->avail_in + 4) : (2 * strm->avail_in); compress ? (strm->avail_in + 4) : (2 * strm->avail_in);
if ((out_maxlen > (int) payload_limit) && !compress && limiter++) { if ((out_maxlen > (int) payload_limit) && !compress && limiter++) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression phase", 0);
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression phase");
} }
newout = LIBSSH2_REALLOC(session, out, out_maxlen); newout = LIBSSH2_REALLOC(session, out, out_maxlen);
if (!newout) { if (!newout) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand compress/decompression buffer",
0);
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand compress/"
"decompression buffer");
} }
out = newout; out = newout;
strm->next_out = (unsigned char *) out + out_ofs; strm->next_out = (unsigned char *) out + out_ofs;
@ -241,11 +236,10 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
char *newout; char *newout;
if (out_maxlen >= (int) payload_limit) { if (out_maxlen >= (int) payload_limit) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression phase",
0);
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression "
"phase");
} }
if (grow_size > (int) (payload_limit - out_maxlen)) { if (grow_size > (int) (payload_limit - out_maxlen)) {
@ -257,11 +251,10 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
newout = LIBSSH2_REALLOC(session, out, out_maxlen); newout = LIBSSH2_REALLOC(session, out, out_maxlen);
if (!newout) { if (!newout) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand final compress/decompress buffer",
0);
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand final compress/"
"decompress buffer");
} }
out = newout; out = newout;
strm->next_out = (unsigned char *) out + out_maxlen - strm->next_out = (unsigned char *) out + out_maxlen -
@ -273,10 +266,9 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
status = inflate(strm, Z_PARTIAL_FLUSH); status = inflate(strm, Z_PARTIAL_FLUSH);
} }
if (status != Z_OK) { if (status != Z_OK) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure", 0);
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure");
} }
} }
} }

View File

@ -324,9 +324,8 @@ hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session,
sig += 15; sig += 15;
sig_len -= 15; sig_len -= 15;
if (sig_len != 40) { if (sig_len != 40) {
libssh2_error(session, LIBSSH2_ERROR_PROTO, return libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Invalid DSS signature length", 0); "Invalid DSS signature length");
return -1;
} }
return _libssh2_dsa_sha1_verify(dsactx, sig, m, m_len); return _libssh2_dsa_sha1_verify(dsactx, sig, m, m_len);
} }

View File

@ -118,9 +118,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
exchange_state->e_packet = exchange_state->e_packet =
LIBSSH2_ALLOC(session, exchange_state->e_packet_len); LIBSSH2_ALLOC(session, exchange_state->e_packet_len);
if (!exchange_state->e_packet) { if (!exchange_state->e_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Out of memory error", ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
0); "Out of memory error");
ret = LIBSSH2_ERROR_ALLOC;
goto clean_exit; goto clean_exit;
} }
exchange_state->e_packet[0] = packet_type_init; exchange_state->e_packet[0] = packet_type_init;
@ -146,9 +145,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, ret = libssh2_error(session, rc,
"Unable to send KEX init message", 0); "Unable to send KEX init message");
ret = rc;
goto clean_exit; goto clean_exit;
} }
exchange_state->state = libssh2_NB_state_sent; exchange_state->state = libssh2_NB_state_sent;
@ -192,9 +190,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
return rc; return rc;
} }
if (rc) { if (rc) {
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT, ret = libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
"Timed out waiting for KEX reply", 0); "Timed out waiting for KEX reply");
ret = rc;
goto clean_exit; goto clean_exit;
} }
@ -206,10 +203,9 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
session->server_hostkey = session->server_hostkey =
LIBSSH2_ALLOC(session, session->server_hostkey_len); LIBSSH2_ALLOC(session, session->server_hostkey_len);
if (!session->server_hostkey) { if (!session->server_hostkey) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for a copy of the host key", "Unable to allocate memory for a copy "
0); "of the host key");
ret = LIBSSH2_ERROR_ALLOC;
goto clean_exit; goto clean_exit;
} }
memcpy(session->server_hostkey, exchange_state->s, memcpy(session->server_hostkey, exchange_state->s,
@ -264,9 +260,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (session->hostkey->init(session, session->server_hostkey, if (session->hostkey->init(session, session->server_hostkey,
session->server_hostkey_len, session->server_hostkey_len,
&session->server_hostkey_abstract)) { &session->server_hostkey_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, ret = libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
"Unable to initialize hostkey importer", 0); "Unable to initialize hostkey importer");
ret = LIBSSH2_ERROR_HOSTKEY_INIT;
goto clean_exit; goto clean_exit;
} }
@ -292,9 +287,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
exchange_state->k_value = exchange_state->k_value =
LIBSSH2_ALLOC(session, exchange_state->k_value_len); LIBSSH2_ALLOC(session, exchange_state->k_value_len);
if (!exchange_state->k_value) { if (!exchange_state->k_value) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate buffer for K", 0); "Unable to allocate buffer for K");
ret = LIBSSH2_ERROR_ALLOC;
goto clean_exit; goto clean_exit;
} }
_libssh2_htonu32(exchange_state->k_value, _libssh2_htonu32(exchange_state->k_value,
@ -404,9 +398,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
sig_verify(session, exchange_state->h_sig, sig_verify(session, exchange_state->h_sig,
exchange_state->h_sig_len, exchange_state->h_sig_comp, exchange_state->h_sig_len, exchange_state->h_sig_comp,
20, &session->server_hostkey_abstract)) { 20, &session->server_hostkey_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN, ret = libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN,
"Unable to verify hostkey signature", 0); "Unable to verify hostkey signature");
ret = -1;
goto clean_exit; goto clean_exit;
} }
@ -421,8 +414,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, "Unable to send NEWKEYS message", 0); ret = libssh2_error(session, rc, "Unable to send NEWKEYS message");
ret = rc;
goto clean_exit; goto clean_exit;
} }
@ -437,8 +429,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, "Timed out waiting for NEWKEYS", 0); ret = libssh2_error(session, rc, "Timed out waiting for NEWKEYS");
ret = rc;
goto clean_exit; goto clean_exit;
} }
/* The first key exchange has been performed, /* The first key exchange has been performed,
@ -453,9 +444,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (!session->session_id) { if (!session->session_id) {
session->session_id = LIBSSH2_ALLOC(session, SHA_DIGEST_LENGTH); session->session_id = LIBSSH2_ALLOC(session, SHA_DIGEST_LENGTH);
if (!session->session_id) { if (!session->session_id) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate buffer for SHA digest", 0); "Unable to allocate buffer for SHA digest");
ret = LIBSSH2_ERROR_ALLOC;
goto clean_exit; goto clean_exit;
} }
memcpy(session->session_id, exchange_state->h_sig_comp, memcpy(session->session_id, exchange_state->h_sig_comp,
@ -825,9 +815,8 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, ret = libssh2_error(session, rc,
"Unable to send Group Exchange Request", 0); "Unable to send Group Exchange Request");
ret = rc;
goto dh_gex_clean_exit; goto dh_gex_clean_exit;
} }
@ -841,9 +830,8 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, ret = libssh2_error(session, rc,
"Timeout waiting for GEX_GROUP reply", 0); "Timeout waiting for GEX_GROUP reply");
ret = rc;
goto dh_gex_clean_exit; goto dh_gex_clean_exit;
} }
@ -1038,9 +1026,8 @@ static int kexinit(LIBSSH2_SESSION * session)
s = data = LIBSSH2_ALLOC(session, data_len); s = data = LIBSSH2_ALLOC(session, data_len);
if (!data) { if (!data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory", 0); "Unable to allocate memory");
return LIBSSH2_ERROR_ALLOC;
} }
*(s++) = SSH_MSG_KEXINIT; *(s++) = SSH_MSG_KEXINIT;
@ -1127,10 +1114,10 @@ static int kexinit(LIBSSH2_SESSION * session)
} }
else if (rc) { else if (rc) {
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
libssh2_error(session, rc,
"Unable to send KEXINIT packet to remote host", 0);
session->kexinit_state = libssh2_NB_state_idle; session->kexinit_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send KEXINIT packet to remote host");
} }
if (session->local.kexinit) { if (session->local.kexinit) {
@ -1749,9 +1736,8 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
return retcode; return retcode;
} else if (retcode) { } else if (retcode) {
libssh2_error(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE, rc = libssh2_error(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE,
"Unrecoverable error exchanging keys", 0); "Unrecoverable error exchanging keys");
rc = retcode;
} }
} }
} }
@ -1839,16 +1825,14 @@ libssh2_session_method_pref(LIBSSH2_SESSION * session, int method_type,
break; break;
default: default:
libssh2_error(session, LIBSSH2_ERROR_INVAL, return libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid parameter specified for method_type", 0); "Invalid parameter specified for method_type");
return -1;
} }
s = newprefs = LIBSSH2_ALLOC(session, prefs_len + 1); s = newprefs = LIBSSH2_ALLOC(session, prefs_len + 1);
if (!newprefs) { if (!newprefs) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Error allocated space for method preferences", 0); "Error allocated space for method preferences");
return -1;
} }
memcpy(s, prefs, prefs_len + 1); memcpy(s, prefs, prefs_len + 1);
@ -1873,11 +1857,10 @@ libssh2_session_method_pref(LIBSSH2_SESSION * session, int method_type,
} }
if (strlen(newprefs) == 0) { if (strlen(newprefs) == 0) {
libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"The requested method(s) are not currently supported",
0);
LIBSSH2_FREE(session, newprefs); LIBSSH2_FREE(session, newprefs);
return -1; return libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"The requested method(s) are not currently "
"supported");
} }
if (*prefvar) { if (*prefvar) {

View File

@ -87,7 +87,7 @@ libssh2_knownhost_init(LIBSSH2_SESSION *session)
if(!knh) { if(!knh) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for known-hosts " "Unable to allocate memory for known-hosts "
"collection", 0); "collection");
return NULL; return NULL;
} }
@ -155,12 +155,12 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
if(!entry) if(!entry)
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for known host " "Unable to allocate memory for known host "
"entry", 0); "entry");
/* make sure we have a key type set */ /* make sure we have a key type set */
if(!(typemask & LIBSSH2_KNOWNHOST_KEY_MASK)) if(!(typemask & LIBSSH2_KNOWNHOST_KEY_MASK))
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
"No key type set", 0); "No key type set");
memset(entry, 0, sizeof(struct known_host)); memset(entry, 0, sizeof(struct known_host));
@ -172,7 +172,7 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
entry->name = LIBSSH2_ALLOC(hosts->session, hostlen+1); entry->name = LIBSSH2_ALLOC(hosts->session, hostlen+1);
if(!entry->name) { if(!entry->name) {
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for host name", 0); "Unable to allocate memory for host name");
goto error; goto error;
} }
memcpy(entry->name, host, hostlen+1); memcpy(entry->name, host, hostlen+1);
@ -194,7 +194,7 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
break; break;
default: default:
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unknown host name type", 0); "Unknown host name type");
goto error; goto error;
} }
@ -205,7 +205,7 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
entry->key = LIBSSH2_ALLOC(hosts->session, keylen+1); entry->key = LIBSSH2_ALLOC(hosts->session, keylen+1);
if(!entry->key) { if(!entry->key) {
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for key", 0); "Unable to allocate memory for key");
goto error; goto error;
} }
memcpy(entry->key, key, keylen+1); memcpy(entry->key, key, keylen+1);
@ -218,7 +218,7 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
if(!nlen) { if(!nlen) {
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"base64-encoded key", 0); "base64-encoded key");
goto error; goto error;
} }
@ -278,7 +278,7 @@ libssh2_knownhost_check(LIBSSH2_KNOWNHOSTS *hosts,
if(!nlen) { if(!nlen) {
libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for base64-encoded " "Unable to allocate memory for base64-encoded "
"key", 0); "key");
return LIBSSH2_KNOWNHOST_CHECK_FAILURE; return LIBSSH2_KNOWNHOST_CHECK_FAILURE;
} }
@ -366,11 +366,11 @@ libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts,
struct libssh2_knownhost *entry) struct libssh2_knownhost *entry)
{ {
struct known_host *node; struct known_host *node;
/* check that this was retrieved the right way or get out */ /* check that this was retrieved the right way or get out */
if(!entry || (entry->magic != KNOWNHOST_MAGIC)) if(!entry || (entry->magic != KNOWNHOST_MAGIC))
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
"Invalid host information", 0); "Invalid host information");
/* get the internal node pointer */ /* get the internal node pointer */
node = entry->node; node = entry->node;
@ -468,7 +468,7 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Failed to parse known_hosts line " "Failed to parse known_hosts line "
"(unexpectedly long salt)", 0); "(unexpectedly long salt)");
memcpy(saltbuf, salt, saltlen); memcpy(saltbuf, salt, saltlen);
saltbuf[saltlen] = 0; /* zero terminate */ saltbuf[saltlen] = 0; /* zero terminate */
@ -491,7 +491,7 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Failed to parse known_hosts line " "Failed to parse known_hosts line "
"(unexpected length)", 0); "(unexpected length)");
switch(key[0]) { switch(key[0]) {
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
@ -514,7 +514,7 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
/* unknown key type */ /* unknown key type */
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unknown key type", 0); "Unknown key type");
key += 7; key += 7;
keylen -= 7; keylen -= 7;
@ -529,7 +529,7 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
default: /* unknown key format */ default: /* unknown key format */
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unknown key format", 0); "Unknown key format");
} }
if(sep) { if(sep) {
@ -599,7 +599,7 @@ libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unsupported type of known-host information " "Unsupported type of known-host information "
"store", 0); "store");
cp = line; cp = line;
@ -633,7 +633,7 @@ libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
if(!*cp || !len) /* illegal line */ if(!*cp || !len) /* illegal line */
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Failed to parse known_hosts line", 0); "Failed to parse known_hosts line");
keyp = cp; /* the key starts here */ keyp = cp; /* the key starts here */
keylen = len; keylen = len;
@ -677,7 +677,7 @@ libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unsupported type of known-host information " "Unsupported type of known-host information "
"store", 0); "store");
file = fopen(filename, "r"); file = fopen(filename, "r");
if(file) { if(file) {
@ -690,7 +690,7 @@ libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
} }
else else
return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
"Failed to open file", 0); "Failed to open file");
return num; return num;
} }
@ -729,7 +729,7 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unsupported type of known-host information " "Unsupported type of known-host information "
"store", 0); "store");
tindex = (node->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) >> tindex = (node->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) >>
LIBSSH2_KNOWNHOST_KEY_SHIFT; LIBSSH2_KNOWNHOST_KEY_SHIFT;
@ -746,7 +746,7 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
if(!nlen) if(!nlen)
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"base64-encoded host name", 0); "base64-encoded host name");
nlen = _libssh2_base64_encode(hosts->session, nlen = _libssh2_base64_encode(hosts->session,
node->salt, node->salt_len, node->salt, node->salt_len,
@ -755,7 +755,7 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
free(namealloc); free(namealloc);
return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, return libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"base64-encoded salt", 0); "base64-encoded salt");
} }
nlen = strlen(saltalloc) + strlen(namealloc) + strlen(keytype) + nlen = strlen(saltalloc) + strlen(namealloc) + strlen(keytype) +
@ -766,7 +766,7 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
node->key); node->key);
else else
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
"Known-host write buffer too small", 0); "Known-host write buffer too small");
free(namealloc); free(namealloc);
free(saltalloc); free(saltalloc);
@ -779,7 +779,7 @@ knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
sprintf(buf, "%s%s %s\n", node->name, keytype, node->key); sprintf(buf, "%s%s %s\n", node->name, keytype, node->key);
else else
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
"Known-host write buffer too small", 0); "Known-host write buffer too small");
} }
/* we report the full length of the data with the trailing zero excluded */ /* we report the full length of the data with the trailing zero excluded */
@ -807,7 +807,7 @@ libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
if(known->magic != KNOWNHOST_MAGIC) if(known->magic != KNOWNHOST_MAGIC)
return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, return libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
"Invalid host information", 0); "Invalid host information");
node = known->node; node = known->node;
@ -834,12 +834,12 @@ libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts,
return libssh2_error(hosts->session, return libssh2_error(hosts->session,
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"Unsupported type of known-host information " "Unsupported type of known-host information "
"store", 0); "store");
file = fopen(filename, "w"); file = fopen(filename, "w");
if(!file) if(!file)
return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, return libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
"Failed to open file", 0); "Failed to open file");
for(node = _libssh2_list_first(&hosts->head); for(node = _libssh2_list_first(&hosts->head);
node; node;
@ -855,7 +855,7 @@ libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts,
if(nwrote != wrote) { if(nwrote != wrote) {
/* failed to write the whole thing, bail out */ /* failed to write the whole thing, bail out */
rc = libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, rc = libssh2_error(hosts->session, LIBSSH2_ERROR_FILE,
"Write failed", 0); "Write failed");
break; break;
} }
} }

View File

@ -731,8 +731,6 @@ struct _LIBSSH2_SESSION
/* Error tracking */ /* Error tracking */
const char *err_msg; const char *err_msg;
unsigned long err_msglen;
int err_should_free;
int err_code; int err_code;
/* struct members for packet-level reading */ /* struct members for packet-level reading */
@ -1036,8 +1034,7 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
#endif #endif
#endif #endif
int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg, int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg);
int should_free);
#define LIBSSH2_SOCKET_UNKNOWN 1 #define LIBSSH2_SOCKET_UNKNOWN 1
#define LIBSSH2_SOCKET_CONNECTED 0 #define LIBSSH2_SOCKET_CONNECTED 0

View File

@ -1,4 +1,4 @@
/* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2007 Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2009 by Daniel Stenberg * Copyright (c) 2009 by Daniel Stenberg
* All rights reserved. * All rights reserved.
* *
@ -49,15 +49,9 @@
#include <errno.h> #include <errno.h>
int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg, int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg)
int should_free)
{ {
if (session->err_msg && session->err_should_free) {
LIBSSH2_FREE(session, session->err_msg);
}
session->err_msg = errmsg; session->err_msg = errmsg;
session->err_msglen = strlen(errmsg);
session->err_should_free = should_free;
session->err_code = errcode; session->err_code = errcode;
#ifdef LIBSSH2DEBUG #ifdef LIBSSH2DEBUG
_libssh2_debug(session, LIBSSH2_TRACE_ERROR, "%d - %s", session->err_code, _libssh2_debug(session, LIBSSH2_TRACE_ERROR, "%d - %s", session->err_code,
@ -207,9 +201,8 @@ libssh2_base64_decode(LIBSSH2_SESSION *session, char **data,
*data = LIBSSH2_ALLOC(session, (3 * src_len / 4) + 1); *data = LIBSSH2_ALLOC(session, (3 * src_len / 4) + 1);
d = (unsigned char *) *data; d = (unsigned char *) *data;
if (!d) { if (!d) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for base64 decoding", 0); "Unable to allocate memory for base64 decoding");
return -1;
} }
for(s = (unsigned char *) src; ((char *) s) < (src + src_len); s++) { for(s = (unsigned char *) src; ((char *) s) < (src + src_len); s++) {
@ -237,9 +230,8 @@ libssh2_base64_decode(LIBSSH2_SESSION *session, char **data,
/* Invalid -- We have a byte which belongs exclusively to a partial /* Invalid -- We have a byte which belongs exclusively to a partial
octet */ octet */
LIBSSH2_FREE(session, *data); LIBSSH2_FREE(session, *data);
libssh2_error(session, LIBSSH2_ERROR_INVAL, return libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid data (byte belonging to partial octet)", 0); "Invalid data (byte belonging to partial octet)");
return -1;
} }
*datalen = len; *datalen = len;

View File

@ -141,8 +141,8 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!channel) { if (!channel) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection", "Unable to allocate a channel for "
0); "new connection");
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
listen_state->state = libssh2_NB_state_sent; listen_state->state = libssh2_NB_state_sent;
break; break;
@ -159,8 +159,8 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
1); 1);
if (!channel->channel_type) { if (!channel->channel_type) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection", "Unable to allocate a channel for new"
0); " connection");
LIBSSH2_FREE(session, channel); LIBSSH2_FREE(session, channel);
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
listen_state->state = libssh2_NB_state_sent; listen_state->state = libssh2_NB_state_sent;
@ -212,11 +212,10 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) if (rc == PACKET_EAGAIN)
return rc; return rc;
else if (rc) { else if (rc) {
libssh2_error(session, rc,
"Unable to send channel "
"open confirmation", 0);
listen_state->state = libssh2_NB_state_idle; listen_state->state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Unable to send channel "
"open confirmation");
} }
/* Link the channel into the end of the queue list */ /* Link the channel into the end of the queue list */
@ -253,9 +252,9 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, "Unable to send open failure", 0);
listen_state->state = libssh2_NB_state_idle; listen_state->state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc, "Unable to send open failure");
} }
listen_state->state = libssh2_NB_state_idle; listen_state->state = libssh2_NB_state_idle;
return 0; return 0;
@ -308,8 +307,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!channel) { if (!channel) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection", "Unable to allocate a channel for new connection");
0);
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
goto x11_exit; goto x11_exit;
} }
@ -322,8 +320,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
1); 1);
if (!channel->channel_type) { if (!channel->channel_type) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection", "Unable to allocate a channel for new connection");
0);
LIBSSH2_FREE(session, channel); LIBSSH2_FREE(session, channel);
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
goto x11_exit; goto x11_exit;
@ -369,10 +366,10 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel open confirmation", 0);
x11open_state->state = libssh2_NB_state_idle; x11open_state->state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel open "
"confirmation");
} }
/* Link the channel into the session */ /* Link the channel into the session */
@ -409,9 +406,8 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, rc, "Unable to send open failure", 0);
x11open_state->state = libssh2_NB_state_idle; x11open_state->state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc, "Unable to send open failure");
} }
x11open_state->state = libssh2_NB_state_idle; x11open_state->state = libssh2_NB_state_idle;
return 0; return 0;
@ -456,9 +452,6 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
/* Calling app has given the OK, Process it anyway */ /* Calling app has given the OK, Process it anyway */
macstate = LIBSSH2_MAC_CONFIRMED; macstate = LIBSSH2_MAC_CONFIRMED;
} else { } else {
libssh2_error(session, LIBSSH2_ERROR_INVALID_MAC,
"Invalid Message Authentication Code received",
0);
if (session->ssh_msg_disconnect) { if (session->ssh_msg_disconnect) {
LIBSSH2_DISCONNECT(session, SSH_DISCONNECT_MAC_ERROR, LIBSSH2_DISCONNECT(session, SSH_DISCONNECT_MAC_ERROR,
"Invalid MAC received", "Invalid MAC received",
@ -466,7 +459,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
"", 0); "", 0);
} }
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
return LIBSSH2_ERROR_INVALID_MAC; return libssh2_error(session, LIBSSH2_ERROR_INVALID_MAC,
"Invalid MAC received");
} }
} }
@ -535,9 +529,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; session->socket_state = LIBSSH2_SOCKET_DISCONNECTED;
session->packAdd_state = libssh2_NB_state_idle; session->packAdd_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, return libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT,
"socket disconnect", 0); "socket disconnect");
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
} }
break; break;
@ -636,8 +629,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
if (!session->packAdd_channel) { if (!session->packAdd_channel) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_UNKNOWN, libssh2_error(session, LIBSSH2_ERROR_CHANNEL_UNKNOWN,
"Packet received for unknown channel, ignoring", "Packet received for unknown channel, ignoring");
0);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->packAdd_state = libssh2_NB_state_idle; session->packAdd_state = libssh2_NB_state_idle;
return 0; return 0;
@ -693,7 +685,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
libssh2_error(session, libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
"Packet contains more data than we offered" "Packet contains more data than we offered"
" to receive, truncating", 0); " to receive, truncating");
datalen = datalen =
session->packAdd_channel->remote.packet_size + session->packAdd_channel->remote.packet_size +
session->packAdd_data_head; session->packAdd_data_head;
@ -706,8 +698,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
libssh2_error(session, libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED, LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED,
"The current receive window is full," "The current receive window is full,"
" data ignored", " data ignored");
0);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->packAdd_state = libssh2_NB_state_idle; session->packAdd_state = libssh2_NB_state_idle;
return 0; return 0;
@ -720,8 +711,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
libssh2_error(session, libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED, LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED,
"Remote sent more data than current " "Remote sent more data than current "
"window allows, truncating", "window allows, truncating");
0);
datalen = datalen =
session->packAdd_channel->remote.window_size + session->packAdd_channel->remote.window_size +
session->packAdd_data_head; session->packAdd_data_head;

View File

@ -101,18 +101,11 @@ static const LIBSSH2_PUBLICKEY_CODE_LIST publickey_status_codes[] = {
* *
* Format an error message from a status code * Format an error message from a status code
*/ */
#define LIBSSH2_PUBLICKEY_STATUS_TEXT_START "Publickey Subsystem Error: \""
#define LIBSSH2_PUBLICKEY_STATUS_TEXT_MID "\" Server Reports: \""
#define LIBSSH2_PUBLICKEY_STATUS_TEXT_END "\""
static void static void
publickey_status_error(const LIBSSH2_PUBLICKEY * pkey, publickey_status_error(const LIBSSH2_PUBLICKEY *pkey,
LIBSSH2_SESSION * session, int status, LIBSSH2_SESSION *session, int status)
const unsigned char *message, int message_len)
{ {
const char *status_text; const char *msg;
int status_text_len;
char *m, *s;
int m_len;
/* GENERAL_FAILURE got remapped between version 1 and 2 */ /* GENERAL_FAILURE got remapped between version 1 and 2 */
if (status == 6 && pkey && pkey->version == 1) { if (status == 6 && pkey && pkey->version == 1) {
@ -120,38 +113,12 @@ publickey_status_error(const LIBSSH2_PUBLICKEY * pkey,
} }
if (status < 0 || status > LIBSSH2_PUBLICKEY_STATUS_CODE_MAX) { if (status < 0 || status > LIBSSH2_PUBLICKEY_STATUS_CODE_MAX) {
status_text = "unknown"; msg = "unknown";
status_text_len = sizeof("unknown") - 1;
} else { } else {
status_text = publickey_status_codes[status].name; msg = publickey_status_codes[status].name;
status_text_len = publickey_status_codes[status].name_len;
} }
m_len = libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, msg);
(sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_START) - 1) + status_text_len +
(sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_MID) - 1) + message_len +
(sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_END) - 1);
m = LIBSSH2_ALLOC(session, m_len + 1);
if (!m) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for status message", 0);
return;
}
s = m;
memcpy(s, LIBSSH2_PUBLICKEY_STATUS_TEXT_START,
sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_START) - 1);
s += sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_START) - 1;
memcpy(s, status_text, status_text_len);
s += status_text_len;
memcpy(s, LIBSSH2_PUBLICKEY_STATUS_TEXT_MID,
sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_MID) - 1);
s += sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_MID) - 1;
memcpy(s, message, message_len);
s += message_len;
memcpy(s, LIBSSH2_PUBLICKEY_STATUS_TEXT_END,
sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_END) - 1);
s += sizeof(LIBSSH2_PUBLICKEY_STATUS_TEXT_END);
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, m, 1);
} }
/* /*
@ -173,18 +140,17 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc != 4) { } else if (rc != 4) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid response from publickey subsystem", 0); "Invalid response from publickey subsystem");
return -1;
} }
pkey->receive_packet_len = _libssh2_ntohu32(buffer); pkey->receive_packet_len = _libssh2_ntohu32(buffer);
pkey->receive_packet = pkey->receive_packet =
LIBSSH2_ALLOC(session, pkey->receive_packet_len); LIBSSH2_ALLOC(session, pkey->receive_packet_len);
if (!pkey->receive_packet) { if (!pkey->receive_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate publickey response buffer", 0); "Unable to allocate publickey response "
return -1; "buffer");
} }
pkey->receive_state = libssh2_NB_state_sent; pkey->receive_state = libssh2_NB_state_sent;
@ -196,13 +162,12 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc != (int)pkey->receive_packet_len) { } else if (rc != (int)pkey->receive_packet_len) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for publickey subsystem response packet",
0);
LIBSSH2_FREE(session, pkey->receive_packet); LIBSSH2_FREE(session, pkey->receive_packet);
pkey->receive_packet = NULL; pkey->receive_packet = NULL;
pkey->receive_state = libssh2_NB_state_idle; pkey->receive_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for publickey subsystem "
"response packet");
} }
*data = pkey->receive_packet; *data = pkey->receive_packet;
@ -268,60 +233,36 @@ publickey_response_success(LIBSSH2_PUBLICKEY * pkey)
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from publickey subsystem", "Timeout waiting for response from "
0); "publickey subsystem");
return -1;
} }
s = data; s = data;
if ((response = publickey_response_id(&s, data_len)) < 0) { if ((response = publickey_response_id(&s, data_len)) < 0) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code", 0);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
return -1; return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code");
} }
switch (response) { switch (response) {
case LIBSSH2_PUBLICKEY_RESPONSE_STATUS: case LIBSSH2_PUBLICKEY_RESPONSE_STATUS:
/* Error, or processing complete */ /* Error, or processing complete */
{ {
unsigned long status, descr_len, lang_len; unsigned long status = _libssh2_ntohu32(s);
unsigned char *descr, *lang;
status = _libssh2_ntohu32(s);
s += 4;
descr_len = _libssh2_ntohu32(s);
s += 4;
descr = s;
s += descr_len;
lang_len = _libssh2_ntohu32(s);
s += 4;
lang = s;
s += lang_len;
if (s > data + data_len) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Malformed publickey subsystem packet", 0);
LIBSSH2_FREE(session, data);
return -1;
}
if (status == LIBSSH2_PUBLICKEY_SUCCESS) {
LIBSSH2_FREE(session, data);
return 0;
}
publickey_status_error(pkey, session, status, descr,
descr_len);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
if (status == LIBSSH2_PUBLICKEY_SUCCESS)
return 0;
publickey_status_error(pkey, session, status);
return -1; return -1;
} }
default: default:
/* Unknown/Unexpected */ /* Unknown/Unexpected */
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Unexpected publickey subsystem response, ignoring", "Unexpected publickey subsystem response, ignoring");
0);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
data = NULL; data = NULL;
} }
@ -372,13 +313,13 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
/* The error state is already set, so leave it */ /* The error state is already set, so leave it */
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block to startup channel", 0); "Would block to startup channel");
return NULL; return NULL;
} else if (!session->pkeyInit_channel } else if (!session->pkeyInit_channel
&& (libssh2_session_last_errno(session) != && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to startup channel", 0); "Unable to startup channel");
goto err_exit; goto err_exit;
} }
} while (!session->pkeyInit_channel); } while (!session->pkeyInit_channel);
@ -393,11 +334,11 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
"publickey", strlen("publickey")); "publickey", strlen("publickey"));
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting publickey subsystem", 0); "Would block starting publickey subsystem");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to request publickey subsystem", 0); "Unable to request publickey subsystem");
goto err_exit; goto err_exit;
} }
@ -409,7 +350,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting publickey subsystem", 0); "Would block starting publickey subsystem");
return NULL; return NULL;
} }
@ -417,7 +358,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PUBLICKEY)); LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PUBLICKEY));
if (!session->pkeyInit_pkey) { if (!session->pkeyInit_pkey) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a new publickey structure", 0); "Unable to allocate a new publickey structure");
goto err_exit; goto err_exit;
} }
memset(session->pkeyInit_pkey, 0, sizeof(LIBSSH2_PUBLICKEY)); memset(session->pkeyInit_pkey, 0, sizeof(LIBSSH2_PUBLICKEY));
@ -446,11 +387,11 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
(char *) buffer, (s - buffer)); (char *) buffer, (s - buffer));
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending publickey version packet", 0); "Would block sending publickey version packet");
return NULL; return NULL;
} else if ((s - buffer) != rc) { } else if ((s - buffer) != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey version packet", 0); "Unable to send publickey version packet");
goto err_exit; goto err_exit;
} }
@ -464,13 +405,13 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
&session->pkeyInit_data_len); &session->pkeyInit_data_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from publickey subsystem", "Would block waiting for response from "
0); "publickey subsystem");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from publickey subsystem", "Timeout waiting for response from "
0); "publickey subsystem");
goto err_exit; goto err_exit;
} }
@ -478,7 +419,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
if ((response = if ((response =
publickey_response_id(&s, session->pkeyInit_data_len)) < 0) { publickey_response_id(&s, session->pkeyInit_data_len)) < 0) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code", 0); "Invalid publickey subsystem response code");
goto err_exit; goto err_exit;
} }
@ -504,13 +445,12 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
session->pkeyInit_data + session->pkeyInit_data_len) { session->pkeyInit_data + session->pkeyInit_data_len) {
libssh2_error(session, libssh2_error(session,
LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Malformed publickey subsystem packet", "Malformed publickey subsystem packet");
0);
goto err_exit; goto err_exit;
} }
publickey_status_error(NULL, session, status, publickey_status_error(NULL, session, status);
descr, descr_len);
goto err_exit; goto err_exit;
} }
@ -536,8 +476,8 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
default: default:
/* Unknown/Unexpected */ /* Unknown/Unexpected */
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Unexpected publickey subsystem response, ignoring", "Unexpected publickey subsystem response, "
0); "ignoring");
LIBSSH2_FREE(session, session->pkeyInit_data); LIBSSH2_FREE(session, session->pkeyInit_data);
session->pkeyInit_data = NULL; session->pkeyInit_data = NULL;
} }
@ -551,7 +491,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
rc = libssh2_channel_close(session->pkeyInit_channel); rc = libssh2_channel_close(session->pkeyInit_channel);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block closing channel", 0); "Would block closing channel");
return NULL; return NULL;
} }
} }
@ -615,10 +555,9 @@ libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY * pkey, const unsigned char *name,
pkey->add_packet = LIBSSH2_ALLOC(session, packet_len); pkey->add_packet = LIBSSH2_ALLOC(session, packet_len);
if (!pkey->add_packet) { if (!pkey->add_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey \"add\" packet", "Unable to allocate memory for "
0); "publickey \"add\" packet");
return -1;
} }
pkey->add_s = pkey->add_packet; pkey->add_s = pkey->add_packet;
@ -684,11 +623,10 @@ libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY * pkey, const unsigned char *name,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if ((pkey->add_s - pkey->add_packet) != rc) { } else if ((pkey->add_s - pkey->add_packet) != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey add packet", 0);
LIBSSH2_FREE(session, pkey->add_packet); LIBSSH2_FREE(session, pkey->add_packet);
pkey->add_packet = NULL; pkey->add_packet = NULL;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey add packet");
} }
LIBSSH2_FREE(session, pkey->add_packet); LIBSSH2_FREE(session, pkey->add_packet);
pkey->add_packet = NULL; pkey->add_packet = NULL;
@ -725,10 +663,9 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey,
pkey->remove_packet = LIBSSH2_ALLOC(session, packet_len); pkey->remove_packet = LIBSSH2_ALLOC(session, packet_len);
if (!pkey->remove_packet) { if (!pkey->remove_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey \"remove\" packet", "Unable to allocate memory for "
0); "publickey \"remove\" packet");
return -1;
} }
pkey->remove_s = pkey->remove_packet; pkey->remove_s = pkey->remove_packet;
@ -760,12 +697,11 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if ((pkey->remove_s - pkey->remove_packet) != rc) { } else if ((pkey->remove_s - pkey->remove_packet) != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey remove packet", 0);
LIBSSH2_FREE(session, pkey->remove_packet); LIBSSH2_FREE(session, pkey->remove_packet);
pkey->remove_packet = NULL; pkey->remove_packet = NULL;
pkey->remove_state = libssh2_NB_state_idle; pkey->remove_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey remove packet");
} }
LIBSSH2_FREE(session, pkey->remove_packet); LIBSSH2_FREE(session, pkey->remove_packet);
pkey->remove_packet = NULL; pkey->remove_packet = NULL;
@ -823,10 +759,9 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if ((pkey->listFetch_s - pkey->listFetch_buffer) != rc) { } else if ((pkey->listFetch_s - pkey->listFetch_buffer) != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey list packet", 0);
pkey->listFetch_state = libssh2_NB_state_idle; pkey->listFetch_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey list packet");
} }
pkey->listFetch_state = libssh2_NB_state_sent; pkey->listFetch_state = libssh2_NB_state_sent;
@ -839,8 +774,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from publickey subsystem", "Timeout waiting for response from "
0); "publickey subsystem");
goto err_exit; goto err_exit;
} }
@ -849,7 +784,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
publickey_response_id(&pkey->listFetch_s, publickey_response_id(&pkey->listFetch_s,
pkey->listFetch_data_len)) < 0) { pkey->listFetch_data_len)) < 0) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code", 0); "Invalid publickey subsystem response code");
goto err_exit; goto err_exit;
} }
@ -874,7 +809,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
if (pkey->listFetch_s > if (pkey->listFetch_s >
pkey->listFetch_data + pkey->listFetch_data_len) { pkey->listFetch_data + pkey->listFetch_data_len) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Malformed publickey subsystem packet", 0); "Malformed publickey subsystem packet");
goto err_exit; goto err_exit;
} }
@ -887,8 +822,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
return 0; return 0;
} }
publickey_status_error(pkey, session, status, descr, publickey_status_error(pkey, session, status);
descr_len);
goto err_exit; goto err_exit;
} }
case LIBSSH2_PUBLICKEY_RESPONSE_PUBLICKEY: case LIBSSH2_PUBLICKEY_RESPONSE_PUBLICKEY:
@ -903,8 +837,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
1) * sizeof(libssh2_publickey_list)); 1) * sizeof(libssh2_publickey_list));
if (!newlist) { if (!newlist) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey list", "Unable to allocate memory for "
0); "publickey list");
goto err_exit; goto err_exit;
} }
list = newlist; list = newlist;
@ -921,8 +855,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
sizeof(libssh2_publickey_attribute)); sizeof(libssh2_publickey_attribute));
if (!list[keys].attrs) { if (!list[keys].attrs) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey attributes", "Unable to allocate memory for "
0); "publickey attributes");
goto err_exit; goto err_exit;
} }
list[keys].attrs[0].name = "comment"; list[keys].attrs[0].name = "comment";
@ -963,8 +897,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
sizeof(libssh2_publickey_attribute)); sizeof(libssh2_publickey_attribute));
if (!list[keys].attrs) { if (!list[keys].attrs) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey attributes", "Unable to allocate memory for "
0); "publickey attributes");
goto err_exit; goto err_exit;
} }
for(i = 0; i < list[keys].num_attrs; i++) { for(i = 0; i < list[keys].num_attrs; i++) {
@ -993,8 +927,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
default: default:
/* Unknown/Unexpected */ /* Unknown/Unexpected */
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Unexpected publickey subsystem response, ignoring", "Unexpected publickey subsystem response, ignoring");
0);
LIBSSH2_FREE(session, pkey->listFetch_data); LIBSSH2_FREE(session, pkey->listFetch_data);
pkey->listFetch_data = NULL; pkey->listFetch_data = NULL;
} }

211
src/scp.c
View File

@ -44,7 +44,7 @@
/* Max. length of a quoted string after libssh2_shell_quotearg() processing */ /* Max. length of a quoted string after libssh2_shell_quotearg() processing */
#define libssh2_shell_quotedsize(s) (3 * strlen(s) + 2) #define libssh2_shell_quotedsize(s) (3 * strlen(s) + 2)
/* /*
This function quotes a string in a way suitable to be used with a This function quotes a string in a way suitable to be used with a
@ -59,32 +59,32 @@
The following special cases are handled: The following special cases are handled:
o If the string contains an apostrophy itself, the apostrophy o If the string contains an apostrophy itself, the apostrophy
character is written in quotation marks, e.g. "'". character is written in quotation marks, e.g. "'".
The shell cannot handle the syntax 'doesn\'t', so we close the The shell cannot handle the syntax 'doesn\'t', so we close the
current argument word, add the apostrophe in quotation marks "", current argument word, add the apostrophe in quotation marks "",
and open a new argument word instead (_ indicate the input and open a new argument word instead (_ indicate the input
string characters): string characters):
_____ _ _ _____ _ _
'doesn' "'" 't' 'doesn' "'" 't'
Sequences of apostrophes are combined in one pair of quotation marks: Sequences of apostrophes are combined in one pair of quotation marks:
a'''b a'''b
becomes becomes
_ ___ _ _ ___ _
'a'"'''"'b' 'a'"'''"'b'
o If the string contains an exclamation mark (!), the C-Shell o If the string contains an exclamation mark (!), the C-Shell
interprets it as an event number. Using \! (not within quotation interprets it as an event number. Using \! (not within quotation
marks or single quotation marks) is a mechanism understood by marks or single quotation marks) is a mechanism understood by
both Bourne Shell and C-Shell. both Bourne Shell and C-Shell.
If a quotation was already started, the argument word is closed If a quotation was already started, the argument word is closed
first: first:
a!b a!b
become become
_ _ _ _ _ _
'a'\!'b' 'a'\!'b'
The result buffer must be large enough for the expanded result. A The result buffer must be large enough for the expanded result. A
bad case regarding expansion is alternating characters and bad case regarding expansion is alternating characters and
@ -96,7 +96,7 @@
This is the worst case. This is the worst case.
Maximum length of the result: Maximum length of the result:
1 + 6 * (length(input) + 1) / 2) + 1 1 + 6 * (length(input) + 1) / 2) + 1
=> 3 * length(input) + 2 => 3 * length(input) + 2
@ -130,8 +130,8 @@ libssh2_shell_quotearg(const char *path, unsigned char *buf,
/* /*
* Processing States: * Processing States:
* UQSTRING: unquoted string: ... -- used for quoting exclamation * UQSTRING: unquoted string: ... -- used for quoting exclamation
* marks. This is the initial state * marks. This is the initial state
* SQSTRING: single-qouted-string: '... -- any character may follow * SQSTRING: single-qouted-string: '... -- any character may follow
* QSTRING: quoted string: "... -- only apostrophes may follow * QSTRING: quoted string: "... -- only apostrophes may follow
*/ */
@ -142,23 +142,23 @@ libssh2_shell_quotearg(const char *path, unsigned char *buf,
dst = buf; dst = buf;
while (*src && dst < endp - 1) { while (*src && dst < endp - 1) {
switch (*src) { switch (*src) {
/* /*
* Special handling for apostrophe. * Special handling for apostrophe.
* An apostrophe is always written in quotation marks, e.g. * An apostrophe is always written in quotation marks, e.g.
* ' -> "'". * ' -> "'".
*/ */
case '\'': case '\'':
switch (state) { switch (state) {
case UQSTRING: /* Unquoted string */ case UQSTRING: /* Unquoted string */
if (dst+1 >= endp) if (dst+1 >= endp)
return 0; return 0;
*dst++ = '"'; *dst++ = '"';
break; break;
case QSTRING: /* Continue quoted string */ case QSTRING: /* Continue quoted string */
break; break;
case SQSTRING: /* Close single quoted string */ case SQSTRING: /* Close single quoted string */
if (dst+2 >= endp) if (dst+2 >= endp)
return 0; return 0;
*dst++ = '\''; *dst++ = '\'';
@ -187,10 +187,10 @@ libssh2_shell_quotearg(const char *path, unsigned char *buf,
case QSTRING: case QSTRING:
if (dst+2 >= endp) if (dst+2 >= endp)
return 0; return 0;
*dst++ = '"'; /* Closing quotation mark */ *dst++ = '"'; /* Closing quotation mark */
*dst++ = '\\'; *dst++ = '\\';
break; break;
case SQSTRING: /* Close single quoted string */ case SQSTRING: /* Close single quoted string */
if (dst+2 >= endp) if (dst+2 >= endp)
return 0; return 0;
*dst++ = '\''; *dst++ = '\'';
@ -202,9 +202,9 @@ libssh2_shell_quotearg(const char *path, unsigned char *buf,
state = UQSTRING; state = UQSTRING;
break; break;
/* /*
* Ordinary character: prefer single-quoted string * Ordinary character: prefer single-quoted string
*/ */
default: default:
switch (state) { switch (state) {
@ -216,17 +216,17 @@ libssh2_shell_quotearg(const char *path, unsigned char *buf,
case QSTRING: case QSTRING:
if (dst+2 >= endp) if (dst+2 >= endp)
return 0; return 0;
*dst++ = '"'; /* Closing quotation mark */ *dst++ = '"'; /* Closing quotation mark */
*dst++ = '\''; *dst++ = '\'';
break; break;
case SQSTRING: /* Continue single quoted string */ case SQSTRING: /* Continue single quoted string */
break; break;
default: default:
break; break;
} }
state = SQSTRING; /* Start single-quoted string */ state = SQSTRING; /* Start single-quoted string */
break; break;
} }
if (dst+1 >= endp) if (dst+1 >= endp)
return 0; return 0;
@ -234,7 +234,7 @@ libssh2_shell_quotearg(const char *path, unsigned char *buf,
} }
switch (state) { switch (state) {
case UQSTRING: case UQSTRING:
break; break;
case QSTRING: /* Close quoted string */ case QSTRING: /* Close quoted string */
if (dst+1 >= endp) if (dst+1 >= endp)
@ -286,8 +286,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!session->scpRecv_command) { if (!session->scpRecv_command) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a command buffer for SCP session", "Unable to allocate a command buffer for "
0); "SCP session");
return NULL; return NULL;
} }
@ -324,7 +324,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
} }
else { else {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting up channel", 0); "Would block starting up channel");
} }
return NULL; return NULL;
} }
@ -340,7 +340,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_command_len); session->scpRecv_command_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting SCP startup", 0); "Would block requesting SCP startup");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, session->scpRecv_command); LIBSSH2_FREE(session, session->scpRecv_command);
@ -362,7 +362,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
(char *) session->scpRecv_response, 1); (char *) session->scpRecv_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending initial wakeup", 0); "Would block sending initial wakeup");
return NULL; return NULL;
} else if (rc != 1) { } else if (rc != 1) {
goto scp_recv_error; goto scp_recv_error;
@ -387,12 +387,12 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_response_len, 1); session->scpRecv_response_len, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for SCP response", 0); "Would block waiting for SCP response");
return NULL; return NULL;
} else if (rc <= 0) { } else if (rc <= 0) {
/* Timeout, give up */ /* Timeout, give up */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Timed out waiting for SCP response", 0); "Timed out waiting for SCP response");
goto scp_recv_error; goto scp_recv_error;
} }
session->scpRecv_response_len++; session->scpRecv_response_len++;
@ -403,8 +403,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
* we are successful it will be replaced * we are successful it will be replaced
*/ */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid data in SCP response, missing Time data", "Invalid data in SCP response, missing Time data");
0);
session->scpRecv_err_len = session->scpRecv_err_len =
_libssh2_channel_packet_data_len(session-> _libssh2_channel_packet_data_len(session->
@ -427,16 +426,18 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
* it is already in the systems so it can't return * it is already in the systems so it can't return
* PACKET_EAGAIN * PACKET_EAGAIN
*/ */
LIBSSH2_FREE(session, session->scpRecv_err_msg);
session->scpRecv_err_msg = NULL;
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unknown error while getting error string", "Unknown error" );
0);
goto scp_recv_error;
} }
else
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"SCP protocol error");
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, /* TODO: for debugging purposes, the
session->scpRecv_err_msg, 1); session->scpRecv_err_msg should be displayed here
when available */
LIBSSH2_FREE(session, session->scpRecv_err_msg);
session->scpRecv_err_msg = NULL; session->scpRecv_err_msg = NULL;
goto scp_recv_error; goto scp_recv_error;
} }
@ -458,7 +459,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
scpRecv_response[session->scpRecv_response_len - 1] != scpRecv_response[session->scpRecv_response_len - 1] !=
'\n')) { '\n')) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid data in SCP response", 0); "Invalid data in SCP response");
goto scp_recv_error; goto scp_recv_error;
} }
@ -470,8 +471,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
LIBSSH2_SCP_RESPONSE_BUFLEN) { LIBSSH2_SCP_RESPONSE_BUFLEN) {
/* You had your chance */ /* You had your chance */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unterminated response from SCP server", "Unterminated response from SCP server");
0);
goto scp_recv_error; goto scp_recv_error;
} }
/* Way too short to be an SCP response, or not done yet, /* Way too short to be an SCP response, or not done yet,
@ -494,8 +494,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (session->scpRecv_response_len < 8) { if (session->scpRecv_response_len < 8) {
/* EOL came too soon */ /* EOL came too soon */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short", "Invalid response from SCP server, "
0); "too short" );
goto scp_recv_error; goto scp_recv_error;
} }
@ -505,8 +505,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!p || ((p - s) <= 0)) { if (!p || ((p - s) <= 0)) {
/* No spaces or space in the wrong spot */ /* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, malformed mtime", "Invalid response from SCP server, "
0); "malformed mtime");
goto scp_recv_error; goto scp_recv_error;
} }
@ -516,16 +516,14 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_mtime = strtol((char *) s, NULL, 10); session->scpRecv_mtime = strtol((char *) s, NULL, 10);
if (errno) { if (errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid mtime", "Invalid response from SCP server, invalid mtime");
0);
goto scp_recv_error; goto scp_recv_error;
} }
s = (unsigned char *) strchr((char *) p, ' '); s = (unsigned char *) strchr((char *) p, ' ');
if (!s || ((s - p) <= 0)) { if (!s || ((s - p) <= 0)) {
/* No spaces or space in the wrong spot */ /* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, malformed mtime.usec", "Invalid response from SCP server, malformed mtime.usec");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -535,8 +533,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!p || ((p - s) <= 0)) { if (!p || ((p - s) <= 0)) {
/* No spaces or space in the wrong spot */ /* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short or malformed", "Invalid response from SCP server, too short or malformed");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -546,8 +543,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_atime = strtol((char *) s, NULL, 10); session->scpRecv_atime = strtol((char *) s, NULL, 10);
if (errno) { if (errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid atime", "Invalid response from SCP server, invalid atime");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -563,7 +559,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
scpRecv_response, 1); scpRecv_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting to send SCP ACK", 0); "Would block waiting to send SCP ACK");
return NULL; return NULL;
} else if (rc != 1) { } else if (rc != 1) {
goto scp_recv_error; goto scp_recv_error;
@ -600,19 +596,19 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_response_len, 1); session->scpRecv_response_len, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for SCP response", 0); "Would block waiting for SCP response");
return NULL; return NULL;
} else if (rc <= 0) { } else if (rc <= 0) {
/* Timeout, give up */ /* Timeout, give up */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Timed out waiting for SCP response", 0); "Timed out waiting for SCP response");
goto scp_recv_error; goto scp_recv_error;
} }
session->scpRecv_response_len++; session->scpRecv_response_len++;
if (session->scpRecv_response[0] != 'C') { if (session->scpRecv_response[0] != 'C') {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server", 0); "Invalid response from SCP server");
goto scp_recv_error; goto scp_recv_error;
} }
@ -630,7 +626,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
scpRecv_response[session->scpRecv_response_len - 1] > scpRecv_response[session->scpRecv_response_len - 1] >
126))) { 126))) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid data in SCP response", 0); "Invalid data in SCP response");
goto scp_recv_error; goto scp_recv_error;
} }
@ -642,8 +638,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
LIBSSH2_SCP_RESPONSE_BUFLEN) { LIBSSH2_SCP_RESPONSE_BUFLEN) {
/* You had your chance */ /* You had your chance */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unterminated response from SCP server", "Unterminated response from SCP server");
0);
goto scp_recv_error; goto scp_recv_error;
} }
/* Way too short to be an SCP response, or not done yet, /* Way too short to be an SCP response, or not done yet,
@ -667,8 +662,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (session->scpRecv_response_len < 6) { if (session->scpRecv_response_len < 6) {
/* EOL came too soon */ /* EOL came too soon */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short", "Invalid response from SCP server, too short");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -678,8 +672,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!p || ((p - s) <= 0)) { if (!p || ((p - s) <= 0)) {
/* No spaces or space in the wrong spot */ /* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, malformed mode", "Invalid response from SCP server, malformed mode");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -689,8 +682,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_mode = strtol(s, &e, 8); session->scpRecv_mode = strtol(s, &e, 8);
if ((e && *e) || errno) { if ((e && *e) || errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid mode", "Invalid response from SCP server, invalid mode");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -698,8 +690,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!s || ((s - p) <= 0)) { if (!s || ((s - p) <= 0)) {
/* No spaces or space in the wrong spot */ /* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short or malformed", "Invalid response from SCP server, too short or malformed");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -709,8 +700,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_size = scpsize_strtol(p, &e, 10); session->scpRecv_size = scpsize_strtol(p, &e, 10);
if ((e && *e) || errno) { if ((e && *e) || errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid size", "Invalid response from SCP server, invalid size");
0);
goto scp_recv_error; goto scp_recv_error;
} }
@ -726,7 +716,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
scpRecv_response, 1); scpRecv_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending SCP ACK", 0); "Would block sending SCP ACK");
return NULL; return NULL;
} else if (rc != 1) { } else if (rc != 1) {
goto scp_recv_error; goto scp_recv_error;
@ -800,8 +790,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
LIBSSH2_ALLOC(session, session->scpSend_command_len); LIBSSH2_ALLOC(session, session->scpSend_command_len);
if (!session->scpSend_command) { if (!session->scpSend_command) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a command buffer for scp session", "Unable to allocate a command buffer for scp session");
0);
return NULL; return NULL;
} }
@ -838,7 +827,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
} }
else { else {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting up channel", 0); "Would block starting up channel");
} }
return NULL; return NULL;
} }
@ -854,7 +843,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
session->scpSend_command_len); session->scpSend_command_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting SCP startup", 0); "Would block requesting SCP startup");
return NULL; return NULL;
} }
else if (rc) { else if (rc) {
@ -863,7 +852,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
LIBSSH2_FREE(session, session->scpSend_command); LIBSSH2_FREE(session, session->scpSend_command);
session->scpSend_command = NULL; session->scpSend_command = NULL;
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unknown error while getting error string", 0); "Unknown error while getting error string");
goto scp_send_error; goto scp_send_error;
} }
LIBSSH2_FREE(session, session->scpSend_command); LIBSSH2_FREE(session, session->scpSend_command);
@ -878,11 +867,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
(char *) session->scpSend_response, 1); (char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from remote", 0); "Would block waiting for response from remote");
return NULL; return NULL;
} else if ((rc <= 0) || (session->scpSend_response[0] != 0)) { } else if ((rc <= 0) || (session->scpSend_response[0] != 0)) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0); "Invalid ACK response from remote");
goto scp_send_error; goto scp_send_error;
} }
@ -907,11 +896,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
session->scpSend_response_len); session->scpSend_response_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending time data for SCP file", 0); "Would block sending time data for SCP file");
return NULL; return NULL;
} else if (rc != (int)session->scpSend_response_len) { } else if (rc != (int)session->scpSend_response_len) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send time data for SCP file", 0); "Unable to send time data for SCP file");
goto scp_send_error; goto scp_send_error;
} }
@ -924,11 +913,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
(char *) session->scpSend_response, 1); (char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response", 0); "Would block waiting for response");
return NULL; return NULL;
} else if ((rc <= 0) || (session->scpSend_response[0] != 0)) { } else if ((rc <= 0) || (session->scpSend_response[0] != 0)) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0); "Invalid ACK response from remote");
goto scp_send_error; goto scp_send_error;
} }
@ -965,11 +954,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
session->scpSend_response_len); session->scpSend_response_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block send core file data for SCP file", 0); "Would block send core file data for SCP file");
return NULL; return NULL;
} else if (rc != (int)session->scpSend_response_len) { } else if (rc != (int)session->scpSend_response_len) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send core file data for SCP file", 0); "Unable to send core file data for SCP file");
goto scp_send_error; goto scp_send_error;
} }
@ -982,11 +971,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
(char *) session->scpSend_response, 1); (char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response", 0); "Would block waiting for response");
return NULL; return NULL;
} else if (rc <= 0) { } else if (rc <= 0) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0); "Invalid ACK response from remote");
goto scp_send_error; goto scp_send_error;
} else if (session->scpSend_response[0] != 0) { } else if (session->scpSend_response[0] != 0) {
/* /*
@ -994,7 +983,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
* we are successful it will be replaced * we are successful it will be replaced
*/ */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0); "Invalid ACK response from remote");
session->scpSend_err_len = session->scpSend_err_len =
_libssh2_channel_packet_data_len(session->scpSend_channel, 0); _libssh2_channel_packet_data_len(session->scpSend_channel, 0);
@ -1020,7 +1009,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
} }
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
session->scpSend_err_msg, 1); "failed waiting for ACK");
session->scpSend_err_msg = NULL; session->scpSend_err_msg = NULL;
goto scp_send_error; goto scp_send_error;
} }

View File

@ -85,7 +85,7 @@ LIBSSH2_REALLOC_FUNC(libssh2_default_realloc)
* *
* Wait for a hello from the remote host * Wait for a hello from the remote host
* Allocate a buffer and store the banner in session->remote.banner * Allocate a buffer and store the banner in session->remote.banner
* Returns: 0 on success, PACKET_EAGAIN if read would block, 1 on failure * Returns: 0 on success, PACKET_EAGAIN if read would block, negative on failure
*/ */
static int static int
banner_receive(LIBSSH2_SESSION * session) banner_receive(LIBSSH2_SESSION * session)
@ -129,7 +129,7 @@ banner_receive(LIBSSH2_SESSION * session)
/* Some kinda error */ /* Some kinda error */
session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_state = libssh2_NB_state_idle;
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
return 1; return -1;
} }
if (ret == 0) { if (ret == 0) {
@ -141,7 +141,7 @@ banner_receive(LIBSSH2_SESSION * session)
/* NULLs are not allowed in SSH banners */ /* NULLs are not allowed in SSH banners */
session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_state = libssh2_NB_state_idle;
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
return 1; return -1;
} }
session->banner_TxRx_banner[banner_len++] = c; session->banner_TxRx_banner[banner_len++] = c;
@ -158,13 +158,12 @@ banner_receive(LIBSSH2_SESSION * session)
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
if (!banner_len) if (!banner_len)
return 1; return -1;
session->remote.banner = LIBSSH2_ALLOC(session, banner_len + 1); session->remote.banner = LIBSSH2_ALLOC(session, banner_len + 1);
if (!session->remote.banner) { if (!session->remote.banner) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Error allocating space for remote banner", 0); "Error allocating space for remote banner");
return 1;
} }
memcpy(session->remote.banner, session->banner_TxRx_banner, banner_len); memcpy(session->remote.banner, session->banner_TxRx_banner, banner_len);
session->remote.banner[banner_len] = '\0'; session->remote.banner[banner_len] = '\0';
@ -403,9 +402,8 @@ libssh2_banner_set(LIBSSH2_SESSION * session, const char *banner)
session->local.banner = LIBSSH2_ALLOC(session, banner_len + 3); session->local.banner = LIBSSH2_ALLOC(session, banner_len + 3);
if (!session->local.banner) { if (!session->local.banner) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for local banner", 0); "Unable to allocate memory for local banner");
return -1;
} }
memcpy(session->local.banner, banner, banner_len); memcpy(session->local.banner, banner, banner_len);
@ -559,9 +557,8 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
"session_startup for socket %d", sock); "session_startup for socket %d", sock);
if (INVALID_SOCKET == sock) { if (INVALID_SOCKET == sock) {
/* Did we forget something? */ /* Did we forget something? */
libssh2_error(session, LIBSSH2_ERROR_SOCKET_NONE, return libssh2_error(session, LIBSSH2_ERROR_SOCKET_NONE,
"Bad socket provided", 0); "Bad socket provided");
return LIBSSH2_ERROR_SOCKET_NONE;
} }
session->socket_fd = sock; session->socket_fd = sock;
@ -579,9 +576,8 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
if (session->startup_state == libssh2_NB_state_created) { if (session->startup_state == libssh2_NB_state_created) {
rc = banner_send(session); rc = banner_send(session);
if (rc) { if (rc) {
libssh2_error(session, rc, return libssh2_error(session, rc,
"Failed sending banner", 0); "Failed sending banner");
return rc;
} }
session->startup_state = libssh2_NB_state_sent; session->startup_state = libssh2_NB_state_sent;
} }
@ -589,9 +585,8 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
if (session->startup_state == libssh2_NB_state_sent) { if (session->startup_state == libssh2_NB_state_sent) {
rc = banner_receive(session); rc = banner_receive(session);
if (rc) { if (rc) {
libssh2_error(session, rc, return libssh2_error(session, rc,
"Failed getting banner", 0); "Failed getting banner");
return rc;
} }
session->startup_state = libssh2_NB_state_sent1; session->startup_state = libssh2_NB_state_sent1;
@ -600,9 +595,8 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
if (session->startup_state == libssh2_NB_state_sent1) { if (session->startup_state == libssh2_NB_state_sent1) {
rc = libssh2_kex_exchange(session, 0, &session->startup_key_state); rc = libssh2_kex_exchange(session, 0, &session->startup_key_state);
if (rc) { if (rc) {
libssh2_error(session, rc, return libssh2_error(session, rc,
"Unable to exchange encryption keys", 0); "Unable to exchange encryption keys");
return rc;
} }
session->startup_state = libssh2_NB_state_sent2; session->startup_state = libssh2_NB_state_sent2;
@ -626,9 +620,8 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
rc = _libssh2_transport_write(session, session->startup_service, rc = _libssh2_transport_write(session, session->startup_service,
sizeof("ssh-userauth") + 5 - 1); sizeof("ssh-userauth") + 5 - 1);
if (rc) { if (rc) {
libssh2_error(session, rc, return libssh2_error(session, rc,
"Unable to ask for ssh-userauth service", 0); "Unable to ask for ssh-userauth service");
return rc;
} }
session->startup_state = libssh2_NB_state_sent4; session->startup_state = libssh2_NB_state_sent4;
@ -650,9 +643,8 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
session->startup_service_length)) { session->startup_service_length)) {
LIBSSH2_FREE(session, session->startup_data); LIBSSH2_FREE(session, session->startup_data);
session->startup_data = NULL; session->startup_data = NULL;
libssh2_error(session, LIBSSH2_ERROR_PROTO, return libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Invalid response received from server", 0); "Invalid response received from server");
return LIBSSH2_ERROR_PROTO;
} }
LIBSSH2_FREE(session, session->startup_data); LIBSSH2_FREE(session, session->startup_data);
session->startup_data = NULL; session->startup_data = NULL;
@ -905,11 +897,6 @@ session_free(LIBSSH2_SESSION *session)
LIBSSH2_FREE(session, session->scpSend_err_msg); LIBSSH2_FREE(session, session->scpSend_err_msg);
} }
/* Free the error message, if we ar supposed to */
if (session->err_msg && session->err_should_free) {
LIBSSH2_FREE(session, session->err_msg);
}
/* Cleanup all remaining packets */ /* Cleanup all remaining packets */
while ((pkg = _libssh2_list_first(&session->packets))) { while ((pkg = _libssh2_list_first(&session->packets))) {
/* unlink the node */ /* unlink the node */
@ -981,11 +968,10 @@ session_disconnect(LIBSSH2_SESSION *session, int reason,
s = session->disconnect_data = s = session->disconnect_data =
LIBSSH2_ALLOC(session, session->disconnect_data_len); LIBSSH2_ALLOC(session, session->disconnect_data_len);
if (!session->disconnect_data) { if (!session->disconnect_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for disconnect packet",
0);
session->disconnect_state = libssh2_NB_state_idle; session->disconnect_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"disconnect packet");
} }
*(s++) = SSH_MSG_DISCONNECT; *(s++) = SSH_MSG_DISCONNECT;
@ -1085,22 +1071,19 @@ libssh2_session_methods(LIBSSH2_SESSION * session, int method_type)
case LIBSSH2_METHOD_LANG_CS: case LIBSSH2_METHOD_LANG_CS:
return ""; return "";
break;
case LIBSSH2_METHOD_LANG_SC: case LIBSSH2_METHOD_LANG_SC:
return ""; return "";
break;
default: default:
libssh2_error(session, LIBSSH2_ERROR_INVAL, libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid parameter specified for method_type", 0); "Invalid parameter specified for method_type");
return NULL; return NULL;
break;
} }
if (!method) { if (!method) {
libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE, libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE,
"No method negotiated", 0); "No method negotiated");
return NULL; return NULL;
} }
@ -1126,6 +1109,8 @@ LIBSSH2_API int
libssh2_session_last_error(LIBSSH2_SESSION * session, char **errmsg, libssh2_session_last_error(LIBSSH2_SESSION * session, char **errmsg,
int *errmsg_len, int want_buf) int *errmsg_len, int want_buf)
{ {
size_t msglen = 0;
/* No error to report */ /* No error to report */
if (!session->err_code) { if (!session->err_code) {
if (errmsg) { if (errmsg) {
@ -1145,29 +1130,24 @@ libssh2_session_last_error(LIBSSH2_SESSION * session, char **errmsg,
} }
if (errmsg) { if (errmsg) {
char *serrmsg = session->err_msg ? session->err_msg : (char *) ""; const char *error = session->err_msg ? session->err_msg : "";
int ownbuf = session->err_msg ? session->err_should_free : 0;
msglen = strlen(error);
if (want_buf) { if (want_buf) {
if (ownbuf) { /* Make a copy so the calling program can own it */
/* Just give the calling program the buffer */ *errmsg = LIBSSH2_ALLOC(session, msglen + 1);
*errmsg = serrmsg; if (*errmsg) {
session->err_should_free = 0; memcpy(*errmsg, error, msglen);
} else { (*errmsg)[msglen] = 0;
/* Make a copy so the calling program can own it */
*errmsg = LIBSSH2_ALLOC(session, session->err_msglen + 1);
if (*errmsg) {
memcpy(*errmsg, session->err_msg, session->err_msglen);
(*errmsg)[session->err_msglen] = 0;
}
} }
} else {
*errmsg = serrmsg;
} }
else
*errmsg = (char *)error;
} }
if (errmsg_len) { if (errmsg_len) {
*errmsg_len = session->err_msglen; *errmsg_len = msglen;
} }
return session->err_code; return session->err_code;
@ -1344,8 +1324,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
default: default:
if (session) if (session)
libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE,
"Invalid descriptor passed to libssh2_poll()", "Invalid descriptor passed to libssh2_poll()");
0);
return -1; return -1;
} }
} }
@ -1392,8 +1371,7 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
default: default:
if (session) if (session)
libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE,
"Invalid descriptor passed to libssh2_poll()", "Invalid descriptor passed to libssh2_poll()");
0);
return -1; return -1;
} }
} }

View File

@ -122,9 +122,8 @@ sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data,
(int) data[0], data_len); (int) data[0], data_len);
packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PACKET)); packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PACKET));
if (!packet) { if (!packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate datablock for SFTP packet", 0); "Unable to allocate datablock for SFTP packet");
return LIBSSH2_ERROR_ALLOC;
} }
memset(packet, 0, sizeof(LIBSSH2_PACKET)); memset(packet, 0, sizeof(LIBSSH2_PACKET));
@ -175,25 +174,22 @@ sftp_packet_read(LIBSSH2_SFTP *sftp)
/* TODO: this is stupid since we can in fact get 1-3 bytes in a /* TODO: this is stupid since we can in fact get 1-3 bytes in a
legitimate working case as well if the connection happens to be legitimate working case as well if the connection happens to be
super slow or something */ super slow or something */
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Read part of packet", 0); "Read part of packet");
return LIBSSH2_ERROR_CHANNEL_FAILURE;
} }
packet_len = _libssh2_ntohu32(buffer); packet_len = _libssh2_ntohu32(buffer);
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"Data begin - Packet Length: %lu", packet_len); "Data begin - Packet Length: %lu", packet_len);
if (packet_len > LIBSSH2_SFTP_PACKET_MAXLEN) { if (packet_len > LIBSSH2_SFTP_PACKET_MAXLEN) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
"SFTP packet too large", 0); "SFTP packet too large");
return LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED;
} }
packet = LIBSSH2_ALLOC(session, packet_len); packet = LIBSSH2_ALLOC(session, packet_len);
if (!packet) { if (!packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate SFTP packet", 0); "Unable to allocate SFTP packet");
return LIBSSH2_ERROR_ALLOC;
} }
packet_received = 0; packet_received = 0;
@ -219,10 +215,9 @@ sftp_packet_read(LIBSSH2_SFTP *sftp)
return bytes_received; return bytes_received;
} }
else if (bytes_received < 0) { else if (bytes_received < 0) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Receive error waiting for SFTP packet", 0);
LIBSSH2_FREE(session, packet); LIBSSH2_FREE(session, packet);
return bytes_received; return libssh2_error(session, bytes_received,
"Receive error waiting for SFTP packet");
} }
packet_received += bytes_received; packet_received += bytes_received;
} }
@ -560,11 +555,11 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
if (!session->sftpInit_channel) { if (!session->sftpInit_channel) {
if (libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) { if (libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting up channel", 0); "Would block starting up channel");
} }
else { else {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to startup channel", 0); "Unable to startup channel");
session->sftpInit_state = libssh2_NB_state_idle; session->sftpInit_state = libssh2_NB_state_idle;
} }
return NULL; return NULL;
@ -580,11 +575,11 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
strlen("sftp")); strlen("sftp"));
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block to request SFTP subsystem", 0); "Would block to request SFTP subsystem");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to request SFTP subsystem", 0); "Unable to request SFTP subsystem");
goto sftp_init_error; goto sftp_init_error;
} }
@ -596,7 +591,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting handle extended data", 0); "Would block requesting handle extended data");
return NULL; return NULL;
} }
@ -605,7 +600,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP)); LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP));
if (!sftp_handle) { if (!sftp_handle) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a new SFTP structure", 0); "Unable to allocate a new SFTP structure");
goto sftp_init_error; goto sftp_init_error;
} }
memset(sftp_handle, 0, sizeof(LIBSSH2_SFTP)); memset(sftp_handle, 0, sizeof(LIBSSH2_SFTP));
@ -628,11 +623,11 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
(char *) session->sftpInit_buffer, 9); (char *) session->sftpInit_buffer, 9);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending SSH_FXP_INIT", 0); "Would block sending SSH_FXP_INIT");
return NULL; return NULL;
} else if (9 != rc) { } else if (9 != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send SSH_FXP_INIT", 0); "Unable to send SSH_FXP_INIT");
goto sftp_init_error; goto sftp_init_error;
} }
@ -643,17 +638,16 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
0, &data, &data_len); 0, &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from SFTP subsystem", "Would block waiting for response from SFTP subsystem");
0);
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from SFTP subsystem", 0); "Timeout waiting for response from SFTP subsystem");
goto sftp_init_error; goto sftp_init_error;
} }
if (data_len < 5) { if (data_len < 5) {
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Invalid SSH_FXP_VERSION response", 0); "Invalid SSH_FXP_VERSION response");
goto sftp_init_error; goto sftp_init_error;
} }
@ -836,8 +830,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
if (!sftp->open_packet) { if (!sftp->open_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_OPEN or " "Unable to allocate memory for FXP_OPEN or "
"FXP_OPENDIR packet", "FXP_OPENDIR packet");
0);
return NULL; return NULL;
} }
/* Filetype in SFTP 3 and earlier */ /* Filetype in SFTP 3 and earlier */
@ -876,8 +869,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
sftp->open_packet_len); sftp->open_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending FXP_OPEN or FXP_OPENDIR command", "Would block sending FXP_OPEN or FXP_OPENDIR command");
0);
return NULL; return NULL;
} }
else if (sftp->open_packet_len != rc) { else if (sftp->open_packet_len != rc) {
@ -885,7 +877,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
an error when in non-blocking mode! */ an error when in non-blocking mode! */
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_OPEN or FXP_OPENDIR command", 0); "Unable to send FXP_OPEN or FXP_OPENDIR command");
LIBSSH2_FREE(session, sftp->open_packet); LIBSSH2_FREE(session, sftp->open_packet);
sftp->open_packet = NULL; sftp->open_packet = NULL;
sftp->open_state = libssh2_NB_state_idle; sftp->open_state = libssh2_NB_state_idle;
@ -903,12 +895,11 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for status message", 0); "Would block waiting for status message");
return NULL; return NULL;
} }
else if (rc) { else if (rc) {
libssh2_error(session, rc, libssh2_error(session, rc, "Timeout waiting for status message");
"Timeout waiting for status message", 0);
sftp->open_state = libssh2_NB_state_idle; sftp->open_state = libssh2_NB_state_idle;
return NULL; return NULL;
} }
@ -942,7 +933,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
if(badness) { if(badness) {
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Failed opening remote file", 0); "Failed opening remote file");
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "got FXP_STATUS %d", _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "got FXP_STATUS %d",
sftp->last_errno); sftp->last_errno);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
@ -953,7 +944,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
fp = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE)); fp = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE));
if (!fp) { if (!fp) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate new SFTP handle structure", 0); "Unable to allocate new SFTP handle structure");
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
return NULL; return NULL;
} }
@ -1095,11 +1086,10 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
} else if (packet_len != retcode) { } else if (packet_len != retcode) {
/* TODO: a partial write is not a critical error when in /* TODO: a partial write is not a critical error when in
non-blocking mode! */ non-blocking mode! */
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed", 0);
sftp->read_packet = NULL; sftp->read_packet = NULL;
sftp->read_state = libssh2_NB_state_idle; sftp->read_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
} }
sftp->read_packet = packet; sftp->read_packet = packet;
sftp->read_request_id = request_id; sftp->read_request_id = request_id;
@ -1112,15 +1102,13 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
sftp_packet_requirev(sftp, 2, read_responses, sftp_packet_requirev(sftp, 2, read_responses,
request_id, &data, &data_len); request_id, &data, &data_len);
if (retcode == PACKET_EAGAIN) { if (retcode == PACKET_EAGAIN) {
libssh2_error(session, retcode, return libssh2_error(session, retcode,
"Would block waiting for status message", 0); "Would block waiting for status message");
return retcode;
} else if (retcode) { } else if (retcode) {
libssh2_error(session, retcode,
"Timeout waiting for status message", 0);
sftp->read_packet = NULL; sftp->read_packet = NULL;
sftp->read_state = libssh2_NB_state_idle; sftp->read_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, retcode,
"Timeout waiting for status message");
} }
sftp->read_state = libssh2_NB_state_sent1; sftp->read_state = libssh2_NB_state_sent1;
@ -1140,9 +1128,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
return total_read; return total_read;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
return -1;
} }
case SSH_FXP_DATA: case SSH_FXP_DATA:
@ -1267,10 +1254,9 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
s = sftp->readdir_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->readdir_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->readdir_packet) { if (!sftp->readdir_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_READDIR packet", "Unable to allocate memory for "
0); "FXP_READDIR packet");
return -1;
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
@ -1297,12 +1283,11 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
return retcode; return retcode;
} }
else if (packet_len != retcode) { else if (packet_len != retcode) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed", 0);
LIBSSH2_FREE(session, sftp->readdir_packet); LIBSSH2_FREE(session, sftp->readdir_packet);
sftp->readdir_packet = NULL; sftp->readdir_packet = NULL;
sftp->readdir_state = libssh2_NB_state_idle; sftp->readdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
} }
LIBSSH2_FREE(session, sftp->readdir_packet); LIBSSH2_FREE(session, sftp->readdir_packet);
@ -1318,10 +1303,9 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
if (retcode == PACKET_EAGAIN) { if (retcode == PACKET_EAGAIN) {
return retcode; return retcode;
} else if (retcode) { } else if (retcode) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->readdir_state = libssh2_NB_state_idle; sftp->readdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
if (data[0] == SSH_FXP_STATUS) { if (data[0] == SSH_FXP_STATUS) {
@ -1332,10 +1316,9 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
return 0; return 0;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0);
sftp->readdir_state = libssh2_NB_state_idle; sftp->readdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
} }
} }
@ -1435,9 +1418,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
(unsigned long) count); (unsigned long) count);
s = sftp->write_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->write_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->write_packet) { if (!sftp->write_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_WRITE", 0); "Unable to allocate memory for FXP_WRITE");
return LIBSSH2_ERROR_ALLOC;
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
s += 4; s += 4;
@ -1484,10 +1466,9 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
return rc; return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, rc,
"Timeout waiting for status message", 0);
sftp->write_state = libssh2_NB_state_idle; sftp->write_state = libssh2_NB_state_idle;
return rc; return libssh2_error(session, rc,
"Timeout waiting for status message");
} }
sftp->write_state = libssh2_NB_state_idle; sftp->write_state = libssh2_NB_state_idle;
@ -1499,11 +1480,10 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
handle->u.file.offset += count; handle->u.file.offset += count;
return count; return count;
} }
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, "SFTP Protocol Error",
0);
sftp->last_errno = retcode; sftp->last_errno = retcode;
return LIBSSH2_ERROR_SFTP_PROTOCOL; return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
} }
/* libssh2_sftp_write /* libssh2_sftp_write
@ -1545,10 +1525,9 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
setstat ? "set-stat" : "stat"); setstat ? "set-stat" : "stat");
s = sftp->fstat_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->fstat_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->fstat_packet) { if (!sftp->fstat_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FSTAT/FSETSTAT packet", "Unable to allocate memory for "
0); "FSTAT/FSETSTAT packet");
return -1;
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
@ -1574,13 +1553,12 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
(setstat ? "Unable to send FXP_FSETSTAT"
: "Unable to send FXP_FSTAT command"), 0);
LIBSSH2_FREE(session, sftp->fstat_packet); LIBSSH2_FREE(session, sftp->fstat_packet);
sftp->fstat_packet = NULL; sftp->fstat_packet = NULL;
sftp->fstat_state = libssh2_NB_state_idle; sftp->fstat_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
(setstat ? "Unable to send FXP_FSETSTAT"
: "Unable to send FXP_FSTAT command"));
} }
LIBSSH2_FREE(session, sftp->fstat_packet); LIBSSH2_FREE(session, sftp->fstat_packet);
sftp->fstat_packet = NULL; sftp->fstat_packet = NULL;
@ -1594,10 +1572,9 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->fstat_state = libssh2_NB_state_idle; sftp->fstat_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->fstat_state = libssh2_NB_state_idle; sftp->fstat_state = libssh2_NB_state_idle;
@ -1611,9 +1588,8 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
return 0; return 0;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
return -1;
} }
} }
@ -1696,9 +1672,9 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Closing handle"); _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Closing handle");
s = handle->close_packet = LIBSSH2_ALLOC(session, packet_len); s = handle->close_packet = LIBSSH2_ALLOC(session, packet_len);
if (!handle->close_packet) { if (!handle->close_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_CLOSE packet", 0); "Unable to allocate memory for FXP_CLOSE "
return -1; "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
@ -1721,12 +1697,11 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_CLOSE command", 0);
LIBSSH2_FREE(session, handle->close_packet); LIBSSH2_FREE(session, handle->close_packet);
handle->close_packet = NULL; handle->close_packet = NULL;
handle->close_state = libssh2_NB_state_idle; handle->close_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_CLOSE command");
} }
LIBSSH2_FREE(session, handle->close_packet); LIBSSH2_FREE(session, handle->close_packet);
handle->close_packet = NULL; handle->close_packet = NULL;
@ -1741,10 +1716,9 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
handle->close_state = libssh2_NB_state_idle; handle->close_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
handle->close_state = libssh2_NB_state_sent1; handle->close_state = libssh2_NB_state_sent1;
@ -1755,10 +1729,9 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
if (retcode != LIBSSH2_FX_OK) { if (retcode != LIBSSH2_FX_OK) {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0);
handle->close_state = libssh2_NB_state_idle; handle->close_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
} }
/* remove this handle from the parent's list */ /* remove this handle from the parent's list */
@ -1807,10 +1780,9 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Unlinking %s", filename); _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Unlinking %s", filename);
s = sftp->unlink_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->unlink_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->unlink_packet) { if (!sftp->unlink_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_REMOVE packet", "Unable to allocate memory for FXP_REMOVE "
0); "packet");
return -1;
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
@ -1833,12 +1805,11 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_REMOVE command", 0);
LIBSSH2_FREE(session, sftp->unlink_packet); LIBSSH2_FREE(session, sftp->unlink_packet);
sftp->unlink_packet = NULL; sftp->unlink_packet = NULL;
sftp->unlink_state = libssh2_NB_state_idle; sftp->unlink_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_REMOVE command");
} }
LIBSSH2_FREE(session, sftp->unlink_packet); LIBSSH2_FREE(session, sftp->unlink_packet);
sftp->unlink_packet = NULL; sftp->unlink_packet = NULL;
@ -1853,10 +1824,9 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
return rc; return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->unlink_state = libssh2_NB_state_idle; sftp->unlink_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->unlink_state = libssh2_NB_state_idle; sftp->unlink_state = libssh2_NB_state_idle;
@ -1868,9 +1838,8 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
return 0; return 0;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
return -1;
} }
} }
@ -1910,9 +1879,8 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
int rc; int rc;
if (sftp->version < 2) { if (sftp->version < 2) {
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Server does not support RENAME", 0); "Server does not support RENAME");
return -1;
} }
if (sftp->rename_state == libssh2_NB_state_idle) { if (sftp->rename_state == libssh2_NB_state_idle) {
@ -1921,10 +1889,9 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
sftp->rename_s = sftp->rename_packet = sftp->rename_s = sftp->rename_packet =
LIBSSH2_ALLOC(session, packet_len); LIBSSH2_ALLOC(session, packet_len);
if (!sftp->rename_packet) { if (!sftp->rename_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_RENAME packet", "Unable to allocate memory for FXP_RENAME "
0); "packet");
return -1;
} }
_libssh2_htonu32(sftp->rename_s, packet_len - 4); _libssh2_htonu32(sftp->rename_s, packet_len - 4);
@ -1956,12 +1923,11 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_RENAME command", 0);
LIBSSH2_FREE(session, sftp->rename_packet); LIBSSH2_FREE(session, sftp->rename_packet);
sftp->rename_packet = NULL; sftp->rename_packet = NULL;
sftp->rename_state = libssh2_NB_state_idle; sftp->rename_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_RENAME command");
} }
LIBSSH2_FREE(session, sftp->rename_packet); LIBSSH2_FREE(session, sftp->rename_packet);
sftp->rename_packet = NULL; sftp->rename_packet = NULL;
@ -1975,10 +1941,9 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->rename_state = libssh2_NB_state_idle; sftp->rename_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->rename_state = libssh2_NB_state_idle; sftp->rename_state = libssh2_NB_state_idle;
@ -1986,31 +1951,30 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
retcode = _libssh2_ntohu32(data + 5); retcode = _libssh2_ntohu32(data + 5);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
sftp->last_errno = retcode;
/* now convert the SFTP error code to libssh2 return code or error
message */
switch (retcode) { switch (retcode) {
case LIBSSH2_FX_OK: case LIBSSH2_FX_OK:
retcode = 0; retcode = LIBSSH2_ERROR_NONE;
break; break;
case LIBSSH2_FX_FILE_ALREADY_EXISTS: case LIBSSH2_FX_FILE_ALREADY_EXISTS:
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, retcode = libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"File already exists and SSH_FXP_RENAME_OVERWRITE not specified", "File already exists and "
0); "SSH_FXP_RENAME_OVERWRITE not specified");
sftp->last_errno = retcode;
retcode = -1;
break; break;
case LIBSSH2_FX_OP_UNSUPPORTED: case LIBSSH2_FX_OP_UNSUPPORTED:
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, retcode = libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Operation Not Supported", 0); "Operation Not Supported");
sftp->last_errno = retcode;
retcode = -1;
break; break;
default: default:
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, retcode = libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
sftp->last_errno = retcode; break;
retcode = -1;
} }
return retcode; return retcode;
@ -2056,9 +2020,9 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
"Creating directory %s with mode 0%lo", path, mode); "Creating directory %s with mode 0%lo", path, mode);
s = packet = LIBSSH2_ALLOC(session, packet_len); s = packet = LIBSSH2_ALLOC(session, packet_len);
if (!packet) { if (!packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_MKDIR packet", 0); "Unable to allocate memory for FXP_MKDIR "
return -1; "packet");
} }
/* Filetype in SFTP 3 and earlier */ /* Filetype in SFTP 3 and earlier */
attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR; attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR;
@ -2088,11 +2052,10 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
return rc; return rc;
} }
if (packet_len != rc) { if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed", 0);
LIBSSH2_FREE(session, packet); LIBSSH2_FREE(session, packet);
sftp->mkdir_state = libssh2_NB_state_idle; sftp->mkdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"_libssh2_channel_write() failed");
} }
LIBSSH2_FREE(session, packet); LIBSSH2_FREE(session, packet);
sftp->mkdir_state = libssh2_NB_state_sent; sftp->mkdir_state = libssh2_NB_state_sent;
@ -2104,10 +2067,9 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->mkdir_state = libssh2_NB_state_idle; sftp->mkdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->mkdir_state = libssh2_NB_state_idle; sftp->mkdir_state = libssh2_NB_state_idle;
@ -2119,10 +2081,9 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "OK!"); _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "OK!");
return 0; return 0;
} else { } else {
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0);
sftp->last_errno = retcode; sftp->last_errno = retcode;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error");
} }
} }
@ -2160,9 +2121,9 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
path); path);
s = sftp->rmdir_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->rmdir_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->rmdir_packet) { if (!sftp->rmdir_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_RMDIR packet", 0); "Unable to allocate memory for FXP_RMDIR "
return -1; "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
@ -2185,12 +2146,11 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_RMDIR command", 0);
LIBSSH2_FREE(session, sftp->rmdir_packet); LIBSSH2_FREE(session, sftp->rmdir_packet);
sftp->rmdir_packet = NULL; sftp->rmdir_packet = NULL;
sftp->rmdir_state = libssh2_NB_state_idle; sftp->rmdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send FXP_RMDIR command");
} }
LIBSSH2_FREE(session, sftp->rmdir_packet); LIBSSH2_FREE(session, sftp->rmdir_packet);
sftp->rmdir_packet = NULL; sftp->rmdir_packet = NULL;
@ -2203,10 +2163,9 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->rmdir_state = libssh2_NB_state_idle; sftp->rmdir_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->rmdir_state = libssh2_NB_state_idle; sftp->rmdir_state = libssh2_NB_state_idle;
@ -2218,9 +2177,8 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
return 0; return 0;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
return -1;
} }
} }
@ -2264,9 +2222,9 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), path); LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), path);
s = sftp->stat_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->stat_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->stat_packet) { if (!sftp->stat_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_*STAT packet", 0); "Unable to allocate memory for FXP_*STAT "
return -1; "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_htonu32(s, packet_len - 4);
@ -2304,12 +2262,11 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send STAT/LSTAT/SETSTAT command", 0);
LIBSSH2_FREE(session, sftp->stat_packet); LIBSSH2_FREE(session, sftp->stat_packet);
sftp->stat_packet = NULL; sftp->stat_packet = NULL;
sftp->stat_state = libssh2_NB_state_idle; sftp->stat_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send STAT/LSTAT/SETSTAT command");
} }
LIBSSH2_FREE(session, sftp->stat_packet); LIBSSH2_FREE(session, sftp->stat_packet);
sftp->stat_packet = NULL; sftp->stat_packet = NULL;
@ -2322,10 +2279,9 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->stat_state = libssh2_NB_state_idle; sftp->stat_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->stat_state = libssh2_NB_state_idle; sftp->stat_state = libssh2_NB_state_idle;
@ -2339,9 +2295,8 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
return 0; return 0;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
return -1;
} }
} }
@ -2386,18 +2341,16 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
int rc; int rc;
if ((sftp->version < 3) && (link_type != LIBSSH2_SFTP_REALPATH)) { if ((sftp->version < 3) && (link_type != LIBSSH2_SFTP_REALPATH)) {
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Server does not support SYMLINK or READLINK", 0); "Server does not support SYMLINK or READLINK");
return -1;
} }
if (sftp->symlink_state == libssh2_NB_state_idle) { if (sftp->symlink_state == libssh2_NB_state_idle) {
s = sftp->symlink_packet = LIBSSH2_ALLOC(session, packet_len); s = sftp->symlink_packet = LIBSSH2_ALLOC(session, packet_len);
if (!sftp->symlink_packet) { if (!sftp->symlink_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for SYMLINK/READLINK" "Unable to allocate memory for "
"/REALPATH packet", 0); "SYMLINK/READLINK/REALPATH packet");
return -1;
} }
_libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%s %s on %s", _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%s %s on %s",
@ -2444,12 +2397,11 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send SYMLINK/READLINK command", 0);
LIBSSH2_FREE(session, sftp->symlink_packet); LIBSSH2_FREE(session, sftp->symlink_packet);
sftp->symlink_packet = NULL; sftp->symlink_packet = NULL;
sftp->symlink_state = libssh2_NB_state_idle; sftp->symlink_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send SYMLINK/READLINK command");
} }
LIBSSH2_FREE(session, sftp->symlink_packet); LIBSSH2_FREE(session, sftp->symlink_packet);
sftp->symlink_packet = NULL; sftp->symlink_packet = NULL;
@ -2464,10 +2416,9 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
return rc; return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message", 0);
sftp->symlink_state = libssh2_NB_state_idle; sftp->symlink_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for status message");
} }
sftp->symlink_state = libssh2_NB_state_idle; sftp->symlink_state = libssh2_NB_state_idle;
@ -2481,18 +2432,16 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
return 0; return 0;
} else { } else {
sftp->last_errno = retcode; sftp->last_errno = retcode;
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"SFTP Protocol Error", 0); "SFTP Protocol Error");
return -1;
} }
} }
if (_libssh2_ntohu32(data + 5) < 1) { if (_libssh2_ntohu32(data + 5) < 1) {
libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Invalid READLINK/REALPATH response, no name entries",
0);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
return -1; return libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
"Invalid READLINK/REALPATH response, "
"no name entries");
} }
link_len = _libssh2_ntohu32(data + 9); link_len = _libssh2_ntohu32(data + 9);

View File

@ -135,8 +135,6 @@ decrypt(LIBSSH2_SESSION * session, unsigned char *source,
while (len >= blocksize) { while (len >= blocksize) {
if (session->remote.crypt->crypt(session, source, if (session->remote.crypt->crypt(session, source,
&session->remote.crypt_abstract)) { &session->remote.crypt_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_DECRYPT,
(char *) "Error decrypting packet", 0);
LIBSSH2_FREE(session, p->payload); LIBSSH2_FREE(session, p->payload);
return PACKET_FAIL; return PACKET_FAIL;
} }
@ -232,11 +230,9 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
/* We need a freeable struct otherwise the /* We need a freeable struct otherwise the
* brigade won't know what to do with it */ * brigade won't know what to do with it */
p->payload = LIBSSH2_ALLOC(session, data_len); p->payload = LIBSSH2_ALLOC(session, data_len);
if (!p->payload) { if (!p->payload)
libssh2_error(session, LIBSSH2_ERROR_ALLOC, (char *)
"Unable to allocate memory", 0);
return PACKET_ENOMEM; return PACKET_ENOMEM;
}
memcpy(p->payload, data, data_len); memcpy(p->payload, data, data_len);
session->fullpacket_payload_len = data_len; session->fullpacket_payload_len = data_len;
} }

View File

@ -79,7 +79,7 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
LIBSSH2_ALLOC(session, session->userauth_list_data_len); LIBSSH2_ALLOC(session, session->userauth_list_data_len);
if (!session->userauth_list_data) { if (!session->userauth_list_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth_list", 0); "Unable to allocate memory for userauth_list");
return NULL; return NULL;
} }
@ -109,11 +109,11 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
session->userauth_list_data_len); session->userauth_list_data_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list", 0); "Would block requesting userauth list");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-none request", 0); "Unable to send userauth-none request");
LIBSSH2_FREE(session, session->userauth_list_data); LIBSSH2_FREE(session, session->userauth_list_data);
session->userauth_list_data = NULL; session->userauth_list_data = NULL;
session->userauth_list_state = libssh2_NB_state_idle; session->userauth_list_state = libssh2_NB_state_idle;
@ -133,17 +133,17 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
&session->userauth_list_packet_requirev_state); &session->userauth_list_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list", 0); "Would block requesting userauth list");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_NONE, "No error", 0); libssh2_error(session, LIBSSH2_ERROR_NONE, "No error");
session->userauth_list_state = libssh2_NB_state_idle; session->userauth_list_state = libssh2_NB_state_idle;
return NULL; return NULL;
} }
if (session->userauth_list_data[0] == SSH_MSG_USERAUTH_SUCCESS) { if (session->userauth_list_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
/* Wow, who'dve thought... */ /* Wow, who'dve thought... */
libssh2_error(session, LIBSSH2_ERROR_NONE, "No error", 0); libssh2_error(session, LIBSSH2_ERROR_NONE, "No error");
LIBSSH2_FREE(session, session->userauth_list_data); LIBSSH2_FREE(session, session->userauth_list_data);
session->userauth_list_data = NULL; session->userauth_list_data = NULL;
session->state |= LIBSSH2_STATE_AUTHENTICATED; session->state |= LIBSSH2_STATE_AUTHENTICATED;
@ -228,10 +228,9 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
s = session->userauth_pswd_data = s = session->userauth_pswd_data =
LIBSSH2_ALLOC(session, session->userauth_pswd_data_len); LIBSSH2_ALLOC(session, session->userauth_pswd_data_len);
if (!session->userauth_pswd_data) { if (!session->userauth_pswd_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth-password" "Unable to allocate memory for "
" request", 0); "userauth-password request");
return -1;
} }
*(s++) = SSH_MSG_USERAUTH_REQUEST; *(s++) = SSH_MSG_USERAUTH_REQUEST;
@ -268,16 +267,14 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
rc = _libssh2_transport_write(session, session->userauth_pswd_data, rc = _libssh2_transport_write(session, session->userauth_pswd_data,
session->userauth_pswd_data_len); session->userauth_pswd_data_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block writing password request", 0); "Would block writing password request");
return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-password request", 0);
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
session->userauth_pswd_state = libssh2_NB_state_idle; session->userauth_pswd_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-password request");
} }
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
@ -298,14 +295,12 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
&session-> &session->
userauth_pswd_packet_requirev_state); userauth_pswd_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting", 0); "Would block waiting");
return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
"Would block waiting", 0);
session->userauth_pswd_state = libssh2_NB_state_idle; session->userauth_pswd_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
"Would block waiting");
} }
if (session->userauth_pswd_data[0] == SSH_MSG_USERAUTH_SUCCESS) { if (session->userauth_pswd_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
@ -322,11 +317,10 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
session->userauth_pswd_state = libssh2_NB_state_idle; session->userauth_pswd_state = libssh2_NB_state_idle;
libssh2_error(session, return libssh2_error(session,
LIBSSH2_ERROR_AUTHENTICATION_FAILED, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed (username/password)", "Authentication failed "
0); "(username/password)");
return -1;
} }
session->userauth_pswd_newpw = NULL; session->userauth_pswd_newpw = NULL;
@ -356,11 +350,10 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
&session->userauth_pswd_newpw_len, &session->userauth_pswd_newpw_len,
&session->abstract); &session->abstract);
if (!session->userauth_pswd_newpw) { if (!session->userauth_pswd_newpw) {
libssh2_error(session, return libssh2_error(session,
LIBSSH2_ERROR_PASSWORD_EXPIRED, LIBSSH2_ERROR_PASSWORD_EXPIRED,
"Password expired, and callback failed", "Password expired, and "
0); "callback failed");
return -1;
} }
/* basic data_len + newpw_len(4) */ /* basic data_len + newpw_len(4) */
@ -372,13 +365,13 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
LIBSSH2_ALLOC(session, LIBSSH2_ALLOC(session,
session->userauth_pswd_data_len); session->userauth_pswd_data_len);
if (!session->userauth_pswd_data) { if (!session->userauth_pswd_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth-password-change request",
0);
LIBSSH2_FREE(session, LIBSSH2_FREE(session,
session->userauth_pswd_newpw); session->userauth_pswd_newpw);
session->userauth_pswd_newpw = NULL; session->userauth_pswd_newpw = NULL;
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory "
"for userauth password "
"change request");
} }
*(s++) = SSH_MSG_USERAUTH_REQUEST; *(s++) = SSH_MSG_USERAUTH_REQUEST;
@ -421,19 +414,19 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
session-> session->
userauth_pswd_data_len); userauth_pswd_data_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting", 0); "Would block waiting");
return rc; }
} else if (rc) { else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-password-change request",
0);
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
LIBSSH2_FREE(session, LIBSSH2_FREE(session,
session->userauth_pswd_newpw); session->userauth_pswd_newpw);
session->userauth_pswd_newpw = NULL; session->userauth_pswd_newpw = NULL;
return -1; return libssh2_error(session,
LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth "
"password-change request");
} }
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
@ -449,11 +442,10 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
} }
} }
} else { } else {
libssh2_error(session, LIBSSH2_ERROR_PASSWORD_EXPIRED,
"Password Expired, and no callback specified",
0);
session->userauth_pswd_state = libssh2_NB_state_idle; session->userauth_pswd_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_PASSWORD_EXPIRED,
"Password Expired, and no callback "
"specified");
} }
} }
} }
@ -463,9 +455,8 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
session->userauth_pswd_state = libssh2_NB_state_idle; session->userauth_pswd_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, return libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed", 0); "Authentication failed");
return -1;
} }
/* /*
@ -516,9 +507,8 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
/* Read Public Key */ /* Read Public Key */
fd = fopen(pubkeyfile, "r"); fd = fopen(pubkeyfile, "r");
if (!fd) { if (!fd) {
libssh2_error(session, LIBSSH2_ERROR_FILE, return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to open public key file", 0); "Unable to open public key file");
return -1;
} }
while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n') while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n')
pubkey_len++; pubkey_len++;
@ -529,25 +519,22 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
rewind(fd); rewind(fd);
if (pubkey_len <= 1) { if (pubkey_len <= 1) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid data in public key file", 0);
fclose(fd); fclose(fd);
return -1; return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid data in public key file");
} }
pubkey = LIBSSH2_ALLOC(session, pubkey_len); pubkey = LIBSSH2_ALLOC(session, pubkey_len);
if (!pubkey) { if (!pubkey) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for public key data", 0);
fclose(fd); fclose(fd);
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for public key data");
} }
if (fread(pubkey, 1, pubkey_len, fd) != pubkey_len) { if (fread(pubkey, 1, pubkey_len, fd) != pubkey_len) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to read public key from file", 0);
LIBSSH2_FREE(session, pubkey); LIBSSH2_FREE(session, pubkey);
fclose(fd); fclose(fd);
return -1; return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to read public key from file");
} }
fclose(fd); fclose(fd);
/* /*
@ -557,17 +544,15 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
pubkey_len--; pubkey_len--;
if (!pubkey_len) { if (!pubkey_len) {
libssh2_error(session, LIBSSH2_ERROR_FILE, "Missing public key data",
0);
LIBSSH2_FREE(session, pubkey); LIBSSH2_FREE(session, pubkey);
return -1; return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Missing public key data");
} }
if ((sp1 = memchr(pubkey, ' ', pubkey_len)) == NULL) { if ((sp1 = memchr(pubkey, ' ', pubkey_len)) == NULL) {
libssh2_error(session, LIBSSH2_ERROR_FILE, "Invalid public key data",
0);
LIBSSH2_FREE(session, pubkey); LIBSSH2_FREE(session, pubkey);
return -1; return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid public key data");
} }
sp1++; sp1++;
@ -579,10 +564,9 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
if (libssh2_base64_decode(session, (char **) &tmp, &tmp_len, if (libssh2_base64_decode(session, (char **) &tmp, &tmp_len,
(char *) sp1, sp2 - sp1)) { (char *) sp1, sp2 - sp1)) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid key data, not base64 encoded", 0);
LIBSSH2_FREE(session, pubkey); LIBSSH2_FREE(session, pubkey);
return -1; return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid key data, not base64 encoded");
} }
/* Wasting some bytes here (okay, more than some), but since it's likely /* Wasting some bytes here (okay, more than some), but since it's likely
@ -626,17 +610,15 @@ file_read_privatekey(LIBSSH2_SESSION * session,
hostkey_methods_avail++; hostkey_methods_avail++;
} }
if (!*hostkey_method) { if (!*hostkey_method) {
libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE, return libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE,
"No handler for specified private key", 0); "No handler for specified private key");
return -1;
} }
if ((*hostkey_method)-> if ((*hostkey_method)->
initPEM(session, privkeyfile, (unsigned char *) passphrase, initPEM(session, privkeyfile, (unsigned char *) passphrase,
hostkey_abstract)) { hostkey_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_FILE, return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to initialize private key from file", 0); "Unable to initialize private key from file");
return -1;
} }
return 0; return 0;
@ -710,13 +692,13 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
memset(&session->userauth_host_packet_requirev_state, 0, memset(&session->userauth_host_packet_requirev_state, 0,
sizeof(session->userauth_host_packet_requirev_state)); sizeof(session->userauth_host_packet_requirev_state));
if (file_read_publickey(session, &session->userauth_host_method, rc = file_read_publickey(session, &session->userauth_host_method,
&session->userauth_host_method_len, &session->userauth_host_method_len,
&pubkeydata, &pubkeydata_len, &pubkeydata, &pubkeydata_len,
publickey)) { publickey);
if(rc)
/* Note: file_read_publickey() calls libssh2_error() */ /* Note: file_read_publickey() calls libssh2_error() */
return -1; return rc;
}
/* /*
* 48 = packet_type(1) + username_len(4) + servicename_len(4) + * 48 = packet_type(1) + username_len(4) + servicename_len(4) +
@ -742,9 +724,8 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
LIBSSH2_FREE(session, session->userauth_host_method); LIBSSH2_FREE(session, session->userauth_host_method);
session->userauth_host_method = NULL; session->userauth_host_method = NULL;
LIBSSH2_FREE(session, pubkeydata); LIBSSH2_FREE(session, pubkeydata);
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Out of memory", 0); "Out of memory");
return -1;
} }
*(session->userauth_host_s++) = SSH_MSG_USERAUTH_REQUEST; *(session->userauth_host_s++) = SSH_MSG_USERAUTH_REQUEST;
@ -831,16 +812,14 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
(4 + session->userauth_host_method_len) (4 + session->userauth_host_method_len)
+ (4 + sig_len)); /* PK sigblob */ + (4 + sig_len)); /* PK sigblob */
if (!newpacket) { if (!newpacket) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating additional space for "
"userauth-hostbased packet",
0);
LIBSSH2_FREE(session, sig); LIBSSH2_FREE(session, sig);
LIBSSH2_FREE(session, session->userauth_host_packet); LIBSSH2_FREE(session, session->userauth_host_packet);
session->userauth_host_packet = NULL; session->userauth_host_packet = NULL;
LIBSSH2_FREE(session, session->userauth_host_method); LIBSSH2_FREE(session, session->userauth_host_method);
session->userauth_host_method = NULL; session->userauth_host_method = NULL;
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating additional space for "
"userauth-hostbased packet");
} }
session->userauth_host_packet = newpacket; session->userauth_host_packet = newpacket;
} }
@ -878,16 +857,14 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
session->userauth_host_s - session->userauth_host_s -
session->userauth_host_packet); session->userauth_host_packet);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc;
} }
else if (rc) { else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-hostbased request", 0);
LIBSSH2_FREE(session, session->userauth_host_packet); LIBSSH2_FREE(session, session->userauth_host_packet);
session->userauth_host_packet = NULL; session->userauth_host_packet = NULL;
session->userauth_host_state = libssh2_NB_state_idle; session->userauth_host_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-hostbased request");
} }
LIBSSH2_FREE(session, session->userauth_host_packet); LIBSSH2_FREE(session, session->userauth_host_packet);
session->userauth_host_packet = NULL; session->userauth_host_packet = NULL;
@ -905,15 +882,13 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
&session-> &session->
userauth_host_packet_requirev_state); userauth_host_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc;
} }
session->userauth_host_state = libssh2_NB_state_idle; session->userauth_host_state = libssh2_NB_state_idle;
if (rc) { if (rc) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Auth failed", 0); "Auth failed");
return -1;
} }
if (session->userauth_host_data[0] == SSH_MSG_USERAUTH_SUCCESS) { if (session->userauth_host_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
@ -930,11 +905,9 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
/* This public key is not allowed for this user on this server */ /* This public key is not allowed for this user on this server */
LIBSSH2_FREE(session, session->userauth_host_data); LIBSSH2_FREE(session, session->userauth_host_data);
session->userauth_host_data = NULL; session->userauth_host_data = NULL;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Invalid signature for supplied public key, or bad " "Invalid signature for supplied public key, or bad "
"username/public key combination", "username/public key combination");
0);
return -1;
} }
/* libssh2_userauth_hostbased_fromfile_ex /* libssh2_userauth_hostbased_fromfile_ex
@ -994,10 +967,9 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_method = session->userauth_pblc_method =
LIBSSH2_ALLOC(session, session->userauth_pblc_method_len); LIBSSH2_ALLOC(session, session->userauth_pblc_method_len);
if (!session->userauth_pblc_method) { if (!session->userauth_pblc_method) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for public key " "Unable to allocate memory for public key "
"data", 0); "data");
return -1;
} }
memcpy(session->userauth_pblc_method, pubkeydata + 4, memcpy(session->userauth_pblc_method, pubkeydata + 4,
session->userauth_pblc_method_len); session->userauth_pblc_method_len);
@ -1028,8 +1000,8 @@ userauth_publickey(LIBSSH2_SESSION *session,
if (!session->userauth_pblc_packet) { if (!session->userauth_pblc_packet) {
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Out of memory", 0); return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
return -1; "Out of memory");
} }
*(session->userauth_pblc_s++) = SSH_MSG_USERAUTH_REQUEST; *(session->userauth_pblc_s++) = SSH_MSG_USERAUTH_REQUEST;
@ -1073,18 +1045,16 @@ userauth_publickey(LIBSSH2_SESSION *session,
if (session->userauth_pblc_state == libssh2_NB_state_created) { if (session->userauth_pblc_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->userauth_pblc_packet, rc = _libssh2_transport_write(session, session->userauth_pblc_packet,
session->userauth_pblc_packet_len); session->userauth_pblc_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN)
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc; else if (rc) {
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-publickey request", 0);
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL; session->userauth_pblc_packet = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-publickey request");
} }
session->userauth_pblc_state = libssh2_NB_state_sent; session->userauth_pblc_state = libssh2_NB_state_sent;
@ -1098,8 +1068,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
&session-> &session->
userauth_pblc_packet_requirev_state); userauth_pblc_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc;
} }
else if (rc) { else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
@ -1107,9 +1076,8 @@ userauth_publickey(LIBSSH2_SESSION *session,
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Failed waiting", 0); "Failed waiting");
return -1;
} }
if (session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) { if (session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
@ -1138,10 +1106,9 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_packet = NULL; session->userauth_pblc_packet = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Username/PublicKey combination invalid", 0);
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Username/PublicKey combination invalid");
} }
/* Semi-Success! */ /* Semi-Success! */
@ -1160,10 +1127,9 @@ userauth_publickey(LIBSSH2_SESSION *session,
s = buf = LIBSSH2_ALLOC(session, 4 + session->session_id_len s = buf = LIBSSH2_ALLOC(session, 4 + session->session_id_len
+ session->userauth_pblc_packet_len); + session->userauth_pblc_packet_len);
if (!buf) { if (!buf) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth-publickey " "Unable to allocate memory for "
"signed data", 0); "userauth-publickey signed data");
return -1;
} }
_libssh2_htonu32(s, session->session_id_len); _libssh2_htonu32(s, session->session_id_len);
@ -1177,17 +1143,15 @@ userauth_publickey(LIBSSH2_SESSION *session,
rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract); rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract);
LIBSSH2_FREE(session, buf); LIBSSH2_FREE(session, buf);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc;
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL; session->userauth_pblc_packet = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Callback returned error", 0); "Callback returned error");
return -1;
} }
/* /*
@ -1203,17 +1167,15 @@ userauth_publickey(LIBSSH2_SESSION *session,
(4 + session->userauth_pblc_method_len) (4 + session->userauth_pblc_method_len)
+ (4 + sig_len)); /* PK sigblob */ + (4 + sig_len)); /* PK sigblob */
if (!newpacket) { if (!newpacket) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating additional space for "
"userauth-publickey packet",
0);
LIBSSH2_FREE(session, sig); LIBSSH2_FREE(session, sig);
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL; session->userauth_pblc_packet = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating additional space for "
"userauth-publickey packet");
} }
session->userauth_pblc_packet = newpacket; session->userauth_pblc_packet = newpacket;
} }
@ -1252,15 +1214,13 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_s - session->userauth_pblc_s -
session->userauth_pblc_packet); session->userauth_pblc_packet);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-publickey request", 0);
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL; session->userauth_pblc_packet = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-publickey request");
} }
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL; session->userauth_pblc_packet = NULL;
@ -1276,14 +1236,12 @@ userauth_publickey(LIBSSH2_SESSION *session,
&session->userauth_pblc_data_len, 0, NULL, 0, &session->userauth_pblc_data_len, 0, NULL, 0,
&session->userauth_pblc_packet_requirev_state); &session->userauth_pblc_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list", 0); "Would block requesting userauth list");
return rc;
} else if (rc) { } else if (rc) {
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Failed waiting", 0); "Failed waiting");
return -1;
} }
if (session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) { if (session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
@ -1300,12 +1258,10 @@ userauth_publickey(LIBSSH2_SESSION *session,
/* This public key is not allowed for this user on this server */ /* This public key is not allowed for this user on this server */
LIBSSH2_FREE(session, session->userauth_pblc_data); LIBSSH2_FREE(session, session->userauth_pblc_data);
session->userauth_pblc_data = NULL; session->userauth_pblc_data = NULL;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Invalid signature for supplied public key, or bad "
"username/public key combination",
0);
session->userauth_pblc_state = libssh2_NB_state_idle; session->userauth_pblc_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Invalid signature for supplied public key, or bad "
"username/public key combination");
} }
/* /*
@ -1330,11 +1286,11 @@ userauth_publickey_fromfile(LIBSSH2_SESSION *session,
privkey_file.passphrase = passphrase; privkey_file.passphrase = passphrase;
if (session->userauth_pblc_state == libssh2_NB_state_idle) { if (session->userauth_pblc_state == libssh2_NB_state_idle) {
if (file_read_publickey(session, &session->userauth_pblc_method, rc = file_read_publickey(session, &session->userauth_pblc_method,
&session->userauth_pblc_method_len, &session->userauth_pblc_method_len,
&pubkeydata, &pubkeydata_len, publickey)) { &pubkeydata, &pubkeydata_len, publickey);
return -1; if(rc)
} return rc;
} }
rc = userauth_publickey(session, username, username_len, rc = userauth_publickey(session, username, username_len,
@ -1433,11 +1389,9 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
session->userauth_kybd_data = s = session->userauth_kybd_data = s =
LIBSSH2_ALLOC(session, session->userauth_kybd_packet_len); LIBSSH2_ALLOC(session, session->userauth_kybd_packet_len);
if (!s) { if (!s) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive authentication", "keyboard-interactive authentication");
0);
return -1;
} }
*s++ = SSH_MSG_USERAUTH_REQUEST; *s++ = SSH_MSG_USERAUTH_REQUEST;
@ -1478,15 +1432,13 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
rc = _libssh2_transport_write(session, session->userauth_kybd_data, rc = _libssh2_transport_write(session, session->userauth_kybd_data,
session->userauth_kybd_packet_len); session->userauth_kybd_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return rc;
} else if (rc) { } else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send keyboard-interactive request", 0);
LIBSSH2_FREE(session, session->userauth_kybd_data); LIBSSH2_FREE(session, session->userauth_kybd_data);
session->userauth_kybd_data = NULL; session->userauth_kybd_data = NULL;
session->userauth_kybd_state = libssh2_NB_state_idle; session->userauth_kybd_state = libssh2_NB_state_idle;
return -1; return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send keyboard-interactive request");
} }
LIBSSH2_FREE(session, session->userauth_kybd_data); LIBSSH2_FREE(session, session->userauth_kybd_data);
session->userauth_kybd_data = NULL; session->userauth_kybd_data = NULL;
@ -1503,13 +1455,13 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
&session-> &session->
userauth_kybd_packet_requirev_state); userauth_kybd_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
return rc; "Would block");
} else if (rc) { } else if (rc) {
session->userauth_kybd_state = libssh2_NB_state_idle; session->userauth_kybd_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, return libssh2_error(session,
"Failed waiting", 0); LIBSSH2_ERROR_AUTHENTICATION_FAILED,
return -1; "Failed waiting");
} }
if (session->userauth_kybd_data[0] == SSH_MSG_USERAUTH_SUCCESS) { if (session->userauth_kybd_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
@ -1528,11 +1480,10 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
LIBSSH2_FREE(session, session->userauth_kybd_data); LIBSSH2_FREE(session, session->userauth_kybd_data);
session->userauth_kybd_data = NULL; session->userauth_kybd_data = NULL;
session->userauth_kybd_state = libssh2_NB_state_idle; session->userauth_kybd_state = libssh2_NB_state_idle;
libssh2_error(session, return libssh2_error(session,
LIBSSH2_ERROR_AUTHENTICATION_FAILED, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed (keyboard-interactive)", "Authentication failed "
0); "(keyboard-interactive)");
return -1;
} }
/* server requested PAM-like conversation */ /* server requested PAM-like conversation */
@ -1547,8 +1498,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_auth_name) { if (!session->userauth_kybd_auth_name) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive 'name' request field", "keyboard-interactive 'name' request field");
0);
goto cleanup; goto cleanup;
} }
memcpy(session->userauth_kybd_auth_name, s, memcpy(session->userauth_kybd_auth_name, s,
@ -1565,8 +1515,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive 'instruction' " "keyboard-interactive 'instruction' "
"request field", "request field");
0);
goto cleanup; goto cleanup;
} }
memcpy(session->userauth_kybd_auth_instruction, s, memcpy(session->userauth_kybd_auth_instruction, s,
@ -1590,8 +1539,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_prompts) { if (!session->userauth_kybd_prompts) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive prompts array", "keyboard-interactive prompts array");
0);
goto cleanup; goto cleanup;
} }
memset(session->userauth_kybd_prompts, 0, memset(session->userauth_kybd_prompts, 0,
@ -1605,8 +1553,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_responses) { if (!session->userauth_kybd_responses) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive responses array", "keyboard-interactive responses array");
0);
goto cleanup; goto cleanup;
} }
memset(session->userauth_kybd_responses, 0, memset(session->userauth_kybd_responses, 0,
@ -1623,8 +1570,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_prompts[i].text) { if (!session->userauth_kybd_prompts[i].text) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive prompt message", "keyboard-interactive prompt message");
0);
goto cleanup; goto cleanup;
} }
memcpy(session->userauth_kybd_prompts[i].text, s, memcpy(session->userauth_kybd_prompts[i].text, s,
@ -1668,8 +1614,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!s) { if (!s) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for keyboard-" "Unable to allocate memory for keyboard-"
"interactive response packet", "interactive response packet");
0);
goto cleanup; goto cleanup;
} }
@ -1693,13 +1638,13 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
rc = _libssh2_transport_write(session, session->userauth_kybd_data, rc = _libssh2_transport_write(session, session->userauth_kybd_data,
session->userauth_kybd_packet_len); session->userauth_kybd_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0); return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
return rc; "Would block");
} }
if (rc) { if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-keyboard-interactive" "Unable to send userauth-keyboard-interactive"
" request", 0); " request");
goto cleanup; goto cleanup;
} }