diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-06-08 01:20:31 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-06-08 01:20:31 +0200 |
commit | 6c10fd5e3e563791008a22803a536e7436fee073 (patch) | |
tree | 15515ede47c465a29facb2d4be34ebee23ea2711 | |
parent | 2d8bfaab51c2dd42d73c348e238802912e870b22 (diff) |
log_bin2hex_sodium cares about logging prio set, fixed missing cleanup on bufferevent failure
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | client.c | 4 | ||||
-rw-r--r-- | common-sodium.c | 14 | ||||
-rw-r--r-- | common-sodium.h | 4 | ||||
-rw-r--r-- | logging.c | 4 | ||||
-rw-r--r-- | logging.h | 2 | ||||
-rw-r--r-- | server.c | 9 |
6 files changed, 23 insertions, 14 deletions
@@ -266,7 +266,7 @@ int main(int argc, char ** argv) LOG(ERROR, "Sodium keypair generation failed"); cleanup_and_exit(&ev_base, &ev_sig, &my_keypair, &c, 5); } - log_bin2hex_sodium("Client public key", my_keypair->publickey, sizeof(my_keypair->publickey)); + log_bin2hex_sodium(NOTICE, "Client public key", my_keypair->publickey, sizeof(my_keypair->publickey)); /* create global connection state */ c = new_connection_to_server(my_keypair); @@ -281,7 +281,7 @@ int main(int argc, char ** argv) LOG(ERROR, "Could not parse server public key: %s", opts.key_string); cleanup_and_exit(&ev_base, &ev_sig, &my_keypair, &c, 7); } - log_bin2hex_sodium("Server public key", c->peer_publickey, sizeof(c->peer_publickey)); + log_bin2hex_sodium(NOTICE, "Server public key", c->peer_publickey, sizeof(c->peer_publickey)); ev_base = event_base_new(); if (ev_base == NULL) { diff --git a/common-sodium.c b/common-sodium.c index a69196d..1f3c88e 100644 --- a/common-sodium.c +++ b/common-sodium.c @@ -4,11 +4,15 @@ #include "logging.h" #include "protocol.h" -void log_bin2hex_sodium(char const * const prefix, uint8_t const * const buffer, size_t size) +void log_bin2hex_sodium(enum log_priority log_prio, char const * const prefix, + uint8_t const * const buffer, size_t size) { char hexstr[2 * size + 1]; - LOG(NOTICE, "%s: %s", prefix, sodium_bin2hex(hexstr, sizeof(hexstr), buffer, size)); - sodium_memzero(hexstr, sizeof(hexstr)); + + if (log_prio >= lower_prio) { + LOG(log_prio, "%s: %s", prefix, sodium_bin2hex(hexstr, sizeof(hexstr), buffer, size)); + sodium_memzero(hexstr, sizeof(hexstr)); + } } struct longterm_keypair * generate_keypair_sodium(void) @@ -84,8 +88,8 @@ int generate_session_keypair_sodium(struct connection * const state) return 1; } - log_bin2hex_sodium("Generated session rx key", state->session_keys->rx, crypto_kx_SESSIONKEYBYTES); - log_bin2hex_sodium("Generated session tx key", state->session_keys->tx, crypto_kx_SESSIONKEYBYTES); + log_bin2hex_sodium(NOTICE, "Generated session rx key", state->session_keys->rx, crypto_kx_SESSIONKEYBYTES); + log_bin2hex_sodium(NOTICE, "Generated session tx key", state->session_keys->tx, crypto_kx_SESSIONKEYBYTES); return 0; } diff --git a/common-sodium.h b/common-sodium.h index 7ccfc0c..8f218a0 100644 --- a/common-sodium.h +++ b/common-sodium.h @@ -5,9 +5,11 @@ #include <stdlib.h> #include <stdint.h> +#include "logging.h" + struct connection; -void log_bin2hex_sodium(char const * const prefix, uint8_t const * const buffer, size_t size); +void log_bin2hex_sodium(enum log_priority log_prio, char const * const prefix, uint8_t const * const buffer, size_t size); __attribute__((warn_unused_result)) struct longterm_keypair * generate_keypair_sodium(void); @@ -18,9 +18,9 @@ #define DEF RESET #ifdef DEBUG_BUILD -static log_priority lower_prio = LP_DEBUG; +log_priority lower_prio = LP_DEBUG; #else -static log_priority lower_prio = NOTICE; +log_priority lower_prio = NOTICE; #endif void log_fmt_colored(log_priority prio, const char * fmt, ...) @@ -5,6 +5,8 @@ typedef enum log_priority { LP_DEBUG = 0, NOTICE, WARNING, ERROR, EVENT, PROTO } log_priority; +extern log_priority lower_prio; + void log_fmt_colored(log_priority prio, const char * fmt, ...); #endif @@ -65,7 +65,7 @@ enum recv_return protocol_request_client_auth(struct connection * const state, return RECV_FATAL_UNAUTH; } - log_bin2hex_sodium("Client AUTH with PublicKey", auth_pkt->client_publickey, sizeof(auth_pkt->client_publickey)); + log_bin2hex_sodium(NOTICE, "Client AUTH with PublicKey", auth_pkt->client_publickey, sizeof(auth_pkt->client_publickey)); if (init_crypto_server(state, auth_pkt->server_rx_header, sizeof(auth_pkt->server_rx_header)) != 0) { LOG(ERROR, "Client session keypair generation failed"); return RECV_FATAL; @@ -104,7 +104,7 @@ enum recv_return protocol_request_data(struct connection * const state, (void)state; (void)processed; LOG(NOTICE, "Received DATA with size: %u", data_pkt->header.body_size); - log_bin2hex_sodium("DATA", data_pkt->payload, data_pkt->header.body_size); + log_bin2hex_sodium(LP_DEBUG, "DATA", data_pkt->payload, data_pkt->header.body_size); recv_data(data_pkt->payload, data_pkt->header.body_size); snprintf(response, sizeof(response), "DATA OK: RECEIVED %u BYTES", data_pkt->header.body_size); if (ev_protocol_data(state, (uint8_t *)response, sizeof(response)) != 0) { @@ -168,6 +168,7 @@ static void event_cb(struct bufferevent * bev, short events, void * con) if (events & BEV_EVENT_ERROR) { LOG(ERROR, "Error from bufferevent: %s", strerror(errno)); + ev_disconnect(c); return; } if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { @@ -327,9 +328,9 @@ int main(int argc, char ** argv) cleanup_and_exit(&ev_base, &ev_listener, &ev_sig, &my_keypair, 4); } if (opts.key_string == NULL) { - log_bin2hex_sodium("Server PrivateKey", my_keypair->secretkey, sizeof(my_keypair->secretkey)); + log_bin2hex_sodium(NOTICE, "Server PrivateKey", my_keypair->secretkey, sizeof(my_keypair->secretkey)); } - log_bin2hex_sodium("Server PublicKey", my_keypair->publickey, sizeof(my_keypair->publickey)); + log_bin2hex_sodium(NOTICE, "Server PublicKey", my_keypair->publickey, sizeof(my_keypair->publickey)); ev_base = event_base_new(); if (ev_base == NULL) { |