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);
if (!agent) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for agent connection", 0);
"Unable to allocate space for agent connection");
return NULL;
}
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));
if (!session->open_channel) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for channel data", 0);
"Unable to allocate space for channel data");
return NULL;
}
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);
if (!session->open_channel->channel_type) {
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);
session->open_channel = NULL;
return NULL;
@ -188,7 +188,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
LIBSSH2_ALLOC(session, session->open_packet_len);
if (!session->open_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate temporary space for packet", 0);
"Unable to allocate temporary space for packet");
goto channel_error;
}
*(s++) = SSH_MSG_CHANNEL_OPEN;
@ -220,11 +220,11 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
session->open_packet_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending channel-open request", 0);
"Would block sending channel-open request");
return NULL;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel-open request", 0);
"Unable to send channel-open request");
goto channel_error;
}
@ -239,7 +239,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
channel_type_len, 4,
&session->open_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return NULL;
} else if (rc) {
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) {
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);
if (!session->direct_message) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for direct-tcpip connection",
0);
"Unable to allocate memory for direct-tcpip connection");
return NULL;
}
_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);
if (!session->fwdLstn_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memeory for setenv packet", 0);
"Unable to allocate memeory for setenv packet");
return NULL;
}
@ -490,14 +489,12 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending global-request packet for "
"forward listen request",
0);
"forward listen request");
return NULL;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send global-request packet for forward "
"listen request",
0);
"listen request");
LIBSSH2_FREE(session, session->fwdLstn_packet);
session->fwdLstn_packet = NULL;
session->fwdLstn_state = libssh2_NB_state_idle;
@ -514,10 +511,10 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
0, NULL, 0,
&session->fwdLstn_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return NULL;
} 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;
return NULL;
}
@ -528,8 +525,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER));
if (!listener) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue",
0);
"Unable to allocate memory for listener queue");
LIBSSH2_FREE(session, data);
session->fwdLstn_state = libssh2_NB_state_idle;
return NULL;
@ -540,8 +536,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1);
if (!listener->host) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue",
0);
"Unable to allocate memory for listener queue");
LIBSSH2_FREE(session, listener);
LIBSSH2_FREE(session, data);
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) {
LIBSSH2_FREE(session, data);
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;
return NULL;
}
@ -632,7 +627,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
s = packet = LIBSSH2_ALLOC(session, packet_len);
if (!packet) {
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;
}
@ -664,8 +659,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send global-request packet for forward "
"listen request",
0);
"listen request");
LIBSSH2_FREE(session, packet);
listener->chanFwdCncl_state = libssh2_NB_state_idle;
return LIBSSH2_ERROR_SOCKET_SEND;
@ -743,11 +737,11 @@ channel_forward_accept(LIBSSH2_LISTENER *listener)
if (rc == PACKET_EAGAIN) {
libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for packet", 0);
"Would block waiting for packet");
}
else
libssh2_error(listener->session, LIBSSH2_ERROR_CHANNEL_UNKNOWN,
"Channel not found", 0);
"Channel not found");
return NULL;
}
@ -799,9 +793,9 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
s = channel->setenv_packet =
LIBSSH2_ALLOC(session, channel->setenv_packet_len);
if (!channel->setenv_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memeory for setenv packet", 0);
return LIBSSH2_ERROR_ALLOC;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memeory "
"for setenv packet");
}
*(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -833,14 +827,12 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
if (rc == PACKET_EAGAIN) {
return 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);
channel->setenv_packet = NULL;
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);
channel->setenv_packet = NULL;
@ -872,10 +864,9 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
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;
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 =
LIBSSH2_ALLOC(session, channel->reqPTY_packet_len);
if (!channel->reqPTY_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request");
}
*(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -976,12 +966,11 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc,
"Unable to send pty-request packet", 0);
LIBSSH2_FREE(session, channel->reqPTY_packet);
channel->reqPTY_packet = NULL;
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);
channel->reqPTY_packet = NULL;
@ -1010,10 +999,9 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
}
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;
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);
if (!channel->reqPTY_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request");
}
*(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -1090,12 +1077,11 @@ channel_request_pty_size(LIBSSH2_CHANNEL * channel, int width,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc,
"Unable to send window-change packet", 0);
LIBSSH2_FREE(session, channel->reqPTY_packet);
channel->reqPTY_packet = NULL;
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);
channel->reqPTY_packet = NULL;
@ -1164,9 +1150,8 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
s = channel->reqX11_packet =
LIBSSH2_ALLOC(session, channel->reqX11_packet_len);
if (!channel->reqX11_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request");
}
*(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -1217,12 +1202,11 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
return rc;
}
if (rc) {
libssh2_error(session, rc,
"Unable to send x11-req packet", 0);
LIBSSH2_FREE(session, channel->reqX11_packet);
channel->reqX11_packet = NULL;
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);
channel->reqX11_packet = NULL;
@ -1239,9 +1223,9 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
/* TODO: call libssh2_error() here! */
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) {
@ -1252,9 +1236,8 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
}
LIBSSH2_FREE(session, data);
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel x11-req", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel x11-req");
}
/*
@ -1310,10 +1293,9 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
s = channel->process_packet =
LIBSSH2_ALLOC(session, channel->process_packet_len);
if (!channel->process_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for channel-process request",
0);
return LIBSSH2_ERROR_ALLOC;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory "
"for channel-process request");
}
*(s++) = SSH_MSG_CHANNEL_REQUEST;
@ -1343,12 +1325,11 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
return rc;
}
else if (rc) {
libssh2_error(session, rc,
"Unable to send channel request", 0);
LIBSSH2_FREE(session, channel->process_packet);
channel->process_packet = NULL;
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);
channel->process_packet = NULL;
@ -1366,9 +1347,8 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
return rc;
} else if (rc) {
channel->process_state = libssh2_NB_state_idle;
libssh2_error(session, rc,
"Failed waiting for channel success", 0);
return rc;
return libssh2_error(session, rc,
"Failed waiting for 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_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
"Unable to complete request for channel-process-startup", 0);
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;
}
else if (rc) {
libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send transfer-window adjustment packet, "
"deferring", 0);
channel->adjust_queue = adjustment;
return rc;
return libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send transfer-window adjustment "
"packet, deferring");
}
else {
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);
if ((rc < 0) && (rc != PACKET_EAGAIN)) {
libssh2_error(session, rc, "tranport read", 0);
return rc;
return libssh2_error(session, rc, "tranport read");
}
/*
@ -1972,15 +1950,14 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
stream_id);
if (channel->local.close) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED,
"We've already closed this channel", 0);
return LIBSSH2_ERROR_CHANNEL_CLOSED;
return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED,
"We've already closed this channel");
}
if (channel->local.eof) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_EOF_SENT,
"EOF has already been sight, data might be ignored",
0);
return libssh2_error(session, LIBSSH2_ERROR_CHANNEL_EOF_SENT,
"EOF has already been sight, "
"data might be ignored");
}
/* [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 =
LIBSSH2_ALLOC(session, channel->write_packet_len);
if (!channel->write_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocte space for data transmission packet",
0);
return LIBSSH2_ERROR_ALLOC;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocte space "
"for data transmission packet");
}
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_packet);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, rc,
"Unable to send channel data", 0);
return rc;
return libssh2_error(session, rc,
"Unable to send channel data");
}
else if (rc) {
libssh2_error(session, rc,
"Unable to send channel data", 0);
LIBSSH2_FREE(session, channel->write_packet);
channel->write_packet = NULL;
channel->write_state = libssh2_NB_state_idle;
return rc;
return libssh2_error(session, rc,
"Unable to send channel data");
}
/* Shrink local window size */
channel->local.window_size -= channel->write_bufwrite;
@ -2131,9 +2105,8 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel)
return rc;
}
else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send EOF on channel", 0);
return LIBSSH2_ERROR_SOCKET_SEND;
return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send EOF on channel");
}
channel->local.eof = 1;
@ -2268,10 +2241,9 @@ channel_close(LIBSSH2_CHANNEL * channel)
if (retcode == PACKET_EAGAIN) {
return retcode;
} else if (retcode) {
libssh2_error(session, retcode,
"Unable to send close-channel request", 0);
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;
@ -2327,11 +2299,9 @@ static int channel_wait_closed(LIBSSH2_CHANNEL *channel)
int rc;
if (!libssh2_channel_eof(channel)) {
libssh2_error(session, LIBSSH2_ERROR_INVAL,
"libssh2_channel_wait_closed() invoked when channel is "
"not in EOF state",
0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_INVAL,
"libssh2_channel_wait_closed() invoked when "
"channel is not in EOF state");
}
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));
if (!strm) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for zlib compression/decompression",
0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"zlib compression/decompression");
}
memset(strm, 0, sizeof(z_stream));
@ -186,10 +185,9 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
out = (char *) strm->next_out;
strm->avail_out = out_maxlen;
if (!strm->next_out) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate compression/decompression buffer",
0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate compression/decompression "
"buffer");
}
while (strm->avail_in) {
int status;
@ -200,10 +198,9 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
status = inflate(strm, Z_PARTIAL_FLUSH);
}
if (status != Z_OK) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure", 0);
LIBSSH2_FREE(session, out);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure");
}
if (strm->avail_in) {
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);
if ((out_maxlen > (int) payload_limit) && !compress && limiter++) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression phase", 0);
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);
if (!newout) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand compress/decompression buffer",
0);
LIBSSH2_FREE(session, out);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand compress/"
"decompression buffer");
}
out = newout;
strm->next_out = (unsigned char *) out + out_ofs;
@ -241,11 +236,10 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
char *newout;
if (out_maxlen >= (int) payload_limit) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression phase",
0);
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)) {
@ -257,11 +251,10 @@ comp_method_zlib_comp(LIBSSH2_SESSION * session,
newout = LIBSSH2_REALLOC(session, out, out_maxlen);
if (!newout) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand final compress/decompress buffer",
0);
LIBSSH2_FREE(session, out);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand final compress/"
"decompress buffer");
}
out = newout;
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);
}
if (status != Z_OK) {
libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure", 0);
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_len -= 15;
if (sig_len != 40) {
libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Invalid DSS signature length", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Invalid DSS signature length");
}
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 =
LIBSSH2_ALLOC(session, exchange_state->e_packet_len);
if (!exchange_state->e_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Out of memory error",
0);
ret = LIBSSH2_ERROR_ALLOC;
ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Out of memory error");
goto clean_exit;
}
exchange_state->e_packet[0] = packet_type_init;
@ -146,9 +145,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc,
"Unable to send KEX init message", 0);
ret = rc;
ret = libssh2_error(session, rc,
"Unable to send KEX init message");
goto clean_exit;
}
exchange_state->state = libssh2_NB_state_sent;
@ -192,9 +190,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
return rc;
}
if (rc) {
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
"Timed out waiting for KEX reply", 0);
ret = rc;
ret = libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
"Timed out waiting for KEX reply");
goto clean_exit;
}
@ -206,10 +203,9 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
session->server_hostkey =
LIBSSH2_ALLOC(session, session->server_hostkey_len);
if (!session->server_hostkey) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for a copy of the host key",
0);
ret = LIBSSH2_ERROR_ALLOC;
ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for a copy "
"of the host key");
goto clean_exit;
}
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,
session->server_hostkey_len,
&session->server_hostkey_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
"Unable to initialize hostkey importer", 0);
ret = LIBSSH2_ERROR_HOSTKEY_INIT;
ret = libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
"Unable to initialize hostkey importer");
goto clean_exit;
}
@ -292,9 +287,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
exchange_state->k_value =
LIBSSH2_ALLOC(session, exchange_state->k_value_len);
if (!exchange_state->k_value) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate buffer for K", 0);
ret = LIBSSH2_ERROR_ALLOC;
ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate buffer for K");
goto clean_exit;
}
_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,
exchange_state->h_sig_len, exchange_state->h_sig_comp,
20, &session->server_hostkey_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN,
"Unable to verify hostkey signature", 0);
ret = -1;
ret = libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN,
"Unable to verify hostkey signature");
goto clean_exit;
}
@ -421,8 +414,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc, "Unable to send NEWKEYS message", 0);
ret = rc;
ret = libssh2_error(session, rc, "Unable to send NEWKEYS message");
goto clean_exit;
}
@ -437,8 +429,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc, "Timed out waiting for NEWKEYS", 0);
ret = rc;
ret = libssh2_error(session, rc, "Timed out waiting for NEWKEYS");
goto clean_exit;
}
/* The first key exchange has been performed,
@ -453,9 +444,8 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (!session->session_id) {
session->session_id = LIBSSH2_ALLOC(session, SHA_DIGEST_LENGTH);
if (!session->session_id) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate buffer for SHA digest", 0);
ret = LIBSSH2_ERROR_ALLOC;
ret = libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate buffer for SHA digest");
goto clean_exit;
}
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) {
return rc;
} else if (rc) {
libssh2_error(session, rc,
"Unable to send Group Exchange Request", 0);
ret = rc;
ret = libssh2_error(session, rc,
"Unable to send Group Exchange Request");
goto dh_gex_clean_exit;
}
@ -841,9 +830,8 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc,
"Timeout waiting for GEX_GROUP reply", 0);
ret = rc;
ret = libssh2_error(session, rc,
"Timeout waiting for GEX_GROUP reply");
goto dh_gex_clean_exit;
}
@ -1038,9 +1026,8 @@ static int kexinit(LIBSSH2_SESSION * session)
s = data = LIBSSH2_ALLOC(session, data_len);
if (!data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory", 0);
return LIBSSH2_ERROR_ALLOC;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory");
}
*(s++) = SSH_MSG_KEXINIT;
@ -1127,10 +1114,10 @@ static int kexinit(LIBSSH2_SESSION * session)
}
else if (rc) {
LIBSSH2_FREE(session, data);
libssh2_error(session, rc,
"Unable to send KEXINIT packet to remote host", 0);
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) {
@ -1749,9 +1736,8 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
return retcode;
} else if (retcode) {
libssh2_error(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE,
"Unrecoverable error exchanging keys", 0);
rc = retcode;
rc = libssh2_error(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE,
"Unrecoverable error exchanging keys");
}
}
}
@ -1839,16 +1825,14 @@ libssh2_session_method_pref(LIBSSH2_SESSION * session, int method_type,
break;
default:
libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid parameter specified for method_type", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid parameter specified for method_type");
}
s = newprefs = LIBSSH2_ALLOC(session, prefs_len + 1);
if (!newprefs) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Error allocated space for method preferences", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Error allocated space for method preferences");
}
memcpy(s, prefs, prefs_len + 1);
@ -1873,11 +1857,10 @@ libssh2_session_method_pref(LIBSSH2_SESSION * session, int method_type,
}
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);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED,
"The requested method(s) are not currently "
"supported");
}
if (*prefvar) {

View File

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

View File

@ -731,8 +731,6 @@ struct _LIBSSH2_SESSION
/* Error tracking */
const char *err_msg;
unsigned long err_msglen;
int err_should_free;
int err_code;
/* struct members for packet-level reading */
@ -1036,8 +1034,7 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
#endif
#endif
int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg,
int should_free);
int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg);
#define LIBSSH2_SOCKET_UNKNOWN 1
#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
* All rights reserved.
*
@ -49,15 +49,9 @@
#include <errno.h>
int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg,
int should_free)
int libssh2_error(LIBSSH2_SESSION* session, int errcode, const char* errmsg)
{
if (session->err_msg && session->err_should_free) {
LIBSSH2_FREE(session, session->err_msg);
}
session->err_msg = errmsg;
session->err_msglen = strlen(errmsg);
session->err_should_free = should_free;
session->err_code = errcode;
#ifdef LIBSSH2DEBUG
_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);
d = (unsigned char *) *data;
if (!d) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for base64 decoding", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for base64 decoding");
}
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
octet */
LIBSSH2_FREE(session, *data);
libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid data (byte belonging to partial octet)", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_INVAL,
"Invalid data (byte belonging to partial octet)");
}
*datalen = len;

