Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 17 | #include "node.hpp" |
| 18 | |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 19 | #include <dbus_utility.hpp> |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 20 | #include <error_messages.hpp> |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 21 | #include <openbmc_dbus_rest.hpp> |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 22 | #include <utils/json_utils.hpp> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 23 | #include <variant> |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 24 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | namespace redfish |
| 26 | { |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 27 | |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame^] | 28 | constexpr const char* ldapConfigObject = |
| 29 | "/xyz/openbmc_project/user/ldap/openldap"; |
| 30 | constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap"; |
| 31 | constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config"; |
| 32 | constexpr const char* ldapConfigInterface = |
| 33 | "xyz.openbmc_project.User.Ldap.Config"; |
| 34 | constexpr const char* ldapCreateInterface = |
| 35 | "xyz.openbmc_project.User.Ldap.Create"; |
| 36 | constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable"; |
| 37 | constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager"; |
| 38 | constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties"; |
| 39 | constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; |
| 40 | constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper"; |
| 41 | constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 42 | |
| 43 | struct LDAPConfigData |
| 44 | { |
| 45 | std::string uri{}; |
| 46 | std::string bindDN{}; |
| 47 | std::string baseDN{}; |
| 48 | std::string searchScope{}; |
| 49 | std::string serverType{}; |
| 50 | bool serviceEnabled = false; |
| 51 | std::string userNameAttribute{}; |
| 52 | std::string groupAttribute{}; |
| 53 | }; |
| 54 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 55 | using ManagedObjectType = std::vector<std::pair< |
| 56 | sdbusplus::message::object_path, |
| 57 | boost::container::flat_map< |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 58 | std::string, boost::container::flat_map< |
| 59 | std::string, std::variant<bool, std::string>>>>>; |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame^] | 60 | using GetObjectType = |
| 61 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 62 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 63 | inline std::string getPrivilegeFromRoleId(std::string_view role) |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 64 | { |
| 65 | if (role == "priv-admin") |
| 66 | { |
| 67 | return "Administrator"; |
| 68 | } |
| 69 | else if (role == "priv-callback") |
| 70 | { |
| 71 | return "Callback"; |
| 72 | } |
| 73 | else if (role == "priv-user") |
| 74 | { |
| 75 | return "User"; |
| 76 | } |
| 77 | else if (role == "priv-operator") |
| 78 | { |
| 79 | return "Operator"; |
| 80 | } |
| 81 | return ""; |
| 82 | } |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 83 | inline std::string getRoleIdFromPrivilege(std::string_view role) |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 84 | { |
| 85 | if (role == "Administrator") |
| 86 | { |
| 87 | return "priv-admin"; |
| 88 | } |
| 89 | else if (role == "Callback") |
| 90 | { |
| 91 | return "priv-callback"; |
| 92 | } |
| 93 | else if (role == "User") |
| 94 | { |
| 95 | return "priv-user"; |
| 96 | } |
| 97 | else if (role == "Operator") |
| 98 | { |
| 99 | return "priv-operator"; |
| 100 | } |
| 101 | return ""; |
| 102 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 103 | |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame^] | 104 | void parseLDAPConfigData(nlohmann::json& json_response, |
| 105 | const LDAPConfigData& confData) |
| 106 | { |
| 107 | std::string service = "LDAPService"; |
| 108 | json_response["LDAP"] = { |
| 109 | {"AccountProviderType", service}, |
| 110 | {"AccountProviderType@Redfish.AllowableValues", |
| 111 | nlohmann::json::array({service})}, |
| 112 | {"ServiceEnabled", confData.serviceEnabled}, |
| 113 | {"ServiceAddresses", nlohmann::json::array({confData.uri})}, |
| 114 | {"Authentication", |
| 115 | {{"AuthenticationType", "UsernameAndPassword"}, |
| 116 | {"AuthenticationType@Redfish.AllowableValues", |
| 117 | nlohmann::json::array({"UsernameAndPassword"})}, |
| 118 | {"Username", confData.bindDN}, |
| 119 | {"Password", nullptr}}}, |
| 120 | {"LDAPService", |
| 121 | {{"SearchSettings", |
| 122 | {{"BaseDistinguishedNames", |
| 123 | nlohmann::json::array({confData.baseDN})}, |
| 124 | {"UsernameAttribute", confData.userNameAttribute}, |
| 125 | {"GroupsAttribute", confData.groupAttribute}}}}}, |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Function that retrieves all properties for LDAP config object |
| 131 | * into JSON |
| 132 | */ |
| 133 | template <typename CallbackFunc> |
| 134 | inline void getLDAPConfigData(const std::string& ldapType, |
| 135 | CallbackFunc&& callback) |
| 136 | { |
| 137 | auto getConfig = [callback, |
| 138 | ldapType](const boost::system::error_code error_code, |
| 139 | const ManagedObjectType& ldapObjects) { |
| 140 | LDAPConfigData confData{}; |
| 141 | if (error_code) |
| 142 | { |
| 143 | callback(false, confData); |
| 144 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << error_code; |
| 145 | return; |
| 146 | } |
| 147 | std::string ldapConfigObjectStr = std::string(ldapConfigObject); |
| 148 | std::string ldapEnableInterfaceStr = std::string(ldapEnableInterface); |
| 149 | std::string ldapConfigInterfaceStr = std::string(ldapConfigInterface); |
| 150 | for (const auto& object : ldapObjects) |
| 151 | { |
| 152 | if (object.first == ldapConfigObjectStr) |
| 153 | { |
| 154 | for (const auto& interface : object.second) |
| 155 | { |
| 156 | if (interface.first == ldapEnableInterfaceStr) |
| 157 | { |
| 158 | // rest of the properties are string. |
| 159 | for (const auto& property : interface.second) |
| 160 | { |
| 161 | if (property.first == "Enabled") |
| 162 | { |
| 163 | const bool* value = |
| 164 | std::get_if<bool>(&property.second); |
| 165 | if (value == nullptr) |
| 166 | { |
| 167 | continue; |
| 168 | } |
| 169 | confData.serviceEnabled = *value; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | else if (interface.first == ldapConfigInterfaceStr) |
| 175 | { |
| 176 | |
| 177 | for (const auto& property : interface.second) |
| 178 | { |
| 179 | const std::string* value = |
| 180 | std::get_if<std::string>(&property.second); |
| 181 | if (value == nullptr) |
| 182 | { |
| 183 | continue; |
| 184 | } |
| 185 | if (property.first == "LDAPServerURI") |
| 186 | { |
| 187 | confData.uri = *value; |
| 188 | } |
| 189 | else if (property.first == "LDAPBindDN") |
| 190 | { |
| 191 | confData.bindDN = *value; |
| 192 | } |
| 193 | else if (property.first == "LDAPBaseDN") |
| 194 | { |
| 195 | confData.baseDN = *value; |
| 196 | } |
| 197 | else if (property.first == "LDAPSearchScope") |
| 198 | { |
| 199 | confData.searchScope = *value; |
| 200 | } |
| 201 | else if (property.first == "LDAPType") |
| 202 | { |
| 203 | confData.serverType = *value; |
| 204 | } |
| 205 | else if (property.first == "GroupNameAttribute") |
| 206 | { |
| 207 | confData.groupAttribute = *value; |
| 208 | } |
| 209 | else if (property.first == "UserNameAttribute") |
| 210 | { |
| 211 | confData.userNameAttribute = *value; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | callback(true, confData); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | }; |
| 222 | auto getServiceName = [callback, getConfig(std::move(getConfig))]( |
| 223 | const boost::system::error_code ec, |
| 224 | const GetObjectType& resp) { |
| 225 | LDAPConfigData confData{}; |
| 226 | if (ec || resp.empty()) |
| 227 | { |
| 228 | BMCWEB_LOG_ERROR |
| 229 | << "DBUS response error during getting of service name: " << ec; |
| 230 | callback(false, confData); |
| 231 | return; |
| 232 | } |
| 233 | std::string service = resp.begin()->first; |
| 234 | crow::connections::systemBus->async_method_call( |
| 235 | std::move(getConfig), service, ldapRootObject, dbusObjManagerIntf, |
| 236 | "GetManagedObjects"); |
| 237 | }; |
| 238 | |
| 239 | const std::array<std::string, 2> interfaces = {ldapEnableInterface, |
| 240 | ldapConfigInterface}; |
| 241 | |
| 242 | crow::connections::systemBus->async_method_call( |
| 243 | std::move(getServiceName), mapperBusName, mapperObjectPath, mapperIntf, |
| 244 | "GetObject", ldapConfigObject, interfaces); |
| 245 | } |
| 246 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 247 | class AccountService : public Node |
| 248 | { |
| 249 | public: |
| 250 | AccountService(CrowApp& app) : Node(app, "/redfish/v1/AccountService/") |
| 251 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 252 | entityPrivileges = { |
| 253 | {boost::beast::http::verb::get, |
| 254 | {{"ConfigureUsers"}, {"ConfigureManager"}}}, |
| 255 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 256 | {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, |
| 257 | {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, |
| 258 | {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, |
| 259 | {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; |
| 260 | } |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 261 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 262 | private: |
| 263 | void doGet(crow::Response& res, const crow::Request& req, |
| 264 | const std::vector<std::string>& params) override |
| 265 | { |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 266 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 267 | res.jsonValue = { |
| 268 | {"@odata.context", "/redfish/v1/" |
| 269 | "$metadata#AccountService.AccountService"}, |
| 270 | {"@odata.id", "/redfish/v1/AccountService"}, |
| 271 | {"@odata.type", "#AccountService." |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame^] | 272 | "v1_3_1.AccountService"}, |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 273 | {"Id", "AccountService"}, |
| 274 | {"Name", "Account Service"}, |
| 275 | {"Description", "Account Service"}, |
| 276 | {"ServiceEnabled", true}, |
AppaRao Puli | 343ff2e | 2019-03-24 00:42:13 +0530 | [diff] [blame] | 277 | {"MaxPasswordLength", 20}, |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 278 | {"Accounts", |
| 279 | {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}}, |
| 280 | {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}}}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 281 | |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 282 | crow::connections::systemBus->async_method_call( |
| 283 | [asyncResp]( |
| 284 | const boost::system::error_code ec, |
| 285 | const std::vector<std::pair< |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 286 | std::string, std::variant<uint32_t, uint16_t, uint8_t>>>& |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 287 | propertiesList) { |
| 288 | if (ec) |
| 289 | { |
| 290 | messages::internalError(asyncResp->res); |
| 291 | return; |
| 292 | } |
| 293 | BMCWEB_LOG_DEBUG << "Got " << propertiesList.size() |
| 294 | << "properties for AccountService"; |
| 295 | for (const std::pair<std::string, |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 296 | std::variant<uint32_t, uint16_t, uint8_t>>& |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 297 | property : propertiesList) |
| 298 | { |
| 299 | if (property.first == "MinPasswordLength") |
| 300 | { |
| 301 | const uint8_t* value = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 302 | std::get_if<uint8_t>(&property.second); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 303 | if (value != nullptr) |
| 304 | { |
| 305 | asyncResp->res.jsonValue["MinPasswordLength"] = |
| 306 | *value; |
| 307 | } |
| 308 | } |
| 309 | if (property.first == "AccountUnlockTimeout") |
| 310 | { |
| 311 | const uint32_t* value = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 312 | std::get_if<uint32_t>(&property.second); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 313 | if (value != nullptr) |
| 314 | { |
| 315 | asyncResp->res.jsonValue["AccountLockoutDuration"] = |
| 316 | *value; |
| 317 | } |
| 318 | } |
| 319 | if (property.first == "MaxLoginAttemptBeforeLockout") |
| 320 | { |
| 321 | const uint16_t* value = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 322 | std::get_if<uint16_t>(&property.second); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 323 | if (value != nullptr) |
| 324 | { |
| 325 | asyncResp->res |
| 326 | .jsonValue["AccountLockoutThreshold"] = *value; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | }, |
| 331 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 332 | "org.freedesktop.DBus.Properties", "GetAll", |
| 333 | "xyz.openbmc_project.User.AccountPolicy"); |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame^] | 334 | |
| 335 | std::string ldapType = "LDAP"; |
| 336 | getLDAPConfigData( |
| 337 | ldapType, |
| 338 | [asyncResp, ldapType](bool success, LDAPConfigData& confData) { |
| 339 | parseLDAPConfigData(asyncResp->res.jsonValue, confData); |
| 340 | }); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 341 | } |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame^] | 342 | |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 343 | void doPatch(crow::Response& res, const crow::Request& req, |
| 344 | const std::vector<std::string>& params) override |
| 345 | { |
| 346 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 347 | |
| 348 | std::optional<uint32_t> unlockTimeout; |
| 349 | std::optional<uint16_t> lockoutThreshold; |
Ratan Gupta | 19fb6e7 | 2019-03-04 13:30:50 +0530 | [diff] [blame] | 350 | std::optional<uint16_t> minPasswordLength; |
| 351 | std::optional<uint16_t> maxPasswordLength; |
| 352 | |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 353 | if (!json_util::readJson(req, res, "AccountLockoutDuration", |
| 354 | unlockTimeout, "AccountLockoutThreshold", |
Ratan Gupta | 19fb6e7 | 2019-03-04 13:30:50 +0530 | [diff] [blame] | 355 | lockoutThreshold, "MaxPasswordLength", |
| 356 | maxPasswordLength, "MinPasswordLength", |
| 357 | minPasswordLength)) |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 358 | { |
| 359 | return; |
| 360 | } |
Ratan Gupta | 19fb6e7 | 2019-03-04 13:30:50 +0530 | [diff] [blame] | 361 | |
| 362 | if (minPasswordLength) |
| 363 | { |
| 364 | messages::propertyNotWritable(asyncResp->res, "MinPasswordLength"); |
| 365 | } |
| 366 | |
| 367 | if (maxPasswordLength) |
| 368 | { |
| 369 | messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength"); |
| 370 | } |
| 371 | |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 372 | if (unlockTimeout) |
| 373 | { |
| 374 | crow::connections::systemBus->async_method_call( |
| 375 | [asyncResp](const boost::system::error_code ec) { |
| 376 | if (ec) |
| 377 | { |
| 378 | messages::internalError(asyncResp->res); |
| 379 | return; |
| 380 | } |
Ratan Gupta | add6133 | 2019-02-13 20:49:16 +0530 | [diff] [blame] | 381 | messages::success(asyncResp->res); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 382 | }, |
| 383 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 384 | "org.freedesktop.DBus.Properties", "Set", |
| 385 | "xyz.openbmc_project.User.AccountPolicy", |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 386 | "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout)); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 387 | } |
| 388 | if (lockoutThreshold) |
| 389 | { |
| 390 | crow::connections::systemBus->async_method_call( |
| 391 | [asyncResp](const boost::system::error_code ec) { |
| 392 | if (ec) |
| 393 | { |
| 394 | messages::internalError(asyncResp->res); |
| 395 | return; |
| 396 | } |
Ratan Gupta | add6133 | 2019-02-13 20:49:16 +0530 | [diff] [blame] | 397 | messages::success(asyncResp->res); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 398 | }, |
| 399 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 400 | "org.freedesktop.DBus.Properties", "Set", |
| 401 | "xyz.openbmc_project.User.AccountPolicy", |
| 402 | "MaxLoginAttemptBeforeLockout", |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 403 | std::variant<uint16_t>(*lockoutThreshold)); |
AppaRao Puli | 3d958bb | 2018-12-25 12:45:54 +0530 | [diff] [blame] | 404 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 405 | } |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 406 | }; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 407 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 408 | class AccountsCollection : public Node |
| 409 | { |
| 410 | public: |
| 411 | AccountsCollection(CrowApp& app) : |
| 412 | Node(app, "/redfish/v1/AccountService/Accounts/") |
| 413 | { |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 414 | entityPrivileges = { |
| 415 | {boost::beast::http::verb::get, |
| 416 | {{"ConfigureUsers"}, {"ConfigureManager"}}}, |
| 417 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 418 | {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, |
| 419 | {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, |
| 420 | {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, |
| 421 | {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; |
| 422 | } |
| 423 | |
| 424 | private: |
| 425 | void doGet(crow::Response& res, const crow::Request& req, |
| 426 | const std::vector<std::string>& params) override |
| 427 | { |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 428 | auto asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 429 | res.jsonValue = {{"@odata.context", |
| 430 | "/redfish/v1/" |
| 431 | "$metadata#ManagerAccountCollection." |
| 432 | "ManagerAccountCollection"}, |
| 433 | {"@odata.id", "/redfish/v1/AccountService/Accounts"}, |
| 434 | {"@odata.type", "#ManagerAccountCollection." |
| 435 | "ManagerAccountCollection"}, |
| 436 | {"Name", "Accounts Collection"}, |
| 437 | {"Description", "BMC User Accounts"}}; |
| 438 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 439 | crow::connections::systemBus->async_method_call( |
| 440 | [asyncResp](const boost::system::error_code ec, |
| 441 | const ManagedObjectType& users) { |
| 442 | if (ec) |
| 443 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 444 | messages::internalError(asyncResp->res); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
| 448 | nlohmann::json& memberArray = |
| 449 | asyncResp->res.jsonValue["Members"]; |
| 450 | memberArray = nlohmann::json::array(); |
| 451 | |
| 452 | asyncResp->res.jsonValue["Members@odata.count"] = users.size(); |
| 453 | for (auto& user : users) |
| 454 | { |
| 455 | const std::string& path = |
| 456 | static_cast<const std::string&>(user.first); |
| 457 | std::size_t lastIndex = path.rfind("/"); |
| 458 | if (lastIndex == std::string::npos) |
| 459 | { |
| 460 | lastIndex = 0; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | lastIndex += 1; |
| 465 | } |
| 466 | memberArray.push_back( |
| 467 | {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + |
| 468 | path.substr(lastIndex)}}); |
| 469 | } |
| 470 | }, |
| 471 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 472 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 473 | } |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 474 | void doPost(crow::Response& res, const crow::Request& req, |
| 475 | const std::vector<std::string>& params) override |
| 476 | { |
| 477 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 478 | |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 479 | std::string username; |
| 480 | std::string password; |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 481 | std::optional<std::string> roleId("User"); |
| 482 | std::optional<bool> enabled = true; |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 483 | if (!json_util::readJson(req, res, "UserName", username, "Password", |
| 484 | password, "RoleId", roleId, "Enabled", |
| 485 | enabled)) |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 486 | { |
| 487 | return; |
| 488 | } |
| 489 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 490 | std::string priv = getRoleIdFromPrivilege(*roleId); |
| 491 | if (priv.empty()) |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 492 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 493 | messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId"); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 494 | return; |
| 495 | } |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 496 | roleId = priv; |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 497 | |
| 498 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 499 | [asyncResp, username, password{std::move(password)}]( |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 500 | const boost::system::error_code ec) { |
| 501 | if (ec) |
| 502 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 503 | messages::resourceAlreadyExists( |
| 504 | asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", |
| 505 | "UserName", username); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | |
| 509 | if (!pamUpdatePassword(username, password)) |
| 510 | { |
| 511 | // At this point we have a user that's been created, but the |
| 512 | // password set failed. Something is wrong, so delete the |
| 513 | // user that we've already created |
| 514 | crow::connections::systemBus->async_method_call( |
| 515 | [asyncResp](const boost::system::error_code ec) { |
| 516 | if (ec) |
| 517 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 518 | messages::internalError(asyncResp->res); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 522 | messages::invalidObject(asyncResp->res, "Password"); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 523 | }, |
| 524 | "xyz.openbmc_project.User.Manager", |
| 525 | "/xyz/openbmc_project/user/" + username, |
| 526 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 527 | |
| 528 | BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; |
| 529 | return; |
| 530 | } |
| 531 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 532 | messages::created(asyncResp->res); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 533 | asyncResp->res.addHeader( |
| 534 | "Location", |
| 535 | "/redfish/v1/AccountService/Accounts/" + username); |
| 536 | }, |
| 537 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 538 | "xyz.openbmc_project.User.Manager", "CreateUser", username, |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 539 | std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"}, |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 540 | *roleId, *enabled); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 541 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 542 | }; |
| 543 | |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 544 | template <typename Callback> |
| 545 | inline void checkDbusPathExists(const std::string& path, Callback&& callback) |
| 546 | { |
| 547 | using GetObjectType = |
| 548 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 549 | |
| 550 | crow::connections::systemBus->async_method_call( |
| 551 | [callback{std::move(callback)}](const boost::system::error_code ec, |
| 552 | const GetObjectType& object_names) { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 553 | callback(!ec && object_names.size() != 0); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 554 | }, |
| 555 | "xyz.openbmc_project.ObjectMapper", |
| 556 | "/xyz/openbmc_project/object_mapper", |
| 557 | "xyz.openbmc_project.ObjectMapper", "GetObject", path, |
| 558 | std::array<std::string, 0>()); |
| 559 | } |
| 560 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 561 | class ManagerAccount : public Node |
| 562 | { |
| 563 | public: |
| 564 | ManagerAccount(CrowApp& app) : |
| 565 | Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string()) |
| 566 | { |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 567 | entityPrivileges = { |
| 568 | {boost::beast::http::verb::get, |
| 569 | {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}}, |
| 570 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 571 | {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, |
| 572 | {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, |
| 573 | {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, |
| 574 | {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | void doGet(crow::Response& res, const crow::Request& req, |
| 579 | const std::vector<std::string>& params) override |
| 580 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 581 | res.jsonValue = { |
| 582 | {"@odata.context", |
| 583 | "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"}, |
| 584 | {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"}, |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 585 | {"Name", "User Account"}, |
| 586 | {"Description", "User Account"}, |
| 587 | {"Password", nullptr}, |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 588 | {"RoleId", "Administrator"}}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 589 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 590 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 591 | |
| 592 | if (params.size() != 1) |
| 593 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 594 | messages::internalError(asyncResp->res); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
| 598 | crow::connections::systemBus->async_method_call( |
| 599 | [asyncResp, accountName{std::string(params[0])}]( |
| 600 | const boost::system::error_code ec, |
| 601 | const ManagedObjectType& users) { |
| 602 | if (ec) |
| 603 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 604 | messages::internalError(asyncResp->res); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 605 | return; |
| 606 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 607 | auto userIt = users.begin(); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 608 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 609 | for (; userIt != users.end(); userIt++) |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 610 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 611 | if (boost::ends_with(userIt->first.str, "/" + accountName)) |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 612 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 613 | break; |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 614 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 615 | } |
| 616 | if (userIt == users.end()) |
| 617 | { |
| 618 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", |
| 619 | accountName); |
| 620 | return; |
| 621 | } |
| 622 | for (const auto& interface : userIt->second) |
| 623 | { |
| 624 | if (interface.first == |
| 625 | "xyz.openbmc_project.User.Attributes") |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 626 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 627 | for (const auto& property : interface.second) |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 628 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 629 | if (property.first == "UserEnabled") |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 630 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 631 | const bool* userEnabled = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 632 | std::get_if<bool>(&property.second); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 633 | if (userEnabled == nullptr) |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 634 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 635 | BMCWEB_LOG_ERROR |
| 636 | << "UserEnabled wasn't a bool"; |
| 637 | messages::internalError(asyncResp->res); |
| 638 | return; |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 639 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 640 | asyncResp->res.jsonValue["Enabled"] = |
| 641 | *userEnabled; |
| 642 | } |
| 643 | else if (property.first == |
| 644 | "UserLockedForFailedAttempt") |
| 645 | { |
| 646 | const bool* userLocked = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 647 | std::get_if<bool>(&property.second); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 648 | if (userLocked == nullptr) |
| 649 | { |
| 650 | BMCWEB_LOG_ERROR << "UserLockedForF" |
| 651 | "ailedAttempt " |
| 652 | "wasn't a bool"; |
| 653 | messages::internalError(asyncResp->res); |
| 654 | return; |
| 655 | } |
| 656 | asyncResp->res.jsonValue["Locked"] = |
| 657 | *userLocked; |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 658 | asyncResp->res.jsonValue |
| 659 | ["Locked@Redfish.AllowableValues"] = { |
Gunnar Mills | 4d64ce3 | 2019-03-29 16:34:56 -0500 | [diff] [blame] | 660 | "false"}; |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 661 | } |
| 662 | else if (property.first == "UserPrivilege") |
| 663 | { |
| 664 | const std::string* userRolePtr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 665 | std::get_if<std::string>(&property.second); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 666 | if (userRolePtr == nullptr) |
| 667 | { |
| 668 | BMCWEB_LOG_ERROR |
| 669 | << "UserPrivilege wasn't a " |
| 670 | "string"; |
| 671 | messages::internalError(asyncResp->res); |
| 672 | return; |
| 673 | } |
| 674 | std::string priv = |
| 675 | getPrivilegeFromRoleId(*userRolePtr); |
| 676 | if (priv.empty()) |
| 677 | { |
| 678 | BMCWEB_LOG_ERROR << "Invalid user role"; |
| 679 | messages::internalError(asyncResp->res); |
| 680 | return; |
| 681 | } |
| 682 | asyncResp->res.jsonValue["RoleId"] = priv; |
| 683 | |
| 684 | asyncResp->res.jsonValue["Links"]["Role"] = { |
| 685 | {"@odata.id", "/redfish/v1/AccountService/" |
| 686 | "Roles/" + |
| 687 | priv}}; |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 693 | asyncResp->res.jsonValue["@odata.id"] = |
| 694 | "/redfish/v1/AccountService/Accounts/" + accountName; |
| 695 | asyncResp->res.jsonValue["Id"] = accountName; |
| 696 | asyncResp->res.jsonValue["UserName"] = accountName; |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 697 | }, |
| 698 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 699 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 700 | } |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 701 | |
| 702 | void doPatch(crow::Response& res, const crow::Request& req, |
| 703 | const std::vector<std::string>& params) override |
| 704 | { |
| 705 | auto asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 706 | if (params.size() != 1) |
| 707 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 708 | messages::internalError(asyncResp->res); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 709 | return; |
| 710 | } |
| 711 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 712 | std::optional<std::string> newUserName; |
| 713 | std::optional<std::string> password; |
| 714 | std::optional<bool> enabled; |
| 715 | std::optional<std::string> roleId; |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 716 | std::optional<bool> locked; |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 717 | if (!json_util::readJson(req, res, "UserName", newUserName, "Password", |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 718 | password, "RoleId", roleId, "Enabled", enabled, |
| 719 | "Locked", locked)) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 720 | { |
| 721 | return; |
| 722 | } |
| 723 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 724 | const std::string& username = params[0]; |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 725 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 726 | if (!newUserName) |
| 727 | { |
| 728 | // If the username isn't being updated, we can update the properties |
| 729 | // directly |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 730 | updateUserProperties(asyncResp, username, password, enabled, roleId, |
| 731 | locked); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | crow::connections::systemBus->async_method_call( |
| 737 | [this, asyncResp, username, password(std::move(password)), |
| 738 | roleId(std::move(roleId)), enabled(std::move(enabled)), |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 739 | newUser{std::string(*newUserName)}, locked(std::move(locked))]( |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 740 | const boost::system::error_code ec) { |
| 741 | if (ec) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 742 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 743 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 744 | messages::resourceNotFound( |
| 745 | asyncResp->res, |
| 746 | "#ManagerAccount.v1_0_3.ManagerAccount", username); |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | updateUserProperties(asyncResp, newUser, password, enabled, |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 751 | roleId, locked); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 752 | }, |
| 753 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 754 | "xyz.openbmc_project.User.Manager", "RenameUser", username, |
| 755 | *newUserName); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp, |
| 760 | const std::string& username, |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 761 | std::optional<std::string> password, |
| 762 | std::optional<bool> enabled, |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 763 | std::optional<std::string> roleId, |
| 764 | std::optional<bool> locked) |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 765 | { |
| 766 | if (password) |
| 767 | { |
| 768 | if (!pamUpdatePassword(username, *password)) |
| 769 | { |
| 770 | BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; |
| 771 | messages::internalError(asyncResp->res); |
| 772 | return; |
| 773 | } |
| 774 | } |
| 775 | |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 776 | std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username; |
| 777 | dbus::utility::escapePathForDbus(dbusObjectPath); |
| 778 | |
| 779 | checkDbusPathExists( |
| 780 | dbusObjectPath, |
| 781 | [dbusObjectPath(std::move(dbusObjectPath)), username, |
| 782 | password(std::move(password)), roleId(std::move(roleId)), |
| 783 | enabled(std::move(enabled)), locked(std::move(locked)), |
| 784 | asyncResp{std::move(asyncResp)}](int rc) { |
| 785 | if (!rc) |
| 786 | { |
| 787 | messages::invalidObject(asyncResp->res, username.c_str()); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 788 | return; |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 789 | } |
| 790 | if (enabled) |
| 791 | { |
| 792 | crow::connections::systemBus->async_method_call( |
| 793 | [asyncResp](const boost::system::error_code ec) { |
| 794 | if (ec) |
| 795 | { |
| 796 | BMCWEB_LOG_ERROR << "D-Bus responses error: " |
| 797 | << ec; |
| 798 | messages::internalError(asyncResp->res); |
| 799 | return; |
| 800 | } |
| 801 | messages::success(asyncResp->res); |
| 802 | return; |
| 803 | }, |
| 804 | "xyz.openbmc_project.User.Manager", |
| 805 | dbusObjectPath.c_str(), |
| 806 | "org.freedesktop.DBus.Properties", "Set", |
| 807 | "xyz.openbmc_project.User.Attributes", "UserEnabled", |
| 808 | std::variant<bool>{*enabled}); |
| 809 | } |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 810 | |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 811 | if (roleId) |
| 812 | { |
| 813 | std::string priv = getRoleIdFromPrivilege(*roleId); |
| 814 | if (priv.empty()) |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 815 | { |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 816 | messages::propertyValueNotInList(asyncResp->res, |
| 817 | *roleId, "RoleId"); |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 818 | return; |
| 819 | } |
Ratan Gupta | 24c8542 | 2019-01-30 19:41:24 +0530 | [diff] [blame] | 820 | |
| 821 | crow::connections::systemBus->async_method_call( |
| 822 | [asyncResp](const boost::system::error_code ec) { |
| 823 | if (ec) |
| 824 | { |
| 825 | BMCWEB_LOG_ERROR << "D-Bus responses error: " |
| 826 | << ec; |
| 827 | messages::internalError(asyncResp->res); |
| 828 | return; |
| 829 | } |
| 830 | messages::success(asyncResp->res); |
| 831 | }, |
| 832 | "xyz.openbmc_project.User.Manager", |
| 833 | dbusObjectPath.c_str(), |
| 834 | "org.freedesktop.DBus.Properties", "Set", |
| 835 | "xyz.openbmc_project.User.Attributes", "UserPrivilege", |
| 836 | std::variant<std::string>{priv}); |
| 837 | } |
| 838 | |
| 839 | if (locked) |
| 840 | { |
| 841 | // admin can unlock the account which is locked by |
| 842 | // successive authentication failures but admin should not |
| 843 | // be allowed to lock an account. |
| 844 | if (*locked) |
| 845 | { |
| 846 | messages::propertyValueNotInList(asyncResp->res, "true", |
| 847 | "Locked"); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | crow::connections::systemBus->async_method_call( |
| 852 | [asyncResp](const boost::system::error_code ec) { |
| 853 | if (ec) |
| 854 | { |
| 855 | BMCWEB_LOG_ERROR << "D-Bus responses error: " |
| 856 | << ec; |
| 857 | messages::internalError(asyncResp->res); |
| 858 | return; |
| 859 | } |
| 860 | messages::success(asyncResp->res); |
| 861 | return; |
| 862 | }, |
| 863 | "xyz.openbmc_project.User.Manager", |
| 864 | dbusObjectPath.c_str(), |
| 865 | "org.freedesktop.DBus.Properties", "Set", |
| 866 | "xyz.openbmc_project.User.Attributes", |
| 867 | "UserLockedForFailedAttempt", |
| 868 | sdbusplus::message::variant<bool>{*locked}); |
| 869 | } |
| 870 | }); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 871 | } |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 872 | |
| 873 | void doDelete(crow::Response& res, const crow::Request& req, |
| 874 | const std::vector<std::string>& params) override |
| 875 | { |
| 876 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 877 | |
| 878 | if (params.size() != 1) |
| 879 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 880 | messages::internalError(asyncResp->res); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
| 884 | const std::string userPath = "/xyz/openbmc_project/user/" + params[0]; |
| 885 | |
| 886 | crow::connections::systemBus->async_method_call( |
| 887 | [asyncResp, username{std::move(params[0])}]( |
| 888 | const boost::system::error_code ec) { |
| 889 | if (ec) |
| 890 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 891 | messages::resourceNotFound( |
| 892 | asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", |
| 893 | username); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 894 | return; |
| 895 | } |
| 896 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 897 | messages::accountRemoved(asyncResp->res); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 898 | }, |
| 899 | "xyz.openbmc_project.User.Manager", userPath, |
| 900 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 901 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 902 | }; |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 903 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 904 | } // namespace redfish |