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 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "app.hpp" |
| 19 | #include "dbus_utility.hpp" |
| 20 | #include "error_messages.hpp" |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 21 | #include "generated/enums/account_service.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 22 | #include "openbmc_dbus_rest.hpp" |
| 23 | #include "persistent_data.hpp" |
| 24 | #include "query.hpp" |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 25 | #include "registries/privilege_registry.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 26 | #include "utils/dbus_utils.hpp" |
| 27 | #include "utils/json_utils.hpp" |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 28 | |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 29 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 30 | #include <sdbusplus/unpack_properties.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 31 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 32 | #include <array> |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 33 | #include <optional> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 34 | #include <ranges> |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 35 | #include <string> |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 36 | #include <string_view> |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 39 | namespace redfish |
| 40 | { |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 41 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 42 | constexpr const char* ldapConfigObjectName = |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 43 | "/xyz/openbmc_project/user/ldap/openldap"; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 44 | constexpr const char* adConfigObject = |
Ratan Gupta | ab828d7 | 2019-04-22 14:18:33 +0530 | [diff] [blame] | 45 | "/xyz/openbmc_project/user/ldap/active_directory"; |
| 46 | |
P Dheeraj Srujan Kumar | b477fd4 | 2021-12-16 07:17:51 +0530 | [diff] [blame] | 47 | constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/"; |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 48 | constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap"; |
| 49 | constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config"; |
| 50 | constexpr const char* ldapConfigInterface = |
| 51 | "xyz.openbmc_project.User.Ldap.Config"; |
| 52 | constexpr const char* ldapCreateInterface = |
| 53 | "xyz.openbmc_project.User.Ldap.Create"; |
| 54 | constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable"; |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 55 | constexpr const char* ldapPrivMapperInterface = |
| 56 | "xyz.openbmc_project.User.PrivilegeMapper"; |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 57 | |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 58 | struct LDAPRoleMapData |
| 59 | { |
| 60 | std::string groupName; |
| 61 | std::string privilege; |
| 62 | }; |
| 63 | |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 64 | struct LDAPConfigData |
| 65 | { |
| 66 | std::string uri{}; |
| 67 | std::string bindDN{}; |
| 68 | std::string baseDN{}; |
| 69 | std::string searchScope{}; |
| 70 | std::string serverType{}; |
| 71 | bool serviceEnabled = false; |
| 72 | std::string userNameAttribute{}; |
| 73 | std::string groupAttribute{}; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 74 | std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList; |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 75 | }; |
| 76 | |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 77 | inline std::string getRoleIdFromPrivilege(std::string_view role) |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 78 | { |
| 79 | if (role == "priv-admin") |
| 80 | { |
| 81 | return "Administrator"; |
| 82 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 83 | if (role == "priv-user") |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 84 | { |
AppaRao Puli | c80fee5 | 2019-10-16 14:49:36 +0530 | [diff] [blame] | 85 | return "ReadOnly"; |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 86 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 87 | if (role == "priv-operator") |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 88 | { |
| 89 | return "Operator"; |
| 90 | } |
| 91 | return ""; |
| 92 | } |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 93 | inline std::string getPrivilegeFromRoleId(std::string_view role) |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 94 | { |
| 95 | if (role == "Administrator") |
| 96 | { |
| 97 | return "priv-admin"; |
| 98 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 99 | if (role == "ReadOnly") |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 100 | { |
| 101 | return "priv-user"; |
| 102 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 103 | if (role == "Operator") |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame] | 104 | { |
| 105 | return "priv-operator"; |
| 106 | } |
| 107 | return ""; |
| 108 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 109 | |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 110 | /** |
| 111 | * @brief Maps user group names retrieved from D-Bus object to |
| 112 | * Account Types. |
| 113 | * |
| 114 | * @param[in] userGroups List of User groups |
| 115 | * @param[out] res AccountTypes populated |
| 116 | * |
| 117 | * @return true in case of success, false if UserGroups contains |
| 118 | * invalid group name(s). |
| 119 | */ |
| 120 | inline bool translateUserGroup(const std::vector<std::string>& userGroups, |
| 121 | crow::Response& res) |
| 122 | { |
| 123 | std::vector<std::string> accountTypes; |
| 124 | for (const auto& userGroup : userGroups) |
| 125 | { |
| 126 | if (userGroup == "redfish") |
| 127 | { |
| 128 | accountTypes.emplace_back("Redfish"); |
| 129 | accountTypes.emplace_back("WebUI"); |
| 130 | } |
| 131 | else if (userGroup == "ipmi") |
| 132 | { |
| 133 | accountTypes.emplace_back("IPMI"); |
| 134 | } |
| 135 | else if (userGroup == "ssh") |
| 136 | { |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 137 | accountTypes.emplace_back("ManagerConsole"); |
| 138 | } |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 139 | else if (userGroup == "hostconsole") |
| 140 | { |
| 141 | // The hostconsole group controls who can access the host console |
| 142 | // port via ssh and websocket. |
| 143 | accountTypes.emplace_back("HostConsole"); |
| 144 | } |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 145 | else if (userGroup == "web") |
| 146 | { |
| 147 | // 'web' is one of the valid groups in the UserGroups property of |
| 148 | // the user account in the D-Bus object. This group is currently not |
| 149 | // doing anything, and is considered to be equivalent to 'redfish'. |
| 150 | // 'redfish' user group is mapped to 'Redfish'and 'WebUI' |
| 151 | // AccountTypes, so do nothing here... |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | // Invalid user group name. Caller throws an excption. |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | res.jsonValue["AccountTypes"] = std::move(accountTypes); |
| 161 | return true; |
| 162 | } |
| 163 | |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 164 | /** |
| 165 | * @brief Builds User Groups from the Account Types |
| 166 | * |
| 167 | * @param[in] asyncResp Async Response |
| 168 | * @param[in] accountTypes List of Account Types |
| 169 | * @param[out] userGroups List of User Groups mapped from Account Types |
| 170 | * |
| 171 | * @return true if Account Types mapped to User Groups, false otherwise. |
| 172 | */ |
| 173 | inline bool |
| 174 | getUserGroupFromAccountType(crow::Response& res, |
| 175 | const std::vector<std::string>& accountTypes, |
| 176 | std::vector<std::string>& userGroups) |
| 177 | { |
| 178 | // Need both Redfish and WebUI Account Types to map to 'redfish' User Group |
| 179 | bool redfishType = false; |
| 180 | bool webUIType = false; |
| 181 | |
| 182 | for (const auto& accountType : accountTypes) |
| 183 | { |
| 184 | if (accountType == "Redfish") |
| 185 | { |
| 186 | redfishType = true; |
| 187 | } |
| 188 | else if (accountType == "WebUI") |
| 189 | { |
| 190 | webUIType = true; |
| 191 | } |
| 192 | else if (accountType == "IPMI") |
| 193 | { |
| 194 | userGroups.emplace_back("ipmi"); |
| 195 | } |
| 196 | else if (accountType == "HostConsole") |
| 197 | { |
| 198 | userGroups.emplace_back("hostconsole"); |
| 199 | } |
| 200 | else if (accountType == "ManagerConsole") |
| 201 | { |
| 202 | userGroups.emplace_back("ssh"); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | // Invalid Account Type |
| 207 | messages::propertyValueNotInList(res, "AccountTypes", accountType); |
| 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Both Redfish and WebUI Account Types are needed to PATCH |
| 213 | if (redfishType ^ webUIType) |
| 214 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 215 | BMCWEB_LOG_ERROR( |
| 216 | "Missing Redfish or WebUI Account Type to set redfish User Group"); |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 217 | messages::strictAccountTypes(res, "AccountTypes"); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | if (redfishType && webUIType) |
| 222 | { |
| 223 | userGroups.emplace_back("redfish"); |
| 224 | } |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @brief Sets UserGroups property of the user based on the Account Types |
| 231 | * |
| 232 | * @param[in] accountTypes List of User Account Types |
| 233 | * @param[in] asyncResp Async Response |
| 234 | * @param[in] dbusObjectPath D-Bus Object Path |
| 235 | * @param[in] userSelf true if User is updating OWN Account Types |
| 236 | */ |
| 237 | inline void |
| 238 | patchAccountTypes(const std::vector<std::string>& accountTypes, |
| 239 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 240 | const std::string& dbusObjectPath, bool userSelf) |
| 241 | { |
| 242 | // Check if User is disabling own Redfish Account Type |
| 243 | if (userSelf && |
| 244 | (accountTypes.cend() == |
| 245 | std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish"))) |
| 246 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 247 | BMCWEB_LOG_ERROR( |
| 248 | "User disabling OWN Redfish Account Type is not allowed"); |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 249 | messages::strictAccountTypes(asyncResp->res, "AccountTypes"); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | std::vector<std::string> updatedUserGroups; |
| 254 | if (!getUserGroupFromAccountType(asyncResp->res, accountTypes, |
| 255 | updatedUserGroups)) |
| 256 | { |
| 257 | // Problem in mapping Account Types to User Groups, Error already |
| 258 | // logged. |
| 259 | return; |
| 260 | } |
| 261 | |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 262 | sdbusplus::asio::setProperty( |
| 263 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 264 | dbusObjectPath, "xyz.openbmc_project.User.Attributes", "UserGroups", |
| 265 | updatedUserGroups, [asyncResp](const boost::system::error_code& ec) { |
| 266 | if (ec) |
| 267 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 268 | BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 269 | messages::internalError(asyncResp->res); |
| 270 | return; |
| 271 | } |
| 272 | messages::success(asyncResp->res); |
| 273 | }); |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 274 | } |
| 275 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 276 | inline void userErrorMessageHandler( |
| 277 | const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 278 | const std::string& newUser, const std::string& username) |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 279 | { |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 280 | if (e == nullptr) |
| 281 | { |
| 282 | messages::internalError(asyncResp->res); |
| 283 | return; |
| 284 | } |
| 285 | |
Manojkiran Eda | 055806b | 2020-11-03 09:36:28 +0530 | [diff] [blame] | 286 | const char* errorMessage = e->name; |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 287 | if (strcmp(errorMessage, |
| 288 | "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0) |
| 289 | { |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 290 | messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount", |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 291 | "UserName", newUser); |
| 292 | } |
| 293 | else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error." |
| 294 | "UserNameDoesNotExist") == 0) |
| 295 | { |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 296 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", username); |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 297 | } |
Ed Tanous | d4d2579 | 2020-09-29 15:15:03 -0700 | [diff] [blame] | 298 | else if ((strcmp(errorMessage, |
| 299 | "xyz.openbmc_project.Common.Error.InvalidArgument") == |
| 300 | 0) || |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 301 | (strcmp( |
| 302 | errorMessage, |
| 303 | "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") == |
| 304 | 0)) |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 305 | { |
| 306 | messages::propertyValueFormatError(asyncResp->res, newUser, "UserName"); |
| 307 | } |
| 308 | else if (strcmp(errorMessage, |
| 309 | "xyz.openbmc_project.User.Common.Error.NoResource") == 0) |
| 310 | { |
| 311 | messages::createLimitReachedForResource(asyncResp->res); |
| 312 | } |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 313 | else |
| 314 | { |
| 315 | messages::internalError(asyncResp->res); |
| 316 | } |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 319 | inline void parseLDAPConfigData(nlohmann::json& jsonResponse, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 320 | const LDAPConfigData& confData, |
| 321 | const std::string& ldapType) |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 322 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 323 | std::string service = (ldapType == "LDAP") ? "LDAPService" |
| 324 | : "ActiveDirectoryService"; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 325 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 326 | nlohmann::json& ldap = jsonResponse[ldapType]; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 327 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 328 | ldap["ServiceEnabled"] = confData.serviceEnabled; |
| 329 | ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri}); |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 330 | ldap["Authentication"]["AuthenticationType"] = |
| 331 | account_service::AuthenticationTypes::UsernameAndPassword; |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 332 | ldap["Authentication"]["Username"] = confData.bindDN; |
| 333 | ldap["Authentication"]["Password"] = nullptr; |
| 334 | |
| 335 | ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] = |
| 336 | nlohmann::json::array({confData.baseDN}); |
| 337 | ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] = |
| 338 | confData.userNameAttribute; |
| 339 | ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] = |
| 340 | confData.groupAttribute; |
| 341 | |
| 342 | nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"]; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 343 | roleMapArray = nlohmann::json::array(); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 344 | for (const auto& obj : confData.groupRoleList) |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 345 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 346 | BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName); |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 347 | |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 348 | nlohmann::json::object_t remoteGroup; |
| 349 | remoteGroup["RemoteGroup"] = obj.second.groupName; |
Jorge Cisneros | 329f034 | 2022-11-04 16:26:25 +0000 | [diff] [blame] | 350 | remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege); |
| 351 | roleMapArray.emplace_back(std::move(remoteGroup)); |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 352 | } |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 356 | * @brief validates given JSON input and then calls appropriate method to |
| 357 | * create, to delete or to set Rolemapping object based on the given input. |
| 358 | * |
| 359 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 360 | inline void handleRoleMapPatch( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 361 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 362 | const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData, |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 363 | const std::string& serverType, const std::vector<nlohmann::json>& input) |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 364 | { |
| 365 | for (size_t index = 0; index < input.size(); index++) |
| 366 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 367 | const nlohmann::json& thisJson = input[index]; |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 368 | |
| 369 | if (thisJson.is_null()) |
| 370 | { |
| 371 | // delete the existing object |
| 372 | if (index < roleMapObjData.size()) |
| 373 | { |
| 374 | crow::connections::systemBus->async_method_call( |
| 375 | [asyncResp, roleMapObjData, serverType, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 376 | index](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 377 | if (ec) |
| 378 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 379 | BMCWEB_LOG_ERROR("DBUS response error: {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 380 | messages::internalError(asyncResp->res); |
| 381 | return; |
| 382 | } |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 383 | asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"] |
| 384 | [index] = nullptr; |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 385 | }, |
| 386 | ldapDbusService, roleMapObjData[index].first, |
| 387 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 388 | } |
| 389 | else |
| 390 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 391 | BMCWEB_LOG_ERROR("Can't delete the object"); |
Ed Tanous | 2e8c4bd | 2022-06-27 12:59:12 -0700 | [diff] [blame] | 392 | messages::propertyValueTypeError(asyncResp->res, thisJson, |
| 393 | "RemoteRoleMapping/" + |
| 394 | std::to_string(index)); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | } |
| 398 | else if (thisJson.empty()) |
| 399 | { |
| 400 | // Don't do anything for the empty objects,parse next json |
| 401 | // eg {"RemoteRoleMapping",[{}]} |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | // update/create the object |
| 406 | std::optional<std::string> remoteGroup; |
| 407 | std::optional<std::string> localRole; |
| 408 | |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 409 | // This is a copy, but it's required in this case because of how |
| 410 | // readJson is structured |
| 411 | nlohmann::json thisJsonCopy = thisJson; |
| 412 | if (!json_util::readJson(thisJsonCopy, asyncResp->res, |
| 413 | "RemoteGroup", remoteGroup, "LocalRole", |
| 414 | localRole)) |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 415 | { |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | // Update existing RoleMapping Object |
| 420 | if (index < roleMapObjData.size()) |
| 421 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 422 | BMCWEB_LOG_DEBUG("Update Role Map Object"); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 423 | // If "RemoteGroup" info is provided |
| 424 | if (remoteGroup) |
| 425 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 426 | sdbusplus::asio::setProperty( |
| 427 | *crow::connections::systemBus, ldapDbusService, |
| 428 | roleMapObjData[index].first, |
| 429 | "xyz.openbmc_project.User.PrivilegeMapperEntry", |
| 430 | "GroupName", *remoteGroup, |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 431 | [asyncResp, roleMapObjData, serverType, index, |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 432 | remoteGroup](const boost::system::error_code& ec, |
Patrick Williams | 7a54389 | 2023-06-24 03:07:55 -0500 | [diff] [blame] | 433 | const sdbusplus::message_t& msg) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 434 | if (ec) |
| 435 | { |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 436 | const sd_bus_error* dbusError = msg.get_error(); |
| 437 | if ((dbusError != nullptr) && |
| 438 | (dbusError->name == |
| 439 | std::string_view( |
| 440 | "xyz.openbmc_project.Common.Error.InvalidArgument"))) |
| 441 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 442 | BMCWEB_LOG_WARNING("DBUS response error: {}", |
| 443 | ec); |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 444 | messages::propertyValueIncorrect(asyncResp->res, |
| 445 | "RemoteGroup", |
| 446 | *remoteGroup); |
| 447 | return; |
| 448 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 449 | messages::internalError(asyncResp->res); |
| 450 | return; |
| 451 | } |
| 452 | asyncResp->res |
| 453 | .jsonValue[serverType]["RemoteRoleMapping"][index] |
| 454 | ["RemoteGroup"] = *remoteGroup; |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 455 | }); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // If "LocalRole" info is provided |
| 459 | if (localRole) |
| 460 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 461 | sdbusplus::asio::setProperty( |
| 462 | *crow::connections::systemBus, ldapDbusService, |
| 463 | roleMapObjData[index].first, |
| 464 | "xyz.openbmc_project.User.PrivilegeMapperEntry", |
| 465 | "Privilege", *localRole, |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 466 | [asyncResp, roleMapObjData, serverType, index, |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 467 | localRole](const boost::system::error_code& ec, |
Patrick Williams | 7a54389 | 2023-06-24 03:07:55 -0500 | [diff] [blame] | 468 | const sdbusplus::message_t& msg) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 469 | if (ec) |
| 470 | { |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 471 | const sd_bus_error* dbusError = msg.get_error(); |
| 472 | if ((dbusError != nullptr) && |
| 473 | (dbusError->name == |
| 474 | std::string_view( |
| 475 | "xyz.openbmc_project.Common.Error.InvalidArgument"))) |
| 476 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 477 | BMCWEB_LOG_WARNING("DBUS response error: {}", |
| 478 | ec); |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 479 | messages::propertyValueIncorrect( |
| 480 | asyncResp->res, "LocalRole", *localRole); |
| 481 | return; |
| 482 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 483 | messages::internalError(asyncResp->res); |
| 484 | return; |
| 485 | } |
| 486 | asyncResp->res |
| 487 | .jsonValue[serverType]["RemoteRoleMapping"][index] |
| 488 | ["LocalRole"] = *localRole; |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 489 | }); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | // Create a new RoleMapping Object. |
| 493 | else |
| 494 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 495 | BMCWEB_LOG_DEBUG( |
| 496 | "setRoleMappingProperties: Creating new Object"); |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 497 | std::string pathString = "RemoteRoleMapping/" + |
| 498 | std::to_string(index); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 499 | |
| 500 | if (!localRole) |
| 501 | { |
| 502 | messages::propertyMissing(asyncResp->res, |
| 503 | pathString + "/LocalRole"); |
| 504 | continue; |
| 505 | } |
| 506 | if (!remoteGroup) |
| 507 | { |
| 508 | messages::propertyMissing(asyncResp->res, |
| 509 | pathString + "/RemoteGroup"); |
| 510 | continue; |
| 511 | } |
| 512 | |
| 513 | std::string dbusObjectPath; |
| 514 | if (serverType == "ActiveDirectory") |
| 515 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 516 | dbusObjectPath = adConfigObject; |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 517 | } |
| 518 | else if (serverType == "LDAP") |
| 519 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 520 | dbusObjectPath = ldapConfigObjectName; |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 521 | } |
| 522 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 523 | BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup, |
| 524 | *localRole); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 525 | |
| 526 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 527 | [asyncResp, serverType, localRole, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 528 | remoteGroup](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 529 | if (ec) |
| 530 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 531 | BMCWEB_LOG_ERROR("DBUS response error: {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 532 | messages::internalError(asyncResp->res); |
| 533 | return; |
| 534 | } |
| 535 | nlohmann::json& remoteRoleJson = |
| 536 | asyncResp->res |
| 537 | .jsonValue[serverType]["RemoteRoleMapping"]; |
| 538 | nlohmann::json::object_t roleMapEntry; |
| 539 | roleMapEntry["LocalRole"] = *localRole; |
| 540 | roleMapEntry["RemoteGroup"] = *remoteGroup; |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 541 | remoteRoleJson.emplace_back(std::move(roleMapEntry)); |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 542 | }, |
| 543 | ldapDbusService, dbusObjectPath, ldapPrivMapperInterface, |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 544 | "Create", *remoteGroup, |
Ratan Gupta | 0678524 | 2019-07-26 22:30:16 +0530 | [diff] [blame] | 545 | getPrivilegeFromRoleId(std::move(*localRole))); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 552 | * Function that retrieves all properties for LDAP config object |
| 553 | * into JSON |
| 554 | */ |
| 555 | template <typename CallbackFunc> |
| 556 | inline void getLDAPConfigData(const std::string& ldapType, |
| 557 | CallbackFunc&& callback) |
| 558 | { |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 559 | constexpr std::array<std::string_view, 2> interfaces = { |
| 560 | ldapEnableInterface, ldapConfigInterface}; |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 561 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 562 | dbus::utility::getDbusObject( |
| 563 | ldapConfigObjectName, interfaces, |
| 564 | [callback, ldapType](const boost::system::error_code& ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 565 | const dbus::utility::MapperGetObject& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 566 | if (ec || resp.empty()) |
| 567 | { |
Carson Labrado | bf2dded | 2023-08-10 00:37:06 +0000 | [diff] [blame] | 568 | BMCWEB_LOG_WARNING( |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 569 | "DBUS response error during getting of service name: {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 570 | LDAPConfigData empty{}; |
| 571 | callback(false, empty, ldapType); |
| 572 | return; |
| 573 | } |
| 574 | std::string service = resp.begin()->first; |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 575 | sdbusplus::message::object_path path(ldapRootObject); |
| 576 | dbus::utility::getManagedObjects( |
| 577 | service, path, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 578 | [callback, |
Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 579 | ldapType](const boost::system::error_code& ec2, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 580 | const dbus::utility::ManagedObjectType& ldapObjects) { |
| 581 | LDAPConfigData confData{}; |
Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 582 | if (ec2) |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 583 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 584 | callback(false, confData, ldapType); |
Carson Labrado | bf2dded | 2023-08-10 00:37:06 +0000 | [diff] [blame] | 585 | BMCWEB_LOG_WARNING("D-Bus responses error: {}", ec2); |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 586 | return; |
| 587 | } |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 588 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 589 | std::string ldapDbusType; |
| 590 | std::string searchString; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 591 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 592 | if (ldapType == "LDAP") |
| 593 | { |
| 594 | ldapDbusType = |
| 595 | "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap"; |
| 596 | searchString = "openldap"; |
| 597 | } |
| 598 | else if (ldapType == "ActiveDirectory") |
| 599 | { |
| 600 | ldapDbusType = |
| 601 | "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory"; |
| 602 | searchString = "active_directory"; |
| 603 | } |
| 604 | else |
| 605 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 606 | BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}", |
| 607 | ldapType); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 608 | callback(false, confData, ldapType); |
| 609 | return; |
| 610 | } |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 611 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 612 | std::string ldapEnableInterfaceStr = ldapEnableInterface; |
| 613 | std::string ldapConfigInterfaceStr = ldapConfigInterface; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 614 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 615 | for (const auto& object : ldapObjects) |
| 616 | { |
| 617 | // let's find the object whose ldap type is equal to the |
| 618 | // given type |
| 619 | if (object.first.str.find(searchString) == std::string::npos) |
| 620 | { |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | for (const auto& interface : object.second) |
| 625 | { |
| 626 | if (interface.first == ldapEnableInterfaceStr) |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 627 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 628 | // rest of the properties are string. |
| 629 | for (const auto& property : interface.second) |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 630 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 631 | if (property.first == "Enabled") |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 632 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 633 | const bool* value = |
| 634 | std::get_if<bool>(&property.second); |
| 635 | if (value == nullptr) |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 636 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 637 | continue; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 638 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 639 | confData.serviceEnabled = *value; |
| 640 | break; |
Nagaraju Goruganti | 54fc587 | 2019-01-30 05:11:00 -0600 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 644 | else if (interface.first == ldapConfigInterfaceStr) |
| 645 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 646 | for (const auto& property : interface.second) |
| 647 | { |
| 648 | const std::string* strValue = |
| 649 | std::get_if<std::string>(&property.second); |
| 650 | if (strValue == nullptr) |
| 651 | { |
| 652 | continue; |
| 653 | } |
| 654 | if (property.first == "LDAPServerURI") |
| 655 | { |
| 656 | confData.uri = *strValue; |
| 657 | } |
| 658 | else if (property.first == "LDAPBindDN") |
| 659 | { |
| 660 | confData.bindDN = *strValue; |
| 661 | } |
| 662 | else if (property.first == "LDAPBaseDN") |
| 663 | { |
| 664 | confData.baseDN = *strValue; |
| 665 | } |
| 666 | else if (property.first == "LDAPSearchScope") |
| 667 | { |
| 668 | confData.searchScope = *strValue; |
| 669 | } |
| 670 | else if (property.first == "GroupNameAttribute") |
| 671 | { |
| 672 | confData.groupAttribute = *strValue; |
| 673 | } |
| 674 | else if (property.first == "UserNameAttribute") |
| 675 | { |
| 676 | confData.userNameAttribute = *strValue; |
| 677 | } |
| 678 | else if (property.first == "LDAPType") |
| 679 | { |
| 680 | confData.serverType = *strValue; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | else if (interface.first == |
| 685 | "xyz.openbmc_project.User.PrivilegeMapperEntry") |
| 686 | { |
| 687 | LDAPRoleMapData roleMapData{}; |
| 688 | for (const auto& property : interface.second) |
| 689 | { |
| 690 | const std::string* strValue = |
| 691 | std::get_if<std::string>(&property.second); |
| 692 | |
| 693 | if (strValue == nullptr) |
| 694 | { |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | if (property.first == "GroupName") |
| 699 | { |
| 700 | roleMapData.groupName = *strValue; |
| 701 | } |
| 702 | else if (property.first == "Privilege") |
| 703 | { |
| 704 | roleMapData.privilege = *strValue; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | confData.groupRoleList.emplace_back(object.first.str, |
| 709 | roleMapData); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | callback(true, confData, ldapType); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 714 | }); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 715 | }); |
Ratan Gupta | 6973a58 | 2018-12-13 18:25:44 +0530 | [diff] [blame] | 716 | } |
| 717 | |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 718 | /** |
| 719 | * @brief parses the authentication section under the LDAP |
| 720 | * @param input JSON data |
| 721 | * @param asyncResp pointer to the JSON response |
| 722 | * @param userName userName to be filled from the given JSON. |
| 723 | * @param password password to be filled from the given JSON. |
| 724 | */ |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 725 | inline void parseLDAPAuthenticationJson( |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 726 | nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 727 | std::optional<std::string>& username, std::optional<std::string>& password) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 728 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 729 | std::optional<std::string> authType; |
| 730 | |
| 731 | if (!json_util::readJson(input, asyncResp->res, "AuthenticationType", |
| 732 | authType, "Username", username, "Password", |
| 733 | password)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 734 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 735 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 736 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 737 | if (!authType) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 738 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 739 | return; |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 740 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 741 | if (*authType != "UsernameAndPassword") |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 742 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 743 | messages::propertyValueNotInList(asyncResp->res, *authType, |
| 744 | "AuthenticationType"); |
| 745 | return; |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 746 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 747 | } |
| 748 | /** |
| 749 | * @brief parses the LDAPService section under the LDAP |
| 750 | * @param input JSON data |
| 751 | * @param asyncResp pointer to the JSON response |
| 752 | * @param baseDNList baseDN to be filled from the given JSON. |
| 753 | * @param userNameAttribute userName to be filled from the given JSON. |
| 754 | * @param groupaAttribute password to be filled from the given JSON. |
| 755 | */ |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 756 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 757 | inline void |
| 758 | parseLDAPServiceJson(nlohmann::json input, |
| 759 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 760 | std::optional<std::vector<std::string>>& baseDNList, |
| 761 | std::optional<std::string>& userNameAttribute, |
| 762 | std::optional<std::string>& groupsAttribute) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 763 | { |
| 764 | std::optional<nlohmann::json> searchSettings; |
| 765 | |
| 766 | if (!json_util::readJson(input, asyncResp->res, "SearchSettings", |
| 767 | searchSettings)) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 768 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 769 | return; |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 770 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 771 | if (!searchSettings) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 772 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 773 | return; |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 774 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 775 | if (!json_util::readJson(*searchSettings, asyncResp->res, |
| 776 | "BaseDistinguishedNames", baseDNList, |
| 777 | "UsernameAttribute", userNameAttribute, |
| 778 | "GroupsAttribute", groupsAttribute)) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 779 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 780 | return; |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 781 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 782 | } |
| 783 | /** |
| 784 | * @brief updates the LDAP server address and updates the |
| 785 | json response with the new value. |
| 786 | * @param serviceAddressList address to be updated. |
| 787 | * @param asyncResp pointer to the JSON response |
| 788 | * @param ldapServerElementName Type of LDAP |
| 789 | server(openLDAP/ActiveDirectory) |
| 790 | */ |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 791 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 792 | inline void handleServiceAddressPatch( |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 793 | const std::vector<std::string>& serviceAddressList, |
| 794 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 795 | const std::string& ldapServerElementName, |
| 796 | const std::string& ldapConfigObject) |
| 797 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 798 | sdbusplus::asio::setProperty( |
| 799 | *crow::connections::systemBus, ldapDbusService, ldapConfigObject, |
| 800 | ldapConfigInterface, "LDAPServerURI", serviceAddressList.front(), |
Patrick Williams | 7a54389 | 2023-06-24 03:07:55 -0500 | [diff] [blame] | 801 | [asyncResp, ldapServerElementName, serviceAddressList]( |
| 802 | const boost::system::error_code& ec, sdbusplus::message_t& msg) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 803 | if (ec) |
| 804 | { |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 805 | const sd_bus_error* dbusError = msg.get_error(); |
| 806 | if ((dbusError != nullptr) && |
| 807 | (dbusError->name == |
| 808 | std::string_view( |
| 809 | "xyz.openbmc_project.Common.Error.InvalidArgument"))) |
| 810 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 811 | BMCWEB_LOG_WARNING( |
| 812 | "Error Occurred in updating the service address"); |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 813 | messages::propertyValueIncorrect(asyncResp->res, |
| 814 | "ServiceAddresses", |
| 815 | serviceAddressList.front()); |
| 816 | return; |
| 817 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 818 | messages::internalError(asyncResp->res); |
| 819 | return; |
| 820 | } |
| 821 | std::vector<std::string> modifiedserviceAddressList = { |
| 822 | serviceAddressList.front()}; |
| 823 | asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] = |
| 824 | modifiedserviceAddressList; |
| 825 | if ((serviceAddressList).size() > 1) |
| 826 | { |
| 827 | messages::propertyValueModified(asyncResp->res, "ServiceAddresses", |
| 828 | serviceAddressList.front()); |
| 829 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 830 | BMCWEB_LOG_DEBUG("Updated the service address"); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 831 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 832 | } |
| 833 | /** |
| 834 | * @brief updates the LDAP Bind DN and updates the |
| 835 | json response with the new value. |
| 836 | * @param username name of the user which needs to be updated. |
| 837 | * @param asyncResp pointer to the JSON response |
| 838 | * @param ldapServerElementName Type of LDAP |
| 839 | server(openLDAP/ActiveDirectory) |
| 840 | */ |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 841 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 842 | inline void |
| 843 | handleUserNamePatch(const std::string& username, |
| 844 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 845 | const std::string& ldapServerElementName, |
| 846 | const std::string& ldapConfigObject) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 847 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 848 | sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService, |
| 849 | ldapConfigObject, ldapConfigInterface, |
| 850 | "LDAPBindDN", username, |
| 851 | [asyncResp, username, ldapServerElementName]( |
| 852 | const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 853 | if (ec) |
| 854 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 855 | BMCWEB_LOG_DEBUG("Error occurred in updating the username"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 856 | messages::internalError(asyncResp->res); |
| 857 | return; |
| 858 | } |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 859 | asyncResp->res.jsonValue[ldapServerElementName]["Authentication"] |
| 860 | ["Username"] = username; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 861 | BMCWEB_LOG_DEBUG("Updated the username"); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 862 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /** |
| 866 | * @brief updates the LDAP password |
| 867 | * @param password : ldap password which needs to be updated. |
| 868 | * @param asyncResp pointer to the JSON response |
| 869 | * @param ldapServerElementName Type of LDAP |
| 870 | * server(openLDAP/ActiveDirectory) |
| 871 | */ |
| 872 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 873 | inline void |
| 874 | handlePasswordPatch(const std::string& password, |
| 875 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 876 | const std::string& ldapServerElementName, |
| 877 | const std::string& ldapConfigObject) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 878 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 879 | sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService, |
| 880 | ldapConfigObject, ldapConfigInterface, |
| 881 | "LDAPBindDNPassword", password, |
| 882 | [asyncResp, password, ldapServerElementName]( |
| 883 | const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 884 | if (ec) |
| 885 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 886 | BMCWEB_LOG_DEBUG("Error occurred in updating the password"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 887 | messages::internalError(asyncResp->res); |
| 888 | return; |
| 889 | } |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 890 | asyncResp->res.jsonValue[ldapServerElementName]["Authentication"] |
| 891 | ["Password"] = ""; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 892 | BMCWEB_LOG_DEBUG("Updated the password"); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 893 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /** |
| 897 | * @brief updates the LDAP BaseDN and updates the |
| 898 | json response with the new value. |
| 899 | * @param baseDNList baseDN list which needs to be updated. |
| 900 | * @param asyncResp pointer to the JSON response |
| 901 | * @param ldapServerElementName Type of LDAP |
| 902 | server(openLDAP/ActiveDirectory) |
| 903 | */ |
| 904 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 905 | inline void |
| 906 | handleBaseDNPatch(const std::vector<std::string>& baseDNList, |
| 907 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 908 | const std::string& ldapServerElementName, |
| 909 | const std::string& ldapConfigObject) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 910 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 911 | sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService, |
| 912 | ldapConfigObject, ldapConfigInterface, |
| 913 | "LDAPBaseDN", baseDNList.front(), |
| 914 | [asyncResp, baseDNList, ldapServerElementName]( |
| 915 | const boost::system::error_code& ec, |
| 916 | const sdbusplus::message_t& msg) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 917 | if (ec) |
| 918 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 919 | BMCWEB_LOG_DEBUG("Error Occurred in Updating the base DN"); |
Ravi Teja | 25e055a | 2022-08-09 03:18:38 -0500 | [diff] [blame] | 920 | const sd_bus_error* dbusError = msg.get_error(); |
| 921 | if ((dbusError != nullptr) && |
| 922 | (dbusError->name == |
| 923 | std::string_view( |
| 924 | "xyz.openbmc_project.Common.Error.InvalidArgument"))) |
| 925 | { |
| 926 | messages::propertyValueIncorrect(asyncResp->res, |
| 927 | "BaseDistinguishedNames", |
| 928 | baseDNList.front()); |
| 929 | return; |
| 930 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 931 | messages::internalError(asyncResp->res); |
| 932 | return; |
| 933 | } |
| 934 | auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName]; |
| 935 | auto& searchSettingsJson = |
| 936 | serverTypeJson["LDAPService"]["SearchSettings"]; |
| 937 | std::vector<std::string> modifiedBaseDNList = {baseDNList.front()}; |
| 938 | searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList; |
| 939 | if (baseDNList.size() > 1) |
| 940 | { |
| 941 | messages::propertyValueModified( |
| 942 | asyncResp->res, "BaseDistinguishedNames", baseDNList.front()); |
| 943 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 944 | BMCWEB_LOG_DEBUG("Updated the base DN"); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 945 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 946 | } |
| 947 | /** |
| 948 | * @brief updates the LDAP user name attribute and updates the |
| 949 | json response with the new value. |
| 950 | * @param userNameAttribute attribute to be updated. |
| 951 | * @param asyncResp pointer to the JSON response |
| 952 | * @param ldapServerElementName Type of LDAP |
| 953 | server(openLDAP/ActiveDirectory) |
| 954 | */ |
| 955 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 956 | inline void |
| 957 | handleUserNameAttrPatch(const std::string& userNameAttribute, |
| 958 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 959 | const std::string& ldapServerElementName, |
| 960 | const std::string& ldapConfigObject) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 961 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 962 | sdbusplus::asio::setProperty( |
| 963 | *crow::connections::systemBus, ldapDbusService, ldapConfigObject, |
| 964 | ldapConfigInterface, "UserNameAttribute", userNameAttribute, |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 965 | [asyncResp, userNameAttribute, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 966 | ldapServerElementName](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 967 | if (ec) |
| 968 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 969 | BMCWEB_LOG_DEBUG("Error Occurred in Updating the " |
| 970 | "username attribute"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 971 | messages::internalError(asyncResp->res); |
| 972 | return; |
| 973 | } |
| 974 | auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName]; |
| 975 | auto& searchSettingsJson = |
| 976 | serverTypeJson["LDAPService"]["SearchSettings"]; |
| 977 | searchSettingsJson["UsernameAttribute"] = userNameAttribute; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 978 | BMCWEB_LOG_DEBUG("Updated the user name attr."); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 979 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 980 | } |
| 981 | /** |
| 982 | * @brief updates the LDAP group attribute and updates the |
| 983 | json response with the new value. |
| 984 | * @param groupsAttribute attribute to be updated. |
| 985 | * @param asyncResp pointer to the JSON response |
| 986 | * @param ldapServerElementName Type of LDAP |
| 987 | server(openLDAP/ActiveDirectory) |
| 988 | */ |
| 989 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 990 | inline void handleGroupNameAttrPatch( |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 991 | const std::string& groupsAttribute, |
| 992 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 993 | const std::string& ldapServerElementName, |
| 994 | const std::string& ldapConfigObject) |
| 995 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 996 | sdbusplus::asio::setProperty( |
| 997 | *crow::connections::systemBus, ldapDbusService, ldapConfigObject, |
| 998 | ldapConfigInterface, "GroupNameAttribute", groupsAttribute, |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 999 | [asyncResp, groupsAttribute, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1000 | ldapServerElementName](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1001 | if (ec) |
| 1002 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1003 | BMCWEB_LOG_DEBUG("Error Occurred in Updating the " |
| 1004 | "groupname attribute"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1005 | messages::internalError(asyncResp->res); |
| 1006 | return; |
| 1007 | } |
| 1008 | auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName]; |
| 1009 | auto& searchSettingsJson = |
| 1010 | serverTypeJson["LDAPService"]["SearchSettings"]; |
| 1011 | searchSettingsJson["GroupsAttribute"] = groupsAttribute; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1012 | BMCWEB_LOG_DEBUG("Updated the groupname attr"); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1013 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1014 | } |
| 1015 | /** |
| 1016 | * @brief updates the LDAP service enable and updates the |
| 1017 | json response with the new value. |
| 1018 | * @param input JSON data. |
| 1019 | * @param asyncResp pointer to the JSON response |
| 1020 | * @param ldapServerElementName Type of LDAP |
| 1021 | server(openLDAP/ActiveDirectory) |
| 1022 | */ |
| 1023 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1024 | inline void handleServiceEnablePatch( |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1025 | bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1026 | const std::string& ldapServerElementName, |
| 1027 | const std::string& ldapConfigObject) |
| 1028 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1029 | sdbusplus::asio::setProperty( |
| 1030 | *crow::connections::systemBus, ldapDbusService, ldapConfigObject, |
| 1031 | ldapEnableInterface, "Enabled", serviceEnabled, |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1032 | [asyncResp, serviceEnabled, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1033 | ldapServerElementName](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1034 | if (ec) |
| 1035 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1036 | BMCWEB_LOG_DEBUG("Error Occurred in Updating the service enable"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1037 | messages::internalError(asyncResp->res); |
| 1038 | return; |
| 1039 | } |
| 1040 | asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] = |
| 1041 | serviceEnabled; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1042 | BMCWEB_LOG_DEBUG("Updated Service enable = {}", serviceEnabled); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1043 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1046 | inline void |
| 1047 | handleAuthMethodsPatch(nlohmann::json& input, |
| 1048 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1049 | { |
| 1050 | std::optional<bool> basicAuth; |
| 1051 | std::optional<bool> cookie; |
| 1052 | std::optional<bool> sessionToken; |
| 1053 | std::optional<bool> xToken; |
| 1054 | std::optional<bool> tls; |
| 1055 | |
| 1056 | if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth, |
| 1057 | "Cookie", cookie, "SessionToken", sessionToken, |
| 1058 | "XToken", xToken, "TLS", tls)) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1059 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1060 | BMCWEB_LOG_ERROR("Cannot read values from AuthMethod tag"); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | // Make a copy of methods configuration |
| 1065 | persistent_data::AuthConfigMethods authMethodsConfig = |
| 1066 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
| 1067 | |
| 1068 | if (basicAuth) |
| 1069 | { |
| 1070 | #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION |
| 1071 | messages::actionNotSupported( |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1072 | asyncResp->res, |
| 1073 | "Setting BasicAuth when basic-auth feature is disabled"); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1074 | return; |
| 1075 | #endif |
| 1076 | authMethodsConfig.basic = *basicAuth; |
| 1077 | } |
| 1078 | |
| 1079 | if (cookie) |
| 1080 | { |
| 1081 | #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1082 | messages::actionNotSupported( |
| 1083 | asyncResp->res, |
| 1084 | "Setting Cookie when cookie-auth feature is disabled"); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1085 | return; |
| 1086 | #endif |
| 1087 | authMethodsConfig.cookie = *cookie; |
| 1088 | } |
| 1089 | |
| 1090 | if (sessionToken) |
| 1091 | { |
| 1092 | #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION |
| 1093 | messages::actionNotSupported( |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1094 | asyncResp->res, |
| 1095 | "Setting SessionToken when session-auth feature is disabled"); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1096 | return; |
| 1097 | #endif |
| 1098 | authMethodsConfig.sessionToken = *sessionToken; |
| 1099 | } |
| 1100 | |
| 1101 | if (xToken) |
| 1102 | { |
| 1103 | #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1104 | messages::actionNotSupported( |
| 1105 | asyncResp->res, |
| 1106 | "Setting XToken when xtoken-auth feature is disabled"); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1107 | return; |
| 1108 | #endif |
| 1109 | authMethodsConfig.xtoken = *xToken; |
| 1110 | } |
| 1111 | |
| 1112 | if (tls) |
| 1113 | { |
| 1114 | #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1115 | messages::actionNotSupported( |
| 1116 | asyncResp->res, |
| 1117 | "Setting TLS when mutual-tls-auth feature is disabled"); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1118 | return; |
| 1119 | #endif |
| 1120 | authMethodsConfig.tls = *tls; |
| 1121 | } |
| 1122 | |
| 1123 | if (!authMethodsConfig.basic && !authMethodsConfig.cookie && |
| 1124 | !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken && |
| 1125 | !authMethodsConfig.tls) |
| 1126 | { |
| 1127 | // Do not allow user to disable everything |
| 1128 | messages::actionNotSupported(asyncResp->res, |
| 1129 | "of disabling all available methods"); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | persistent_data::SessionStore::getInstance().updateAuthMethodsConfig( |
| 1134 | authMethodsConfig); |
| 1135 | // Save configuration immediately |
| 1136 | persistent_data::getConfig().writeData(); |
| 1137 | |
| 1138 | messages::success(asyncResp->res); |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * @brief Get the required values from the given JSON, validates the |
| 1143 | * value and create the LDAP config object. |
| 1144 | * @param input JSON data |
| 1145 | * @param asyncResp pointer to the JSON response |
| 1146 | * @param serverType Type of LDAP server(openLDAP/ActiveDirectory) |
| 1147 | */ |
| 1148 | |
| 1149 | inline void handleLDAPPatch(nlohmann::json& input, |
| 1150 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1151 | const std::string& serverType) |
| 1152 | { |
| 1153 | std::string dbusObjectPath; |
| 1154 | if (serverType == "ActiveDirectory") |
| 1155 | { |
| 1156 | dbusObjectPath = adConfigObject; |
| 1157 | } |
| 1158 | else if (serverType == "LDAP") |
| 1159 | { |
| 1160 | dbusObjectPath = ldapConfigObjectName; |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | std::optional<nlohmann::json> authentication; |
| 1168 | std::optional<nlohmann::json> ldapService; |
| 1169 | std::optional<std::vector<std::string>> serviceAddressList; |
| 1170 | std::optional<bool> serviceEnabled; |
| 1171 | std::optional<std::vector<std::string>> baseDNList; |
| 1172 | std::optional<std::string> userNameAttribute; |
| 1173 | std::optional<std::string> groupsAttribute; |
| 1174 | std::optional<std::string> userName; |
| 1175 | std::optional<std::string> password; |
| 1176 | std::optional<std::vector<nlohmann::json>> remoteRoleMapData; |
| 1177 | |
| 1178 | if (!json_util::readJson(input, asyncResp->res, "Authentication", |
| 1179 | authentication, "LDAPService", ldapService, |
| 1180 | "ServiceAddresses", serviceAddressList, |
| 1181 | "ServiceEnabled", serviceEnabled, |
| 1182 | "RemoteRoleMapping", remoteRoleMapData)) |
| 1183 | { |
| 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | if (authentication) |
| 1188 | { |
| 1189 | parseLDAPAuthenticationJson(*authentication, asyncResp, userName, |
| 1190 | password); |
| 1191 | } |
| 1192 | if (ldapService) |
| 1193 | { |
| 1194 | parseLDAPServiceJson(*ldapService, asyncResp, baseDNList, |
| 1195 | userNameAttribute, groupsAttribute); |
| 1196 | } |
| 1197 | if (serviceAddressList) |
| 1198 | { |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 1199 | if (serviceAddressList->empty()) |
Ratan Gupta | eb2bbe5 | 2019-04-22 14:27:01 +0530 | [diff] [blame] | 1200 | { |
Ed Tanous | e2616cc | 2022-06-27 12:45:55 -0700 | [diff] [blame] | 1201 | messages::propertyValueNotInList( |
| 1202 | asyncResp->res, *serviceAddressList, "ServiceAddress"); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1205 | } |
| 1206 | if (baseDNList) |
| 1207 | { |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 1208 | if (baseDNList->empty()) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1209 | { |
Ed Tanous | e2616cc | 2022-06-27 12:45:55 -0700 | [diff] [blame] | 1210 | messages::propertyValueNotInList(asyncResp->res, *baseDNList, |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1211 | "BaseDistinguishedNames"); |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1212 | return; |
| 1213 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1214 | } |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1215 | |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1216 | // nothing to update, then return |
| 1217 | if (!userName && !password && !serviceAddressList && !baseDNList && |
| 1218 | !userNameAttribute && !groupsAttribute && !serviceEnabled && |
| 1219 | !remoteRoleMapData) |
| 1220 | { |
| 1221 | return; |
| 1222 | } |
| 1223 | |
| 1224 | // Get the existing resource first then keep modifying |
| 1225 | // whenever any property gets updated. |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1226 | getLDAPConfigData( |
| 1227 | serverType, |
| 1228 | [asyncResp, userName, password, baseDNList, userNameAttribute, |
| 1229 | groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath, |
| 1230 | remoteRoleMapData](bool success, const LDAPConfigData& confData, |
| 1231 | const std::string& serverT) { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1232 | if (!success) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1233 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1234 | messages::internalError(asyncResp->res); |
| 1235 | return; |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1236 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1237 | parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT); |
| 1238 | if (confData.serviceEnabled) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1239 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1240 | // Disable the service first and update the rest of |
| 1241 | // the properties. |
| 1242 | handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath); |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1243 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1244 | |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1245 | if (serviceAddressList) |
| 1246 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1247 | handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT, |
| 1248 | dbusObjectPath); |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1249 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1250 | if (userName) |
| 1251 | { |
| 1252 | handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath); |
| 1253 | } |
| 1254 | if (password) |
| 1255 | { |
| 1256 | handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath); |
| 1257 | } |
| 1258 | |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1259 | if (baseDNList) |
| 1260 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1261 | handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath); |
| 1262 | } |
| 1263 | if (userNameAttribute) |
| 1264 | { |
| 1265 | handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT, |
| 1266 | dbusObjectPath); |
| 1267 | } |
| 1268 | if (groupsAttribute) |
| 1269 | { |
| 1270 | handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT, |
| 1271 | dbusObjectPath); |
| 1272 | } |
| 1273 | if (serviceEnabled) |
| 1274 | { |
| 1275 | // if user has given the value as true then enable |
| 1276 | // the service. if user has given false then no-op |
| 1277 | // as service is already stopped. |
| 1278 | if (*serviceEnabled) |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1279 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1280 | handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT, |
| 1281 | dbusObjectPath); |
Ratan Gupta | 8a07d28 | 2019-03-16 08:33:47 +0530 | [diff] [blame] | 1282 | } |
| 1283 | } |
jayaprakash Mutyala | 9620060 | 2020-04-08 11:09:10 +0000 | [diff] [blame] | 1284 | else |
| 1285 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1286 | // if user has not given the service enabled value |
| 1287 | // then revert it to the same state as it was |
| 1288 | // before. |
| 1289 | handleServiceEnablePatch(confData.serviceEnabled, asyncResp, |
| 1290 | serverT, dbusObjectPath); |
jayaprakash Mutyala | 9620060 | 2020-04-08 11:09:10 +0000 | [diff] [blame] | 1291 | } |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 1292 | |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1293 | if (remoteRoleMapData) |
| 1294 | { |
| 1295 | handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT, |
| 1296 | *remoteRoleMapData); |
| 1297 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1298 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 1301 | inline void updateUserProperties( |
| 1302 | std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username, |
| 1303 | const std::optional<std::string>& password, |
| 1304 | const std::optional<bool>& enabled, |
| 1305 | const std::optional<std::string>& roleId, const std::optional<bool>& locked, |
| 1306 | std::optional<std::vector<std::string>> accountTypes, bool userSelf) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1307 | { |
P Dheeraj Srujan Kumar | b477fd4 | 2021-12-16 07:17:51 +0530 | [diff] [blame] | 1308 | sdbusplus::message::object_path tempObjPath(rootUserDbusPath); |
| 1309 | tempObjPath /= username; |
| 1310 | std::string dbusObjectPath(tempObjPath); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1311 | |
| 1312 | dbus::utility::checkDbusPathExists( |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1313 | dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled, |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 1314 | locked, accountTypes(std::move(accountTypes)), |
| 1315 | userSelf, asyncResp{std::move(asyncResp)}](int rc) { |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1316 | if (rc <= 0) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1317 | { |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 1318 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", |
| 1319 | username); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1320 | return; |
| 1321 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1322 | |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1323 | if (password) |
| 1324 | { |
| 1325 | int retval = pamUpdatePassword(username, *password); |
| 1326 | |
| 1327 | if (retval == PAM_USER_UNKNOWN) |
| 1328 | { |
| 1329 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", |
| 1330 | username); |
| 1331 | } |
| 1332 | else if (retval == PAM_AUTHTOK_ERR) |
| 1333 | { |
| 1334 | // If password is invalid |
| 1335 | messages::propertyValueFormatError(asyncResp->res, |
| 1336 | *password, "Password"); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1337 | BMCWEB_LOG_ERROR("pamUpdatePassword Failed"); |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1338 | } |
| 1339 | else if (retval != PAM_SUCCESS) |
| 1340 | { |
| 1341 | messages::internalError(asyncResp->res); |
| 1342 | return; |
| 1343 | } |
| 1344 | else |
| 1345 | { |
| 1346 | messages::success(asyncResp->res); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | if (enabled) |
| 1351 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1352 | sdbusplus::asio::setProperty( |
| 1353 | *crow::connections::systemBus, |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1354 | "xyz.openbmc_project.User.Manager", dbusObjectPath, |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1355 | "xyz.openbmc_project.User.Attributes", "UserEnabled", |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1356 | *enabled, [asyncResp](const boost::system::error_code& ec) { |
| 1357 | if (ec) |
| 1358 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1359 | BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1360 | messages::internalError(asyncResp->res); |
| 1361 | return; |
| 1362 | } |
| 1363 | messages::success(asyncResp->res); |
| 1364 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1365 | } |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 1366 | |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1367 | if (roleId) |
| 1368 | { |
| 1369 | std::string priv = getPrivilegeFromRoleId(*roleId); |
| 1370 | if (priv.empty()) |
| 1371 | { |
Ed Tanous | e2616cc | 2022-06-27 12:45:55 -0700 | [diff] [blame] | 1372 | messages::propertyValueNotInList(asyncResp->res, true, |
| 1373 | "Locked"); |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1374 | return; |
| 1375 | } |
| 1376 | |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1377 | sdbusplus::asio::setProperty( |
| 1378 | *crow::connections::systemBus, |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1379 | "xyz.openbmc_project.User.Manager", dbusObjectPath, |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1380 | "xyz.openbmc_project.User.Attributes", "UserPrivilege", |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1381 | priv, [asyncResp](const boost::system::error_code& ec) { |
| 1382 | if (ec) |
| 1383 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1384 | BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1385 | messages::internalError(asyncResp->res); |
| 1386 | return; |
| 1387 | } |
| 1388 | messages::success(asyncResp->res); |
| 1389 | }); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1392 | if (locked) |
| 1393 | { |
| 1394 | // admin can unlock the account which is locked by |
| 1395 | // successive authentication failures but admin should |
| 1396 | // not be allowed to lock an account. |
| 1397 | if (*locked) |
| 1398 | { |
| 1399 | messages::propertyValueNotInList(asyncResp->res, "true", |
| 1400 | "Locked"); |
| 1401 | return; |
| 1402 | } |
| 1403 | |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1404 | sdbusplus::asio::setProperty( |
| 1405 | *crow::connections::systemBus, |
| 1406 | "xyz.openbmc_project.User.Manager", dbusObjectPath, |
| 1407 | "xyz.openbmc_project.User.Attributes", |
| 1408 | "UserLockedForFailedAttempt", *locked, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1409 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1410 | if (ec) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1411 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1412 | BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1413 | messages::internalError(asyncResp->res); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 1414 | return; |
| 1415 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1416 | messages::success(asyncResp->res); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1417 | }); |
Ed Tanous | 618c14b | 2022-06-30 17:44:25 -0700 | [diff] [blame] | 1418 | } |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 1419 | |
| 1420 | if (accountTypes) |
| 1421 | { |
| 1422 | patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath, |
| 1423 | userSelf); |
| 1424 | } |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 1425 | }); |
| 1426 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 1427 | |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1428 | inline void handleAccountServiceHead( |
| 1429 | App& app, const crow::Request& req, |
| 1430 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1431 | { |
| 1432 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1433 | { |
| 1434 | return; |
| 1435 | } |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1436 | asyncResp->res.addHeader( |
| 1437 | boost::beast::http::field::link, |
| 1438 | "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby"); |
| 1439 | } |
| 1440 | |
| 1441 | inline void |
| 1442 | handleAccountServiceGet(App& app, const crow::Request& req, |
| 1443 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1444 | { |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1445 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1446 | { |
| 1447 | return; |
| 1448 | } |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1449 | |
| 1450 | if (req.session == nullptr) |
| 1451 | { |
| 1452 | messages::internalError(asyncResp->res); |
| 1453 | return; |
| 1454 | } |
| 1455 | |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1456 | asyncResp->res.addHeader( |
| 1457 | boost::beast::http::field::link, |
| 1458 | "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby"); |
| 1459 | |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1460 | const persistent_data::AuthConfigMethods& authMethodsConfig = |
| 1461 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
| 1462 | |
| 1463 | nlohmann::json& json = asyncResp->res.jsonValue; |
| 1464 | json["@odata.id"] = "/redfish/v1/AccountService"; |
| 1465 | json["@odata.type"] = "#AccountService." |
| 1466 | "v1_10_0.AccountService"; |
| 1467 | json["Id"] = "AccountService"; |
| 1468 | json["Name"] = "Account Service"; |
| 1469 | json["Description"] = "Account Service"; |
| 1470 | json["ServiceEnabled"] = true; |
| 1471 | json["MaxPasswordLength"] = 20; |
| 1472 | json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts"; |
| 1473 | json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles"; |
| 1474 | json["Oem"]["OpenBMC"]["@odata.type"] = |
Ed Tanous | 5b5574a | 2022-09-26 19:53:36 -0700 | [diff] [blame] | 1475 | "#OpenBMCAccountService.v1_0_0.AccountService"; |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1476 | json["Oem"]["OpenBMC"]["@odata.id"] = |
| 1477 | "/redfish/v1/AccountService#/Oem/OpenBMC"; |
| 1478 | json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] = |
| 1479 | authMethodsConfig.basic; |
| 1480 | json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] = |
| 1481 | authMethodsConfig.sessionToken; |
| 1482 | json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken; |
| 1483 | json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie; |
| 1484 | json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls; |
| 1485 | |
| 1486 | // /redfish/v1/AccountService/LDAP/Certificates is something only |
| 1487 | // ConfigureManager can access then only display when the user has |
| 1488 | // permissions ConfigureManager |
| 1489 | Privileges effectiveUserPrivileges = |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1490 | redfish::getUserPrivileges(*req.session); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1491 | |
| 1492 | if (isOperationAllowedWithPrivileges({{"ConfigureManager"}}, |
| 1493 | effectiveUserPrivileges)) |
| 1494 | { |
| 1495 | asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] = |
| 1496 | "/redfish/v1/AccountService/LDAP/Certificates"; |
| 1497 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 1498 | sdbusplus::asio::getAllProperties( |
| 1499 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 1500 | "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy", |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1501 | [asyncResp](const boost::system::error_code& ec, |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1502 | const dbus::utility::DBusPropertiesMap& propertiesList) { |
| 1503 | if (ec) |
| 1504 | { |
| 1505 | messages::internalError(asyncResp->res); |
| 1506 | return; |
| 1507 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 1508 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1509 | BMCWEB_LOG_DEBUG("Got {}properties for AccountService", |
| 1510 | propertiesList.size()); |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 1511 | |
| 1512 | const uint8_t* minPasswordLength = nullptr; |
| 1513 | const uint32_t* accountUnlockTimeout = nullptr; |
| 1514 | const uint16_t* maxLoginAttemptBeforeLockout = nullptr; |
| 1515 | |
| 1516 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 1517 | dbus_utils::UnpackErrorPrinter(), propertiesList, |
| 1518 | "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout", |
| 1519 | accountUnlockTimeout, "MaxLoginAttemptBeforeLockout", |
| 1520 | maxLoginAttemptBeforeLockout); |
| 1521 | |
| 1522 | if (!success) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1523 | { |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 1524 | messages::internalError(asyncResp->res); |
| 1525 | return; |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1526 | } |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 1527 | |
| 1528 | if (minPasswordLength != nullptr) |
| 1529 | { |
| 1530 | asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength; |
| 1531 | } |
| 1532 | |
| 1533 | if (accountUnlockTimeout != nullptr) |
| 1534 | { |
| 1535 | asyncResp->res.jsonValue["AccountLockoutDuration"] = |
| 1536 | *accountUnlockTimeout; |
| 1537 | } |
| 1538 | |
| 1539 | if (maxLoginAttemptBeforeLockout != nullptr) |
| 1540 | { |
| 1541 | asyncResp->res.jsonValue["AccountLockoutThreshold"] = |
| 1542 | *maxLoginAttemptBeforeLockout; |
| 1543 | } |
| 1544 | }); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1545 | |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 1546 | auto callback = [asyncResp](bool success, const LDAPConfigData& confData, |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1547 | const std::string& ldapType) { |
| 1548 | if (!success) |
| 1549 | { |
| 1550 | return; |
| 1551 | } |
| 1552 | parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType); |
| 1553 | }; |
| 1554 | |
| 1555 | getLDAPConfigData("LDAP", callback); |
| 1556 | getLDAPConfigData("ActiveDirectory", callback); |
| 1557 | } |
| 1558 | |
| 1559 | inline void handleAccountServicePatch( |
| 1560 | App& app, const crow::Request& req, |
| 1561 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1562 | { |
| 1563 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1564 | { |
| 1565 | return; |
| 1566 | } |
| 1567 | std::optional<uint32_t> unlockTimeout; |
| 1568 | std::optional<uint16_t> lockoutThreshold; |
| 1569 | std::optional<uint8_t> minPasswordLength; |
| 1570 | std::optional<uint16_t> maxPasswordLength; |
| 1571 | std::optional<nlohmann::json> ldapObject; |
| 1572 | std::optional<nlohmann::json> activeDirectoryObject; |
| 1573 | std::optional<nlohmann::json> oemObject; |
| 1574 | |
| 1575 | if (!json_util::readJsonPatch( |
| 1576 | req, asyncResp->res, "AccountLockoutDuration", unlockTimeout, |
| 1577 | "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength", |
| 1578 | maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP", |
| 1579 | ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem", |
| 1580 | oemObject)) |
| 1581 | { |
| 1582 | return; |
| 1583 | } |
| 1584 | |
| 1585 | if (minPasswordLength) |
| 1586 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1587 | sdbusplus::asio::setProperty( |
| 1588 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 1589 | "/xyz/openbmc_project/user", |
| 1590 | "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength", |
| 1591 | *minPasswordLength, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1592 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1593 | if (ec) |
| 1594 | { |
| 1595 | messages::internalError(asyncResp->res); |
| 1596 | return; |
| 1597 | } |
| 1598 | messages::success(asyncResp->res); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1599 | }); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | if (maxPasswordLength) |
| 1603 | { |
| 1604 | messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength"); |
| 1605 | } |
| 1606 | |
| 1607 | if (ldapObject) |
| 1608 | { |
| 1609 | handleLDAPPatch(*ldapObject, asyncResp, "LDAP"); |
| 1610 | } |
| 1611 | |
| 1612 | if (std::optional<nlohmann::json> oemOpenBMCObject; |
| 1613 | oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", |
| 1614 | oemOpenBMCObject)) |
| 1615 | { |
| 1616 | if (std::optional<nlohmann::json> authMethodsObject; |
| 1617 | oemOpenBMCObject && |
| 1618 | json_util::readJson(*oemOpenBMCObject, asyncResp->res, |
| 1619 | "AuthMethods", authMethodsObject)) |
| 1620 | { |
| 1621 | if (authMethodsObject) |
| 1622 | { |
| 1623 | handleAuthMethodsPatch(*authMethodsObject, asyncResp); |
| 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | if (activeDirectoryObject) |
| 1629 | { |
| 1630 | handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory"); |
| 1631 | } |
| 1632 | |
| 1633 | if (unlockTimeout) |
| 1634 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1635 | sdbusplus::asio::setProperty( |
| 1636 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 1637 | "/xyz/openbmc_project/user", |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1638 | "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout", |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1639 | *unlockTimeout, [asyncResp](const boost::system::error_code& ec) { |
| 1640 | if (ec) |
| 1641 | { |
| 1642 | messages::internalError(asyncResp->res); |
| 1643 | return; |
| 1644 | } |
| 1645 | messages::success(asyncResp->res); |
| 1646 | }); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | if (lockoutThreshold) |
| 1649 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1650 | sdbusplus::asio::setProperty( |
| 1651 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 1652 | "/xyz/openbmc_project/user", |
| 1653 | "xyz.openbmc_project.User.AccountPolicy", |
| 1654 | "MaxLoginAttemptBeforeLockout", *lockoutThreshold, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1655 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1656 | if (ec) |
| 1657 | { |
| 1658 | messages::internalError(asyncResp->res); |
| 1659 | return; |
| 1660 | } |
| 1661 | messages::success(asyncResp->res); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1662 | }); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1666 | inline void handleAccountCollectionHead( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1667 | App& app, const crow::Request& req, |
| 1668 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1669 | { |
| 1670 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1671 | { |
| 1672 | return; |
| 1673 | } |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1674 | asyncResp->res.addHeader( |
| 1675 | boost::beast::http::field::link, |
| 1676 | "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby"); |
| 1677 | } |
| 1678 | |
| 1679 | inline void handleAccountCollectionGet( |
| 1680 | App& app, const crow::Request& req, |
| 1681 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1682 | { |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1683 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1684 | { |
| 1685 | return; |
| 1686 | } |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1687 | |
| 1688 | if (req.session == nullptr) |
| 1689 | { |
| 1690 | messages::internalError(asyncResp->res); |
| 1691 | return; |
| 1692 | } |
| 1693 | |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1694 | asyncResp->res.addHeader( |
| 1695 | boost::beast::http::field::link, |
| 1696 | "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1697 | |
| 1698 | asyncResp->res.jsonValue["@odata.id"] = |
| 1699 | "/redfish/v1/AccountService/Accounts"; |
| 1700 | asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection." |
| 1701 | "ManagerAccountCollection"; |
| 1702 | asyncResp->res.jsonValue["Name"] = "Accounts Collection"; |
| 1703 | asyncResp->res.jsonValue["Description"] = "BMC User Accounts"; |
| 1704 | |
| 1705 | Privileges effectiveUserPrivileges = |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1706 | redfish::getUserPrivileges(*req.session); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1707 | |
| 1708 | std::string thisUser; |
| 1709 | if (req.session) |
| 1710 | { |
| 1711 | thisUser = req.session->username; |
| 1712 | } |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 1713 | sdbusplus::message::object_path path("/xyz/openbmc_project/user"); |
| 1714 | dbus::utility::getManagedObjects( |
| 1715 | "xyz.openbmc_project.User.Manager", path, |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1716 | [asyncResp, thisUser, effectiveUserPrivileges]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1717 | const boost::system::error_code& ec, |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1718 | const dbus::utility::ManagedObjectType& users) { |
| 1719 | if (ec) |
| 1720 | { |
| 1721 | messages::internalError(asyncResp->res); |
| 1722 | return; |
| 1723 | } |
| 1724 | |
| 1725 | bool userCanSeeAllAccounts = |
| 1726 | effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}); |
| 1727 | |
| 1728 | bool userCanSeeSelf = |
| 1729 | effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"}); |
| 1730 | |
| 1731 | nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"]; |
| 1732 | memberArray = nlohmann::json::array(); |
| 1733 | |
| 1734 | for (const auto& userpath : users) |
| 1735 | { |
| 1736 | std::string user = userpath.first.filename(); |
| 1737 | if (user.empty()) |
| 1738 | { |
| 1739 | messages::internalError(asyncResp->res); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1740 | BMCWEB_LOG_ERROR("Invalid firmware ID"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1741 | |
| 1742 | return; |
| 1743 | } |
| 1744 | |
| 1745 | // As clarified by Redfish here: |
| 1746 | // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration |
| 1747 | // Users without ConfigureUsers, only see their own |
| 1748 | // account. Users with ConfigureUsers, see all |
| 1749 | // accounts. |
| 1750 | if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf)) |
| 1751 | { |
| 1752 | nlohmann::json::object_t member; |
Ed Tanous | 3b32780 | 2023-08-14 09:23:43 -0700 | [diff] [blame] | 1753 | member["@odata.id"] = boost::urls::format( |
| 1754 | "/redfish/v1/AccountService/Accounts/{}", user); |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 1755 | memberArray.emplace_back(std::move(member)); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size(); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 1759 | }); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1762 | inline void processAfterCreateUser( |
| 1763 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1764 | const std::string& username, const std::string& password, |
| 1765 | const boost::system::error_code& ec, sdbusplus::message_t& m) |
| 1766 | { |
| 1767 | if (ec) |
| 1768 | { |
| 1769 | userErrorMessageHandler(m.get_error(), asyncResp, username, ""); |
| 1770 | return; |
| 1771 | } |
| 1772 | |
| 1773 | if (pamUpdatePassword(username, password) != PAM_SUCCESS) |
| 1774 | { |
| 1775 | // At this point we have a user that's been |
| 1776 | // created, but the password set |
| 1777 | // failed.Something is wrong, so delete the user |
| 1778 | // that we've already created |
| 1779 | sdbusplus::message::object_path tempObjPath(rootUserDbusPath); |
| 1780 | tempObjPath /= username; |
| 1781 | const std::string userPath(tempObjPath); |
| 1782 | |
| 1783 | crow::connections::systemBus->async_method_call( |
| 1784 | [asyncResp, password](const boost::system::error_code& ec3) { |
| 1785 | if (ec3) |
| 1786 | { |
| 1787 | messages::internalError(asyncResp->res); |
| 1788 | return; |
| 1789 | } |
| 1790 | |
| 1791 | // If password is invalid |
| 1792 | messages::propertyValueFormatError(asyncResp->res, password, |
| 1793 | "Password"); |
| 1794 | }, |
| 1795 | "xyz.openbmc_project.User.Manager", userPath, |
| 1796 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 1797 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1798 | BMCWEB_LOG_ERROR("pamUpdatePassword Failed"); |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1799 | return; |
| 1800 | } |
| 1801 | |
| 1802 | messages::created(asyncResp->res); |
| 1803 | asyncResp->res.addHeader("Location", |
| 1804 | "/redfish/v1/AccountService/Accounts/" + username); |
| 1805 | } |
| 1806 | |
| 1807 | inline void processAfterGetAllGroups( |
| 1808 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1809 | const std::string& username, const std::string& password, |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1810 | const std::string& roleId, bool enabled, |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1811 | std::optional<std::vector<std::string>> accountTypes, |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1812 | const std::vector<std::string>& allGroupsList) |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1813 | { |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1814 | std::vector<std::string> userGroups; |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1815 | std::vector<std::string> accountTypeUserGroups; |
| 1816 | |
| 1817 | // If user specified account types then convert them to unix user groups |
| 1818 | if (accountTypes) |
| 1819 | { |
| 1820 | if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes, |
| 1821 | accountTypeUserGroups)) |
| 1822 | { |
| 1823 | // Problem in mapping Account Types to User Groups, Error already |
| 1824 | // logged. |
| 1825 | return; |
| 1826 | } |
| 1827 | } |
| 1828 | |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1829 | for (const auto& grp : allGroupsList) |
| 1830 | { |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1831 | // If user specified the account type then only accept groups which are |
| 1832 | // in the account types group list. |
| 1833 | if (!accountTypeUserGroups.empty()) |
| 1834 | { |
| 1835 | bool found = false; |
| 1836 | for (const auto& grp1 : accountTypeUserGroups) |
| 1837 | { |
| 1838 | if (grp == grp1) |
| 1839 | { |
| 1840 | found = true; |
| 1841 | break; |
| 1842 | } |
| 1843 | } |
| 1844 | if (!found) |
| 1845 | { |
| 1846 | continue; |
| 1847 | } |
| 1848 | } |
| 1849 | |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1850 | // Console access is provided to the user who is a member of |
| 1851 | // hostconsole group and has a administrator role. So, set |
| 1852 | // hostconsole group only for the administrator. |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1853 | if ((grp == "hostconsole") && (roleId != "priv-admin")) |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1854 | { |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1855 | if (!accountTypeUserGroups.empty()) |
| 1856 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1857 | BMCWEB_LOG_ERROR( |
| 1858 | "Only administrator can get HostConsole access"); |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1859 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 1860 | return; |
| 1861 | } |
| 1862 | continue; |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1863 | } |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1864 | userGroups.emplace_back(grp); |
| 1865 | } |
| 1866 | |
| 1867 | // Make sure user specified groups are valid. This is internal error because |
| 1868 | // it some inconsistencies between user manager and bmcweb. |
| 1869 | if (!accountTypeUserGroups.empty() && |
| 1870 | accountTypeUserGroups.size() != userGroups.size()) |
| 1871 | { |
| 1872 | messages::internalError(asyncResp->res); |
| 1873 | return; |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1874 | } |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1875 | crow::connections::systemBus->async_method_call( |
| 1876 | [asyncResp, username, password](const boost::system::error_code& ec2, |
| 1877 | sdbusplus::message_t& m) { |
| 1878 | processAfterCreateUser(asyncResp, username, password, ec2, m); |
| 1879 | }, |
| 1880 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1881 | "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups, |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1882 | roleId, enabled); |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1883 | } |
| 1884 | |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1885 | inline void handleAccountCollectionPost( |
| 1886 | App& app, const crow::Request& req, |
| 1887 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1888 | { |
| 1889 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1890 | { |
| 1891 | return; |
| 1892 | } |
| 1893 | std::string username; |
| 1894 | std::string password; |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1895 | std::optional<std::string> roleIdJson; |
| 1896 | std::optional<bool> enabledJson; |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1897 | std::optional<std::vector<std::string>> accountTypes; |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1898 | if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username, |
| 1899 | "Password", password, "RoleId", roleIdJson, |
| 1900 | "Enabled", enabledJson, "AccountTypes", |
| 1901 | accountTypes)) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1902 | { |
| 1903 | return; |
| 1904 | } |
| 1905 | |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1906 | std::string roleId = roleIdJson.value_or("User"); |
| 1907 | std::string priv = getPrivilegeFromRoleId(roleId); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1908 | if (priv.empty()) |
| 1909 | { |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1910 | messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1911 | return; |
| 1912 | } |
Asmitha Karunanithi | 239adf8 | 2022-03-25 02:59:03 -0500 | [diff] [blame] | 1913 | roleId = priv; |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1914 | |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 1915 | bool enabled = enabledJson.value_or(true); |
| 1916 | |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1917 | // Reading AllGroups property |
| 1918 | sdbusplus::asio::getProperty<std::vector<std::string>>( |
| 1919 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 1920 | "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager", |
| 1921 | "AllGroups", |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1922 | [asyncResp, username, password{std::move(password)}, roleId, enabled, |
| 1923 | accountTypes](const boost::system::error_code& ec, |
| 1924 | const std::vector<std::string>& allGroupsList) { |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1925 | if (ec) |
| 1926 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1927 | BMCWEB_LOG_DEBUG("ERROR with async_method_call"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1928 | messages::internalError(asyncResp->res); |
| 1929 | return; |
| 1930 | } |
| 1931 | |
| 1932 | if (allGroupsList.empty()) |
| 1933 | { |
| 1934 | messages::internalError(asyncResp->res); |
| 1935 | return; |
| 1936 | } |
| 1937 | |
Ninad Palsule | 97e90da | 2023-05-17 14:04:52 -0500 | [diff] [blame] | 1938 | processAfterGetAllGroups(asyncResp, username, password, roleId, enabled, |
Ninad Palsule | 9ba7393 | 2023-06-01 16:38:57 -0500 | [diff] [blame] | 1939 | accountTypes, allGroupsList); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1940 | }); |
| 1941 | } |
| 1942 | |
| 1943 | inline void |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1944 | handleAccountHead(App& app, const crow::Request& req, |
| 1945 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1946 | const std::string& /*accountName*/) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1947 | { |
| 1948 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1949 | { |
| 1950 | return; |
| 1951 | } |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1952 | asyncResp->res.addHeader( |
| 1953 | boost::beast::http::field::link, |
| 1954 | "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby"); |
| 1955 | } |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1956 | |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 1957 | inline void |
| 1958 | handleAccountGet(App& app, const crow::Request& req, |
| 1959 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1960 | const std::string& accountName) |
| 1961 | { |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1962 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1963 | { |
| 1964 | return; |
| 1965 | } |
| 1966 | asyncResp->res.addHeader( |
| 1967 | boost::beast::http::field::link, |
| 1968 | "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby"); |
| 1969 | |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1970 | #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
| 1971 | // If authentication is disabled, there are no user accounts |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 1972 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1973 | return; |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1974 | #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
Jiaqing Zhao | afd369c | 2023-03-07 15:12:22 +0800 | [diff] [blame] | 1975 | |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1976 | if (req.session == nullptr) |
| 1977 | { |
| 1978 | messages::internalError(asyncResp->res); |
| 1979 | return; |
| 1980 | } |
| 1981 | if (req.session->username != accountName) |
| 1982 | { |
| 1983 | // At this point we've determined that the user is trying to |
| 1984 | // modify a user that isn't them. We need to verify that they |
| 1985 | // have permissions to modify other users, so re-run the auth |
| 1986 | // check with the same permissions, minus ConfigureSelf. |
| 1987 | Privileges effectiveUserPrivileges = |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 1988 | redfish::getUserPrivileges(*req.session); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1989 | Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers", |
| 1990 | "ConfigureManager"}; |
| 1991 | if (!effectiveUserPrivileges.isSupersetOf( |
| 1992 | requiredPermissionsToChangeNonSelf)) |
| 1993 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1994 | BMCWEB_LOG_DEBUG("GET Account denied access"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 1995 | messages::insufficientPrivilege(asyncResp->res); |
| 1996 | return; |
| 1997 | } |
| 1998 | } |
| 1999 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 2000 | sdbusplus::message::object_path path("/xyz/openbmc_project/user"); |
| 2001 | dbus::utility::getManagedObjects( |
| 2002 | "xyz.openbmc_project.User.Manager", path, |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2003 | [asyncResp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 2004 | accountName](const boost::system::error_code& ec, |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2005 | const dbus::utility::ManagedObjectType& users) { |
| 2006 | if (ec) |
| 2007 | { |
| 2008 | messages::internalError(asyncResp->res); |
| 2009 | return; |
| 2010 | } |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 2011 | const auto userIt = std::ranges::find_if( |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame^] | 2012 | users, |
| 2013 | [accountName]( |
| 2014 | const std::pair<sdbusplus::message::object_path, |
| 2015 | dbus::utility::DBusInterfacesMap>& user) { |
| 2016 | return accountName == user.first.filename(); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2017 | }); |
| 2018 | |
| 2019 | if (userIt == users.end()) |
| 2020 | { |
| 2021 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", |
| 2022 | accountName); |
| 2023 | return; |
| 2024 | } |
| 2025 | |
| 2026 | asyncResp->res.jsonValue["@odata.type"] = |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2027 | "#ManagerAccount.v1_7_0.ManagerAccount"; |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2028 | asyncResp->res.jsonValue["Name"] = "User Account"; |
| 2029 | asyncResp->res.jsonValue["Description"] = "User Account"; |
| 2030 | asyncResp->res.jsonValue["Password"] = nullptr; |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2031 | asyncResp->res.jsonValue["StrictAccountTypes"] = true; |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2032 | |
| 2033 | for (const auto& interface : userIt->second) |
| 2034 | { |
| 2035 | if (interface.first == "xyz.openbmc_project.User.Attributes") |
| 2036 | { |
| 2037 | for (const auto& property : interface.second) |
| 2038 | { |
| 2039 | if (property.first == "UserEnabled") |
| 2040 | { |
| 2041 | const bool* userEnabled = |
| 2042 | std::get_if<bool>(&property.second); |
| 2043 | if (userEnabled == nullptr) |
| 2044 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2045 | BMCWEB_LOG_ERROR("UserEnabled wasn't a bool"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2046 | messages::internalError(asyncResp->res); |
| 2047 | return; |
| 2048 | } |
| 2049 | asyncResp->res.jsonValue["Enabled"] = *userEnabled; |
| 2050 | } |
| 2051 | else if (property.first == "UserLockedForFailedAttempt") |
| 2052 | { |
| 2053 | const bool* userLocked = |
| 2054 | std::get_if<bool>(&property.second); |
| 2055 | if (userLocked == nullptr) |
| 2056 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2057 | BMCWEB_LOG_ERROR("UserLockedForF" |
| 2058 | "ailedAttempt " |
| 2059 | "wasn't a bool"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2060 | messages::internalError(asyncResp->res); |
| 2061 | return; |
| 2062 | } |
| 2063 | asyncResp->res.jsonValue["Locked"] = *userLocked; |
| 2064 | asyncResp->res |
| 2065 | .jsonValue["Locked@Redfish.AllowableValues"] = { |
| 2066 | "false"}; // can only unlock accounts |
| 2067 | } |
| 2068 | else if (property.first == "UserPrivilege") |
| 2069 | { |
| 2070 | const std::string* userPrivPtr = |
| 2071 | std::get_if<std::string>(&property.second); |
| 2072 | if (userPrivPtr == nullptr) |
| 2073 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2074 | BMCWEB_LOG_ERROR("UserPrivilege wasn't a " |
| 2075 | "string"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2076 | messages::internalError(asyncResp->res); |
| 2077 | return; |
| 2078 | } |
| 2079 | std::string role = getRoleIdFromPrivilege(*userPrivPtr); |
| 2080 | if (role.empty()) |
| 2081 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2082 | BMCWEB_LOG_ERROR("Invalid user role"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2083 | messages::internalError(asyncResp->res); |
| 2084 | return; |
| 2085 | } |
| 2086 | asyncResp->res.jsonValue["RoleId"] = role; |
| 2087 | |
| 2088 | nlohmann::json& roleEntry = |
| 2089 | asyncResp->res.jsonValue["Links"]["Role"]; |
Ed Tanous | 3b32780 | 2023-08-14 09:23:43 -0700 | [diff] [blame] | 2090 | roleEntry["@odata.id"] = boost::urls::format( |
| 2091 | "/redfish/v1/AccountService/Roles/{}", role); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2092 | } |
| 2093 | else if (property.first == "UserPasswordExpired") |
| 2094 | { |
| 2095 | const bool* userPasswordExpired = |
| 2096 | std::get_if<bool>(&property.second); |
| 2097 | if (userPasswordExpired == nullptr) |
| 2098 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2099 | BMCWEB_LOG_ERROR( |
| 2100 | "UserPasswordExpired wasn't a bool"); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2101 | messages::internalError(asyncResp->res); |
| 2102 | return; |
| 2103 | } |
| 2104 | asyncResp->res.jsonValue["PasswordChangeRequired"] = |
| 2105 | *userPasswordExpired; |
| 2106 | } |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 2107 | else if (property.first == "UserGroups") |
| 2108 | { |
| 2109 | const std::vector<std::string>* userGroups = |
| 2110 | std::get_if<std::vector<std::string>>( |
| 2111 | &property.second); |
| 2112 | if (userGroups == nullptr) |
| 2113 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2114 | BMCWEB_LOG_ERROR( |
| 2115 | "userGroups wasn't a string vector"); |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 2116 | messages::internalError(asyncResp->res); |
| 2117 | return; |
| 2118 | } |
| 2119 | if (!translateUserGroup(*userGroups, asyncResp->res)) |
| 2120 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 2121 | BMCWEB_LOG_ERROR("userGroups mapping failed"); |
Abhishek Patel | c722981 | 2022-02-01 10:07:15 -0600 | [diff] [blame] | 2122 | messages::internalError(asyncResp->res); |
| 2123 | return; |
| 2124 | } |
| 2125 | } |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | |
Ed Tanous | 3b32780 | 2023-08-14 09:23:43 -0700 | [diff] [blame] | 2130 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 2131 | "/redfish/v1/AccountService/Accounts/{}", accountName); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2132 | asyncResp->res.jsonValue["Id"] = accountName; |
| 2133 | asyncResp->res.jsonValue["UserName"] = accountName; |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 2134 | }); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | inline void |
Gunnar Mills | 20fc307 | 2023-01-27 15:13:36 -0600 | [diff] [blame] | 2138 | handleAccountDelete(App& app, const crow::Request& req, |
| 2139 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2140 | const std::string& username) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2141 | { |
| 2142 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 2143 | { |
| 2144 | return; |
| 2145 | } |
| 2146 | |
| 2147 | #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
| 2148 | // If authentication is disabled, there are no user accounts |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 2149 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", username); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2150 | return; |
| 2151 | |
| 2152 | #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
| 2153 | sdbusplus::message::object_path tempObjPath(rootUserDbusPath); |
| 2154 | tempObjPath /= username; |
| 2155 | const std::string userPath(tempObjPath); |
| 2156 | |
| 2157 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 2158 | [asyncResp, username](const boost::system::error_code& ec) { |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2159 | if (ec) |
| 2160 | { |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 2161 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2162 | username); |
| 2163 | return; |
| 2164 | } |
| 2165 | |
| 2166 | messages::accountRemoved(asyncResp->res); |
| 2167 | }, |
| 2168 | "xyz.openbmc_project.User.Manager", userPath, |
| 2169 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2170 | } |
| 2171 | |
| 2172 | inline void |
| 2173 | handleAccountPatch(App& app, const crow::Request& req, |
| 2174 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2175 | const std::string& username) |
| 2176 | { |
| 2177 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 2178 | { |
| 2179 | return; |
| 2180 | } |
| 2181 | #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
| 2182 | // If authentication is disabled, there are no user accounts |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 2183 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", username); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2184 | return; |
| 2185 | |
| 2186 | #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
| 2187 | std::optional<std::string> newUserName; |
| 2188 | std::optional<std::string> password; |
| 2189 | std::optional<bool> enabled; |
| 2190 | std::optional<std::string> roleId; |
| 2191 | std::optional<bool> locked; |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2192 | std::optional<std::vector<std::string>> accountTypes; |
| 2193 | |
| 2194 | bool userSelf = (username == req.session->username); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2195 | |
| 2196 | if (req.session == nullptr) |
| 2197 | { |
| 2198 | messages::internalError(asyncResp->res); |
| 2199 | return; |
| 2200 | } |
| 2201 | |
| 2202 | Privileges effectiveUserPrivileges = |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 2203 | redfish::getUserPrivileges(*req.session); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2204 | Privileges configureUsers = {"ConfigureUsers"}; |
| 2205 | bool userHasConfigureUsers = |
| 2206 | effectiveUserPrivileges.isSupersetOf(configureUsers); |
| 2207 | if (userHasConfigureUsers) |
| 2208 | { |
| 2209 | // Users with ConfigureUsers can modify for all users |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2210 | if (!json_util::readJsonPatch( |
| 2211 | req, asyncResp->res, "UserName", newUserName, "Password", |
| 2212 | password, "RoleId", roleId, "Enabled", enabled, "Locked", |
| 2213 | locked, "AccountTypes", accountTypes)) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2214 | { |
| 2215 | return; |
| 2216 | } |
| 2217 | } |
| 2218 | else |
| 2219 | { |
| 2220 | // ConfigureSelf accounts can only modify their own account |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2221 | if (!userSelf) |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2222 | { |
| 2223 | messages::insufficientPrivilege(asyncResp->res); |
| 2224 | return; |
| 2225 | } |
| 2226 | |
| 2227 | // ConfigureSelf accounts can only modify their password |
| 2228 | if (!json_util::readJsonPatch(req, asyncResp->res, "Password", |
| 2229 | password)) |
| 2230 | { |
| 2231 | return; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | // if user name is not provided in the patch method or if it |
| 2236 | // matches the user name in the URI, then we are treating it as |
| 2237 | // updating user properties other then username. If username |
| 2238 | // provided doesn't match the URI, then we are treating this as |
| 2239 | // user rename request. |
| 2240 | if (!newUserName || (newUserName.value() == username)) |
| 2241 | { |
| 2242 | updateUserProperties(asyncResp, username, password, enabled, roleId, |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2243 | locked, accountTypes, userSelf); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2244 | return; |
| 2245 | } |
| 2246 | crow::connections::systemBus->async_method_call( |
| 2247 | [asyncResp, username, password(std::move(password)), |
| 2248 | roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)}, |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2249 | locked, userSelf, accountTypes(std::move(accountTypes))]( |
Ed Tanous | e81de51 | 2023-06-27 17:07:00 -0700 | [diff] [blame] | 2250 | const boost::system::error_code& ec, sdbusplus::message_t& m) { |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2251 | if (ec) |
| 2252 | { |
| 2253 | userErrorMessageHandler(m.get_error(), asyncResp, newUser, |
| 2254 | username); |
| 2255 | return; |
| 2256 | } |
| 2257 | |
| 2258 | updateUserProperties(asyncResp, newUser, password, enabled, roleId, |
Abhishek Patel | 5834585 | 2022-02-02 08:54:25 -0600 | [diff] [blame] | 2259 | locked, accountTypes, userSelf); |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2260 | }, |
| 2261 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 2262 | "xyz.openbmc_project.User.Manager", "RenameUser", username, |
| 2263 | *newUserName); |
| 2264 | } |
| 2265 | |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2266 | inline void requestAccountServiceRoutes(App& app) |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 2267 | { |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2268 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 2269 | .privileges(redfish::privileges::headAccountService) |
| 2270 | .methods(boost::beast::http::verb::head)( |
| 2271 | std::bind_front(handleAccountServiceHead, std::ref(app))); |
| 2272 | |
| 2273 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2274 | .privileges(redfish::privileges::getAccountService) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2275 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2276 | std::bind_front(handleAccountServiceGet, std::ref(app))); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2277 | |
Ed Tanous | f5ffd80 | 2021-07-19 10:55:33 -0700 | [diff] [blame] | 2278 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") |
Gunnar Mills | 1ec43ee | 2022-01-04 15:39:52 -0600 | [diff] [blame] | 2279 | .privileges(redfish::privileges::patchAccountService) |
Ed Tanous | f5ffd80 | 2021-07-19 10:55:33 -0700 | [diff] [blame] | 2280 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2281 | std::bind_front(handleAccountServicePatch, std::ref(app))); |
Ed Tanous | f5ffd80 | 2021-07-19 10:55:33 -0700 | [diff] [blame] | 2282 | |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2283 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 2284 | .privileges(redfish::privileges::headManagerAccountCollection) |
| 2285 | .methods(boost::beast::http::verb::head)( |
| 2286 | std::bind_front(handleAccountCollectionHead, std::ref(app))); |
| 2287 | |
| 2288 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2289 | .privileges(redfish::privileges::getManagerAccountCollection) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2290 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2291 | std::bind_front(handleAccountCollectionGet, std::ref(app))); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 2292 | |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2293 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2294 | .privileges(redfish::privileges::postManagerAccountCollection) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2295 | .methods(boost::beast::http::verb::post)( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2296 | std::bind_front(handleAccountCollectionPost, std::ref(app))); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2297 | |
| 2298 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") |
Ed Tanous | 4c7d4d3 | 2022-07-07 15:29:35 -0700 | [diff] [blame] | 2299 | .privileges(redfish::privileges::headManagerAccount) |
| 2300 | .methods(boost::beast::http::verb::head)( |
| 2301 | std::bind_front(handleAccountHead, std::ref(app))); |
| 2302 | |
| 2303 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2304 | .privileges(redfish::privileges::getManagerAccount) |
| 2305 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2306 | std::bind_front(handleAccountGet, std::ref(app))); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2307 | |
| 2308 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2309 | // TODO this privilege should be using the generated endpoints, but |
| 2310 | // because of the special handling of ConfigureSelf, it's not able to |
| 2311 | // yet |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2312 | .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}}) |
| 2313 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 1ef4c34 | 2022-05-12 16:12:36 -0700 | [diff] [blame] | 2314 | std::bind_front(handleAccountPatch, std::ref(app))); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2315 | |
| 2316 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 2317 | .privileges(redfish::privileges::deleteManagerAccount) |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2318 | .methods(boost::beast::http::verb::delete_)( |
Gunnar Mills | 20fc307 | 2023-01-27 15:13:36 -0600 | [diff] [blame] | 2319 | std::bind_front(handleAccountDelete, std::ref(app))); |
Ed Tanous | 6c51eab | 2021-06-03 12:30:29 -0700 | [diff] [blame] | 2320 | } |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 2321 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2322 | } // namespace redfish |