Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 1 | #include "ldap_configuration.hpp" |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 2 | #include <ldap.h> |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 3 | #include <experimental/filesystem> |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 4 | #include <fstream> |
| 5 | #include <sstream> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace ldap |
| 10 | { |
| 11 | constexpr auto nslcdService = "nslcd.service"; |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 12 | constexpr auto nscdService = "nscd.service"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 13 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 14 | using namespace phosphor::logging; |
| 15 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 16 | namespace fs = std::experimental::filesystem; |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 17 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 18 | |
| 19 | using Line = std::string; |
| 20 | using Key = std::string; |
| 21 | using Val = std::string; |
| 22 | using ConfigInfo = std::map<Key, Val>; |
| 23 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 24 | Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath, |
| 25 | bool secureLDAP, std::string lDAPServerURI, |
| 26 | std::string lDAPBindDN, std::string lDAPBaseDN, |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 27 | std::string&& lDAPBindDNPassword, |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 28 | ldap_base::Config::SearchScope lDAPSearchScope, |
| 29 | ldap_base::Config::Type lDAPType, ConfigMgr& parent) : |
| 30 | ConfigIface(bus, path, true), |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 31 | secureLDAP(secureLDAP), configFilePath(filePath), |
| 32 | lDAPBindDNPassword(std::move(lDAPBindDNPassword)), bus(bus), parent(parent) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 33 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 34 | ConfigIface::lDAPServerURI(lDAPServerURI); |
| 35 | ConfigIface::lDAPBindDN(lDAPBindDN); |
| 36 | ConfigIface::lDAPBaseDN(lDAPBaseDN); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 37 | ConfigIface::lDAPSearchScope(lDAPSearchScope); |
| 38 | ConfigIface::lDAPType(lDAPType); |
| 39 | writeConfig(); |
| 40 | parent.restartService(nslcdService); |
| 41 | // Emit deferred signal. |
| 42 | this->emit_object_added(); |
| 43 | } |
| 44 | |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 45 | void Config::delete_() |
| 46 | { |
| 47 | parent.deleteObject(); |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 48 | try |
| 49 | { |
| 50 | fs::copy_file(defaultNslcdFile, LDAP_CONFIG_FILE, |
| 51 | fs::copy_options::overwrite_existing); |
| 52 | fs::copy_file(nsSwitchFile, LDAPNsSwitchFile, |
| 53 | fs::copy_options::overwrite_existing); |
| 54 | fs::copy_file(linuxNsSwitchFile, nsSwitchFile, |
| 55 | fs::copy_options::overwrite_existing); |
| 56 | } |
| 57 | catch (const std::exception& e) |
| 58 | { |
| 59 | log<level::ERR>("Failed to rename Config Files while deleting Object", |
| 60 | entry("ERR=%s", e.what())); |
| 61 | elog<InternalFailure>(); |
| 62 | } |
| 63 | |
| 64 | parent.restartService(nscdService); |
| 65 | parent.stopService(nslcdService); |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 68 | void Config::writeConfig() |
| 69 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 70 | std::stringstream confData; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 71 | auto isPwdTobeWritten = false; |
| 72 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 73 | confData << "uid root\n"; |
| 74 | confData << "gid root\n\n"; |
| 75 | confData << "ldap_version 3\n\n"; |
| 76 | confData << "timelimit 30\n"; |
| 77 | confData << "bind_timelimit 30\n"; |
| 78 | confData << "pagesize 1000\n"; |
| 79 | confData << "referrals off\n\n"; |
| 80 | confData << "uri " << lDAPServerURI() << "\n\n"; |
| 81 | confData << "base " << lDAPBaseDN() << "\n\n"; |
| 82 | confData << "binddn " << lDAPBindDN() << "\n"; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 83 | if (!lDAPBindDNPassword.empty()) |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 84 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 85 | confData << "bindpw " << lDAPBindDNPassword << "\n"; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 86 | isPwdTobeWritten = true; |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 87 | } |
| 88 | confData << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 89 | switch (lDAPSearchScope()) |
| 90 | { |
| 91 | case ldap_base::Config::SearchScope::sub: |
| 92 | confData << "scope sub\n\n"; |
| 93 | break; |
| 94 | case ldap_base::Config::SearchScope::one: |
| 95 | confData << "scope one\n\n"; |
| 96 | break; |
| 97 | case ldap_base::Config::SearchScope::base: |
| 98 | confData << "scope base\n\n"; |
| 99 | break; |
| 100 | } |
| 101 | confData << "base passwd " << lDAPBaseDN() << "\n"; |
| 102 | confData << "base shadow " << lDAPBaseDN() << "\n\n"; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 103 | if (secureLDAP == true) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 104 | { |
| 105 | confData << "ssl on\n"; |
| 106 | confData << "tls_reqcert allow\n"; |
| 107 | confData << "tls_cert /etc/nslcd/certs/cert.pem\n"; |
| 108 | } |
| 109 | else |
| 110 | { |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 111 | confData << "ssl off\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 112 | } |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 113 | confData << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 114 | if (lDAPType() == ldap_base::Config::Type::ActiveDirectory) |
| 115 | { |
| 116 | confData << "filter passwd (&(objectClass=user)(objectClass=person)" |
| 117 | "(!(objectClass=computer)))\n"; |
| 118 | confData |
| 119 | << "filter group (|(objectclass=group)(objectclass=groupofnames) " |
| 120 | "(objectclass=groupofuniquenames))\n"; |
| 121 | confData << "map passwd uid sAMAccountName\n"; |
| 122 | confData << "map passwd uidNumber " |
| 123 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
| 124 | confData << "map passwd gidNumber primaryGroupID\n"; |
| 125 | confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n"; |
| 126 | confData << "map passwd gecos displayName\n"; |
| 127 | confData << "map passwd loginShell \"/bin/bash\"\n"; |
| 128 | confData << "map group gidNumber primaryGroupID\n"; |
| 129 | confData << "map group gidNumber " |
| 130 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
| 131 | confData << "map group cn sAMAccountName\n"; |
| 132 | } |
| 133 | else if (lDAPType() == ldap_base::Config::Type::OpenLdap) |
| 134 | { |
| 135 | confData << "filter passwd (objectclass=*)\n"; |
| 136 | confData << "map passwd uid cn\n"; |
| 137 | confData << "map passwd gecos displayName\n"; |
| 138 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 139 | try |
| 140 | { |
| 141 | std::fstream stream(configFilePath.c_str(), std::fstream::out); |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 142 | // remove the read permission from others if password is being written. |
| 143 | // nslcd forces this behaviour. |
| 144 | auto permission = fs::perms::owner_read | fs::perms::owner_write | |
| 145 | fs::perms::group_read; |
| 146 | if (isPwdTobeWritten) |
| 147 | { |
| 148 | fs::permissions(configFilePath, permission); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | fs::permissions(configFilePath, |
| 153 | permission | fs::perms::others_read); |
| 154 | } |
| 155 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 156 | stream << confData.str(); |
| 157 | stream.flush(); |
| 158 | stream.close(); |
| 159 | } |
| 160 | catch (const std::exception& e) |
| 161 | { |
| 162 | log<level::ERR>(e.what()); |
| 163 | elog<InternalFailure>(); |
| 164 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 168 | std::string Config::lDAPServerURI(std::string value) |
| 169 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 170 | std::string val; |
| 171 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 172 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 173 | if (value == lDAPServerURI()) |
| 174 | { |
| 175 | return value; |
| 176 | } |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 177 | if (secureLDAP) |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 178 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 179 | if (!ldap_is_ldaps_url(value.c_str())) |
| 180 | { |
| 181 | log<level::ERR>("bad LDAPS Server URI", |
| 182 | entry("LDAPSSERVERURI=%s", value.c_str())); |
| 183 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 184 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | if (!ldap_is_ldap_url(value.c_str())) |
| 190 | { |
| 191 | log<level::ERR>("bad LDAP Server URI", |
| 192 | entry("LDAPSERVERURI=%s", value.c_str())); |
| 193 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 194 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 195 | } |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 196 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 197 | val = ConfigIface::lDAPServerURI(value); |
| 198 | writeConfig(); |
| 199 | parent.restartService(nslcdService); |
| 200 | } |
| 201 | catch (const InternalFailure& e) |
| 202 | { |
| 203 | throw; |
| 204 | } |
| 205 | catch (const std::exception& e) |
| 206 | { |
| 207 | log<level::ERR>(e.what()); |
| 208 | elog<InternalFailure>(); |
| 209 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 210 | return val; |
| 211 | } |
| 212 | |
| 213 | std::string Config::lDAPBindDN(std::string value) |
| 214 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 215 | std::string val; |
| 216 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 217 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 218 | if (value == lDAPBindDN()) |
| 219 | { |
| 220 | return value; |
| 221 | } |
| 222 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 223 | if (value.empty()) |
| 224 | { |
| 225 | log<level::ERR>("Not a valid LDAP BINDDN"), |
| 226 | entry("LDAPBINDDN=%s", value.c_str()); |
| 227 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"), |
| 228 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 229 | } |
| 230 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 231 | val = ConfigIface::lDAPBindDN(value); |
| 232 | writeConfig(); |
| 233 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 234 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 235 | catch (const InternalFailure& e) |
| 236 | { |
| 237 | throw; |
| 238 | } |
| 239 | catch (const std::exception& e) |
| 240 | { |
| 241 | log<level::ERR>(e.what()); |
| 242 | elog<InternalFailure>(); |
| 243 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 244 | return val; |
| 245 | } |
| 246 | |
| 247 | std::string Config::lDAPBaseDN(std::string value) |
| 248 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 249 | std::string val; |
| 250 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 251 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 252 | if (value == lDAPBaseDN()) |
| 253 | { |
| 254 | return value; |
| 255 | } |
| 256 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 257 | if (value.empty()) |
| 258 | { |
| 259 | log<level::ERR>("Not a valid LDAP BASEDN"), |
| 260 | entry("BASEDN=%s", value.c_str()); |
| 261 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"), |
| 262 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 263 | } |
| 264 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 265 | val = ConfigIface::lDAPBaseDN(value); |
| 266 | writeConfig(); |
| 267 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 268 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 269 | catch (const InternalFailure& e) |
| 270 | { |
| 271 | throw; |
| 272 | } |
| 273 | catch (const std::exception& e) |
| 274 | { |
| 275 | log<level::ERR>(e.what()); |
| 276 | elog<InternalFailure>(); |
| 277 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 278 | return val; |
| 279 | } |
| 280 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 281 | ldap_base::Config::SearchScope |
| 282 | Config::lDAPSearchScope(ldap_base::Config::SearchScope value) |
| 283 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 284 | ldap_base::Config::SearchScope val; |
| 285 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 286 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 287 | if (value == lDAPSearchScope()) |
| 288 | { |
| 289 | return value; |
| 290 | } |
| 291 | |
| 292 | val = ConfigIface::lDAPSearchScope(value); |
| 293 | writeConfig(); |
| 294 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 295 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 296 | catch (const InternalFailure& e) |
| 297 | { |
| 298 | throw; |
| 299 | } |
| 300 | catch (const std::exception& e) |
| 301 | { |
| 302 | log<level::ERR>(e.what()); |
| 303 | elog<InternalFailure>(); |
| 304 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 305 | return val; |
| 306 | } |
| 307 | |
| 308 | ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value) |
| 309 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 310 | ldap_base::Config::Type val; |
| 311 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 312 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 313 | if (value == lDAPType()) |
| 314 | { |
| 315 | return value; |
| 316 | } |
| 317 | |
| 318 | val = ConfigIface::lDAPType(value); |
| 319 | writeConfig(); |
| 320 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 321 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 322 | catch (const InternalFailure& e) |
| 323 | { |
| 324 | throw; |
| 325 | } |
| 326 | catch (const std::exception& e) |
| 327 | { |
| 328 | log<level::ERR>(e.what()); |
| 329 | elog<InternalFailure>(); |
| 330 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 331 | return val; |
| 332 | } |
| 333 | |
| 334 | void ConfigMgr::restartService(const std::string& service) |
| 335 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 336 | try |
| 337 | { |
| 338 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 339 | SYSTEMD_INTERFACE, "RestartUnit"); |
| 340 | method.append(service.c_str(), "replace"); |
| 341 | bus.call_noreply(method); |
| 342 | } |
| 343 | catch (const sdbusplus::exception::SdBusError& ex) |
| 344 | { |
| 345 | log<level::ERR>("Failed to restart nslcd service", |
| 346 | entry("ERR=%s", ex.what())); |
| 347 | elog<InternalFailure>(); |
| 348 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 351 | void ConfigMgr::stopService(const std::string& service) |
| 352 | { |
| 353 | try |
| 354 | { |
| 355 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 356 | SYSTEMD_INTERFACE, "StopUnit"); |
| 357 | method.append(service.c_str(), "replace"); |
| 358 | bus.call_noreply(method); |
| 359 | } |
| 360 | catch (const sdbusplus::exception::SdBusError& ex) |
| 361 | { |
| 362 | log<level::ERR>("Failed to stop nslcd service", |
| 363 | entry("ERR=%s", ex.what())); |
| 364 | elog<InternalFailure>(); |
| 365 | } |
| 366 | } |
| 367 | |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 368 | void ConfigMgr::deleteObject() |
| 369 | { |
| 370 | configPtr.reset(nullptr); |
| 371 | } |
| 372 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 373 | std::string |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 374 | ConfigMgr::createConfig(std::string lDAPServerURI, std::string lDAPBindDN, |
| 375 | std::string lDAPBaseDN, |
| 376 | std::string lDAPBindDNPassword, |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 377 | ldap_base::Create::SearchScope lDAPSearchScope, |
| 378 | ldap_base::Create::Type lDAPType) |
| 379 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 380 | bool secureLDAP = false; |
| 381 | |
| 382 | if (ldap_is_ldaps_url(lDAPServerURI.c_str())) |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 383 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 384 | secureLDAP = true; |
| 385 | } |
| 386 | else if (ldap_is_ldap_url(lDAPServerURI.c_str())) |
| 387 | { |
| 388 | secureLDAP = false; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | log<level::ERR>("bad LDAP Server URI", |
| 393 | entry("LDAPSERVERURI=%s", lDAPServerURI.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 394 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 395 | Argument::ARGUMENT_VALUE(lDAPServerURI.c_str())); |
| 396 | } |
| 397 | |
| 398 | if (lDAPBindDN.empty()) |
| 399 | { |
| 400 | log<level::ERR>("Not a valid LDAP BINDDN"), |
| 401 | entry("LDAPBINDDN=%s", lDAPBindDN.c_str()); |
| 402 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"), |
| 403 | Argument::ARGUMENT_VALUE(lDAPBindDN.c_str())); |
| 404 | } |
| 405 | |
| 406 | if (lDAPBaseDN.empty()) |
| 407 | { |
| 408 | log<level::ERR>("Not a valid LDAP BASEDN"), |
| 409 | entry("LDAPBASEDN=%s", lDAPBaseDN.c_str()); |
| 410 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"), |
| 411 | Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str())); |
| 412 | } |
| 413 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 414 | // With current implementation we support only one LDAP server. |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 415 | deleteObject(); |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 416 | try |
| 417 | { |
| 418 | fs::copy_file(nsSwitchFile, linuxNsSwitchFile, |
| 419 | fs::copy_options::overwrite_existing); |
| 420 | fs::copy_file(LDAPNsSwitchFile, nsSwitchFile, |
| 421 | fs::copy_options::overwrite_existing); |
| 422 | } |
| 423 | catch (const std::exception& e) |
| 424 | { |
| 425 | log<level::ERR>("Failed to rename Config Files while creating Object", |
| 426 | entry("ERR=%s", e.what())); |
| 427 | elog<InternalFailure>(); |
| 428 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 429 | |
| 430 | auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH); |
| 431 | configPtr = std::make_unique<Config>( |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 432 | bus, objPath.c_str(), configFilePath.c_str(), secureLDAP, lDAPServerURI, |
| 433 | lDAPBindDN, lDAPBaseDN, std::move(lDAPBindDNPassword), |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 434 | static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope), |
| 435 | static_cast<ldap_base::Config::Type>(lDAPType), *this); |
| 436 | |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 437 | restartService(nslcdService); |
| 438 | restartService(nscdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 439 | return objPath; |
| 440 | } |
| 441 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 442 | void ConfigMgr::restore(const char* filePath) |
| 443 | { |
| 444 | if (!fs::exists(filePath)) |
| 445 | { |
| 446 | log<level::ERR>("Config file doesn't exists", |
| 447 | entry("LDAP_CONFIG_FILE=%s", LDAP_CONFIG_FILE)); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | ConfigInfo configValues; |
| 452 | |
| 453 | try |
| 454 | { |
| 455 | std::fstream stream(filePath, std::fstream::in); |
| 456 | Line line; |
| 457 | // read characters from stream and places them into line |
| 458 | while (std::getline(stream, line)) |
| 459 | { |
| 460 | // remove leading and trailing extra spaces |
| 461 | auto firstScan = line.find_first_not_of(' '); |
| 462 | auto first = |
| 463 | (firstScan == std::string::npos ? line.length() : firstScan); |
| 464 | auto last = line.find_last_not_of(' '); |
| 465 | line = line.substr(first, last - first + 1); |
| 466 | // reduce multiple spaces between two words to a single space |
| 467 | auto pred = [](char a, char b) { |
| 468 | return (a == b && a == ' ') ? true : false; |
| 469 | }; |
| 470 | |
| 471 | auto lastPos = std::unique(line.begin(), line.end(), pred); |
| 472 | |
| 473 | line.erase(lastPos, line.end()); |
| 474 | |
| 475 | // Ignore if line is empty or starts with '#' |
| 476 | if (line.empty() || line.at(0) == '#') |
| 477 | { |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | Key key; |
| 482 | std::istringstream isLine(line); |
| 483 | // extract characters from isLine and stores them into |
| 484 | // key until the delimitation character ' ' is found. |
| 485 | // If the delimiter is found, it is extracted and discarded |
| 486 | // the next input operation will begin after it. |
| 487 | if (std::getline(isLine, key, ' ')) |
| 488 | { |
| 489 | Val value; |
| 490 | // extract characters after delimitation character ' ' |
| 491 | if (std::getline(isLine, value, ' ')) |
| 492 | { |
| 493 | // skip line if it starts with "base shadow" or |
| 494 | // "base passwd" because we would have 3 entries |
| 495 | // ("base lDAPBaseDN" , "base passwd lDAPBaseDN" and |
| 496 | // "base shadow lDAPBaseDN") for the property "lDAPBaseDN", |
| 497 | // one is enough to restore it. |
| 498 | |
| 499 | if ((key == "base") && |
| 500 | (value == "passwd" || value == "shadow")) |
| 501 | { |
| 502 | continue; |
| 503 | } |
| 504 | // skip the line if it starts with "map passwd". |
| 505 | // if config type is AD "map group" entry would be add to |
| 506 | // the map configValues. For OpenLdap config file no map |
| 507 | // entry would be there. |
| 508 | if ((key == "map") && (value == "passwd")) |
| 509 | { |
| 510 | continue; |
| 511 | } |
| 512 | configValues[key] = value; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 517 | ldap_base::Create::SearchScope lDAPSearchScope; |
| 518 | if (configValues["scope"] == "sub") |
| 519 | { |
| 520 | lDAPSearchScope = ldap_base::Create::SearchScope::sub; |
| 521 | } |
| 522 | else if (configValues["scope"] == "one") |
| 523 | { |
| 524 | lDAPSearchScope = ldap_base::Create::SearchScope::one; |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | lDAPSearchScope = ldap_base::Create::SearchScope::base; |
| 529 | } |
| 530 | |
| 531 | ldap_base::Create::Type lDAPType; |
| 532 | // If the file is having a line which starts with "map group" |
| 533 | if (configValues["map"] == "group") |
| 534 | { |
| 535 | lDAPType = ldap_base::Create::Type::ActiveDirectory; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | lDAPType = ldap_base::Create::Type::OpenLdap; |
| 540 | } |
| 541 | |
| 542 | createConfig( |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame^] | 543 | std::move(configValues["uri"]), std::move(configValues["binddn"]), |
| 544 | std::move(configValues["base"]), std::move(configValues["bindpw"]), |
| 545 | lDAPSearchScope, lDAPType); |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 546 | } |
| 547 | catch (const InvalidArgument& e) |
| 548 | { |
| 549 | // Don't throw - we don't want to create a D-Bus |
| 550 | // object upon finding empty values in config, as |
| 551 | // this can be a default config. |
| 552 | } |
| 553 | catch (const InternalFailure& e) |
| 554 | { |
| 555 | throw; |
| 556 | } |
| 557 | catch (const std::exception& e) |
| 558 | { |
| 559 | log<level::ERR>(e.what()); |
| 560 | elog<InternalFailure>(); |
| 561 | } |
| 562 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 563 | } // namespace ldap |
| 564 | } // namespace phosphor |