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" |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 3 | #include "utils.hpp" |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 4 | |
| 5 | #include <cereal/types/string.hpp> |
| 6 | #include <cereal/types/vector.hpp> |
| 7 | #include <cereal/archives/binary.hpp> |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 8 | #include <filesystem> |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 9 | #include <fstream> |
| 10 | #include <sstream> |
| 11 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 12 | // Register class version |
| 13 | // From cereal documentation; |
| 14 | // "This macro should be placed at global scope" |
| 15 | CEREAL_CLASS_VERSION(phosphor::ldap::Config, CLASS_VERSION); |
| 16 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace ldap |
| 20 | { |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 21 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 22 | constexpr auto nslcdService = "nslcd.service"; |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 23 | constexpr auto nscdService = "nscd.service"; |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 24 | constexpr auto LDAPscheme = "ldap"; |
| 25 | constexpr auto LDAPSscheme = "ldaps"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 26 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 29 | namespace fs = std::filesystem; |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 30 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 31 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 32 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 33 | |
| 34 | using Line = std::string; |
| 35 | using Key = std::string; |
| 36 | using Val = std::string; |
| 37 | using ConfigInfo = std::map<Key, Val>; |
| 38 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 39 | Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath, |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 40 | const char* caCertFile, bool secureLDAP, |
| 41 | std::string lDAPServerURI, std::string lDAPBindDN, |
| 42 | std::string lDAPBaseDN, std::string&& lDAPBindDNPassword, |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 43 | ConfigIface::SearchScope lDAPSearchScope, |
| 44 | ConfigIface::Type lDAPType, bool lDAPServiceEnabled, |
| 45 | std::string userNameAttr, std::string groupNameAttr, |
| 46 | ConfigMgr& parent) : |
| 47 | Ifaces(bus, path, true), |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 48 | secureLDAP(secureLDAP), lDAPBindPassword(std::move(lDAPBindDNPassword)), |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 49 | tlsCacertFile(caCertFile), configFilePath(filePath), objectPath(path), |
| 50 | bus(bus), parent(parent) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 51 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 52 | ConfigIface::lDAPServerURI(lDAPServerURI); |
| 53 | ConfigIface::lDAPBindDN(lDAPBindDN); |
| 54 | ConfigIface::lDAPBaseDN(lDAPBaseDN); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 55 | ConfigIface::lDAPSearchScope(lDAPSearchScope); |
| 56 | ConfigIface::lDAPType(lDAPType); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 57 | EnableIface::enabled(lDAPServiceEnabled); |
| 58 | ConfigIface::userNameAttribute(userNameAttr); |
| 59 | ConfigIface::groupNameAttribute(groupNameAttr); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 60 | // NOTE: Don't update the bindDN password under ConfigIface |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 61 | if (enabled()) |
| 62 | { |
| 63 | writeConfig(); |
| 64 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 65 | // save the config. |
| 66 | configPersistPath = parent.dbusPersistentPath; |
| 67 | configPersistPath += objectPath; |
| 68 | |
| 69 | // create the persistent directory |
| 70 | fs::create_directories(configPersistPath); |
| 71 | |
| 72 | configPersistPath += "/config"; |
| 73 | |
| 74 | std::ofstream os(configPersistPath, std::ios::binary | std::ios::out); |
| 75 | // remove the read permission from others |
| 76 | auto permission = |
| 77 | fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read; |
| 78 | fs::permissions(configPersistPath, permission); |
| 79 | |
| 80 | serialize(); |
| 81 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 82 | // Emit deferred signal. |
| 83 | this->emit_object_added(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 84 | parent.startOrStopService(nslcdService, enabled()); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 87 | Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath, |
| 88 | const char* caCertFile, ConfigIface::Type lDAPType, |
| 89 | ConfigMgr& parent) : |
| 90 | Ifaces(bus, path, true), |
| 91 | tlsCacertFile(caCertFile), configFilePath(filePath), objectPath(path), |
| 92 | bus(bus), parent(parent) |
| 93 | { |
| 94 | ConfigIface::lDAPType(lDAPType); |
| 95 | |
| 96 | configPersistPath = parent.dbusPersistentPath; |
| 97 | configPersistPath += objectPath; |
| 98 | |
| 99 | // create the persistent directory |
| 100 | fs::create_directories(configPersistPath); |
| 101 | |
| 102 | configPersistPath += "/config"; |
| 103 | |
| 104 | std::ofstream os(configPersistPath, std::ios::binary | std::ios::out); |
| 105 | // remove the read permission from others |
| 106 | auto permission = |
| 107 | fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read; |
| 108 | fs::permissions(configPersistPath, permission); |
| 109 | } |
| 110 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 111 | void Config::writeConfig() |
| 112 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 113 | std::stringstream confData; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 114 | auto isPwdTobeWritten = false; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 115 | std::string userNameAttr; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 116 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 117 | confData << "uid root\n"; |
| 118 | confData << "gid root\n\n"; |
| 119 | confData << "ldap_version 3\n\n"; |
| 120 | confData << "timelimit 30\n"; |
| 121 | confData << "bind_timelimit 30\n"; |
| 122 | confData << "pagesize 1000\n"; |
| 123 | confData << "referrals off\n\n"; |
| 124 | confData << "uri " << lDAPServerURI() << "\n\n"; |
| 125 | confData << "base " << lDAPBaseDN() << "\n\n"; |
| 126 | confData << "binddn " << lDAPBindDN() << "\n"; |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 127 | if (!lDAPBindPassword.empty()) |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 128 | { |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 129 | confData << "bindpw " << lDAPBindPassword << "\n"; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 130 | isPwdTobeWritten = true; |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 131 | } |
| 132 | confData << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 133 | switch (lDAPSearchScope()) |
| 134 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 135 | case ConfigIface::SearchScope::sub: |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 136 | confData << "scope sub\n\n"; |
| 137 | break; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 138 | case ConfigIface::SearchScope::one: |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 139 | confData << "scope one\n\n"; |
| 140 | break; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 141 | case ConfigIface::SearchScope::base: |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 142 | confData << "scope base\n\n"; |
| 143 | break; |
| 144 | } |
| 145 | confData << "base passwd " << lDAPBaseDN() << "\n"; |
| 146 | confData << "base shadow " << lDAPBaseDN() << "\n\n"; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 147 | if (secureLDAP == true) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 148 | { |
| 149 | confData << "ssl on\n"; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 150 | confData << "tls_reqcert hard\n"; |
| 151 | confData << "tls_cacertFile " << tlsCacertFile.c_str() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 152 | } |
| 153 | else |
| 154 | { |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 155 | confData << "ssl off\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 156 | } |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 157 | confData << "\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 158 | if (lDAPType() == ConfigIface::Type::ActiveDirectory) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 159 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 160 | if (ConfigIface::userNameAttribute().empty()) |
| 161 | { |
| 162 | ConfigIface::userNameAttribute("sAMAccountName"); |
| 163 | } |
| 164 | if (ConfigIface::groupNameAttribute().empty()) |
| 165 | { |
| 166 | ConfigIface::groupNameAttribute("primaryGroupID"); |
| 167 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 168 | confData << "filter passwd (&(objectClass=user)(objectClass=person)" |
| 169 | "(!(objectClass=computer)))\n"; |
| 170 | confData |
| 171 | << "filter group (|(objectclass=group)(objectclass=groupofnames) " |
| 172 | "(objectclass=groupofuniquenames))\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 173 | confData << "map passwd uid " |
| 174 | << ConfigIface::userNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 175 | confData << "map passwd uidNumber " |
| 176 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 177 | confData << "map passwd gidNumber " |
| 178 | << ConfigIface::groupNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 179 | confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n"; |
| 180 | confData << "map passwd gecos displayName\n"; |
| 181 | confData << "map passwd loginShell \"/bin/bash\"\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 182 | confData << "map group gidNumber " |
| 183 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 184 | confData << "map group cn " |
| 185 | << ConfigIface::userNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 186 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 187 | else if (lDAPType() == ConfigIface::Type::OpenLdap) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 188 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 189 | if (ConfigIface::userNameAttribute().empty()) |
| 190 | { |
raviteja-b | c3f56c5 | 2019-04-02 11:09:04 -0500 | [diff] [blame] | 191 | ConfigIface::userNameAttribute("cn"); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 192 | } |
| 193 | if (ConfigIface::groupNameAttribute().empty()) |
| 194 | { |
raviteja-b | c3f56c5 | 2019-04-02 11:09:04 -0500 | [diff] [blame] | 195 | ConfigIface::groupNameAttribute("gidNumber"); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 196 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 197 | confData << "filter passwd (objectclass=*)\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 198 | confData << "map passwd gecos displayName\n"; |
Nagaraju Goruganti | 808eda4 | 2018-10-10 08:48:12 -0500 | [diff] [blame] | 199 | confData << "filter group (objectclass=posixGroup)\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 200 | confData << "map passwd uid " |
| 201 | << ConfigIface::userNameAttribute() << "\n"; |
| 202 | confData << "map passwd gidNumber " |
| 203 | << ConfigIface::groupNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 204 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 205 | try |
| 206 | { |
| 207 | std::fstream stream(configFilePath.c_str(), std::fstream::out); |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 208 | // remove the read permission from others if password is being written. |
| 209 | // nslcd forces this behaviour. |
| 210 | auto permission = fs::perms::owner_read | fs::perms::owner_write | |
| 211 | fs::perms::group_read; |
| 212 | if (isPwdTobeWritten) |
| 213 | { |
| 214 | fs::permissions(configFilePath, permission); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | fs::permissions(configFilePath, |
| 219 | permission | fs::perms::others_read); |
| 220 | } |
| 221 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 222 | stream << confData.str(); |
| 223 | stream.flush(); |
| 224 | stream.close(); |
| 225 | } |
| 226 | catch (const std::exception& e) |
| 227 | { |
| 228 | log<level::ERR>(e.what()); |
| 229 | elog<InternalFailure>(); |
| 230 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 234 | std::string Config::lDAPBindDNPassword(std::string value) |
| 235 | { |
| 236 | // Don't update the D-bus object, this is just to |
| 237 | // facilitate if user wants to change the bind dn password |
| 238 | // once d-bus object gets created. |
| 239 | lDAPBindPassword = value; |
| 240 | try |
| 241 | { |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 242 | if (enabled()) |
| 243 | { |
| 244 | writeConfig(); |
| 245 | parent.startOrStopService(nslcdService, enabled()); |
| 246 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 247 | serialize(); |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 248 | } |
| 249 | catch (const InternalFailure& e) |
| 250 | { |
| 251 | throw; |
| 252 | } |
| 253 | catch (const std::exception& e) |
| 254 | { |
| 255 | log<level::ERR>(e.what()); |
| 256 | elog<InternalFailure>(); |
| 257 | } |
| 258 | return value; |
| 259 | } |
| 260 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 261 | std::string Config::lDAPServerURI(std::string value) |
| 262 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 263 | std::string val; |
| 264 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 265 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 266 | if (value == lDAPServerURI()) |
| 267 | { |
| 268 | return value; |
| 269 | } |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 270 | if (isValidLDAPURI(value, LDAPSscheme)) |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 271 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 272 | secureLDAP = true; |
| 273 | } |
| 274 | else if (isValidLDAPURI(value, LDAPscheme)) |
| 275 | { |
| 276 | secureLDAP = false; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 280 | log<level::ERR>("bad LDAP Server URI", |
| 281 | entry("LDAPSERVERURI=%s", value.c_str())); |
| 282 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 283 | Argument::ARGUMENT_VALUE(value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 284 | } |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 285 | |
| 286 | if (secureLDAP && !fs::exists(tlsCacertFile.c_str())) |
| 287 | { |
| 288 | log<level::ERR>("LDAP server's CA certificate not provided", |
| 289 | entry("TLSCACERTFILE=%s", tlsCacertFile.c_str())); |
| 290 | elog<NoCACertificate>(); |
| 291 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 292 | val = ConfigIface::lDAPServerURI(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 293 | if (enabled()) |
| 294 | { |
| 295 | writeConfig(); |
| 296 | parent.startOrStopService(nslcdService, enabled()); |
| 297 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 298 | // save the object. |
| 299 | serialize(); |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 300 | } |
| 301 | catch (const InternalFailure& e) |
| 302 | { |
| 303 | throw; |
| 304 | } |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 305 | catch (const InvalidArgument& e) |
| 306 | { |
| 307 | throw; |
| 308 | } |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 309 | catch (const NoCACertificate& e) |
| 310 | { |
| 311 | throw; |
| 312 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 313 | catch (const std::exception& e) |
| 314 | { |
| 315 | log<level::ERR>(e.what()); |
| 316 | elog<InternalFailure>(); |
| 317 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 318 | return val; |
| 319 | } |
| 320 | |
| 321 | std::string Config::lDAPBindDN(std::string value) |
| 322 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 323 | std::string val; |
| 324 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 325 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 326 | if (value == lDAPBindDN()) |
| 327 | { |
| 328 | return value; |
| 329 | } |
| 330 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 331 | if (value.empty()) |
| 332 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 333 | log<level::ERR>("Not a valid LDAP BINDDN", |
| 334 | entry("LDAPBINDDN=%s", value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 335 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"), |
| 336 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 337 | } |
| 338 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 339 | val = ConfigIface::lDAPBindDN(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 340 | if (enabled()) |
| 341 | { |
| 342 | writeConfig(); |
| 343 | parent.startOrStopService(nslcdService, enabled()); |
| 344 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 345 | // save the object. |
| 346 | serialize(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 347 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 348 | catch (const InternalFailure& e) |
| 349 | { |
| 350 | throw; |
| 351 | } |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 352 | catch (const InvalidArgument& e) |
| 353 | { |
| 354 | throw; |
| 355 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 356 | catch (const std::exception& e) |
| 357 | { |
| 358 | log<level::ERR>(e.what()); |
| 359 | elog<InternalFailure>(); |
| 360 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 361 | return val; |
| 362 | } |
| 363 | |
| 364 | std::string Config::lDAPBaseDN(std::string value) |
| 365 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 366 | std::string val; |
| 367 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 368 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 369 | if (value == lDAPBaseDN()) |
| 370 | { |
| 371 | return value; |
| 372 | } |
| 373 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 374 | if (value.empty()) |
| 375 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 376 | log<level::ERR>("Not a valid LDAP BASEDN", |
| 377 | entry("BASEDN=%s", value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 378 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"), |
| 379 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 380 | } |
| 381 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 382 | val = ConfigIface::lDAPBaseDN(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 383 | if (enabled()) |
| 384 | { |
| 385 | writeConfig(); |
| 386 | parent.startOrStopService(nslcdService, enabled()); |
| 387 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 388 | // save the object. |
| 389 | serialize(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 390 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 391 | catch (const InternalFailure& e) |
| 392 | { |
| 393 | throw; |
| 394 | } |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 395 | catch (const InvalidArgument& e) |
| 396 | { |
| 397 | throw; |
| 398 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 399 | catch (const std::exception& e) |
| 400 | { |
| 401 | log<level::ERR>(e.what()); |
| 402 | elog<InternalFailure>(); |
| 403 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 404 | return val; |
| 405 | } |
| 406 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 407 | ConfigIface::SearchScope Config::lDAPSearchScope(ConfigIface::SearchScope value) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 408 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 409 | ConfigIface::SearchScope val; |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 410 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 411 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 412 | if (value == lDAPSearchScope()) |
| 413 | { |
| 414 | return value; |
| 415 | } |
| 416 | |
| 417 | val = ConfigIface::lDAPSearchScope(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 418 | if (enabled()) |
| 419 | { |
| 420 | writeConfig(); |
| 421 | |
| 422 | parent.startOrStopService(nslcdService, enabled()); |
| 423 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 424 | // save the object. |
| 425 | serialize(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 426 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 427 | catch (const InternalFailure& e) |
| 428 | { |
| 429 | throw; |
| 430 | } |
| 431 | catch (const std::exception& e) |
| 432 | { |
| 433 | log<level::ERR>(e.what()); |
| 434 | elog<InternalFailure>(); |
| 435 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 436 | return val; |
| 437 | } |
| 438 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 439 | ConfigIface::Type Config::lDAPType(ConfigIface::Type value) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 440 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 441 | elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property")); |
| 442 | return lDAPType(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 445 | bool Config::enabled(bool value) |
| 446 | { |
Ratan Gupta | c5481d1 | 2019-04-12 18:31:05 +0530 | [diff] [blame^] | 447 | if (value == enabled()) |
| 448 | { |
| 449 | return value; |
| 450 | } |
| 451 | // Let parent decide that can we enable this config. |
| 452 | // It may happen that other config is already enabled, |
| 453 | // Current implementation support only one config can |
| 454 | // be active at a time. |
| 455 | return parent.enableService(*this, value); |
| 456 | } |
| 457 | |
| 458 | bool Config::enableService(bool value) |
| 459 | { |
| 460 | bool isEnable = false; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 461 | try |
| 462 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 463 | isEnable = EnableIface::enabled(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 464 | if (isEnable) |
| 465 | { |
| 466 | writeConfig(); |
| 467 | } |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 468 | parent.startOrStopService(nslcdService, value); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 469 | serialize(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 470 | } |
| 471 | catch (const InternalFailure& e) |
| 472 | { |
| 473 | throw; |
| 474 | } |
| 475 | catch (const std::exception& e) |
| 476 | { |
| 477 | log<level::ERR>(e.what()); |
| 478 | elog<InternalFailure>(); |
| 479 | } |
| 480 | return isEnable; |
| 481 | } |
| 482 | |
| 483 | std::string Config::userNameAttribute(std::string value) |
| 484 | { |
| 485 | std::string val; |
| 486 | try |
| 487 | { |
| 488 | if (value == userNameAttribute()) |
| 489 | { |
| 490 | return value; |
| 491 | } |
| 492 | |
| 493 | val = ConfigIface::userNameAttribute(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 494 | if (enabled()) |
| 495 | { |
| 496 | writeConfig(); |
| 497 | |
| 498 | parent.startOrStopService(nslcdService, enabled()); |
| 499 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 500 | // save the object. |
| 501 | serialize(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 502 | } |
| 503 | catch (const InternalFailure& e) |
| 504 | { |
| 505 | throw; |
| 506 | } |
| 507 | catch (const std::exception& e) |
| 508 | { |
| 509 | log<level::ERR>(e.what()); |
| 510 | elog<InternalFailure>(); |
| 511 | } |
| 512 | return val; |
| 513 | } |
| 514 | |
| 515 | std::string Config::groupNameAttribute(std::string value) |
| 516 | { |
| 517 | std::string val; |
| 518 | try |
| 519 | { |
| 520 | if (value == groupNameAttribute()) |
| 521 | { |
| 522 | return value; |
| 523 | } |
| 524 | |
| 525 | val = ConfigIface::groupNameAttribute(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 526 | if (enabled()) |
| 527 | { |
| 528 | writeConfig(); |
| 529 | |
| 530 | parent.startOrStopService(nslcdService, enabled()); |
| 531 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 532 | // save the object. |
| 533 | serialize(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 534 | } |
| 535 | catch (const InternalFailure& e) |
| 536 | { |
| 537 | throw; |
| 538 | } |
| 539 | catch (const std::exception& e) |
| 540 | { |
| 541 | log<level::ERR>(e.what()); |
| 542 | elog<InternalFailure>(); |
| 543 | } |
| 544 | return val; |
| 545 | } |
| 546 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 547 | template <class Archive> |
| 548 | void Config::save(Archive& archive, const std::uint32_t version) const |
| 549 | { |
| 550 | archive(this->enabled()); |
| 551 | archive(lDAPServerURI()); |
| 552 | archive(lDAPBindDN()); |
| 553 | archive(lDAPBaseDN()); |
| 554 | archive(lDAPSearchScope()); |
| 555 | archive(lDAPBindPassword); |
| 556 | archive(userNameAttribute()); |
| 557 | archive(groupNameAttribute()); |
| 558 | } |
| 559 | |
| 560 | template <class Archive> |
| 561 | void Config::load(Archive& archive, const std::uint32_t version) |
| 562 | { |
| 563 | |
| 564 | bool bVal; |
| 565 | archive(bVal); |
| 566 | EnableIface::enabled(bVal); |
| 567 | |
| 568 | std::string str; |
| 569 | archive(str); |
| 570 | ConfigIface::lDAPServerURI(str); |
| 571 | |
| 572 | archive(str); |
| 573 | ConfigIface::lDAPBindDN(str); |
| 574 | |
| 575 | archive(str); |
| 576 | ConfigIface::lDAPBaseDN(str); |
| 577 | |
| 578 | ConfigIface::SearchScope scope; |
| 579 | archive(scope); |
| 580 | ConfigIface::lDAPSearchScope(scope); |
| 581 | |
| 582 | archive(str); |
| 583 | lDAPBindPassword = str; |
| 584 | |
| 585 | archive(str); |
| 586 | ConfigIface::userNameAttribute(str); |
| 587 | |
| 588 | archive(str); |
| 589 | ConfigIface::groupNameAttribute(str); |
| 590 | } |
| 591 | |
| 592 | void Config::serialize() |
| 593 | { |
| 594 | std::ofstream os(configPersistPath.string(), |
| 595 | std::ios::binary | std::ios::out); |
| 596 | cereal::BinaryOutputArchive oarchive(os); |
| 597 | oarchive(*this); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | bool Config::deserialize() |
| 602 | { |
| 603 | try |
| 604 | { |
| 605 | if (fs::exists(configPersistPath)) |
| 606 | { |
| 607 | std::ifstream is(configPersistPath.c_str(), |
| 608 | std::ios::in | std::ios::binary); |
| 609 | cereal::BinaryInputArchive iarchive(is); |
| 610 | iarchive(*this); |
| 611 | return true; |
| 612 | } |
| 613 | return false; |
| 614 | } |
| 615 | catch (cereal::Exception& e) |
| 616 | { |
| 617 | log<level::ERR>(e.what()); |
| 618 | std::error_code ec; |
| 619 | fs::remove(configPersistPath, ec); |
| 620 | return false; |
| 621 | } |
| 622 | catch (const fs::filesystem_error& e) |
| 623 | { |
| 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 628 | } // namespace ldap |
| 629 | } // namespace phosphor |