From e721c122d3f453094d4ab27040765974620f15ef Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Fri, 23 Jul 2010 11:01:06 +0200 Subject: Continue removing old flushing code --- libssh/auth.c | 12 +++--- libssh/auth1.c | 4 +- libssh/channels.c | 10 ++--- libssh/channels1.c | 10 ++--- libssh/client.c | 20 +++++----- libssh/kex.c | 4 +- libssh/keyfiles.c | 15 +++++++- libssh/messages.c | 2 +- libssh/packet.c | 18 ++------- libssh/server.c | 4 +- libssh/socket.c | 108 +---------------------------------------------------- 11 files changed, 52 insertions(+), 155 deletions(-) (limited to 'libssh') diff --git a/libssh/auth.c b/libssh/auth.c index 0e88fbd3..6045c123 100644 --- a/libssh/auth.c +++ b/libssh/auth.c @@ -466,7 +466,7 @@ int ssh_userauth_offer_pubkey(ssh_session session, const char *username, ssh_string_free(service); ssh_string_free(algo); session->auth_state=SSH_AUTH_STATE_NONE; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } @@ -601,7 +601,7 @@ int ssh_userauth_pubkey(ssh_session session, const char *username, } ssh_string_free(sign); session->auth_state=SSH_AUTH_STATE_NONE; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } @@ -786,7 +786,7 @@ int ssh_userauth_agent_pubkey(ssh_session session, const char *username, } ssh_string_free(sign); session->auth_state=SSH_AUTH_STATE_NONE; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } @@ -906,7 +906,7 @@ int ssh_userauth_password(ssh_session session, const char *username, ssh_string_burn(pwd); ssh_string_free(pwd); session->auth_state=SSH_AUTH_STATE_NONE; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } @@ -1280,7 +1280,7 @@ static int kbdauth_init(ssh_session session, const char *user, ssh_string_free(method); ssh_string_free(sub); session->auth_state=SSH_AUTH_STATE_KBDINT_SENT; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } @@ -1465,7 +1465,7 @@ static int kbdauth_send(ssh_session session) { session->auth_state=SSH_AUTH_STATE_KBDINT_SENT; kbdint_free(session->kbdint); session->kbdint = NULL; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } diff --git a/libssh/auth1.c b/libssh/auth1.c index 100e0c8b..06f05497 100644 --- a/libssh/auth1.c +++ b/libssh/auth1.c @@ -101,7 +101,7 @@ static int send_username(ssh_session session, const char *username) { } ssh_string_free(user); session->auth_state=SSH_AUTH_STATE_NONE; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { return SSH_AUTH_ERROR; } @@ -193,7 +193,7 @@ int ssh_userauth1_password(ssh_session session, const char *username, ssh_string_burn(pwd); ssh_string_free(pwd); session->auth_state=SSH_AUTH_STATE_NONE; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return SSH_AUTH_ERROR; } diff --git a/libssh/channels.c b/libssh/channels.c index e372b41d..24cec4c6 100644 --- a/libssh/channels.c +++ b/libssh/channels.c @@ -263,7 +263,7 @@ static int channel_open(ssh_channel channel, const char *type_c, int window, } } - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return err; } @@ -314,7 +314,7 @@ static int grow_window(ssh_session session, ssh_channel channel, int minimumsize goto error; } - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { /* FIXME should we fail here or not? */ leave_function(); return 1; @@ -979,7 +979,7 @@ int channel_write_common(ssh_channel channel, const void *data, goto error; } - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return SSH_ERROR; } @@ -1176,7 +1176,7 @@ static int channel_request(ssh_channel channel, const char *request, } } channel->request_state = SSH_CHANNEL_REQ_STATE_PENDING; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } @@ -1632,7 +1632,7 @@ static int global_request(ssh_session session, const char *request, } } session->global_req_state = SSH_CHANNEL_REQ_STATE_PENDING; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } diff --git a/libssh/channels1.c b/libssh/channels1.c index 293e5fde..4cf7e778 100644 --- a/libssh/channels1.c +++ b/libssh/channels1.c @@ -111,7 +111,7 @@ int channel_request_pty_size1(ssh_channel channel, const char *terminal, int col ssh_log(session, SSH_LOG_FUNCTIONS, "Opening a ssh1 pty"); - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { return -1; } switch(channel->request_state){ @@ -149,7 +149,7 @@ int channel_change_pty_size1(ssh_channel channel, int cols, int rows) { return SSH_ERROR; } channel->request_state=SSH_CHANNEL_REQ_STATE_PENDING; - if (packet_send(session)) { + if (packet_send(session) == SSH_ERROR) { return SSH_ERROR; } @@ -185,7 +185,7 @@ int channel_request_shell1(ssh_channel channel) { return -1; } - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { return -1; } @@ -210,7 +210,7 @@ int channel_request_exec1(ssh_channel channel, const char *cmd) { } ssh_string_free(command); - if(packet_send(session) != SSH_OK) { + if(packet_send(session) == SSH_ERROR) { return -1; } @@ -286,7 +286,7 @@ int channel_write1(ssh_channel channel, const void *data, int len) { ptr += effectivelen; len -= effectivelen; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { return -1; } } diff --git a/libssh/client.c b/libssh/client.c index a7d803b8..42c896ac 100644 --- a/libssh/client.c +++ b/libssh/client.c @@ -204,6 +204,7 @@ static int ssh_analyze_banner(ssh_session session, int *ssh1, int *ssh2) { int ssh_send_banner(ssh_session session, int server) { const char *banner = NULL; char buffer[128] = {0}; + int err=SSH_ERROR; enter_function(); @@ -216,30 +217,31 @@ int ssh_send_banner(ssh_session session, int server) { if (server) { session->serverbanner = strdup(banner); if (session->serverbanner == NULL) { - leave_function(); - return -1; + goto end; } } else { session->clientbanner = strdup(banner); if (session->clientbanner == NULL) { - leave_function(); - return -1; + goto end; } } snprintf(buffer, 128, "%s\n", banner); if (ssh_socket_write(session->socket, buffer, strlen(buffer)) == SSH_ERROR) { - leave_function(); - return -1; + goto end; + } + if (ssh_socket_nonblocking_flush(session->socket) == SSH_ERROR){ + goto end; } - #ifdef WITH_PCAP if(session->pcap_ctx) ssh_pcap_context_write(session->pcap_ctx,SSH_PCAP_DIR_OUT,buffer,strlen(buffer),strlen(buffer)); #endif + err=SSH_OK; +end: leave_function(); - return 0; + return err; } @@ -510,7 +512,7 @@ int ssh_service_request(ssh_session session, const char *service) { } ssh_string_free(service_s); - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { ssh_set_error(session, SSH_FATAL, "Sending SSH2_MSG_SERVICE_REQUEST failed."); break; diff --git a/libssh/kex.c b/libssh/kex.c index a137a594..c2ca7f26 100644 --- a/libssh/kex.c +++ b/libssh/kex.c @@ -428,7 +428,7 @@ int ssh_send_kex(ssh_session session, int server_kex) { goto error; } - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return -1; } @@ -764,7 +764,7 @@ SSH_PACKET_CALLBACK(ssh_packet_publickey1){ goto error; } session->session_state=SSH_SESSION_STATE_KEXINIT_RECEIVED; - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { goto error; } diff --git a/libssh/keyfiles.c b/libssh/keyfiles.c index df1e24f5..f715d25a 100644 --- a/libssh/keyfiles.c +++ b/libssh/keyfiles.c @@ -1700,7 +1700,7 @@ int ssh_is_server_known(ssh_session session) { * @return SSH_OK on success, SSH_ERROR on error. */ int ssh_write_knownhost(ssh_session session) { - ssh_string pubkey = session->current_crypto->server_pubkey; + ssh_string pubkey; unsigned char *pubkey_64; char buffer[4096] = {0}; FILE *file; @@ -1727,10 +1727,21 @@ int ssh_write_knownhost(ssh_session session) { if (session->knownhosts == NULL) { if (ssh_options_apply(session) < 0) { ssh_set_error(session, SSH_FATAL, "Can't find a known_hosts file"); - return -1; + return SSH_ERROR; } } + if(session->current_crypto==NULL) { + ssh_set_error(session, SSH_FATAL, "No current crypto context"); + return SSH_ERROR; + } + + pubkey = session->current_crypto->server_pubkey; + if(pubkey == NULL){ + ssh_set_error(session, SSH_FATAL, "No public key present"); + return SSH_ERROR; + } + /* Check if ~/.ssh exists and create it if not */ dir = ssh_dirname(session->knownhosts); if (dir == NULL) { diff --git a/libssh/messages.c b/libssh/messages.c index 0964c9dc..ef925cd5 100644 --- a/libssh/messages.c +++ b/libssh/messages.c @@ -494,7 +494,7 @@ ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg) { ssh_log(session, SSH_LOG_PACKET, "Accepting a channel request_open for chan %d", chan->remote_channel); - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { goto error; } diff --git a/libssh/packet.c b/libssh/packet.c index ca8db178..a97db93b 100644 --- a/libssh/packet.c +++ b/libssh/packet.c @@ -421,20 +421,6 @@ int ssh_packet_parse_type(ssh_session session) { return SSH_OK; } -/* - * Write the the bufferized output. If the session is blocking, or - * enforce_blocking is set, the call may block. Otherwise, it won't block. - * Return SSH_OK if everything has been sent, SSH_AGAIN if there are still - * things to send on buffer, SSH_ERROR if there is an error. - */ -int packet_flush(ssh_session session, int enforce_blocking) { - if (enforce_blocking || session->blocking) { - return ssh_socket_blocking_flush(session->socket); - } - - return ssh_socket_nonblocking_flush(session->socket); -} - /* * This function places the outgoing packet buffer into an outgoing * socket buffer @@ -447,7 +433,9 @@ static int ssh_packet_write(ssh_session session) { rc=ssh_socket_write(session->socket, ssh_buffer_get_begin(session->out_buffer), ssh_buffer_get_len(session->out_buffer)); - + if(rc == SSH_OK){ + rc=ssh_socket_nonblocking_flush(session->socket); + } leave_function(); return rc; } diff --git a/libssh/server.c b/libssh/server.c index 3db9132b..d62e3b3f 100644 --- a/libssh/server.c +++ b/libssh/server.c @@ -477,7 +477,7 @@ static int dh_handshake_server(ssh_session session) { } ssh_string_free(f); ssh_string_free(sign); - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { return -1; } @@ -486,7 +486,7 @@ static int dh_handshake_server(ssh_session session) { return -1; } - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { return -1; } ssh_log(session, SSH_LOG_PACKET, "SSH_MSG_NEWKEYS sent"); diff --git a/libssh/socket.c b/libssh/socket.c index 8c76dd1f..d8e3ac12 100644 --- a/libssh/socket.c +++ b/libssh/socket.c @@ -463,6 +463,7 @@ static int ssh_socket_unbuffered_write(ssh_socket s, const void *buffer, s->data_to_write = 0; /* Reactive the POLLOUT detector in the poll multiplexer system */ if(s->poll_out){ + ssh_log(s->session, SSH_LOG_PACKET, "Enabling POLLOUT for socket"); ssh_poll_set_events(s->poll_out,ssh_poll_get_events(s->poll_out) | POLLOUT); } if (w < 0) { @@ -502,63 +503,6 @@ void ssh_socket_fd_set(ssh_socket s, fd_set *set, socket_t *max_fd) { } } -/** \internal - * \brief reads blocking until len bytes have been read - */ -//int ssh_socket_completeread(ssh_socket s, void *buffer, uint32_t len) { -// int r = -1; -// uint32_t total = 0; -// uint32_t toread = len; -// if(! ssh_socket_is_open(s)) { -// return SSH_ERROR; -// } -// -// while((r = ssh_socket_unbuffered_read(s, ((uint8_t*)buffer + total), toread))) { -// if (r < 0) { -// return SSH_ERROR; -// } -// total += r; -// toread -= r; -// if (total == len) { -// return len; -// } -// if (r == 0) { -// return 0; -// } -// } -// -// /* connection closed */ -// return total; -//} -// -///** \internal -// * \brief Blocking write of len bytes -// */ -//int ssh_socket_completewrite(ssh_socket s, const void *buffer, uint32_t len) { -// ssh_session session = s->session; -// int written = -1; -// -// enter_function(); -// -// if(! ssh_socket_is_open(s)) { -// leave_function(); -// return SSH_ERROR; -// } -// -// while (len >0) { -// written = ssh_socket_unbuffered_write(s, buffer, len); -// if (written == 0 || written == -1) { -// leave_function(); -// return SSH_ERROR; -// } -// len -= written; -// buffer = ((uint8_t*)buffer + written); -// } -// -// leave_function(); -// return SSH_OK; -//} - /** \internal * \brief buffered write of data * \returns SSH_OK, or SSH_ERROR @@ -571,7 +515,7 @@ int ssh_socket_write(ssh_socket s, const void *buffer, int len) { return SSH_ERROR; } leave_function(); - return len; + return SSH_OK; } @@ -626,54 +570,6 @@ int ssh_socket_nonblocking_flush(ssh_socket s) { return SSH_OK; } - -/** \internal - * \brief locking flush of the output packet buffer - */ -int ssh_socket_blocking_flush(ssh_socket s) { - ssh_session session = s->session; - - enter_function(); - - if (!ssh_socket_is_open(s)) { - session->alive = 0; - - leave_function(); - return SSH_ERROR; - } - - if (s->data_except) { - leave_function(); - return SSH_ERROR; - } - - if (buffer_get_rest_len(s->out_buffer) == 0) { - leave_function(); - return SSH_OK; - } - - if (ssh_socket_completewrite(s, buffer_get_rest(s->out_buffer), - buffer_get_rest_len(s->out_buffer)) != SSH_OK) { - session->alive = 0; - ssh_socket_close(s); - /* FIXME use the proper errno */ - ssh_set_error(session, SSH_FATAL, - "Writing packet: error on socket (or connection closed): %s", - strerror(errno)); - - leave_function(); - return SSH_ERROR; - } - - if (buffer_reinit(s->out_buffer) < 0) { - leave_function(); - return SSH_ERROR; - } - - leave_function(); - return SSH_OK; // no data pending -} - void ssh_socket_set_towrite(ssh_socket s) { s->data_to_write = 1; } -- cgit v1.2.3