blob: 0e1d3a103311af32c373a4c1be7c27f4e4b20ff0 [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"
Ed Tanous1aa375b2024-04-13 11:51:10 -070019#include "certificate_service.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "dbus_utility.hpp"
21#include "error_messages.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070022#include "generated/enums/account_service.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "persistent_data.hpp"
24#include "query.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070025#include "registries/privilege_registry.hpp"
Ed Tanous1aa375b2024-04-13 11:51:10 -070026#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "utils/dbus_utils.hpp"
28#include "utils/json_utils.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070029
Ed Tanous1aa375b2024-04-13 11:51:10 -070030#include <boost/url/format.hpp>
31#include <boost/url/url.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070032#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020033#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050034
George Liu2b731192023-01-11 16:27:13 +080035#include <array>
Ed Tanous1aa375b2024-04-13 11:51:10 -070036#include <memory>
Abhishek Patelc7229812022-02-01 10:07:15 -060037#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070038#include <ranges>
Abhishek Patelc7229812022-02-01 10:07:15 -060039#include <string>
George Liu2b731192023-01-11 16:27:13 +080040#include <string_view>
Abhishek Patelc7229812022-02-01 10:07:15 -060041#include <vector>
42
Ed Tanous1abe55e2018-09-05 08:30:59 -070043namespace redfish
44{
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +010045
Ed Tanous23a21a12020-07-25 04:45:05 +000046constexpr const char* ldapConfigObjectName =
Ratan Gupta6973a582018-12-13 18:25:44 +053047 "/xyz/openbmc_project/user/ldap/openldap";
Ed Tanous2c70f802020-09-28 14:29:23 -070048constexpr const char* adConfigObject =
Ratan Guptaab828d72019-04-22 14:18:33 +053049 "/xyz/openbmc_project/user/ldap/active_directory";
50
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +053051constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
Ratan Gupta6973a582018-12-13 18:25:44 +053052constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
53constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
54constexpr const char* ldapConfigInterface =
55 "xyz.openbmc_project.User.Ldap.Config";
56constexpr const char* ldapCreateInterface =
57 "xyz.openbmc_project.User.Ldap.Create";
58constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
Ratan Gupta06785242019-07-26 22:30:16 +053059constexpr const char* ldapPrivMapperInterface =
60 "xyz.openbmc_project.User.PrivilegeMapper";
Ratan Gupta6973a582018-12-13 18:25:44 +053061
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060062struct LDAPRoleMapData
63{
64 std::string groupName;
65 std::string privilege;
66};
67
Ratan Gupta6973a582018-12-13 18:25:44 +053068struct LDAPConfigData
69{
Ed Tanous47f29342024-03-19 12:18:06 -070070 std::string uri;
71 std::string bindDN;
72 std::string baseDN;
73 std::string searchScope;
74 std::string serverType;
Ratan Gupta6973a582018-12-13 18:25:44 +053075 bool serviceEnabled = false;
Ed Tanous47f29342024-03-19 12:18:06 -070076 std::string userNameAttribute;
77 std::string groupAttribute;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060078 std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
Ratan Gupta6973a582018-12-13 18:25:44 +053079};
80
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060081inline std::string getRoleIdFromPrivilege(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053082{
83 if (role == "priv-admin")
84 {
85 return "Administrator";
86 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070087 if (role == "priv-user")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053088 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053089 return "ReadOnly";
AppaRao Puli84e12cb2018-10-11 01:28:15 +053090 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070091 if (role == "priv-operator")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053092 {
93 return "Operator";
94 }
95 return "";
96}
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060097inline std::string getPrivilegeFromRoleId(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053098{
99 if (role == "Administrator")
100 {
101 return "priv-admin";
102 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700103 if (role == "ReadOnly")
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530104 {
105 return "priv-user";
106 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700107 if (role == "Operator")
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530108 {
109 return "priv-operator";
110 }
111 return "";
112}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700113
Abhishek Patelc7229812022-02-01 10:07:15 -0600114/**
115 * @brief Maps user group names retrieved from D-Bus object to
116 * Account Types.
117 *
118 * @param[in] userGroups List of User groups
119 * @param[out] res AccountTypes populated
120 *
121 * @return true in case of success, false if UserGroups contains
122 * invalid group name(s).
123 */
124inline bool translateUserGroup(const std::vector<std::string>& userGroups,
125 crow::Response& res)
126{
127 std::vector<std::string> accountTypes;
128 for (const auto& userGroup : userGroups)
129 {
130 if (userGroup == "redfish")
131 {
132 accountTypes.emplace_back("Redfish");
133 accountTypes.emplace_back("WebUI");
134 }
135 else if (userGroup == "ipmi")
136 {
137 accountTypes.emplace_back("IPMI");
138 }
139 else if (userGroup == "ssh")
140 {
Abhishek Patelc7229812022-02-01 10:07:15 -0600141 accountTypes.emplace_back("ManagerConsole");
142 }
Ninad Palsule3e72c202023-03-27 17:19:55 -0500143 else if (userGroup == "hostconsole")
144 {
145 // The hostconsole group controls who can access the host console
146 // port via ssh and websocket.
147 accountTypes.emplace_back("HostConsole");
148 }
Abhishek Patelc7229812022-02-01 10:07:15 -0600149 else if (userGroup == "web")
150 {
151 // 'web' is one of the valid groups in the UserGroups property of
152 // the user account in the D-Bus object. This group is currently not
153 // doing anything, and is considered to be equivalent to 'redfish'.
154 // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
155 // AccountTypes, so do nothing here...
156 }
157 else
158 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800159 // Invalid user group name. Caller throws an exception.
Abhishek Patelc7229812022-02-01 10:07:15 -0600160 return false;
161 }
162 }
163
164 res.jsonValue["AccountTypes"] = std::move(accountTypes);
165 return true;
166}
167
Abhishek Patel58345852022-02-02 08:54:25 -0600168/**
169 * @brief Builds User Groups from the Account Types
170 *
171 * @param[in] asyncResp Async Response
172 * @param[in] accountTypes List of Account Types
173 * @param[out] userGroups List of User Groups mapped from Account Types
174 *
175 * @return true if Account Types mapped to User Groups, false otherwise.
176 */
177inline bool
178 getUserGroupFromAccountType(crow::Response& res,
179 const std::vector<std::string>& accountTypes,
180 std::vector<std::string>& userGroups)
181{
182 // Need both Redfish and WebUI Account Types to map to 'redfish' User Group
183 bool redfishType = false;
184 bool webUIType = false;
185
186 for (const auto& accountType : accountTypes)
187 {
188 if (accountType == "Redfish")
189 {
190 redfishType = true;
191 }
192 else if (accountType == "WebUI")
193 {
194 webUIType = true;
195 }
196 else if (accountType == "IPMI")
197 {
198 userGroups.emplace_back("ipmi");
199 }
200 else if (accountType == "HostConsole")
201 {
202 userGroups.emplace_back("hostconsole");
203 }
204 else if (accountType == "ManagerConsole")
205 {
206 userGroups.emplace_back("ssh");
207 }
208 else
209 {
210 // Invalid Account Type
211 messages::propertyValueNotInList(res, "AccountTypes", accountType);
212 return false;
213 }
214 }
215
216 // Both Redfish and WebUI Account Types are needed to PATCH
217 if (redfishType ^ webUIType)
218 {
Ed Tanous62598e32023-07-17 17:06:25 -0700219 BMCWEB_LOG_ERROR(
220 "Missing Redfish or WebUI Account Type to set redfish User Group");
Abhishek Patel58345852022-02-02 08:54:25 -0600221 messages::strictAccountTypes(res, "AccountTypes");
222 return false;
223 }
224
225 if (redfishType && webUIType)
226 {
227 userGroups.emplace_back("redfish");
228 }
229
230 return true;
231}
232
233/**
234 * @brief Sets UserGroups property of the user based on the Account Types
235 *
236 * @param[in] accountTypes List of User Account Types
237 * @param[in] asyncResp Async Response
238 * @param[in] dbusObjectPath D-Bus Object Path
239 * @param[in] userSelf true if User is updating OWN Account Types
240 */
241inline void
242 patchAccountTypes(const std::vector<std::string>& accountTypes,
243 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
244 const std::string& dbusObjectPath, bool userSelf)
245{
246 // Check if User is disabling own Redfish Account Type
247 if (userSelf &&
248 (accountTypes.cend() ==
249 std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish")))
250 {
Ed Tanous62598e32023-07-17 17:06:25 -0700251 BMCWEB_LOG_ERROR(
252 "User disabling OWN Redfish Account Type is not allowed");
Abhishek Patel58345852022-02-02 08:54:25 -0600253 messages::strictAccountTypes(asyncResp->res, "AccountTypes");
254 return;
255 }
256
257 std::vector<std::string> updatedUserGroups;
258 if (!getUserGroupFromAccountType(asyncResp->res, accountTypes,
259 updatedUserGroups))
260 {
261 // Problem in mapping Account Types to User Groups, Error already
262 // logged.
263 return;
264 }
Ed Tanousd02aad32024-02-13 14:43:34 -0800265 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
266 dbusObjectPath, "xyz.openbmc_project.User.Attributes",
267 "UserGroups", "AccountTypes", updatedUserGroups);
Abhishek Patel58345852022-02-02 08:54:25 -0600268}
269
zhanghch058d1b46d2021-04-01 11:18:24 +0800270inline void userErrorMessageHandler(
271 const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272 const std::string& newUser, const std::string& username)
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000273{
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000274 if (e == nullptr)
275 {
276 messages::internalError(asyncResp->res);
277 return;
278 }
279
Manojkiran Eda055806b2020-11-03 09:36:28 +0530280 const char* errorMessage = e->name;
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000281 if (strcmp(errorMessage,
282 "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
283 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800284 messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000285 "UserName", newUser);
286 }
287 else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
288 "UserNameDoesNotExist") == 0)
289 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800290 messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000291 }
Ed Tanousd4d25792020-09-29 15:15:03 -0700292 else if ((strcmp(errorMessage,
293 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
294 0) ||
George Liu0fda0f12021-11-16 10:06:17 +0800295 (strcmp(
296 errorMessage,
297 "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
298 0))
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000299 {
300 messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
301 }
302 else if (strcmp(errorMessage,
303 "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
304 {
305 messages::createLimitReachedForResource(asyncResp->res);
306 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000307 else
308 {
Gunnar Millsb8ad5832023-10-02 16:26:07 -0500309 BMCWEB_LOG_ERROR("DBUS response error {}", errorMessage);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000310 messages::internalError(asyncResp->res);
311 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000312}
313
Ed Tanous81ce6092020-12-17 16:54:55 +0000314inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
Ed Tanous23a21a12020-07-25 04:45:05 +0000315 const LDAPConfigData& confData,
316 const std::string& ldapType)
Ratan Gupta6973a582018-12-13 18:25:44 +0530317{
Ed Tanous49cc2632024-03-20 12:49:15 -0700318 nlohmann::json::object_t ldap;
Ed Tanous14766872022-03-15 10:44:42 -0700319 ldap["ServiceEnabled"] = confData.serviceEnabled;
Ed Tanous49cc2632024-03-20 12:49:15 -0700320 nlohmann::json::array_t serviceAddresses;
321 serviceAddresses.emplace_back(confData.uri);
322 ldap["ServiceAddresses"] = std::move(serviceAddresses);
323
324 nlohmann::json::object_t authentication;
325 authentication["AuthenticationType"] =
Ed Tanous0ec8b832022-03-14 14:56:47 -0700326 account_service::AuthenticationTypes::UsernameAndPassword;
Ed Tanous49cc2632024-03-20 12:49:15 -0700327 authentication["Username"] = confData.bindDN;
328 authentication["Password"] = nullptr;
329 ldap["Authentication"] = std::move(authentication);
Ed Tanous14766872022-03-15 10:44:42 -0700330
Ed Tanous49cc2632024-03-20 12:49:15 -0700331 nlohmann::json::object_t ldapService;
332 nlohmann::json::object_t searchSettings;
333 nlohmann::json::array_t baseDistinguishedNames;
334 baseDistinguishedNames.emplace_back(confData.baseDN);
Ed Tanous14766872022-03-15 10:44:42 -0700335
Ed Tanous49cc2632024-03-20 12:49:15 -0700336 searchSettings["BaseDistinguishedNames"] =
337 std::move(baseDistinguishedNames);
338 searchSettings["UsernameAttribute"] = confData.userNameAttribute;
339 searchSettings["GroupsAttribute"] = confData.groupAttribute;
340 ldapService["SearchSettings"] = std::move(searchSettings);
341 ldap["LDAPService"] = std::move(ldapService);
342
343 nlohmann::json::array_t roleMapArray;
Ed Tanous9eb808c2022-01-25 10:19:23 -0800344 for (const auto& obj : confData.groupRoleList)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600345 {
Ed Tanous62598e32023-07-17 17:06:25 -0700346 BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName);
Ed Tanous613dabe2022-07-09 11:17:36 -0700347
Ed Tanous613dabe2022-07-09 11:17:36 -0700348 nlohmann::json::object_t remoteGroup;
349 remoteGroup["RemoteGroup"] = obj.second.groupName;
Jorge Cisneros329f0342022-11-04 16:26:25 +0000350 remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
351 roleMapArray.emplace_back(std::move(remoteGroup));
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600352 }
Ed Tanous49cc2632024-03-20 12:49:15 -0700353
354 ldap["RemoteRoleMapping"] = std::move(roleMapArray);
355
356 jsonResponse[ldapType].update(ldap);
Ratan Gupta6973a582018-12-13 18:25:44 +0530357}
358
359/**
Ratan Gupta06785242019-07-26 22:30:16 +0530360 * @brief validates given JSON input and then calls appropriate method to
361 * create, to delete or to set Rolemapping object based on the given input.
362 *
363 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000364inline void handleRoleMapPatch(
zhanghch058d1b46d2021-04-01 11:18:24 +0800365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ratan Gupta06785242019-07-26 22:30:16 +0530366 const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
Ed Tanousc1019822024-03-06 12:54:38 -0800367 const std::string& serverType,
368 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input)
Ratan Gupta06785242019-07-26 22:30:16 +0530369{
370 for (size_t index = 0; index < input.size(); index++)
371 {
Ed Tanousc1019822024-03-06 12:54:38 -0800372 std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson =
373 input[index];
374 nlohmann::json::object_t* obj =
375 std::get_if<nlohmann::json::object_t>(&thisJson);
376 if (obj == nullptr)
Ratan Gupta06785242019-07-26 22:30:16 +0530377 {
378 // delete the existing object
379 if (index < roleMapObjData.size())
380 {
381 crow::connections::systemBus->async_method_call(
382 [asyncResp, roleMapObjData, serverType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800383 index](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700384 if (ec)
385 {
Ed Tanous62598e32023-07-17 17:06:25 -0700386 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700387 messages::internalError(asyncResp->res);
388 return;
389 }
Patrick Williams89492a12023-05-10 07:51:34 -0500390 asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
391 [index] = nullptr;
Patrick Williams5a39f772023-10-20 11:20:21 -0500392 },
Ratan Gupta06785242019-07-26 22:30:16 +0530393 ldapDbusService, roleMapObjData[index].first,
394 "xyz.openbmc_project.Object.Delete", "Delete");
395 }
396 else
397 {
Ed Tanous62598e32023-07-17 17:06:25 -0700398 BMCWEB_LOG_ERROR("Can't delete the object");
Ed Tanousc1019822024-03-06 12:54:38 -0800399 messages::propertyValueTypeError(asyncResp->res, "null",
Ed Tanous2e8c4bd2022-06-27 12:59:12 -0700400 "RemoteRoleMapping/" +
401 std::to_string(index));
Ratan Gupta06785242019-07-26 22:30:16 +0530402 return;
403 }
404 }
Ed Tanousc1019822024-03-06 12:54:38 -0800405 else if (obj->empty())
Ratan Gupta06785242019-07-26 22:30:16 +0530406 {
407 // Don't do anything for the empty objects,parse next json
408 // eg {"RemoteRoleMapping",[{}]}
409 }
410 else
411 {
412 // update/create the object
413 std::optional<std::string> remoteGroup;
414 std::optional<std::string> localRole;
415
Ed Tanousc1019822024-03-06 12:54:38 -0800416 if (!json_util::readJsonObject(*obj, asyncResp->res, "RemoteGroup",
417 remoteGroup, "LocalRole", localRole))
Ratan Gupta06785242019-07-26 22:30:16 +0530418 {
419 continue;
420 }
421
422 // Update existing RoleMapping Object
423 if (index < roleMapObjData.size())
424 {
Ed Tanous62598e32023-07-17 17:06:25 -0700425 BMCWEB_LOG_DEBUG("Update Role Map Object");
Ratan Gupta06785242019-07-26 22:30:16 +0530426 // If "RemoteGroup" info is provided
427 if (remoteGroup)
428 {
Ed Tanousd02aad32024-02-13 14:43:34 -0800429 setDbusProperty(
430 asyncResp, ldapDbusService, roleMapObjData[index].first,
George Liu9ae226f2023-06-21 17:56:46 +0800431 "xyz.openbmc_project.User.PrivilegeMapperEntry",
Ed Tanousd02aad32024-02-13 14:43:34 -0800432 "GroupName",
433 std::format("RemoteRoleMapping/{}/RemoteGroup", index),
434 *remoteGroup);
Ratan Gupta06785242019-07-26 22:30:16 +0530435 }
436
437 // If "LocalRole" info is provided
438 if (localRole)
439 {
Ed Tanousd02aad32024-02-13 14:43:34 -0800440 setDbusProperty(
441 asyncResp, ldapDbusService, roleMapObjData[index].first,
George Liu9ae226f2023-06-21 17:56:46 +0800442 "xyz.openbmc_project.User.PrivilegeMapperEntry",
Ed Tanousd02aad32024-02-13 14:43:34 -0800443 "Privilege",
444 std::format("RemoteRoleMapping/{}/LocalRole", index),
445 *localRole);
Ratan Gupta06785242019-07-26 22:30:16 +0530446 }
447 }
448 // Create a new RoleMapping Object.
449 else
450 {
Ed Tanous62598e32023-07-17 17:06:25 -0700451 BMCWEB_LOG_DEBUG(
452 "setRoleMappingProperties: Creating new Object");
Patrick Williams89492a12023-05-10 07:51:34 -0500453 std::string pathString = "RemoteRoleMapping/" +
454 std::to_string(index);
Ratan Gupta06785242019-07-26 22:30:16 +0530455
456 if (!localRole)
457 {
458 messages::propertyMissing(asyncResp->res,
459 pathString + "/LocalRole");
460 continue;
461 }
462 if (!remoteGroup)
463 {
464 messages::propertyMissing(asyncResp->res,
465 pathString + "/RemoteGroup");
466 continue;
467 }
468
469 std::string dbusObjectPath;
470 if (serverType == "ActiveDirectory")
471 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700472 dbusObjectPath = adConfigObject;
Ratan Gupta06785242019-07-26 22:30:16 +0530473 }
474 else if (serverType == "LDAP")
475 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000476 dbusObjectPath = ldapConfigObjectName;
Ratan Gupta06785242019-07-26 22:30:16 +0530477 }
478
Ed Tanous62598e32023-07-17 17:06:25 -0700479 BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup,
480 *localRole);
Ratan Gupta06785242019-07-26 22:30:16 +0530481
482 crow::connections::systemBus->async_method_call(
Ed Tanous271584a2019-07-09 16:24:22 -0700483 [asyncResp, serverType, localRole,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800484 remoteGroup](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700485 if (ec)
486 {
Ed Tanous62598e32023-07-17 17:06:25 -0700487 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700488 messages::internalError(asyncResp->res);
489 return;
490 }
491 nlohmann::json& remoteRoleJson =
492 asyncResp->res
493 .jsonValue[serverType]["RemoteRoleMapping"];
494 nlohmann::json::object_t roleMapEntry;
495 roleMapEntry["LocalRole"] = *localRole;
496 roleMapEntry["RemoteGroup"] = *remoteGroup;
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500497 remoteRoleJson.emplace_back(std::move(roleMapEntry));
Patrick Williams5a39f772023-10-20 11:20:21 -0500498 },
Ratan Gupta06785242019-07-26 22:30:16 +0530499 ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
Ed Tanous3174e4d2020-10-07 11:41:22 -0700500 "Create", *remoteGroup,
Ratan Gupta06785242019-07-26 22:30:16 +0530501 getPrivilegeFromRoleId(std::move(*localRole)));
502 }
503 }
504 }
505}
506
507/**
Ratan Gupta6973a582018-12-13 18:25:44 +0530508 * Function that retrieves all properties for LDAP config object
509 * into JSON
510 */
511template <typename CallbackFunc>
512inline void getLDAPConfigData(const std::string& ldapType,
513 CallbackFunc&& callback)
514{
George Liu2b731192023-01-11 16:27:13 +0800515 constexpr std::array<std::string_view, 2> interfaces = {
516 ldapEnableInterface, ldapConfigInterface};
Ratan Gupta6973a582018-12-13 18:25:44 +0530517
George Liu2b731192023-01-11 16:27:13 +0800518 dbus::utility::getDbusObject(
519 ldapConfigObjectName, interfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700520 [callback = std::forward<CallbackFunc>(callback),
Ed Tanousc1019822024-03-06 12:54:38 -0800521 ldapType](const boost::system::error_code& ec,
522 const dbus::utility::MapperGetObject& resp) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 if (ec || resp.empty())
524 {
Carson Labradobf2dded2023-08-10 00:37:06 +0000525 BMCWEB_LOG_WARNING(
Ed Tanous62598e32023-07-17 17:06:25 -0700526 "DBUS response error during getting of service name: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700527 LDAPConfigData empty{};
528 callback(false, empty, ldapType);
529 return;
530 }
531 std::string service = resp.begin()->first;
George Liu5eb468d2023-06-20 17:03:24 +0800532 sdbusplus::message::object_path path(ldapRootObject);
533 dbus::utility::getManagedObjects(
534 service, path,
Ed Tanousc1019822024-03-06 12:54:38 -0800535 [callback, ldapType](
536 const boost::system::error_code& ec2,
537 const dbus::utility::ManagedObjectType& ldapObjects) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700538 LDAPConfigData confData{};
Ed Tanous8b242752023-06-27 17:17:13 -0700539 if (ec2)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600540 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700541 callback(false, confData, ldapType);
Carson Labradobf2dded2023-08-10 00:37:06 +0000542 BMCWEB_LOG_WARNING("D-Bus responses error: {}", ec2);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600543 return;
544 }
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600545
Ed Tanous002d39b2022-05-31 08:59:27 -0700546 std::string ldapDbusType;
547 std::string searchString;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600548
Ed Tanous002d39b2022-05-31 08:59:27 -0700549 if (ldapType == "LDAP")
550 {
551 ldapDbusType =
552 "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
553 searchString = "openldap";
554 }
555 else if (ldapType == "ActiveDirectory")
556 {
557 ldapDbusType =
558 "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
559 searchString = "active_directory";
560 }
561 else
562 {
Ed Tanous62598e32023-07-17 17:06:25 -0700563 BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}",
564 ldapType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 callback(false, confData, ldapType);
566 return;
567 }
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600568
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 std::string ldapEnableInterfaceStr = ldapEnableInterface;
570 std::string ldapConfigInterfaceStr = ldapConfigInterface;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600571
Ed Tanous002d39b2022-05-31 08:59:27 -0700572 for (const auto& object : ldapObjects)
573 {
574 // let's find the object whose ldap type is equal to the
575 // given type
576 if (object.first.str.find(searchString) == std::string::npos)
577 {
578 continue;
579 }
580
581 for (const auto& interface : object.second)
582 {
583 if (interface.first == ldapEnableInterfaceStr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600584 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700585 // rest of the properties are string.
586 for (const auto& property : interface.second)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600587 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700588 if (property.first == "Enabled")
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600589 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 const bool* value =
591 std::get_if<bool>(&property.second);
592 if (value == nullptr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600593 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700594 continue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600595 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700596 confData.serviceEnabled = *value;
597 break;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600598 }
599 }
600 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700601 else if (interface.first == ldapConfigInterfaceStr)
602 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700603 for (const auto& property : interface.second)
604 {
605 const std::string* strValue =
606 std::get_if<std::string>(&property.second);
607 if (strValue == nullptr)
608 {
609 continue;
610 }
611 if (property.first == "LDAPServerURI")
612 {
613 confData.uri = *strValue;
614 }
615 else if (property.first == "LDAPBindDN")
616 {
617 confData.bindDN = *strValue;
618 }
619 else if (property.first == "LDAPBaseDN")
620 {
621 confData.baseDN = *strValue;
622 }
623 else if (property.first == "LDAPSearchScope")
624 {
625 confData.searchScope = *strValue;
626 }
627 else if (property.first == "GroupNameAttribute")
628 {
629 confData.groupAttribute = *strValue;
630 }
631 else if (property.first == "UserNameAttribute")
632 {
633 confData.userNameAttribute = *strValue;
634 }
635 else if (property.first == "LDAPType")
636 {
637 confData.serverType = *strValue;
638 }
639 }
640 }
641 else if (interface.first ==
642 "xyz.openbmc_project.User.PrivilegeMapperEntry")
643 {
644 LDAPRoleMapData roleMapData{};
645 for (const auto& property : interface.second)
646 {
647 const std::string* strValue =
648 std::get_if<std::string>(&property.second);
649
650 if (strValue == nullptr)
651 {
652 continue;
653 }
654
655 if (property.first == "GroupName")
656 {
657 roleMapData.groupName = *strValue;
658 }
659 else if (property.first == "Privilege")
660 {
661 roleMapData.privilege = *strValue;
662 }
663 }
664
665 confData.groupRoleList.emplace_back(object.first.str,
666 roleMapData);
667 }
668 }
669 }
670 callback(true, confData, ldapType);
George Liu2b731192023-01-11 16:27:13 +0800671 });
Patrick Williams5a39f772023-10-20 11:20:21 -0500672 });
Ratan Gupta6973a582018-12-13 18:25:44 +0530673}
674
Ed Tanous6c51eab2021-06-03 12:30:29 -0700675/**
Ed Tanous6c51eab2021-06-03 12:30:29 -0700676 * @brief updates the LDAP server address and updates the
677 json response with the new value.
678 * @param serviceAddressList address to be updated.
679 * @param asyncResp pointer to the JSON response
680 * @param ldapServerElementName Type of LDAP
681 server(openLDAP/ActiveDirectory)
682 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530683
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700684inline void handleServiceAddressPatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700685 const std::vector<std::string>& serviceAddressList,
686 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
687 const std::string& ldapServerElementName,
688 const std::string& ldapConfigObject)
689{
Ed Tanousd02aad32024-02-13 14:43:34 -0800690 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
691 ldapConfigInterface, "LDAPServerURI",
692 ldapServerElementName + "/ServiceAddress",
693 serviceAddressList.front());
Ed Tanous6c51eab2021-06-03 12:30:29 -0700694}
695/**
696 * @brief updates the LDAP Bind DN and updates the
697 json response with the new value.
698 * @param username name of the user which needs to be updated.
699 * @param asyncResp pointer to the JSON response
700 * @param ldapServerElementName Type of LDAP
701 server(openLDAP/ActiveDirectory)
702 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530703
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700704inline void
705 handleUserNamePatch(const std::string& username,
706 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
707 const std::string& ldapServerElementName,
708 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700709{
Ed Tanousd02aad32024-02-13 14:43:34 -0800710 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
711 ldapConfigInterface, "LDAPBindDN",
712 ldapServerElementName + "/Authentication/Username",
713 username);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700714}
715
716/**
717 * @brief updates the LDAP password
718 * @param password : ldap password which needs to be updated.
719 * @param asyncResp pointer to the JSON response
720 * @param ldapServerElementName Type of LDAP
721 * server(openLDAP/ActiveDirectory)
722 */
723
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700724inline void
725 handlePasswordPatch(const std::string& password,
726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
727 const std::string& ldapServerElementName,
728 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700729{
Ed Tanousd02aad32024-02-13 14:43:34 -0800730 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
731 ldapConfigInterface, "LDAPBindDNPassword",
732 ldapServerElementName + "/Authentication/Password",
733 password);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700734}
735
736/**
737 * @brief updates the LDAP BaseDN and updates the
738 json response with the new value.
739 * @param baseDNList baseDN list which needs to be updated.
740 * @param asyncResp pointer to the JSON response
741 * @param ldapServerElementName Type of LDAP
742 server(openLDAP/ActiveDirectory)
743 */
744
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700745inline void
746 handleBaseDNPatch(const std::vector<std::string>& baseDNList,
747 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
748 const std::string& ldapServerElementName,
749 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700750{
Ed Tanousd02aad32024-02-13 14:43:34 -0800751 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
752 ldapConfigInterface, "LDAPBaseDN",
753 ldapServerElementName +
754 "/LDAPService/SearchSettings/BaseDistinguishedNames",
755 baseDNList.front());
Ed Tanous6c51eab2021-06-03 12:30:29 -0700756}
757/**
758 * @brief updates the LDAP user name attribute and updates the
759 json response with the new value.
760 * @param userNameAttribute attribute to be updated.
761 * @param asyncResp pointer to the JSON response
762 * @param ldapServerElementName Type of LDAP
763 server(openLDAP/ActiveDirectory)
764 */
765
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700766inline void
767 handleUserNameAttrPatch(const std::string& userNameAttribute,
768 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
769 const std::string& ldapServerElementName,
770 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700771{
Ed Tanousd02aad32024-02-13 14:43:34 -0800772 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
773 ldapConfigInterface, "UserNameAttribute",
774 ldapServerElementName +
775 "LDAPService/SearchSettings/UsernameAttribute",
776 userNameAttribute);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700777}
778/**
779 * @brief updates the LDAP group attribute and updates the
780 json response with the new value.
781 * @param groupsAttribute attribute to be updated.
782 * @param asyncResp pointer to the JSON response
783 * @param ldapServerElementName Type of LDAP
784 server(openLDAP/ActiveDirectory)
785 */
786
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700787inline void handleGroupNameAttrPatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700788 const std::string& groupsAttribute,
789 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
790 const std::string& ldapServerElementName,
791 const std::string& ldapConfigObject)
792{
Ed Tanousd02aad32024-02-13 14:43:34 -0800793 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
794 ldapConfigInterface, "GroupNameAttribute",
795 ldapServerElementName +
796 "/LDAPService/SearchSettings/GroupsAttribute",
797 groupsAttribute);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700798}
799/**
800 * @brief updates the LDAP service enable and updates the
801 json response with the new value.
802 * @param input JSON data.
803 * @param asyncResp pointer to the JSON response
804 * @param ldapServerElementName Type of LDAP
805 server(openLDAP/ActiveDirectory)
806 */
807
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700808inline void handleServiceEnablePatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700809 bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
810 const std::string& ldapServerElementName,
811 const std::string& ldapConfigObject)
812{
Ed Tanousd02aad32024-02-13 14:43:34 -0800813 setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
814 ldapEnableInterface, "Enabled",
815 ldapServerElementName + "/ServiceEnabled", serviceEnabled);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700816}
817
Ed Tanousc1019822024-03-06 12:54:38 -0800818struct AuthMethods
Ed Tanous6c51eab2021-06-03 12:30:29 -0700819{
820 std::optional<bool> basicAuth;
821 std::optional<bool> cookie;
822 std::optional<bool> sessionToken;
823 std::optional<bool> xToken;
824 std::optional<bool> tls;
Ed Tanousc1019822024-03-06 12:54:38 -0800825};
Ed Tanous6c51eab2021-06-03 12:30:29 -0700826
Ed Tanousc1019822024-03-06 12:54:38 -0800827inline void
828 handleAuthMethodsPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
829 const AuthMethods& auth)
830{
831 persistent_data::AuthConfigMethods& authMethodsConfig =
Ed Tanous6c51eab2021-06-03 12:30:29 -0700832 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
833
Ed Tanousc1019822024-03-06 12:54:38 -0800834 if (auth.basicAuth)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700835 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700836 if constexpr (!BMCWEB_BASIC_AUTH)
837 {
838 messages::actionNotSupported(
839 asyncResp->res,
840 "Setting BasicAuth when basic-auth feature is disabled");
841 return;
842 }
843
Ed Tanousc1019822024-03-06 12:54:38 -0800844 authMethodsConfig.basic = *auth.basicAuth;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700845 }
846
Ed Tanousc1019822024-03-06 12:54:38 -0800847 if (auth.cookie)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700848 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700849 if constexpr (!BMCWEB_COOKIE_AUTH)
850 {
851 messages::actionNotSupported(
852 asyncResp->res,
853 "Setting Cookie when cookie-auth feature is disabled");
854 return;
855 }
Ed Tanousc1019822024-03-06 12:54:38 -0800856 authMethodsConfig.cookie = *auth.cookie;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700857 }
858
Ed Tanousc1019822024-03-06 12:54:38 -0800859 if (auth.sessionToken)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700860 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700861 if constexpr (!BMCWEB_SESSION_AUTH)
862 {
863 messages::actionNotSupported(
864 asyncResp->res,
865 "Setting SessionToken when session-auth feature is disabled");
866 return;
867 }
Ed Tanousc1019822024-03-06 12:54:38 -0800868 authMethodsConfig.sessionToken = *auth.sessionToken;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700869 }
870
Ed Tanousc1019822024-03-06 12:54:38 -0800871 if (auth.xToken)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700872 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700873 if constexpr (!BMCWEB_XTOKEN_AUTH)
874 {
875 messages::actionNotSupported(
876 asyncResp->res,
877 "Setting XToken when xtoken-auth feature is disabled");
878 return;
879 }
Ed Tanousc1019822024-03-06 12:54:38 -0800880 authMethodsConfig.xtoken = *auth.xToken;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700881 }
882
Ed Tanousc1019822024-03-06 12:54:38 -0800883 if (auth.tls)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700884 {
Ed Tanous25b54db2024-04-17 15:40:31 -0700885 if constexpr (!BMCWEB_MUTUAL_TLS_AUTH)
886 {
887 messages::actionNotSupported(
888 asyncResp->res,
889 "Setting TLS when mutual-tls-auth feature is disabled");
890 return;
891 }
Ed Tanousc1019822024-03-06 12:54:38 -0800892 authMethodsConfig.tls = *auth.tls;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700893 }
894
895 if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
896 !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
897 !authMethodsConfig.tls)
898 {
899 // Do not allow user to disable everything
900 messages::actionNotSupported(asyncResp->res,
901 "of disabling all available methods");
902 return;
903 }
904
905 persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
906 authMethodsConfig);
907 // Save configuration immediately
908 persistent_data::getConfig().writeData();
909
910 messages::success(asyncResp->res);
911}
912
913/**
914 * @brief Get the required values from the given JSON, validates the
915 * value and create the LDAP config object.
916 * @param input JSON data
917 * @param asyncResp pointer to the JSON response
918 * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
919 */
920
Ed Tanous10cb44f2024-04-11 13:05:20 -0700921struct LdapPatchParams
922{
923 std::optional<std::string> authType;
924 std::optional<std::vector<std::string>> serviceAddressList;
925 std::optional<bool> serviceEnabled;
926 std::optional<std::vector<std::string>> baseDNList;
927 std::optional<std::string> userNameAttribute;
928 std::optional<std::string> groupsAttribute;
929 std::optional<std::string> userName;
930 std::optional<std::string> password;
931 std::optional<
932 std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
933 remoteRoleMapData;
934};
935
936inline void handleLDAPPatch(LdapPatchParams&& input,
Ed Tanous6c51eab2021-06-03 12:30:29 -0700937 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
938 const std::string& serverType)
939{
940 std::string dbusObjectPath;
941 if (serverType == "ActiveDirectory")
942 {
943 dbusObjectPath = adConfigObject;
944 }
945 else if (serverType == "LDAP")
946 {
947 dbusObjectPath = ldapConfigObjectName;
948 }
949 else
950 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700951 BMCWEB_LOG_ERROR("serverType wasn't AD or LDAP but was {}????",
952 serverType);
Ed Tanous6c51eab2021-06-03 12:30:29 -0700953 return;
954 }
955
Ed Tanous10cb44f2024-04-11 13:05:20 -0700956 if (input.authType && *input.authType != "UsernameAndPassword")
Ed Tanous6c51eab2021-06-03 12:30:29 -0700957 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700958 messages::propertyValueNotInList(asyncResp->res, *input.authType,
Ed Tanousc1019822024-03-06 12:54:38 -0800959 "AuthenticationType");
960 return;
Ed Tanous6c51eab2021-06-03 12:30:29 -0700961 }
Ed Tanousc1019822024-03-06 12:54:38 -0800962
Ed Tanous10cb44f2024-04-11 13:05:20 -0700963 if (input.serviceAddressList)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700964 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700965 if (input.serviceAddressList->empty())
Ratan Guptaeb2bbe52019-04-22 14:27:01 +0530966 {
Ed Tanouse2616cc2022-06-27 12:45:55 -0700967 messages::propertyValueNotInList(
Ed Tanous10cb44f2024-04-11 13:05:20 -0700968 asyncResp->res, *input.serviceAddressList, "ServiceAddress");
Ed Tanouscb13a392020-07-25 19:02:03 +0000969 return;
970 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700971 }
Ed Tanous10cb44f2024-04-11 13:05:20 -0700972 if (input.baseDNList)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700973 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700974 if (input.baseDNList->empty())
Ratan Gupta8a07d282019-03-16 08:33:47 +0530975 {
Ed Tanous10cb44f2024-04-11 13:05:20 -0700976 messages::propertyValueNotInList(asyncResp->res, *input.baseDNList,
Ed Tanous6c51eab2021-06-03 12:30:29 -0700977 "BaseDistinguishedNames");
Ratan Gupta8a07d282019-03-16 08:33:47 +0530978 return;
979 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700980 }
Ratan Gupta8a07d282019-03-16 08:33:47 +0530981
Ed Tanous6c51eab2021-06-03 12:30:29 -0700982 // nothing to update, then return
Ed Tanous10cb44f2024-04-11 13:05:20 -0700983 if (!input.userName && !input.password && !input.serviceAddressList &&
984 !input.baseDNList && !input.userNameAttribute &&
985 !input.groupsAttribute && !input.serviceEnabled &&
986 !input.remoteRoleMapData)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700987 {
988 return;
989 }
990
991 // Get the existing resource first then keep modifying
992 // whenever any property gets updated.
Ed Tanous10cb44f2024-04-11 13:05:20 -0700993 getLDAPConfigData(serverType,
994 [asyncResp, input = std::move(input),
995 dbusObjectPath = std::move(dbusObjectPath)](
996 bool success, const LDAPConfigData& confData,
997 const std::string& serverT) mutable {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700998 if (!success)
Ratan Gupta8a07d282019-03-16 08:33:47 +0530999 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001000 messages::internalError(asyncResp->res);
1001 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +05301002 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001003 parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
1004 if (confData.serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301005 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001006 // Disable the service first and update the rest of
1007 // the properties.
1008 handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301009 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001010
Ed Tanous10cb44f2024-04-11 13:05:20 -07001011 if (input.serviceAddressList)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301012 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001013 handleServiceAddressPatch(*input.serviceAddressList, asyncResp,
1014 serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301015 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001016 if (input.userName)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001017 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001018 handleUserNamePatch(*input.userName, asyncResp, serverT,
1019 dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001020 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001021 if (input.password)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001022 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001023 handlePasswordPatch(*input.password, asyncResp, serverT,
1024 dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001025 }
1026
Ed Tanous10cb44f2024-04-11 13:05:20 -07001027 if (input.baseDNList)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301028 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001029 handleBaseDNPatch(*input.baseDNList, asyncResp, serverT,
1030 dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001031 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001032 if (input.userNameAttribute)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001033 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001034 handleUserNameAttrPatch(*input.userNameAttribute, asyncResp,
1035 serverT, dbusObjectPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001036 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001037 if (input.groupsAttribute)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001038 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001039 handleGroupNameAttrPatch(*input.groupsAttribute, asyncResp, serverT,
Ed Tanous6c51eab2021-06-03 12:30:29 -07001040 dbusObjectPath);
1041 }
Ed Tanous10cb44f2024-04-11 13:05:20 -07001042 if (input.serviceEnabled)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001043 {
1044 // if user has given the value as true then enable
1045 // the service. if user has given false then no-op
1046 // as service is already stopped.
Ed Tanous10cb44f2024-04-11 13:05:20 -07001047 if (*input.serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301048 {
Ed Tanous10cb44f2024-04-11 13:05:20 -07001049 handleServiceEnablePatch(*input.serviceEnabled, asyncResp,
1050 serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301051 }
1052 }
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001053 else
1054 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001055 // if user has not given the service enabled value
1056 // then revert it to the same state as it was
1057 // before.
1058 handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
1059 serverT, dbusObjectPath);
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001060 }
Ed Tanous04ae99e2018-09-20 15:54:36 -07001061
Ed Tanous10cb44f2024-04-11 13:05:20 -07001062 if (input.remoteRoleMapData)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001063 {
1064 handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
Ed Tanous10cb44f2024-04-11 13:05:20 -07001065 *input.remoteRoleMapData);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001066 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001067 });
Ed Tanous6c51eab2021-06-03 12:30:29 -07001068}
1069
Abhishek Patel58345852022-02-02 08:54:25 -06001070inline void updateUserProperties(
1071 std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1072 const std::optional<std::string>& password,
1073 const std::optional<bool>& enabled,
1074 const std::optional<std::string>& roleId, const std::optional<bool>& locked,
Ravi Tejae518ef32024-05-16 10:33:08 -05001075 std::optional<std::vector<std::string>> accountTypes, bool userSelf,
1076 const std::shared_ptr<persistent_data::UserSession>& session)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001077{
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301078 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1079 tempObjPath /= username;
1080 std::string dbusObjectPath(tempObjPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001081
1082 dbus::utility::checkDbusPathExists(
Ravi Tejae518ef32024-05-16 10:33:08 -05001083 dbusObjectPath,
1084 [dbusObjectPath, username, password, roleId, enabled, locked,
1085 accountTypes(std::move(accountTypes)), userSelf, session,
1086 asyncResp{std::move(asyncResp)}](int rc) {
Patrick Williams5a39f772023-10-20 11:20:21 -05001087 if (rc <= 0)
1088 {
1089 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1090 username);
1091 return;
1092 }
1093
1094 if (password)
1095 {
1096 int retval = pamUpdatePassword(username, *password);
1097
1098 if (retval == PAM_USER_UNKNOWN)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001099 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001100 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1101 username);
Patrick Williams5a39f772023-10-20 11:20:21 -05001102 }
1103 else if (retval == PAM_AUTHTOK_ERR)
1104 {
1105 // If password is invalid
1106 messages::propertyValueFormatError(asyncResp->res, nullptr,
1107 "Password");
1108 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
1109 }
1110 else if (retval != PAM_SUCCESS)
1111 {
1112 messages::internalError(asyncResp->res);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001113 return;
1114 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001115 else
Ed Tanous618c14b2022-06-30 17:44:25 -07001116 {
Ravi Tejae518ef32024-05-16 10:33:08 -05001117 // Remove existing sessions of the user when password changed
1118 persistent_data::SessionStore::getInstance()
1119 .removeSessionsByUsernameExceptSession(username, session);
Patrick Williams5a39f772023-10-20 11:20:21 -05001120 messages::success(asyncResp->res);
Ed Tanous618c14b2022-06-30 17:44:25 -07001121 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001122 }
Ed Tanous618c14b2022-06-30 17:44:25 -07001123
Patrick Williams5a39f772023-10-20 11:20:21 -05001124 if (enabled)
1125 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001126 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1127 dbusObjectPath,
1128 "xyz.openbmc_project.User.Attributes",
1129 "UserEnabled", "Enabled", *enabled);
Patrick Williams5a39f772023-10-20 11:20:21 -05001130 }
1131
1132 if (roleId)
1133 {
1134 std::string priv = getPrivilegeFromRoleId(*roleId);
1135 if (priv.empty())
1136 {
1137 messages::propertyValueNotInList(asyncResp->res, true,
1138 "Locked");
1139 return;
Ed Tanous618c14b2022-06-30 17:44:25 -07001140 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001141 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1142 dbusObjectPath,
1143 "xyz.openbmc_project.User.Attributes",
1144 "UserPrivilege", "RoleId", priv);
Patrick Williams5a39f772023-10-20 11:20:21 -05001145 }
1146
1147 if (locked)
1148 {
1149 // admin can unlock the account which is locked by
1150 // successive authentication failures but admin should
1151 // not be allowed to lock an account.
1152 if (*locked)
Abhishek Patel58345852022-02-02 08:54:25 -06001153 {
Patrick Williams5a39f772023-10-20 11:20:21 -05001154 messages::propertyValueNotInList(asyncResp->res, "true",
1155 "Locked");
1156 return;
Abhishek Patel58345852022-02-02 08:54:25 -06001157 }
Ed Tanousd02aad32024-02-13 14:43:34 -08001158 setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1159 dbusObjectPath,
1160 "xyz.openbmc_project.User.Attributes",
1161 "UserLockedForFailedAttempt", "Locked", *locked);
Patrick Williams5a39f772023-10-20 11:20:21 -05001162 }
1163
1164 if (accountTypes)
1165 {
1166 patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
1167 userSelf);
1168 }
1169 });
Ed Tanous6c51eab2021-06-03 12:30:29 -07001170}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001171
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001172inline void handleAccountServiceHead(
1173 App& app, const crow::Request& req,
1174 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001175{
1176 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1177 {
1178 return;
1179 }
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001180 asyncResp->res.addHeader(
1181 boost::beast::http::field::link,
1182 "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1183}
1184
1185inline void
Ed Tanous1aa375b2024-04-13 11:51:10 -07001186 getClientCertificates(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1187 const nlohmann::json::json_pointer& keyLocation)
1188{
1189 boost::urls::url url(
1190 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates");
1191 std::array<std::string_view, 1> interfaces = {
1192 "xyz.openbmc_project.Certs.Certificate"};
1193 std::string path = "/xyz/openbmc_project/certs/authority/truststore";
1194
1195 collection_util::getCollectionToKey(asyncResp, url, interfaces, path,
1196 keyLocation);
1197}
1198
1199inline void handleAccountServiceClientCertificatesInstanceHead(
1200 App& app, const crow::Request& req,
1201 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1202 const std::string& /*id*/)
1203{
1204 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1205 {
1206 return;
1207 }
1208
1209 asyncResp->res.addHeader(
1210 boost::beast::http::field::link,
1211 "</redfish/v1/JsonSchemas/Certificate/Certificate.json>; rel=describedby");
1212}
1213
1214inline void handleAccountServiceClientCertificatesInstanceGet(
1215 App& app, const crow::Request& req,
1216 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
1217{
1218 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1219 {
1220 return;
1221 }
1222 BMCWEB_LOG_DEBUG("ClientCertificate Certificate ID={}", id);
1223 const boost::urls::url certURL = boost::urls::format(
1224 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates/{}",
1225 id);
1226 std::string objPath =
1227 sdbusplus::message::object_path(certs::authorityObjectPath) / id;
1228 getCertificateProperties(
1229 asyncResp, objPath,
1230 "xyz.openbmc_project.Certs.Manager.Authority.Truststore", id, certURL,
1231 "Client Certificate");
1232}
1233
1234inline void handleAccountServiceClientCertificatesHead(
1235 App& app, const crow::Request& req,
1236 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1237{
1238 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1239 {
1240 return;
1241 }
1242
1243 asyncResp->res.addHeader(
1244 boost::beast::http::field::link,
1245 "</redfish/v1/JsonSchemas/CertificateCollection/CertificateCollection.json>; rel=describedby");
1246}
1247
1248inline void handleAccountServiceClientCertificatesGet(
1249 App& app, const crow::Request& req,
1250 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1251{
1252 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1253 {
1254 return;
1255 }
1256 getClientCertificates(asyncResp, "/Members"_json_pointer);
1257}
1258
1259inline void
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001260 handleAccountServiceGet(App& app, const crow::Request& req,
1261 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1262{
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001263 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1264 {
1265 return;
1266 }
Ninad Palsule3e72c202023-03-27 17:19:55 -05001267
1268 if (req.session == nullptr)
1269 {
1270 messages::internalError(asyncResp->res);
1271 return;
1272 }
1273
Ed Tanousc1019822024-03-06 12:54:38 -08001274 const persistent_data::AuthConfigMethods& authMethodsConfig =
1275 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
1276
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001277 asyncResp->res.addHeader(
1278 boost::beast::http::field::link,
1279 "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1280
Ed Tanous1ef4c342022-05-12 16:12:36 -07001281 nlohmann::json& json = asyncResp->res.jsonValue;
1282 json["@odata.id"] = "/redfish/v1/AccountService";
Ravi Teja482a69e2024-04-22 06:56:13 -05001283 json["@odata.type"] = "#AccountService.v1_15_0.AccountService";
Ed Tanous1ef4c342022-05-12 16:12:36 -07001284 json["Id"] = "AccountService";
1285 json["Name"] = "Account Service";
1286 json["Description"] = "Account Service";
1287 json["ServiceEnabled"] = true;
1288 json["MaxPasswordLength"] = 20;
1289 json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
1290 json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
Ravi Teja482a69e2024-04-22 06:56:13 -05001291 json["HTTPBasicAuth"] = authMethodsConfig.basic
1292 ? account_service::BasicAuthState::Enabled
1293 : account_service::BasicAuthState::Disabled;
1294
1295 nlohmann::json::array_t allowed;
1296 allowed.emplace_back(account_service::BasicAuthState::Enabled);
1297 allowed.emplace_back(account_service::BasicAuthState::Disabled);
1298 json["HTTPBasicAuth@AllowableValues"] = std::move(allowed);
1299
Ed Tanous1aa375b2024-04-13 11:51:10 -07001300 nlohmann::json::object_t clientCertificate;
1301 clientCertificate["Enabled"] = authMethodsConfig.tls;
1302 clientCertificate["RespondToUnauthenticatedClients"] = true;
1303 clientCertificate["CertificateMappingAttribute"] =
1304 account_service::CertificateMappingAttribute::CommonName;
1305 nlohmann::json::object_t certificates;
1306 certificates["@odata.id"] =
1307 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates";
1308 certificates["@odata.type"] =
1309 "#CertificateCollection.CertificateCollection";
1310 clientCertificate["Certificates"] = std::move(certificates);
1311 json["MultiFactorAuth"]["ClientCertificate"] = std::move(clientCertificate);
1312
1313 getClientCertificates(
1314 asyncResp,
1315 "/MultiFactorAuth/ClientCertificate/Certificates/Members"_json_pointer);
1316
Ed Tanous1ef4c342022-05-12 16:12:36 -07001317 json["Oem"]["OpenBMC"]["@odata.type"] =
Ed Tanous5b5574a2022-09-26 19:53:36 -07001318 "#OpenBMCAccountService.v1_0_0.AccountService";
Ed Tanous1ef4c342022-05-12 16:12:36 -07001319 json["Oem"]["OpenBMC"]["@odata.id"] =
1320 "/redfish/v1/AccountService#/Oem/OpenBMC";
1321 json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
1322 authMethodsConfig.basic;
1323 json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
1324 authMethodsConfig.sessionToken;
1325 json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
1326 json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
1327 json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
1328
1329 // /redfish/v1/AccountService/LDAP/Certificates is something only
1330 // ConfigureManager can access then only display when the user has
1331 // permissions ConfigureManager
1332 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001333 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001334
1335 if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
1336 effectiveUserPrivileges))
1337 {
1338 asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
1339 "/redfish/v1/AccountService/LDAP/Certificates";
1340 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001341 sdbusplus::asio::getAllProperties(
1342 *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1343 "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001344 [asyncResp](const boost::system::error_code& ec,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001345 const dbus::utility::DBusPropertiesMap& propertiesList) {
1346 if (ec)
1347 {
1348 messages::internalError(asyncResp->res);
1349 return;
1350 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001351
Ed Tanous1aa375b2024-04-13 11:51:10 -07001352 BMCWEB_LOG_DEBUG("Got {} properties for AccountService",
Ed Tanous62598e32023-07-17 17:06:25 -07001353 propertiesList.size());
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001354
1355 const uint8_t* minPasswordLength = nullptr;
1356 const uint32_t* accountUnlockTimeout = nullptr;
1357 const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1358
1359 const bool success = sdbusplus::unpackPropertiesNoThrow(
1360 dbus_utils::UnpackErrorPrinter(), propertiesList,
1361 "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1362 accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1363 maxLoginAttemptBeforeLockout);
1364
1365 if (!success)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001366 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001367 messages::internalError(asyncResp->res);
1368 return;
Ed Tanous1ef4c342022-05-12 16:12:36 -07001369 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001370
1371 if (minPasswordLength != nullptr)
1372 {
1373 asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
1374 }
1375
1376 if (accountUnlockTimeout != nullptr)
1377 {
1378 asyncResp->res.jsonValue["AccountLockoutDuration"] =
1379 *accountUnlockTimeout;
1380 }
1381
1382 if (maxLoginAttemptBeforeLockout != nullptr)
1383 {
1384 asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1385 *maxLoginAttemptBeforeLockout;
1386 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001387 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001388
Ed Tanous02cad962022-06-30 16:50:15 -07001389 auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001390 const std::string& ldapType) {
1391 if (!success)
1392 {
1393 return;
1394 }
1395 parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1396 };
1397
1398 getLDAPConfigData("LDAP", callback);
1399 getLDAPConfigData("ActiveDirectory", callback);
1400}
1401
1402inline void handleAccountServicePatch(
1403 App& app, const crow::Request& req,
1404 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1405{
1406 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1407 {
1408 return;
1409 }
1410 std::optional<uint32_t> unlockTimeout;
1411 std::optional<uint16_t> lockoutThreshold;
1412 std::optional<uint8_t> minPasswordLength;
1413 std::optional<uint16_t> maxPasswordLength;
Ed Tanous10cb44f2024-04-11 13:05:20 -07001414 LdapPatchParams ldapObject;
1415 LdapPatchParams activeDirectoryObject;
Ed Tanousc1019822024-03-06 12:54:38 -08001416 AuthMethods auth;
Ravi Teja482a69e2024-04-22 06:56:13 -05001417 std::optional<std::string> httpBasicAuth;
Ed Tanousc1019822024-03-06 12:54:38 -08001418 // clang-format off
Ed Tanous1ef4c342022-05-12 16:12:36 -07001419 if (!json_util::readJsonPatch(
Ed Tanousc1019822024-03-06 12:54:38 -08001420 req, asyncResp->res,
1421 "AccountLockoutDuration", unlockTimeout,
1422 "AccountLockoutThreshold", lockoutThreshold,
Ed Tanous10cb44f2024-04-11 13:05:20 -07001423 "ActiveDirectory/Authentication/AuthenticationType", activeDirectoryObject.authType,
1424 "ActiveDirectory/Authentication/Password", activeDirectoryObject.password,
1425 "ActiveDirectory/Authentication/Username", activeDirectoryObject.userName,
1426 "ActiveDirectory/LDAPService/SearchSettings/BaseDistinguishedNames", activeDirectoryObject.baseDNList,
1427 "ActiveDirectory/LDAPService/SearchSettings/GroupsAttribute", activeDirectoryObject.groupsAttribute,
1428 "ActiveDirectory/LDAPService/SearchSettings/UsernameAttribute", activeDirectoryObject.userNameAttribute,
1429 "ActiveDirectory/RemoteRoleMapping", activeDirectoryObject.remoteRoleMapData,
1430 "ActiveDirectory/ServiceAddresses", activeDirectoryObject.serviceAddressList,
1431 "ActiveDirectory/ServiceEnabled", activeDirectoryObject.serviceEnabled,
1432 "LDAP/Authentication/AuthenticationType", ldapObject.authType,
1433 "LDAP/Authentication/Password", ldapObject.password,
1434 "LDAP/Authentication/Username", ldapObject.userName,
1435 "LDAP/LDAPService/SearchSettings/BaseDistinguishedNames", ldapObject.baseDNList,
1436 "LDAP/LDAPService/SearchSettings/GroupsAttribute", ldapObject.groupsAttribute,
1437 "LDAP/LDAPService/SearchSettings/UsernameAttribute", ldapObject.userNameAttribute,
1438 "LDAP/RemoteRoleMapping", ldapObject.remoteRoleMapData,
1439 "LDAP/ServiceAddresses", ldapObject.serviceAddressList,
1440 "LDAP/ServiceEnabled", ldapObject.serviceEnabled,
Ed Tanousc1019822024-03-06 12:54:38 -08001441 "MaxPasswordLength", maxPasswordLength,
1442 "MinPasswordLength", minPasswordLength,
Ed Tanousc1019822024-03-06 12:54:38 -08001443 "Oem/OpenBMC/AuthMethods/BasicAuth", auth.basicAuth,
1444 "Oem/OpenBMC/AuthMethods/Cookie", auth.cookie,
1445 "Oem/OpenBMC/AuthMethods/SessionToken", auth.sessionToken,
Ed Tanous10cb44f2024-04-11 13:05:20 -07001446 "Oem/OpenBMC/AuthMethods/TLS", auth.tls,
Ravi Teja482a69e2024-04-22 06:56:13 -05001447 "Oem/OpenBMC/AuthMethods/XToken", auth.xToken,
1448 "HTTPBasicAuth", httpBasicAuth))
Ed Tanous1ef4c342022-05-12 16:12:36 -07001449 {
1450 return;
1451 }
Ed Tanousc1019822024-03-06 12:54:38 -08001452 // clang-format on
Ed Tanous1ef4c342022-05-12 16:12:36 -07001453
Ravi Teja482a69e2024-04-22 06:56:13 -05001454 if (httpBasicAuth)
1455 {
1456 if (*httpBasicAuth == "Enabled")
1457 {
1458 auth.basicAuth = true;
1459 }
1460 else if (*httpBasicAuth == "Disabled")
1461 {
1462 auth.basicAuth = false;
1463 }
1464 else
1465 {
1466 messages::propertyValueNotInList(asyncResp->res, "HttpBasicAuth",
1467 *httpBasicAuth);
1468 }
1469 }
1470
Ed Tanous1ef4c342022-05-12 16:12:36 -07001471 if (minPasswordLength)
1472 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001473 setDbusProperty(
1474 asyncResp, "xyz.openbmc_project.User.Manager",
1475 sdbusplus::message::object_path("/xyz/openbmc_project/user"),
George Liu9ae226f2023-06-21 17:56:46 +08001476 "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
Ed Tanousd02aad32024-02-13 14:43:34 -08001477 "MinPasswordLength", *minPasswordLength);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001478 }
1479
1480 if (maxPasswordLength)
1481 {
1482 messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1483 }
1484
Ed Tanous10cb44f2024-04-11 13:05:20 -07001485 handleLDAPPatch(std::move(activeDirectoryObject), asyncResp,
1486 "ActiveDirectory");
1487 handleLDAPPatch(std::move(ldapObject), asyncResp, "LDAP");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001488
Ed Tanousc1019822024-03-06 12:54:38 -08001489 handleAuthMethodsPatch(asyncResp, auth);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001490
Ed Tanous1ef4c342022-05-12 16:12:36 -07001491 if (unlockTimeout)
1492 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001493 setDbusProperty(
1494 asyncResp, "xyz.openbmc_project.User.Manager",
1495 sdbusplus::message::object_path("/xyz/openbmc_project/user"),
Ed Tanous1ef4c342022-05-12 16:12:36 -07001496 "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
Ed Tanousd02aad32024-02-13 14:43:34 -08001497 "AccountLockoutDuration", *unlockTimeout);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001498 }
1499 if (lockoutThreshold)
1500 {
Ed Tanousd02aad32024-02-13 14:43:34 -08001501 setDbusProperty(
1502 asyncResp, "xyz.openbmc_project.User.Manager",
1503 sdbusplus::message::object_path("/xyz/openbmc_project/user"),
George Liu9ae226f2023-06-21 17:56:46 +08001504 "xyz.openbmc_project.User.AccountPolicy",
Ed Tanousd02aad32024-02-13 14:43:34 -08001505 "MaxLoginAttemptBeforeLockout", "AccountLockoutThreshold",
1506 *lockoutThreshold);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001507 }
1508}
1509
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001510inline void handleAccountCollectionHead(
Ed Tanous1ef4c342022-05-12 16:12:36 -07001511 App& app, const crow::Request& req,
1512 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1513{
1514 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1515 {
1516 return;
1517 }
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001518 asyncResp->res.addHeader(
1519 boost::beast::http::field::link,
1520 "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
1521}
1522
1523inline void handleAccountCollectionGet(
1524 App& app, const crow::Request& req,
1525 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1526{
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001527 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1528 {
1529 return;
1530 }
Ninad Palsule3e72c202023-03-27 17:19:55 -05001531
1532 if (req.session == nullptr)
1533 {
1534 messages::internalError(asyncResp->res);
1535 return;
1536 }
1537
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001538 asyncResp->res.addHeader(
1539 boost::beast::http::field::link,
1540 "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001541
1542 asyncResp->res.jsonValue["@odata.id"] =
1543 "/redfish/v1/AccountService/Accounts";
1544 asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
1545 "ManagerAccountCollection";
1546 asyncResp->res.jsonValue["Name"] = "Accounts Collection";
1547 asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
1548
1549 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001550 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001551
1552 std::string thisUser;
1553 if (req.session)
1554 {
1555 thisUser = req.session->username;
1556 }
George Liu5eb468d2023-06-20 17:03:24 +08001557 sdbusplus::message::object_path path("/xyz/openbmc_project/user");
1558 dbus::utility::getManagedObjects(
1559 "xyz.openbmc_project.User.Manager", path,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001560 [asyncResp, thisUser, effectiveUserPrivileges](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001561 const boost::system::error_code& ec,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001562 const dbus::utility::ManagedObjectType& users) {
1563 if (ec)
1564 {
1565 messages::internalError(asyncResp->res);
1566 return;
1567 }
1568
1569 bool userCanSeeAllAccounts =
1570 effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1571
1572 bool userCanSeeSelf =
1573 effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1574
1575 nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1576 memberArray = nlohmann::json::array();
1577
1578 for (const auto& userpath : users)
1579 {
1580 std::string user = userpath.first.filename();
1581 if (user.empty())
1582 {
1583 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -07001584 BMCWEB_LOG_ERROR("Invalid firmware ID");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001585
1586 return;
1587 }
1588
1589 // As clarified by Redfish here:
1590 // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
1591 // Users without ConfigureUsers, only see their own
1592 // account. Users with ConfigureUsers, see all
1593 // accounts.
1594 if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1595 {
1596 nlohmann::json::object_t member;
Ed Tanous3b327802023-08-14 09:23:43 -07001597 member["@odata.id"] = boost::urls::format(
1598 "/redfish/v1/AccountService/Accounts/{}", user);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001599 memberArray.emplace_back(std::move(member));
Ed Tanous1ef4c342022-05-12 16:12:36 -07001600 }
1601 }
1602 asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05001603 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001604}
1605
Ninad Palsule97e90da2023-05-17 14:04:52 -05001606inline void processAfterCreateUser(
1607 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1608 const std::string& username, const std::string& password,
1609 const boost::system::error_code& ec, sdbusplus::message_t& m)
1610{
1611 if (ec)
1612 {
1613 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
1614 return;
1615 }
1616
1617 if (pamUpdatePassword(username, password) != PAM_SUCCESS)
1618 {
1619 // At this point we have a user that's been
1620 // created, but the password set
1621 // failed.Something is wrong, so delete the user
1622 // that we've already created
1623 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1624 tempObjPath /= username;
1625 const std::string userPath(tempObjPath);
1626
1627 crow::connections::systemBus->async_method_call(
1628 [asyncResp, password](const boost::system::error_code& ec3) {
1629 if (ec3)
1630 {
1631 messages::internalError(asyncResp->res);
1632 return;
1633 }
1634
1635 // If password is invalid
Jason M. Bills9bd80832023-08-30 15:19:41 -07001636 messages::propertyValueFormatError(asyncResp->res, nullptr,
Ninad Palsule97e90da2023-05-17 14:04:52 -05001637 "Password");
Patrick Williams5a39f772023-10-20 11:20:21 -05001638 },
Ninad Palsule97e90da2023-05-17 14:04:52 -05001639 "xyz.openbmc_project.User.Manager", userPath,
1640 "xyz.openbmc_project.Object.Delete", "Delete");
1641
Ed Tanous62598e32023-07-17 17:06:25 -07001642 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
Ninad Palsule97e90da2023-05-17 14:04:52 -05001643 return;
1644 }
1645
1646 messages::created(asyncResp->res);
1647 asyncResp->res.addHeader("Location",
1648 "/redfish/v1/AccountService/Accounts/" + username);
1649}
1650
1651inline void processAfterGetAllGroups(
1652 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1653 const std::string& username, const std::string& password,
Ed Tanouse01d0c32023-06-30 13:21:32 -07001654 const std::string& roleId, bool enabled,
Ninad Palsule9ba73932023-06-01 16:38:57 -05001655 std::optional<std::vector<std::string>> accountTypes,
Ninad Palsule97e90da2023-05-17 14:04:52 -05001656 const std::vector<std::string>& allGroupsList)
Ninad Palsule97e90da2023-05-17 14:04:52 -05001657{
Ninad Palsule3e72c202023-03-27 17:19:55 -05001658 std::vector<std::string> userGroups;
Ninad Palsule9ba73932023-06-01 16:38:57 -05001659 std::vector<std::string> accountTypeUserGroups;
1660
1661 // If user specified account types then convert them to unix user groups
1662 if (accountTypes)
1663 {
1664 if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
1665 accountTypeUserGroups))
1666 {
1667 // Problem in mapping Account Types to User Groups, Error already
1668 // logged.
1669 return;
1670 }
1671 }
1672
Ninad Palsule3e72c202023-03-27 17:19:55 -05001673 for (const auto& grp : allGroupsList)
1674 {
Ninad Palsule9ba73932023-06-01 16:38:57 -05001675 // If user specified the account type then only accept groups which are
1676 // in the account types group list.
1677 if (!accountTypeUserGroups.empty())
1678 {
1679 bool found = false;
1680 for (const auto& grp1 : accountTypeUserGroups)
1681 {
1682 if (grp == grp1)
1683 {
1684 found = true;
1685 break;
1686 }
1687 }
1688 if (!found)
1689 {
1690 continue;
1691 }
1692 }
1693
Ninad Palsule3e72c202023-03-27 17:19:55 -05001694 // Console access is provided to the user who is a member of
1695 // hostconsole group and has a administrator role. So, set
1696 // hostconsole group only for the administrator.
Ninad Palsule9ba73932023-06-01 16:38:57 -05001697 if ((grp == "hostconsole") && (roleId != "priv-admin"))
Ninad Palsule3e72c202023-03-27 17:19:55 -05001698 {
Ninad Palsule9ba73932023-06-01 16:38:57 -05001699 if (!accountTypeUserGroups.empty())
1700 {
Ed Tanous62598e32023-07-17 17:06:25 -07001701 BMCWEB_LOG_ERROR(
1702 "Only administrator can get HostConsole access");
Ninad Palsule9ba73932023-06-01 16:38:57 -05001703 asyncResp->res.result(boost::beast::http::status::bad_request);
1704 return;
1705 }
1706 continue;
Ninad Palsule3e72c202023-03-27 17:19:55 -05001707 }
Ninad Palsule9ba73932023-06-01 16:38:57 -05001708 userGroups.emplace_back(grp);
1709 }
1710
1711 // Make sure user specified groups are valid. This is internal error because
1712 // it some inconsistencies between user manager and bmcweb.
1713 if (!accountTypeUserGroups.empty() &&
1714 accountTypeUserGroups.size() != userGroups.size())
1715 {
1716 messages::internalError(asyncResp->res);
1717 return;
Ninad Palsule3e72c202023-03-27 17:19:55 -05001718 }
Ninad Palsule97e90da2023-05-17 14:04:52 -05001719 crow::connections::systemBus->async_method_call(
1720 [asyncResp, username, password](const boost::system::error_code& ec2,
1721 sdbusplus::message_t& m) {
1722 processAfterCreateUser(asyncResp, username, password, ec2, m);
Patrick Williams5a39f772023-10-20 11:20:21 -05001723 },
Ninad Palsule97e90da2023-05-17 14:04:52 -05001724 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
Ninad Palsule3e72c202023-03-27 17:19:55 -05001725 "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
Ed Tanouse01d0c32023-06-30 13:21:32 -07001726 roleId, enabled);
Ninad Palsule97e90da2023-05-17 14:04:52 -05001727}
1728
Ed Tanous1ef4c342022-05-12 16:12:36 -07001729inline void handleAccountCollectionPost(
1730 App& app, const crow::Request& req,
1731 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1732{
1733 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1734 {
1735 return;
1736 }
1737 std::string username;
1738 std::string password;
Ed Tanouse01d0c32023-06-30 13:21:32 -07001739 std::optional<std::string> roleIdJson;
1740 std::optional<bool> enabledJson;
Ninad Palsule9ba73932023-06-01 16:38:57 -05001741 std::optional<std::vector<std::string>> accountTypes;
Ed Tanouse01d0c32023-06-30 13:21:32 -07001742 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
1743 "Password", password, "RoleId", roleIdJson,
1744 "Enabled", enabledJson, "AccountTypes",
1745 accountTypes))
Ed Tanous1ef4c342022-05-12 16:12:36 -07001746 {
1747 return;
1748 }
1749
Ed Tanouse01d0c32023-06-30 13:21:32 -07001750 std::string roleId = roleIdJson.value_or("User");
1751 std::string priv = getPrivilegeFromRoleId(roleId);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001752 if (priv.empty())
1753 {
Ed Tanouse01d0c32023-06-30 13:21:32 -07001754 messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001755 return;
1756 }
Asmitha Karunanithi239adf82022-03-25 02:59:03 -05001757 roleId = priv;
Ed Tanous1ef4c342022-05-12 16:12:36 -07001758
Ed Tanouse01d0c32023-06-30 13:21:32 -07001759 bool enabled = enabledJson.value_or(true);
1760
Ed Tanous1ef4c342022-05-12 16:12:36 -07001761 // Reading AllGroups property
1762 sdbusplus::asio::getProperty<std::vector<std::string>>(
1763 *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1764 "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
1765 "AllGroups",
Ninad Palsule9ba73932023-06-01 16:38:57 -05001766 [asyncResp, username, password{std::move(password)}, roleId, enabled,
1767 accountTypes](const boost::system::error_code& ec,
1768 const std::vector<std::string>& allGroupsList) {
Ed Tanous1ef4c342022-05-12 16:12:36 -07001769 if (ec)
1770 {
Ed Tanous62598e32023-07-17 17:06:25 -07001771 BMCWEB_LOG_DEBUG("ERROR with async_method_call");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001772 messages::internalError(asyncResp->res);
1773 return;
1774 }
1775
1776 if (allGroupsList.empty())
1777 {
1778 messages::internalError(asyncResp->res);
1779 return;
1780 }
1781
Ninad Palsule97e90da2023-05-17 14:04:52 -05001782 processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
Ninad Palsule9ba73932023-06-01 16:38:57 -05001783 accountTypes, allGroupsList);
Patrick Williams5a39f772023-10-20 11:20:21 -05001784 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001785}
1786
1787inline void
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001788 handleAccountHead(App& app, const crow::Request& req,
1789 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1790 const std::string& /*accountName*/)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001791{
1792 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1793 {
1794 return;
1795 }
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001796 asyncResp->res.addHeader(
1797 boost::beast::http::field::link,
1798 "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1799}
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001800
Ed Tanous4c7d4d32022-07-07 15:29:35 -07001801inline void
1802 handleAccountGet(App& app, const crow::Request& req,
1803 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1804 const std::string& accountName)
1805{
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001806 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1807 {
1808 return;
1809 }
1810 asyncResp->res.addHeader(
1811 boost::beast::http::field::link,
1812 "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1813
Ed Tanous25b54db2024-04-17 15:40:31 -07001814 if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1815 {
1816 // If authentication is disabled, there are no user accounts
1817 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1818 accountName);
1819 return;
1820 }
Jiaqing Zhaoafd369c2023-03-07 15:12:22 +08001821
Ed Tanous1ef4c342022-05-12 16:12:36 -07001822 if (req.session == nullptr)
1823 {
1824 messages::internalError(asyncResp->res);
1825 return;
1826 }
1827 if (req.session->username != accountName)
1828 {
1829 // At this point we've determined that the user is trying to
1830 // modify a user that isn't them. We need to verify that they
1831 // have permissions to modify other users, so re-run the auth
1832 // check with the same permissions, minus ConfigureSelf.
1833 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05001834 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001835 Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
1836 "ConfigureManager"};
1837 if (!effectiveUserPrivileges.isSupersetOf(
1838 requiredPermissionsToChangeNonSelf))
1839 {
Ed Tanous62598e32023-07-17 17:06:25 -07001840 BMCWEB_LOG_DEBUG("GET Account denied access");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001841 messages::insufficientPrivilege(asyncResp->res);
1842 return;
1843 }
1844 }
1845
George Liu5eb468d2023-06-20 17:03:24 +08001846 sdbusplus::message::object_path path("/xyz/openbmc_project/user");
1847 dbus::utility::getManagedObjects(
1848 "xyz.openbmc_project.User.Manager", path,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001849 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001850 accountName](const boost::system::error_code& ec,
Ed Tanous1ef4c342022-05-12 16:12:36 -07001851 const dbus::utility::ManagedObjectType& users) {
1852 if (ec)
1853 {
1854 messages::internalError(asyncResp->res);
1855 return;
1856 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001857 const auto userIt = std::ranges::find_if(
Michael Shen80f79a42023-08-24 13:41:53 +00001858 users,
1859 [accountName](
1860 const std::pair<sdbusplus::message::object_path,
1861 dbus::utility::DBusInterfacesMap>& user) {
1862 return accountName == user.first.filename();
Patrick Williams5a39f772023-10-20 11:20:21 -05001863 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001864
1865 if (userIt == users.end())
1866 {
1867 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1868 accountName);
1869 return;
1870 }
1871
1872 asyncResp->res.jsonValue["@odata.type"] =
Abhishek Patel58345852022-02-02 08:54:25 -06001873 "#ManagerAccount.v1_7_0.ManagerAccount";
Ed Tanous1ef4c342022-05-12 16:12:36 -07001874 asyncResp->res.jsonValue["Name"] = "User Account";
1875 asyncResp->res.jsonValue["Description"] = "User Account";
1876 asyncResp->res.jsonValue["Password"] = nullptr;
Abhishek Patel58345852022-02-02 08:54:25 -06001877 asyncResp->res.jsonValue["StrictAccountTypes"] = true;
Ed Tanous1ef4c342022-05-12 16:12:36 -07001878
1879 for (const auto& interface : userIt->second)
1880 {
1881 if (interface.first == "xyz.openbmc_project.User.Attributes")
1882 {
1883 for (const auto& property : interface.second)
1884 {
1885 if (property.first == "UserEnabled")
1886 {
1887 const bool* userEnabled =
1888 std::get_if<bool>(&property.second);
1889 if (userEnabled == nullptr)
1890 {
Ed Tanous62598e32023-07-17 17:06:25 -07001891 BMCWEB_LOG_ERROR("UserEnabled wasn't a bool");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001892 messages::internalError(asyncResp->res);
1893 return;
1894 }
1895 asyncResp->res.jsonValue["Enabled"] = *userEnabled;
1896 }
1897 else if (property.first == "UserLockedForFailedAttempt")
1898 {
1899 const bool* userLocked =
1900 std::get_if<bool>(&property.second);
1901 if (userLocked == nullptr)
1902 {
Ed Tanous62598e32023-07-17 17:06:25 -07001903 BMCWEB_LOG_ERROR("UserLockedForF"
1904 "ailedAttempt "
1905 "wasn't a bool");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001906 messages::internalError(asyncResp->res);
1907 return;
1908 }
1909 asyncResp->res.jsonValue["Locked"] = *userLocked;
1910 asyncResp->res
1911 .jsonValue["Locked@Redfish.AllowableValues"] = {
1912 "false"}; // can only unlock accounts
1913 }
1914 else if (property.first == "UserPrivilege")
1915 {
1916 const std::string* userPrivPtr =
1917 std::get_if<std::string>(&property.second);
1918 if (userPrivPtr == nullptr)
1919 {
Ed Tanous62598e32023-07-17 17:06:25 -07001920 BMCWEB_LOG_ERROR("UserPrivilege wasn't a "
1921 "string");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001922 messages::internalError(asyncResp->res);
1923 return;
1924 }
1925 std::string role = getRoleIdFromPrivilege(*userPrivPtr);
1926 if (role.empty())
1927 {
Ed Tanous62598e32023-07-17 17:06:25 -07001928 BMCWEB_LOG_ERROR("Invalid user role");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001929 messages::internalError(asyncResp->res);
1930 return;
1931 }
1932 asyncResp->res.jsonValue["RoleId"] = role;
1933
1934 nlohmann::json& roleEntry =
1935 asyncResp->res.jsonValue["Links"]["Role"];
Ed Tanous3b327802023-08-14 09:23:43 -07001936 roleEntry["@odata.id"] = boost::urls::format(
1937 "/redfish/v1/AccountService/Roles/{}", role);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001938 }
1939 else if (property.first == "UserPasswordExpired")
1940 {
1941 const bool* userPasswordExpired =
1942 std::get_if<bool>(&property.second);
1943 if (userPasswordExpired == nullptr)
1944 {
Ed Tanous62598e32023-07-17 17:06:25 -07001945 BMCWEB_LOG_ERROR(
1946 "UserPasswordExpired wasn't a bool");
Ed Tanous1ef4c342022-05-12 16:12:36 -07001947 messages::internalError(asyncResp->res);
1948 return;
1949 }
1950 asyncResp->res.jsonValue["PasswordChangeRequired"] =
1951 *userPasswordExpired;
1952 }
Abhishek Patelc7229812022-02-01 10:07:15 -06001953 else if (property.first == "UserGroups")
1954 {
1955 const std::vector<std::string>* userGroups =
1956 std::get_if<std::vector<std::string>>(
1957 &property.second);
1958 if (userGroups == nullptr)
1959 {
Ed Tanous62598e32023-07-17 17:06:25 -07001960 BMCWEB_LOG_ERROR(
1961 "userGroups wasn't a string vector");
Abhishek Patelc7229812022-02-01 10:07:15 -06001962 messages::internalError(asyncResp->res);
1963 return;
1964 }
1965 if (!translateUserGroup(*userGroups, asyncResp->res))
1966 {
Ed Tanous62598e32023-07-17 17:06:25 -07001967 BMCWEB_LOG_ERROR("userGroups mapping failed");
Abhishek Patelc7229812022-02-01 10:07:15 -06001968 messages::internalError(asyncResp->res);
1969 return;
1970 }
1971 }
Ed Tanous1ef4c342022-05-12 16:12:36 -07001972 }
1973 }
1974 }
1975
Ed Tanous3b327802023-08-14 09:23:43 -07001976 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1977 "/redfish/v1/AccountService/Accounts/{}", accountName);
Ed Tanous1ef4c342022-05-12 16:12:36 -07001978 asyncResp->res.jsonValue["Id"] = accountName;
1979 asyncResp->res.jsonValue["UserName"] = accountName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001980 });
Ed Tanous1ef4c342022-05-12 16:12:36 -07001981}
1982
1983inline void
Gunnar Mills20fc3072023-01-27 15:13:36 -06001984 handleAccountDelete(App& app, const crow::Request& req,
1985 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1986 const std::string& username)
Ed Tanous1ef4c342022-05-12 16:12:36 -07001987{
1988 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1989 {
1990 return;
1991 }
1992
Ed Tanous25b54db2024-04-17 15:40:31 -07001993 if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1994 {
1995 // If authentication is disabled, there are no user accounts
1996 messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1997 return;
1998 }
Ed Tanous1ef4c342022-05-12 16:12:36 -07001999 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
2000 tempObjPath /= username;
2001 const std::string userPath(tempObjPath);
2002
2003 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002004 [asyncResp, username](const boost::system::error_code& ec) {
Ed Tanous1ef4c342022-05-12 16:12:36 -07002005 if (ec)
2006 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002007 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
Ed Tanous1ef4c342022-05-12 16:12:36 -07002008 username);
2009 return;
2010 }
2011
2012 messages::accountRemoved(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05002013 },
Ed Tanous1ef4c342022-05-12 16:12:36 -07002014 "xyz.openbmc_project.User.Manager", userPath,
2015 "xyz.openbmc_project.Object.Delete", "Delete");
2016}
2017
2018inline void
2019 handleAccountPatch(App& app, const crow::Request& req,
2020 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2021 const std::string& username)
2022{
2023 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2024 {
2025 return;
2026 }
Ed Tanous25b54db2024-04-17 15:40:31 -07002027 if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
2028 {
2029 // If authentication is disabled, there are no user accounts
2030 messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
2031 return;
2032 }
Ed Tanous1ef4c342022-05-12 16:12:36 -07002033 std::optional<std::string> newUserName;
2034 std::optional<std::string> password;
2035 std::optional<bool> enabled;
2036 std::optional<std::string> roleId;
2037 std::optional<bool> locked;
Abhishek Patel58345852022-02-02 08:54:25 -06002038 std::optional<std::vector<std::string>> accountTypes;
2039
Ed Tanous1ef4c342022-05-12 16:12:36 -07002040 if (req.session == nullptr)
2041 {
2042 messages::internalError(asyncResp->res);
2043 return;
2044 }
2045
Ed Tanous2b9c1df2024-04-06 13:52:01 -07002046 bool userSelf = (username == req.session->username);
2047
Ed Tanous1ef4c342022-05-12 16:12:36 -07002048 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -05002049 redfish::getUserPrivileges(*req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07002050 Privileges configureUsers = {"ConfigureUsers"};
2051 bool userHasConfigureUsers =
2052 effectiveUserPrivileges.isSupersetOf(configureUsers);
2053 if (userHasConfigureUsers)
2054 {
2055 // Users with ConfigureUsers can modify for all users
Abhishek Patel58345852022-02-02 08:54:25 -06002056 if (!json_util::readJsonPatch(
2057 req, asyncResp->res, "UserName", newUserName, "Password",
2058 password, "RoleId", roleId, "Enabled", enabled, "Locked",
2059 locked, "AccountTypes", accountTypes))
Ed Tanous1ef4c342022-05-12 16:12:36 -07002060 {
2061 return;
2062 }
2063 }
2064 else
2065 {
2066 // ConfigureSelf accounts can only modify their own account
Abhishek Patel58345852022-02-02 08:54:25 -06002067 if (!userSelf)
Ed Tanous1ef4c342022-05-12 16:12:36 -07002068 {
2069 messages::insufficientPrivilege(asyncResp->res);
2070 return;
2071 }
2072
2073 // ConfigureSelf accounts can only modify their password
2074 if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
2075 password))
2076 {
2077 return;
2078 }
2079 }
2080
2081 // if user name is not provided in the patch method or if it
2082 // matches the user name in the URI, then we are treating it as
2083 // updating user properties other then username. If username
2084 // provided doesn't match the URI, then we are treating this as
2085 // user rename request.
2086 if (!newUserName || (newUserName.value() == username))
2087 {
2088 updateUserProperties(asyncResp, username, password, enabled, roleId,
Ravi Tejae518ef32024-05-16 10:33:08 -05002089 locked, accountTypes, userSelf, req.session);
Ed Tanous1ef4c342022-05-12 16:12:36 -07002090 return;
2091 }
2092 crow::connections::systemBus->async_method_call(
2093 [asyncResp, username, password(std::move(password)),
2094 roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
Ravi Tejae518ef32024-05-16 10:33:08 -05002095 locked, userSelf, req, accountTypes(std::move(accountTypes))](
Ed Tanouse81de512023-06-27 17:07:00 -07002096 const boost::system::error_code& ec, sdbusplus::message_t& m) {
Ed Tanous1ef4c342022-05-12 16:12:36 -07002097 if (ec)
2098 {
2099 userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2100 username);
2101 return;
2102 }
2103
2104 updateUserProperties(asyncResp, newUser, password, enabled, roleId,
Ravi Tejae518ef32024-05-16 10:33:08 -05002105 locked, accountTypes, userSelf, req.session);
Patrick Williams5a39f772023-10-20 11:20:21 -05002106 },
Ed Tanous1ef4c342022-05-12 16:12:36 -07002107 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
2108 "xyz.openbmc_project.User.Manager", "RenameUser", username,
2109 *newUserName);
2110}
2111
Ed Tanous6c51eab2021-06-03 12:30:29 -07002112inline void requestAccountServiceRoutes(App& app)
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07002113{
Ed Tanous6c51eab2021-06-03 12:30:29 -07002114 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Ed Tanous4c7d4d32022-07-07 15:29:35 -07002115 .privileges(redfish::privileges::headAccountService)
2116 .methods(boost::beast::http::verb::head)(
2117 std::bind_front(handleAccountServiceHead, std::ref(app)));
2118
2119 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Ed Tanoused398212021-06-09 17:05:54 -07002120 .privileges(redfish::privileges::getAccountService)
Ed Tanous002d39b2022-05-31 08:59:27 -07002121 .methods(boost::beast::http::verb::get)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002122 std::bind_front(handleAccountServiceGet, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002123
Ed Tanousf5ffd802021-07-19 10:55:33 -07002124 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Gunnar Mills1ec43ee2022-01-04 15:39:52 -06002125 .privileges(redfish::privileges::patchAccountService)
Ed Tanousf5ffd802021-07-19 10:55:33 -07002126 .methods(boost::beast::http::verb::patch)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002127 std::bind_front(handleAccountServicePatch, std::ref(app)));
Ed Tanousf5ffd802021-07-19 10:55:33 -07002128
Ed Tanous1aa375b2024-04-13 11:51:10 -07002129 BMCWEB_ROUTE(
2130 app,
2131 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates")
2132 .privileges(redfish::privileges::headCertificateCollection)
2133 .methods(boost::beast::http::verb::head)(std::bind_front(
2134 handleAccountServiceClientCertificatesHead, std::ref(app)));
2135
2136 BMCWEB_ROUTE(
2137 app,
2138 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates")
2139 .privileges(redfish::privileges::getCertificateCollection)
2140 .methods(boost::beast::http::verb::get)(std::bind_front(
2141 handleAccountServiceClientCertificatesGet, std::ref(app)));
2142
2143 BMCWEB_ROUTE(
2144 app,
2145 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates/<str>")
2146 .privileges(redfish::privileges::headCertificate)
2147 .methods(boost::beast::http::verb::head)(std::bind_front(
2148 handleAccountServiceClientCertificatesInstanceHead, std::ref(app)));
2149
2150 BMCWEB_ROUTE(
2151 app,
2152 "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates/<str>/")
2153 .privileges(redfish::privileges::getCertificate)
2154 .methods(boost::beast::http::verb::get)(std::bind_front(
2155 handleAccountServiceClientCertificatesInstanceGet, std::ref(app)));
2156
Ed Tanous6c51eab2021-06-03 12:30:29 -07002157 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanous4c7d4d32022-07-07 15:29:35 -07002158 .privileges(redfish::privileges::headManagerAccountCollection)
2159 .methods(boost::beast::http::verb::head)(
2160 std::bind_front(handleAccountCollectionHead, std::ref(app)));
2161
2162 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanoused398212021-06-09 17:05:54 -07002163 .privileges(redfish::privileges::getManagerAccountCollection)
Ed Tanous6c51eab2021-06-03 12:30:29 -07002164 .methods(boost::beast::http::verb::get)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002165 std::bind_front(handleAccountCollectionGet, std::ref(app)));
Ed Tanous06e086d2018-09-19 17:19:52 -07002166
Ed Tanous6c51eab2021-06-03 12:30:29 -07002167 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanoused398212021-06-09 17:05:54 -07002168 .privileges(redfish::privileges::postManagerAccountCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 .methods(boost::beast::http::verb::post)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002170 std::bind_front(handleAccountCollectionPost, std::ref(app)));
Ed Tanous002d39b2022-05-31 08:59:27 -07002171
2172 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanous4c7d4d32022-07-07 15:29:35 -07002173 .privileges(redfish::privileges::headManagerAccount)
2174 .methods(boost::beast::http::verb::head)(
2175 std::bind_front(handleAccountHead, std::ref(app)));
2176
2177 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanous002d39b2022-05-31 08:59:27 -07002178 .privileges(redfish::privileges::getManagerAccount)
2179 .methods(boost::beast::http::verb::get)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002180 std::bind_front(handleAccountGet, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002181
2182 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002183 // TODO this privilege should be using the generated endpoints, but
2184 // because of the special handling of ConfigureSelf, it's not able to
2185 // yet
Ed Tanous6c51eab2021-06-03 12:30:29 -07002186 .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
2187 .methods(boost::beast::http::verb::patch)(
Ed Tanous1ef4c342022-05-12 16:12:36 -07002188 std::bind_front(handleAccountPatch, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002189
2190 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002191 .privileges(redfish::privileges::deleteManagerAccount)
Ed Tanous6c51eab2021-06-03 12:30:29 -07002192 .methods(boost::beast::http::verb::delete_)(
Gunnar Mills20fc3072023-01-27 15:13:36 -06002193 std::bind_front(handleAccountDelete, std::ref(app)));
Ed Tanous6c51eab2021-06-03 12:30:29 -07002194}
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +01002195
Ed Tanous1abe55e2018-09-05 08:30:59 -07002196} // namespace redfish