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