]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
apparmor: Convert plugin settings to regular settings
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 28 Aug 2024 00:23:28 +0000 (03:23 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 17 Jan 2025 08:40:00 +0000 (10:40 +0200)
src/plugins/apparmor/Makefile.am
src/plugins/apparmor/apparmor-plugin.c

index 510053bbfff000c7640b5e3436e9b0cc1db47295..a6b13d7160749c45d247ee4f586245a7c5c60b9e 100644 (file)
@@ -1,5 +1,6 @@
 AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
+       -I$(top_srcdir)/src/lib-settings \
        -I$(top_srcdir)/src/lib-mail \
        -I$(top_srcdir)/src/lib-index \
        -I$(top_srcdir)/src/lib-storage
index 410c652ac65ef0eb70417ca96b69cc3499a9750f..1e6447352fd0540fd571d4cb0078ce386d32675a 100644 (file)
@@ -4,13 +4,13 @@
 #include "array.h"
 #include "module-dir.h"
 #include "randgen.h"
+#include "settings.h"
+#include "settings-parser.h"
 #include "mail-user.h"
 #include "mail-storage-private.h"
 #include "mail-storage-hooks.h"
 #include <sys/apparmor.h>
 
-#define APPARMOR_PLUGIN_SETTING_HAT_PREFIX "apparmor_hat"
-
 const char *apparmor_plugin_version = DOVECOT_ABI_VERSION;
 
 /* hooks into user creation and deinit, will try to use
@@ -27,6 +27,34 @@ struct apparmor_mail_user {
        unsigned long token;
 };
 
+struct apparmor_settings {
+       pool_t pool;
+
+       ARRAY_TYPE(const_string) apparmor_hats;
+};
+
+#undef DEF
+#define DEF(type, name) \
+       SETTING_DEFINE_STRUCT_##type(#name, name, struct apparmor_settings)
+static const struct setting_define apparmor_setting_defines[] = {
+       DEF(BOOLLIST, apparmor_hats),
+
+       SETTING_DEFINE_LIST_END
+};
+static const struct apparmor_settings apparmor_default_settings = {
+       .apparmor_hats = ARRAY_INIT,
+};
+
+const struct setting_parser_info apparmor_setting_parser_info = {
+       .name = "apparmor",
+
+       .defines = apparmor_setting_defines,
+       .defaults = &apparmor_default_settings,
+
+       .struct_size = sizeof(struct apparmor_settings),
+       .pool_offset1 = 1 + offsetof(struct apparmor_settings, pool),
+};
+
 void apparmor_plugin_init(struct module*);
 void apparmor_plugin_deinit(void);
 
@@ -60,22 +88,18 @@ static void apparmor_mail_user_created(struct mail_user *user)
 {
        struct mail_user_vfuncs *v = user->vlast;
        struct apparmor_mail_user *auser;
-       ARRAY_TYPE(const_string) hats;
-       /* see if we can find any hats */
-       const char *hat =
-               mail_user_plugin_getenv(user, APPARMOR_PLUGIN_SETTING_HAT_PREFIX);
-       if (hat == NULL)
-               return;
+       const struct apparmor_settings *set;
+       const char *error;
 
-       t_array_init(&hats, 8);
-       array_push_back(&hats, &hat);
-       for(unsigned int i = 2;; i++) {
-               hat = mail_user_plugin_getenv(user, t_strdup_printf("%s%u",
-                               APPARMOR_PLUGIN_SETTING_HAT_PREFIX, i));
-               if (hat == NULL) break;
-               array_push_back(&hats, &hat);
+       if (settings_get(user->event, &apparmor_setting_parser_info, 0,
+                        &set, &error) < 0) {
+               user->error = p_strdup(user->pool, error);
+               return;
+       }
+       if (array_is_empty(&set->apparmor_hats)) {
+               settings_free(set);
+               return;
        }
-       array_append_zero(&hats);
 
        /* we got hat(s) to try */
        auser = p_new(user->pool, struct apparmor_mail_user, 1);
@@ -88,10 +112,12 @@ static void apparmor_mail_user_created(struct mail_user *user)
        random_fill(&auser->token, sizeof(auser->token));
 
        /* try change hat */
-       if (aa_change_hatv(array_front_modifiable(&hats), auser->token) < 0) {
+       const char *const *hats = settings_boollist_get(&set->apparmor_hats);
+       if (aa_change_hatv((const char **)hats, auser->token) < 0) {
                i_fatal("aa_change_hatv(%s) failed: %m",
-                       t_array_const_string_join(&hats, ","));
+                       t_array_const_string_join(&set->apparmor_hats, ","));
        }
+       settings_free(set);
 
        apparmor_log_current_context(user);
 }