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