View File

@ -141,8 +141,8 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!channel) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection",
0);
"Unable to allocate a channel for "
"new connection");
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
listen_state->state = libssh2_NB_state_sent;
break;
@ -159,8 +159,8 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
1);
if (!channel->channel_type) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection",
0);
"Unable to allocate a channel for new"
" connection");
LIBSSH2_FREE(session, channel);
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
listen_state->state = libssh2_NB_state_sent;
@ -212,11 +212,10 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN)
return rc;
else if (rc) {
libssh2_error(session, rc,
"Unable to send channel "
"open confirmation", 0);
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 */
@ -253,9 +252,9 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc, "Unable to send open failure", 0);
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;
return 0;
@ -308,8 +307,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!channel) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection",
0);
"Unable to allocate a channel for new connection");
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
goto x11_exit;
}
@ -322,8 +320,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
1);
if (!channel->channel_type) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for new connection",
0);
"Unable to allocate a channel for new connection");
LIBSSH2_FREE(session, channel);
failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */
goto x11_exit;
@ -369,10 +366,10 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) {
return 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;
return -1;
return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send channel open "
"confirmation");
}
/* Link the channel into the session */
@ -409,9 +406,8 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, rc, "Unable to send open failure", 0);
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;
return 0;
@ -456,9 +452,6 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
/* Calling app has given the OK, Process it anyway */
macstate = LIBSSH2_MAC_CONFIRMED;
} else {
libssh2_error(session, LIBSSH2_ERROR_INVALID_MAC,
"Invalid Message Authentication Code received",
0);
if (session->ssh_msg_disconnect) {
LIBSSH2_DISCONNECT(session, SSH_DISCONNECT_MAC_ERROR,
"Invalid MAC received",
@ -466,7 +459,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
"", 0);
}
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);
session->socket_state = LIBSSH2_SOCKET_DISCONNECTED;
session->packAdd_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT,
"socket disconnect", 0);
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
return libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT,
"socket disconnect");
}
break;
@ -636,8 +629,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
if (!session->packAdd_channel) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_UNKNOWN,
"Packet received for unknown channel, ignoring",
0);
"Packet received for unknown channel, ignoring");
LIBSSH2_FREE(session, data);
session->packAdd_state = libssh2_NB_state_idle;
return 0;
@ -693,7 +685,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
"Packet contains more data than we offered"
" to receive, truncating", 0);
" to receive, truncating");
datalen =
session->packAdd_channel->remote.packet_size +
session->packAdd_data_head;
@ -706,8 +698,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED,
"The current receive window is full,"
" data ignored",
0);
" data ignored");
LIBSSH2_FREE(session, data);
session->packAdd_state = libssh2_NB_state_idle;
return 0;
@ -720,8 +711,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
libssh2_error(session,
LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED,
"Remote sent more data than current "
"window allows, truncating",
0);
"window allows, truncating");
datalen =
session->packAdd_channel->remote.window_size +
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
*/
#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
publickey_status_error(const LIBSSH2_PUBLICKEY *pkey,
LIBSSH2_SESSION * session, int status,
const unsigned char *message, int message_len)
LIBSSH2_SESSION *session, int status)
{
const char *status_text;
int status_text_len;
char *m, *s;
int m_len;
const char *msg;
/* GENERAL_FAILURE got remapped between version 1 and 2 */
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) {
status_text = "unknown";
status_text_len = sizeof("unknown") - 1;
msg = "unknown";
} else {
status_text = publickey_status_codes[status].name;
status_text_len = publickey_status_codes[status].name_len;
msg = publickey_status_codes[status].name;
}
m_len =
(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);
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, msg);
}
/*
@ -173,18 +140,17 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc != 4) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid response from publickey subsystem", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid response from publickey subsystem");
}
pkey->receive_packet_len = _libssh2_ntohu32(buffer);
pkey->receive_packet =
LIBSSH2_ALLOC(session, pkey->receive_packet_len);
if (!pkey->receive_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate publickey response buffer", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate publickey response "
"buffer");
}
pkey->receive_state = libssh2_NB_state_sent;
@ -196,13 +162,12 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
if (rc == PACKET_EAGAIN) {
return rc;
} 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);
pkey->receive_packet = NULL;
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;
@ -268,60 +233,36 @@ publickey_response_success(LIBSSH2_PUBLICKEY * pkey)
if (rc == PACKET_EAGAIN) {
return rc;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from publickey subsystem",
0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from "
"publickey subsystem");
}
s = data;
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);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code");
}
switch (response) {
case LIBSSH2_PUBLICKEY_RESPONSE_STATUS:
/* Error, or processing complete */
{
unsigned long status, descr_len, lang_len;
unsigned char *descr, *lang;
unsigned long status = _libssh2_ntohu32(s);
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);
if (status == LIBSSH2_PUBLICKEY_SUCCESS)
return 0;
}
publickey_status_error(pkey, session, status, descr,
descr_len);
LIBSSH2_FREE(session, data);
publickey_status_error(pkey, session, status);
return -1;
}
default:
/* Unknown/Unexpected */
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Unexpected publickey subsystem response, ignoring",
0);
"Unexpected publickey subsystem response, ignoring");
LIBSSH2_FREE(session, data);
data = NULL;
}
@ -372,13 +313,13 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
LIBSSH2_ERROR_EAGAIN)) {
/* The error state is already set, so leave it */
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block to startup channel", 0);
"Would block to startup channel");
return NULL;
} else if (!session->pkeyInit_channel
&& (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to startup channel", 0);
"Unable to startup channel");
goto err_exit;
}
} while (!session->pkeyInit_channel);
@ -393,11 +334,11 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
"publickey", strlen("publickey"));
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting publickey subsystem", 0);
"Would block starting publickey subsystem");
return NULL;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
"Unable to request publickey subsystem", 0);
"Unable to request publickey subsystem");
goto err_exit;
}
@ -409,7 +350,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting publickey subsystem", 0);
"Would block starting publickey subsystem");
return NULL;
}
@ -417,7 +358,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PUBLICKEY));
if (!session->pkeyInit_pkey) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a new publickey structure", 0);
"Unable to allocate a new publickey structure");
goto err_exit;
}
memset(session->pkeyInit_pkey, 0, sizeof(LIBSSH2_PUBLICKEY));
@ -446,11 +387,11 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
(char *) buffer, (s - buffer));
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending publickey version packet", 0);
"Would block sending publickey version packet");
return NULL;
} else if ((s - buffer) != rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey version packet", 0);
"Unable to send publickey version packet");
goto err_exit;
}
@ -464,13 +405,13 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
&session->pkeyInit_data_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from publickey subsystem",
0);
"Would block waiting for response from "
"publickey subsystem");
return NULL;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from publickey subsystem",
0);
"Timeout waiting for response from "
"publickey subsystem");
goto err_exit;
}
@ -478,7 +419,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
if ((response =
publickey_response_id(&s, session->pkeyInit_data_len)) < 0) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code", 0);
"Invalid publickey subsystem response code");
goto err_exit;
}
@ -504,13 +445,12 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
session->pkeyInit_data + session->pkeyInit_data_len) {
libssh2_error(session,
LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Malformed publickey subsystem packet",
0);
"Malformed publickey subsystem packet");
goto err_exit;
}
publickey_status_error(NULL, session, status,
descr, descr_len);
publickey_status_error(NULL, session, status);
goto err_exit;
}
@ -536,8 +476,8 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
default:
/* Unknown/Unexpected */
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Unexpected publickey subsystem response, ignoring",
0);
"Unexpected publickey subsystem response, "
"ignoring");
LIBSSH2_FREE(session, session->pkeyInit_data);
session->pkeyInit_data = NULL;
}
@ -551,7 +491,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
rc = libssh2_channel_close(session->pkeyInit_channel);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block closing channel", 0);
"Would block closing channel");
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);
if (!pkey->add_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey \"add\" packet",
0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"publickey \"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) {
return 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);
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);
pkey->add_packet = NULL;
@ -725,10 +663,9 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey,
pkey->remove_packet = LIBSSH2_ALLOC(session, packet_len);
if (!pkey->remove_packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey \"remove\" packet",
0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"publickey \"remove\" packet");
}
pkey->remove_s = pkey->remove_packet;
@ -760,12 +697,11 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey,
if (rc == PACKET_EAGAIN) {
return 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);
pkey->remove_packet = NULL;
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);
pkey->remove_packet = NULL;
@ -823,10 +759,9 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
if (rc == PACKET_EAGAIN) {
return 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;
return -1;
return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send publickey list packet");
}
pkey->listFetch_state = libssh2_NB_state_sent;
@ -839,8 +774,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
return rc;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
"Timeout waiting for response from publickey subsystem",
0);
"Timeout waiting for response from "
"publickey subsystem");
goto err_exit;
}
@ -849,7 +784,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
publickey_response_id(&pkey->listFetch_s,
pkey->listFetch_data_len)) < 0) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Invalid publickey subsystem response code", 0);
"Invalid publickey subsystem response code");
goto err_exit;
}
@ -874,7 +809,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
if (pkey->listFetch_s >
pkey->listFetch_data + pkey->listFetch_data_len) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Malformed publickey subsystem packet", 0);
"Malformed publickey subsystem packet");
goto err_exit;
}
@ -887,8 +822,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
return 0;
}
publickey_status_error(pkey, session, status, descr,
descr_len);
publickey_status_error(pkey, session, status);
goto err_exit;
}
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));
if (!newlist) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey list",
0);
"Unable to allocate memory for "
"publickey list");
goto err_exit;
}
list = newlist;
@ -921,8 +855,8 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
sizeof(libssh2_publickey_attribute));
if (!list[keys].attrs) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey attributes",
0);
"Unable to allocate memory for "
"publickey attributes");
goto err_exit;
}
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));
if (!list[keys].attrs) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for publickey attributes",
0);
"Unable to allocate memory for "
"publickey attributes");
goto err_exit;
}
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:
/* Unknown/Unexpected */
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
"Unexpected publickey subsystem response, ignoring",
0);
"Unexpected publickey subsystem response, ignoring");
LIBSSH2_FREE(session, pkey->listFetch_data);
pkey->listFetch_data = NULL;
}

