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