From 12cbb9f07e1726346ad2724ad1c5c8a0e3ed82eb Mon Sep 17 00:00:00 2001 From: S-A-L13 Date: Mon, 26 Feb 2024 15:47:57 +0100 Subject: [PATCH 1/6] Added config fields for mail prefix Mail prefix is needed to filter mailidentities. For example suche set by Exchange in the ProxyAddresses field, which can hold different address types --- plugins/ldap-identities/LdapConfig.php | 3 +++ plugins/ldap-identities/index.php | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/ldap-identities/LdapConfig.php b/plugins/ldap-identities/LdapConfig.php index 8cebd8b2c..c55f88bcb 100644 --- a/plugins/ldap-identities/LdapConfig.php +++ b/plugins/ldap-identities/LdapConfig.php @@ -8,6 +8,7 @@ class LdapConfig public const CONFIG_SERVER = "server"; public const CONFIG_PROTOCOL_VERSION = "server_version"; public const CONFIG_STARTTLS = "starttls"; + public const CONFIG_MAIL_PREFIX = "mail_prefix"; public const CONFIG_BIND_USER = "bind_user"; public const CONFIG_BIND_PASSWORD = "bind_password"; @@ -30,6 +31,7 @@ class LdapConfig public $server; public $protocol; public $starttls; + public $mail_prefix; public $bind_user; public $bind_password; public $user_base; @@ -51,6 +53,7 @@ class LdapConfig $ldap->server = trim($config->Get("plugin", self::CONFIG_SERVER)); $ldap->protocol = (int)trim($config->Get("plugin", self::CONFIG_PROTOCOL_VERSION, 3)); $ldap->starttls = (bool)trim($config->Get("plugin", self::CONFIG_STARTTLS)); + $ldap->mail_prefix = trim($config->Get("plugin", self::CONFIG_MAIL_PREFIX)); $ldap->bind_user = trim($config->Get("plugin", self::CONFIG_BIND_USER)); $ldap->bind_password = trim($config->Get("plugin", self::CONFIG_BIND_PASSWORD)); $ldap->user_base = trim($config->Get("plugin", self::CONFIG_USER_BASE)); diff --git a/plugins/ldap-identities/index.php b/plugins/ldap-identities/index.php index 19e7a1503..121d0261b 100644 --- a/plugins/ldap-identities/index.php +++ b/plugins/ldap-identities/index.php @@ -57,13 +57,19 @@ class LdapIdentitiesPlugin extends AbstractPlugin ->SetLabel("LDAP Protocol Version") ->SetType(PluginPropertyType::SELECTION) ->SetDefaultValue([2, 3]), - + Property::NewInstance(LdapConfig::CONFIG_STARTTLS) ->SetLabel("Use StartTLS") ->SetType(PluginPropertyType::BOOL) ->SetDescription("Whether or not to use TLS encrypted connection") ->SetDefaultValue(true), + Property::NewInstance(LdapConfig::CONFIG_MAIL_PREFIX) + ->SetLabel("Email prefix") + ->SetType(PluginPropertyType::STRING) + ->SetDescription("Only addresses with this prefix will be used as identity. The prefix is removed from the identity list.\nThis is useful for example to import identities from Exchange, which stores mail addresses in the ProxyAddresses attribut of Active Directory with \"smtp:\" as prefix. \(e.g. \"smtp:john.doe@topsecret.info\"\)\n-> To use addresses set by Exchange use \"smtp:\" as prefix.") + ->SetDefaultValue(""), + Property::NewInstance(LdapConfig::CONFIG_BIND_USER) ->SetLabel("Bind User DN") ->SetDescription("The user to use for binding to the LDAP server. Should be a DN or RDN. Leave empty for anonymous bind") From ebf75405544793d4ac98220b55e132bac28ae07b Mon Sep 17 00:00:00 2001 From: S-A-L13 Date: Tue, 27 Feb 2024 12:04:07 +0100 Subject: [PATCH 2/6] Added function CleanupMailAddresses Added function (and additional code to start it) that helps to clean up the mail addresses array. It removes the prefix (if set in config) and removes all addresses with no or a different prefix. --- plugins/ldap-identities/LdapIdentities.php | 52 ++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/plugins/ldap-identities/LdapIdentities.php b/plugins/ldap-identities/LdapIdentities.php index 4f14b1bee..cade58ed0 100644 --- a/plugins/ldap-identities/LdapIdentities.php +++ b/plugins/ldap-identities/LdapIdentities.php @@ -69,7 +69,8 @@ class LdapIdentities implements IIdentities $this->config->user_base, $this->config->user_objectclass, $this->config->user_field_name, - $this->config->user_field_mail + $this->config->user_field_mail, + $this->config->mail_prefix ); } catch (LdapException $e) { return []; // exceptions are only thrown from the handleerror function that does logging already @@ -104,7 +105,8 @@ class LdapIdentities implements IIdentities $this->config->group_base, $this->config->group_objectclass, $this->config->group_field_name, - $this->config->group_field_mail + $this->config->group_field_mail, + $this->config->mail_prefix ); } catch (LdapException $e) { return []; // exceptions are only thrown from the handleerror function that does logging already @@ -241,7 +243,7 @@ class LdapIdentities implements IIdentities * @return LdapResult[] * @throws LdapException */ - private function FindLdapResults(string $searchField, string $searchValue, string $searchBase, string $objectClass, string $nameField, string $mailField): array + private function FindLdapResults(string $searchField, string $searchValue, string $searchBase, string $objectClass, string $nameField, string $mailField, string $mailPrefix): array { $this->EnsureBound(); @@ -261,6 +263,8 @@ class LdapIdentities implements IIdentities return []; } + $entries = $this->CleanupMailAddresses($entries, $mailField, $mailPrefix); + $results = []; for ($i = 0; $i < $entries["count"]; $i++) { $entry = $entries[$i]; @@ -276,6 +280,48 @@ class LdapIdentities implements IIdentities return $results; } + // Function CleanupMailAddresses(): If a prefix is given this function removes addresses without / with the wrong prefix and then the prefix itself from all remaining values. + // This is usefull for example for importing Active Directory LDAP entry "proxyAddresses" which can hold different address types with prefixes like "X400:", "smtp:" "sip:" and others. + + /** + @param array $entries + @param string $mailField + @paraam string $mailPrefix + @return array + */ + private function CleanupMailAddresses(array $entries, string $mailField, string $mailPrefix) { + if (!empty($mailPrefix)) { + for ($i = 0; $i < $entries["count"]; $i++) { + // Remove addresses without the given prefix + $entries[$i]["$mailField"] = array_filter($entries[$i]["$mailField"], + function($prefixMail) { + // $mailPrefix can't be used here, because it's nailed to the CleanupMailAddresses function and can't be passed to the array_filter function afaik. + // Ideas to avoid this are welcome. + if (stripos($prefixMail, $this->config->mail_prefix) === 0) { + return TRUE; + } + return FALSE; + } + ); + // Set "count" to new value + $newcount = count($entries[$i]["$mailField"]); + if (array_key_exists("count", $entries[$i]["$mailField"])) { + $newcount = $newcount - 1; + } + $entries[$i]["$mailField"]["count"] = $newcount; + + // Remove the prefix + for ($j = 0; $j < $entries[$i]["$mailField"]["count"]; $j++) { + $mailPrefixLen = mb_strlen($mailPrefix); + $entries[$i]["$mailField"][$j] = substr($entries[$i]["$mailField"][$j], $mailPrefixLen); + } + } + } + + return $entries; + } + + /** * @param array $entry * @param string $attribute From f4554374bd7e4e24ff6f20c7798501652df3c812 Mon Sep 17 00:00:00 2001 From: S-A-L13 Date: Tue, 27 Feb 2024 13:10:53 +0100 Subject: [PATCH 3/6] Minor changes to match coding style of original coder --- plugins/ldap-identities/LdapIdentities.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/ldap-identities/LdapIdentities.php b/plugins/ldap-identities/LdapIdentities.php index cade58ed0..a354487a5 100644 --- a/plugins/ldap-identities/LdapIdentities.php +++ b/plugins/ldap-identities/LdapIdentities.php @@ -289,12 +289,14 @@ class LdapIdentities implements IIdentities @paraam string $mailPrefix @return array */ - private function CleanupMailAddresses(array $entries, string $mailField, string $mailPrefix) { + private function CleanupMailAddresses(array $entries, string $mailField, string $mailPrefix) + { if (!empty($mailPrefix)) { for ($i = 0; $i < $entries["count"]; $i++) { // Remove addresses without the given prefix $entries[$i]["$mailField"] = array_filter($entries[$i]["$mailField"], - function($prefixMail) { + function($prefixMail) + { // $mailPrefix can't be used here, because it's nailed to the CleanupMailAddresses function and can't be passed to the array_filter function afaik. // Ideas to avoid this are welcome. if (stripos($prefixMail, $this->config->mail_prefix) === 0) { @@ -321,7 +323,6 @@ class LdapIdentities implements IIdentities return $entries; } - /** * @param array $entry * @param string $attribute From 0763f3d1c6559f627237c8582baa1048303d399c Mon Sep 17 00:00:00 2001 From: S-A-L13 Date: Tue, 27 Feb 2024 13:22:38 +0100 Subject: [PATCH 4/6] Removed wrong escapes --- plugins/ldap-identities/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ldap-identities/index.php b/plugins/ldap-identities/index.php index 121d0261b..d56df3ca6 100644 --- a/plugins/ldap-identities/index.php +++ b/plugins/ldap-identities/index.php @@ -67,7 +67,7 @@ class LdapIdentitiesPlugin extends AbstractPlugin Property::NewInstance(LdapConfig::CONFIG_MAIL_PREFIX) ->SetLabel("Email prefix") ->SetType(PluginPropertyType::STRING) - ->SetDescription("Only addresses with this prefix will be used as identity. The prefix is removed from the identity list.\nThis is useful for example to import identities from Exchange, which stores mail addresses in the ProxyAddresses attribut of Active Directory with \"smtp:\" as prefix. \(e.g. \"smtp:john.doe@topsecret.info\"\)\n-> To use addresses set by Exchange use \"smtp:\" as prefix.") + ->SetDescription("Only addresses with this prefix will be used as identity. The prefix is removed from the identity list.\nThis is useful for example to import identities from Exchange, which stores mail addresses in the ProxyAddresses attribut of Active Directory with \"smtp:\" as prefix. (e.g. \"smtp:john.doe@topsecret.info\")\n-> To use addresses set by Exchange use \"smtp:\" as prefix.") ->SetDefaultValue(""), Property::NewInstance(LdapConfig::CONFIG_BIND_USER) From 20f6dea345b665e272497f28b7f00fcf000b0bf0 Mon Sep 17 00:00:00 2001 From: S-A-L13 Date: Tue, 27 Feb 2024 13:46:02 +0100 Subject: [PATCH 5/6] minor changes --- plugins/ldap-identities/LdapIdentities.php | 72 +++++++++++----------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/plugins/ldap-identities/LdapIdentities.php b/plugins/ldap-identities/LdapIdentities.php index a354487a5..4cc3e69d0 100644 --- a/plugins/ldap-identities/LdapIdentities.php +++ b/plugins/ldap-identities/LdapIdentities.php @@ -280,49 +280,49 @@ class LdapIdentities implements IIdentities return $results; } - // Function CleanupMailAddresses(): If a prefix is given this function removes addresses without / with the wrong prefix and then the prefix itself from all remaining values. - // This is usefull for example for importing Active Directory LDAP entry "proxyAddresses" which can hold different address types with prefixes like "X400:", "smtp:" "sip:" and others. + // Function CleanupMailAddresses(): If a prefix is given this function removes addresses without / with the wrong prefix and then the prefix itself from all remaining values. + // This is usefull for example for importing Active Directory LDAP entry "proxyAddresses" which can hold different address types with prefixes like "X400:", "smtp:" "sip:" and others. - /** - @param array $entries - @param string $mailField - @paraam string $mailPrefix - @return array - */ - private function CleanupMailAddresses(array $entries, string $mailField, string $mailPrefix) - { - if (!empty($mailPrefix)) { - for ($i = 0; $i < $entries["count"]; $i++) { - // Remove addresses without the given prefix - $entries[$i]["$mailField"] = array_filter($entries[$i]["$mailField"], - function($prefixMail) - { - // $mailPrefix can't be used here, because it's nailed to the CleanupMailAddresses function and can't be passed to the array_filter function afaik. - // Ideas to avoid this are welcome. - if (stripos($prefixMail, $this->config->mail_prefix) === 0) { - return TRUE; - } - return FALSE; + /** + @param array $entries + @param string $mailField + @paraam string $mailPrefix + @return array + */ + private function CleanupMailAddresses(array $entries, string $mailField, string $mailPrefix) + { + if (!empty($mailPrefix)) { + for ($i = 0; $i < $entries["count"]; $i++) { + // Remove addresses without the given prefix + $entries[$i]["$mailField"] = array_filter($entries[$i]["$mailField"], + function($prefixMail) + { + // $mailPrefix can't be used here, because it's nailed to the CleanupMailAddresses function and can't be passed to the array_filter function afaik. + // Ideas to avoid this are welcome. + if (stripos($prefixMail, $this->config->mail_prefix) === 0) { + return TRUE; } - ); - // Set "count" to new value - $newcount = count($entries[$i]["$mailField"]); - if (array_key_exists("count", $entries[$i]["$mailField"])) { - $newcount = $newcount - 1; - } - $entries[$i]["$mailField"]["count"] = $newcount; + return FALSE; + } + ); + // Set "count" to new value + $newcount = count($entries[$i]["$mailField"]); + if (array_key_exists("count", $entries[$i]["$mailField"])) { + $newcount = $newcount - 1; + } + $entries[$i]["$mailField"]["count"] = $newcount; - // Remove the prefix - for ($j = 0; $j < $entries[$i]["$mailField"]["count"]; $j++) { - $mailPrefixLen = mb_strlen($mailPrefix); - $entries[$i]["$mailField"][$j] = substr($entries[$i]["$mailField"][$j], $mailPrefixLen); - } + // Remove the prefix + for ($j = 0; $j < $entries[$i]["$mailField"]["count"]; $j++) { + $mailPrefixLen = mb_strlen($mailPrefix); + $entries[$i]["$mailField"][$j] = substr($entries[$i]["$mailField"][$j], $mailPrefixLen); } } - - return $entries; } + return $entries; + } + /** * @param array $entry * @param string $attribute From 31d9e9fd1b011fbecf37cd4284b5a89c9af59d91 Mon Sep 17 00:00:00 2001 From: S-A-L13 Date: Tue, 27 Feb 2024 14:30:57 +0100 Subject: [PATCH 6/6] Changed version to 2.3 --- plugins/ldap-identities/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/ldap-identities/index.php b/plugins/ldap-identities/index.php index d56df3ca6..4ac3f2faf 100644 --- a/plugins/ldap-identities/index.php +++ b/plugins/ldap-identities/index.php @@ -8,10 +8,10 @@ class LdapIdentitiesPlugin extends AbstractPlugin { const NAME = 'LDAP Identities', - VERSION = '2.2', + VERSION = '2.3', AUTHOR = 'FWest98', URL = 'https://github.com/FWest98', - RELEASE = '2024-02-22', + RELEASE = '2024-02-27', REQUIRED = '2.20.0', CATEGORY = 'Accounts', DESCRIPTION = 'Adds functionality to import account identities from LDAP.';