From: Marco Bettini Date: Fri, 20 Sep 2024 14:04:25 +0000 (+0000) Subject: lib-ldap: ldap_client_settings - Add settings definitions and validation X-Git-Tag: 2.4.0~141 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8d8746851da7ea275db512f1a320bd97de93f9f6;p=thirdparty%2Fdovecot%2Fcore.git lib-ldap: ldap_client_settings - Add settings definitions and validation --- diff --git a/src/lib-ldap/Makefile.am b/src/lib-ldap/Makefile.am index 63b3d19c4d..871256f2c3 100644 --- a/src/lib-ldap/Makefile.am +++ b/src/lib-ldap/Makefile.am @@ -4,6 +4,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/src/lib \ -I$(top_srcdir)/src/lib-test \ -I$(top_srcdir)/src/lib-settings \ + -I$(top_srcdir)/src/lib-var-expand \ -I$(top_srcdir)/src/lib-master \ -I$(top_srcdir)/src/lib-ssl-iostream \ $(LDAP_CFLAGS) diff --git a/src/lib-ldap/ldap-settings.c b/src/lib-ldap/ldap-settings.c index f32f380050..f02126fe28 100644 --- a/src/lib-ldap/ldap-settings.c +++ b/src/lib-ldap/ldap-settings.c @@ -1 +1,82 @@ /* Copyright (c) 2024 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "str.h" +#include "settings.h" +#include "ldap-settings.h" + +#undef DEF +#undef DEFN +#define DEF(type, name) \ + SETTING_DEFINE_STRUCT_##type("ldap_"#name, name, struct ldap_client_settings) +#define DEFN(type, field, name) \ + SETTING_DEFINE_STRUCT_##type(#name, field, struct ldap_client_settings) +static const struct setting_define ldap_client_setting_defines[] = { + DEF(STR, uris), + DEF(STR, auth_dn), + DEF(STR, auth_dn_password), + DEFN(TIME, timeout_secs, ldap_timeout), + DEFN(TIME, max_idle_time_secs, ldap_max_idle_time), + DEF(UINT, debug_level), + DEF(BOOL, require_ssl), + DEF(BOOL, starttls), + SETTING_DEFINE_LIST_END +}; + +static const struct ldap_client_settings ldap_client_default_settings = { + .uris = "", + .auth_dn = "", + .auth_dn_password = "", + .timeout_secs = 30, + .max_idle_time_secs = 0, + .debug_level = 0, + .require_ssl = FALSE, + .starttls = FALSE, +}; + +const struct setting_parser_info ldap_client_setting_parser_info = { + .name = "ldap", + + .defines = ldap_client_setting_defines, + .defaults = &ldap_client_default_settings, + + .struct_size = sizeof(struct ldap_client_settings), + .pool_offset1 = 1 + offsetof(struct ldap_client_settings, pool), +}; + +static int +ldap_client_settings_postcheck(struct ldap_client_settings *set, const char **error_r) +{ + if (*set->uris == '\0') { + *error_r = "ldap_uris not set"; + return -1; + } + + if (*set->auth_dn == '\0') { + *error_r = "auth_dn not set"; + return -1; + } + + if (*set->auth_dn_password == '\0') { + *error_r = "auth_dn_password not set"; + return -1; + } + + return 0; +} + +int ldap_client_settings_get(struct event *event, + const struct ldap_client_settings **set_r, + const char **error_r) +{ + struct ldap_client_settings *set = NULL; + if (settings_get(event, &ldap_client_setting_parser_info, 0, &set, error_r) < 0 || + ldap_client_settings_postcheck(set, error_r) < 0) { + settings_free(set); + return -1; + } + + *set_r = set; + *error_r = NULL; + return 0; +} diff --git a/src/lib-ldap/ldap-settings.h b/src/lib-ldap/ldap-settings.h index f55b54d75f..502ef769e8 100644 --- a/src/lib-ldap/ldap-settings.h +++ b/src/lib-ldap/ldap-settings.h @@ -18,4 +18,10 @@ struct ldap_client_settings { const struct ssl_iostream_settings *ssl_ioset; }; +extern const struct setting_parser_info ldap_client_setting_parser_info; + +int ldap_client_settings_get(struct event *event, + const struct ldap_client_settings **set_r, + const char **error_r); + #endif