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