From a957c0d16593521a1026d34877f6e0ea8f7ea16b Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Wed, 16 Aug 2023 16:56:42 -0400 Subject: [PATCH] mail-crypt: Remove fs-crypt-settings It was only used by test-mail-global-key code. --- src/plugins/mail-crypt/Makefile.am | 17 ++-- src/plugins/mail-crypt/fs-crypt-settings.c | 29 ------ src/plugins/mail-crypt/fs-crypt-settings.h | 12 --- src/plugins/mail-crypt/mail-crypt-pluginenv.c | 88 ------------------- src/plugins/mail-crypt/test-mail-global-key.c | 65 +++++++++++--- 5 files changed, 60 insertions(+), 151 deletions(-) delete mode 100644 src/plugins/mail-crypt/fs-crypt-settings.c delete mode 100644 src/plugins/mail-crypt/fs-crypt-settings.h delete mode 100644 src/plugins/mail-crypt/mail-crypt-pluginenv.c diff --git a/src/plugins/mail-crypt/Makefile.am b/src/plugins/mail-crypt/Makefile.am index cf8abaddae..de5be4988e 100644 --- a/src/plugins/mail-crypt/Makefile.am +++ b/src/plugins/mail-crypt/Makefile.am @@ -50,9 +50,7 @@ lib05_mail_crypt_acl_plugin_la_SOURCES = \ libfs_crypt_la_SOURCES = fs-crypt.c \ crypt-settings.c \ - mail-crypt-global-key.c \ - mail-crypt-pluginenv.c \ - fs-crypt-settings.c + mail-crypt-global-key.c libfs_crypt_la_LIBADD = $(LIBDOVECOT) libfs_crypt_la_DEPENDENCIES = $(LIBDOVECOT_DEPS) @@ -70,25 +68,21 @@ test_programs = \ test-mail-key test_fs_crypt_SOURCES = \ - test-fs-crypt.c \ - fs-crypt-settings.c + test-fs-crypt.c test_fs_crypt_LDADD = $(LIBDOVECOT) \ crypt-settings.lo \ fs-crypt.lo \ - mail-crypt-global-key.lo \ - mail-crypt-pluginenv.lo + mail-crypt-global-key.lo test_fs_crypt_DEPENDENCIES = $(LIBDOVECOT_DEPS) \ crypt-settings.lo \ fs-crypt.lo \ - mail-crypt-global-key.lo \ - mail-crypt-pluginenv.lo + mail-crypt-global-key.lo test_fs_crypt_LDFLAGS = $(DOVECOT_BINARY_LDFLAGS) test_fs_crypt_CFLAGS = $(AM_CPPFLAGS) $(DOVECOT_BINARY_CFLAGS) -Dtop_builddir=\"$(top_builddir)\" test_mail_global_key_SOURCES = \ test-mail-global-key.c \ crypt-settings.c \ - fs-crypt-settings.c \ mail-crypt-global-key.c test_mail_global_key_LDADD = $(LIBDOVECOT) test_mail_global_key_DEPENDENCIES = $(LIBDOVECOT_DEPS) @@ -112,8 +106,7 @@ noinst_HEADERS = \ mail-crypt-plugin.h \ mail-crypt-common.h \ mail-crypt-global-key.h \ - mail-crypt-key.h \ - fs-crypt-settings.h + mail-crypt-key.h check-local: for bin in $(test_programs); do \ diff --git a/src/plugins/mail-crypt/fs-crypt-settings.c b/src/plugins/mail-crypt/fs-crypt-settings.c deleted file mode 100644 index dacdaa62dd..0000000000 --- a/src/plugins/mail-crypt/fs-crypt-settings.c +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */ - -#include "lib.h" -#include "buffer.h" -#include "settings-parser.h" -#include "service-settings.h" -#include "mail-storage-settings.h" -#include "fs-crypt-settings.h" - -static const struct setting_define fs_crypt_setting_defines[] = { - { .type = SET_STRLIST, .key = "plugin", - .offset = offsetof(struct fs_crypt_settings, plugin_envs) }, - - SETTING_DEFINE_LIST_END -}; - -const struct fs_crypt_settings fs_crypt_default_settings = { - .plugin_envs = ARRAY_INIT -}; - -const struct setting_parser_info fs_crypt_setting_parser_info = { - .name = "fs_crypt", - - .defines = fs_crypt_setting_defines, - .defaults = &fs_crypt_default_settings, - - .struct_size = sizeof(struct fs_crypt_settings), - .pool_offset1 = 1 + offsetof(struct fs_crypt_settings, pool), -}; diff --git a/src/plugins/mail-crypt/fs-crypt-settings.h b/src/plugins/mail-crypt/fs-crypt-settings.h deleted file mode 100644 index e1aa94b160..0000000000 --- a/src/plugins/mail-crypt/fs-crypt-settings.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef FS_CRYPT_SETTINGS_H -#define FS_CRYPT_SETTINGS_H - -struct fs_crypt_settings { - pool_t pool; - ARRAY(const char *) plugin_envs; -}; - -extern const struct setting_parser_info fs_crypt_setting_parser_info; - -#endif - diff --git a/src/plugins/mail-crypt/mail-crypt-pluginenv.c b/src/plugins/mail-crypt/mail-crypt-pluginenv.c deleted file mode 100644 index bc76a4dbc0..0000000000 --- a/src/plugins/mail-crypt/mail-crypt-pluginenv.c +++ /dev/null @@ -1,88 +0,0 @@ -/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */ -#include "lib.h" -#include "str.h" -#include "array.h" -#include "settings.h" -#include "master-service.h" -#include "master-service-settings.h" -#include "mail-crypt-common.h" -#include "mail-crypt-key.h" -#include "fs-crypt-settings.h" - -static -const char *mail_crypt_plugin_getenv(const struct fs_crypt_settings *set, - const char *name) -{ - const char *const *envs; - unsigned int i, count; - - if (set == NULL) - return NULL; - - if (!array_is_created(&set->plugin_envs)) - return NULL; - - envs = array_get(&set->plugin_envs, &count); - for (i = 0; i < count; i += 2) { - if (strcmp(envs[i], name) == 0) - return envs[i+1]; - } - return NULL; -} - -static int -mail_crypt_load_global_private_keys(const struct fs_crypt_settings *set, - const char *set_prefix, - struct mail_crypt_global_keys *global_keys, - const char **error_r) -{ - string_t *set_key = t_str_new(64); - str_append(set_key, set_prefix); - str_append(set_key, "_private_key"); - size_t prefix_len = str_len(set_key); - - unsigned int i = 1; - const char *key_data; - while ((key_data = mail_crypt_plugin_getenv(set, str_c(set_key))) != NULL) { - const char *set_pw = t_strconcat(str_c(set_key), "_password", NULL); - const char *password = mail_crypt_plugin_getenv(set, set_pw); - if (*key_data != '\0' && - mail_crypt_load_global_private_key(str_c(set_key), key_data, - set_pw, password, - global_keys, error_r) < 0) - return -1; - str_truncate(set_key, prefix_len); - str_printfa(set_key, "%u", ++i); - } - return 0; -} - -int mail_crypt_global_keys_load_pluginenv(const char *set_prefix, - struct mail_crypt_global_keys *global_keys_r, - const char **error_r) -{ - const struct fs_crypt_settings *set = - settings_get_or_fatal(master_service_get_event(master_service), - &fs_crypt_setting_parser_info); - - const char *set_key = t_strconcat(set_prefix, "_public_key", NULL); - const char *key_data = mail_crypt_plugin_getenv(set, set_key); - int ret = 0; - - mail_crypt_global_keys_init(global_keys_r); - if (key_data != NULL && *key_data != '\0') { - if (mail_crypt_load_global_public_key(set_key, key_data, - global_keys_r, error_r) < 0) - ret = -1; - } - - if (ret == 0 && - mail_crypt_load_global_private_keys(set, set_prefix, global_keys_r, - error_r) < 0) - ret = -1; - - if (ret != 0) - mail_crypt_global_keys_free(global_keys_r); - settings_free(set); - return ret; -} diff --git a/src/plugins/mail-crypt/test-mail-global-key.c b/src/plugins/mail-crypt/test-mail-global-key.c index fa3fb6cd7d..94f7eb1fc7 100644 --- a/src/plugins/mail-crypt/test-mail-global-key.c +++ b/src/plugins/mail-crypt/test-mail-global-key.c @@ -4,15 +4,18 @@ #include "test-common.h" #include "randgen.h" #include "array.h" +#include "str.h" #include "dcrypt.h" #include "hex-binary.h" #include "mail-crypt-common.h" #include "mail-crypt-key.h" -#include "mail-crypt-pluginenv.c" +struct test_settings { + ARRAY(const char *) plugin_envs; +}; -static struct fs_crypt_settings fs_set; +static struct test_settings test_set; static const char *settings[] = { "mail_crypt_global_private_key", @@ -25,11 +28,53 @@ static const char *settings[] = { "password", }; -int -mail_crypt_load_global_private_keys(const struct fs_crypt_settings *set, +static +const char *mail_crypt_plugin_getenv(const struct test_settings *set, + const char *name) +{ + const char *const *envs; + unsigned int i, count; + + if (set == NULL) + return NULL; + + if (!array_is_created(&set->plugin_envs)) + return NULL; + + envs = array_get(&set->plugin_envs, &count); + for (i = 0; i < count; i += 2) { + if (strcmp(envs[i], name) == 0) + return envs[i+1]; + } + return NULL; +} + +static int +mail_crypt_load_global_private_keys(const struct test_settings *set, const char *set_prefix, struct mail_crypt_global_keys *global_keys, - const char **error_r); + const char **error_r) +{ + string_t *set_key = t_str_new(64); + str_append(set_key, set_prefix); + str_append(set_key, "_private_key"); + size_t prefix_len = str_len(set_key); + + unsigned int i = 1; + const char *key_data; + while ((key_data = mail_crypt_plugin_getenv(set, str_c(set_key))) != NULL) { + const char *set_pw = t_strconcat(str_c(set_key), "_password", NULL); + const char *password = mail_crypt_plugin_getenv(set, set_pw); + if (*key_data != '\0' && + mail_crypt_load_global_private_key(str_c(set_key), key_data, + set_pw, password, + global_keys, error_r) < 0) + return -1; + str_truncate(set_key, prefix_len); + str_printfa(set_key, "%u", ++i); + } + return 0; +} static void test_setup(void) { @@ -41,8 +86,8 @@ static void test_setup(void) i_info("No functional dcrypt backend found - skipping tests: %s", error); test_exit(0); } - i_array_init(&fs_set.plugin_envs, 8); - array_append(&fs_set.plugin_envs, settings, N_ELEMENTS(settings)); + i_array_init(&test_set.plugin_envs, 8); + array_append(&test_set.plugin_envs, settings, N_ELEMENTS(settings)); } static void test_try_load_keys(void) @@ -60,14 +105,14 @@ static void test_try_load_keys(void) const char *set_prefix = "mail_crypt_global"; const char *set_key = t_strconcat(set_prefix, "_public_key", NULL); - const char *key_data = mail_crypt_plugin_getenv(&fs_set, set_key); + const char *key_data = mail_crypt_plugin_getenv(&test_set, set_key); test_assert(key_data != NULL); if (key_data != NULL) { test_assert(mail_crypt_load_global_public_key(set_key, key_data, &keys, &error) == 0); - test_assert(mail_crypt_load_global_private_keys(&fs_set, set_prefix, + test_assert(mail_crypt_load_global_private_keys(&test_set, set_prefix, &keys, &error) == 0); /* did we get two private keys? */ test_assert(array_count(&keys.private_keys) == 2); @@ -111,7 +156,7 @@ static void test_empty_keyset(void) static void test_teardown(void) { - array_free(&fs_set.plugin_envs); + array_free(&test_set.plugin_envs); dcrypt_deinitialize(); } -- 2.47.3