blob: 435d8906b12312447f31ab95ceff273ac4a4dfd2 [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
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Ratan Gupta24c85422019-01-30 19:41:24 +053019#include <dbus_utility.hpp>
Ed Tanous65b0dc32018-09-19 16:04:03 -070020#include <error_messages.hpp>
Ed Tanousb9b2e0b2018-09-13 13:47:50 -070021#include <openbmc_dbus_rest.hpp>
Ed Tanous52cc1122020-07-18 13:51:21 -070022#include <persistent_data.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070023#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070025#include <sdbusplus/asio/property.hpp>
Ed Tanousa8408792018-09-05 16:08:38 -070026#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +010030
Ed Tanous23a21a12020-07-25 04:45:05 +000031constexpr const char* ldapConfigObjectName =
Ratan Gupta6973a582018-12-13 18:25:44 +053032 "/xyz/openbmc_project/user/ldap/openldap";
Ed Tanous2c70f802020-09-28 14:29:23 -070033constexpr const char* adConfigObject =
Ratan Guptaab828d72019-04-22 14:18:33 +053034 "/xyz/openbmc_project/user/ldap/active_directory";
35
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +053036constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
Ratan Gupta6973a582018-12-13 18:25:44 +053037constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
38constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
39constexpr const char* ldapConfigInterface =
40 "xyz.openbmc_project.User.Ldap.Config";
41constexpr const char* ldapCreateInterface =
42 "xyz.openbmc_project.User.Ldap.Create";
43constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
Ratan Gupta06785242019-07-26 22:30:16 +053044constexpr const char* ldapPrivMapperInterface =
45 "xyz.openbmc_project.User.PrivilegeMapper";
Ratan Gupta6973a582018-12-13 18:25:44 +053046constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
47constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
48constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
49constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
50constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
51
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060052struct LDAPRoleMapData
53{
54 std::string groupName;
55 std::string privilege;
56};
57
Ratan Gupta6973a582018-12-13 18:25:44 +053058struct LDAPConfigData
59{
60 std::string uri{};
61 std::string bindDN{};
62 std::string baseDN{};
63 std::string searchScope{};
64 std::string serverType{};
65 bool serviceEnabled = false;
66 std::string userNameAttribute{};
67 std::string groupAttribute{};
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060068 std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
Ratan Gupta6973a582018-12-13 18:25:44 +053069};
70
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060071inline std::string getRoleIdFromPrivilege(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053072{
73 if (role == "priv-admin")
74 {
75 return "Administrator";
76 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070077 if (role == "priv-user")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053078 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053079 return "ReadOnly";
AppaRao Puli84e12cb2018-10-11 01:28:15 +053080 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070081 if (role == "priv-operator")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053082 {
83 return "Operator";
84 }
Ed Tanous26f69762022-01-25 09:49:11 -080085 if (role.empty() || (role == "priv-noaccess"))
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000086 {
87 return "NoAccess";
88 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +053089 return "";
90}
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060091inline std::string getPrivilegeFromRoleId(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053092{
93 if (role == "Administrator")
94 {
95 return "priv-admin";
96 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070097 if (role == "ReadOnly")
AppaRao Puli84e12cb2018-10-11 01:28:15 +053098 {
99 return "priv-user";
100 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700101 if (role == "Operator")
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530102 {
103 return "priv-operator";
104 }
Ed Tanous26f69762022-01-25 09:49:11 -0800105 if ((role == "NoAccess") || (role.empty()))
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +0000106 {
107 return "priv-noaccess";
108 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530109 return "";
110}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700111
zhanghch058d1b46d2021-04-01 11:18:24 +0800112inline void userErrorMessageHandler(
113 const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
114 const std::string& newUser, const std::string& username)
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000115{
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000116 if (e == nullptr)
117 {
118 messages::internalError(asyncResp->res);
119 return;
120 }
121
Manojkiran Eda055806b2020-11-03 09:36:28 +0530122 const char* errorMessage = e->name;
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000123 if (strcmp(errorMessage,
124 "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
125 {
126 messages::resourceAlreadyExists(asyncResp->res,
Gunnar Mills8114bd42020-06-11 20:55:21 -0500127 "#ManagerAccount.v1_4_0.ManagerAccount",
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000128 "UserName", newUser);
129 }
130 else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
131 "UserNameDoesNotExist") == 0)
132 {
133 messages::resourceNotFound(
Gunnar Mills8114bd42020-06-11 20:55:21 -0500134 asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount", username);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000135 }
Ed Tanousd4d25792020-09-29 15:15:03 -0700136 else if ((strcmp(errorMessage,
137 "xyz.openbmc_project.Common.Error.InvalidArgument") ==
138 0) ||
George Liu0fda0f12021-11-16 10:06:17 +0800139 (strcmp(
140 errorMessage,
141 "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
142 0))
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000143 {
144 messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
145 }
146 else if (strcmp(errorMessage,
147 "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
148 {
149 messages::createLimitReachedForResource(asyncResp->res);
150 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000151 else
152 {
153 messages::internalError(asyncResp->res);
154 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000155}
156
Ed Tanous81ce6092020-12-17 16:54:55 +0000157inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
Ed Tanous23a21a12020-07-25 04:45:05 +0000158 const LDAPConfigData& confData,
159 const std::string& ldapType)
Ratan Gupta6973a582018-12-13 18:25:44 +0530160{
Ratan Guptaab828d72019-04-22 14:18:33 +0530161 std::string service =
162 (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
Marri Devender Rao37cce912019-02-20 01:05:22 -0600163 nlohmann::json ldap = {
Ratan Gupta6973a582018-12-13 18:25:44 +0530164 {"ServiceEnabled", confData.serviceEnabled},
165 {"ServiceAddresses", nlohmann::json::array({confData.uri})},
166 {"Authentication",
167 {{"AuthenticationType", "UsernameAndPassword"},
Ratan Gupta6973a582018-12-13 18:25:44 +0530168 {"Username", confData.bindDN},
169 {"Password", nullptr}}},
170 {"LDAPService",
171 {{"SearchSettings",
172 {{"BaseDistinguishedNames",
173 nlohmann::json::array({confData.baseDN})},
174 {"UsernameAttribute", confData.userNameAttribute},
175 {"GroupsAttribute", confData.groupAttribute}}}}},
176 };
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600177
Ed Tanous81ce6092020-12-17 16:54:55 +0000178 jsonResponse[ldapType].update(ldap);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600179
Ed Tanous81ce6092020-12-17 16:54:55 +0000180 nlohmann::json& roleMapArray = jsonResponse[ldapType]["RemoteRoleMapping"];
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600181 roleMapArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -0800182 for (const auto& obj : confData.groupRoleList)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600183 {
184 BMCWEB_LOG_DEBUG << "Pushing the data groupName="
185 << obj.second.groupName << "\n";
186 roleMapArray.push_back(
187 {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
188 nlohmann::json::array(
189 {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
190 }
Ratan Gupta6973a582018-12-13 18:25:44 +0530191}
192
193/**
Ratan Gupta06785242019-07-26 22:30:16 +0530194 * @brief validates given JSON input and then calls appropriate method to
195 * create, to delete or to set Rolemapping object based on the given input.
196 *
197 */
Ed Tanous23a21a12020-07-25 04:45:05 +0000198inline void handleRoleMapPatch(
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ratan Gupta06785242019-07-26 22:30:16 +0530200 const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
Ed Tanousf23b7292020-10-15 09:41:17 -0700201 const std::string& serverType, const std::vector<nlohmann::json>& input)
Ratan Gupta06785242019-07-26 22:30:16 +0530202{
203 for (size_t index = 0; index < input.size(); index++)
204 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700205 const nlohmann::json& thisJson = input[index];
Ratan Gupta06785242019-07-26 22:30:16 +0530206
207 if (thisJson.is_null())
208 {
209 // delete the existing object
210 if (index < roleMapObjData.size())
211 {
212 crow::connections::systemBus->async_method_call(
213 [asyncResp, roleMapObjData, serverType,
214 index](const boost::system::error_code ec) {
215 if (ec)
216 {
217 BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
218 messages::internalError(asyncResp->res);
219 return;
220 }
221 asyncResp->res
222 .jsonValue[serverType]["RemoteRoleMapping"][index] =
223 nullptr;
224 },
225 ldapDbusService, roleMapObjData[index].first,
226 "xyz.openbmc_project.Object.Delete", "Delete");
227 }
228 else
229 {
230 BMCWEB_LOG_ERROR << "Can't delete the object";
231 messages::propertyValueTypeError(
Ed Tanous71f52d92021-02-19 08:51:17 -0800232 asyncResp->res,
233 thisJson.dump(2, ' ', true,
234 nlohmann::json::error_handler_t::replace),
Ratan Gupta06785242019-07-26 22:30:16 +0530235 "RemoteRoleMapping/" + std::to_string(index));
236 return;
237 }
238 }
239 else if (thisJson.empty())
240 {
241 // Don't do anything for the empty objects,parse next json
242 // eg {"RemoteRoleMapping",[{}]}
243 }
244 else
245 {
246 // update/create the object
247 std::optional<std::string> remoteGroup;
248 std::optional<std::string> localRole;
249
Ed Tanousf23b7292020-10-15 09:41:17 -0700250 // This is a copy, but it's required in this case because of how
251 // readJson is structured
252 nlohmann::json thisJsonCopy = thisJson;
253 if (!json_util::readJson(thisJsonCopy, asyncResp->res,
254 "RemoteGroup", remoteGroup, "LocalRole",
255 localRole))
Ratan Gupta06785242019-07-26 22:30:16 +0530256 {
257 continue;
258 }
259
260 // Update existing RoleMapping Object
261 if (index < roleMapObjData.size())
262 {
263 BMCWEB_LOG_DEBUG << "Update Role Map Object";
264 // If "RemoteGroup" info is provided
265 if (remoteGroup)
266 {
267 crow::connections::systemBus->async_method_call(
268 [asyncResp, roleMapObjData, serverType, index,
269 remoteGroup](const boost::system::error_code ec) {
270 if (ec)
271 {
272 BMCWEB_LOG_ERROR << "DBUS response error: "
273 << ec;
274 messages::internalError(asyncResp->res);
275 return;
276 }
277 asyncResp->res
278 .jsonValue[serverType]["RemoteRoleMapping"]
279 [index]["RemoteGroup"] = *remoteGroup;
280 },
281 ldapDbusService, roleMapObjData[index].first,
282 propertyInterface, "Set",
283 "xyz.openbmc_project.User.PrivilegeMapperEntry",
284 "GroupName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800285 dbus::utility::DbusVariantType(
286 std::move(*remoteGroup)));
Ratan Gupta06785242019-07-26 22:30:16 +0530287 }
288
289 // If "LocalRole" info is provided
290 if (localRole)
291 {
292 crow::connections::systemBus->async_method_call(
293 [asyncResp, roleMapObjData, serverType, index,
294 localRole](const boost::system::error_code ec) {
295 if (ec)
296 {
297 BMCWEB_LOG_ERROR << "DBUS response error: "
298 << ec;
299 messages::internalError(asyncResp->res);
300 return;
301 }
302 asyncResp->res
303 .jsonValue[serverType]["RemoteRoleMapping"]
304 [index]["LocalRole"] = *localRole;
305 },
306 ldapDbusService, roleMapObjData[index].first,
307 propertyInterface, "Set",
308 "xyz.openbmc_project.User.PrivilegeMapperEntry",
309 "Privilege",
Ed Tanous168e20c2021-12-13 14:39:53 -0800310 dbus::utility::DbusVariantType(
Ratan Gupta06785242019-07-26 22:30:16 +0530311 getPrivilegeFromRoleId(std::move(*localRole))));
312 }
313 }
314 // Create a new RoleMapping Object.
315 else
316 {
317 BMCWEB_LOG_DEBUG
318 << "setRoleMappingProperties: Creating new Object";
319 std::string pathString =
320 "RemoteRoleMapping/" + std::to_string(index);
321
322 if (!localRole)
323 {
324 messages::propertyMissing(asyncResp->res,
325 pathString + "/LocalRole");
326 continue;
327 }
328 if (!remoteGroup)
329 {
330 messages::propertyMissing(asyncResp->res,
331 pathString + "/RemoteGroup");
332 continue;
333 }
334
335 std::string dbusObjectPath;
336 if (serverType == "ActiveDirectory")
337 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700338 dbusObjectPath = adConfigObject;
Ratan Gupta06785242019-07-26 22:30:16 +0530339 }
340 else if (serverType == "LDAP")
341 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000342 dbusObjectPath = ldapConfigObjectName;
Ratan Gupta06785242019-07-26 22:30:16 +0530343 }
344
345 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
346 << ",LocalRole=" << *localRole;
347
348 crow::connections::systemBus->async_method_call(
Ed Tanous271584a2019-07-09 16:24:22 -0700349 [asyncResp, serverType, localRole,
Ratan Gupta06785242019-07-26 22:30:16 +0530350 remoteGroup](const boost::system::error_code ec) {
351 if (ec)
352 {
353 BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
354 messages::internalError(asyncResp->res);
355 return;
356 }
357 nlohmann::json& remoteRoleJson =
358 asyncResp->res
359 .jsonValue[serverType]["RemoteRoleMapping"];
360 remoteRoleJson.push_back(
361 {{"LocalRole", *localRole},
362 {"RemoteGroup", *remoteGroup}});
363 },
364 ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
Ed Tanous3174e4d2020-10-07 11:41:22 -0700365 "Create", *remoteGroup,
Ratan Gupta06785242019-07-26 22:30:16 +0530366 getPrivilegeFromRoleId(std::move(*localRole)));
367 }
368 }
369 }
370}
371
372/**
Ratan Gupta6973a582018-12-13 18:25:44 +0530373 * Function that retrieves all properties for LDAP config object
374 * into JSON
375 */
376template <typename CallbackFunc>
377inline void getLDAPConfigData(const std::string& ldapType,
378 CallbackFunc&& callback)
379{
Ratan Guptaab828d72019-04-22 14:18:33 +0530380
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600381 const std::array<const char*, 2> interfaces = {ldapEnableInterface,
Ratan Gupta6973a582018-12-13 18:25:44 +0530382 ldapConfigInterface};
383
384 crow::connections::systemBus->async_method_call(
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600385 [callback, ldapType](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800386 const dbus::utility::MapperGetObject& resp) {
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600387 if (ec || resp.empty())
388 {
George Liu0fda0f12021-11-16 10:06:17 +0800389 BMCWEB_LOG_ERROR
390 << "DBUS response error during getting of service name: "
391 << ec;
Ed Tanous23a21a12020-07-25 04:45:05 +0000392 LDAPConfigData empty{};
393 callback(false, empty, ldapType);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600394 return;
395 }
396 std::string service = resp.begin()->first;
397 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -0800398 [callback, ldapType](
399 const boost::system::error_code errorCode,
400 const dbus::utility::ManagedObjectType& ldapObjects) {
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600401 LDAPConfigData confData{};
Ed Tanous81ce6092020-12-17 16:54:55 +0000402 if (errorCode)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600403 {
404 callback(false, confData, ldapType);
405 BMCWEB_LOG_ERROR << "D-Bus responses error: "
Ed Tanous81ce6092020-12-17 16:54:55 +0000406 << errorCode;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600407 return;
408 }
409
410 std::string ldapDbusType;
411 std::string searchString;
412
413 if (ldapType == "LDAP")
414 {
George Liu0fda0f12021-11-16 10:06:17 +0800415 ldapDbusType =
416 "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600417 searchString = "openldap";
418 }
419 else if (ldapType == "ActiveDirectory")
420 {
421 ldapDbusType =
George Liu0fda0f12021-11-16 10:06:17 +0800422 "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600423 searchString = "active_directory";
424 }
425 else
426 {
427 BMCWEB_LOG_ERROR
428 << "Can't get the DbusType for the given type="
429 << ldapType;
430 callback(false, confData, ldapType);
431 return;
432 }
433
434 std::string ldapEnableInterfaceStr = ldapEnableInterface;
435 std::string ldapConfigInterfaceStr = ldapConfigInterface;
436
437 for (const auto& object : ldapObjects)
438 {
439 // let's find the object whose ldap type is equal to the
440 // given type
441 if (object.first.str.find(searchString) ==
442 std::string::npos)
443 {
444 continue;
445 }
446
447 for (const auto& interface : object.second)
448 {
449 if (interface.first == ldapEnableInterfaceStr)
450 {
451 // rest of the properties are string.
452 for (const auto& property : interface.second)
453 {
454 if (property.first == "Enabled")
455 {
456 const bool* value =
457 std::get_if<bool>(&property.second);
458 if (value == nullptr)
459 {
460 continue;
461 }
462 confData.serviceEnabled = *value;
463 break;
464 }
465 }
466 }
467 else if (interface.first == ldapConfigInterfaceStr)
468 {
469
470 for (const auto& property : interface.second)
471 {
Ed Tanous271584a2019-07-09 16:24:22 -0700472 const std::string* strValue =
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600473 std::get_if<std::string>(
474 &property.second);
Ed Tanous271584a2019-07-09 16:24:22 -0700475 if (strValue == nullptr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600476 {
477 continue;
478 }
479 if (property.first == "LDAPServerURI")
480 {
Ed Tanous271584a2019-07-09 16:24:22 -0700481 confData.uri = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600482 }
483 else if (property.first == "LDAPBindDN")
484 {
Ed Tanous271584a2019-07-09 16:24:22 -0700485 confData.bindDN = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600486 }
487 else if (property.first == "LDAPBaseDN")
488 {
Ed Tanous271584a2019-07-09 16:24:22 -0700489 confData.baseDN = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600490 }
491 else if (property.first ==
492 "LDAPSearchScope")
493 {
Ed Tanous271584a2019-07-09 16:24:22 -0700494 confData.searchScope = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600495 }
496 else if (property.first ==
497 "GroupNameAttribute")
498 {
Ed Tanous271584a2019-07-09 16:24:22 -0700499 confData.groupAttribute = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600500 }
501 else if (property.first ==
502 "UserNameAttribute")
503 {
Ed Tanous271584a2019-07-09 16:24:22 -0700504 confData.userNameAttribute = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600505 }
506 else if (property.first == "LDAPType")
507 {
Ed Tanous271584a2019-07-09 16:24:22 -0700508 confData.serverType = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600509 }
510 }
511 }
George Liu0fda0f12021-11-16 10:06:17 +0800512 else if (
513 interface.first ==
514 "xyz.openbmc_project.User.PrivilegeMapperEntry")
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600515 {
516 LDAPRoleMapData roleMapData{};
517 for (const auto& property : interface.second)
518 {
Ed Tanous271584a2019-07-09 16:24:22 -0700519 const std::string* strValue =
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600520 std::get_if<std::string>(
521 &property.second);
522
Ed Tanous271584a2019-07-09 16:24:22 -0700523 if (strValue == nullptr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600524 {
525 continue;
526 }
527
528 if (property.first == "GroupName")
529 {
Ed Tanous271584a2019-07-09 16:24:22 -0700530 roleMapData.groupName = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600531 }
532 else if (property.first == "Privilege")
533 {
Ed Tanous271584a2019-07-09 16:24:22 -0700534 roleMapData.privilege = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600535 }
536 }
537
Ed Tanous0f0353b2019-10-24 11:37:51 -0700538 confData.groupRoleList.emplace_back(
539 object.first.str, roleMapData);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600540 }
541 }
542 }
543 callback(true, confData, ldapType);
544 },
545 service, ldapRootObject, dbusObjManagerIntf,
546 "GetManagedObjects");
547 },
548 mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
Ed Tanous23a21a12020-07-25 04:45:05 +0000549 ldapConfigObjectName, interfaces);
Ratan Gupta6973a582018-12-13 18:25:44 +0530550}
551
Ed Tanous6c51eab2021-06-03 12:30:29 -0700552/**
553 * @brief parses the authentication section under the LDAP
554 * @param input JSON data
555 * @param asyncResp pointer to the JSON response
556 * @param userName userName to be filled from the given JSON.
557 * @param password password to be filled from the given JSON.
558 */
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700559inline void parseLDAPAuthenticationJson(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700560 nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
561 std::optional<std::string>& username, std::optional<std::string>& password)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562{
Ed Tanous6c51eab2021-06-03 12:30:29 -0700563 std::optional<std::string> authType;
564
565 if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
566 authType, "Username", username, "Password",
567 password))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700568 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700569 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700571 if (!authType)
Ratan Gupta8a07d282019-03-16 08:33:47 +0530572 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700573 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530574 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700575 if (*authType != "UsernameAndPassword")
Ratan Gupta8a07d282019-03-16 08:33:47 +0530576 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700577 messages::propertyValueNotInList(asyncResp->res, *authType,
578 "AuthenticationType");
579 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530580 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700581}
582/**
583 * @brief parses the LDAPService section under the LDAP
584 * @param input JSON data
585 * @param asyncResp pointer to the JSON response
586 * @param baseDNList baseDN to be filled from the given JSON.
587 * @param userNameAttribute userName to be filled from the given JSON.
588 * @param groupaAttribute password to be filled from the given JSON.
589 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530590
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700591inline void
592 parseLDAPServiceJson(nlohmann::json input,
593 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
594 std::optional<std::vector<std::string>>& baseDNList,
595 std::optional<std::string>& userNameAttribute,
596 std::optional<std::string>& groupsAttribute)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700597{
598 std::optional<nlohmann::json> searchSettings;
599
600 if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
601 searchSettings))
Ratan Gupta8a07d282019-03-16 08:33:47 +0530602 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700603 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530604 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700605 if (!searchSettings)
Ratan Gupta8a07d282019-03-16 08:33:47 +0530606 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700607 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530608 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700609 if (!json_util::readJson(*searchSettings, asyncResp->res,
610 "BaseDistinguishedNames", baseDNList,
611 "UsernameAttribute", userNameAttribute,
612 "GroupsAttribute", groupsAttribute))
Ratan Gupta8a07d282019-03-16 08:33:47 +0530613 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700614 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530615 }
Ed Tanous6c51eab2021-06-03 12:30:29 -0700616}
617/**
618 * @brief updates the LDAP server address and updates the
619 json response with the new value.
620 * @param serviceAddressList address to be updated.
621 * @param asyncResp pointer to the JSON response
622 * @param ldapServerElementName Type of LDAP
623 server(openLDAP/ActiveDirectory)
624 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530625
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700626inline void handleServiceAddressPatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700627 const std::vector<std::string>& serviceAddressList,
628 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
629 const std::string& ldapServerElementName,
630 const std::string& ldapConfigObject)
631{
632 crow::connections::systemBus->async_method_call(
633 [asyncResp, ldapServerElementName,
634 serviceAddressList](const boost::system::error_code ec) {
635 if (ec)
636 {
637 BMCWEB_LOG_DEBUG
638 << "Error Occurred in updating the service address";
639 messages::internalError(asyncResp->res);
640 return;
641 }
642 std::vector<std::string> modifiedserviceAddressList = {
643 serviceAddressList.front()};
644 asyncResp->res
645 .jsonValue[ldapServerElementName]["ServiceAddresses"] =
646 modifiedserviceAddressList;
647 if ((serviceAddressList).size() > 1)
648 {
649 messages::propertyValueModified(asyncResp->res,
650 "ServiceAddresses",
651 serviceAddressList.front());
652 }
653 BMCWEB_LOG_DEBUG << "Updated the service address";
654 },
655 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
656 ldapConfigInterface, "LDAPServerURI",
Ed Tanous168e20c2021-12-13 14:39:53 -0800657 dbus::utility::DbusVariantType(serviceAddressList.front()));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700658}
659/**
660 * @brief updates the LDAP Bind DN and updates the
661 json response with the new value.
662 * @param username name of the user which needs to be updated.
663 * @param asyncResp pointer to the JSON response
664 * @param ldapServerElementName Type of LDAP
665 server(openLDAP/ActiveDirectory)
666 */
Ratan Gupta8a07d282019-03-16 08:33:47 +0530667
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700668inline void
669 handleUserNamePatch(const std::string& username,
670 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
671 const std::string& ldapServerElementName,
672 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700673{
674 crow::connections::systemBus->async_method_call(
675 [asyncResp, username,
676 ldapServerElementName](const boost::system::error_code ec) {
677 if (ec)
678 {
679 BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
680 messages::internalError(asyncResp->res);
681 return;
682 }
683 asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
684 ["Username"] = username;
685 BMCWEB_LOG_DEBUG << "Updated the username";
686 },
687 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
Ed Tanous168e20c2021-12-13 14:39:53 -0800688 ldapConfigInterface, "LDAPBindDN",
689 dbus::utility::DbusVariantType(username));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700690}
691
692/**
693 * @brief updates the LDAP password
694 * @param password : ldap password which needs to be updated.
695 * @param asyncResp pointer to the JSON response
696 * @param ldapServerElementName Type of LDAP
697 * server(openLDAP/ActiveDirectory)
698 */
699
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700700inline void
701 handlePasswordPatch(const std::string& password,
702 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
703 const std::string& ldapServerElementName,
704 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700705{
706 crow::connections::systemBus->async_method_call(
707 [asyncResp, password,
708 ldapServerElementName](const boost::system::error_code ec) {
709 if (ec)
710 {
711 BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
712 messages::internalError(asyncResp->res);
713 return;
714 }
715 asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
716 ["Password"] = "";
717 BMCWEB_LOG_DEBUG << "Updated the password";
718 },
719 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
720 ldapConfigInterface, "LDAPBindDNPassword",
Ed Tanous168e20c2021-12-13 14:39:53 -0800721 dbus::utility::DbusVariantType(password));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700722}
723
724/**
725 * @brief updates the LDAP BaseDN and updates the
726 json response with the new value.
727 * @param baseDNList baseDN list which needs to be updated.
728 * @param asyncResp pointer to the JSON response
729 * @param ldapServerElementName Type of LDAP
730 server(openLDAP/ActiveDirectory)
731 */
732
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700733inline void
734 handleBaseDNPatch(const std::vector<std::string>& baseDNList,
735 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
736 const std::string& ldapServerElementName,
737 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700738{
739 crow::connections::systemBus->async_method_call(
740 [asyncResp, baseDNList,
741 ldapServerElementName](const boost::system::error_code ec) {
742 if (ec)
743 {
744 BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
745 messages::internalError(asyncResp->res);
746 return;
747 }
748 auto& serverTypeJson =
749 asyncResp->res.jsonValue[ldapServerElementName];
750 auto& searchSettingsJson =
751 serverTypeJson["LDAPService"]["SearchSettings"];
752 std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
753 searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
754 if (baseDNList.size() > 1)
755 {
756 messages::propertyValueModified(asyncResp->res,
757 "BaseDistinguishedNames",
758 baseDNList.front());
759 }
760 BMCWEB_LOG_DEBUG << "Updated the base DN";
761 },
762 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
763 ldapConfigInterface, "LDAPBaseDN",
Ed Tanous168e20c2021-12-13 14:39:53 -0800764 dbus::utility::DbusVariantType(baseDNList.front()));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700765}
766/**
767 * @brief updates the LDAP user name attribute and updates the
768 json response with the new value.
769 * @param userNameAttribute attribute to be updated.
770 * @param asyncResp pointer to the JSON response
771 * @param ldapServerElementName Type of LDAP
772 server(openLDAP/ActiveDirectory)
773 */
774
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700775inline void
776 handleUserNameAttrPatch(const std::string& userNameAttribute,
777 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
778 const std::string& ldapServerElementName,
779 const std::string& ldapConfigObject)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700780{
781 crow::connections::systemBus->async_method_call(
782 [asyncResp, userNameAttribute,
783 ldapServerElementName](const boost::system::error_code ec) {
784 if (ec)
785 {
786 BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
787 "username attribute";
788 messages::internalError(asyncResp->res);
789 return;
790 }
791 auto& serverTypeJson =
792 asyncResp->res.jsonValue[ldapServerElementName];
793 auto& searchSettingsJson =
794 serverTypeJson["LDAPService"]["SearchSettings"];
795 searchSettingsJson["UsernameAttribute"] = userNameAttribute;
796 BMCWEB_LOG_DEBUG << "Updated the user name attr.";
797 },
798 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
799 ldapConfigInterface, "UserNameAttribute",
Ed Tanous168e20c2021-12-13 14:39:53 -0800800 dbus::utility::DbusVariantType(userNameAttribute));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700801}
802/**
803 * @brief updates the LDAP group attribute and updates the
804 json response with the new value.
805 * @param groupsAttribute attribute to be updated.
806 * @param asyncResp pointer to the JSON response
807 * @param ldapServerElementName Type of LDAP
808 server(openLDAP/ActiveDirectory)
809 */
810
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700811inline void handleGroupNameAttrPatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700812 const std::string& groupsAttribute,
813 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
814 const std::string& ldapServerElementName,
815 const std::string& ldapConfigObject)
816{
817 crow::connections::systemBus->async_method_call(
818 [asyncResp, groupsAttribute,
819 ldapServerElementName](const boost::system::error_code ec) {
820 if (ec)
821 {
822 BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
823 "groupname attribute";
824 messages::internalError(asyncResp->res);
825 return;
826 }
827 auto& serverTypeJson =
828 asyncResp->res.jsonValue[ldapServerElementName];
829 auto& searchSettingsJson =
830 serverTypeJson["LDAPService"]["SearchSettings"];
831 searchSettingsJson["GroupsAttribute"] = groupsAttribute;
832 BMCWEB_LOG_DEBUG << "Updated the groupname attr";
833 },
834 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
835 ldapConfigInterface, "GroupNameAttribute",
Ed Tanous168e20c2021-12-13 14:39:53 -0800836 dbus::utility::DbusVariantType(groupsAttribute));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700837}
838/**
839 * @brief updates the LDAP service enable and updates the
840 json response with the new value.
841 * @param input JSON data.
842 * @param asyncResp pointer to the JSON response
843 * @param ldapServerElementName Type of LDAP
844 server(openLDAP/ActiveDirectory)
845 */
846
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700847inline void handleServiceEnablePatch(
Ed Tanous6c51eab2021-06-03 12:30:29 -0700848 bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
849 const std::string& ldapServerElementName,
850 const std::string& ldapConfigObject)
851{
852 crow::connections::systemBus->async_method_call(
853 [asyncResp, serviceEnabled,
854 ldapServerElementName](const boost::system::error_code ec) {
855 if (ec)
856 {
857 BMCWEB_LOG_DEBUG
858 << "Error Occurred in Updating the service enable";
859 messages::internalError(asyncResp->res);
860 return;
861 }
862 asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
863 serviceEnabled;
864 BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
865 },
866 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
Ed Tanous168e20c2021-12-13 14:39:53 -0800867 ldapEnableInterface, "Enabled",
868 dbus::utility::DbusVariantType(serviceEnabled));
Ed Tanous6c51eab2021-06-03 12:30:29 -0700869}
870
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700871inline void
872 handleAuthMethodsPatch(nlohmann::json& input,
873 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous6c51eab2021-06-03 12:30:29 -0700874{
875 std::optional<bool> basicAuth;
876 std::optional<bool> cookie;
877 std::optional<bool> sessionToken;
878 std::optional<bool> xToken;
879 std::optional<bool> tls;
880
881 if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
882 "Cookie", cookie, "SessionToken", sessionToken,
883 "XToken", xToken, "TLS", tls))
Ratan Gupta8a07d282019-03-16 08:33:47 +0530884 {
Ed Tanous6c51eab2021-06-03 12:30:29 -0700885 BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
886 return;
887 }
888
889 // Make a copy of methods configuration
890 persistent_data::AuthConfigMethods authMethodsConfig =
891 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
892
893 if (basicAuth)
894 {
895#ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
896 messages::actionNotSupported(
George Liu0fda0f12021-11-16 10:06:17 +0800897 asyncResp->res,
898 "Setting BasicAuth when basic-auth feature is disabled");
Ed Tanous6c51eab2021-06-03 12:30:29 -0700899 return;
900#endif
901 authMethodsConfig.basic = *basicAuth;
902 }
903
904 if (cookie)
905 {
906#ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
George Liu0fda0f12021-11-16 10:06:17 +0800907 messages::actionNotSupported(
908 asyncResp->res,
909 "Setting Cookie when cookie-auth feature is disabled");
Ed Tanous6c51eab2021-06-03 12:30:29 -0700910 return;
911#endif
912 authMethodsConfig.cookie = *cookie;
913 }
914
915 if (sessionToken)
916 {
917#ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
918 messages::actionNotSupported(
George Liu0fda0f12021-11-16 10:06:17 +0800919 asyncResp->res,
920 "Setting SessionToken when session-auth feature is disabled");
Ed Tanous6c51eab2021-06-03 12:30:29 -0700921 return;
922#endif
923 authMethodsConfig.sessionToken = *sessionToken;
924 }
925
926 if (xToken)
927 {
928#ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
George Liu0fda0f12021-11-16 10:06:17 +0800929 messages::actionNotSupported(
930 asyncResp->res,
931 "Setting XToken when xtoken-auth feature is disabled");
Ed Tanous6c51eab2021-06-03 12:30:29 -0700932 return;
933#endif
934 authMethodsConfig.xtoken = *xToken;
935 }
936
937 if (tls)
938 {
939#ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
George Liu0fda0f12021-11-16 10:06:17 +0800940 messages::actionNotSupported(
941 asyncResp->res,
942 "Setting TLS when mutual-tls-auth feature is disabled");
Ed Tanous6c51eab2021-06-03 12:30:29 -0700943 return;
944#endif
945 authMethodsConfig.tls = *tls;
946 }
947
948 if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
949 !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
950 !authMethodsConfig.tls)
951 {
952 // Do not allow user to disable everything
953 messages::actionNotSupported(asyncResp->res,
954 "of disabling all available methods");
955 return;
956 }
957
958 persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
959 authMethodsConfig);
960 // Save configuration immediately
961 persistent_data::getConfig().writeData();
962
963 messages::success(asyncResp->res);
964}
965
966/**
967 * @brief Get the required values from the given JSON, validates the
968 * value and create the LDAP config object.
969 * @param input JSON data
970 * @param asyncResp pointer to the JSON response
971 * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
972 */
973
974inline void handleLDAPPatch(nlohmann::json& input,
975 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
976 const std::string& serverType)
977{
978 std::string dbusObjectPath;
979 if (serverType == "ActiveDirectory")
980 {
981 dbusObjectPath = adConfigObject;
982 }
983 else if (serverType == "LDAP")
984 {
985 dbusObjectPath = ldapConfigObjectName;
986 }
987 else
988 {
989 return;
990 }
991
992 std::optional<nlohmann::json> authentication;
993 std::optional<nlohmann::json> ldapService;
994 std::optional<std::vector<std::string>> serviceAddressList;
995 std::optional<bool> serviceEnabled;
996 std::optional<std::vector<std::string>> baseDNList;
997 std::optional<std::string> userNameAttribute;
998 std::optional<std::string> groupsAttribute;
999 std::optional<std::string> userName;
1000 std::optional<std::string> password;
1001 std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
1002
1003 if (!json_util::readJson(input, asyncResp->res, "Authentication",
1004 authentication, "LDAPService", ldapService,
1005 "ServiceAddresses", serviceAddressList,
1006 "ServiceEnabled", serviceEnabled,
1007 "RemoteRoleMapping", remoteRoleMapData))
1008 {
1009 return;
1010 }
1011
1012 if (authentication)
1013 {
1014 parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
1015 password);
1016 }
1017 if (ldapService)
1018 {
1019 parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
1020 userNameAttribute, groupsAttribute);
1021 }
1022 if (serviceAddressList)
1023 {
Ed Tanous26f69762022-01-25 09:49:11 -08001024 if (serviceAddressList->empty())
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301025 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001026 messages::propertyValueNotInList(asyncResp->res, "[]",
1027 "ServiceAddress");
Ed Tanouscb13a392020-07-25 19:02:03 +00001028 return;
1029 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001030 }
1031 if (baseDNList)
1032 {
Ed Tanous26f69762022-01-25 09:49:11 -08001033 if (baseDNList->empty())
Ratan Gupta8a07d282019-03-16 08:33:47 +05301034 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001035 messages::propertyValueNotInList(asyncResp->res, "[]",
1036 "BaseDistinguishedNames");
Ratan Gupta8a07d282019-03-16 08:33:47 +05301037 return;
1038 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001039 }
Ratan Gupta8a07d282019-03-16 08:33:47 +05301040
Ed Tanous6c51eab2021-06-03 12:30:29 -07001041 // nothing to update, then return
1042 if (!userName && !password && !serviceAddressList && !baseDNList &&
1043 !userNameAttribute && !groupsAttribute && !serviceEnabled &&
1044 !remoteRoleMapData)
1045 {
1046 return;
1047 }
1048
1049 // Get the existing resource first then keep modifying
1050 // whenever any property gets updated.
1051 getLDAPConfigData(serverType, [asyncResp, userName, password, baseDNList,
1052 userNameAttribute, groupsAttribute,
1053 serviceAddressList, serviceEnabled,
1054 dbusObjectPath, remoteRoleMapData](
1055 bool success,
1056 const LDAPConfigData& confData,
1057 const std::string& serverT) {
1058 if (!success)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301059 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001060 messages::internalError(asyncResp->res);
1061 return;
Ratan Gupta8a07d282019-03-16 08:33:47 +05301062 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001063 parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
1064 if (confData.serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301065 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001066 // Disable the service first and update the rest of
1067 // the properties.
1068 handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301069 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001070
Ratan Gupta8a07d282019-03-16 08:33:47 +05301071 if (serviceAddressList)
1072 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001073 handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
1074 dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301075 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001076 if (userName)
1077 {
1078 handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
1079 }
1080 if (password)
1081 {
1082 handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
1083 }
1084
Ratan Gupta8a07d282019-03-16 08:33:47 +05301085 if (baseDNList)
1086 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001087 handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
1088 }
1089 if (userNameAttribute)
1090 {
1091 handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
1092 dbusObjectPath);
1093 }
1094 if (groupsAttribute)
1095 {
1096 handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
1097 dbusObjectPath);
1098 }
1099 if (serviceEnabled)
1100 {
1101 // if user has given the value as true then enable
1102 // the service. if user has given false then no-op
1103 // as service is already stopped.
1104 if (*serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301105 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001106 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
1107 dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301108 }
1109 }
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001110 else
1111 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001112 // if user has not given the service enabled value
1113 // then revert it to the same state as it was
1114 // before.
1115 handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
1116 serverT, dbusObjectPath);
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001117 }
Ed Tanous04ae99e2018-09-20 15:54:36 -07001118
Ed Tanous6c51eab2021-06-03 12:30:29 -07001119 if (remoteRoleMapData)
1120 {
1121 handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
1122 *remoteRoleMapData);
1123 }
1124 });
1125}
1126
1127inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
1128 const std::string& username,
1129 std::optional<std::string> password,
1130 std::optional<bool> enabled,
1131 std::optional<std::string> roleId,
1132 std::optional<bool> locked)
1133{
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301134 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1135 tempObjPath /= username;
1136 std::string dbusObjectPath(tempObjPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001137
1138 dbus::utility::checkDbusPathExists(
1139 dbusObjectPath,
Ed Tanous11063332021-09-24 11:55:44 -07001140 [dbusObjectPath, username, password(std::move(password)),
1141 roleId(std::move(roleId)), enabled, locked,
1142 asyncResp{std::move(asyncResp)}](int rc) {
Ed Tanouse662eae2022-01-25 10:39:19 -08001143 if (rc <= 0)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001144 {
1145 messages::resourceNotFound(
1146 asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
1147 username);
1148 return;
1149 }
1150
1151 if (password)
1152 {
1153 int retval = pamUpdatePassword(username, *password);
1154
1155 if (retval == PAM_USER_UNKNOWN)
Ed Tanous04ae99e2018-09-20 15:54:36 -07001156 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001157 messages::resourceNotFound(
1158 asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
1159 username);
1160 }
1161 else if (retval == PAM_AUTHTOK_ERR)
1162 {
1163 // If password is invalid
1164 messages::propertyValueFormatError(asyncResp->res,
1165 *password, "Password");
1166 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
1167 }
1168 else if (retval != PAM_SUCCESS)
1169 {
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001170 messages::internalError(asyncResp->res);
Ed Tanous04ae99e2018-09-20 15:54:36 -07001171 return;
1172 }
Ed Tanouse7b1b622022-03-24 19:14:03 -07001173 else
1174 {
1175 messages::success(asyncResp->res);
1176 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001177 }
Ed Tanous04ae99e2018-09-20 15:54:36 -07001178
Ed Tanous6c51eab2021-06-03 12:30:29 -07001179 if (enabled)
1180 {
1181 crow::connections::systemBus->async_method_call(
1182 [asyncResp](const boost::system::error_code ec) {
1183 if (ec)
1184 {
1185 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1186 messages::internalError(asyncResp->res);
1187 return;
1188 }
1189 messages::success(asyncResp->res);
1190 return;
1191 },
Ed Tanouse05aec52022-01-25 10:28:56 -08001192 "xyz.openbmc_project.User.Manager", dbusObjectPath,
Ed Tanous6c51eab2021-06-03 12:30:29 -07001193 "org.freedesktop.DBus.Properties", "Set",
1194 "xyz.openbmc_project.User.Attributes", "UserEnabled",
Ed Tanous168e20c2021-12-13 14:39:53 -08001195 dbus::utility::DbusVariantType{*enabled});
Ed Tanous6c51eab2021-06-03 12:30:29 -07001196 }
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001197
Ed Tanous6c51eab2021-06-03 12:30:29 -07001198 if (roleId)
1199 {
1200 std::string priv = getPrivilegeFromRoleId(*roleId);
1201 if (priv.empty())
Ed Tanous04ae99e2018-09-20 15:54:36 -07001202 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001203 messages::propertyValueNotInList(asyncResp->res, *roleId,
1204 "RoleId");
1205 return;
1206 }
1207 if (priv == "priv-noaccess")
1208 {
1209 priv = "";
1210 }
1211
1212 crow::connections::systemBus->async_method_call(
1213 [asyncResp](const boost::system::error_code ec) {
1214 if (ec)
1215 {
1216 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1217 messages::internalError(asyncResp->res);
1218 return;
1219 }
1220 messages::success(asyncResp->res);
1221 },
Ed Tanouse05aec52022-01-25 10:28:56 -08001222 "xyz.openbmc_project.User.Manager", dbusObjectPath,
Ed Tanous6c51eab2021-06-03 12:30:29 -07001223 "org.freedesktop.DBus.Properties", "Set",
1224 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
Ed Tanous168e20c2021-12-13 14:39:53 -08001225 dbus::utility::DbusVariantType{priv});
Ed Tanous6c51eab2021-06-03 12:30:29 -07001226 }
1227
1228 if (locked)
1229 {
1230 // admin can unlock the account which is locked by
1231 // successive authentication failures but admin should
1232 // not be allowed to lock an account.
1233 if (*locked)
1234 {
1235 messages::propertyValueNotInList(asyncResp->res, "true",
1236 "Locked");
Ed Tanous04ae99e2018-09-20 15:54:36 -07001237 return;
1238 }
1239
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001240 crow::connections::systemBus->async_method_call(
Ed Tanous6c51eab2021-06-03 12:30:29 -07001241 [asyncResp](const boost::system::error_code ec) {
1242 if (ec)
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001243 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001244 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1245 messages::internalError(asyncResp->res);
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001246 return;
1247 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001248 messages::success(asyncResp->res);
1249 return;
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001250 },
Ed Tanouse05aec52022-01-25 10:28:56 -08001251 "xyz.openbmc_project.User.Manager", dbusObjectPath,
Ed Tanous6c51eab2021-06-03 12:30:29 -07001252 "org.freedesktop.DBus.Properties", "Set",
1253 "xyz.openbmc_project.User.Attributes",
Ed Tanous168e20c2021-12-13 14:39:53 -08001254 "UserLockedForFailedAttempt",
1255 dbus::utility::DbusVariantType{*locked});
Ed Tanous6c51eab2021-06-03 12:30:29 -07001256 }
1257 });
1258}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001259
Ed Tanous6c51eab2021-06-03 12:30:29 -07001260inline void requestAccountServiceRoutes(App& app)
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001261{
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001262
Ed Tanous6c51eab2021-06-03 12:30:29 -07001263 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Ed Tanoused398212021-06-09 17:05:54 -07001264 .privileges(redfish::privileges::getAccountService)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001265 .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1266 const std::shared_ptr<
1267 bmcweb::AsyncResp>&
1268 asyncResp) -> void {
1269 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1270 {
1271 return;
1272 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001273 const persistent_data::AuthConfigMethods& authMethodsConfig =
1274 persistent_data::SessionStore::getInstance()
1275 .getAuthMethodsConfig();
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001276
Ed Tanous6c51eab2021-06-03 12:30:29 -07001277 asyncResp->res.jsonValue = {
1278 {"@odata.id", "/redfish/v1/AccountService"},
1279 {"@odata.type", "#AccountService."
1280 "v1_5_0.AccountService"},
1281 {"Id", "AccountService"},
1282 {"Name", "Account Service"},
1283 {"Description", "Account Service"},
1284 {"ServiceEnabled", true},
1285 {"MaxPasswordLength", 20},
1286 {"Accounts",
1287 {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
1288 {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}},
1289 {"Oem",
1290 {{"OpenBMC",
1291 {{"@odata.type", "#OemAccountService.v1_0_0.AccountService"},
Ed Tanousd8e3c292021-09-21 18:01:43 -07001292 {"@odata.id", "/redfish/v1/AccountService#/Oem/OpenBMC"},
Ed Tanous6c51eab2021-06-03 12:30:29 -07001293 {"AuthMethods",
1294 {
1295 {"BasicAuth", authMethodsConfig.basic},
1296 {"SessionToken", authMethodsConfig.sessionToken},
1297 {"XToken", authMethodsConfig.xtoken},
1298 {"Cookie", authMethodsConfig.cookie},
1299 {"TLS", authMethodsConfig.tls},
Abhishek Patel72048782021-06-02 09:53:24 -05001300 }}}}}}};
1301 // /redfish/v1/AccountService/LDAP/Certificates is something only
1302 // ConfigureManager can access then only display when the user has
1303 // permissions ConfigureManager
1304 Privileges effectiveUserPrivileges =
1305 redfish::getUserPrivileges(req.userRole);
1306
1307 if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
1308 effectiveUserPrivileges))
1309 {
1310 asyncResp->res.jsonValue["LDAP"] = {
1311 {"Certificates",
1312 {{"@odata.id",
1313 "/redfish/v1/AccountService/LDAP/Certificates"}}}};
1314 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001315 crow::connections::systemBus->async_method_call(
1316 [asyncResp](
1317 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001318 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001319 if (ec)
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001320 {
1321 messages::internalError(asyncResp->res);
1322 return;
1323 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001324 BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
1325 << "properties for AccountService";
1326 for (const std::pair<std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08001327 dbus::utility::DbusVariantType>&
1328 property : propertiesList)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001329 {
1330 if (property.first == "MinPasswordLength")
1331 {
1332 const uint8_t* value =
1333 std::get_if<uint8_t>(&property.second);
1334 if (value != nullptr)
Ratan Gupta24c85422019-01-30 19:41:24 +05301335 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001336 asyncResp->res.jsonValue["MinPasswordLength"] =
1337 *value;
Ratan Gupta24c85422019-01-30 19:41:24 +05301338 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001339 }
1340 if (property.first == "AccountUnlockTimeout")
1341 {
1342 const uint32_t* value =
1343 std::get_if<uint32_t>(&property.second);
1344 if (value != nullptr)
1345 {
1346 asyncResp->res
1347 .jsonValue["AccountLockoutDuration"] =
1348 *value;
1349 }
1350 }
1351 if (property.first == "MaxLoginAttemptBeforeLockout")
1352 {
1353 const uint16_t* value =
1354 std::get_if<uint16_t>(&property.second);
1355 if (value != nullptr)
1356 {
1357 asyncResp->res
1358 .jsonValue["AccountLockoutThreshold"] =
1359 *value;
1360 }
1361 }
1362 }
1363 },
1364 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1365 "org.freedesktop.DBus.Properties", "GetAll",
1366 "xyz.openbmc_project.User.AccountPolicy");
1367
1368 auto callback = [asyncResp](bool success, LDAPConfigData& confData,
1369 const std::string& ldapType) {
1370 if (!success)
1371 {
1372 return;
1373 }
1374 parseLDAPConfigData(asyncResp->res.jsonValue, confData,
1375 ldapType);
1376 };
1377
1378 getLDAPConfigData("LDAP", callback);
1379 getLDAPConfigData("ActiveDirectory", callback);
1380 });
1381
Ed Tanousf5ffd802021-07-19 10:55:33 -07001382 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
Gunnar Mills1ec43ee2022-01-04 15:39:52 -06001383 .privileges(redfish::privileges::patchAccountService)
Ed Tanousf5ffd802021-07-19 10:55:33 -07001384 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001385 [&app](
1386 const crow::Request& req,
1387 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
1388 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1389 {
1390 return;
1391 }
Ed Tanousf5ffd802021-07-19 10:55:33 -07001392 std::optional<uint32_t> unlockTimeout;
1393 std::optional<uint16_t> lockoutThreshold;
Paul Fertseref73ad02022-01-21 19:44:40 +00001394 std::optional<uint8_t> minPasswordLength;
Ed Tanousf5ffd802021-07-19 10:55:33 -07001395 std::optional<uint16_t> maxPasswordLength;
1396 std::optional<nlohmann::json> ldapObject;
1397 std::optional<nlohmann::json> activeDirectoryObject;
1398 std::optional<nlohmann::json> oemObject;
1399
Willy Tu15ed6782021-12-14 11:03:16 -08001400 if (!json_util::readJsonPatch(
Ed Tanousf5ffd802021-07-19 10:55:33 -07001401 req, asyncResp->res, "AccountLockoutDuration",
1402 unlockTimeout, "AccountLockoutThreshold",
1403 lockoutThreshold, "MaxPasswordLength",
1404 maxPasswordLength, "MinPasswordLength",
1405 minPasswordLength, "LDAP", ldapObject,
1406 "ActiveDirectory", activeDirectoryObject, "Oem",
1407 oemObject))
1408 {
1409 return;
1410 }
1411
1412 if (minPasswordLength)
1413 {
Paul Fertseref73ad02022-01-21 19:44:40 +00001414 crow::connections::systemBus->async_method_call(
1415 [asyncResp](const boost::system::error_code ec) {
1416 if (ec)
1417 {
1418 messages::internalError(asyncResp->res);
1419 return;
1420 }
1421 messages::success(asyncResp->res);
1422 },
1423 "xyz.openbmc_project.User.Manager",
1424 "/xyz/openbmc_project/user",
1425 "org.freedesktop.DBus.Properties", "Set",
1426 "xyz.openbmc_project.User.AccountPolicy",
1427 "MinPasswordLength",
1428 dbus::utility::DbusVariantType(*minPasswordLength));
Ed Tanousf5ffd802021-07-19 10:55:33 -07001429 }
1430
1431 if (maxPasswordLength)
1432 {
1433 messages::propertyNotWritable(asyncResp->res,
1434 "MaxPasswordLength");
1435 }
1436
1437 if (ldapObject)
1438 {
1439 handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1440 }
1441
1442 if (std::optional<nlohmann::json> oemOpenBMCObject;
1443 oemObject &&
1444 json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1445 oemOpenBMCObject))
1446 {
1447 if (std::optional<nlohmann::json> authMethodsObject;
1448 oemOpenBMCObject &&
1449 json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1450 "AuthMethods", authMethodsObject))
1451 {
1452 if (authMethodsObject)
1453 {
1454 handleAuthMethodsPatch(*authMethodsObject,
1455 asyncResp);
1456 }
1457 }
1458 }
1459
1460 if (activeDirectoryObject)
1461 {
1462 handleLDAPPatch(*activeDirectoryObject, asyncResp,
1463 "ActiveDirectory");
1464 }
1465
1466 if (unlockTimeout)
1467 {
1468 crow::connections::systemBus->async_method_call(
1469 [asyncResp](const boost::system::error_code ec) {
1470 if (ec)
1471 {
1472 messages::internalError(asyncResp->res);
1473 return;
1474 }
1475 messages::success(asyncResp->res);
1476 },
1477 "xyz.openbmc_project.User.Manager",
1478 "/xyz/openbmc_project/user",
1479 "org.freedesktop.DBus.Properties", "Set",
1480 "xyz.openbmc_project.User.AccountPolicy",
1481 "AccountUnlockTimeout",
Ed Tanous168e20c2021-12-13 14:39:53 -08001482 dbus::utility::DbusVariantType(*unlockTimeout));
Ed Tanousf5ffd802021-07-19 10:55:33 -07001483 }
1484 if (lockoutThreshold)
1485 {
1486 crow::connections::systemBus->async_method_call(
1487 [asyncResp](const boost::system::error_code ec) {
1488 if (ec)
1489 {
1490 messages::internalError(asyncResp->res);
1491 return;
1492 }
1493 messages::success(asyncResp->res);
1494 },
1495 "xyz.openbmc_project.User.Manager",
1496 "/xyz/openbmc_project/user",
1497 "org.freedesktop.DBus.Properties", "Set",
1498 "xyz.openbmc_project.User.AccountPolicy",
1499 "MaxLoginAttemptBeforeLockout",
Ed Tanous168e20c2021-12-13 14:39:53 -08001500 dbus::utility::DbusVariantType(*lockoutThreshold));
Ed Tanousf5ffd802021-07-19 10:55:33 -07001501 }
1502 });
1503
Ed Tanous6c51eab2021-06-03 12:30:29 -07001504 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanoused398212021-06-09 17:05:54 -07001505 .privileges(redfish::privileges::getManagerAccountCollection)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001506 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001507 [&app](
1508 const crow::Request& req,
1509 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
1510 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1511 {
1512 return;
1513 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001514 asyncResp->res.jsonValue = {
1515 {"@odata.id", "/redfish/v1/AccountService/Accounts"},
1516 {"@odata.type", "#ManagerAccountCollection."
1517 "ManagerAccountCollection"},
1518 {"Name", "Accounts Collection"},
1519 {"Description", "BMC User Accounts"}};
1520
Ed Tanous6c51eab2021-06-03 12:30:29 -07001521 Privileges effectiveUserPrivileges =
1522 redfish::getUserPrivileges(req.userRole);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001523
JunLin Chenf5e29f32021-12-08 16:47:04 +08001524 std::string thisUser;
1525 if (req.session)
1526 {
1527 thisUser = req.session->username;
1528 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001529 crow::connections::systemBus->async_method_call(
Ed Tanouscef1ddf2021-06-03 13:45:10 -07001530 [asyncResp, thisUser, effectiveUserPrivileges](
1531 const boost::system::error_code ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001532 const dbus::utility::ManagedObjectType& users) {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001533 if (ec)
1534 {
1535 messages::internalError(asyncResp->res);
Ratan Gupta24c85422019-01-30 19:41:24 +05301536 return;
Ed Tanous6c51eab2021-06-03 12:30:29 -07001537 }
Ed Tanous9712f8a2018-09-21 13:38:49 -07001538
Ed Tanouscef1ddf2021-06-03 13:45:10 -07001539 bool userCanSeeAllAccounts =
1540 effectiveUserPrivileges.isSupersetOf(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001541 {"ConfigureUsers"});
Ed Tanouscef1ddf2021-06-03 13:45:10 -07001542
1543 bool userCanSeeSelf =
1544 effectiveUserPrivileges.isSupersetOf(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001545 {"ConfigureSelf"});
Ed Tanouscef1ddf2021-06-03 13:45:10 -07001546
Ed Tanous6c51eab2021-06-03 12:30:29 -07001547 nlohmann::json& memberArray =
1548 asyncResp->res.jsonValue["Members"];
1549 memberArray = nlohmann::json::array();
Ratan Gupta24c85422019-01-30 19:41:24 +05301550
Ed Tanous9eb808c2022-01-25 10:19:23 -08001551 for (const auto& userpath : users)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001552 {
1553 std::string user = userpath.first.filename();
1554 if (user.empty())
Ratan Gupta24c85422019-01-30 19:41:24 +05301555 {
Ratan Gupta24c85422019-01-30 19:41:24 +05301556 messages::internalError(asyncResp->res);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001557 BMCWEB_LOG_ERROR << "Invalid firmware ID";
1558
Ratan Gupta24c85422019-01-30 19:41:24 +05301559 return;
1560 }
Ratan Gupta24c85422019-01-30 19:41:24 +05301561
Ed Tanous6c51eab2021-06-03 12:30:29 -07001562 // As clarified by Redfish here:
1563 // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
1564 // Users without ConfigureUsers, only see their own
1565 // account. Users with ConfigureUsers, see all
1566 // accounts.
Ed Tanouscef1ddf2021-06-03 13:45:10 -07001567 if (userCanSeeAllAccounts ||
1568 (thisUser == user && userCanSeeSelf))
Ratan Gupta24c85422019-01-30 19:41:24 +05301569 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001570 memberArray.push_back(
1571 {{"@odata.id",
1572 "/redfish/v1/AccountService/Accounts/" +
1573 user}});
Ratan Gupta24c85422019-01-30 19:41:24 +05301574 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001575 }
1576 asyncResp->res.jsonValue["Members@odata.count"] =
1577 memberArray.size();
1578 },
1579 "xyz.openbmc_project.User.Manager",
1580 "/xyz/openbmc_project/user",
1581 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ratan Gupta24c85422019-01-30 19:41:24 +05301582 });
Ed Tanous06e086d2018-09-19 17:19:52 -07001583
Ed Tanous6c51eab2021-06-03 12:30:29 -07001584 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanoused398212021-06-09 17:05:54 -07001585 .privileges(redfish::privileges::postManagerAccountCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001586 .methods(
1587 boost::beast::http::verb::post)([&app](const crow::Request& req,
1588 const std::shared_ptr<
1589 bmcweb::AsyncResp>&
1590 asyncResp) -> void {
1591 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1592 {
1593 return;
1594 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001595 std::string username;
1596 std::string password;
1597 std::optional<std::string> roleId("User");
1598 std::optional<bool> enabled = true;
Willy Tu15ed6782021-12-14 11:03:16 -08001599 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
1600 username, "Password", password,
1601 "RoleId", roleId, "Enabled", enabled))
Ed Tanous6c51eab2021-06-03 12:30:29 -07001602 {
1603 return;
1604 }
Ed Tanous06e086d2018-09-19 17:19:52 -07001605
Ed Tanous6c51eab2021-06-03 12:30:29 -07001606 std::string priv = getPrivilegeFromRoleId(*roleId);
1607 if (priv.empty())
1608 {
1609 messages::propertyValueNotInList(asyncResp->res, *roleId,
1610 "RoleId");
1611 return;
1612 }
1613 // TODO: Following override will be reverted once support in
1614 // phosphor-user-manager is added. In order to avoid dependency
1615 // issues, this is added in bmcweb, which will removed, once
1616 // phosphor-user-manager supports priv-noaccess.
1617 if (priv == "priv-noaccess")
1618 {
1619 roleId = "";
1620 }
1621 else
1622 {
1623 roleId = priv;
1624 }
Ed Tanous06e086d2018-09-19 17:19:52 -07001625
Ed Tanous6c51eab2021-06-03 12:30:29 -07001626 // Reading AllGroups property
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001627 sdbusplus::asio::getProperty<std::vector<std::string>>(
1628 *crow::connections::systemBus,
1629 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1630 "xyz.openbmc_project.User.Manager", "AllGroups",
Ed Tanous6c51eab2021-06-03 12:30:29 -07001631 [asyncResp, username, password{std::move(password)}, roleId,
Ed Tanous168e20c2021-12-13 14:39:53 -08001632 enabled](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001633 const std::vector<std::string>& allGroupsList) {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001634 if (ec)
1635 {
1636 BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1637 messages::internalError(asyncResp->res);
1638 return;
1639 }
Ed Tanous06e086d2018-09-19 17:19:52 -07001640
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001641 if (allGroupsList.empty())
Ed Tanous6c51eab2021-06-03 12:30:29 -07001642 {
1643 messages::internalError(asyncResp->res);
1644 return;
1645 }
1646
1647 crow::connections::systemBus->async_method_call(
1648 [asyncResp, username,
1649 password](const boost::system::error_code ec2,
1650 sdbusplus::message::message& m) {
1651 if (ec2)
1652 {
1653 userErrorMessageHandler(
1654 m.get_error(), asyncResp, username, "");
1655 return;
1656 }
1657
1658 if (pamUpdatePassword(username, password) !=
1659 PAM_SUCCESS)
1660 {
1661 // At this point we have a user that's been
1662 // created, but the password set
1663 // failed.Something is wrong, so delete the user
1664 // that we've already created
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301665 sdbusplus::message::object_path tempObjPath(
1666 rootUserDbusPath);
1667 tempObjPath /= username;
1668 const std::string userPath(tempObjPath);
1669
Ed Tanous6c51eab2021-06-03 12:30:29 -07001670 crow::connections::systemBus->async_method_call(
1671 [asyncResp, password](
1672 const boost::system::error_code ec3) {
1673 if (ec3)
1674 {
1675 messages::internalError(
1676 asyncResp->res);
1677 return;
1678 }
1679
1680 // If password is invalid
1681 messages::propertyValueFormatError(
1682 asyncResp->res, password,
1683 "Password");
1684 },
1685 "xyz.openbmc_project.User.Manager",
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301686 userPath,
Ed Tanous6c51eab2021-06-03 12:30:29 -07001687 "xyz.openbmc_project.Object.Delete",
1688 "Delete");
1689
1690 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
1691 return;
1692 }
1693
1694 messages::created(asyncResp->res);
1695 asyncResp->res.addHeader(
1696 "Location",
1697 "/redfish/v1/AccountService/Accounts/" +
1698 username);
1699 },
1700 "xyz.openbmc_project.User.Manager",
1701 "/xyz/openbmc_project/user",
1702 "xyz.openbmc_project.User.Manager", "CreateUser",
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001703 username, allGroupsList, *roleId, *enabled);
1704 });
Ed Tanous6c51eab2021-06-03 12:30:29 -07001705 });
1706
1707 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001708 .privileges(redfish::privileges::getManagerAccount)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001709 .methods(
1710 boost::beast::http::verb::
Ed Tanous45ca1b82022-03-25 13:07:27 -07001711 get)([&app](const crow::Request& req,
1712 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1713 const std::string& accountName) -> void {
1714 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1715 {
1716 return;
1717 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001718 if (req.session->username != accountName)
1719 {
1720 // At this point we've determined that the user is trying to
1721 // modify a user that isn't them. We need to verify that they
1722 // have permissions to modify other users, so re-run the auth
1723 // check with the same permissions, minus ConfigureSelf.
1724 Privileges effectiveUserPrivileges =
1725 redfish::getUserPrivileges(req.userRole);
1726 Privileges requiredPermissionsToChangeNonSelf = {
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001727 "ConfigureUsers", "ConfigureManager"};
Ed Tanous6c51eab2021-06-03 12:30:29 -07001728 if (!effectiveUserPrivileges.isSupersetOf(
1729 requiredPermissionsToChangeNonSelf))
Ed Tanous06e086d2018-09-19 17:19:52 -07001730 {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001731 BMCWEB_LOG_DEBUG << "GET Account denied access";
1732 messages::insufficientPrivilege(asyncResp->res);
1733 return;
1734 }
1735 }
1736
1737 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -08001738 [asyncResp,
1739 accountName](const boost::system::error_code ec,
1740 const dbus::utility::ManagedObjectType& users) {
Ed Tanous6c51eab2021-06-03 12:30:29 -07001741 if (ec)
1742 {
1743 messages::internalError(asyncResp->res);
1744 return;
1745 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001746 const auto userIt = std::find_if(
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301747 users.begin(), users.end(),
1748 [accountName](
1749 const std::pair<sdbusplus::message::object_path,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001750 dbus::utility::DBusInteracesMap>&
1751 user) {
Ed Tanous55f79e62022-01-25 11:26:16 -08001752 return accountName == user.first.filename();
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301753 });
Ed Tanous6c51eab2021-06-03 12:30:29 -07001754
Ed Tanous6c51eab2021-06-03 12:30:29 -07001755 if (userIt == users.end())
1756 {
1757 messages::resourceNotFound(
1758 asyncResp->res, "ManagerAccount", accountName);
1759 return;
1760 }
1761
1762 asyncResp->res.jsonValue = {
1763 {"@odata.type",
1764 "#ManagerAccount.v1_4_0.ManagerAccount"},
1765 {"Name", "User Account"},
1766 {"Description", "User Account"},
1767 {"Password", nullptr},
1768 {"AccountTypes", {"Redfish"}}};
1769
1770 for (const auto& interface : userIt->second)
1771 {
1772 if (interface.first ==
1773 "xyz.openbmc_project.User.Attributes")
1774 {
1775 for (const auto& property : interface.second)
1776 {
1777 if (property.first == "UserEnabled")
1778 {
1779 const bool* userEnabled =
1780 std::get_if<bool>(&property.second);
1781 if (userEnabled == nullptr)
1782 {
1783 BMCWEB_LOG_ERROR
1784 << "UserEnabled wasn't a bool";
1785 messages::internalError(asyncResp->res);
1786 return;
1787 }
1788 asyncResp->res.jsonValue["Enabled"] =
1789 *userEnabled;
1790 }
1791 else if (property.first ==
1792 "UserLockedForFailedAttempt")
1793 {
1794 const bool* userLocked =
1795 std::get_if<bool>(&property.second);
1796 if (userLocked == nullptr)
1797 {
1798 BMCWEB_LOG_ERROR << "UserLockedForF"
1799 "ailedAttempt "
1800 "wasn't a bool";
1801 messages::internalError(asyncResp->res);
1802 return;
1803 }
1804 asyncResp->res.jsonValue["Locked"] =
1805 *userLocked;
1806 asyncResp->res.jsonValue
1807 ["Locked@Redfish.AllowableValues"] = {
1808 "false"}; // can only unlock accounts
1809 }
1810 else if (property.first == "UserPrivilege")
1811 {
1812 const std::string* userPrivPtr =
1813 std::get_if<std::string>(
1814 &property.second);
1815 if (userPrivPtr == nullptr)
1816 {
1817 BMCWEB_LOG_ERROR
1818 << "UserPrivilege wasn't a "
1819 "string";
1820 messages::internalError(asyncResp->res);
1821 return;
1822 }
1823 std::string role =
1824 getRoleIdFromPrivilege(*userPrivPtr);
1825 if (role.empty())
1826 {
1827 BMCWEB_LOG_ERROR << "Invalid user role";
1828 messages::internalError(asyncResp->res);
1829 return;
1830 }
1831 asyncResp->res.jsonValue["RoleId"] = role;
1832
1833 asyncResp->res.jsonValue["Links"]["Role"] =
1834 {{"@odata.id",
George Liu0fda0f12021-11-16 10:06:17 +08001835 "/redfish/v1/AccountService/Roles/" +
Ed Tanous6c51eab2021-06-03 12:30:29 -07001836 role}};
1837 }
1838 else if (property.first ==
1839 "UserPasswordExpired")
1840 {
1841 const bool* userPasswordExpired =
1842 std::get_if<bool>(&property.second);
1843 if (userPasswordExpired == nullptr)
1844 {
George Liu0fda0f12021-11-16 10:06:17 +08001845 BMCWEB_LOG_ERROR
1846 << "UserPasswordExpired wasn't a bool";
Ed Tanous6c51eab2021-06-03 12:30:29 -07001847 messages::internalError(asyncResp->res);
1848 return;
1849 }
1850 asyncResp->res
1851 .jsonValue["PasswordChangeRequired"] =
1852 *userPasswordExpired;
1853 }
1854 }
1855 }
1856 }
1857
1858 asyncResp->res.jsonValue["@odata.id"] =
1859 "/redfish/v1/AccountService/Accounts/" + accountName;
1860 asyncResp->res.jsonValue["Id"] = accountName;
1861 asyncResp->res.jsonValue["UserName"] = accountName;
1862 },
1863 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1864 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1865 });
1866
1867 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001868 // TODO this privilege should be using the generated endpoints, but
1869 // because of the special handling of ConfigureSelf, it's not able to
1870 // yet
Ed Tanous6c51eab2021-06-03 12:30:29 -07001871 .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
1872 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001873 [&app](const crow::Request& req,
1874 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1875 const std::string& username) -> void {
1876 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1877 {
1878 return;
1879 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001880 std::optional<std::string> newUserName;
1881 std::optional<std::string> password;
1882 std::optional<bool> enabled;
1883 std::optional<std::string> roleId;
1884 std::optional<bool> locked;
Ed Tanouse9cc5172021-11-03 14:13:19 +08001885
1886 Privileges effectiveUserPrivileges =
1887 redfish::getUserPrivileges(req.userRole);
1888 Privileges configureUsers = {"ConfigureUsers"};
1889 bool userHasConfigureUsers =
1890 effectiveUserPrivileges.isSupersetOf(configureUsers);
1891 if (userHasConfigureUsers)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001892 {
Ed Tanouse9cc5172021-11-03 14:13:19 +08001893 // Users with ConfigureUsers can modify for all users
Willy Tu15ed6782021-12-14 11:03:16 -08001894 if (!json_util::readJsonPatch(
1895 req, asyncResp->res, "UserName", newUserName,
1896 "Password", password, "RoleId", roleId, "Enabled",
1897 enabled, "Locked", locked))
Ed Tanouse9cc5172021-11-03 14:13:19 +08001898 {
1899 return;
1900 }
Ed Tanous06e086d2018-09-19 17:19:52 -07001901 }
Ed Tanouse9cc5172021-11-03 14:13:19 +08001902 else
Ed Tanous6c51eab2021-06-03 12:30:29 -07001903 {
Ed Tanouse9cc5172021-11-03 14:13:19 +08001904 // ConfigureSelf accounts can only modify their own account
1905 if (username != req.session->username)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001906 {
1907 messages::insufficientPrivilege(asyncResp->res);
1908 return;
1909 }
Ed Tanouse9cc5172021-11-03 14:13:19 +08001910 // ConfigureSelf accounts can only modify their password
Willy Tu15ed6782021-12-14 11:03:16 -08001911 if (!json_util::readJsonPatch(req, asyncResp->res,
1912 "Password", password))
Ed Tanouse9cc5172021-11-03 14:13:19 +08001913 {
1914 return;
1915 }
Ed Tanous6c51eab2021-06-03 12:30:29 -07001916 }
1917
1918 // if user name is not provided in the patch method or if it
1919 // matches the user name in the URI, then we are treating it as
1920 // updating user properties other then username. If username
1921 // provided doesn't match the URI, then we are treating this as
1922 // user rename request.
1923 if (!newUserName || (newUserName.value() == username))
1924 {
1925 updateUserProperties(asyncResp, username, password, enabled,
1926 roleId, locked);
1927 return;
1928 }
1929 crow::connections::systemBus->async_method_call(
1930 [asyncResp, username, password(std::move(password)),
1931 roleId(std::move(roleId)), enabled,
1932 newUser{std::string(*newUserName)},
1933 locked](const boost::system::error_code ec,
1934 sdbusplus::message::message& m) {
1935 if (ec)
1936 {
1937 userErrorMessageHandler(m.get_error(), asyncResp,
1938 newUser, username);
1939 return;
1940 }
1941
1942 updateUserProperties(asyncResp, newUser, password,
1943 enabled, roleId, locked);
1944 },
1945 "xyz.openbmc_project.User.Manager",
1946 "/xyz/openbmc_project/user",
1947 "xyz.openbmc_project.User.Manager", "RenameUser", username,
1948 *newUserName);
1949 });
1950
1951 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001952 .privileges(redfish::privileges::deleteManagerAccount)
Ed Tanous6c51eab2021-06-03 12:30:29 -07001953 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001954 [&app](const crow::Request& req,
1955 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1956 const std::string& username) -> void {
1957 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1958 {
1959 return;
1960 }
P Dheeraj Srujan Kumarb477fd42021-12-16 07:17:51 +05301961 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1962 tempObjPath /= username;
1963 const std::string userPath(tempObjPath);
Ed Tanous6c51eab2021-06-03 12:30:29 -07001964
1965 crow::connections::systemBus->async_method_call(
1966 [asyncResp, username](const boost::system::error_code ec) {
1967 if (ec)
1968 {
1969 messages::resourceNotFound(
1970 asyncResp->res,
1971 "#ManagerAccount.v1_4_0.ManagerAccount",
1972 username);
1973 return;
1974 }
1975
1976 messages::accountRemoved(asyncResp->res);
1977 },
1978 "xyz.openbmc_project.User.Manager", userPath,
1979 "xyz.openbmc_project.Object.Delete", "Delete");
1980 });
1981}
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +01001982
Ed Tanous1abe55e2018-09-05 08:30:59 -07001983} // namespace redfish