summaryrefslogtreecommitdiffstats
path: root/phosphor-ldap-config/utils.cpp
blob: 7a40a3e5b492bee1398801216c1573eb4062c740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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