xref: /openbmc/phosphor-user-manager/phosphor-ldap-config/utils.cpp (revision cf53a9466cc3aaf07d0a766d0af99e2c134183c5)
1 #include "utils.hpp"
2 
3 #include <arpa/inet.h>
4 #include <ldap.h>
5 #include <netdb.h>
6 
7 #include <cstring>
8 #include <memory>
9 
10 namespace phosphor
11 {
12 namespace ldap
13 {
14 
15 bool isValidLDAPURI(const std::string& URI, const char* scheme)
16 {
17     LDAPURLDesc* ludpp = nullptr;
18     int res = LDAP_URL_ERR_BADURL;
19     res = ldap_url_parse(URI.c_str(), &ludpp);
20 
21     auto ludppCleanupFunc = [](LDAPURLDesc* ludpp) {
22         ldap_free_urldesc(ludpp);
23     };
24     std::unique_ptr<LDAPURLDesc, decltype(ludppCleanupFunc)> ludppPtr(
25         ludpp, ludppCleanupFunc);
26 
27     if (res != LDAP_URL_SUCCESS)
28     {
29         return false;
30     }
31     if (std::strcmp(scheme, ludppPtr->lud_scheme) != 0)
32     {
33         return false;
34     }
35     addrinfo hints{};
36     addrinfo* servinfo = nullptr;
37     hints.ai_family = AF_UNSPEC;
38     hints.ai_socktype = SOCK_STREAM;
39     hints.ai_flags |= AI_CANONNAME;
40 
41     auto result = getaddrinfo(ludppPtr->lud_host, nullptr, &hints, &servinfo);
42     auto cleanupFunc = [](addrinfo* servinfo) { freeaddrinfo(servinfo); };
43     std::unique_ptr<addrinfo, decltype(cleanupFunc)> servinfoPtr(servinfo,
44                                                                  cleanupFunc);
45 
46     if (result)
47     {
48         return false;
49     }
50     return true;
51 }
52 
53 } // namespace ldap
54 } // namespace phosphor
55