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