]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-imap: Add function to log IMAP ID parameters
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Wed, 1 Mar 2023 13:27:21 +0000 (14:27 +0100)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 31 Mar 2023 05:54:46 +0000 (05:54 +0000)
src/lib-imap/imap-id.c
src/lib-imap/imap-id.h

index 7916d1a46910cc3d6f282f6f786140eab09adb03..39f00c84bbea17d08f53bb26ca09e9987ed2066e 100644 (file)
@@ -13,6 +13,9 @@
 #  include <sys/utsname.h>
 #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);
+       }
+}
index 853153aac7405734df6ae429f79c4ad96fd149a3..8dc3916d9045b4eef3128f307756313901dcbf8e 100644 (file)
@@ -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