121
src/scp.c
View File

@ -286,8 +286,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!session->scpRecv_command) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a command buffer for SCP session",
0);
"Unable to allocate a command buffer for "
"SCP session");
return NULL;
}
@ -324,7 +324,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
}
else {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting up channel", 0);
"Would block starting up channel");
}
return NULL;
}
@ -340,7 +340,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_command_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting SCP startup", 0);
"Would block requesting SCP startup");
return NULL;
} else if (rc) {
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);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending initial wakeup", 0);
"Would block sending initial wakeup");
return NULL;
} else if (rc != 1) {
goto scp_recv_error;
@ -387,12 +387,12 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_response_len, 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for SCP response", 0);
"Would block waiting for SCP response");
return NULL;
} else if (rc <= 0) {
/* Timeout, give up */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Timed out waiting for SCP response", 0);
"Timed out waiting for SCP response");
goto scp_recv_error;
}
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
*/
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid data in SCP response, missing Time data",
0);
"Invalid data in SCP response, missing Time data");
session->scpRecv_err_len =
_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
* PACKET_EAGAIN
*/
LIBSSH2_FREE(session, session->scpRecv_err_msg);
session->scpRecv_err_msg = NULL;
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unknown error while getting error string",
0);
goto scp_recv_error;
"Unknown error" );
}
else
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
session->scpRecv_err_msg, 1);
"SCP protocol error");
/* TODO: for debugging purposes, the
session->scpRecv_err_msg should be displayed here
when available */
LIBSSH2_FREE(session, session->scpRecv_err_msg);
session->scpRecv_err_msg = NULL;
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] !=
'\n')) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid data in SCP response", 0);
"Invalid data in SCP response");
goto scp_recv_error;
}
@ -470,8 +471,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
LIBSSH2_SCP_RESPONSE_BUFLEN) {
/* You had your chance */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unterminated response from SCP server",
0);
"Unterminated response from SCP server");
goto scp_recv_error;
}
/* 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) {
/* EOL came too soon */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short",
0);
"Invalid response from SCP server, "
"too short" );
goto scp_recv_error;
}
@ -505,8 +505,8 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!p || ((p - s) <= 0)) {
/* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, malformed mtime",
0);
"Invalid response from SCP server, "
"malformed mtime");
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);
if (errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid mtime",
0);
"Invalid response from SCP server, invalid mtime");
goto scp_recv_error;
}
s = (unsigned char *) strchr((char *) p, ' ');
if (!s || ((s - p) <= 0)) {
/* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, malformed mtime.usec",
0);
"Invalid response from SCP server, malformed mtime.usec");
goto scp_recv_error;
}
@ -535,8 +533,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!p || ((p - s) <= 0)) {
/* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short or malformed",
0);
"Invalid response from SCP server, too short or malformed");
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);
if (errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid atime",
0);
"Invalid response from SCP server, invalid atime");
goto scp_recv_error;
}
@ -563,7 +559,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
scpRecv_response, 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting to send SCP ACK", 0);
"Would block waiting to send SCP ACK");
return NULL;
} else if (rc != 1) {
goto scp_recv_error;
@ -600,19 +596,19 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
session->scpRecv_response_len, 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for SCP response", 0);
"Would block waiting for SCP response");
return NULL;
} else if (rc <= 0) {
/* Timeout, give up */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Timed out waiting for SCP response", 0);
"Timed out waiting for SCP response");
goto scp_recv_error;
}
session->scpRecv_response_len++;
if (session->scpRecv_response[0] != 'C') {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server", 0);
"Invalid response from SCP server");
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] >
126))) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid data in SCP response", 0);
"Invalid data in SCP response");
goto scp_recv_error;
}
@ -642,8 +638,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
LIBSSH2_SCP_RESPONSE_BUFLEN) {
/* You had your chance */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unterminated response from SCP server",
0);
"Unterminated response from SCP server");
goto scp_recv_error;
}
/* 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) {
/* EOL came too soon */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short",
0);
"Invalid response from SCP server, too short");
goto scp_recv_error;
}
@ -678,8 +672,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!p || ((p - s) <= 0)) {
/* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, malformed mode",
0);
"Invalid response from SCP server, malformed mode");
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);
if ((e && *e) || errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid mode",
0);
"Invalid response from SCP server, invalid mode");
goto scp_recv_error;
}
@ -698,8 +690,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (!s || ((s - p) <= 0)) {
/* No spaces or space in the wrong spot */
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, too short or malformed",
0);
"Invalid response from SCP server, too short or malformed");
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);
if ((e && *e) || errno) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid response from SCP server, invalid size",
0);
"Invalid response from SCP server, invalid size");
goto scp_recv_error;
}
@ -726,7 +716,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
scpRecv_response, 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending SCP ACK", 0);
"Would block sending SCP ACK");
return NULL;
} else if (rc != 1) {
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);
if (!session->scpSend_command) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a command buffer for scp session",
0);
"Unable to allocate a command buffer for scp session");
return NULL;
}
@ -838,7 +827,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
}
else {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting up channel", 0);
"Would block starting up channel");
}
return NULL;
}
@ -854,7 +843,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
session->scpSend_command_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting SCP startup", 0);
"Would block requesting SCP startup");
return NULL;
}
else if (rc) {
@ -863,7 +852,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
LIBSSH2_FREE(session, session->scpSend_command);
session->scpSend_command = NULL;
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unknown error while getting error string", 0);
"Unknown error while getting error string");
goto scp_send_error;
}
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);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from remote", 0);
"Would block waiting for response from remote");
return NULL;
} else if ((rc <= 0) || (session->scpSend_response[0] != 0)) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0);
"Invalid ACK response from remote");
goto scp_send_error;
}
@ -907,11 +896,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
session->scpSend_response_len);
if (rc == PACKET_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;
} else if (rc != (int)session->scpSend_response_len) {
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;
}
@ -924,11 +913,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
(char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response", 0);
"Would block waiting for response");
return NULL;
} else if ((rc <= 0) || (session->scpSend_response[0] != 0)) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0);
"Invalid ACK response from remote");
goto scp_send_error;
}
@ -965,11 +954,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
session->scpSend_response_len);
if (rc == PACKET_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;
} else if (rc != (int)session->scpSend_response_len) {
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;
}
@ -982,11 +971,11 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
(char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response", 0);
"Would block waiting for response");
return NULL;
} else if (rc <= 0) {
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0);
"Invalid ACK response from remote");
goto scp_send_error;
} 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
*/
libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Invalid ACK response from remote", 0);
"Invalid ACK response from remote");
session->scpSend_err_len =
_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,
session->scpSend_err_msg, 1);
"failed waiting for ACK");
session->scpSend_err_msg = NULL;
goto scp_send_error;
}

