blob: 8a8b86f05aa858a5c8f617e350395b559a266411 [file] [log] [blame]
Nagaraju Goruganti59287f02018-10-12 07:00:20 -05001#include "utils.hpp"
Patrick Williams9638afb2021-02-22 17:16:24 -06002
Nagaraju Goruganti59287f02018-10-12 07:00:20 -05003#include <arpa/inet.h>
4#include <ldap.h>
Patrick Williams9638afb2021-02-22 17:16:24 -06005#include <netdb.h>
6
7#include <cstring>
Nagaraju Goruganti59287f02018-10-12 07:00:20 -05008#include <memory>
9
10namespace phosphor
11{
12namespace ldap
13{
14
15bool 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