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 | { |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame^] | 52 | fs::path configDir = fs::path(configFilePath.c_str()).parent_path(); |
| 53 | |
| 54 | fs::copy_file(configDir / defaultNslcdFile, LDAP_CONFIG_FILE, |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 55 | fs::copy_options::overwrite_existing); |
Ratan Gupta | 3e7a72e | 2018-10-18 09:57:14 +0530 | [diff] [blame] | 56 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame^] | 57 | fs::copy_file(configDir / linuxNsSwitchFile, configDir / nsSwitchFile, |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 58 | fs::copy_options::overwrite_existing); |
| 59 | } |
| 60 | catch (const std::exception& e) |
| 61 | { |
| 62 | log<level::ERR>("Failed to rename Config Files while deleting Object", |
| 63 | entry("ERR=%s", e.what())); |
| 64 | elog<InternalFailure>(); |
| 65 | } |
| 66 | |
| 67 | parent.restartService(nscdService); |
| 68 | parent.stopService(nslcdService); |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 71 | void Config::writeConfig() |
| 72 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 73 | std::stringstream confData; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 74 | auto isPwdTobeWritten = false; |
| 75 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 76 | confData << "uid root\n"; |
| 77 | confData << "gid root\n\n"; |
| 78 | confData << "ldap_version 3\n\n"; |
| 79 | confData << "timelimit 30\n"; |
| 80 | confData << "bind_timelimit 30\n"; |
| 81 | confData << "pagesize 1000\n"; |
| 82 | confData << "referrals off\n\n"; |
| 83 | confData << "uri " << lDAPServerURI() << "\n\n"; |
| 84 | confData << "base " << lDAPBaseDN() << "\n\n"; |
| 85 | confData << "binddn " << lDAPBindDN() << "\n"; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 86 | if (!lDAPBindDNPassword.empty()) |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 87 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 88 | confData << "bindpw " << lDAPBindDNPassword << "\n"; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 89 | isPwdTobeWritten = true; |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 90 | } |
| 91 | confData << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 92 | switch (lDAPSearchScope()) |
| 93 | { |
| 94 | case ldap_base::Config::SearchScope::sub: |
| 95 | confData << "scope sub\n\n"; |
| 96 | break; |
| 97 | case ldap_base::Config::SearchScope::one: |
| 98 | confData << "scope one\n\n"; |
| 99 | break; |
| 100 | case ldap_base::Config::SearchScope::base: |
| 101 | confData << "scope base\n\n"; |
| 102 | break; |
| 103 | } |
| 104 | confData << "base passwd " << lDAPBaseDN() << "\n"; |
| 105 | confData << "base shadow " << lDAPBaseDN() << "\n\n"; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 106 | if (secureLDAP == true) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 107 | { |
| 108 | confData << "ssl on\n"; |
| 109 | confData << "tls_reqcert allow\n"; |
| 110 | confData << "tls_cert /etc/nslcd/certs/cert.pem\n"; |
| 111 | } |
| 112 | else |
| 113 | { |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 114 | confData << "ssl off\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 115 | } |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 116 | confData << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 117 | if (lDAPType() == ldap_base::Config::Type::ActiveDirectory) |
| 118 | { |
| 119 | confData << "filter passwd (&(objectClass=user)(objectClass=person)" |
| 120 | "(!(objectClass=computer)))\n"; |
| 121 | confData |
| 122 | << "filter group (|(objectclass=group)(objectclass=groupofnames) " |
| 123 | "(objectclass=groupofuniquenames))\n"; |
| 124 | confData << "map passwd uid sAMAccountName\n"; |
| 125 | confData << "map passwd uidNumber " |
| 126 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
| 127 | confData << "map passwd gidNumber primaryGroupID\n"; |
| 128 | confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n"; |
| 129 | confData << "map passwd gecos displayName\n"; |
| 130 | confData << "map passwd loginShell \"/bin/bash\"\n"; |
| 131 | confData << "map group gidNumber primaryGroupID\n"; |
| 132 | confData << "map group gidNumber " |
| 133 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
| 134 | confData << "map group cn sAMAccountName\n"; |
| 135 | } |
| 136 | else if (lDAPType() == ldap_base::Config::Type::OpenLdap) |
| 137 | { |
| 138 | confData << "filter passwd (objectclass=*)\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 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 | } |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame^] | 241 | catch (const InvalidArgument& e) |
| 242 | { |
| 243 | throw; |
| 244 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 245 | catch (const std::exception& e) |
| 246 | { |
| 247 | log<level::ERR>(e.what()); |
| 248 | elog<InternalFailure>(); |
| 249 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 250 | return val; |
| 251 | } |
| 252 | |
| 253 | std::string Config::lDAPBaseDN(std::string value) |
| 254 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 255 | std::string val; |
| 256 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 257 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 258 | if (value == lDAPBaseDN()) |
| 259 | { |
| 260 | return value; |
| 261 | } |
| 262 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 263 | if (value.empty()) |
| 264 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 265 | log<level::ERR>("Not a valid LDAP BASEDN", |
| 266 | entry("BASEDN=%s", value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 267 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"), |
| 268 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 269 | } |
| 270 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 271 | val = ConfigIface::lDAPBaseDN(value); |
| 272 | writeConfig(); |
| 273 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 274 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 275 | catch (const InternalFailure& e) |
| 276 | { |
| 277 | throw; |
| 278 | } |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame^] | 279 | catch (const InvalidArgument& e) |
| 280 | { |
| 281 | throw; |
| 282 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 283 | catch (const std::exception& e) |
| 284 | { |
| 285 | log<level::ERR>(e.what()); |
| 286 | elog<InternalFailure>(); |
| 287 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 288 | return val; |
| 289 | } |
| 290 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 291 | ldap_base::Config::SearchScope |
| 292 | Config::lDAPSearchScope(ldap_base::Config::SearchScope value) |
| 293 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 294 | ldap_base::Config::SearchScope val; |
| 295 | try |
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 | if (value == lDAPSearchScope()) |
| 298 | { |
| 299 | return value; |
| 300 | } |
| 301 | |
| 302 | val = ConfigIface::lDAPSearchScope(value); |
| 303 | writeConfig(); |
| 304 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 305 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 306 | catch (const InternalFailure& e) |
| 307 | { |
| 308 | throw; |
| 309 | } |
| 310 | catch (const std::exception& e) |
| 311 | { |
| 312 | log<level::ERR>(e.what()); |
| 313 | elog<InternalFailure>(); |
| 314 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 315 | return val; |
| 316 | } |
| 317 | |
| 318 | ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value) |
| 319 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 320 | ldap_base::Config::Type val; |
| 321 | try |
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 | if (value == lDAPType()) |
| 324 | { |
| 325 | return value; |
| 326 | } |
| 327 | |
| 328 | val = ConfigIface::lDAPType(value); |
| 329 | writeConfig(); |
| 330 | parent.restartService(nslcdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 331 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 332 | catch (const InternalFailure& e) |
| 333 | { |
| 334 | throw; |
| 335 | } |
| 336 | catch (const std::exception& e) |
| 337 | { |
| 338 | log<level::ERR>(e.what()); |
| 339 | elog<InternalFailure>(); |
| 340 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 341 | return val; |
| 342 | } |
| 343 | |
| 344 | void ConfigMgr::restartService(const std::string& service) |
| 345 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 346 | try |
| 347 | { |
| 348 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 349 | SYSTEMD_INTERFACE, "RestartUnit"); |
| 350 | method.append(service.c_str(), "replace"); |
| 351 | bus.call_noreply(method); |
| 352 | } |
| 353 | catch (const sdbusplus::exception::SdBusError& ex) |
| 354 | { |
| 355 | log<level::ERR>("Failed to restart nslcd service", |
| 356 | entry("ERR=%s", ex.what())); |
| 357 | elog<InternalFailure>(); |
| 358 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 361 | void ConfigMgr::stopService(const std::string& service) |
| 362 | { |
| 363 | try |
| 364 | { |
| 365 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 366 | SYSTEMD_INTERFACE, "StopUnit"); |
| 367 | method.append(service.c_str(), "replace"); |
| 368 | bus.call_noreply(method); |
| 369 | } |
| 370 | catch (const sdbusplus::exception::SdBusError& ex) |
| 371 | { |
| 372 | log<level::ERR>("Failed to stop nslcd service", |
| 373 | entry("ERR=%s", ex.what())); |
| 374 | elog<InternalFailure>(); |
| 375 | } |
| 376 | } |
| 377 | |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 378 | void ConfigMgr::deleteObject() |
| 379 | { |
| 380 | configPtr.reset(nullptr); |
| 381 | } |
| 382 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 383 | std::string |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 384 | ConfigMgr::createConfig(std::string lDAPServerURI, std::string lDAPBindDN, |
| 385 | std::string lDAPBaseDN, |
| 386 | std::string lDAPBindDNPassword, |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 387 | ldap_base::Create::SearchScope lDAPSearchScope, |
| 388 | ldap_base::Create::Type lDAPType) |
| 389 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 390 | bool secureLDAP = false; |
| 391 | |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 392 | if (isValidLDAPURI(lDAPServerURI, LDAPSscheme)) |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 393 | { |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 394 | secureLDAP = true; |
| 395 | } |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 396 | else if (isValidLDAPURI(lDAPServerURI, LDAPscheme)) |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 397 | { |
| 398 | secureLDAP = false; |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | log<level::ERR>("bad LDAP Server URI", |
| 403 | entry("LDAPSERVERURI=%s", lDAPServerURI.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 404 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 405 | Argument::ARGUMENT_VALUE(lDAPServerURI.c_str())); |
| 406 | } |
| 407 | |
| 408 | if (lDAPBindDN.empty()) |
| 409 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 410 | log<level::ERR>("Not a valid LDAP BINDDN", |
| 411 | entry("LDAPBINDDN=%s", lDAPBindDN.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 412 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"), |
| 413 | Argument::ARGUMENT_VALUE(lDAPBindDN.c_str())); |
| 414 | } |
| 415 | |
| 416 | if (lDAPBaseDN.empty()) |
| 417 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 418 | log<level::ERR>("Not a valid LDAP BASEDN", |
| 419 | entry("LDAPBASEDN=%s", lDAPBaseDN.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 420 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"), |
| 421 | Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str())); |
| 422 | } |
| 423 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 424 | // With current implementation we support only one LDAP server. |
Nagaraju Goruganti | 24194bd | 2018-09-18 09:55:09 -0500 | [diff] [blame] | 425 | deleteObject(); |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 426 | try |
| 427 | { |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame^] | 428 | fs::path configDir = fs::path(configFilePath.c_str()).parent_path(); |
| 429 | fs::copy_file(configDir / LDAPNsSwitchFile, configDir / nsSwitchFile, |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 430 | fs::copy_options::overwrite_existing); |
| 431 | } |
| 432 | catch (const std::exception& e) |
| 433 | { |
| 434 | log<level::ERR>("Failed to rename Config Files while creating Object", |
| 435 | entry("ERR=%s", e.what())); |
| 436 | elog<InternalFailure>(); |
| 437 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 438 | |
| 439 | auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH); |
| 440 | configPtr = std::make_unique<Config>( |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 441 | bus, objPath.c_str(), configFilePath.c_str(), secureLDAP, lDAPServerURI, |
| 442 | lDAPBindDN, lDAPBaseDN, std::move(lDAPBindDNPassword), |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 443 | static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope), |
| 444 | static_cast<ldap_base::Config::Type>(lDAPType), *this); |
| 445 | |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 446 | restartService(nslcdService); |
| 447 | restartService(nscdService); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 448 | return objPath; |
| 449 | } |
| 450 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 451 | void ConfigMgr::restore(const char* filePath) |
| 452 | { |
| 453 | if (!fs::exists(filePath)) |
| 454 | { |
| 455 | log<level::ERR>("Config file doesn't exists", |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame^] | 456 | entry("LDAP_CONFIG_FILE=%s", configFilePath.c_str())); |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
| 460 | ConfigInfo configValues; |
| 461 | |
| 462 | try |
| 463 | { |
| 464 | std::fstream stream(filePath, std::fstream::in); |
| 465 | Line line; |
| 466 | // read characters from stream and places them into line |
| 467 | while (std::getline(stream, line)) |
| 468 | { |
| 469 | // remove leading and trailing extra spaces |
| 470 | auto firstScan = line.find_first_not_of(' '); |
| 471 | auto first = |
| 472 | (firstScan == std::string::npos ? line.length() : firstScan); |
| 473 | auto last = line.find_last_not_of(' '); |
| 474 | line = line.substr(first, last - first + 1); |
| 475 | // reduce multiple spaces between two words to a single space |
| 476 | auto pred = [](char a, char b) { |
| 477 | return (a == b && a == ' ') ? true : false; |
| 478 | }; |
| 479 | |
| 480 | auto lastPos = std::unique(line.begin(), line.end(), pred); |
| 481 | |
| 482 | line.erase(lastPos, line.end()); |
| 483 | |
| 484 | // Ignore if line is empty or starts with '#' |
| 485 | if (line.empty() || line.at(0) == '#') |
| 486 | { |
| 487 | continue; |
| 488 | } |
| 489 | |
| 490 | Key key; |
| 491 | std::istringstream isLine(line); |
| 492 | // extract characters from isLine and stores them into |
| 493 | // key until the delimitation character ' ' is found. |
| 494 | // If the delimiter is found, it is extracted and discarded |
| 495 | // the next input operation will begin after it. |
| 496 | if (std::getline(isLine, key, ' ')) |
| 497 | { |
| 498 | Val value; |
| 499 | // extract characters after delimitation character ' ' |
| 500 | if (std::getline(isLine, value, ' ')) |
| 501 | { |
| 502 | // skip line if it starts with "base shadow" or |
| 503 | // "base passwd" because we would have 3 entries |
| 504 | // ("base lDAPBaseDN" , "base passwd lDAPBaseDN" and |
| 505 | // "base shadow lDAPBaseDN") for the property "lDAPBaseDN", |
| 506 | // one is enough to restore it. |
| 507 | |
| 508 | if ((key == "base") && |
| 509 | (value == "passwd" || value == "shadow")) |
| 510 | { |
| 511 | continue; |
| 512 | } |
| 513 | // skip the line if it starts with "map passwd". |
| 514 | // if config type is AD "map group" entry would be add to |
| 515 | // the map configValues. For OpenLdap config file no map |
| 516 | // entry would be there. |
| 517 | if ((key == "map") && (value == "passwd")) |
| 518 | { |
| 519 | continue; |
| 520 | } |
| 521 | configValues[key] = value; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 526 | ldap_base::Create::SearchScope lDAPSearchScope; |
| 527 | if (configValues["scope"] == "sub") |
| 528 | { |
| 529 | lDAPSearchScope = ldap_base::Create::SearchScope::sub; |
| 530 | } |
| 531 | else if (configValues["scope"] == "one") |
| 532 | { |
| 533 | lDAPSearchScope = ldap_base::Create::SearchScope::one; |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | lDAPSearchScope = ldap_base::Create::SearchScope::base; |
| 538 | } |
| 539 | |
| 540 | ldap_base::Create::Type lDAPType; |
| 541 | // If the file is having a line which starts with "map group" |
| 542 | if (configValues["map"] == "group") |
| 543 | { |
| 544 | lDAPType = ldap_base::Create::Type::ActiveDirectory; |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | lDAPType = ldap_base::Create::Type::OpenLdap; |
| 549 | } |
| 550 | |
Ratan Gupta | c9c86a2 | 2018-10-18 00:47:01 +0530 | [diff] [blame] | 551 | // Don't create the config object if either of the field is empty. |
| 552 | if (configValues["uri"] == "" || configValues["binddn"] == "" || |
| 553 | configValues["base"] == "") |
| 554 | { |
| 555 | log<level::INFO>( |
| 556 | "LDAP config parameter value missing", |
| 557 | entry("URI=%s", configValues["uri"].c_str()), |
| 558 | entry("BASEDN=%s", configValues["base"].c_str()), |
| 559 | entry("BINDDN=%s", configValues["binddn"].c_str())); |
| 560 | return; |
| 561 | } |
| 562 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 563 | createConfig( |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 564 | std::move(configValues["uri"]), std::move(configValues["binddn"]), |
| 565 | std::move(configValues["base"]), std::move(configValues["bindpw"]), |
| 566 | lDAPSearchScope, lDAPType); |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 567 | } |
| 568 | catch (const InvalidArgument& e) |
| 569 | { |
| 570 | // Don't throw - we don't want to create a D-Bus |
| 571 | // object upon finding empty values in config, as |
| 572 | // this can be a default config. |
| 573 | } |
| 574 | catch (const InternalFailure& e) |
| 575 | { |
| 576 | throw; |
| 577 | } |
| 578 | catch (const std::exception& e) |
| 579 | { |
| 580 | log<level::ERR>(e.what()); |
| 581 | elog<InternalFailure>(); |
| 582 | } |
| 583 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 584 | } // namespace ldap |
| 585 | } // namespace phosphor |