From cee85233149eb16c45132170d3f067496f17c368 Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Fri, 6 Mar 2009 00:58:22 +1100 Subject: - djm@cvs.openbsd.org 2009/03/05 07:18:19 [auth2-jpake.c jpake.c jpake.h monitor_wrap.c monitor_wrap.h schnorr.c] [sshconnect2.c] refactor the (disabled) Schnorr proof code to make it a little more generally useful --- monitor_wrap.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'monitor_wrap.c') diff --git a/monitor_wrap.c b/monitor_wrap.c index 0986fc518..db3251b93 100644 --- a/monitor_wrap.c +++ b/monitor_wrap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor_wrap.c,v 1.64 2008/11/04 08:22:13 djm Exp $ */ +/* $OpenBSD: monitor_wrap.c,v 1.65 2009/03/05 07:18:19 djm Exp $ */ /* * Copyright 2002 Niels Provos * Copyright 2002 Markus Friedl @@ -71,6 +71,7 @@ #include "atomicio.h" #include "monitor_fdpass.h" #include "misc.h" +#include "schnorr.h" #include "jpake.h" #include "channels.h" @@ -1282,7 +1283,7 @@ mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s, } void -mm_jpake_step1(struct jpake_group *grp, +mm_jpake_step1(struct modp_group *grp, u_char **id, u_int *id_len, BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2, u_char **priv1_proof, u_int *priv1_proof_len, @@ -1317,7 +1318,7 @@ mm_jpake_step1(struct jpake_group *grp, } void -mm_jpake_step2(struct jpake_group *grp, BIGNUM *s, +mm_jpake_step2(struct modp_group *grp, BIGNUM *s, BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2, const u_char *theirid, u_int theirid_len, const u_char *myid, u_int myid_len, @@ -1357,7 +1358,7 @@ mm_jpake_step2(struct jpake_group *grp, BIGNUM *s, } void -mm_jpake_key_confirm(struct jpake_group *grp, BIGNUM *s, BIGNUM *step2_val, +mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val, BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2, BIGNUM *theirpub1, BIGNUM *theirpub2, const u_char *my_id, u_int my_id_len, -- cgit v1.2.3 From f7288d77e4e705cbbc12c2ad55ed50f9de9a87e2 Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Sun, 21 Jun 2009 18:12:20 +1000 Subject: - andreas@cvs.openbsd.org 2009/05/27 06:31:25 [canohost.h canohost.c] Add clear_cached_addr(), needed for upcoming changes allowing the peer address to change. ok markus@ --- ChangeLog | 11 + channels.c | 4 +- clientloop.c | 6 +- monitor.c | 12 +- monitor_wrap.c | 17 +- packet.c | 871 ++++++++++++++++++++++++++++++++------------------------- packet.h | 12 +- serverloop.c | 6 +- 8 files changed, 528 insertions(+), 411 deletions(-) (limited to 'monitor_wrap.c') diff --git a/ChangeLog b/ChangeLog index f2e6e8ccb..bd1367e7c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -51,6 +51,17 @@ [sshd_config.5] clarify we cd to user's home after chroot; ok markus@ on earlier version; tweaks and ok jmc@ + - andreas@cvs.openbsd.org 2009/05/25 06:48:01 + [channels.c packet.c clientloop.c packet.h serverloop.c monitor_wrap.c + monitor.c] + Put the globals in packet.c into a struct and don't access it directly + from other files. No functional changes. + ok markus@ djm@ + - andreas@cvs.openbsd.org 2009/05/27 06:31:25 + [canohost.h canohost.c] + Add clear_cached_addr(), needed for upcoming changes allowing the peer + address to change. + ok markus@ 20090616 - (dtucker) [configure.ac defines.h] Bug #1607: handle the case where fsid_t diff --git a/channels.c b/channels.c index dea60ba24..efb04d655 100644 --- a/channels.c +++ b/channels.c @@ -1,4 +1,4 @@ -/* $OpenBSD: channels.c,v 1.295 2009/02/12 03:00:56 djm Exp $ */ +/* $OpenBSD: channels.c,v 1.296 2009/05/25 06:48:00 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -2431,7 +2431,7 @@ channel_input_status_confirm(int type, u_int32_t seq, void *ctxt) int id; /* Reset keepalive timeout */ - keep_alive_timeouts = 0; + packet_set_alive_timeouts(0); id = packet_get_int(); packet_check_eom(); diff --git a/clientloop.c b/clientloop.c index a2d2d1d07..2cb8c3a49 100644 --- a/clientloop.c +++ b/clientloop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: clientloop.c,v 1.209 2009/02/12 03:00:56 djm Exp $ */ +/* $OpenBSD: clientloop.c,v 1.210 2009/05/25 06:48:01 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -491,13 +491,13 @@ client_global_request_reply(int type, u_int32_t seq, void *ctxt) xfree(gc); } - keep_alive_timeouts = 0; + packet_set_alive_timeouts(0); } static void server_alive_check(void) { - if (++keep_alive_timeouts > options.server_alive_count_max) { + if (packet_inc_alive_timeouts() > options.server_alive_count_max) { logit("Timeout, server not responding."); cleanup_exit(255); } diff --git a/monitor.c b/monitor.c index f57e74ba5..61242e6d0 100644 --- a/monitor.c +++ b/monitor.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor.c,v 1.101 2009/02/12 03:26:22 djm Exp $ */ +/* $OpenBSD: monitor.c,v 1.102 2009/05/25 06:48:01 andreas Exp $ */ /* * Copyright 2002 Niels Provos * Copyright 2002 Markus Friedl @@ -100,7 +100,6 @@ extern Newkeys *current_keys[]; extern z_stream incoming_stream; extern z_stream outgoing_stream; extern u_char session_id[]; -extern Buffer input, output; extern Buffer auth_debug; extern int auth_debug_init; extern Buffer loginmsg; @@ -1670,13 +1669,14 @@ monitor_apply_keystate(struct monitor *pmonitor) /* Network I/O buffers */ /* XXX inefficient for large buffers, need: buffer_init_from_string */ - buffer_clear(&input); - buffer_append(&input, child_state.input, child_state.ilen); + buffer_clear(packet_get_input()); + buffer_append(packet_get_input(), child_state.input, child_state.ilen); memset(child_state.input, 0, child_state.ilen); xfree(child_state.input); - buffer_clear(&output); - buffer_append(&output, child_state.output, child_state.olen); + buffer_clear(packet_get_output()); + buffer_append(packet_get_output(), child_state.output, + child_state.olen); memset(child_state.output, 0, child_state.olen); xfree(child_state.output); } diff --git a/monitor_wrap.c b/monitor_wrap.c index db3251b93..d71d4a8c5 100644 --- a/monitor_wrap.c +++ b/monitor_wrap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor_wrap.c,v 1.65 2009/03/05 07:18:19 djm Exp $ */ +/* $OpenBSD: monitor_wrap.c,v 1.66 2009/05/25 06:48:01 andreas Exp $ */ /* * Copyright 2002 Niels Provos * Copyright 2002 Markus Friedl @@ -80,11 +80,9 @@ /* Imports */ extern int compat20; -extern Newkeys *newkeys[]; extern z_stream incoming_stream; extern z_stream outgoing_stream; extern struct monitor *pmonitor; -extern Buffer input, output; extern Buffer loginmsg; extern ServerOptions options; @@ -509,7 +507,7 @@ mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) Enc *enc; Mac *mac; Comp *comp; - Newkeys *newkey = newkeys[mode]; + Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode); debug3("%s: converting %p", __func__, newkey); @@ -571,7 +569,7 @@ mm_send_kex(Buffer *m, Kex *kex) void mm_send_keystate(struct monitor *monitor) { - Buffer m; + Buffer m, *input, *output; u_char *blob, *p; u_int bloblen, plen; u_int32_t seqnr, packets; @@ -609,7 +607,8 @@ mm_send_keystate(struct monitor *monitor) } debug3("%s: Sending new keys: %p %p", - __func__, newkeys[MODE_OUT], newkeys[MODE_IN]); + __func__, packet_get_newkeys(MODE_OUT), + packet_get_newkeys(MODE_IN)); /* Keys from Kex */ if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) @@ -656,8 +655,10 @@ mm_send_keystate(struct monitor *monitor) buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); /* Network I/O buffers */ - buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input)); - buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output)); + input = (Buffer *)packet_get_input(); + output = (Buffer *)packet_get_output(); + buffer_put_string(&m, buffer_ptr(input), buffer_len(input)); + buffer_put_string(&m, buffer_ptr(output), buffer_len(output)); mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); debug3("%s: Finished sending state", __func__); diff --git a/packet.c b/packet.c index 5afc84ce0..fdc648888 100644 --- a/packet.c +++ b/packet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.160 2009/02/13 11:50:21 markus Exp $ */ +/* $OpenBSD: packet.c,v 1.161 2009/05/25 06:48:01 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -86,92 +86,117 @@ #define PACKET_MAX_SIZE (256 * 1024) -/* - * This variable contains the file descriptors used for communicating with - * the other side. connection_in is used for reading; connection_out for - * writing. These can be the same descriptor, in which case it is assumed to - * be a socket. - */ -static int connection_in = -1; -static int connection_out = -1; +struct packet_state { + u_int32_t seqnr; + u_int32_t packets; + u_int64_t blocks; + u_int64_t bytes; +}; -/* Protocol flags for the remote side. */ -static u_int remote_protocol_flags = 0; +struct packet { + TAILQ_ENTRY(packet) next; + u_char type; + Buffer payload; +}; -/* Encryption context for receiving data. This is only used for decryption. */ -static CipherContext receive_context; +struct session_state { + /* + * This variable contains the file descriptors used for + * communicating with the other side. connection_in is used for + * reading; connection_out for writing. These can be the same + * descriptor, in which case it is assumed to be a socket. + */ + int connection_in; + int connection_out; -/* Encryption context for sending data. This is only used for encryption. */ -static CipherContext send_context; + /* Protocol flags for the remote side. */ + u_int remote_protocol_flags; -/* Buffer for raw input data from the socket. */ -Buffer input; + /* Encryption context for receiving data. Only used for decryption. */ + CipherContext receive_context; -/* Buffer for raw output data going to the socket. */ -Buffer output; + /* Encryption context for sending data. Only used for encryption. */ + CipherContext send_context; -/* Buffer for the partial outgoing packet being constructed. */ -static Buffer outgoing_packet; + /* Buffer for raw input data from the socket. */ + Buffer input; -/* Buffer for the incoming packet currently being processed. */ -static Buffer incoming_packet; + /* Buffer for raw output data going to the socket. */ + Buffer output; -/* Scratch buffer for packet compression/decompression. */ -static Buffer compression_buffer; -static int compression_buffer_ready = 0; + /* Buffer for the partial outgoing packet being constructed. */ + Buffer outgoing_packet; -/* Flag indicating whether packet compression/decompression is enabled. */ -static int packet_compression = 0; + /* Buffer for the incoming packet currently being processed. */ + Buffer incoming_packet; -/* default maximum packet size */ -u_int max_packet_size = 32768; + /* Scratch buffer for packet compression/decompression. */ + Buffer compression_buffer; + int compression_buffer_ready; -/* Flag indicating whether this module has been initialized. */ -static int initialized = 0; + /* + * Flag indicating whether packet compression/decompression is + * enabled. + */ + int packet_compression; -/* Set to true if the connection is interactive. */ -static int interactive_mode = 0; + /* default maximum packet size */ + u_int max_packet_size; -/* Set to true if we are the server side. */ -static int server_side = 0; + /* Flag indicating whether this module has been initialized. */ + int initialized; -/* Set to true if we are authenticated. */ -static int after_authentication = 0; + /* Set to true if the connection is interactive. */ + int interactive_mode; -int keep_alive_timeouts = 0; + /* Set to true if we are the server side. */ + int server_side; -/* Set to the maximum time that we will wait to send or receive a packet */ -static int packet_timeout_ms = -1; + /* Set to true if we are authenticated. */ + int after_authentication; -/* Session key information for Encryption and MAC */ -Newkeys *newkeys[MODE_MAX]; -static struct packet_state { - u_int32_t seqnr; - u_int32_t packets; - u_int64_t blocks; - u_int64_t bytes; -} p_read, p_send; + int keep_alive_timeouts; -static u_int64_t max_blocks_in, max_blocks_out; -static u_int32_t rekey_limit; + /* The maximum time that we will wait to send or receive a packet */ + int packet_timeout_ms; -/* Session key for protocol v1 */ -static u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; -static u_int ssh1_keylen; + /* Session key information for Encryption and MAC */ + Newkeys *newkeys[MODE_MAX]; + struct packet_state p_read, p_send; -/* roundup current message to extra_pad bytes */ -static u_char extra_pad = 0; + u_int64_t max_blocks_in, max_blocks_out; + u_int32_t rekey_limit; -/* XXX discard incoming data after MAC error */ -static u_int packet_discard = 0; -static Mac *packet_discard_mac = NULL; + /* Session key for protocol v1 */ + u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; + u_int ssh1_keylen; -struct packet { - TAILQ_ENTRY(packet) next; - u_char type; - Buffer payload; + /* roundup current message to extra_pad bytes */ + u_char extra_pad; + + /* XXX discard incoming data after MAC error */ + u_int packet_discard; + Mac *packet_discard_mac; + + /* Used in packet_read_poll2() */ + u_int packlen; + + TAILQ_HEAD(, packet) outgoing; }; -TAILQ_HEAD(, packet) outgoing; + +static struct session_state *active_state; + +static struct session_state * +alloc_session_state() +{ + struct session_state *s = xcalloc(1, sizeof(*s)); + + s->connection_in = -1; + s->connection_out = -1; + s->max_packet_size = 32768; + s->packet_timeout_ms = -1; + return s; +} /* * Sets the descriptors used for communication. Disables encryption until @@ -184,21 +209,23 @@ packet_set_connection(int fd_in, int fd_out) if (none == NULL) fatal("packet_set_connection: cannot load cipher 'none'"); - connection_in = fd_in; - connection_out = fd_out; - cipher_init(&send_context, none, (const u_char *)"", + if (active_state == NULL) + active_state = alloc_session_state(); + active_state->connection_in = fd_in; + active_state->connection_out = fd_out; + cipher_init(&active_state->send_context, none, (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT); - cipher_init(&receive_context, none, (const u_char *)"", + cipher_init(&active_state->receive_context, none, (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT); - newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL; - if (!initialized) { - initialized = 1; - buffer_init(&input); - buffer_init(&output); - buffer_init(&outgoing_packet); - buffer_init(&incoming_packet); - TAILQ_INIT(&outgoing); - p_send.packets = p_read.packets = 0; + active_state->newkeys[MODE_IN] = active_state->newkeys[MODE_OUT] = NULL; + if (!active_state->initialized) { + active_state->initialized = 1; + buffer_init(&active_state->input); + buffer_init(&active_state->output); + buffer_init(&active_state->outgoing_packet); + buffer_init(&active_state->incoming_packet); + TAILQ_INIT(&active_state->outgoing); + active_state->p_send.packets = active_state->p_read.packets = 0; } } @@ -206,27 +233,29 @@ void packet_set_timeout(int timeout, int count) { if (timeout == 0 || count == 0) { - packet_timeout_ms = -1; + active_state->packet_timeout_ms = -1; return; } if ((INT_MAX / 1000) / count < timeout) - packet_timeout_ms = INT_MAX; + active_state->packet_timeout_ms = INT_MAX; else - packet_timeout_ms = timeout * count * 1000; + active_state->packet_timeout_ms = timeout * count * 1000; } static void packet_stop_discard(void) { - if (packet_discard_mac) { + if (active_state->packet_discard_mac) { char buf[1024]; memset(buf, 'a', sizeof(buf)); - while (buffer_len(&incoming_packet) < PACKET_MAX_SIZE) - buffer_append(&incoming_packet, buf, sizeof(buf)); - (void) mac_compute(packet_discard_mac, - p_read.seqnr, - buffer_ptr(&incoming_packet), + while (buffer_len(&active_state->incoming_packet) < + PACKET_MAX_SIZE) + buffer_append(&active_state->incoming_packet, buf, + sizeof(buf)); + (void) mac_compute(active_state->packet_discard_mac, + active_state->p_read.seqnr, + buffer_ptr(&active_state->incoming_packet), PACKET_MAX_SIZE); } logit("Finished discarding for %.200s", get_remote_ipaddr()); @@ -239,10 +268,11 @@ packet_start_discard(Enc *enc, Mac *mac, u_int packet_length, u_int discard) if (enc == NULL || !cipher_is_cbc(enc->cipher)) packet_disconnect("Packet corrupt"); if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled) - packet_discard_mac = mac; - if (buffer_len(&input) >= discard) + active_state->packet_discard_mac = mac; + if (buffer_len(&active_state->input) >= discard) packet_stop_discard(); - packet_discard = discard - buffer_len(&input); + active_state->packet_discard = discard - + buffer_len(&active_state->input); } /* Returns 1 if remote host is connected via socket, 0 if not. */ @@ -254,15 +284,17 @@ packet_connection_is_on_socket(void) socklen_t fromlen, tolen; /* filedescriptors in and out are the same, so it's a socket */ - if (connection_in == connection_out) + if (active_state->connection_in == active_state->connection_out) return 1; fromlen = sizeof(from); memset(&from, 0, sizeof(from)); - if (getpeername(connection_in, (struct sockaddr *)&from, &fromlen) < 0) + if (getpeername(active_state->connection_in, (struct sockaddr *)&from, + &fromlen) < 0) return 0; tolen = sizeof(to); memset(&to, 0, sizeof(to)); - if (getpeername(connection_out, (struct sockaddr *)&to, &tolen) < 0) + if (getpeername(active_state->connection_out, (struct sockaddr *)&to, + &tolen) < 0) return 0; if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) return 0; @@ -283,9 +315,9 @@ packet_get_keyiv(int mode, u_char *iv, u_int len) CipherContext *cc; if (mode == MODE_OUT) - cc = &send_context; + cc = &active_state->send_context; else - cc = &receive_context; + cc = &active_state->receive_context; cipher_get_keyiv(cc, iv, len); } @@ -296,9 +328,9 @@ packet_get_keycontext(int mode, u_char *dat) CipherContext *cc; if (mode == MODE_OUT) - cc = &send_context; + cc = &active_state->send_context; else - cc = &receive_context; + cc = &active_state->receive_context; return (cipher_get_keycontext(cc, dat)); } @@ -309,9 +341,9 @@ packet_set_keycontext(int mode, u_char *dat) CipherContext *cc; if (mode == MODE_OUT) - cc = &send_context; + cc = &active_state->send_context; else - cc = &receive_context; + cc = &active_state->receive_context; cipher_set_keycontext(cc, dat); } @@ -322,9 +354,9 @@ packet_get_keyiv_len(int mode) CipherContext *cc; if (mode == MODE_OUT) - cc = &send_context; + cc = &active_state->send_context; else - cc = &receive_context; + cc = &active_state->receive_context; return (cipher_get_keyiv_len(cc)); } @@ -335,9 +367,9 @@ packet_set_iv(int mode, u_char *dat) CipherContext *cc; if (mode == MODE_OUT) - cc = &send_context; + cc = &active_state->send_context; else - cc = &receive_context; + cc = &active_state->receive_context; cipher_set_keyiv(cc, dat); } @@ -345,7 +377,7 @@ packet_set_iv(int mode, u_char *dat) int packet_get_ssh1_cipher(void) { - return (cipher_get_number(receive_context.cipher)); + return (cipher_get_number(active_state->receive_context.cipher)); } void @@ -354,7 +386,8 @@ packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packe { struct packet_state *state; - state = (mode == MODE_IN) ? &p_read : &p_send; + state = (mode == MODE_IN) ? + &active_state->p_read : &active_state->p_send; if (seqnr) *seqnr = state->seqnr; if (blocks) @@ -371,7 +404,8 @@ packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets, { struct packet_state *state; - state = (mode == MODE_IN) ? &p_read : &p_send; + state = (mode == MODE_IN) ? + &active_state->p_read : &active_state->p_send; state->seqnr = seqnr; state->blocks = blocks; state->packets = packets; @@ -387,7 +421,8 @@ packet_connection_is_ipv4(void) socklen_t tolen = sizeof(to); memset(&to, 0, sizeof(to)); - if (getsockname(connection_out, (struct sockaddr *)&to, &tolen) < 0) + if (getsockname(active_state->connection_out, (struct sockaddr *)&to, + &tolen) < 0) return 0; if (to.ss_family == AF_INET) return 1; @@ -405,10 +440,10 @@ void packet_set_nonblocking(void) { /* Set the socket into non-blocking mode. */ - set_nonblock(connection_in); + set_nonblock(active_state->connection_in); - if (connection_out != connection_in) - set_nonblock(connection_out); + if (active_state->connection_out != active_state->connection_in) + set_nonblock(active_state->connection_out); } /* Returns the socket used for reading. */ @@ -416,7 +451,7 @@ packet_set_nonblocking(void) int packet_get_connection_in(void) { - return connection_in; + return active_state->connection_in; } /* Returns the descriptor used for writing. */ @@ -424,7 +459,7 @@ packet_get_connection_in(void) int packet_get_connection_out(void) { - return connection_out; + return active_state->connection_out; } /* Closes the connection and clears and frees internal data structures. */ @@ -432,26 +467,26 @@ packet_get_connection_out(void) void packet_close(void) { - if (!initialized) + if (!active_state->initialized) return; - initialized = 0; - if (connection_in == connection_out) { - shutdown(connection_out, SHUT_RDWR); - close(connection_out); + active_state->initialized = 0; + if (active_state->connection_in == active_state->connection_out) { + shutdown(active_state->connection_out, SHUT_RDWR); + close(active_state->connection_out); } else { - close(connection_in); - close(connection_out); + close(active_state->connection_in); + close(active_state->connection_out); } - buffer_free(&input); - buffer_free(&output); - buffer_free(&outgoing_packet); - buffer_free(&incoming_packet); - if (compression_buffer_ready) { - buffer_free(&compression_buffer); + buffer_free(&active_state->input); + buffer_free(&active_state->output); + buffer_free(&active_state->outgoing_packet); + buffer_free(&active_state->incoming_packet); + if (active_state->compression_buffer_ready) { + buffer_free(&active_state->compression_buffer); buffer_compress_uninit(); } - cipher_cleanup(&send_context); - cipher_cleanup(&receive_context); + cipher_cleanup(&active_state->send_context); + cipher_cleanup(&active_state->receive_context); } /* Sets remote side protocol flags. */ @@ -459,7 +494,7 @@ packet_close(void) void packet_set_protocol_flags(u_int protocol_flags) { - remote_protocol_flags = protocol_flags; + active_state->remote_protocol_flags = protocol_flags; } /* Returns the remote protocol flags set earlier by the above function. */ @@ -467,7 +502,7 @@ packet_set_protocol_flags(u_int protocol_flags) u_int packet_get_protocol_flags(void) { - return remote_protocol_flags; + return active_state->remote_protocol_flags; } /* @@ -478,18 +513,18 @@ packet_get_protocol_flags(void) static void packet_init_compression(void) { - if (compression_buffer_ready == 1) + if (active_state->compression_buffer_ready == 1) return; - compression_buffer_ready = 1; - buffer_init(&compression_buffer); + active_state->compression_buffer_ready = 1; + buffer_init(&active_state->compression_buffer); } void packet_start_compression(int level) { - if (packet_compression && !compat20) + if (active_state->packet_compression && !compat20) fatal("Compression already enabled."); - packet_compression = 1; + active_state->packet_compression = 1; packet_init_compression(); buffer_compress_init_send(level); buffer_compress_init_recv(); @@ -513,19 +548,21 @@ packet_set_encryption_key(const u_char *key, u_int keylen, fatal("packet_set_encryption_key: keylen too small: %d", keylen); if (keylen > SSH_SESSION_KEY_LENGTH) fatal("packet_set_encryption_key: keylen too big: %d", keylen); - memcpy(ssh1_key, key, keylen); - ssh1_keylen = keylen; - cipher_init(&send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT); - cipher_init(&receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT); + memcpy(active_state->ssh1_key, key, keylen); + active_state->ssh1_keylen = keylen; + cipher_init(&active_state->send_context, cipher, key, keylen, NULL, + 0, CIPHER_ENCRYPT); + cipher_init(&active_state->receive_context, cipher, key, keylen, NULL, + 0, CIPHER_DECRYPT); } u_int packet_get_encryption_key(u_char *key) { if (key == NULL) - return (ssh1_keylen); - memcpy(key, ssh1_key, ssh1_keylen); - return (ssh1_keylen); + return (active_state->ssh1_keylen); + memcpy(key, active_state->ssh1_key, active_state->ssh1_keylen); + return (active_state->ssh1_keylen); } /* Start constructing a packet to send. */ @@ -539,8 +576,8 @@ packet_start(u_char type) len = compat20 ? 6 : 9; memset(buf, 0, len - 1); buf[len - 1] = type; - buffer_clear(&outgoing_packet); - buffer_append(&outgoing_packet, buf, len); + buffer_clear(&active_state->outgoing_packet); + buffer_append(&active_state->outgoing_packet, buf, len); } /* Append payload. */ @@ -549,43 +586,43 @@ packet_put_char(int value) { char ch = value; - buffer_append(&outgoing_packet, &ch, 1); + buffer_append(&active_state->outgoing_packet, &ch, 1); } void packet_put_int(u_int value) { - buffer_put_int(&outgoing_packet, value); + buffer_put_int(&active_state->outgoing_packet, value); } void packet_put_string(const void *buf, u_int len) { - buffer_put_string(&outgoing_packet, buf, len); + buffer_put_string(&active_state->outgoing_packet, buf, len); } void packet_put_cstring(const char *str) { - buffer_put_cstring(&outgoing_packet, str); + buffer_put_cstring(&active_state->outgoing_packet, str); } void packet_put_raw(const void *buf, u_int len) { - buffer_append(&outgoing_packet, buf, len); + buffer_append(&active_state->outgoing_packet, buf, len); } void packet_put_bignum(BIGNUM * value) { - buffer_put_bignum(&outgoing_packet, value); + buffer_put_bignum(&active_state->outgoing_packet, value); } void packet_put_bignum2(BIGNUM * value) { - buffer_put_bignum2(&outgoing_packet, value); + buffer_put_bignum2(&active_state->outgoing_packet, value); } /* @@ -605,24 +642,27 @@ packet_send1(void) * If using packet compression, compress the payload of the outgoing * packet. */ - if (packet_compression) { - buffer_clear(&compression_buffer); + if (active_state->packet_compression) { + buffer_clear(&active_state->compression_buffer); /* Skip padding. */ - buffer_consume(&outgoing_packet, 8); + buffer_consume(&active_state->outgoing_packet, 8); /* padding */ - buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8); - buffer_compress(&outgoing_packet, &compression_buffer); - buffer_clear(&outgoing_packet); - buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer), - buffer_len(&compression_buffer)); + buffer_append(&active_state->compression_buffer, + "\0\0\0\0\0\0\0\0", 8); + buffer_compress(&active_state->outgoing_packet, + &active_state->compression_buffer); + buffer_clear(&active_state->outgoing_packet); + buffer_append(&active_state->outgoing_packet, + buffer_ptr(&active_state->compression_buffer), + buffer_len(&active_state->compression_buffer)); } /* Compute packet length without padding (add checksum, remove padding). */ - len = buffer_len(&outgoing_packet) + 4 - 8; + len = buffer_len(&active_state->outgoing_packet) + 4 - 8; /* Insert padding. Initialized to zero in packet_start1() */ padding = 8 - len % 8; - if (!send_context.plaintext) { - cp = buffer_ptr(&outgoing_packet); + if (!active_state->send_context.plaintext) { + cp = buffer_ptr(&active_state->outgoing_packet); for (i = 0; i < padding; i++) { if (i % 4 == 0) rnd = arc4random(); @@ -630,33 +670,36 @@ packet_send1(void) rnd >>= 8; } } - buffer_consume(&outgoing_packet, 8 - padding); + buffer_consume(&active_state->outgoing_packet, 8 - padding); /* Add check bytes. */ - checksum = ssh_crc32(buffer_ptr(&outgoing_packet), - buffer_len(&outgoing_packet)); + checksum = ssh_crc32(buffer_ptr(&active_state->outgoing_packet), + buffer_len(&active_state->outgoing_packet)); put_u32(buf, checksum); - buffer_append(&outgoing_packet, buf, 4); + buffer_append(&active_state->outgoing_packet, buf, 4); #ifdef PACKET_DEBUG fprintf(stderr, "packet_send plain: "); - buffer_dump(&outgoing_packet); + buffer_dump(&active_state->outgoing_packet); #endif /* Append to output. */ put_u32(buf, len); - buffer_append(&output, buf, 4); - cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); - cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet), - buffer_len(&outgoing_packet)); + buffer_append(&active_state->output, buf, 4); + cp = buffer_append_space(&active_state->output, + buffer_len(&active_state->outgoing_packet)); + cipher_crypt(&active_state->send_context, cp, + buffer_ptr(&active_state->outgoing_packet), + buffer_len(&active_state->outgoing_packet)); #ifdef PACKET_DEBUG fprintf(stderr, "encrypted: "); - buffer_dump(&output); + buffer_dump(&active_state->output); #endif - p_send.packets++; - p_send.bytes += len + buffer_len(&outgoing_packet); - buffer_clear(&outgoing_packet); + active_state->p_send.packets++; + active_state->p_send.bytes += len + + buffer_len(&active_state->outgoing_packet); + buffer_clear(&active_state->outgoing_packet); /* * Note that the packet is now only buffered in output. It won't be @@ -678,22 +721,22 @@ set_newkeys(int mode) debug2("set_newkeys: mode %d", mode); if (mode == MODE_OUT) { - cc = &send_context; + cc = &active_state->send_context; crypt_type = CIPHER_ENCRYPT; - p_send.packets = p_send.blocks = 0; - max_blocks = &max_blocks_out; + active_state->p_send.packets = active_state->p_send.blocks = 0; + max_blocks = &active_state->max_blocks_out; } else { - cc = &receive_context; + cc = &active_state->receive_context; crypt_type = CIPHER_DECRYPT; - p_read.packets = p_read.blocks = 0; - max_blocks = &max_blocks_in; + active_state->p_read.packets = active_state->p_read.blocks = 0; + max_blocks = &active_state->max_blocks_in; } - if (newkeys[mode] != NULL) { + if (active_state->newkeys[mode] != NULL) { debug("set_newkeys: rekeying"); cipher_cleanup(cc); - enc = &newkeys[mode]->enc; - mac = &newkeys[mode]->mac; - comp = &newkeys[mode]->comp; + enc = &active_state->newkeys[mode]->enc; + mac = &active_state->newkeys[mode]->mac; + comp = &active_state->newkeys[mode]->comp; mac_clear(mac); xfree(enc->name); xfree(enc->iv); @@ -701,14 +744,14 @@ set_newkeys(int mode) xfree(mac->name); xfree(mac->key); xfree(comp->name); - xfree(newkeys[mode]); + xfree(active_state->newkeys[mode]); } - newkeys[mode] = kex_get_newkeys(mode); - if (newkeys[mode] == NULL) + active_state->newkeys[mode] = kex_get_newkeys(mode); + if (active_state->newkeys[mode] == NULL) fatal("newkeys: no keys for mode %d", mode); - enc = &newkeys[mode]->enc; - mac = &newkeys[mode]->mac; - comp = &newkeys[mode]->comp; + enc = &active_state->newkeys[mode]->enc; + mac = &active_state->newkeys[mode]->mac; + comp = &active_state->newkeys[mode]->comp; if (mac_init(mac) == 0) mac->enabled = 1; DBG(debug("cipher_init_context: %d", mode)); @@ -719,8 +762,8 @@ set_newkeys(int mode) memset(enc->key, 0, enc->key_len); memset(mac->key, 0, mac->key_len); */ if ((comp->type == COMP_ZLIB || - (comp->type == COMP_DELAYED && after_authentication)) && - comp->enabled == 0) { + (comp->type == COMP_DELAYED && + active_state->after_authentication)) && comp->enabled == 0) { packet_init_compression(); if (mode == MODE_OUT) buffer_compress_init_send(6); @@ -736,8 +779,9 @@ set_newkeys(int mode) *max_blocks = (u_int64_t)1 << (enc->block_size*2); else *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; - if (rekey_limit) - *max_blocks = MIN(*max_blocks, rekey_limit / enc->block_size); + if (active_state->rekey_limit) + *max_blocks = MIN(*max_blocks, + active_state->rekey_limit / enc->block_size); } /* @@ -755,12 +799,12 @@ packet_enable_delayed_compress(void) * Remember that we are past the authentication step, so rekeying * with COMP_DELAYED will turn on compression immediately. */ - after_authentication = 1; + active_state->after_authentication = 1; for (mode = 0; mode < MODE_MAX; mode++) { /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */ - if (newkeys[mode] == NULL) + if (active_state->newkeys[mode] == NULL) continue; - comp = &newkeys[mode]->comp; + comp = &active_state->newkeys[mode]->comp; if (comp && !comp->enabled && comp->type == COMP_DELAYED) { packet_init_compression(); if (mode == MODE_OUT) @@ -788,37 +832,39 @@ packet_send2_wrapped(void) Comp *comp = NULL; int block_size; - if (newkeys[MODE_OUT] != NULL) { - enc = &newkeys[MODE_OUT]->enc; - mac = &newkeys[MODE_OUT]->mac; - comp = &newkeys[MODE_OUT]->comp; + if (active_state->newkeys[MODE_OUT] != NULL) { + enc = &active_state->newkeys[MODE_OUT]->enc; + mac = &active_state->newkeys[MODE_OUT]->mac; + comp = &active_state->newkeys[MODE_OUT]->comp; } block_size = enc ? enc->block_size : 8; - cp = buffer_ptr(&outgoing_packet); + cp = buffer_ptr(&active_state->outgoing_packet); type = cp[5]; #ifdef PACKET_DEBUG fprintf(stderr, "plain: "); - buffer_dump(&outgoing_packet); + buffer_dump(&active_state->outgoing_packet); #endif if (comp && comp->enabled) { - len = buffer_len(&outgoing_packet); + len = buffer_len(&active_state->outgoing_packet); /* skip header, compress only payload */ - buffer_consume(&outgoing_packet, 5); - buffer_clear(&compression_buffer); - buffer_compress(&outgoing_packet, &compression_buffer); - buffer_clear(&outgoing_packet); - buffer_append(&outgoing_packet, "\0\0\0\0\0", 5); - buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer), - buffer_len(&compression_buffer)); + buffer_consume(&active_state->outgoing_packet, 5); + buffer_clear(&active_state->compression_buffer); + buffer_compress(&active_state->outgoing_packet, + &active_state->compression_buffer); + buffer_clear(&active_state->outgoing_packet); + buffer_append(&active_state->outgoing_packet, "\0\0\0\0\0", 5); + buffer_append(&active_state->outgoing_packet, + buffer_ptr(&active_state->compression_buffer), + buffer_len(&active_state->compression_buffer)); DBG(debug("compression: raw %d compressed %d", len, - buffer_len(&outgoing_packet))); + buffer_len(&active_state->outgoing_packet))); } /* sizeof (packet_len + pad_len + payload) */ - len = buffer_len(&outgoing_packet); + len = buffer_len(&active_state->outgoing_packet); /* * calc size of padding, alloc space, get random data, @@ -827,17 +873,19 @@ packet_send2_wrapped(void) padlen = block_size - (len % block_size); if (padlen < 4) padlen += block_size; - if (extra_pad) { + if (active_state->extra_pad) { /* will wrap if extra_pad+padlen > 255 */ - extra_pad = roundup(extra_pad, block_size); - pad = extra_pad - ((len + padlen) % extra_pad); + active_state->extra_pad = + roundup(active_state->extra_pad, block_size); + pad = active_state->extra_pad - + ((len + padlen) % active_state->extra_pad); debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)", - pad, len, padlen, extra_pad); + pad, len, padlen, active_state->extra_pad); padlen += pad; - extra_pad = 0; + active_state->extra_pad = 0; } - cp = buffer_append_space(&outgoing_packet, padlen); - if (enc && !send_context.plaintext) { + cp = buffer_append_space(&active_state->outgoing_packet, padlen); + if (enc && !active_state->send_context.plaintext) { /* random padding */ for (i = 0; i < padlen; i++) { if (i % 4 == 0) @@ -850,43 +898,45 @@ packet_send2_wrapped(void) memset(cp, 0, padlen); } /* packet_length includes payload, padding and padding length field */ - packet_length = buffer_len(&outgoing_packet) - 4; - cp = buffer_ptr(&outgoing_packet); + packet_length = buffer_len(&active_state->outgoing_packet) - 4; + cp = buffer_ptr(&active_state->outgoing_packet); put_u32(cp, packet_length); cp[4] = padlen; DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen)); /* compute MAC over seqnr and packet(length fields, payload, padding) */ if (mac && mac->enabled) { - macbuf = mac_compute(mac, p_send.seqnr, - buffer_ptr(&outgoing_packet), - buffer_len(&outgoing_packet)); - DBG(debug("done calc MAC out #%d", p_send.seqnr)); + macbuf = mac_compute(mac, active_state->p_send.seqnr, + buffer_ptr(&active_state->outgoing_packet), + buffer_len(&active_state->outgoing_packet)); + DBG(debug("done calc MAC out #%d", active_state->p_send.seqnr)); } /* encrypt packet and append to output buffer. */ - cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); - cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet), - buffer_len(&outgoing_packet)); + cp = buffer_append_space(&active_state->output, + buffer_len(&active_state->outgoing_packet)); + cipher_crypt(&active_state->send_context, cp, + buffer_ptr(&active_state->outgoing_packet), + buffer_len(&active_state->outgoing_packet)); /* append unencrypted MAC */ if (mac && mac->enabled) - buffer_append(&output, macbuf, mac->mac_len); + buffer_append(&active_state->output, macbuf, mac->mac_len); #ifdef PACKET_DEBUG fprintf(stderr, "encrypted: "); - buffer_dump(&output); + buffer_dump(&active_state->output); #endif /* increment sequence number for outgoing packets */ - if (++p_send.seqnr == 0) + if (++active_state->p_send.seqnr == 0) logit("outgoing seqnr wraps around"); - if (++p_send.packets == 0) + if (++active_state->p_send.packets == 0) if (!(datafellows & SSH_BUG_NOREKEY)) fatal("XXX too many packets with same key"); - p_send.blocks += (packet_length + 4) / block_size; - p_send.bytes += packet_length + 4; - buffer_clear(&outgoing_packet); + active_state->p_send.blocks += (packet_length + 4) / block_size; + active_state->p_send.bytes += packet_length + 4; + buffer_clear(&active_state->outgoing_packet); if (type == SSH2_MSG_NEWKEYS) set_newkeys(MODE_OUT); - else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side) + else if (type == SSH2_MSG_USERAUTH_SUCCESS && active_state->server_side) packet_enable_delayed_compress(); } @@ -897,7 +947,7 @@ packet_send2(void) struct packet *p; u_char type, *cp; - cp = buffer_ptr(&outgoing_packet); + cp = buffer_ptr(&active_state->outgoing_packet); type = cp[5]; /* during rekeying we can only send key exchange messages */ @@ -907,9 +957,10 @@ packet_send2(void) debug("enqueue packet: %u", type); p = xmalloc(sizeof(*p)); p->type = type; - memcpy(&p->payload, &outgoing_packet, sizeof(Buffer)); - buffer_init(&outgoing_packet); - TAILQ_INSERT_TAIL(&outgoing, p, next); + memcpy(&p->payload, &active_state->outgoing_packet, + sizeof(Buffer)); + buffer_init(&active_state->outgoing_packet); + TAILQ_INSERT_TAIL(&active_state->outgoing, p, next); return; } } @@ -923,13 +974,13 @@ packet_send2(void) /* after a NEWKEYS message we can send the complete queue */ if (type == SSH2_MSG_NEWKEYS) { rekeying = 0; - while ((p = TAILQ_FIRST(&outgoing))) { + while ((p = TAILQ_FIRST(&active_state->outgoing))) { type = p->type; debug("dequeue packet: %u", type); - buffer_free(&outgoing_packet); - memcpy(&outgoing_packet, &p->payload, + buffer_free(&active_state->outgoing_packet); + memcpy(&active_state->outgoing_packet, &p->payload, sizeof(Buffer)); - TAILQ_REMOVE(&outgoing, p, next); + TAILQ_REMOVE(&active_state->outgoing, p, next); xfree(p); packet_send2_wrapped(); } @@ -962,8 +1013,8 @@ packet_read_seqnr(u_int32_t *seqnr_p) DBG(debug("packet_read()")); - setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS), - sizeof(fd_mask)); + setp = (fd_set *)xcalloc(howmany(active_state->connection_in + 1, + NFDBITS), sizeof(fd_mask)); /* Since we are blocking, ensure that all written packets have been sent. */ packet_write_wait(); @@ -987,27 +1038,27 @@ packet_read_seqnr(u_int32_t *seqnr_p) * Otherwise, wait for some data to arrive, add it to the * buffer, and try again. */ - memset(setp, 0, howmany(connection_in + 1, NFDBITS) * - sizeof(fd_mask)); - FD_SET(connection_in, setp); + memset(setp, 0, howmany(active_state->connection_in + 1, + NFDBITS) * sizeof(fd_mask)); + FD_SET(active_state->connection_in, setp); - if (packet_timeout_ms > 0) { - ms_remain = packet_timeout_ms; + if (active_state->packet_timeout_ms > 0) { + ms_remain = active_state->packet_timeout_ms; timeoutp = &timeout; } /* Wait for some data to arrive. */ for (;;) { - if (packet_timeout_ms != -1) { + if (active_state->packet_timeout_ms != -1) { ms_to_timeval(&timeout, ms_remain); gettimeofday(&start, NULL); } - if ((ret = select(connection_in + 1, setp, NULL, - NULL, timeoutp)) >= 0) + if ((ret = select(active_state->connection_in + 1, setp, + NULL, NULL, timeoutp)) >= 0) break; - if (errno != EAGAIN && errno != EINTR && + if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) break; - if (packet_timeout_ms == -1) + if (active_state->packet_timeout_ms == -1) continue; ms_subtract_diff(&start, &ms_remain); if (ms_remain <= 0) { @@ -1021,7 +1072,7 @@ packet_read_seqnr(u_int32_t *seqnr_p) cleanup_exit(255); } /* Read data from the socket. */ - len = read(connection_in, buf, sizeof(buf)); + len = read(active_state->connection_in, buf, sizeof(buf)); if (len == 0) { logit("Connection closed by %.200s", get_remote_ipaddr()); cleanup_exit(255); @@ -1073,31 +1124,32 @@ packet_read_poll1(void) u_int checksum, stored_checksum; /* Check if input size is less than minimum packet size. */ - if (buffer_len(&input) < 4 + 8) + if (buffer_len(&active_state->input) < 4 + 8) return SSH_MSG_NONE; /* Get length of incoming packet. */ - cp = buffer_ptr(&input); + cp = buffer_ptr(&active_state->input); len = get_u32(cp); if (len < 1 + 2 + 2 || len > 256 * 1024) packet_disconnect("Bad packet length %u.", len); padded_len = (len + 8) & ~7; /* Check if the packet has been entirely received. */ - if (buffer_len(&input) < 4 + padded_len) + if (buffer_len(&active_state->input) < 4 + padded_len) return SSH_MSG_NONE; /* The entire packet is in buffer. */ /* Consume packet length. */ - buffer_consume(&input, 4); + buffer_consume(&active_state->input, 4); /* * Cryptographic attack detector for ssh * (C)1998 CORE-SDI, Buenos Aires Argentina * Ariel Futoransky(futo@core-sdi.com) */ - if (!receive_context.plaintext) { - switch (detect_attack(buffer_ptr(&input), padded_len)) { + if (!active_state->receive_context.plaintext) { + switch (detect_attack(buffer_ptr(&active_state->input), + padded_len)) { case DEATTACK_DETECTED: packet_disconnect("crc32 compensation attack: " "network attack detected"); @@ -1108,45 +1160,48 @@ packet_read_poll1(void) } /* Decrypt data to incoming_packet. */ - buffer_clear(&incoming_packet); - cp = buffer_append_space(&incoming_packet, padded_len); - cipher_crypt(&receive_context, cp, buffer_ptr(&input), padded_len); + buffer_clear(&active_state->incoming_packet); + cp = buffer_append_space(&active_state->incoming_packet, padded_len); + cipher_crypt(&active_state->receive_context, cp, + buffer_ptr(&active_state->input), padded_len); - buffer_consume(&input, padded_len); + buffer_consume(&active_state->input, padded_len); #ifdef PACKET_DEBUG fprintf(stderr, "read_poll plain: "); - buffer_dump(&incoming_packet); + buffer_dump(&active_state->incoming_packet); #endif /* Compute packet checksum. */ - checksum = ssh_crc32(buffer_ptr(&incoming_packet), - buffer_len(&incoming_packet) - 4); + checksum = ssh_crc32(buffer_ptr(&active_state->incoming_packet), + buffer_len(&active_state->incoming_packet) - 4); /* Skip padding. */ - buffer_consume(&incoming_packet, 8 - len % 8); + buffer_consume(&active_state->incoming_packet, 8 - len % 8); /* Test check bytes. */ - if (len != buffer_len(&incoming_packet)) + if (len != buffer_len(&active_state->incoming_packet)) packet_disconnect("packet_read_poll1: len %d != buffer_len %d.", - len, buffer_len(&incoming_packet)); + len, buffer_len(&active_state->incoming_packet)); - cp = (u_char *)buffer_ptr(&incoming_packet) + len - 4; + cp = (u_char *)buffer_ptr(&active_state->incoming_packet) + len - 4; stored_checksum = get_u32(cp); if (checksum != stored_checksum) packet_disconnect("Corrupted check bytes on input."); - buffer_consume_end(&incoming_packet, 4); - - if (packet_compression) { - buffer_clear(&compression_buffer); - buffer_uncompress(&incoming_packet, &compression_buffer); - buffer_clear(&incoming_packet); - buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), - buffer_len(&compression_buffer)); + buffer_consume_end(&active_state->incoming_packet, 4); + + if (active_state->packet_compression) { + buffer_clear(&active_state->compression_buffer); + buffer_uncompress(&active_state->incoming_packet, + &active_state->compression_buffer); + buffer_clear(&active_state->incoming_packet); + buffer_append(&active_state->incoming_packet, + buffer_ptr(&active_state->compression_buffer), + buffer_len(&active_state->compression_buffer)); } - p_read.packets++; - p_read.bytes += padded_len + 4; - type = buffer_get_char(&incoming_packet); + active_state->p_read.packets++; + active_state->p_read.bytes += padded_len + 4; + type = buffer_get_char(&active_state->incoming_packet); if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) packet_disconnect("Invalid ssh1 packet type: %d", type); return type; @@ -1155,7 +1210,6 @@ packet_read_poll1(void) static int packet_read_poll2(u_int32_t *seqnr_p) { - static u_int packet_length = 0; u_int padlen, need; u_char *macbuf, *cp, type; u_int maclen, block_size; @@ -1163,50 +1217,52 @@ packet_read_poll2(u_int32_t *seqnr_p) Mac *mac = NULL; Comp *comp = NULL; - if (packet_discard) + if (active_state->packet_discard) return SSH_MSG_NONE; - if (newkeys[MODE_IN] != NULL) { - enc = &newkeys[MODE_IN]->enc; - mac = &newkeys[MODE_IN]->mac; - comp = &newkeys[MODE_IN]->comp; + if (active_state->newkeys[MODE_IN] != NULL) { + enc = &active_state->newkeys[MODE_IN]->enc; + mac = &active_state->newkeys[MODE_IN]->mac; + comp = &active_state->newkeys[MODE_IN]->comp; } maclen = mac && mac->enabled ? mac->mac_len : 0; block_size = enc ? enc->block_size : 8; - if (packet_length == 0) { + if (active_state->packlen == 0) { /* * check if input size is less than the cipher block size, * decrypt first block and extract length of incoming packet */ - if (buffer_len(&input) < block_size) + if (buffer_len(&active_state->input) < block_size) return SSH_MSG_NONE; - buffer_clear(&incoming_packet); - cp = buffer_append_space(&incoming_packet, block_size); - cipher_crypt(&receive_context, cp, buffer_ptr(&input), + buffer_clear(&active_state->incoming_packet); + cp = buffer_append_space(&active_state->incoming_packet, block_size); - cp = buffer_ptr(&incoming_packet); - packet_length = get_u32(cp); - if (packet_length < 1 + 4 || packet_length > PACKET_MAX_SIZE) { + cipher_crypt(&active_state->receive_context, cp, + buffer_ptr(&active_state->input), block_size); + cp = buffer_ptr(&active_state->incoming_packet); + active_state->packlen = get_u32(cp); + if (active_state->packlen < 1 + 4 || + active_state->packlen > PACKET_MAX_SIZE) { #ifdef PACKET_DEBUG - buffer_dump(&incoming_packet); + buffer_dump(&active_state->incoming_packet); #endif - logit("Bad packet length %u.", packet_length); - packet_start_discard(enc, mac, packet_length, + logit("Bad packet length %u.", active_state->packlen); + packet_start_discard(enc, mac, active_state->packlen, PACKET_MAX_SIZE); return SSH_MSG_NONE; } - DBG(debug("input: packet len %u", packet_length+4)); - buffer_consume(&input, block_size); + DBG(debug("input: packet len %u", active_state->packlen+4)); + buffer_consume(&active_state->input, block_size); } /* we have a partial packet of block_size bytes */ - need = 4 + packet_length - block_size; + need = 4 + active_state->packlen - block_size; DBG(debug("partial packet %d, need %d, maclen %d", block_size, need, maclen)); if (need % block_size != 0) { logit("padding error: need %d block %d mod %d", need, block_size, need % block_size); - packet_start_discard(enc, mac, packet_length, + packet_start_discard(enc, mac, active_state->packlen, PACKET_MAX_SIZE - block_size); return SSH_MSG_NONE; } @@ -1214,84 +1270,90 @@ packet_read_poll2(u_int32_t *seqnr_p) * check if the entire packet has been received and * decrypt into incoming_packet */ - if (buffer_len(&input) < need + maclen) + if (buffer_len(&active_state->input) < need + maclen) return SSH_MSG_NONE; #ifdef PACKET_DEBUG fprintf(stderr, "read_poll enc/full: "); - buffer_dump(&input); + buffer_dump(&active_state->input); #endif - cp = buffer_append_space(&incoming_packet, need); - cipher_crypt(&receive_context, cp, buffer_ptr(&input), need); - buffer_consume(&input, need); + cp = buffer_append_space(&active_state->incoming_packet, need); + cipher_crypt(&active_state->receive_context, cp, + buffer_ptr(&active_state->input), need); + buffer_consume(&active_state->input, need); /* * compute MAC over seqnr and packet, * increment sequence number for incoming packet */ if (mac && mac->enabled) { - macbuf = mac_compute(mac, p_read.seqnr, - buffer_ptr(&incoming_packet), - buffer_len(&incoming_packet)); - if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0) { + macbuf = mac_compute(mac, active_state->p_read.seqnr, + buffer_ptr(&active_state->incoming_packet), + buffer_len(&active_state->incoming_packet)); + if (memcmp(macbuf, buffer_ptr(&active_state->input), + mac->mac_len) != 0) { logit("Corrupted MAC on input."); if (need > PACKET_MAX_SIZE) fatal("internal error need %d", need); - packet_start_discard(enc, mac, packet_length, + packet_start_discard(enc, mac, active_state->packlen, PACKET_MAX_SIZE - need); return SSH_MSG_NONE; } - DBG(debug("MAC #%d ok", p_read.seqnr)); - buffer_consume(&input, mac->mac_len); + DBG(debug("MAC #%d ok", active_state->p_read.seqnr)); + buffer_consume(&active_state->input, mac->mac_len); } /* XXX now it's safe to use fatal/packet_disconnect */ if (seqnr_p != NULL) - *seqnr_p = p_read.seqnr; - if (++p_read.seqnr == 0) + *seqnr_p = active_state->p_read.seqnr; + if (++active_state->p_read.seqnr == 0) logit("incoming seqnr wraps around"); - if (++p_read.packets == 0) + if (++active_state->p_read.packets == 0) if (!(datafellows & SSH_BUG_NOREKEY)) fatal("XXX too many packets with same key"); - p_read.blocks += (packet_length + 4) / block_size; - p_read.bytes += packet_length + 4; + active_state->p_read.blocks += (active_state->packlen + 4) / block_size; + active_state->p_read.bytes += active_state->packlen + 4; /* get padlen */ - cp = buffer_ptr(&incoming_packet); + cp = buffer_ptr(&active_state->incoming_packet); padlen = cp[4]; DBG(debug("input: padlen %d", padlen)); if (padlen < 4) packet_disconnect("Corrupted padlen %d on input.", padlen); /* skip packet size + padlen, discard padding */ - buffer_consume(&incoming_packet, 4 + 1); - buffer_consume_end(&incoming_packet, padlen); + buffer_consume(&active_state->incoming_packet, 4 + 1); + buffer_consume_end(&active_state->incoming_packet, padlen); - DBG(debug("input: len before de-compress %d", buffer_len(&incoming_packet))); + DBG(debug("input: len before de-compress %d", + buffer_len(&active_state->incoming_packet))); if (comp && comp->enabled) { - buffer_clear(&compression_buffer); - buffer_uncompress(&incoming_packet, &compression_buffer); - buffer_clear(&incoming_packet); - buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), - buffer_len(&compression_buffer)); + buffer_clear(&active_state->compression_buffer); + buffer_uncompress(&active_state->incoming_packet, + &active_state->compression_buffer); + buffer_clear(&active_state->incoming_packet); + buffer_append(&active_state->incoming_packet, + buffer_ptr(&active_state->compression_buffer), + buffer_len(&active_state->compression_buffer)); DBG(debug("input: len after de-compress %d", - buffer_len(&incoming_packet))); + buffer_len(&active_state->incoming_packet))); } /* * get packet type, implies consume. * return length of payload (without type field) */ - type = buffer_get_char(&incoming_packet); + type = buffer_get_char(&active_state->incoming_packet); if (type < SSH2_MSG_MIN || type >= SSH2_MSG_LOCAL_MIN) packet_disconnect("Invalid ssh2 packet type: %d", type); if (type == SSH2_MSG_NEWKEYS) set_newkeys(MODE_IN); - else if (type == SSH2_MSG_USERAUTH_SUCCESS && !server_side) + else if (type == SSH2_MSG_USERAUTH_SUCCESS && + !active_state->server_side) packet_enable_delayed_compress(); #ifdef PACKET_DEBUG fprintf(stderr, "read/plain[%d]:\r\n", type); - buffer_dump(&incoming_packet); + buffer_dump(&active_state->incoming_packet); #endif /* reset for next packet */ - packet_length = 0; + active_state->packlen = 0; return type; } @@ -1306,7 +1368,7 @@ packet_read_poll_seqnr(u_int32_t *seqnr_p) if (compat20) { type = packet_read_poll2(seqnr_p); if (type) { - keep_alive_timeouts = 0; + active_state->keep_alive_timeouts = 0; DBG(debug("received packet type %d", type)); } switch (type) { @@ -1376,14 +1438,14 @@ packet_read_poll(void) void packet_process_incoming(const char *buf, u_int len) { - if (packet_discard) { - keep_alive_timeouts = 0; /* ?? */ - if (len >= packet_discard) + if (active_state->packet_discard) { + active_state->keep_alive_timeouts = 0; /* ?? */ + if (len >= active_state->packet_discard) packet_stop_discard(); - packet_discard -= len; + active_state->packet_discard -= len; return; } - buffer_append(&input, buf, len); + buffer_append(&active_state->input, buf, len); } /* Returns a character from the packet. */ @@ -1393,7 +1455,7 @@ packet_get_char(void) { char ch; - buffer_get(&incoming_packet, &ch, 1); + buffer_get(&active_state->incoming_packet, &ch, 1); return (u_char) ch; } @@ -1402,7 +1464,7 @@ packet_get_char(void) u_int packet_get_int(void) { - return buffer_get_int(&incoming_packet); + return buffer_get_int(&active_state->incoming_packet); } /* @@ -1413,29 +1475,29 @@ packet_get_int(void) void packet_get_bignum(BIGNUM * value) { - buffer_get_bignum(&incoming_packet, value); + buffer_get_bignum(&active_state->incoming_packet, value); } void packet_get_bignum2(BIGNUM * value) { - buffer_get_bignum2(&incoming_packet, value); + buffer_get_bignum2(&active_state->incoming_packet, value); } void * packet_get_raw(u_int *length_ptr) { - u_int bytes = buffer_len(&incoming_packet); + u_int bytes = buffer_len(&active_state->incoming_packet); if (length_ptr != NULL) *length_ptr = bytes; - return buffer_ptr(&incoming_packet); + return buffer_ptr(&active_state->incoming_packet); } int packet_remaining(void) { - return buffer_len(&incoming_packet); + return buffer_len(&active_state->incoming_packet); } /* @@ -1448,13 +1510,13 @@ packet_remaining(void) void * packet_get_string(u_int *length_ptr) { - return buffer_get_string(&incoming_packet, length_ptr); + return buffer_get_string(&active_state->incoming_packet, length_ptr); } void * packet_get_string_ptr(u_int *length_ptr) { - return buffer_get_string_ptr(&incoming_packet, length_ptr); + return buffer_get_string_ptr(&active_state->incoming_packet, length_ptr); } /* @@ -1547,10 +1609,11 @@ packet_disconnect(const char *fmt,...) void packet_write_poll(void) { - int len = buffer_len(&output); + int len = buffer_len(&active_state->output); if (len > 0) { - len = write(connection_out, buffer_ptr(&output), len); + len = write(active_state->connection_out, + buffer_ptr(&active_state->output), len); if (len == -1) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) @@ -1559,7 +1622,7 @@ packet_write_poll(void) } if (len == 0) fatal("Write connection closed"); - buffer_consume(&output, len); + buffer_consume(&active_state->output, len); } } @@ -1576,30 +1639,30 @@ packet_write_wait(void) int ret, ms_remain; struct timeval start, timeout, *timeoutp = NULL; - setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS), - sizeof(fd_mask)); + setp = (fd_set *)xcalloc(howmany(active_state->connection_out + 1, + NFDBITS), sizeof(fd_mask)); packet_write_poll(); while (packet_have_data_to_write()) { - memset(setp, 0, howmany(connection_out + 1, NFDBITS) * - sizeof(fd_mask)); - FD_SET(connection_out, setp); + memset(setp, 0, howmany(active_state->connection_out + 1, + NFDBITS) * sizeof(fd_mask)); + FD_SET(active_state->connection_out, setp); - if (packet_timeout_ms > 0) { - ms_remain = packet_timeout_ms; + if (active_state->packet_timeout_ms > 0) { + ms_remain = active_state->packet_timeout_ms; timeoutp = &timeout; } for (;;) { - if (packet_timeout_ms != -1) { + if (active_state->packet_timeout_ms != -1) { ms_to_timeval(&timeout, ms_remain); gettimeofday(&start, NULL); } - if ((ret = select(connection_out + 1, NULL, setp, - NULL, timeoutp)) >= 0) + if ((ret = select(active_state->connection_out + 1, + NULL, setp, NULL, timeoutp)) >= 0) break; - if (errno != EAGAIN && errno != EINTR && + if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) break; - if (packet_timeout_ms == -1) + if (active_state->packet_timeout_ms == -1) continue; ms_subtract_diff(&start, &ms_remain); if (ms_remain <= 0) { @@ -1622,7 +1685,7 @@ packet_write_wait(void) int packet_have_data_to_write(void) { - return buffer_len(&output) != 0; + return buffer_len(&active_state->output) != 0; } /* Returns true if there is not too much data to write to the connection. */ @@ -1630,10 +1693,10 @@ packet_have_data_to_write(void) int packet_not_very_much_data_to_write(void) { - if (interactive_mode) - return buffer_len(&output) < 16384; + if (active_state->interactive_mode) + return buffer_len(&active_state->output) < 16384; else - return buffer_len(&output) < 128 * 1024; + return buffer_len(&active_state->output) < 128 * 1024; } @@ -1646,7 +1709,7 @@ packet_set_tos(int interactive) if (!packet_connection_is_on_socket() || !packet_connection_is_ipv4()) return; - if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, &tos, + if (setsockopt(active_state->connection_in, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) error("setsockopt IP_TOS %d: %.100s:", tos, strerror(errno)); @@ -1665,12 +1728,12 @@ packet_set_interactive(int interactive) called = 1; /* Record that we are in interactive mode. */ - interactive_mode = interactive; + active_state->interactive_mode = interactive; /* Only set socket options if using a socket. */ if (!packet_connection_is_on_socket()) return; - set_nodelay(connection_in); + set_nodelay(active_state->connection_in); packet_set_tos(interactive); } @@ -1679,7 +1742,7 @@ packet_set_interactive(int interactive) int packet_is_interactive(void) { - return interactive_mode; + return active_state->interactive_mode; } int @@ -1689,7 +1752,7 @@ packet_set_maxsize(u_int s) if (called) { logit("packet_set_maxsize: called twice: old %d new %d", - max_packet_size, s); + active_state->max_packet_size, s); return -1; } if (s < 4 * 1024 || s > 1024 * 1024) { @@ -1698,15 +1761,33 @@ packet_set_maxsize(u_int s) } called = 1; debug("packet_set_maxsize: setting to %d", s); - max_packet_size = s; + active_state->max_packet_size = s; return s; } +int +packet_inc_alive_timeouts(void) +{ + return ++active_state->keep_alive_timeouts; +} + +void +packet_set_alive_timeouts(int ka) +{ + active_state->keep_alive_timeouts = ka; +} + +u_int +packet_get_maxsize(void) +{ + return active_state->max_packet_size; +} + /* roundup current message to pad bytes */ void packet_add_padding(u_char pad) { - extra_pad = pad; + active_state->extra_pad = pad; } /* @@ -1743,26 +1824,46 @@ packet_need_rekeying(void) if (datafellows & SSH_BUG_NOREKEY) return 0; return - (p_send.packets > MAX_PACKETS) || - (p_read.packets > MAX_PACKETS) || - (max_blocks_out && (p_send.blocks > max_blocks_out)) || - (max_blocks_in && (p_read.blocks > max_blocks_in)); + (active_state->p_send.packets > MAX_PACKETS) || + (active_state->p_read.packets > MAX_PACKETS) || + (active_state->max_blocks_out && + (active_state->p_send.blocks > active_state->max_blocks_out)) || + (active_state->max_blocks_in && + (active_state->p_read.blocks > active_state->max_blocks_in)); } void packet_set_rekey_limit(u_int32_t bytes) { - rekey_limit = bytes; + active_state->rekey_limit = bytes; } void packet_set_server(void) { - server_side = 1; + active_state->server_side = 1; } void packet_set_authenticated(void) { - after_authentication = 1; + active_state->after_authentication = 1; +} + +void * +packet_get_input(void) +{ + return (void *)&active_state->input; +} + +void * +packet_get_output(void) +{ + return (void *)&active_state->output; +} + +void * +packet_get_newkeys(int mode) +{ + return (void *)active_state->newkeys[mode]; } diff --git a/packet.h b/packet.h index 03bb87c9b..9a9c97719 100644 --- a/packet.h +++ b/packet.h @@ -1,4 +1,4 @@ -/* $OpenBSD: packet.h,v 1.49 2008/07/10 18:08:11 markus Exp $ */ +/* $OpenBSD: packet.h,v 1.50 2009/05/25 06:48:01 andreas Exp $ */ /* * Author: Tatu Ylonen @@ -72,6 +72,7 @@ void packet_get_state(int, u_int32_t *, u_int64_t *, u_int32_t *, u_int64_t *); void packet_set_state(int, u_int32_t, u_int64_t, u_int32_t, u_int64_t); int packet_get_ssh1_cipher(void); void packet_set_iv(int, u_char *); +void *packet_get_newkeys(int); void packet_write_poll(void); void packet_write_wait(void); @@ -87,10 +88,10 @@ void packet_add_padding(u_char); void tty_make_modes(int, struct termios *); void tty_parse_modes(int, int *); -extern u_int max_packet_size; -extern int keep_alive_timeouts; +void packet_set_alive_timeouts(int); +int packet_inc_alive_timeouts(void); int packet_set_maxsize(u_int); -#define packet_get_maxsize() max_packet_size +u_int packet_get_maxsize(void); /* don't allow remaining bytes after the end of the message */ #define packet_check_eom() \ @@ -106,4 +107,7 @@ do { \ int packet_need_rekeying(void); void packet_set_rekey_limit(u_int32_t); +void *packet_get_input(void); +void *packet_get_output(void); + #endif /* PACKET_H */ diff --git a/serverloop.c b/serverloop.c index 81cafe6ad..53cb67d7b 100644 --- a/serverloop.c +++ b/serverloop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: serverloop.c,v 1.157 2009/02/12 03:16:01 djm Exp $ */ +/* $OpenBSD: serverloop.c,v 1.158 2009/05/25 06:48:01 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -249,7 +249,7 @@ client_alive_check(void) int channel_id; /* timeout, check to see how many we have had */ - if (++keep_alive_timeouts > options.client_alive_count_max) { + if (packet_inc_alive_timeouts() > options.client_alive_count_max) { logit("Timeout, client not responding."); cleanup_exit(255); } @@ -890,7 +890,7 @@ server_input_keep_alive(int type, u_int32_t seq, void *ctxt) * even if this was generated by something other than * the bogus CHANNEL_REQUEST we send for keepalives. */ - keep_alive_timeouts = 0; + packet_set_alive_timeouts(0); } static void -- cgit v1.2.3 From c5564e1c4c41ae9af96973e2996e2a4285acbae8 Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Sun, 21 Jun 2009 18:53:53 +1000 Subject: - andreas@cvs.openbsd.org 2009/05/28 16:50:16 [sshd.c packet.c serverloop.c monitor_wrap.c clientloop.c sshconnect.c monitor.c Added roaming.h roaming_common.c roaming_dummy.c] Keep track of number of bytes read and written. Needed for upcoming changes. Most code from Martin Forssen, maf at appgate dot com. ok markus@ Also, applied appropriate changes to Makefile.in --- ChangeLog | 3 +- Makefile.in | 16 +++++---- clientloop.c | 9 ++--- monitor.c | 14 +++++++- monitor_wrap.c | 9 ++++- packet.c | 30 +++++++++-------- roaming.h | 31 +++++++++++++++++ roaming_common.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ roaming_dummy.c | 55 ++++++++++++++++++++++++++++++ serverloop.c | 8 +++-- sshconnect.c | 8 +++-- sshd.c | 7 ++-- 12 files changed, 254 insertions(+), 36 deletions(-) create mode 100644 roaming.h create mode 100644 roaming_common.c create mode 100644 roaming_dummy.c (limited to 'monitor_wrap.c') diff --git a/ChangeLog b/ChangeLog index 2f73a6bcd..8ed7db993 100644 --- a/ChangeLog +++ b/ChangeLog @@ -83,10 +83,11 @@ ok markus@ - andreas@cvs.openbsd.org 2009/05/28 16:50:16 [sshd.c packet.c serverloop.c monitor_wrap.c clientloop.c sshconnect.c - monitor.c] + monitor.c Added roaming.h roaming_common.c roaming_dummy.c] Keep track of number of bytes read and written. Needed for upcoming changes. Most code from Martin Forssen, maf at appgate dot com. ok markus@ + Also, applied appropriate changes to Makefile.in 20090616 - (dtucker) [configure.ac defines.h] Bug #1607: handle the case where fsid_t diff --git a/Makefile.in b/Makefile.in index 312b8d2b1..75eb06d6f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,4 +1,4 @@ -# $Id: Makefile.in,v 1.298 2008/11/05 05:20:46 djm Exp $ +# $Id: Makefile.in,v 1.299 2009/06/21 08:53:53 dtucker Exp $ # uncomment if you run a non bourne compatable shell. Ie. csh #SHELL = @SH@ @@ -74,7 +74,8 @@ LIBSSH_OBJS=acss.o authfd.o authfile.o bufaux.o bufbn.o buffer.o \ entropy.o scard-opensc.o gss-genr.o umac.o jpake.o schnorr.o SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \ - sshconnect.o sshconnect1.o sshconnect2.o mux.o + sshconnect.o sshconnect1.o sshconnect2.o mux.o \ + roaming_common.o SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o auth-rsa.o auth-rh-rsa.o \ sshpty.o sshlogin.o servconf.o serverloop.o \ @@ -86,7 +87,8 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o auth-rsa.o auth-rh-rsa.o \ auth-krb5.o \ auth2-gss.o gss-serv.o gss-serv-krb5.o \ loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \ - audit.o audit-bsm.o platform.o sftp-server.o sftp-common.o + audit.o audit-bsm.o platform.o sftp-server.o sftp-common.o \ + roaming_common.o MANPAGES = moduli.5.out scp.1.out ssh-add.1.out ssh-agent.1.out ssh-keygen.1.out ssh-keyscan.1.out ssh.1.out sshd.8.out sftp-server.8.out sftp.1.out ssh-rand-helper.8.out ssh-keysign.8.out sshd_config.5.out ssh_config.5.out MANPAGES_IN = moduli.5 scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh-keyscan.1 ssh.1 sshd.8 sftp-server.8 sftp.1 ssh-rand-helper.8 ssh-keysign.8 sshd_config.5 ssh_config.5 @@ -151,11 +153,11 @@ ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o $(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) -ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o - $(LD) -o $@ ssh-keysign.o readconf.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) +ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o roaming_dummy.o + $(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) -ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o - $(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS) +ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o + $(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS) sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o $(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) diff --git a/clientloop.c b/clientloop.c index d5a06556a..43f001bc4 100644 --- a/clientloop.c +++ b/clientloop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: clientloop.c,v 1.211 2009/05/27 06:33:39 andreas Exp $ */ +/* $OpenBSD: clientloop.c,v 1.212 2009/05/28 16:50:16 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -109,6 +109,7 @@ #include "misc.h" #include "match.h" #include "msg.h" +#include "roaming.h" /* import options */ extern Options options; @@ -634,7 +635,7 @@ client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) static void client_process_net_input(fd_set *readset) { - int len; + int len, cont = 0; char buf[8192]; /* @@ -643,8 +644,8 @@ client_process_net_input(fd_set *readset) */ if (FD_ISSET(connection_in, readset)) { /* Read as much as possible. */ - len = read(connection_in, buf, sizeof(buf)); - if (len == 0) { + len = roaming_read(connection_in, buf, sizeof(buf), &cont); + if (len == 0 && cont == 0) { /* * Received EOF. The remote host has closed the * connection. diff --git a/monitor.c b/monitor.c index 61242e6d0..36a9e1dc1 100644 --- a/monitor.c +++ b/monitor.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor.c,v 1.102 2009/05/25 06:48:01 andreas Exp $ */ +/* $OpenBSD: monitor.c,v 1.103 2009/05/28 16:50:16 andreas Exp $ */ /* * Copyright 2002 Niels Provos * Copyright 2002 Markus Friedl @@ -125,6 +125,8 @@ struct { u_int ilen; u_char *output; u_int olen; + u_int64_t sent_bytes; + u_int64_t recv_bytes; } child_state; /* Functions on the monitor that answer unprivileged requests */ @@ -1679,6 +1681,10 @@ monitor_apply_keystate(struct monitor *pmonitor) child_state.olen); memset(child_state.output, 0, child_state.olen); xfree(child_state.output); + + /* Roaming */ + if (compat20) + roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes); } static Kex * @@ -1794,6 +1800,12 @@ mm_get_keystate(struct monitor *pmonitor) child_state.input = buffer_get_string(&m, &child_state.ilen); child_state.output = buffer_get_string(&m, &child_state.olen); + /* Roaming */ + if (compat20) { + child_state.sent_bytes = buffer_get_int64(&m); + child_state.recv_bytes = buffer_get_int64(&m); + } + buffer_free(&m); } diff --git a/monitor_wrap.c b/monitor_wrap.c index d71d4a8c5..b696d7821 100644 --- a/monitor_wrap.c +++ b/monitor_wrap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor_wrap.c,v 1.66 2009/05/25 06:48:01 andreas Exp $ */ +/* $OpenBSD: monitor_wrap.c,v 1.67 2009/05/28 16:50:16 andreas Exp $ */ /* * Copyright 2002 Niels Provos * Copyright 2002 Markus Friedl @@ -77,6 +77,7 @@ #include "channels.h" #include "session.h" #include "servconf.h" +#include "roaming.h" /* Imports */ extern int compat20; @@ -660,6 +661,12 @@ mm_send_keystate(struct monitor *monitor) buffer_put_string(&m, buffer_ptr(input), buffer_len(input)); buffer_put_string(&m, buffer_ptr(output), buffer_len(output)); + /* Roaming */ + if (compat20) { + buffer_put_int64(&m, get_sent_bytes()); + buffer_put_int64(&m, get_recv_bytes()); + } + mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); debug3("%s: Finished sending state", __func__); diff --git a/packet.c b/packet.c index cecab82e9..f3f8389a3 100644 --- a/packet.c +++ b/packet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.162 2009/05/27 06:36:07 andreas Exp $ */ +/* $OpenBSD: packet.c,v 1.163 2009/05/28 16:50:16 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -77,6 +77,7 @@ #include "canohost.h" #include "misc.h" #include "ssh.h" +#include "roaming.h" #ifdef PACKET_DEBUG #define DBG(x) x @@ -1012,7 +1013,7 @@ packet_send(void) int packet_read_seqnr(u_int32_t *seqnr_p) { - int type, len, ret, ms_remain; + int type, len, ret, ms_remain, cont; fd_set *setp; char buf[8192]; struct timeval timeout, start, *timeoutp = NULL; @@ -1061,8 +1062,7 @@ packet_read_seqnr(u_int32_t *seqnr_p) if ((ret = select(active_state->connection_in + 1, setp, NULL, NULL, timeoutp)) >= 0) break; - if (errno != EAGAIN && errno != EINTR && - errno != EWOULDBLOCK) + if (errno != EAGAIN && errno != EINTR) break; if (active_state->packet_timeout_ms == -1) continue; @@ -1078,7 +1078,11 @@ packet_read_seqnr(u_int32_t *seqnr_p) cleanup_exit(255); } /* Read data from the socket. */ - len = read(active_state->connection_in, buf, sizeof(buf)); + do { + cont = 0; + len = roaming_read(active_state->connection_in, buf, + sizeof(buf), &cont); + } while (len == 0 && cont); if (len == 0) { logit("Connection closed by %.200s", get_remote_ipaddr()); cleanup_exit(255); @@ -1624,23 +1628,23 @@ void packet_write_poll(void) { int len = buffer_len(&active_state->output); + int cont; if (len > 0) { - len = write(active_state->connection_out, - buffer_ptr(&active_state->output), len); + cont = 0; + len = roaming_write(active_state->connection_out, + buffer_ptr(&active_state->output), len, &cont); if (len == -1) { - if (errno == EINTR || errno == EAGAIN || - errno == EWOULDBLOCK) + if (errno == EINTR || errno == EAGAIN) return; fatal("Write failed: %.100s", strerror(errno)); } - if (len == 0) + if (len == 0 && !cont) fatal("Write connection closed"); buffer_consume(&active_state->output, len); } } - /* * Calls packet_write_poll repeatedly until all pending output data has been * written. @@ -1673,8 +1677,7 @@ packet_write_wait(void) if ((ret = select(active_state->connection_out + 1, NULL, setp, NULL, timeoutp)) >= 0) break; - if (errno != EAGAIN && errno != EINTR && - errno != EWOULDBLOCK) + if (errno != EAGAIN && errno != EINTR) break; if (active_state->packet_timeout_ms == -1) continue; @@ -1713,7 +1716,6 @@ packet_not_very_much_data_to_write(void) return buffer_len(&active_state->output) < 128 * 1024; } - static void packet_set_tos(int interactive) { diff --git a/roaming.h b/roaming.h new file mode 100644 index 000000000..88193453a --- /dev/null +++ b/roaming.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2004-2009 AppGate Network Security AB + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef ROAMING_H +#define ROAMING_H + +extern int resume_in_progress; + +void add_recv_bytes(u_int64_t); +ssize_t roaming_write(int, const void *, size_t, int *); +ssize_t roaming_read(int, void *, size_t, int *); +ssize_t roaming_atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); +u_int64_t get_recv_bytes(void); +u_int64_t get_sent_bytes(void); +void roam_set_bytes(u_int64_t, u_int64_t); +int resume_kex(void); + +#endif /* ROAMING */ diff --git a/roaming_common.c b/roaming_common.c new file mode 100644 index 000000000..5a871b23e --- /dev/null +++ b/roaming_common.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2004-2009 AppGate Network Security AB + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include "atomicio.h" +#include "log.h" +#include "packet.h" +#include "xmalloc.h" +#include "cipher.h" +#include "buffer.h" +#include "roaming.h" + +static u_int64_t write_bytes = 0; +static u_int64_t read_bytes = 0; + +int resume_in_progress = 0; + +u_int64_t +get_recv_bytes(void) +{ + return read_bytes; +} + +void +add_recv_bytes(u_int64_t num) +{ + read_bytes += num; +} + +u_int64_t +get_sent_bytes(void) +{ + return write_bytes; +} + +void +roam_set_bytes(u_int64_t sent, u_int64_t recv) +{ + read_bytes = recv; + write_bytes = sent; +} + +ssize_t +roaming_write(int fd, const void *buf, size_t count, int *cont) +{ + ssize_t ret; + + ret = write(fd, buf, count); + if (ret > 0 && !resume_in_progress) { + write_bytes += ret; + } + debug("Wrote %d bytes for a total of %lld", ret, write_bytes); + return ret; +} + +ssize_t +roaming_read(int fd, void *buf, size_t count, int *cont) +{ + ssize_t ret = read(fd, buf, count); + if (ret > 0) { + if (!resume_in_progress) { + read_bytes += ret; + } + } + return ret; +} + +ssize_t +roaming_atomicio(ssize_t(*f)(), int fd, void *buf, size_t count) +{ + ssize_t ret = atomicio(f, fd, buf, count); + + if ((f == write || f == vwrite) && ret > 0 && !resume_in_progress) { + write_bytes += ret; + } else if (f == read && ret > 0 && !resume_in_progress) { + read_bytes += ret; + } + return ret; +} diff --git a/roaming_dummy.c b/roaming_dummy.c new file mode 100644 index 000000000..cd1d20257 --- /dev/null +++ b/roaming_dummy.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2004-2009 AppGate Network Security AB + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * This file is included in the client programs which should not + * support roaming. + */ + +#include +#include + +int resume_in_progress = 0; + +u_int64_t get_recv_bytes() +{ + return 0; +} + +ssize_t +roaming_write(int fd, const void *buf, size_t count, int *cont) +{ + return write(fd, buf, count); +} + +ssize_t +roaming_read(int fd, void *buf, size_t count, int *cont) +{ + if (cont) + *cont = 0; + return read(fd, buf, count); +} + +void +add_recv_bytes(u_int64_t num) +{ +} + +int +resume_kex() +{ + return 1; +} diff --git a/serverloop.c b/serverloop.c index 53cb67d7b..d8cb54bc7 100644 --- a/serverloop.c +++ b/serverloop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: serverloop.c,v 1.158 2009/05/25 06:48:01 andreas Exp $ */ +/* $OpenBSD: serverloop.c,v 1.159 2009/05/28 16:50:16 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -78,6 +78,7 @@ #include "auth-options.h" #include "serverloop.h" #include "misc.h" +#include "roaming.h" extern ServerOptions options; @@ -391,8 +392,11 @@ process_input(fd_set *readset) /* Read and buffer any input data from the client. */ if (FD_ISSET(connection_in, readset)) { - len = read(connection_in, buf, sizeof(buf)); + int cont = 0; + len = roaming_read(connection_in, buf, sizeof(buf), &cont); if (len == 0) { + if (cont) + return; verbose("Connection closed by %.100s", get_remote_ipaddr()); connection_closed = 1; diff --git a/sshconnect.c b/sshconnect.c index dee3ba544..3e57e859d 100644 --- a/sshconnect.c +++ b/sshconnect.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sshconnect.c,v 1.213 2009/05/27 06:38:16 andreas Exp $ */ +/* $OpenBSD: sshconnect.c,v 1.214 2009/05/28 16:50:16 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -56,6 +56,7 @@ #include "atomicio.h" #include "misc.h" #include "dns.h" +#include "roaming.h" #include "version.h" char *client_version_string = NULL; @@ -452,7 +453,7 @@ ssh_exchange_identification(int timeout_ms) } } - len = atomicio(read, connection_in, &buf[i], 1); + len = roaming_atomicio(read, connection_in, &buf[i], 1); if (len != 1 && errno == EPIPE) fatal("ssh_exchange_identification: " @@ -537,7 +538,8 @@ ssh_exchange_identification(int timeout_ms) compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, compat20 ? PROTOCOL_MINOR_2 : minor1, SSH_VERSION, compat20 ? "\r\n" : "\n"); - if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf)) + if (roaming_atomicio(vwrite, connection_out, buf, strlen(buf)) + != strlen(buf)) fatal("write: %.100s", strerror(errno)); client_version_string = xstrdup(buf); chop(client_version_string); diff --git a/sshd.c b/sshd.c index 3b5cd3cfd..91831e208 100644 --- a/sshd.c +++ b/sshd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sshd.c,v 1.366 2009/01/22 10:02:34 djm Exp $ */ +/* $OpenBSD: sshd.c,v 1.367 2009/05/28 16:50:16 andreas Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -117,6 +117,7 @@ #include "ssh-gss.h" #endif #include "monitor_wrap.h" +#include "roaming.h" #include "version.h" #ifdef LIBWRAP @@ -419,7 +420,7 @@ sshd_exchange_identification(int sock_in, int sock_out) server_version_string = xstrdup(buf); /* Send our protocol version identification. */ - if (atomicio(vwrite, sock_out, server_version_string, + if (roaming_atomicio(vwrite, sock_out, server_version_string, strlen(server_version_string)) != strlen(server_version_string)) { logit("Could not write ident string to %s", get_remote_ipaddr()); @@ -429,7 +430,7 @@ sshd_exchange_identification(int sock_in, int sock_out) /* Read other sides version identification. */ memset(buf, 0, sizeof(buf)); for (i = 0; i < sizeof(buf) - 1; i++) { - if (atomicio(read, sock_in, &buf[i], 1) != 1) { + if (roaming_atomicio(read, sock_in, &buf[i], 1) != 1) { logit("Did not receive identification string from %s", get_remote_ipaddr()); cleanup_exit(255); -- cgit v1.2.3 From 821d3dbe36faa1e53a269e434487d0617a0a1578 Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Mon, 22 Jun 2009 16:11:06 +1000 Subject: - dtucker@cvs.openbsd.org 2009/06/22 05:39:28 [monitor_wrap.c monitor_mm.c ssh-keygen.c auth2.c gss-genr.c sftp-client.c] alphabetize includes; reduces diff vs portable and style(9). ok stevesk djm (Id sync only; these were already in order in -portable) --- ChangeLog | 8 ++++++++ auth2.c | 2 +- gss-genr.c | 2 +- monitor_mm.c | 2 +- monitor_wrap.c | 2 +- sftp-client.c | 2 +- ssh-keygen.c | 2 +- 7 files changed, 14 insertions(+), 6 deletions(-) (limited to 'monitor_wrap.c') diff --git a/ChangeLog b/ChangeLog index c801521bc..0f39fa215 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +20090622 + - (dtucker) OpenBSD CVS Sync + - dtucker@cvs.openbsd.org 2009/06/22 05:39:28 + [monitor_wrap.c monitor_mm.c ssh-keygen.c auth2.c gss-genr.c sftp-client.c] + alphabetize includes; reduces diff vs portable and style(9). + ok stevesk djm + (Id sync only; these were already in order in -portable) + 20090621 - (dtucker) OpenBSD CVS Sync - markus@cvs.openbsd.org 2009/03/17 21:37:00 diff --git a/auth2.c b/auth2.c index 92e6f5842..5d5468559 100644 --- a/auth2.c +++ b/auth2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: auth2.c,v 1.120 2008/11/04 08:22:12 djm Exp $ */ +/* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */ /* * Copyright (c) 2000 Markus Friedl. All rights reserved. * diff --git a/gss-genr.c b/gss-genr.c index e9190575d..842f38582 100644 --- a/gss-genr.c +++ b/gss-genr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gss-genr.c,v 1.19 2007/06/12 11:56:15 dtucker Exp $ */ +/* $OpenBSD: gss-genr.c,v 1.20 2009/06/22 05:39:28 dtucker Exp $ */ /* * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved. diff --git a/monitor_mm.c b/monitor_mm.c index dab747532..faf9f3dcb 100644 --- a/monitor_mm.c +++ b/monitor_mm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor_mm.c,v 1.15 2006/08/03 03:34:42 deraadt Exp $ */ +/* $OpenBSD: monitor_mm.c,v 1.16 2009/06/22 05:39:28 dtucker Exp $ */ /* * Copyright 2002 Niels Provos * All rights reserved. diff --git a/monitor_wrap.c b/monitor_wrap.c index b696d7821..b8e8710f7 100644 --- a/monitor_wrap.c +++ b/monitor_wrap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monitor_wrap.c,v 1.67 2009/05/28 16:50:16 andreas Exp $ */ +/* $OpenBSD: monitor_wrap.c,v 1.68 2009/06/22 05:39:28 dtucker Exp $ */ /* * Copyright 2002 Niels Provos * Copyright 2002 Markus Friedl diff --git a/sftp-client.c b/sftp-client.c index 5e39aa7d2..0990b7912 100644 --- a/sftp-client.c +++ b/sftp-client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sftp-client.c,v 1.86 2008/06/26 06:10:09 djm Exp $ */ +/* $OpenBSD: sftp-client.c,v 1.87 2009/06/22 05:39:28 dtucker Exp $ */ /* * Copyright (c) 2001-2004 Damien Miller * diff --git a/ssh-keygen.c b/ssh-keygen.c index 5765cff08..da5db9845 100644 --- a/ssh-keygen.c +++ b/ssh-keygen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ssh-keygen.c,v 1.173 2009/02/21 19:32:04 tobias Exp $ */ +/* $OpenBSD: ssh-keygen.c,v 1.174 2009/06/22 05:39:28 dtucker Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1994 Tatu Ylonen , Espoo, Finland -- cgit v1.2.3