summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packet.c62
-rw-r--r--packet.h7
2 files changed, 51 insertions, 18 deletions
diff --git a/packet.c b/packet.c
index 6b9d3525b..94e8460ca 100644
--- a/packet.c
+++ b/packet.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: packet.c,v 1.244 2017/02/03 02:56:00 dtucker Exp $ */ 1/* $OpenBSD: packet.c,v 1.245 2017/02/03 23:03:33 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -353,6 +353,25 @@ ssh_packet_get_mux(struct ssh *ssh)
353} 353}
354 354
355int 355int
356ssh_packet_set_log_preamble(struct ssh *ssh, const char *fmt, ...)
357{
358 va_list args;
359 int r;
360
361 free(ssh->log_preamble);
362 if (fmt == NULL)
363 ssh->log_preamble = NULL;
364 else {
365 va_start(args, fmt);
366 r = vasprintf(&ssh->log_preamble, fmt, args);
367 va_end(args);
368 if (r < 0 || ssh->log_preamble == NULL)
369 return SSH_ERR_ALLOC_FAIL;
370 }
371 return 0;
372}
373
374int
356ssh_packet_stop_discard(struct ssh *ssh) 375ssh_packet_stop_discard(struct ssh *ssh)
357{ 376{
358 struct session_state *state = ssh->state; 377 struct session_state *state = ssh->state;
@@ -2074,27 +2093,36 @@ ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
2074 fatal("%s: %s", __func__, ssh_err(r)); 2093 fatal("%s: %s", __func__, ssh_err(r));
2075} 2094}
2076 2095
2096static void
2097fmt_connection_id(struct ssh *ssh, char *s, size_t l)
2098{
2099 snprintf(s, l, "%.200s%s%s port %d",
2100 ssh->log_preamble ? ssh->log_preamble : "",
2101 ssh->log_preamble ? " " : "",
2102 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
2103}
2104
2077/* 2105/*
2078 * Pretty-print connection-terminating errors and exit. 2106 * Pretty-print connection-terminating errors and exit.
2079 */ 2107 */
2080void 2108void
2081sshpkt_fatal(struct ssh *ssh, const char *tag, int r) 2109sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2082{ 2110{
2111 char remote_id[512];
2112
2113 fmt_connection_id(ssh, remote_id, sizeof(remote_id));
2114
2083 switch (r) { 2115 switch (r) {
2084 case SSH_ERR_CONN_CLOSED: 2116 case SSH_ERR_CONN_CLOSED:
2085 logdie("Connection closed by %.200s port %d", 2117 logdie("Connection closed by %s", remote_id);
2086 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
2087 case SSH_ERR_CONN_TIMEOUT: 2118 case SSH_ERR_CONN_TIMEOUT:
2088 logdie("Connection %s %.200s port %d timed out", 2119 logdie("Connection %s %s timed out",
2089 ssh->state->server_side ? "from" : "to", 2120 ssh->state->server_side ? "from" : "to", remote_id);
2090 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
2091 case SSH_ERR_DISCONNECTED: 2121 case SSH_ERR_DISCONNECTED:
2092 logdie("Disconnected from %.200s port %d", 2122 logdie("Disconnected from %s", remote_id);
2093 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
2094 case SSH_ERR_SYSTEM_ERROR: 2123 case SSH_ERR_SYSTEM_ERROR:
2095 if (errno == ECONNRESET) 2124 if (errno == ECONNRESET)
2096 logdie("Connection reset by %.200s port %d", 2125 logdie("Connection reset by %s", remote_id);
2097 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
2098 /* FALLTHROUGH */ 2126 /* FALLTHROUGH */
2099 case SSH_ERR_NO_CIPHER_ALG_MATCH: 2127 case SSH_ERR_NO_CIPHER_ALG_MATCH:
2100 case SSH_ERR_NO_MAC_ALG_MATCH: 2128 case SSH_ERR_NO_MAC_ALG_MATCH:
@@ -2102,17 +2130,16 @@ sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2102 case SSH_ERR_NO_KEX_ALG_MATCH: 2130 case SSH_ERR_NO_KEX_ALG_MATCH:
2103 case SSH_ERR_NO_HOSTKEY_ALG_MATCH: 2131 case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
2104 if (ssh && ssh->kex && ssh->kex->failed_choice) { 2132 if (ssh && ssh->kex && ssh->kex->failed_choice) {
2105 logdie("Unable to negotiate with %.200s port %d: %s. " 2133 logdie("Unable to negotiate with %s: %s. "
2106 "Their offer: %s", ssh_remote_ipaddr(ssh), 2134 "Their offer: %s", remote_id, ssh_err(r),
2107 ssh_remote_port(ssh), ssh_err(r),
2108 ssh->kex->failed_choice); 2135 ssh->kex->failed_choice);
2109 } 2136 }
2110 /* FALLTHROUGH */ 2137 /* FALLTHROUGH */
2111 default: 2138 default:
2112 logdie("%s%sConnection %s %.200s port %d: %s", 2139 logdie("%s%sConnection %s %s: %s",
2113 tag != NULL ? tag : "", tag != NULL ? ": " : "", 2140 tag != NULL ? tag : "", tag != NULL ? ": " : "",
2114 ssh->state->server_side ? "from" : "to", 2141 ssh->state->server_side ? "from" : "to",
2115 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r)); 2142 remote_id, ssh_err(r));
2116 } 2143 }
2117} 2144}
2118 2145
@@ -2125,7 +2152,7 @@ sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
2125void 2152void
2126ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...) 2153ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
2127{ 2154{
2128 char buf[1024]; 2155 char buf[1024], remote_id[512];
2129 va_list args; 2156 va_list args;
2130 static int disconnecting = 0; 2157 static int disconnecting = 0;
2131 int r; 2158 int r;
@@ -2138,12 +2165,13 @@ ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
2138 * Format the message. Note that the caller must make sure the 2165 * Format the message. Note that the caller must make sure the
2139 * message is of limited size. 2166 * message is of limited size.
2140 */ 2167 */
2168 fmt_connection_id(ssh, remote_id, sizeof(remote_id));
2141 va_start(args, fmt); 2169 va_start(args, fmt);
2142 vsnprintf(buf, sizeof(buf), fmt, args); 2170 vsnprintf(buf, sizeof(buf), fmt, args);
2143 va_end(args); 2171 va_end(args);
2144 2172
2145 /* Display the error locally */ 2173 /* Display the error locally */
2146 logit("Disconnecting: %.100s", buf); 2174 logit("Disconnecting %s: %.100s", remote_id, buf);
2147 2175
2148 /* 2176 /*
2149 * Send the disconnect message to the other side, and wait 2177 * Send the disconnect message to the other side, and wait
diff --git a/packet.h b/packet.h
index c33dd17df..0d25b352c 100644
--- a/packet.h
+++ b/packet.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: packet.h,v 1.75 2017/02/03 02:56:00 dtucker Exp $ */ 1/* $OpenBSD: packet.h,v 1.76 2017/02/03 23:03:33 djm Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -62,6 +62,9 @@ struct ssh {
62 char *local_ipaddr; 62 char *local_ipaddr;
63 int local_port; 63 int local_port;
64 64
65 /* Optional preamble for log messages (e.g. username) */
66 char *log_preamble;
67
65 /* Dispatcher table */ 68 /* Dispatcher table */
66 dispatch_fn *dispatch[DISPATCH_MAX]; 69 dispatch_fn *dispatch[DISPATCH_MAX];
67 /* number of packets to ignore in the dispatcher */ 70 /* number of packets to ignore in the dispatcher */
@@ -104,6 +107,8 @@ void ssh_packet_set_server(struct ssh *);
104void ssh_packet_set_authenticated(struct ssh *); 107void ssh_packet_set_authenticated(struct ssh *);
105void ssh_packet_set_mux(struct ssh *); 108void ssh_packet_set_mux(struct ssh *);
106int ssh_packet_get_mux(struct ssh *); 109int ssh_packet_get_mux(struct ssh *);
110int ssh_packet_set_log_preamble(struct ssh *, const char *, ...)
111 __attribute__((format(printf, 2, 3)));
107 112
108int ssh_packet_log_type(u_char); 113int ssh_packet_log_type(u_char);
109 114