summaryrefslogtreecommitdiffstats
path: root/phosphor-ldap-config/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'phosphor-ldap-config/utils.cpp')
-rw-r--r--phosphor-ldap-config/utils.cpp52
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
OpenPOWER on IntegriCloud