summaryrefslogtreecommitdiff
path: root/usr.bin/ssh/packet.c
diff options
context:
space:
mode:
authorDamien Miller <djm@cvs.openbsd.org>2017-02-03 23:03:34 +0000
committerDamien Miller <djm@cvs.openbsd.org>2017-02-03 23:03:34 +0000
commit7dd9b05a2d2fec00df5af5d36f9b497b3f6b833d (patch)
treed48df35000a3a0cbea36b5005f29c02a8847c6a6 /usr.bin/ssh/packet.c
parent2ae614284f84528343003f5fa7f01216edcc481f (diff)
add ssh_packet_set_log_preamble() to allow inclusion of a preamble
string in disconnect messages; ok markus@
Diffstat (limited to 'usr.bin/ssh/packet.c')
-rw-r--r--usr.bin/ssh/packet.c62
1 files changed, 45 insertions, 17 deletions
diff --git a/usr.bin/ssh/packet.c b/usr.bin/ssh/packet.c
index 1b4a79344cb..f2ed6a527f9 100644
--- a/usr.bin/ssh/packet.c
+++ b/usr.bin/ssh/packet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.c,v 1.244 2017/02/03 02:56:00 dtucker Exp $ */
+/* $OpenBSD: packet.c,v 1.245 2017/02/03 23:03:33 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -347,6 +347,25 @@ ssh_packet_get_mux(struct ssh *ssh)
}
int
+ssh_packet_set_log_preamble(struct ssh *ssh, const char *fmt, ...)
+{
+ va_list args;
+ int r;
+
+ free(ssh->log_preamble);
+ if (fmt == NULL)
+ ssh->log_preamble = NULL;
+ else {
+ va_start(args, fmt);
+ r = vasprintf(&ssh->log_preamble, fmt, args);
+ va_end(args);
+ if (r < 0 || ssh->log_preamble == NULL)
+ return SSH_ERR_ALLOC_FAIL;
+ }
+ return 0;
+}
+
+int
ssh_packet_stop_discard(struct ssh *ssh)
{
struct session_state *state = ssh->state;
@@ -2062,27 +2081,36 @@ ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
fatal("%s: %s", __func__, ssh_err(r));
}
+static void
+fmt_connection_id(struct ssh *ssh, char *s, size_t l)
+{
+ snprintf(s, l, "%.200s%s%s port %d",
+ ssh->log_preamble ? ssh->log_preamble : "",
+ ssh->log_preamble ? " " : "",
+ ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
+}
+
/*
* Pretty-print connection-terminating errors and exit.
*/
void
sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
{
+ char remote_id[512];
+
+ fmt_connection_id(ssh, remote_id, sizeof(remote_id));
+
switch (r) {
case SSH_ERR_CONN_CLOSED:
- logdie("Connection closed by %.200s port %d",
- ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
+ logdie("Connection closed by %s", remote_id);
case SSH_ERR_CONN_TIMEOUT:
- logdie("Connection %s %.200s port %d timed out",
- ssh->state->server_side ? "from" : "to",
- ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
+ logdie("Connection %s %s timed out",
+ ssh->state->server_side ? "from" : "to", remote_id);
case SSH_ERR_DISCONNECTED:
- logdie("Disconnected from %.200s port %d",
- ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
+ logdie("Disconnected from %s", remote_id);
case SSH_ERR_SYSTEM_ERROR:
if (errno == ECONNRESET)
- logdie("Connection reset by %.200s port %d",
- ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
+ logdie("Connection reset by %s", remote_id);
/* FALLTHROUGH */
case SSH_ERR_NO_CIPHER_ALG_MATCH:
case SSH_ERR_NO_MAC_ALG_MATCH:
@@ -2090,17 +2118,16 @@ sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
case SSH_ERR_NO_KEX_ALG_MATCH:
case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
if (ssh && ssh->kex && ssh->kex->failed_choice) {
- logdie("Unable to negotiate with %.200s port %d: %s. "
- "Their offer: %s", ssh_remote_ipaddr(ssh),
- ssh_remote_port(ssh), ssh_err(r),
+ logdie("Unable to negotiate with %s: %s. "
+ "Their offer: %s", remote_id, ssh_err(r),
ssh->kex->failed_choice);
}
/* FALLTHROUGH */
default:
- logdie("%s%sConnection %s %.200s port %d: %s",
+ logdie("%s%sConnection %s %s: %s",
tag != NULL ? tag : "", tag != NULL ? ": " : "",
ssh->state->server_side ? "from" : "to",
- ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
+ remote_id, ssh_err(r));
}
}
@@ -2113,7 +2140,7 @@ sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
void
ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
{
- char buf[1024];
+ char buf[1024], remote_id[512];
va_list args;
static int disconnecting = 0;
int r;
@@ -2126,12 +2153,13 @@ ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
* Format the message. Note that the caller must make sure the
* message is of limited size.
*/
+ fmt_connection_id(ssh, remote_id, sizeof(remote_id));
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
/* Display the error locally */
- logit("Disconnecting: %.100s", buf);
+ logit("Disconnecting %s: %.100s", remote_id, buf);
/*
* Send the disconnect message to the other side, and wait