blob: 972512b145b74881537067c4412b42db31221a68 [file] [log] [blame]
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +01001/*
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, Dawid88d16c92018-02-02 14:51:09 +010017
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_utility.hpp"
20#include "error_messages.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070021#include "generated/enums/account_service.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "openbmc_dbus_rest.hpp"
23#include "persistent_data.hpp"
24#include "query.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070025#include "registries/privilege_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/dbus_utils.hpp"
27#include "utils/json_utils.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070028
Jonathan Doman1e1e5982021-06-11 09:36:17 -070029#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020030#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031
George Liu2b731192023-01-11 16:27:13 +080032#include <array>
Abhishek Patelc7229812022-02-01 10:07:15 -060033#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070034#include <ranges>
Abhishek Patelc7229812022-02-01 10:07:15 -060035#include <string>
George Liu2b731192023-01-11 16:27:13 +080036#include <string_view>
Abhishek Patelc7229812022-02-01 10:07:15 -060037#include <vector>
38
Ed Tanous1abe55e2018-09-05 08:30:59 -070039namespace redfish
40{
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +010041
Ed Tanous23a21a12020-07-25 04:45:05 +000042constexpr const char* ldapConfigObjectName =
Ratan Gupta6973a582018-12-13 18:25:44 +053043 "/xyz/openbmc_project/user/ldap/openldap";
Ed Tanous2c70f802020-09-28 14:29:23 -070044constexpr const char* adConfigObject =
Ratan Guptaab828d72019-04-22 14:18:33 +053045 "/xyz/openbmc_project/user/ldap/active_directory";
46
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +053047constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
Ratan Gupta6973a582018-12-13 18:25:44 +053048constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
49constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
50constexpr const char* ldapConfigInterface =
51 "xyz.openbmc_project.User.Ldap.Config";
52constexpr const char* ldapCreateInterface =
53 "xyz.openbmc_project.User.Ldap.Create";
54constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
Ratan Gupta06785242019-07-26 22:30:16 +053055constexpr const char* ldapPrivMapperInterface =
56 "xyz.openbmc_project.User.PrivilegeMapper";
Ratan Gupta6973a582018-12-13 18:25:44 +053057
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060058struct LDAPRoleMapData
59{
60 std::string groupName;
61 std::string privilege;
62};
63
Ratan Gupta6973a582018-12-13 18:25:44 +053064struct LDAPConfigData
65{
Ed Tanous47f29342024-03-19 12:18:06 -070066 std::string uri;
67 std::string bindDN;
68 std::string baseDN;
69 std::string searchScope;
70 std::string serverType;
Ratan Gupta6973a582018-12-13 18:25:44 +053071 bool serviceEnabled = false;
Ed Tanous47f29342024-03-19 12:18:06 -070072 std::string userNameAttribute;
73 std::string groupAttribute;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060074 std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
Ratan Gupta6973a582018-12-13 18:25:44 +053075};
76
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060077inline std::string getRoleIdFromPrivilege(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053078{
79 if (role == "priv-admin")
80 {
81 return "Administrator";
82 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070083 if (role == "priv-user")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053084 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053085 return "ReadOnly";
AppaRao Puli84e12cb2018-10-11 01:28:15 +053086 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070087 if (role == "priv-operator")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053088 {
89 return "Operator";
90 }
91 return "";
92}
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060093inline std::string getPrivilegeFromRoleId(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053094{
95 if (role == "Administrator")
96 {
97 return "priv-admin";
98 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070099 if (role == "ReadOnly")
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530100 {
101 return "priv-user";
102 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700103 if (role == "Operator")
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530104 {
105 return "priv-operator";
106 }
107 return "";
108}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700109
Abhishek Patelc7229812022-02-01 10:07:15 -0600110/**
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 */
120inline 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 Patelc7229812022-02-01 10:07:15 -0600137 accountTypes.emplace_back("ManagerConsole");
138 }
Ninad Palsule3e72c202023-03-27 17:19:55 -0500139 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 Patelc7229812022-02-01 10:07:15 -0600145 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 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800155 // Invalid user group name. Caller throws an exception.
Abhishek Patelc7229812022-02-01 10:07:15 -0600156 return false;
157 }
158 }
159
160 res.jsonValue["AccountTypes"] = std::move(accountTypes);
161 return true;
162}
163
Abhishek Patel58345852022-02-02 08:54:25 -0600164/**
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 */
173inline 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 Tanous62598e32023-07-17 17:06:25 -0700215 BMCWEB_LOG_ERROR(
216 "Missing Redfish or WebUI Account Type to set redfish User Group");
Abhishek Patel58345852022-02-02 08:54:25 -0600217 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 */
237inline 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 Tanous62598e32023-07-17 17:06:25 -0700247 BMCWEB_LOG_ERROR(
248 "User disabling OWN Redfish Account Type is not allowed");
Abhishek Patel58345852022-02-02 08:54:25 -0600249 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 }
Ed Tanousd02aad32024-02-13 14:43:34 -0800261 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
262 dbusObjectPath, "xyz.openbmc_project.User.Attributes",
263 "UserGroups", "AccountTypes", updatedUserGroups);
Abhishek Patel58345852022-02-02 08:54:25 -0600264}
265
zhanghch058d1b46d2021-04-01 11:18:24 +0800266inline void userErrorMessageHandler(
267 const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
268 const std::string& newUser, const std::string& username)
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000269{
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000270 if (e == nullptr)
271 {
272 messages::internalError(asyncResp->res);
273 return;
274 }
275
Manojkiran Eda055806b2020-11-03 09:36:28 +0530276 const char* errorMessage = e->name;
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000277 if (strcmp(errorMessage,
278 "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
279 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800280 messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000281 "UserName", newUser);
282 }
283 else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
284 "UserNameDoesNotExist") == 0)
285 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800286 messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000287 }
Ed Tanousd4d25792020-09-29 15:15:03 -0700288 else if ((strcmp(errorMessage,
289 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
290 0) ||
George Liu0fda0f12021-11-16 10:06:17 +0800291 (strcmp(
292 errorMessage,
293 "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
294 0))
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000295 {
296 messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
297 }
298 else if (strcmp(errorMessage,
299 "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
300 {
301 messages::createLimitReachedForResource(asyncResp->res);
302 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000303 else
304 {
Gunnar Millsb8ad5832023-10-02 16:26:07 -0500305 BMCWEB_LOG_ERROR("DBUS response error {}", errorMessage);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000306 messages::internalError(asyncResp->res);
307 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000308}
309
Ed Tanous81ce6092020-12-17 16:54:55 +0000310inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
Ed Tanous23a21a12020-07-25 04:45:05 +0000311 const LDAPConfigData& confData,
312 const std::string& ldapType)
Ratan Gupta6973a582018-12-13 18:25:44 +0530313{
Ed Tanous49cc2632024-03-20 12:49:15 -0700314 nlohmann::json::object_t ldap;
Ed Tanous14766872022-03-15 10:44:42 -0700315 ldap["ServiceEnabled"] = confData.serviceEnabled;
Ed Tanous49cc2632024-03-20 12:49:15 -0700316 nlohmann::json::array_t serviceAddresses;
317 serviceAddresses.emplace_back(confData.uri);
318 ldap["ServiceAddresses"] = std::move(serviceAddresses);
319
320 nlohmann::json::object_t authentication;
321 authentication["AuthenticationType"] =
Ed Tanous0ec8b832022-03-14 14:56:47 -0700322 account_service::AuthenticationTypes::UsernameAndPassword;
Ed Tanous49cc2632024-03-20 12:49:15 -0700323 authentication["Username"] = confData.bindDN;
324 authentication["Password"] = nullptr;
325 ldap["Authentication"] = std::move(authentication);
Ed Tanous14766872022-03-15 10:44:42 -0700326
Ed Tanous49cc2632024-03-20 12:49:15 -0700327 nlohmann::json::object_t ldapService;
328 nlohmann::json::object_t searchSettings;
329 nlohmann::json::array_t baseDistinguishedNames;
330 baseDistinguishedNames.emplace_back(confData.baseDN);
Ed Tanous14766872022-03-15 10:44:42 -0700331
Ed Tanous49cc2632024-03-20 12:49:15 -0700332 searchSettings["BaseDistinguishedNames"] =
333 std::move(baseDistinguishedNames);
334 searchSettings["UsernameAttribute"] = confData.userNameAttribute;
335 searchSettings["GroupsAttribute"] = confData.groupAttribute;
336 ldapService["SearchSettings"] = std::move(searchSettings);
337 ldap["LDAPService"] = std::move(ldapService);
338
339 nlohmann::json::array_t roleMapArray;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800340 for (const auto& obj : confData.groupRoleList)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600341 {
Ed Tanous62598e32023-07-17 17:06:25 -0700342 BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName);
Ed Tanous613dabe2022-07-09 11:17:36 -0700343
Ed Tanous613dabe2022-07-09 11:17:36 -0700344 nlohmann::json::object_t remoteGroup;
345 remoteGroup["RemoteGroup"] = obj.second.groupName;
Jorge Cisneros329f0342022-11-04 16:26:25 +0000346 remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
347 roleMapArray.emplace_back(std::move(remoteGroup));
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600348 }
Ed Tanous49cc2632024-03-20 12:49:15 -0700349
350 ldap["RemoteRoleMapping"] = std::move(roleMapArray);
351
352 jsonResponse[ldapType].update(ldap);
Ratan Gupta6973a582018-12-13 18:25:44 +0530353}
354
355/**
Ratan Gupta06785242019-07-26 22:30:16 +0530356 * @brief validates given JSON input and then calls appropriate method to
357 * create, to delete or to set Rolemapping object based on the given input.
358 *
359 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000360inline void handleRoleMapPatch(
zhanghch058d1b46d2021-04-01 11:18:24 +0800361 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ratan Gupta06785242019-07-26 22:30:16 +0530362 const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
Ed Tanousc1019822024-03-06 12:54:38 -0800363 const std::string& serverType,
364 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input)
Ratan Gupta06785242019-07-26 22:30:16 +0530365{
366 for (size_t index = 0; index < input.size(); index++)
367 {
Ed Tanousc1019822024-03-06 12:54:38 -0800368 std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson =
369 input[index];
370 nlohmann::json::object_t* obj =
371 std::get_if<nlohmann::json::object_t>(&thisJson);
372 if (obj == nullptr)
Ratan Gupta06785242019-07-26 22:30:16 +0530373 {
374 // delete the existing object
375 if (index < roleMapObjData.size())
376 {
377 crow::connections::systemBus->async_method_call(
378 [asyncResp, roleMapObjData, serverType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800379 index](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700380 if (ec)
381 {
Ed Tanous62598e32023-07-17 17:06:25 -0700382 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 messages::internalError(asyncResp->res);
384 return;
385 }
Patrick Williams89492a12023-05-10 07:51:34 -0500386 asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
387 [index] = nullptr;
Patrick Williams5a39f772023-10-20 11:20:21 -0500388 },
Ratan Gupta06785242019-07-26 22:30:16 +0530389 ldapDbusService, roleMapObjData[index].first,
390 "xyz.openbmc_project.Object.Delete", "Delete");
391 }
392 else
393 {
Ed Tanous62598e32023-07-17 17:06:25 -0700394 BMCWEB_LOG_ERROR("Can't delete the object");
Ed Tanousc1019822024-03-06 12:54:38 -0800395 messages::propertyValueTypeError(asyncResp->res, "null",
Ed Tanous2e8c4bd2022-06-27 12:59:12 -0700396 "RemoteRoleMapping/" +
397 std::to_string(index));
Ratan Gupta06785242019-07-26 22:30:16 +0530398 return;
399 }
400 }
Ed Tanousc1019822024-03-06 12:54:38 -0800401 else if (obj->empty())
Ratan Gupta06785242019-07-26 22:30:16 +0530402 {
403 // Don't do anything for the empty objects,parse next json
404 // eg {"RemoteRoleMapping",[{}]}
405 }
406 else
407 {
408 // update/create the object
409 std::optional<std::string> remoteGroup;
410 std::optional<std::string> localRole;
411
Ed Tanousc1019822024-03-06 12:54:38 -0800412 if (!json_util::readJsonObject(*obj, asyncResp->res, "RemoteGroup",
413 remoteGroup, "LocalRole", localRole))
Ratan Gupta06785242019-07-26 22:30:16 +0530414 {
415 continue;
416 }
417
418 // Update existing RoleMapping Object
419 if (index < roleMapObjData.size())
420 {
Ed Tanous62598e32023-07-17 17:06:25 -0700421 BMCWEB_LOG_DEBUG("Update Role Map Object");
Ratan Gupta06785242019-07-26 22:30:16 +0530422 // If "RemoteGroup" info is provided
423 if (remoteGroup)
424 {
Ed Tanousd02aad32024-02-13 14:43:34 -0800425 setDbusProperty(
426 asyncResp, ldapDbusService, roleMapObjData[index].first,
George Liu9ae226f2023-06-21 17:56:46 +0800427 "xyz.openbmc_project.User.PrivilegeMapperEntry",
Ed Tanousd02aad32024-02-13 14:43:34 -0800428 "GroupName",
429 std::format("RemoteRoleMapping/{}/RemoteGroup", index),
430 *remoteGroup);
Ratan Gupta06785242019-07-26 22:30:16 +0530431 }
432
433 // If "LocalRole" info is provided
434 if (localRole)
435 {
Ed Tanousd02aad32024-02-13 14:43:34 -0800436 setDbusProperty(
437 asyncResp, ldapDbusService, roleMapObjData[index].first,
George Liu9ae226f2023-06-21 17:56:46 +0800438 "xyz.openbmc_project.User.PrivilegeMapperEntry",
Ed Tanousd02aad32024-02-13 14:43:34 -0800439 "Privilege",
440 std::format("RemoteRoleMapping/{}/LocalRole", index),
441 *localRole);
Ratan Gupta06785242019-07-26 22:30:16 +0530442 }
443 }
444 // Create a new RoleMapping Object.
445 else
446 {
Ed Tanous62598e32023-07-17 17:06:25 -0700447 BMCWEB_LOG_DEBUG(
448 "setRoleMappingProperties: Creating new Object");
Patrick Williams89492a12023-05-10 07:51:34 -0500449 std::string pathString = "RemoteRoleMapping/" +
450 std::to_string(index);
Ratan Gupta06785242019-07-26 22:30:16 +0530451
452 if (!localRole)
453 {
454 messages::propertyMissing(asyncResp->res,
455 pathString + "/LocalRole");
456 continue;
457 }
458 if (!remoteGroup)
459 {
460 messages::propertyMissing(asyncResp->res,
461 pathString + "/RemoteGroup");
462 continue;
463 }
464
465 std::string dbusObjectPath;
466 if (serverType == "ActiveDirectory")
467 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700468 dbusObjectPath = adConfigObject;
Ratan Gupta06785242019-07-26 22:30:16 +0530469 }
470 else if (serverType == "LDAP")
471 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000472 dbusObjectPath = ldapConfigObjectName;
Ratan Gupta06785242019-07-26 22:30:16 +0530473 }
474
Ed Tanous62598e32023-07-17 17:06:25 -0700475 BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup,
476 *localRole);
Ratan Gupta06785242019-07-26 22:30:16 +0530477
478 crow::connections::systemBus->async_method_call(
Ed Tanous271584a2019-07-09 16:24:22 -0700479 [asyncResp, serverType, localRole,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800480 remoteGroup](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 if (ec)
482 {
Ed Tanous62598e32023-07-17 17:06:25 -0700483 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700484 messages::internalError(asyncResp->res);
485 return;
486 }
487 nlohmann::json& remoteRoleJson =
488 asyncResp->res
489 .jsonValue[serverType]["RemoteRoleMapping"];
490 nlohmann::json::object_t roleMapEntry;
491 roleMapEntry["LocalRole"] = *localRole;
492 roleMapEntry["RemoteGroup"] = *remoteGroup;
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500493 remoteRoleJson.emplace_back(std::move(roleMapEntry));
Patrick Williams5a39f772023-10-20 11:20:21 -0500494 },
Ratan Gupta06785242019-07-26 22:30:16 +0530495 ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
Ed Tanous3174e4d2020-10-07 11:41:22 -0700496 "Create", *remoteGroup,
Ratan Gupta06785242019-07-26 22:30:16 +0530497 getPrivilegeFromRoleId(std::move(*localRole)));
498 }
499 }
500 }
501}
502
503/**
Ratan Gupta6973a582018-12-13 18:25:44 +0530504 * Function that retrieves all properties for LDAP config object
505 * into JSON
506 */
507template <typename CallbackFunc>
508inline void getLDAPConfigData(const std::string& ldapType,
509 CallbackFunc&& callback)
510{
George Liu2b731192023-01-11 16:27:13 +0800511 constexpr std::array<std::string_view, 2> interfaces = {
512 ldapEnableInterface, ldapConfigInterface};
Ratan Gupta6973a582018-12-13 18:25:44 +0530513
George Liu2b731192023-01-11 16:27:13 +0800514 dbus::utility::getDbusObject(
515 ldapConfigObjectName, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700516 [callback = std::forward<CallbackFunc>(callback),
Ed Tanousc1019822024-03-06 12:54:38 -0800517 ldapType](const boost::system::error_code& ec,
518 const dbus::utility::MapperGetObject& resp) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700519 if (ec || resp.empty())
520 {
Carson Labradobf2dded2023-08-10 00:37:06 +0000521 BMCWEB_LOG_WARNING(
Ed Tanous62598e32023-07-17 17:06:25 -0700522 "DBUS response error during getting of service name: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 LDAPConfigData empty{};
524 callback(false, empty, ldapType);
525 return;
526 }
527 std::string service = resp.begin()->first;
George Liu5eb468d2023-06-20 17:03:24 +0800528 sdbusplus::message::object_path path(ldapRootObject);
529 dbus::utility::getManagedObjects(
530 service, path,
Ed Tanousc1019822024-03-06 12:54:38 -0800531 [callback, ldapType](
532 const boost::system::error_code& ec2,
533 const dbus::utility::ManagedObjectType& ldapObjects) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700534 LDAPConfigData confData{};
Ed Tanous8b242752023-06-27 17:17:13 -0700535 if (ec2)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600536 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700537 callback(false, confData, ldapType);
Carson Labradobf2dded2023-08-10 00:37:06 +0000538 BMCWEB_LOG_WARNING("D-Bus responses error: {}", ec2);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600539 return;
540 }
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600541
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 std::string ldapDbusType;
543 std::string searchString;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600544
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 if (ldapType == "LDAP")
546 {
547 ldapDbusType =
548 "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
549 searchString = "openldap";
550 }
551 else if (ldapType == "ActiveDirectory")
552 {
553 ldapDbusType =
554 "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
555 searchString = "active_directory";
556 }
557 else
558 {
Ed Tanous62598e32023-07-17 17:06:25 -0700559 BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}",
560 ldapType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700561 callback(false, confData, ldapType);
562 return;
563 }
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600564
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 std::string ldapEnableInterfaceStr = ldapEnableInterface;
566 std::string ldapConfigInterfaceStr = ldapConfigInterface;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600567
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 for (const auto& object : ldapObjects)
569 {
570 // let's find the object whose ldap type is equal to the
571 // given type
572 if (object.first.str.find(searchString) == std::string::npos)
573 {
574 continue;
575 }
576
577 for (const auto& interface : object.second)
578 {
579 if (interface.first == ldapEnableInterfaceStr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600580 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 // rest of the properties are string.
582 for (const auto& property : interface.second)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600583 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 if (property.first == "Enabled")
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600585 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 const bool* value =
587 std::get_if<bool>(&property.second);
588 if (value == nullptr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600589 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 continue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600591 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 confData.serviceEnabled = *value;
593 break;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600594 }
595 }
596 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700597 else if (interface.first == ldapConfigInterfaceStr)
598 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 for (const auto& property : interface.second)
600 {
601 const std::string* strValue =
602 std::get_if<std::string>(&property.second);
603 if (strValue == nullptr)
604 {
605 continue;
606 }
607 if (property.first == "LDAPServerURI")
608 {
609 confData.uri = *strValue;
610 }
611 else if (property.first == "LDAPBindDN")
612 {
613 confData.bindDN = *strValue;
614 }
615 else if (property.first == "LDAPBaseDN")
616 {
617 confData.baseDN = *strValue;
618 }
619 else if (property.first == "LDAPSearchScope")
620 {
621 confData.searchScope = *strValue;
622 }
623 else if (property.first == "GroupNameAttribute")
624 {
625 confData.groupAttribute = *strValue;
626 }
627 else if (property.first == "UserNameAttribute")
628 {
629 confData.userNameAttribute = *strValue;
630 }
631 else if (property.first == "LDAPType")
632 {
633 confData.serverType = *strValue;
634 }
635 }
636 }
637 else if (interface.first ==
638 "xyz.openbmc_project.User.PrivilegeMapperEntry")
639 {
640 LDAPRoleMapData roleMapData{};
641 for (const auto& property : interface.second)
642 {
643 const std::string* strValue =
644 std::get_if<std::string>(&property.second);
645
646 if (strValue == nullptr)
647 {
648 continue;
649 }
650
651 if (property.first == "GroupName")
652 {
653 roleMapData.groupName = *strValue;
654 }
655 else if (property.first == "Privilege")
656 {
657 roleMapData.privilege = *strValue;
658 }
659 }
660
661 confData.groupRoleList.emplace_back(object.first.str,
662 roleMapData);
663 }
664 }
665 }
666 callback(true, confData, ldapType);
George Liu2b731192023-01-11 16:27:13 +0800667 });
Patrick Williams5a39f772023-10-20 11:20:21 -0500668 });
Ratan Gupta6973a582018-12-13 18:25:44 +0530669}
670
Ed Tanous6c51eab2021-06-03 12:30:29 -0700671/**
Ed Tanous6c51eab2021-06-03 12:30:29 -0700672 * @brief updates the LDAP server address and updates the
673 json response with the new value.
674 * @param serviceAddressList address to be updated.
675 * @param asyncResp pointer to the JSON response
676 * @param ldapServerElementName Type of LDAP
677 server(openLDAP/ActiveDirectory)
678 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530679
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700680inline void handleServiceAddressPatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700681 const std::vector<std::string>& serviceAddressList,
682 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
683 const std::string& ldapServerElementName,
684 const std::string& ldapConfigObject)
685{
Ed Tanousd02aad32024-02-13 14:43:34 -0800686 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
687 ldapConfigInterface, "LDAPServerURI",
688 ldapServerElementName + "/ServiceAddress",
689 serviceAddressList.front());
Ed Tanous6c51eab2021-06-03 12:30:29 -0700690}
691/**
692 * @brief updates the LDAP Bind DN and updates the
693 json response with the new value.
694 * @param username name of the user which needs to be updated.
695 * @param asyncResp pointer to the JSON response
696 * @param ldapServerElementName Type of LDAP
697 server(openLDAP/ActiveDirectory)
698 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530699
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700700inline void
701 handleUserNamePatch(const std::string& username,
702 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
703 const std::string& ldapServerElementName,
704 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700705{
Ed Tanousd02aad32024-02-13 14:43:34 -0800706 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
707 ldapConfigInterface, "LDAPBindDN",
708 ldapServerElementName + "/Authentication/Username",
709 username);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700710}
711
712/**
713 * @brief updates the LDAP password
714 * @param password : ldap password which needs to be updated.
715 * @param asyncResp pointer to the JSON response
716 * @param ldapServerElementName Type of LDAP
717 * server(openLDAP/ActiveDirectory)
718 */
719
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700720inline void
721 handlePasswordPatch(const std::string& password,
722 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
723 const std::string& ldapServerElementName,
724 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700725{
Ed Tanousd02aad32024-02-13 14:43:34 -0800726 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
727 ldapConfigInterface, "LDAPBindDNPassword",
728 ldapServerElementName + "/Authentication/Password",
729 password);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700730}
731
732/**
733 * @brief updates the LDAP BaseDN and updates the
734 json response with the new value.
735 * @param baseDNList baseDN list which needs to be updated.
736 * @param asyncResp pointer to the JSON response
737 * @param ldapServerElementName Type of LDAP
738 server(openLDAP/ActiveDirectory)
739 */
740
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700741inline void
742 handleBaseDNPatch(const std::vector<std::string>& baseDNList,
743 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
744 const std::string& ldapServerElementName,
745 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700746{
Ed Tanousd02aad32024-02-13 14:43:34 -0800747 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
748 ldapConfigInterface, "LDAPBaseDN",
749 ldapServerElementName +
750 "/LDAPService/SearchSettings/BaseDistinguishedNames",
751 baseDNList.front());
Ed Tanous6c51eab2021-06-03 12:30:29 -0700752}
753/**
754 * @brief updates the LDAP user name attribute and updates the
755 json response with the new value.
756 * @param userNameAttribute attribute to be updated.
757 * @param asyncResp pointer to the JSON response
758 * @param ldapServerElementName Type of LDAP
759 server(openLDAP/ActiveDirectory)
760 */
761
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700762inline void
763 handleUserNameAttrPatch(const std::string& userNameAttribute,
764 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
765 const std::string& ldapServerElementName,
766 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700767{
Ed Tanousd02aad32024-02-13 14:43:34 -0800768 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
769 ldapConfigInterface, "UserNameAttribute",
770 ldapServerElementName +
771 "LDAPService/SearchSettings/UsernameAttribute",
772 userNameAttribute);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700773}
774/**
775 * @brief updates the LDAP group attribute and updates the
776 json response with the new value.
777 * @param groupsAttribute attribute to be updated.
778 * @param asyncResp pointer to the JSON response
779 * @param ldapServerElementName Type of LDAP
780 server(openLDAP/ActiveDirectory)
781 */
782
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700783inline void handleGroupNameAttrPatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700784 const std::string& groupsAttribute,
785 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
786 const std::string& ldapServerElementName,
787 const std::string& ldapConfigObject)
788{
Ed Tanousd02aad32024-02-13 14:43:34 -0800789 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
790 ldapConfigInterface, "GroupNameAttribute",
791 ldapServerElementName +
792 "/LDAPService/SearchSettings/GroupsAttribute",
793 groupsAttribute);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700794}
795/**
796 * @brief updates the LDAP service enable and updates the
797 json response with the new value.
798 * @param input JSON data.
799 * @param asyncResp pointer to the JSON response
800 * @param ldapServerElementName Type of LDAP
801 server(openLDAP/ActiveDirectory)
802 */
803
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700804inline void handleServiceEnablePatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700805 bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
806 const std::string& ldapServerElementName,
807 const std::string& ldapConfigObject)
808{
Ed Tanousd02aad32024-02-13 14:43:34 -0800809 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
810 ldapEnableInterface, "Enabled",
811 ldapServerElementName + "/ServiceEnabled", serviceEnabled);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700812}
813
Ed Tanousc1019822024-03-06 12:54:38 -0800814struct AuthMethods
Ed Tanous6c51eab2021-06-03 12:30:29 -0700815{
816 std::optional<bool> basicAuth;
817 std::optional<bool> cookie;
818 std::optional<bool> sessionToken;
819 std::optional<bool> xToken;
820 std::optional<bool> tls;
Ed Tanousc1019822024-03-06 12:54:38 -0800821};
Ed Tanous6c51eab2021-06-03 12:30:29 -0700822
Ed Tanousc1019822024-03-06 12:54:38 -0800823inline void
824 handleAuthMethodsPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
825 const AuthMethods& auth)
826{
827 persistent_data::AuthConfigMethods& authMethodsConfig =
Ed Tanous6c51eab2021-06-03 12:30:29 -0700828 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
829
Ed Tanousc1019822024-03-06 12:54:38 -0800830 if (auth.basicAuth)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700831 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700832 if constexpr (!BMCWEB_BASIC_AUTH)
833 {
834 messages::actionNotSupported(
835 asyncResp->res,
836 "Setting BasicAuth when basic-auth feature is disabled");
837 return;
838 }
839
Ed Tanousc1019822024-03-06 12:54:38 -0800840 authMethodsConfig.basic = *auth.basicAuth;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700841 }
842
Ed Tanousc1019822024-03-06 12:54:38 -0800843 if (auth.cookie)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700844 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700845 if constexpr (!BMCWEB_COOKIE_AUTH)
846 {
847 messages::actionNotSupported(
848 asyncResp->res,
849 "Setting Cookie when cookie-auth feature is disabled");
850 return;
851 }
Ed Tanousc1019822024-03-06 12:54:38 -0800852 authMethodsConfig.cookie = *auth.cookie;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700853 }
854
Ed Tanousc1019822024-03-06 12:54:38 -0800855 if (auth.sessionToken)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700856 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700857 if constexpr (!BMCWEB_SESSION_AUTH)
858 {
859 messages::actionNotSupported(
860 asyncResp->res,
861 "Setting SessionToken when session-auth feature is disabled");
862 return;
863 }
Ed Tanousc1019822024-03-06 12:54:38 -0800864 authMethodsConfig.sessionToken = *auth.sessionToken;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700865 }
866
Ed Tanousc1019822024-03-06 12:54:38 -0800867 if (auth.xToken)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700868 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700869 if constexpr (!BMCWEB_XTOKEN_AUTH)
870 {
871 messages::actionNotSupported(
872 asyncResp->res,
873 "Setting XToken when xtoken-auth feature is disabled");
874 return;
875 }
Ed Tanousc1019822024-03-06 12:54:38 -0800876 authMethodsConfig.xtoken = *auth.xToken;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700877 }
878
Ed Tanousc1019822024-03-06 12:54:38 -0800879 if (auth.tls)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700880 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700881 if constexpr (!BMCWEB_MUTUAL_TLS_AUTH)
882 {
883 messages::actionNotSupported(
884 asyncResp->res,
885 "Setting TLS when mutual-tls-auth feature is disabled");
886 return;
887 }
Ed Tanousc1019822024-03-06 12:54:38 -0800888 authMethodsConfig.tls = *auth.tls;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700889 }
890
891 if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
892 !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
893 !authMethodsConfig.tls)
894 {
895 // Do not allow user to disable everything
896 messages::actionNotSupported(asyncResp->res,
897 "of disabling all available methods");
898 return;
899 }
900
901 persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
902 authMethodsConfig);
903 // Save configuration immediately
904 persistent_data::getConfig().writeData();
905
906 messages::success(asyncResp->res);
907}
908
909/**
910 * @brief Get the required values from the given JSON, validates the
911 * value and create the LDAP config object.
912 * @param input JSON data
913 * @param asyncResp pointer to the JSON response
914 * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
915 */
916
Ed Tanous10cb44f2024-04-11 13:05:20 -0700917struct LdapPatchParams
918{
919 std::optional<std::string> authType;
920 std::optional<std::vector<std::string>> serviceAddressList;
921 std::optional<bool> serviceEnabled;
922 std::optional<std::vector<std::string>> baseDNList;
923 std::optional<std::string> userNameAttribute;
924 std::optional<std::string> groupsAttribute;
925 std::optional<std::string> userName;
926 std::optional<std::string> password;
927 std::optional<
928 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
929 remoteRoleMapData;
930};
931
932inline void handleLDAPPatch(LdapPatchParams&& input,
Ed Tanous6c51eab2021-06-03 12:30:29 -0700933 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
934 const std::string& serverType)
935{
936 std::string dbusObjectPath;
937 if (serverType == "ActiveDirectory")
938 {
939 dbusObjectPath = adConfigObject;
940 }
941 else if (serverType == "LDAP")
942 {
943 dbusObjectPath = ldapConfigObjectName;
944 }
945 else
946 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700947 BMCWEB_LOG_ERROR("serverType wasn't AD or LDAP but was {}????",
948 serverType);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700949 return;
950 }
951
Ed Tanous10cb44f2024-04-11 13:05:20 -0700952 if (input.authType && *input.authType != "UsernameAndPassword")
Ed Tanous6c51eab2021-06-03 12:30:29 -0700953 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700954 messages::propertyValueNotInList(asyncResp->res, *input.authType,
Ed Tanousc1019822024-03-06 12:54:38 -0800955 "AuthenticationType");
956 return;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700957 }
Ed Tanousc1019822024-03-06 12:54:38 -0800958
Ed Tanous10cb44f2024-04-11 13:05:20 -0700959 if (input.serviceAddressList)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700960 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700961 if (input.serviceAddressList->empty())
Ratan Guptaeb2bbe52019-04-22 14:27:01 +0530962 {
Ed Tanouse2616cc2022-06-27 12:45:55 -0700963 messages::propertyValueNotInList(
Ed Tanous10cb44f2024-04-11 13:05:20 -0700964 asyncResp->res, *input.serviceAddressList, "ServiceAddress");
Ed Tanouscb13a392020-07-25 19:02:03 +0000965 return;
966 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700967 }
Ed Tanous10cb44f2024-04-11 13:05:20 -0700968 if (input.baseDNList)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700969 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700970 if (input.baseDNList->empty())
Ratan Gupta8a07d282019-03-16 08:33:47 +0530971 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700972 messages::propertyValueNotInList(asyncResp->res, *input.baseDNList,
Ed Tanous6c51eab2021-06-03 12:30:29 -0700973 "BaseDistinguishedNames");
Ratan Gupta8a07d282019-03-16 08:33:47 +0530974 return;
975 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700976 }
Ratan Gupta8a07d282019-03-16 08:33:47 +0530977
Ed Tanous6c51eab2021-06-03 12:30:29 -0700978 // nothing to update, then return
Ed Tanous10cb44f2024-04-11 13:05:20 -0700979 if (!input.userName && !input.password && !input.serviceAddressList &&
980 !input.baseDNList && !input.userNameAttribute &&
981 !input.groupsAttribute && !input.serviceEnabled &&
982 !input.remoteRoleMapData)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700983 {
984 return;
985 }
986
987 // Get the existing resource first then keep modifying
988 // whenever any property gets updated.
Ed Tanous10cb44f2024-04-11 13:05:20 -0700989 getLDAPConfigData(serverType,
990 [asyncResp, input = std::move(input),
991 dbusObjectPath = std::move(dbusObjectPath)](
992 bool success, const LDAPConfigData& confData,
993 const std::string& serverT) mutable {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700994 if (!success)
Ratan Gupta8a07d282019-03-16 08:33:47 +0530995 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700996 messages::internalError(asyncResp->res);
997 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530998 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700999 parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
1000 if (confData.serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301001 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001002 // Disable the service first and update the rest of
1003 // the properties.
1004 handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301005 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001006
Ed Tanous10cb44f2024-04-11 13:05:20 -07001007 if (input.serviceAddressList)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301008 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001009 handleServiceAddressPatch(*input.serviceAddressList, asyncResp,
1010 serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301011 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001012 if (input.userName)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001013 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001014 handleUserNamePatch(*input.userName, asyncResp, serverT,
1015 dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001016 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001017 if (input.password)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001018 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001019 handlePasswordPatch(*input.password, asyncResp, serverT,
1020 dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001021 }
1022
Ed Tanous10cb44f2024-04-11 13:05:20 -07001023 if (input.baseDNList)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301024 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001025 handleBaseDNPatch(*input.baseDNList, asyncResp, serverT,
1026 dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001027 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001028 if (input.userNameAttribute)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001029 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001030 handleUserNameAttrPatch(*input.userNameAttribute, asyncResp,
1031 serverT, dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001032 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001033 if (input.groupsAttribute)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001034 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001035 handleGroupNameAttrPatch(*input.groupsAttribute, asyncResp, serverT,
Ed Tanous6c51eab2021-06-03 12:30:29 -07001036 dbusObjectPath);
1037 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001038 if (input.serviceEnabled)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001039 {
1040 // if user has given the value as true then enable
1041 // the service. if user has given false then no-op
1042 // as service is already stopped.
Ed Tanous10cb44f2024-04-11 13:05:20 -07001043 if (*input.serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301044 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001045 handleServiceEnablePatch(*input.serviceEnabled, asyncResp,
1046 serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301047 }
1048 }
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001049 else
1050 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001051 // if user has not given the service enabled value
1052 // then revert it to the same state as it was
1053 // before.
1054 handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
1055 serverT, dbusObjectPath);
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001056 }
Ed Tanous04ae99e2018-09-20 15:54:36 -07001057
Ed Tanous10cb44f2024-04-11 13:05:20 -07001058 if (input.remoteRoleMapData)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001059 {
1060 handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
Ed Tanous10cb44f2024-04-11 13:05:20 -07001061 *input.remoteRoleMapData);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001062 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001063 });
Ed Tanous6c51eab2021-06-03 12:30:29 -07001064}
1065
Abhishek Patel58345852022-02-02 08:54:25 -06001066inline void updateUserProperties(
1067 std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1068 const std::optional<std::string>& password,
1069 const std::optional<bool>& enabled,
1070 const std::optional<std::string>& roleId, const std::optional<bool>& locked,
1071 std::optional<std::vector<std::string>> accountTypes, bool userSelf)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001072{
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301073 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1074 tempObjPath /= username;
1075 std::string dbusObjectPath(tempObjPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001076
1077 dbus::utility::checkDbusPathExists(
Ed Tanous618c14b2022-06-30 17:44:25 -07001078 dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
Abhishek Patel58345852022-02-02 08:54:25 -06001079 locked, accountTypes(std::move(accountTypes)),
1080 userSelf, asyncResp{std::move(asyncResp)}](int rc) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001081 if (rc <= 0)
1082 {
1083 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1084 username);
1085 return;
1086 }
1087
1088 if (password)
1089 {
1090 int retval = pamUpdatePassword(username, *password);
1091
1092 if (retval == PAM_USER_UNKNOWN)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001093 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001094 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1095 username);
Patrick Williams5a39f772023-10-20 11:20:21 -05001096 }
1097 else if (retval == PAM_AUTHTOK_ERR)
1098 {
1099 // If password is invalid
1100 messages::propertyValueFormatError(asyncResp->res, nullptr,
1101 "Password");
1102 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
1103 }
1104 else if (retval != PAM_SUCCESS)
1105 {
1106 messages::internalError(asyncResp->res);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001107 return;
1108 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001109 else
Ed Tanous618c14b2022-06-30 17:44:25 -07001110 {
Patrick Williams5a39f772023-10-20 11:20:21 -05001111 messages::success(asyncResp->res);
Ed Tanous618c14b2022-06-30 17:44:25 -07001112 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001113 }
Ed Tanous618c14b2022-06-30 17:44:25 -07001114
Patrick Williams5a39f772023-10-20 11:20:21 -05001115 if (enabled)
1116 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001117 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1118 dbusObjectPath,
1119 "xyz.openbmc_project.User.Attributes",
1120 "UserEnabled", "Enabled", *enabled);
Patrick Williams5a39f772023-10-20 11:20:21 -05001121 }
1122
1123 if (roleId)
1124 {
1125 std::string priv = getPrivilegeFromRoleId(*roleId);
1126 if (priv.empty())
1127 {
1128 messages::propertyValueNotInList(asyncResp->res, true,
1129 "Locked");
1130 return;
Ed Tanous618c14b2022-06-30 17:44:25 -07001131 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001132 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1133 dbusObjectPath,
1134 "xyz.openbmc_project.User.Attributes",
1135 "UserPrivilege", "RoleId", priv);
Patrick Williams5a39f772023-10-20 11:20:21 -05001136 }
1137
1138 if (locked)
1139 {
1140 // admin can unlock the account which is locked by
1141 // successive authentication failures but admin should
1142 // not be allowed to lock an account.
1143 if (*locked)
Abhishek Patel58345852022-02-02 08:54:25 -06001144 {
Patrick Williams5a39f772023-10-20 11:20:21 -05001145 messages::propertyValueNotInList(asyncResp->res, "true",
1146 "Locked");
1147 return;
Abhishek Patel58345852022-02-02 08:54:25 -06001148 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001149 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1150 dbusObjectPath,
1151 "xyz.openbmc_project.User.Attributes",
1152 "UserLockedForFailedAttempt", "Locked", *locked);
Patrick Williams5a39f772023-10-20 11:20:21 -05001153 }
1154
1155 if (accountTypes)
1156 {
1157 patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
1158 userSelf);
1159 }
1160 });
Ed Tanous6c51eab2021-06-03 12:30:29 -07001161}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001162
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001163inline void handleAccountServiceHead(
1164 App& app, const crow::Request& req,
1165 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001166{
1167 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1168 {
1169 return;
1170 }
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001171 asyncResp->res.addHeader(
1172 boost::beast::http::field::link,
1173 "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1174}
1175
1176inline void
1177 handleAccountServiceGet(App& app, const crow::Request& req,
1178 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1179{
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001180 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1181 {
1182 return;
1183 }
Ninad Palsule3e72c202023-03-27 17:19:55 -05001184
1185 if (req.session == nullptr)
1186 {
1187 messages::internalError(asyncResp->res);
1188 return;
1189 }
1190
Ed Tanousc1019822024-03-06 12:54:38 -08001191 const persistent_data::AuthConfigMethods& authMethodsConfig =
1192 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
1193
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001194 asyncResp->res.addHeader(
1195 boost::beast::http::field::link,
1196 "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1197
Ed Tanous1ef4c342022-05-12 16:12:36 -07001198 nlohmann::json& json = asyncResp->res.jsonValue;
1199 json["@odata.id"] = "/redfish/v1/AccountService";
Ravi Teja482a69e2024-04-22 06:56:13 -05001200 json["@odata.type"] = "#AccountService.v1_15_0.AccountService";
Ed Tanous1ef4c342022-05-12 16:12:36 -07001201 json["Id"] = "AccountService";
1202 json["Name"] = "Account Service";
1203 json["Description"] = "Account Service";
1204 json["ServiceEnabled"] = true;
1205 json["MaxPasswordLength"] = 20;
1206 json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
1207 json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
Ravi Teja482a69e2024-04-22 06:56:13 -05001208 json["HTTPBasicAuth"] = authMethodsConfig.basic
1209 ? account_service::BasicAuthState::Enabled
1210 : account_service::BasicAuthState::Disabled;
1211
1212 nlohmann::json::array_t allowed;
1213 allowed.emplace_back(account_service::BasicAuthState::Enabled);
1214 allowed.emplace_back(account_service::BasicAuthState::Disabled);
1215 json["HTTPBasicAuth@AllowableValues"] = std::move(allowed);
1216
Ed Tanous1ef4c342022-05-12 16:12:36 -07001217 json["Oem"]["OpenBMC"]["@odata.type"] =
Ed Tanous5b5574a2022-09-26 19:53:36 -07001218 "#OpenBMCAccountService.v1_0_0.AccountService";
Ed Tanous1ef4c342022-05-12 16:12:36 -07001219 json["Oem"]["OpenBMC"]["@odata.id"] =
1220 "/redfish/v1/AccountService#/Oem/OpenBMC";
1221 json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
1222 authMethodsConfig.basic;
1223 json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
1224 authMethodsConfig.sessionToken;
1225 json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
1226 json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
1227 json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
1228
1229 // /redfish/v1/AccountService/LDAP/Certificates is something only
1230 // ConfigureManager can access then only display when the user has
1231 // permissions ConfigureManager
1232 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001233 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001234
1235 if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
1236 effectiveUserPrivileges))
1237 {
1238 asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
1239 "/redfish/v1/AccountService/LDAP/Certificates";
1240 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001241 sdbusplus::asio::getAllProperties(
1242 *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1243 "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001244 [asyncResp](const boost::system::error_code& ec,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001245 const dbus::utility::DBusPropertiesMap& propertiesList) {
1246 if (ec)
1247 {
1248 messages::internalError(asyncResp->res);
1249 return;
1250 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001251
Ed Tanous62598e32023-07-17 17:06:25 -07001252 BMCWEB_LOG_DEBUG("Got {}properties for AccountService",
1253 propertiesList.size());
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001254
1255 const uint8_t* minPasswordLength = nullptr;
1256 const uint32_t* accountUnlockTimeout = nullptr;
1257 const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1258
1259 const bool success = sdbusplus::unpackPropertiesNoThrow(
1260 dbus_utils::UnpackErrorPrinter(), propertiesList,
1261 "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1262 accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1263 maxLoginAttemptBeforeLockout);
1264
1265 if (!success)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001266 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001267 messages::internalError(asyncResp->res);
1268 return;
Ed Tanous1ef4c342022-05-12 16:12:36 -07001269 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001270
1271 if (minPasswordLength != nullptr)
1272 {
1273 asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
1274 }
1275
1276 if (accountUnlockTimeout != nullptr)
1277 {
1278 asyncResp->res.jsonValue["AccountLockoutDuration"] =
1279 *accountUnlockTimeout;
1280 }
1281
1282 if (maxLoginAttemptBeforeLockout != nullptr)
1283 {
1284 asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1285 *maxLoginAttemptBeforeLockout;
1286 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001287 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001288
Ed Tanous02cad962022-06-30 16:50:15 -07001289 auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001290 const std::string& ldapType) {
1291 if (!success)
1292 {
1293 return;
1294 }
1295 parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1296 };
1297
1298 getLDAPConfigData("LDAP", callback);
1299 getLDAPConfigData("ActiveDirectory", callback);
1300}
1301
1302inline void handleAccountServicePatch(
1303 App& app, const crow::Request& req,
1304 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1305{
1306 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1307 {
1308 return;
1309 }
1310 std::optional<uint32_t> unlockTimeout;
1311 std::optional<uint16_t> lockoutThreshold;
1312 std::optional<uint8_t> minPasswordLength;
1313 std::optional<uint16_t> maxPasswordLength;
Ed Tanous10cb44f2024-04-11 13:05:20 -07001314 LdapPatchParams ldapObject;
1315 LdapPatchParams activeDirectoryObject;
Ed Tanousc1019822024-03-06 12:54:38 -08001316 AuthMethods auth;
Ravi Teja482a69e2024-04-22 06:56:13 -05001317 std::optional<std::string> httpBasicAuth;
Ed Tanousc1019822024-03-06 12:54:38 -08001318 // clang-format off
Ed Tanous1ef4c342022-05-12 16:12:36 -07001319 if (!json_util::readJsonPatch(
Ed Tanousc1019822024-03-06 12:54:38 -08001320 req, asyncResp->res,
1321 "AccountLockoutDuration", unlockTimeout,
1322 "AccountLockoutThreshold", lockoutThreshold,
Ed Tanous10cb44f2024-04-11 13:05:20 -07001323 "ActiveDirectory/Authentication/AuthenticationType", activeDirectoryObject.authType,
1324 "ActiveDirectory/Authentication/Password", activeDirectoryObject.password,
1325 "ActiveDirectory/Authentication/Username", activeDirectoryObject.userName,
1326 "ActiveDirectory/LDAPService/SearchSettings/BaseDistinguishedNames", activeDirectoryObject.baseDNList,
1327 "ActiveDirectory/LDAPService/SearchSettings/GroupsAttribute", activeDirectoryObject.groupsAttribute,
1328 "ActiveDirectory/LDAPService/SearchSettings/UsernameAttribute", activeDirectoryObject.userNameAttribute,
1329 "ActiveDirectory/RemoteRoleMapping", activeDirectoryObject.remoteRoleMapData,
1330 "ActiveDirectory/ServiceAddresses", activeDirectoryObject.serviceAddressList,
1331 "ActiveDirectory/ServiceEnabled", activeDirectoryObject.serviceEnabled,
1332 "LDAP/Authentication/AuthenticationType", ldapObject.authType,
1333 "LDAP/Authentication/Password", ldapObject.password,
1334 "LDAP/Authentication/Username", ldapObject.userName,
1335 "LDAP/LDAPService/SearchSettings/BaseDistinguishedNames", ldapObject.baseDNList,
1336 "LDAP/LDAPService/SearchSettings/GroupsAttribute", ldapObject.groupsAttribute,
1337 "LDAP/LDAPService/SearchSettings/UsernameAttribute", ldapObject.userNameAttribute,
1338 "LDAP/RemoteRoleMapping", ldapObject.remoteRoleMapData,
1339 "LDAP/ServiceAddresses", ldapObject.serviceAddressList,
1340 "LDAP/ServiceEnabled", ldapObject.serviceEnabled,
Ed Tanousc1019822024-03-06 12:54:38 -08001341 "MaxPasswordLength", maxPasswordLength,
1342 "MinPasswordLength", minPasswordLength,
Ed Tanousc1019822024-03-06 12:54:38 -08001343 "Oem/OpenBMC/AuthMethods/BasicAuth", auth.basicAuth,
1344 "Oem/OpenBMC/AuthMethods/Cookie", auth.cookie,
1345 "Oem/OpenBMC/AuthMethods/SessionToken", auth.sessionToken,
Ed Tanous10cb44f2024-04-11 13:05:20 -07001346 "Oem/OpenBMC/AuthMethods/TLS", auth.tls,
Ravi Teja482a69e2024-04-22 06:56:13 -05001347 "Oem/OpenBMC/AuthMethods/XToken", auth.xToken,
1348 "HTTPBasicAuth", httpBasicAuth))
Ed Tanous1ef4c342022-05-12 16:12:36 -07001349 {
1350 return;
1351 }
Ed Tanousc1019822024-03-06 12:54:38 -08001352 // clang-format on
Ed Tanous1ef4c342022-05-12 16:12:36 -07001353
Ravi Teja482a69e2024-04-22 06:56:13 -05001354 if (httpBasicAuth)
1355 {
1356 if (*httpBasicAuth == "Enabled")
1357 {
1358 auth.basicAuth = true;
1359 }
1360 else if (*httpBasicAuth == "Disabled")
1361 {
1362 auth.basicAuth = false;
1363 }
1364 else
1365 {
1366 messages::propertyValueNotInList(asyncResp->res, "HttpBasicAuth",
1367 *httpBasicAuth);
1368 }
1369 }
1370
Ed Tanous1ef4c342022-05-12 16:12:36 -07001371 if (minPasswordLength)
1372 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001373 setDbusProperty(
1374 asyncResp, "xyz.openbmc_project.User.Manager",
1375 sdbusplus::message::object_path("/xyz/openbmc_project/user"),
George Liu9ae226f2023-06-21 17:56:46 +08001376 "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
Ed Tanousd02aad32024-02-13 14:43:34 -08001377 "MinPasswordLength", *minPasswordLength);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001378 }
1379
1380 if (maxPasswordLength)
1381 {
1382 messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1383 }
1384
Ed Tanous10cb44f2024-04-11 13:05:20 -07001385 handleLDAPPatch(std::move(activeDirectoryObject), asyncResp,
1386 "ActiveDirectory");
1387 handleLDAPPatch(std::move(ldapObject), asyncResp, "LDAP");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001388
Ed Tanousc1019822024-03-06 12:54:38 -08001389 handleAuthMethodsPatch(asyncResp, auth);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001390
Ed Tanous1ef4c342022-05-12 16:12:36 -07001391 if (unlockTimeout)
1392 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001393 setDbusProperty(
1394 asyncResp, "xyz.openbmc_project.User.Manager",
1395 sdbusplus::message::object_path("/xyz/openbmc_project/user"),
Ed Tanous1ef4c342022-05-12 16:12:36 -07001396 "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
Ed Tanousd02aad32024-02-13 14:43:34 -08001397 "AccountLockoutDuration", *unlockTimeout);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001398 }
1399 if (lockoutThreshold)
1400 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001401 setDbusProperty(
1402 asyncResp, "xyz.openbmc_project.User.Manager",
1403 sdbusplus::message::object_path("/xyz/openbmc_project/user"),
George Liu9ae226f2023-06-21 17:56:46 +08001404 "xyz.openbmc_project.User.AccountPolicy",
Ed Tanousd02aad32024-02-13 14:43:34 -08001405 "MaxLoginAttemptBeforeLockout", "AccountLockoutThreshold",
1406 *lockoutThreshold);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001407 }
1408}
1409
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001410inline void handleAccountCollectionHead(
Ed Tanous1ef4c342022-05-12 16:12:36 -07001411 App& app, const crow::Request& req,
1412 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1413{
1414 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1415 {
1416 return;
1417 }
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001418 asyncResp->res.addHeader(
1419 boost::beast::http::field::link,
1420 "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
1421}
1422
1423inline void handleAccountCollectionGet(
1424 App& app, const crow::Request& req,
1425 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1426{
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001427 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1428 {
1429 return;
1430 }
Ninad Palsule3e72c202023-03-27 17:19:55 -05001431
1432 if (req.session == nullptr)
1433 {
1434 messages::internalError(asyncResp->res);
1435 return;
1436 }
1437
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001438 asyncResp->res.addHeader(
1439 boost::beast::http::field::link,
1440 "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001441
1442 asyncResp->res.jsonValue["@odata.id"] =
1443 "/redfish/v1/AccountService/Accounts";
1444 asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
1445 "ManagerAccountCollection";
1446 asyncResp->res.jsonValue["Name"] = "Accounts Collection";
1447 asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
1448
1449 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001450 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001451
1452 std::string thisUser;
1453 if (req.session)
1454 {
1455 thisUser = req.session->username;
1456 }
George Liu5eb468d2023-06-20 17:03:24 +08001457 sdbusplus::message::object_path path("/xyz/openbmc_project/user");
1458 dbus::utility::getManagedObjects(
1459 "xyz.openbmc_project.User.Manager", path,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001460 [asyncResp, thisUser, effectiveUserPrivileges](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001461 const boost::system::error_code& ec,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001462 const dbus::utility::ManagedObjectType& users) {
1463 if (ec)
1464 {
1465 messages::internalError(asyncResp->res);
1466 return;
1467 }
1468
1469 bool userCanSeeAllAccounts =
1470 effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1471
1472 bool userCanSeeSelf =
1473 effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1474
1475 nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1476 memberArray = nlohmann::json::array();
1477
1478 for (const auto& userpath : users)
1479 {
1480 std::string user = userpath.first.filename();
1481 if (user.empty())
1482 {
1483 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001484 BMCWEB_LOG_ERROR("Invalid firmware ID");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001485
1486 return;
1487 }
1488
1489 // As clarified by Redfish here:
1490 // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
1491 // Users without ConfigureUsers, only see their own
1492 // account. Users with ConfigureUsers, see all
1493 // accounts.
1494 if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1495 {
1496 nlohmann::json::object_t member;
Ed Tanous3b327802023-08-14 09:23:43 -07001497 member["@odata.id"] = boost::urls::format(
1498 "/redfish/v1/AccountService/Accounts/{}", user);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001499 memberArray.emplace_back(std::move(member));
Ed Tanous1ef4c342022-05-12 16:12:36 -07001500 }
1501 }
1502 asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05001503 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001504}
1505
Ninad Palsule97e90da2023-05-17 14:04:52 -05001506inline void processAfterCreateUser(
1507 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1508 const std::string& username, const std::string& password,
1509 const boost::system::error_code& ec, sdbusplus::message_t& m)
1510{
1511 if (ec)
1512 {
1513 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
1514 return;
1515 }
1516
1517 if (pamUpdatePassword(username, password) != PAM_SUCCESS)
1518 {
1519 // At this point we have a user that's been
1520 // created, but the password set
1521 // failed.Something is wrong, so delete the user
1522 // that we've already created
1523 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1524 tempObjPath /= username;
1525 const std::string userPath(tempObjPath);
1526
1527 crow::connections::systemBus->async_method_call(
1528 [asyncResp, password](const boost::system::error_code& ec3) {
1529 if (ec3)
1530 {
1531 messages::internalError(asyncResp->res);
1532 return;
1533 }
1534
1535 // If password is invalid
Jason M. Bills9bd80832023-08-30 15:19:41 -07001536 messages::propertyValueFormatError(asyncResp->res, nullptr,
Ninad Palsule97e90da2023-05-17 14:04:52 -05001537 "Password");
Patrick Williams5a39f772023-10-20 11:20:21 -05001538 },
Ninad Palsule97e90da2023-05-17 14:04:52 -05001539 "xyz.openbmc_project.User.Manager", userPath,
1540 "xyz.openbmc_project.Object.Delete", "Delete");
1541
Ed Tanous62598e32023-07-17 17:06:25 -07001542 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
Ninad Palsule97e90da2023-05-17 14:04:52 -05001543 return;
1544 }
1545
1546 messages::created(asyncResp->res);
1547 asyncResp->res.addHeader("Location",
1548 "/redfish/v1/AccountService/Accounts/" + username);
1549}
1550
1551inline void processAfterGetAllGroups(
1552 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1553 const std::string& username, const std::string& password,
Ed Tanouse01d0c32023-06-30 13:21:32 -07001554 const std::string& roleId, bool enabled,
Ninad Palsule9ba73932023-06-01 16:38:57 -05001555 std::optional<std::vector<std::string>> accountTypes,
Ninad Palsule97e90da2023-05-17 14:04:52 -05001556 const std::vector<std::string>& allGroupsList)
Ninad Palsule97e90da2023-05-17 14:04:52 -05001557{
Ninad Palsule3e72c202023-03-27 17:19:55 -05001558 std::vector<std::string> userGroups;
Ninad Palsule9ba73932023-06-01 16:38:57 -05001559 std::vector<std::string> accountTypeUserGroups;
1560
1561 // If user specified account types then convert them to unix user groups
1562 if (accountTypes)
1563 {
1564 if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
1565 accountTypeUserGroups))
1566 {
1567 // Problem in mapping Account Types to User Groups, Error already
1568 // logged.
1569 return;
1570 }
1571 }
1572
Ninad Palsule3e72c202023-03-27 17:19:55 -05001573 for (const auto& grp : allGroupsList)
1574 {
Ninad Palsule9ba73932023-06-01 16:38:57 -05001575 // If user specified the account type then only accept groups which are
1576 // in the account types group list.
1577 if (!accountTypeUserGroups.empty())
1578 {
1579 bool found = false;
1580 for (const auto& grp1 : accountTypeUserGroups)
1581 {
1582 if (grp == grp1)
1583 {
1584 found = true;
1585 break;
1586 }
1587 }
1588 if (!found)
1589 {
1590 continue;
1591 }
1592 }
1593
Ninad Palsule3e72c202023-03-27 17:19:55 -05001594 // Console access is provided to the user who is a member of
1595 // hostconsole group and has a administrator role. So, set
1596 // hostconsole group only for the administrator.
Ninad Palsule9ba73932023-06-01 16:38:57 -05001597 if ((grp == "hostconsole") && (roleId != "priv-admin"))
Ninad Palsule3e72c202023-03-27 17:19:55 -05001598 {
Ninad Palsule9ba73932023-06-01 16:38:57 -05001599 if (!accountTypeUserGroups.empty())
1600 {
Ed Tanous62598e32023-07-17 17:06:25 -07001601 BMCWEB_LOG_ERROR(
1602 "Only administrator can get HostConsole access");
Ninad Palsule9ba73932023-06-01 16:38:57 -05001603 asyncResp->res.result(boost::beast::http::status::bad_request);
1604 return;
1605 }
1606 continue;
Ninad Palsule3e72c202023-03-27 17:19:55 -05001607 }
Ninad Palsule9ba73932023-06-01 16:38:57 -05001608 userGroups.emplace_back(grp);
1609 }
1610
1611 // Make sure user specified groups are valid. This is internal error because
1612 // it some inconsistencies between user manager and bmcweb.
1613 if (!accountTypeUserGroups.empty() &&
1614 accountTypeUserGroups.size() != userGroups.size())
1615 {
1616 messages::internalError(asyncResp->res);
1617 return;
Ninad Palsule3e72c202023-03-27 17:19:55 -05001618 }
Ninad Palsule97e90da2023-05-17 14:04:52 -05001619 crow::connections::systemBus->async_method_call(
1620 [asyncResp, username, password](const boost::system::error_code& ec2,
1621 sdbusplus::message_t& m) {
1622 processAfterCreateUser(asyncResp, username, password, ec2, m);
Patrick Williams5a39f772023-10-20 11:20:21 -05001623 },
Ninad Palsule97e90da2023-05-17 14:04:52 -05001624 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
Ninad Palsule3e72c202023-03-27 17:19:55 -05001625 "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
Ed Tanouse01d0c32023-06-30 13:21:32 -07001626 roleId, enabled);
Ninad Palsule97e90da2023-05-17 14:04:52 -05001627}
1628
Ed Tanous1ef4c342022-05-12 16:12:36 -07001629inline void handleAccountCollectionPost(
1630 App& app, const crow::Request& req,
1631 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1632{
1633 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1634 {
1635 return;
1636 }
1637 std::string username;
1638 std::string password;
Ed Tanouse01d0c32023-06-30 13:21:32 -07001639 std::optional<std::string> roleIdJson;
1640 std::optional<bool> enabledJson;
Ninad Palsule9ba73932023-06-01 16:38:57 -05001641 std::optional<std::vector<std::string>> accountTypes;
Ed Tanouse01d0c32023-06-30 13:21:32 -07001642 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
1643 "Password", password, "RoleId", roleIdJson,
1644 "Enabled", enabledJson, "AccountTypes",
1645 accountTypes))
Ed Tanous1ef4c342022-05-12 16:12:36 -07001646 {
1647 return;
1648 }
1649
Ed Tanouse01d0c32023-06-30 13:21:32 -07001650 std::string roleId = roleIdJson.value_or("User");
1651 std::string priv = getPrivilegeFromRoleId(roleId);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001652 if (priv.empty())
1653 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001654 messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001655 return;
1656 }
Asmitha Karunanithi239adf82022-03-25 02:59:03 -05001657 roleId = priv;
Ed Tanous1ef4c342022-05-12 16:12:36 -07001658
Ed Tanouse01d0c32023-06-30 13:21:32 -07001659 bool enabled = enabledJson.value_or(true);
1660
Ed Tanous1ef4c342022-05-12 16:12:36 -07001661 // Reading AllGroups property
1662 sdbusplus::asio::getProperty<std::vector<std::string>>(
1663 *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1664 "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
1665 "AllGroups",
Ninad Palsule9ba73932023-06-01 16:38:57 -05001666 [asyncResp, username, password{std::move(password)}, roleId, enabled,
1667 accountTypes](const boost::system::error_code& ec,
1668 const std::vector<std::string>& allGroupsList) {
Ed Tanous1ef4c342022-05-12 16:12:36 -07001669 if (ec)
1670 {
Ed Tanous62598e32023-07-17 17:06:25 -07001671 BMCWEB_LOG_DEBUG("ERROR with async_method_call");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001672 messages::internalError(asyncResp->res);
1673 return;
1674 }
1675
1676 if (allGroupsList.empty())
1677 {
1678 messages::internalError(asyncResp->res);
1679 return;
1680 }
1681
Ninad Palsule97e90da2023-05-17 14:04:52 -05001682 processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
Ninad Palsule9ba73932023-06-01 16:38:57 -05001683 accountTypes, allGroupsList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001684 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001685}
1686
1687inline void
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001688 handleAccountHead(App& app, const crow::Request& req,
1689 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1690 const std::string& /*accountName*/)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001691{
1692 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1693 {
1694 return;
1695 }
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001696 asyncResp->res.addHeader(
1697 boost::beast::http::field::link,
1698 "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1699}
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001700
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001701inline void
1702 handleAccountGet(App& app, const crow::Request& req,
1703 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1704 const std::string& accountName)
1705{
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001706 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1707 {
1708 return;
1709 }
1710 asyncResp->res.addHeader(
1711 boost::beast::http::field::link,
1712 "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1713
Ed Tanous25b54db2024-04-17 15:40:31 -07001714 if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1715 {
1716 // If authentication is disabled, there are no user accounts
1717 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1718 accountName);
1719 return;
1720 }
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001721
Ed Tanous1ef4c342022-05-12 16:12:36 -07001722 if (req.session == nullptr)
1723 {
1724 messages::internalError(asyncResp->res);
1725 return;
1726 }
1727 if (req.session->username != accountName)
1728 {
1729 // At this point we've determined that the user is trying to
1730 // modify a user that isn't them. We need to verify that they
1731 // have permissions to modify other users, so re-run the auth
1732 // check with the same permissions, minus ConfigureSelf.
1733 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001734 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001735 Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
1736 "ConfigureManager"};
1737 if (!effectiveUserPrivileges.isSupersetOf(
1738 requiredPermissionsToChangeNonSelf))
1739 {
Ed Tanous62598e32023-07-17 17:06:25 -07001740 BMCWEB_LOG_DEBUG("GET Account denied access");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001741 messages::insufficientPrivilege(asyncResp->res);
1742 return;
1743 }
1744 }
1745
George Liu5eb468d2023-06-20 17:03:24 +08001746 sdbusplus::message::object_path path("/xyz/openbmc_project/user");
1747 dbus::utility::getManagedObjects(
1748 "xyz.openbmc_project.User.Manager", path,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001749 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001750 accountName](const boost::system::error_code& ec,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001751 const dbus::utility::ManagedObjectType& users) {
1752 if (ec)
1753 {
1754 messages::internalError(asyncResp->res);
1755 return;
1756 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001757 const auto userIt = std::ranges::find_if(
Michael Shen80f79a42023-08-24 13:41:53 +00001758 users,
1759 [accountName](
1760 const std::pair<sdbusplus::message::object_path,
1761 dbus::utility::DBusInterfacesMap>& user) {
1762 return accountName == user.first.filename();
Patrick Williams5a39f772023-10-20 11:20:21 -05001763 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001764
1765 if (userIt == users.end())
1766 {
1767 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1768 accountName);
1769 return;
1770 }
1771
1772 asyncResp->res.jsonValue["@odata.type"] =
Abhishek Patel58345852022-02-02 08:54:25 -06001773 "#ManagerAccount.v1_7_0.ManagerAccount";
Ed Tanous1ef4c342022-05-12 16:12:36 -07001774 asyncResp->res.jsonValue["Name"] = "User Account";
1775 asyncResp->res.jsonValue["Description"] = "User Account";
1776 asyncResp->res.jsonValue["Password"] = nullptr;
Abhishek Patel58345852022-02-02 08:54:25 -06001777 asyncResp->res.jsonValue["StrictAccountTypes"] = true;
Ed Tanous1ef4c342022-05-12 16:12:36 -07001778
1779 for (const auto& interface : userIt->second)
1780 {
1781 if (interface.first == "xyz.openbmc_project.User.Attributes")
1782 {
1783 for (const auto& property : interface.second)
1784 {
1785 if (property.first == "UserEnabled")
1786 {
1787 const bool* userEnabled =
1788 std::get_if<bool>(&property.second);
1789 if (userEnabled == nullptr)
1790 {
Ed Tanous62598e32023-07-17 17:06:25 -07001791 BMCWEB_LOG_ERROR("UserEnabled wasn't a bool");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001792 messages::internalError(asyncResp->res);
1793 return;
1794 }
1795 asyncResp->res.jsonValue["Enabled"] = *userEnabled;
1796 }
1797 else if (property.first == "UserLockedForFailedAttempt")
1798 {
1799 const bool* userLocked =
1800 std::get_if<bool>(&property.second);
1801 if (userLocked == nullptr)
1802 {
Ed Tanous62598e32023-07-17 17:06:25 -07001803 BMCWEB_LOG_ERROR("UserLockedForF"
1804 "ailedAttempt "
1805 "wasn't a bool");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001806 messages::internalError(asyncResp->res);
1807 return;
1808 }
1809 asyncResp->res.jsonValue["Locked"] = *userLocked;
1810 asyncResp->res
1811 .jsonValue["Locked@Redfish.AllowableValues"] = {
1812 "false"}; // can only unlock accounts
1813 }
1814 else if (property.first == "UserPrivilege")
1815 {
1816 const std::string* userPrivPtr =
1817 std::get_if<std::string>(&property.second);
1818 if (userPrivPtr == nullptr)
1819 {
Ed Tanous62598e32023-07-17 17:06:25 -07001820 BMCWEB_LOG_ERROR("UserPrivilege wasn't a "
1821 "string");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001822 messages::internalError(asyncResp->res);
1823 return;
1824 }
1825 std::string role = getRoleIdFromPrivilege(*userPrivPtr);
1826 if (role.empty())
1827 {
Ed Tanous62598e32023-07-17 17:06:25 -07001828 BMCWEB_LOG_ERROR("Invalid user role");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001829 messages::internalError(asyncResp->res);
1830 return;
1831 }
1832 asyncResp->res.jsonValue["RoleId"] = role;
1833
1834 nlohmann::json& roleEntry =
1835 asyncResp->res.jsonValue["Links"]["Role"];
Ed Tanous3b327802023-08-14 09:23:43 -07001836 roleEntry["@odata.id"] = boost::urls::format(
1837 "/redfish/v1/AccountService/Roles/{}", role);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001838 }
1839 else if (property.first == "UserPasswordExpired")
1840 {
1841 const bool* userPasswordExpired =
1842 std::get_if<bool>(&property.second);
1843 if (userPasswordExpired == nullptr)
1844 {
Ed Tanous62598e32023-07-17 17:06:25 -07001845 BMCWEB_LOG_ERROR(
1846 "UserPasswordExpired wasn't a bool");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001847 messages::internalError(asyncResp->res);
1848 return;
1849 }
1850 asyncResp->res.jsonValue["PasswordChangeRequired"] =
1851 *userPasswordExpired;
1852 }
Abhishek Patelc7229812022-02-01 10:07:15 -06001853 else if (property.first == "UserGroups")
1854 {
1855 const std::vector<std::string>* userGroups =
1856 std::get_if<std::vector<std::string>>(
1857 &property.second);
1858 if (userGroups == nullptr)
1859 {
Ed Tanous62598e32023-07-17 17:06:25 -07001860 BMCWEB_LOG_ERROR(
1861 "userGroups wasn't a string vector");
Abhishek Patelc7229812022-02-01 10:07:15 -06001862 messages::internalError(asyncResp->res);
1863 return;
1864 }
1865 if (!translateUserGroup(*userGroups, asyncResp->res))
1866 {
Ed Tanous62598e32023-07-17 17:06:25 -07001867 BMCWEB_LOG_ERROR("userGroups mapping failed");
Abhishek Patelc7229812022-02-01 10:07:15 -06001868 messages::internalError(asyncResp->res);
1869 return;
1870 }
1871 }
Ed Tanous1ef4c342022-05-12 16:12:36 -07001872 }
1873 }
1874 }
1875
Ed Tanous3b327802023-08-14 09:23:43 -07001876 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1877 "/redfish/v1/AccountService/Accounts/{}", accountName);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001878 asyncResp->res.jsonValue["Id"] = accountName;
1879 asyncResp->res.jsonValue["UserName"] = accountName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001880 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001881}
1882
1883inline void
Gunnar Mills20fc3072023-01-27 15:13:36 -06001884 handleAccountDelete(App& app, const crow::Request& req,
1885 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1886 const std::string& username)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001887{
1888 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1889 {
1890 return;
1891 }
1892
Ed Tanous25b54db2024-04-17 15:40:31 -07001893 if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1894 {
1895 // If authentication is disabled, there are no user accounts
1896 messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1897 return;
1898 }
Ed Tanous1ef4c342022-05-12 16:12:36 -07001899 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1900 tempObjPath /= username;
1901 const std::string userPath(tempObjPath);
1902
1903 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001904 [asyncResp, username](const boost::system::error_code& ec) {
Ed Tanous1ef4c342022-05-12 16:12:36 -07001905 if (ec)
1906 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001907 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
Ed Tanous1ef4c342022-05-12 16:12:36 -07001908 username);
1909 return;
1910 }
1911
1912 messages::accountRemoved(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001913 },
Ed Tanous1ef4c342022-05-12 16:12:36 -07001914 "xyz.openbmc_project.User.Manager", userPath,
1915 "xyz.openbmc_project.Object.Delete", "Delete");
1916}
1917
1918inline void
1919 handleAccountPatch(App& app, const crow::Request& req,
1920 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1921 const std::string& username)
1922{
1923 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1924 {
1925 return;
1926 }
Ed Tanous25b54db2024-04-17 15:40:31 -07001927 if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1928 {
1929 // If authentication is disabled, there are no user accounts
1930 messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1931 return;
1932 }
Ed Tanous1ef4c342022-05-12 16:12:36 -07001933 std::optional<std::string> newUserName;
1934 std::optional<std::string> password;
1935 std::optional<bool> enabled;
1936 std::optional<std::string> roleId;
1937 std::optional<bool> locked;
Abhishek Patel58345852022-02-02 08:54:25 -06001938 std::optional<std::vector<std::string>> accountTypes;
1939
Ed Tanous1ef4c342022-05-12 16:12:36 -07001940 if (req.session == nullptr)
1941 {
1942 messages::internalError(asyncResp->res);
1943 return;
1944 }
1945
Ed Tanous2b9c1df2024-04-06 13:52:01 -07001946 bool userSelf = (username == req.session->username);
1947
Ed Tanous1ef4c342022-05-12 16:12:36 -07001948 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001949 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001950 Privileges configureUsers = {"ConfigureUsers"};
1951 bool userHasConfigureUsers =
1952 effectiveUserPrivileges.isSupersetOf(configureUsers);
1953 if (userHasConfigureUsers)
1954 {
1955 // Users with ConfigureUsers can modify for all users
Abhishek Patel58345852022-02-02 08:54:25 -06001956 if (!json_util::readJsonPatch(
1957 req, asyncResp->res, "UserName", newUserName, "Password",
1958 password, "RoleId", roleId, "Enabled", enabled, "Locked",
1959 locked, "AccountTypes", accountTypes))
Ed Tanous1ef4c342022-05-12 16:12:36 -07001960 {
1961 return;
1962 }
1963 }
1964 else
1965 {
1966 // ConfigureSelf accounts can only modify their own account
Abhishek Patel58345852022-02-02 08:54:25 -06001967 if (!userSelf)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001968 {
1969 messages::insufficientPrivilege(asyncResp->res);
1970 return;
1971 }
1972
1973 // ConfigureSelf accounts can only modify their password
1974 if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
1975 password))
1976 {
1977 return;
1978 }
1979 }
1980
1981 // if user name is not provided in the patch method or if it
1982 // matches the user name in the URI, then we are treating it as
1983 // updating user properties other then username. If username
1984 // provided doesn't match the URI, then we are treating this as
1985 // user rename request.
1986 if (!newUserName || (newUserName.value() == username))
1987 {
1988 updateUserProperties(asyncResp, username, password, enabled, roleId,
Abhishek Patel58345852022-02-02 08:54:25 -06001989 locked, accountTypes, userSelf);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001990 return;
1991 }
1992 crow::connections::systemBus->async_method_call(
1993 [asyncResp, username, password(std::move(password)),
1994 roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
Abhishek Patel58345852022-02-02 08:54:25 -06001995 locked, userSelf, accountTypes(std::move(accountTypes))](
Ed Tanouse81de512023-06-27 17:07:00 -07001996 const boost::system::error_code& ec, sdbusplus::message_t& m) {
Ed Tanous1ef4c342022-05-12 16:12:36 -07001997 if (ec)
1998 {
1999 userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2000 username);
2001 return;
2002 }
2003
2004 updateUserProperties(asyncResp, newUser, password, enabled, roleId,
Abhishek Patel58345852022-02-02 08:54:25 -06002005 locked, accountTypes, userSelf);
Patrick Williams5a39f772023-10-20 11:20:21 -05002006 },
Ed Tanous1ef4c342022-05-12 16:12:36 -07002007 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
2008 "xyz.openbmc_project.User.Manager", "RenameUser", username,
2009 *newUserName);
2010}
2011
Ed Tanous6c51eab2021-06-03 12:30:29 -07002012inline void requestAccountServiceRoutes(App& app)
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07002013{
Ed Tanous6c51eab2021-06-03 12:30:29 -07002014 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Ed Tanous4c7d4d32022-07-07 15:29:35 -07002015 .privileges(redfish::privileges::headAccountService)
2016 .methods(boost::beast::http::verb::head)(
2017 std::bind_front(handleAccountServiceHead, std::ref(app)));
2018
2019 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Ed Tanoused398212021-06-09 17:05:54 -07002020 .privileges(redfish::privileges::getAccountService)
Ed Tanous002d39b2022-05-31 08:59:27 -07002021 .methods(boost::beast::http::verb::get)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002022 std::bind_front(handleAccountServiceGet, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002023
Ed Tanousf5ffd802021-07-19 10:55:33 -07002024 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Gunnar Mills1ec43ee2022-01-04 15:39:52 -06002025 .privileges(redfish::privileges::patchAccountService)
Ed Tanousf5ffd802021-07-19 10:55:33 -07002026 .methods(boost::beast::http::verb::patch)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002027 std::bind_front(handleAccountServicePatch, std::ref(app)));
Ed Tanousf5ffd802021-07-19 10:55:33 -07002028
Ed Tanous6c51eab2021-06-03 12:30:29 -07002029 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanous4c7d4d32022-07-07 15:29:35 -07002030 .privileges(redfish::privileges::headManagerAccountCollection)
2031 .methods(boost::beast::http::verb::head)(
2032 std::bind_front(handleAccountCollectionHead, std::ref(app)));
2033
2034 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanoused398212021-06-09 17:05:54 -07002035 .privileges(redfish::privileges::getManagerAccountCollection)
Ed Tanous6c51eab2021-06-03 12:30:29 -07002036 .methods(boost::beast::http::verb::get)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002037 std::bind_front(handleAccountCollectionGet, std::ref(app)));
Ed Tanous06e086d2018-09-19 17:19:52 -07002038
Ed Tanous6c51eab2021-06-03 12:30:29 -07002039 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanoused398212021-06-09 17:05:54 -07002040 .privileges(redfish::privileges::postManagerAccountCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 .methods(boost::beast::http::verb::post)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002042 std::bind_front(handleAccountCollectionPost, std::ref(app)));
Ed Tanous002d39b2022-05-31 08:59:27 -07002043
2044 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanous4c7d4d32022-07-07 15:29:35 -07002045 .privileges(redfish::privileges::headManagerAccount)
2046 .methods(boost::beast::http::verb::head)(
2047 std::bind_front(handleAccountHead, std::ref(app)));
2048
2049 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanous002d39b2022-05-31 08:59:27 -07002050 .privileges(redfish::privileges::getManagerAccount)
2051 .methods(boost::beast::http::verb::get)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002052 std::bind_front(handleAccountGet, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002053
2054 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002055 // TODO this privilege should be using the generated endpoints, but
2056 // because of the special handling of ConfigureSelf, it's not able to
2057 // yet
Ed Tanous6c51eab2021-06-03 12:30:29 -07002058 .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
2059 .methods(boost::beast::http::verb::patch)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002060 std::bind_front(handleAccountPatch, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002061
2062 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002063 .privileges(redfish::privileges::deleteManagerAccount)
Ed Tanous6c51eab2021-06-03 12:30:29 -07002064 .methods(boost::beast::http::verb::delete_)(
Gunnar Mills20fc3072023-01-27 15:13:36 -06002065 std::bind_front(handleAccountDelete, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002066}
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +01002067
Ed Tanous1abe55e2018-09-05 08:30:59 -07002068} // namespace redfish