diff options
-rw-r--r-- | libssh/channels1.c | 2 | ||||
-rw-r--r-- | libssh/client.c | 2 | ||||
-rw-r--r-- | libssh/kex.c | 6 | ||||
-rw-r--r-- | libssh/packet.c | 197 | ||||
-rw-r--r-- | libssh/server.c | 2 |
5 files changed, 112 insertions, 97 deletions
diff --git a/libssh/channels1.c b/libssh/channels1.c index fb742e6..fcac073 100644 --- a/libssh/channels1.c +++ b/libssh/channels1.c @@ -161,7 +161,7 @@ int channel_change_pty_size1(CHANNEL *channel, int cols, int rows) { ssh_log(session, SSH_LOG_RARE, "Change pty size send"); - if (packet_wait(session, SSH_SMSG_SUCCESS, 1) < 0) { + if (packet_wait(session, SSH_SMSG_SUCCESS, 1) != SSH_OK) { return -1; } diff --git a/libssh/client.c b/libssh/client.c index d91c55c..77e80a4 100644 --- a/libssh/client.c +++ b/libssh/client.c @@ -268,7 +268,7 @@ int ssh_service_request(SSH_SESSION *session, const char *service) { packet_send(session); ssh_log(session, SSH_LOG_PACKET, "Sent SSH_MSG_SERVICE_REQUEST (service %s)\n", service); - if(packet_wait(session,SSH2_MSG_SERVICE_ACCEPT,1)){ + if (packet_wait(session,SSH2_MSG_SERVICE_ACCEPT,1) != SSH_OK) { ssh_set_error(session,SSH_FATAL,"did not receive SERVICE_ACCEPT"); leave_function(); return -1; diff --git a/libssh/kex.c b/libssh/kex.c index a0dae95..1ff85c6 100644 --- a/libssh/kex.c +++ b/libssh/kex.c @@ -236,7 +236,7 @@ int ssh_get_kex(SSH_SESSION *session, int server_kex) { enter_function(); - if (packet_wait(session, SSH2_MSG_KEXINIT, 1)) { + if (packet_wait(session, SSH2_MSG_KEXINIT, 1) != SSH_OK) { leave_function(); return -1; } @@ -624,7 +624,7 @@ int ssh_get_kex1(SSH_SESSION *session) { enter_function(); ssh_log(session, SSH_LOG_PROTOCOL, "Waiting for a SSH_SMSG_PUBLIC_KEY"); - if (packet_wait(session, SSH_SMSG_PUBLIC_KEY, 1)) { + if (packet_wait(session, SSH_SMSG_PUBLIC_KEY, 1) != SSH_OK) { leave_function(); return -1; } @@ -760,7 +760,7 @@ int ssh_get_kex1(SSH_SESSION *session) { session->next_crypto = NULL; ssh_log(session, SSH_LOG_PROTOCOL, "Waiting for a SSH_SMSG_SUCCESS"); - if (packet_wait(session,SSH_SMSG_SUCCESS,1)) { + if (packet_wait(session,SSH_SMSG_SUCCESS,1) != SSH_OK) { char buffer[1024] = {0}; snprintf(buffer, sizeof(buffer), "Key exchange failed: %s", ssh_get_error(session)); diff --git a/libssh/packet.c b/libssh/packet.c index 816dc3f..4ee3aaa 100644 --- a/libssh/packet.c +++ b/libssh/packet.c @@ -662,105 +662,120 @@ void packet_parse(SSH_SESSION *session) { } #ifdef HAVE_SSH1 -static int packet_wait1(SSH_SESSION *session,int type,int blocking){ - enter_function(); - ssh_log(session,SSH_LOG_PROTOCOL,"packet_wait1 waiting for %d",type); - while(1){ - if ((packet_read1(session) != SSH_OK) || - (packet_translate(session) != SSH_OK)) { - leave_function(); - return -1; +static int packet_wait1(SSH_SESSION *session, int type, int blocking) { + + enter_function(); + + ssh_log(session, SSH_LOG_PROTOCOL, "packet_wait1 waiting for %d", type); + + do { + if ((packet_read1(session) != SSH_OK) || + (packet_translate(session) != SSH_OK)) { + leave_function(); + return SSH_ERROR; + } + ssh_log(session, SSH_LOG_PACKET, "packet_wait1() received a type %d packet", + session->in_packet.type); + switch (session->in_packet.type) { + case SSH_MSG_DISCONNECT: + packet_parse(session); + leave_function(); + return SSH_ERROR; + case SSH_SMSG_STDOUT_DATA: + case SSH_SMSG_STDERR_DATA: + case SSH_SMSG_EXITSTATUS: + if (channel_handle1(session,type) < 0) { + leave_function(); + return SSH_ERROR; } - ssh_log(session,SSH_LOG_PACKET,"packet_wait 1 received a type %d packet",session->in_packet.type); - switch(session->in_packet.type){ - case SSH_MSG_DISCONNECT: - packet_parse(session); - leave_function(); - return -1; - case SSH_SMSG_STDOUT_DATA: - case SSH_SMSG_STDERR_DATA: - case SSH_SMSG_EXITSTATUS: - if (channel_handle1(session,type) < 0) { - leave_function(); - return -1; - } - break; - case SSH_MSG_DEBUG: - case SSH_MSG_IGNORE: - break; -/* case SSH2_MSG_CHANNEL_CLOSE: - packet_parse(session); - break;; -*/ - default: - if(type && (type != session->in_packet.type)){ - ssh_set_error(session,SSH_FATAL,"waitpacket(): Received a %d type packet, was waiting for a %d\n",session->in_packet.type,type); - leave_function(); - return -1; - } - leave_function(); - return 0; - } - if(blocking==0){ - leave_function(); - return 0; + break; + case SSH_MSG_DEBUG: + case SSH_MSG_IGNORE: + break; + /* case SSH2_MSG_CHANNEL_CLOSE: + packet_parse(session); + break;; + */ + default: + if (type && (type != session->in_packet.type)) { + ssh_set_error(session, SSH_FATAL, + "packet_wait1(): Received a %d type packet, but expected %d\n", + session->in_packet.type, type); + leave_function(); + return SSH_ERROR; } + leave_function(); + return SSH_OK; } - leave_function(); - return 0; + + if (blocking == 0) { + leave_function(); + return SSH_OK; + } + } while(1); + + leave_function(); + return SSH_OK; } #endif /* HAVE_SSH1 */ -static int packet_wait2(SSH_SESSION *session,int type,int blocking){ - int ret; - enter_function(); - while(1){ - ret=packet_read2(session); - if(ret != SSH_OK){ - leave_function(); - return ret; - } - if (packet_translate(session) != SSH_OK) { - leave_function(); - return SSH_ERROR; - } - switch(session->in_packet.type){ - case SSH2_MSG_DISCONNECT: - packet_parse(session); - ssh_log(session, SSH_LOG_PACKET, "received disconnect packet"); - leave_function(); - return SSH_ERROR; - case SSH2_MSG_CHANNEL_WINDOW_ADJUST: - case SSH2_MSG_CHANNEL_DATA: - case SSH2_MSG_CHANNEL_EXTENDED_DATA: - case SSH2_MSG_CHANNEL_REQUEST: - case SSH2_MSG_CHANNEL_EOF: - case SSH2_MSG_CHANNEL_CLOSE: - packet_parse(session); - break; - case SSH2_MSG_IGNORE: - break; - default: - if(type && (type != session->in_packet.type)){ - ssh_set_error(session,SSH_FATAL,"waitpacket(): Received a %d type packet, was waiting for a %d\n",session->in_packet.type,type); - leave_function(); - return SSH_ERROR; - } - leave_function(); - return SSH_OK; - } - if(blocking==0){ - leave_function(); - return SSH_OK; //shouldn't it return SSH_AGAIN here ? + +static int packet_wait2(SSH_SESSION *session, int type, int blocking) { + int rc = SSH_ERROR; + + enter_function(); + do { + rc = packet_read2(session); + if (rc != SSH_OK) { + leave_function(); + return rc; + } + if (packet_translate(session) != SSH_OK) { + leave_function(); + return SSH_ERROR; + } + switch (session->in_packet.type) { + case SSH2_MSG_DISCONNECT: + packet_parse(session); + ssh_log(session, SSH_LOG_PACKET, "received disconnect packet"); + leave_function(); + return SSH_ERROR; + case SSH2_MSG_CHANNEL_WINDOW_ADJUST: + case SSH2_MSG_CHANNEL_DATA: + case SSH2_MSG_CHANNEL_EXTENDED_DATA: + case SSH2_MSG_CHANNEL_REQUEST: + case SSH2_MSG_CHANNEL_EOF: + case SSH2_MSG_CHANNEL_CLOSE: + packet_parse(session); + break; + case SSH2_MSG_IGNORE: + break; + default: + if (type && (type != session->in_packet.type)) { + ssh_set_error(session, SSH_FATAL, + "packet_wait2(): Received a %d type packet, but expected a %d\n", + session->in_packet.type, type); + leave_function(); + return SSH_ERROR; } + leave_function(); + return SSH_OK; } - leave_function(); - return SSH_OK; + if (blocking == 0) { + leave_function(); + return SSH_OK; //shouldn't it return SSH_AGAIN here ? + } + } while(1); + + leave_function(); + return SSH_OK; } -int packet_wait(SSH_SESSION *session, int type, int block){ + +int packet_wait(SSH_SESSION *session, int type, int block) { #ifdef HAVE_SSH1 - if(session->version==1) - return packet_wait1(session,type,block); - else + if (session->version == 1) { + return packet_wait1(session, type, block); + } #endif - return packet_wait2(session,type,block); + return packet_wait2(session, type, block); } + diff --git a/libssh/server.c b/libssh/server.c index 88b8728..358b13e 100644 --- a/libssh/server.c +++ b/libssh/server.c @@ -263,7 +263,7 @@ static int dh_handshake_server(SSH_SESSION *session){ PUBLIC_KEY *pub; PRIVATE_KEY *prv; BUFFER *buf=buffer_new(); - if(packet_wait(session, SSH2_MSG_KEXDH_INIT ,1)) + if (packet_wait(session, SSH2_MSG_KEXDH_INIT, 1) != SSH_OK) return -1; e=buffer_get_ssh_string(session->in_buffer); if(!e){ |