| 52 | }; |
| 53 | |
| 54 | LDAPHandler::LDAPHandler() |
| 55 | : _parser(LDAPHandler::_configValues, " ", /*case_sensitive?*/false, /*validate_keys?*/true), |
| 56 | _ldap_file("/etc/usbguard/usbguard-ldap.conf"), |
| 57 | _updateInterval(1 * 60 * 60 * 1000) /* default update interval is 1h*/ |
| 58 | { |
| 59 | USBGUARD_LOG(Info) << "LDAPHandler Loading..."; |
| 60 | char array[HOST_NAME_MAX]; |
| 61 | int rc = gethostname(array, HOST_NAME_MAX); |
| 62 | |
| 63 | if (rc != -1) { |
| 64 | _hostname = array; |
| 65 | } |
| 66 | else { |
| 67 | /*TODO maybe some option for setting hostname explicitly |
| 68 | * without hostname LDAP query will not work! |
| 69 | */ |
| 70 | USBGUARD_LOG(Debug) << "Cannot get hostname"; |
| 71 | } |
| 72 | |
| 73 | USBGUARD_LOG(Debug) << "Hostname is: " << _hostname; |
| 74 | parseConf(_ldap_file); |
| 75 | validateConf(); |
| 76 | LDAP* ptr = nullptr; |
| 77 | |
| 78 | if (ldap_initialize(&ptr, _parsedOptions["URI"].c_str()) != LDAP_SUCCESS) { |
| 79 | throw ErrnoException("LDAPHandler initialization", "ldap_initialize", errno); |
| 80 | } |
| 81 | |
| 82 | _ldap_ptr.reset(ptr); |
| 83 | int version = LDAP_VERSION3; |
| 84 | ldap_set_option(_ldap_ptr.get(), LDAP_OPT_PROTOCOL_VERSION, &version); |
| 85 | rc = LDAP_SUCCESS; |
| 86 | struct berval passwd; |
| 87 | passwd.bv_val = strdup(_parsedOptions["ROOTPW"].c_str()); |
| 88 | passwd.bv_len = _parsedOptions["ROOTPW"].length(); |
| 89 | rc = ldap_sasl_bind_s(_ldap_ptr.get(), _parsedOptions["ROOTDN"].c_str(), LDAP_SASL_SIMPLE, |
| 90 | &passwd, nullptr, nullptr, nullptr); |
| 91 | free(passwd.bv_val); |
| 92 | |
| 93 | if (rc != LDAP_SUCCESS) { |
| 94 | throw Exception("LDAPHandler initialization", "ldap_sasl_bind_s", ldap_err2string(rc)); |
| 95 | } |
| 96 | |
| 97 | USBGUARD_LOG(Info) << "LDAPHandler Loaded"; |
| 98 | } |
| 99 | |
| 100 | LDAPHandler::~LDAPHandler() |
| 101 | { |
nothing calls this directly
no test coverage detected