blob: 0e5fbfc6eed99914231b3aefc69fa6c9e008cf47 [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
Nan Zhou78d85042022-08-29 17:50:22 +000015bool isValidLDAPURI(const std::string& uri, const char* scheme)
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050016{
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060017 // Return false if the user tries to configure port 0
18 // This check is not done in line 42, because ldap_url_parse
19 // method internally converts port 0 to ldap port 389 and it
20 // will always return true (thus allowing the user to
21 // configure port 0)
22
George Liu06152602025-08-25 14:19:15 +080023 if (uri.ends_with(":0"))
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060024 {
25 return false;
26 }
27
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050028 LDAPURLDesc* ludpp = nullptr;
29 int res = LDAP_URL_ERR_BADURL;
Nan Zhou78d85042022-08-29 17:50:22 +000030 res = ldap_url_parse(uri.c_str(), &ludpp);
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050031
32 auto ludppCleanupFunc = [](LDAPURLDesc* ludpp) {
33 ldap_free_urldesc(ludpp);
34 };
35 std::unique_ptr<LDAPURLDesc, decltype(ludppCleanupFunc)> ludppPtr(
36 ludpp, ludppCleanupFunc);
37
38 if (res != LDAP_URL_SUCCESS)
39 {
40 return false;
41 }
42 if (std::strcmp(scheme, ludppPtr->lud_scheme) != 0)
43 {
44 return false;
45 }
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060046 if (ludppPtr->lud_port < 0 || ludppPtr->lud_port > 65536)
47 {
48 return false;
49 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050050 addrinfo hints{};
51 addrinfo* servinfo = nullptr;
52 hints.ai_family = AF_UNSPEC;
53 hints.ai_socktype = SOCK_STREAM;
54 hints.ai_flags |= AI_CANONNAME;
55
56 auto result = getaddrinfo(ludppPtr->lud_host, nullptr, &hints, &servinfo);
57 auto cleanupFunc = [](addrinfo* servinfo) { freeaddrinfo(servinfo); };
Patrick Williams16c2b682024-08-16 15:20:56 -040058 std::unique_ptr<addrinfo, decltype(cleanupFunc)> servinfoPtr(
59 servinfo, cleanupFunc);
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050060
61 if (result)
62 {
63 return false;
64 }
65 return true;
66}
67
68} // namespace ldap
69} // namespace phosphor