From 3c1d90d7fa99cc1996151f51674835722390cce0 Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Wed, 1 Mar 2023 14:27:21 +0100 Subject: [PATCH] lib-imap: Add function to log IMAP ID parameters --- src/lib-imap/imap-id.c | 27 +++++++++++++++++++++++++++ src/lib-imap/imap-id.h | 14 ++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/lib-imap/imap-id.c b/src/lib-imap/imap-id.c index 7916d1a469..39f00c84bb 100644 --- a/src/lib-imap/imap-id.c +++ b/src/lib-imap/imap-id.c @@ -13,6 +13,9 @@ # include #endif +/* Limit the allowed characters that an IMAP ID parameter might have. */ +#define IMAP_ID_KEY_ACCEPT_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_-" + static struct utsname utsname_result; static bool utsname_set = FALSE; @@ -167,3 +170,27 @@ const char *imap_id_args_get_log_reply(const struct imap_arg *args, } return str_len(reply) == 0 ? NULL : str_c(reply); } + +void +imap_id_add_log_entry(struct imap_id_log_entry *log_entry, const char *key, + const char *value) +{ + if (str_len(log_entry->reply) > 0) + str_append(log_entry->reply, ", "); + str_append(log_entry->reply, key); + str_append_c(log_entry->reply, '='); + str_append(log_entry->reply, value == NULL ? "NIL" : value); + + const char *l_key = t_str_lcase(key); + const char *prefixed_key; + const char *val_str = value == NULL ? "NIL" : value; + if (strspn(l_key, IMAP_ID_KEY_ACCEPT_CHARS) == strlen(l_key)) { + prefixed_key = t_strconcat("id_param_", l_key, NULL); + event_add_str(log_entry->event, prefixed_key, val_str); + } else { + prefixed_key = t_strdup_printf("id_invalid%u", + ++log_entry->invalid_key_id_counter); + const char *key_val = t_strconcat(key, " ", val_str, NULL); + event_add_str(log_entry->event, prefixed_key, key_val); + } +} diff --git a/src/lib-imap/imap-id.h b/src/lib-imap/imap-id.h index 853153aac7..8dc3916d90 100644 --- a/src/lib-imap/imap-id.h +++ b/src/lib-imap/imap-id.h @@ -3,6 +3,16 @@ struct imap_arg; +struct imap_id_log_entry { + struct event *event; + string_t *reply; + /* Enumerator variable that increments per ID parameter with invalid + characters. For convenience reasons the value is pre-incremented, + under the assumption that the value is initialized to 0, it will + start enumerating the value with 1. */ + unsigned int invalid_key_id_counter; +}; + /* RFC 2971 says keys are max. 30 octets */ #define IMAP_ID_KEY_MAX_LEN 30 @@ -15,5 +25,9 @@ const char *imap_id_args_get_log_reply(const struct imap_arg *args, /* Append [, ]key=value to the reply sanitized. */ void imap_id_log_reply_append(string_t *reply, const char *key, const char *value); +/* Format the IMAP ID parameters into string-fields of the given event, and + into a printable log message. */ +void imap_id_add_log_entry(struct imap_id_log_entry *log_entry, + const char *key, const char *value); #endif -- 2.47.3