Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 1 | #include "ldap_config_mgr.hpp" |
Ratan Gupta | 37fb3fe | 2019-04-13 12:54:18 +0530 | [diff] [blame] | 2 | #include "ldap_config.hpp" |
| 3 | #include "ldap_config_serialize.hpp" |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 4 | |
| 5 | #include "utils.hpp" |
| 6 | #include <filesystem> |
| 7 | #include <fstream> |
| 8 | #include <sstream> |
| 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace ldap |
| 13 | { |
| 14 | |
| 15 | constexpr auto nscdService = "nscd.service"; |
| 16 | constexpr auto LDAPscheme = "ldap"; |
| 17 | constexpr auto LDAPSscheme = "ldaps"; |
| 18 | |
| 19 | using namespace phosphor::logging; |
| 20 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 21 | namespace fs = std::filesystem; |
| 22 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 23 | |
| 24 | using Line = std::string; |
| 25 | using Key = std::string; |
| 26 | using Val = std::string; |
| 27 | using ConfigInfo = std::map<Key, Val>; |
| 28 | |
| 29 | void ConfigMgr::startOrStopService(const std::string& service, bool start) |
| 30 | { |
| 31 | if (start) |
| 32 | { |
| 33 | restartService(service); |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | stopService(service); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void ConfigMgr::restartService(const std::string& service) |
| 42 | { |
| 43 | try |
| 44 | { |
| 45 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 46 | SYSTEMD_INTERFACE, "RestartUnit"); |
| 47 | method.append(service.c_str(), "replace"); |
| 48 | bus.call_noreply(method); |
| 49 | } |
| 50 | catch (const sdbusplus::exception::SdBusError& ex) |
| 51 | { |
| 52 | log<level::ERR>("Failed to restart service", |
| 53 | entry("SERVICE=%s", service.c_str()), |
| 54 | entry("ERR=%s", ex.what())); |
| 55 | elog<InternalFailure>(); |
| 56 | } |
| 57 | } |
| 58 | void ConfigMgr::stopService(const std::string& service) |
| 59 | { |
| 60 | try |
| 61 | { |
| 62 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 63 | SYSTEMD_INTERFACE, "StopUnit"); |
| 64 | method.append(service.c_str(), "replace"); |
| 65 | bus.call_noreply(method); |
| 66 | } |
| 67 | catch (const sdbusplus::exception::SdBusError& ex) |
| 68 | { |
| 69 | log<level::ERR>("Failed to stop service", |
| 70 | entry("SERVICE=%s", service.c_str()), |
| 71 | entry("ERR=%s", ex.what())); |
| 72 | elog<InternalFailure>(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void ConfigMgr::deleteObject() |
| 77 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 78 | // TODO Not needed the delete functionality. |
| 79 | // will do in later commit |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::string ConfigMgr::createConfig( |
| 83 | std::string lDAPServerURI, std::string lDAPBindDN, std::string lDAPBaseDN, |
| 84 | std::string lDAPBindDNPassword, CreateIface::SearchScope lDAPSearchScope, |
| 85 | CreateIface::Create::Type lDAPType, std::string groupNameAttribute, |
| 86 | std::string userNameAttribute) |
| 87 | { |
| 88 | bool secureLDAP = false; |
| 89 | |
| 90 | if (isValidLDAPURI(lDAPServerURI, LDAPSscheme)) |
| 91 | { |
| 92 | secureLDAP = true; |
| 93 | } |
| 94 | else if (isValidLDAPURI(lDAPServerURI, LDAPscheme)) |
| 95 | { |
| 96 | secureLDAP = false; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | log<level::ERR>("bad LDAP Server URI", |
| 101 | entry("LDAPSERVERURI=%s", lDAPServerURI.c_str())); |
| 102 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 103 | Argument::ARGUMENT_VALUE(lDAPServerURI.c_str())); |
| 104 | } |
| 105 | |
| 106 | if (secureLDAP && !fs::exists(tlsCacertFile.c_str())) |
| 107 | { |
| 108 | log<level::ERR>("LDAP server's CA certificate not provided", |
| 109 | entry("TLSCACERTFILE=%s", tlsCacertFile.c_str())); |
| 110 | elog<NoCACertificate>(); |
| 111 | } |
| 112 | |
| 113 | if (lDAPBindDN.empty()) |
| 114 | { |
| 115 | log<level::ERR>("Not a valid LDAP BINDDN", |
| 116 | entry("LDAPBINDDN=%s", lDAPBindDN.c_str())); |
| 117 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"), |
| 118 | Argument::ARGUMENT_VALUE(lDAPBindDN.c_str())); |
| 119 | } |
| 120 | |
| 121 | if (lDAPBaseDN.empty()) |
| 122 | { |
| 123 | log<level::ERR>("Not a valid LDAP BASEDN", |
| 124 | entry("LDAPBASEDN=%s", lDAPBaseDN.c_str())); |
| 125 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"), |
| 126 | Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str())); |
| 127 | } |
| 128 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 129 | // With current implementation we support only two default LDAP server. |
| 130 | // which will be always there but when the support comes for additional |
| 131 | // account providers then the create config would be used to create the |
| 132 | // additional config. |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 133 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 134 | std::string objPath; |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 135 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 136 | if (static_cast<ConfigIface::Type>(lDAPType) == ConfigIface::Type::OpenLdap) |
| 137 | { |
| 138 | openLDAPConfigPtr.reset(nullptr); |
| 139 | objPath = openLDAPDbusObjectPath; |
| 140 | openLDAPConfigPtr = std::make_unique<Config>( |
| 141 | bus, objPath.c_str(), configFilePath.c_str(), tlsCacertFile.c_str(), |
| 142 | secureLDAP, lDAPServerURI, lDAPBindDN, lDAPBaseDN, |
| 143 | std::move(lDAPBindDNPassword), |
| 144 | static_cast<ConfigIface::SearchScope>(lDAPSearchScope), |
| 145 | static_cast<ConfigIface::Type>(lDAPType), false, groupNameAttribute, |
| 146 | userNameAttribute, *this); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | ADConfigPtr.reset(nullptr); |
| 151 | objPath = ADDbusObjectPath; |
| 152 | ADConfigPtr = std::make_unique<Config>( |
| 153 | bus, objPath.c_str(), configFilePath.c_str(), tlsCacertFile.c_str(), |
| 154 | secureLDAP, lDAPServerURI, lDAPBindDN, lDAPBaseDN, |
| 155 | std::move(lDAPBindDNPassword), |
| 156 | static_cast<ConfigIface::SearchScope>(lDAPSearchScope), |
| 157 | static_cast<ConfigIface::Type>(lDAPType), false, groupNameAttribute, |
| 158 | userNameAttribute, *this); |
| 159 | } |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 160 | restartService(nscdService); |
| 161 | return objPath; |
| 162 | } |
| 163 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 164 | void ConfigMgr::createDefaultObjects() |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 165 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 166 | if (!openLDAPConfigPtr) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 167 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 168 | openLDAPConfigPtr = std::make_unique<Config>( |
| 169 | bus, openLDAPDbusObjectPath.c_str(), configFilePath.c_str(), |
| 170 | tlsCacertFile.c_str(), false, "", "", "", "", |
| 171 | ConfigIface::SearchScope::sub, ConfigIface::Type::OpenLdap, false, |
| 172 | "", "", *this); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 173 | } |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 174 | if (!ADConfigPtr) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 175 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 176 | ADConfigPtr = std::make_unique<Config>( |
| 177 | bus, ADDbusObjectPath.c_str(), configFilePath.c_str(), |
| 178 | tlsCacertFile.c_str(), false, "", "", "", "", |
| 179 | ConfigIface::SearchScope::sub, ConfigIface::Type::ActiveDirectory, |
| 180 | false, "", "", *this); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 181 | } |
| 182 | } |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 183 | |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 184 | } // namespace ldap |
| 185 | } // namespace phosphor |