View File

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

View File

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

View File

@ -135,8 +135,6 @@ decrypt(LIBSSH2_SESSION * session, unsigned char *source,
while (len >= blocksize) {
if (session->remote.crypt->crypt(session, source,
&session->remote.crypt_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_DECRYPT,
(char *) "Error decrypting packet", 0);
LIBSSH2_FREE(session, p->payload);
return PACKET_FAIL;
}
@ -232,11 +230,9 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
/* We need a freeable struct otherwise the
* brigade won't know what to do with it */
p->payload = LIBSSH2_ALLOC(session, data_len);
if (!p->payload) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, (char *)
"Unable to allocate memory", 0);
if (!p->payload)
return PACKET_ENOMEM;
}
memcpy(p->payload, data, 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);
if (!session->userauth_list_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth_list", 0);
"Unable to allocate memory for userauth_list");
return NULL;
}
@ -109,11 +109,11 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
session->userauth_list_data_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list", 0);
"Would block requesting userauth list");
return NULL;
} else if (rc) {
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);
session->userauth_list_data = NULL;
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);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list", 0);
"Would block requesting userauth list");
return NULL;
} 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;
return NULL;
}
if (session->userauth_list_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
/* 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);
session->userauth_list_data = NULL;
session->state |= LIBSSH2_STATE_AUTHENTICATED;
@ -228,10 +228,9 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
s = session->userauth_pswd_data =
LIBSSH2_ALLOC(session, session->userauth_pswd_data_len);
if (!session->userauth_pswd_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth-password"
" request", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"userauth-password 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,
session->userauth_pswd_data_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block writing password request", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block writing password request");
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-password request", 0);
LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL;
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);
session->userauth_pswd_data = NULL;
@ -298,14 +295,12 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
&session->
userauth_pswd_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting");
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_TIMEOUT,
"Would block waiting", 0);
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) {
@ -322,11 +317,10 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL;
session->userauth_pswd_state = libssh2_NB_state_idle;
libssh2_error(session,
return libssh2_error(session,
LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed (username/password)",
0);
return -1;
"Authentication failed "
"(username/password)");
}
session->userauth_pswd_newpw = NULL;
@ -356,11 +350,10 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
&session->userauth_pswd_newpw_len,
&session->abstract);
if (!session->userauth_pswd_newpw) {
libssh2_error(session,
return libssh2_error(session,
LIBSSH2_ERROR_PASSWORD_EXPIRED,
"Password expired, and callback failed",
0);
return -1;
"Password expired, and "
"callback failed");
}
/* basic data_len + newpw_len(4) */
@ -372,13 +365,13 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
LIBSSH2_ALLOC(session,
session->userauth_pswd_data_len);
if (!session->userauth_pswd_data) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth-password-change request",
0);
LIBSSH2_FREE(session,
session->userauth_pswd_newpw);
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;
@ -421,19 +414,19 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
session->
userauth_pswd_data_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting", 0);
return rc;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-password-change request",
0);
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting");
}
else if (rc) {
LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL;
LIBSSH2_FREE(session,
session->userauth_pswd_newpw);
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);
session->userauth_pswd_data = NULL;
@ -449,11 +442,10 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
}
}
} else {
libssh2_error(session, LIBSSH2_ERROR_PASSWORD_EXPIRED,
"Password Expired, and no callback specified",
0);
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_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed");
}
/*
@ -516,9 +507,8 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
/* Read Public Key */
fd = fopen(pubkeyfile, "r");
if (!fd) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to open public key file", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to open public key file");
}
while (!feof(fd) && (c = fgetc(fd)) != '\r' && c != '\n')
pubkey_len++;
@ -529,25 +519,22 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
rewind(fd);
if (pubkey_len <= 1) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid data in public key file", 0);
fclose(fd);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid data in public key file");
}
pubkey = LIBSSH2_ALLOC(session, pubkey_len);
if (!pubkey) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for public key data", 0);
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) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to read public key from file", 0);
LIBSSH2_FREE(session, pubkey);
fclose(fd);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to read public key from file");
}
fclose(fd);
/*
@ -557,17 +544,15 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
pubkey_len--;
if (!pubkey_len) {
libssh2_error(session, LIBSSH2_ERROR_FILE, "Missing public key data",
0);
LIBSSH2_FREE(session, pubkey);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Missing public key data");
}
if ((sp1 = memchr(pubkey, ' ', pubkey_len)) == NULL) {
libssh2_error(session, LIBSSH2_ERROR_FILE, "Invalid public key data",
0);
LIBSSH2_FREE(session, pubkey);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid public key data");
}
sp1++;
@ -579,10 +564,9 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
if (libssh2_base64_decode(session, (char **) &tmp, &tmp_len,
(char *) sp1, sp2 - sp1)) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Invalid key data, not base64 encoded", 0);
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
@ -626,17 +610,15 @@ file_read_privatekey(LIBSSH2_SESSION * session,
hostkey_methods_avail++;
}
if (!*hostkey_method) {
libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE,
"No handler for specified private key", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE,
"No handler for specified private key");
}
if ((*hostkey_method)->
initPEM(session, privkeyfile, (unsigned char *) passphrase,
hostkey_abstract)) {
libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to initialize private key from file", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to initialize private key from file");
}
return 0;
@ -710,13 +692,13 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
memset(&session->userauth_host_packet_requirev_state, 0,
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,
&pubkeydata, &pubkeydata_len,
publickey)) {
publickey);
if(rc)
/* Note: file_read_publickey() calls libssh2_error() */
return -1;
}
return rc;
/*
* 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);
session->userauth_host_method = NULL;
LIBSSH2_FREE(session, pubkeydata);
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Out of memory", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Out of memory");
}
*(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 + sig_len)); /* PK sigblob */
if (!newpacket) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating additional space for "
"userauth-hostbased packet",
0);
LIBSSH2_FREE(session, sig);
LIBSSH2_FREE(session, session->userauth_host_packet);
session->userauth_host_packet = NULL;
LIBSSH2_FREE(session, session->userauth_host_method);
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;
}
@ -878,16 +857,14 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
session->userauth_host_s -
session->userauth_host_packet);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
}
else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-hostbased request", 0);
LIBSSH2_FREE(session, session->userauth_host_packet);
session->userauth_host_packet = NULL;
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);
session->userauth_host_packet = NULL;
@ -905,15 +882,13 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
&session->
userauth_host_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
}
session->userauth_host_state = libssh2_NB_state_idle;
if (rc) {
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Auth failed", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Auth failed");
}
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 */
LIBSSH2_FREE(session, session->userauth_host_data);
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 "
"username/public key combination",
0);
return -1;
"username/public key combination");
}
/* libssh2_userauth_hostbased_fromfile_ex
@ -994,10 +967,9 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_method =
LIBSSH2_ALLOC(session, session->userauth_pblc_method_len);
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 "
"data", 0);
return -1;
"data");
}
memcpy(session->userauth_pblc_method, pubkeydata + 4,
session->userauth_pblc_method_len);
@ -1028,8 +1000,8 @@ userauth_publickey(LIBSSH2_SESSION *session,
if (!session->userauth_pblc_packet) {
LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL;
libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Out of memory", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Out of memory");
}
*(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) {
rc = _libssh2_transport_write(session, session->userauth_pblc_packet,
session->userauth_pblc_packet_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-publickey request", 0);
if (rc == PACKET_EAGAIN)
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL;
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;
@ -1098,8 +1068,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
&session->
userauth_pblc_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
}
else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_packet);
@ -1107,9 +1076,8 @@ userauth_publickey(LIBSSH2_SESSION *session,
LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Failed waiting", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Failed waiting");
}
if (session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
@ -1138,10 +1106,9 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_packet = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_method);
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;
return -1;
return libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Username/PublicKey combination invalid");
}
/* Semi-Success! */
@ -1160,10 +1127,9 @@ userauth_publickey(LIBSSH2_SESSION *session,
s = buf = LIBSSH2_ALLOC(session, 4 + session->session_id_len
+ session->userauth_pblc_packet_len);
if (!buf) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for userauth-publickey "
"signed data", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"userauth-publickey signed data");
}
_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);
LIBSSH2_FREE(session, buf);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL;
session->userauth_pblc_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Callback returned error", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Callback returned error");
}
/*
@ -1203,17 +1167,15 @@ userauth_publickey(LIBSSH2_SESSION *session,
(4 + session->userauth_pblc_method_len)
+ (4 + sig_len)); /* PK sigblob */
if (!newpacket) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Failed allocating additional space for "
"userauth-publickey packet",
0);
LIBSSH2_FREE(session, sig);
LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL;
LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL;
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;
}
@ -1252,15 +1214,13 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_s -
session->userauth_pblc_packet);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-publickey request", 0);
LIBSSH2_FREE(session, session->userauth_pblc_packet);
session->userauth_pblc_packet = NULL;
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);
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_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list");
} else if (rc) {
session->userauth_pblc_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Failed waiting", 0);
return -1;
return libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
"Failed waiting");
}
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 */
LIBSSH2_FREE(session, session->userauth_pblc_data);
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;
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;
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,
&pubkeydata, &pubkeydata_len, publickey)) {
return -1;
}
&pubkeydata, &pubkeydata_len, publickey);
if(rc)
return rc;
}
rc = userauth_publickey(session, username, username_len,
@ -1433,11 +1389,9 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
session->userauth_kybd_data = s =
LIBSSH2_ALLOC(session, session->userauth_kybd_packet_len);
if (!s) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
return libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"keyboard-interactive authentication",
0);
return -1;
"keyboard-interactive authentication");
}
*s++ = SSH_MSG_USERAUTH_REQUEST;
@ -1478,15 +1432,13 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
rc = _libssh2_transport_write(session, session->userauth_kybd_data,
session->userauth_kybd_packet_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} else if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send keyboard-interactive request", 0);
LIBSSH2_FREE(session, session->userauth_kybd_data);
session->userauth_kybd_data = NULL;
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);
session->userauth_kybd_data = NULL;
@ -1503,13 +1455,13 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
&session->
userauth_kybd_packet_requirev_state);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block");
} else if (rc) {
session->userauth_kybd_state = libssh2_NB_state_idle;
libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Failed waiting", 0);
return -1;
return libssh2_error(session,
LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Failed waiting");
}
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);
session->userauth_kybd_data = NULL;
session->userauth_kybd_state = libssh2_NB_state_idle;
libssh2_error(session,
return libssh2_error(session,
LIBSSH2_ERROR_AUTHENTICATION_FAILED,
"Authentication failed (keyboard-interactive)",
0);
return -1;
"Authentication failed "
"(keyboard-interactive)");
}
/* server requested PAM-like conversation */
@ -1547,8 +1498,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_auth_name) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"keyboard-interactive 'name' request field",
0);
"keyboard-interactive 'name' request field");
goto cleanup;
}
memcpy(session->userauth_kybd_auth_name, s,
@ -1565,8 +1515,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"keyboard-interactive 'instruction' "
"request field",
0);
"request field");
goto cleanup;
}
memcpy(session->userauth_kybd_auth_instruction, s,
@ -1590,8 +1539,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_prompts) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"keyboard-interactive prompts array",
0);
"keyboard-interactive prompts array");
goto cleanup;
}
memset(session->userauth_kybd_prompts, 0,
@ -1605,8 +1553,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_responses) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"keyboard-interactive responses array",
0);
"keyboard-interactive responses array");
goto cleanup;
}
memset(session->userauth_kybd_responses, 0,
@ -1623,8 +1570,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!session->userauth_kybd_prompts[i].text) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for "
"keyboard-interactive prompt message",
0);
"keyboard-interactive prompt message");
goto cleanup;
}
memcpy(session->userauth_kybd_prompts[i].text, s,
@ -1668,8 +1614,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (!s) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for keyboard-"
"interactive response packet",
0);
"interactive response packet");
goto cleanup;
}
@ -1693,13 +1638,13 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
rc = _libssh2_transport_write(session, session->userauth_kybd_data,
session->userauth_kybd_packet_len);
if (rc == PACKET_EAGAIN) {
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block", 0);
return rc;
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block");
}
if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-keyboard-interactive"
" request", 0);
" request");
goto cleanup;
}