diff options
author | Nagaraju Goruganti <ngorugan@in.ibm.com> | 2018-10-12 07:00:20 -0500 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2018-11-20 02:11:46 +0000 |
commit | 59287f090c9ea371a6d7f9c151f26c46a068a0b3 (patch) | |
tree | 28757865672ab23acbeb19fb756a1da48c2321b6 /phosphor-ldap-config/utils.cpp | |
parent | db60f5847bc89e96fbee5eb504726c11382973b8 (diff) | |
download | phosphor-user-manager-59287f090c9ea371a6d7f9c151f26c46a068a0b3.tar.gz phosphor-user-manager-59287f090c9ea371a6d7f9c151f26c46a068a0b3.zip |
phosphor-ldap-conf: validate LDAP Server URI
Validates given URI.
Also updates secureLDAP property based on given URI. If URI is of LDAPS type,
secureLDAP is set to true, else it is set to false.
Change-Id: If96495c01a8bd911d255267ffbbbff7f28fa070b
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
Diffstat (limited to 'phosphor-ldap-config/utils.cpp')
-rw-r--r-- | phosphor-ldap-config/utils.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/phosphor-ldap-config/utils.cpp b/phosphor-ldap-config/utils.cpp new file mode 100644 index 0000000..7a40a3e --- /dev/null +++ b/phosphor-ldap-config/utils.cpp @@ -0,0 +1,52 @@ +#include "utils.hpp" +#include <cstring> +#include <netdb.h> +#include <arpa/inet.h> +#include <ldap.h> +#include <memory> + +namespace phosphor +{ +namespace ldap +{ + +bool isValidLDAPURI(const std::string& URI, const char* scheme) +{ + LDAPURLDesc* ludpp = nullptr; + int res = LDAP_URL_ERR_BADURL; + res = ldap_url_parse(URI.c_str(), &ludpp); + + auto ludppCleanupFunc = [](LDAPURLDesc* ludpp) { + ldap_free_urldesc(ludpp); + }; + std::unique_ptr<LDAPURLDesc, decltype(ludppCleanupFunc)> ludppPtr( + ludpp, ludppCleanupFunc); + + if (res != LDAP_URL_SUCCESS) + { + return false; + } + if (std::strcmp(scheme, ludppPtr->lud_scheme) != 0) + { + return false; + } + addrinfo hints{}; + addrinfo* servinfo = nullptr; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags |= AI_CANONNAME; + + auto result = getaddrinfo(ludppPtr->lud_host, nullptr, &hints, &servinfo); + auto cleanupFunc = [](addrinfo* servinfo) { freeaddrinfo(servinfo); }; + std::unique_ptr<addrinfo, decltype(cleanupFunc)> servinfoPtr(servinfo, + cleanupFunc); + + if (result) + { + return false; + } + return true; +} + +} // namespace ldap +} // namespace phosphor |