Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 1 | #include "utils.hpp" |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 2 | |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 3 | #include <arpa/inet.h> |
| 4 | #include <ldap.h> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 5 | #include <netdb.h> |
| 6 | |
| 7 | #include <cstring> |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 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 |