summaryrefslogtreecommitdiff
path: root/packet.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2017-02-03 23:03:33 +0000
committerDamien Miller <djm@mindrot.org>2017-02-04 10:08:15 +1100
commit07edd7e9537ab32aa52abb5fb2a915c350fcf441 (patch)
tree3f0cdf28bfc6a96933e7233b31a5e7faa87d8144 /packet.c
parent68bc8cfa7642d3ccbf2cd64281c16b8b9205be59 (diff)
upstream commit
add ssh_packet_set_log_preamble() to allow inclusion of a preamble string in disconnect messages; ok markus@ Upstream-ID: 34cb41182cd76d414c214ccb01c01707849afead
Diffstat (limited to 'packet.c')
-rw-r--r--packet.c62
1 files changed, 45 insertions, 17 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