blob: 30f34fa49cae69ea94eab4618fdd92e6b8ef7678 [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#include "node.hpp"
18
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 Tanousa8408792018-09-05 16:08:38 -070022#include <utils/json_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023
Ed Tanousabf2add2019-01-22 16:40:12 -080024#include <variant>
Ed Tanousb9b2e0b2018-09-13 13:47:50 -070025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace redfish
27{
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +010028
Ratan Gupta6973a582018-12-13 18:25:44 +053029constexpr const char* ldapConfigObject =
30 "/xyz/openbmc_project/user/ldap/openldap";
Ratan Guptaab828d72019-04-22 14:18:33 +053031constexpr const char* ADConfigObject =
32 "/xyz/openbmc_project/user/ldap/active_directory";
33
Ratan Gupta6973a582018-12-13 18:25:44 +053034constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
35constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
36constexpr const char* ldapConfigInterface =
37 "xyz.openbmc_project.User.Ldap.Config";
38constexpr const char* ldapCreateInterface =
39 "xyz.openbmc_project.User.Ldap.Create";
40constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
Ratan Gupta06785242019-07-26 22:30:16 +053041constexpr const char* ldapPrivMapperInterface =
42 "xyz.openbmc_project.User.PrivilegeMapper";
Ratan Gupta6973a582018-12-13 18:25:44 +053043constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
44constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
45constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
46constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
47constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
48
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060049struct LDAPRoleMapData
50{
51 std::string groupName;
52 std::string privilege;
53};
54
Ratan Gupta6973a582018-12-13 18:25:44 +053055struct LDAPConfigData
56{
57 std::string uri{};
58 std::string bindDN{};
59 std::string baseDN{};
60 std::string searchScope{};
61 std::string serverType{};
62 bool serviceEnabled = false;
63 std::string userNameAttribute{};
64 std::string groupAttribute{};
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060065 std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
Ratan Gupta6973a582018-12-13 18:25:44 +053066};
67
Patrick Williams19bd78d2020-05-13 17:38:24 -050068using DbusVariantType = std::variant<bool, int32_t, std::string>;
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +020069
70using DbusInterfaceType = boost::container::flat_map<
71 std::string, boost::container::flat_map<std::string, DbusVariantType>>;
72
73using ManagedObjectType =
74 std::vector<std::pair<sdbusplus::message::object_path, DbusInterfaceType>>;
75
Ratan Gupta6973a582018-12-13 18:25:44 +053076using GetObjectType =
77 std::vector<std::pair<std::string, std::vector<std::string>>>;
AppaRao Puli84e12cb2018-10-11 01:28:15 +053078
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060079inline std::string getRoleIdFromPrivilege(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053080{
81 if (role == "priv-admin")
82 {
83 return "Administrator";
84 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +053085 else if (role == "priv-user")
86 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053087 return "ReadOnly";
AppaRao Puli84e12cb2018-10-11 01:28:15 +053088 }
89 else if (role == "priv-operator")
90 {
91 return "Operator";
92 }
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000093 else if ((role == "") || (role == "priv-noaccess"))
94 {
95 return "NoAccess";
96 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +053097 return "";
98}
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -060099inline std::string getPrivilegeFromRoleId(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530100{
101 if (role == "Administrator")
102 {
103 return "priv-admin";
104 }
AppaRao Pulic80fee52019-10-16 14:49:36 +0530105 else if (role == "ReadOnly")
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530106 {
107 return "priv-user";
108 }
109 else if (role == "Operator")
110 {
111 return "priv-operator";
112 }
jayaprakash Mutyala96200602020-04-08 11:09:10 +0000113 else if ((role == "NoAccess") || (role == ""))
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +0000114 {
115 return "priv-noaccess";
116 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530117 return "";
118}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700119
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000120void userErrorMessageHandler(const sd_bus_error* e,
121 std::shared_ptr<AsyncResp> asyncResp,
122 const std::string& newUser,
123 const std::string& username)
124{
125 const char* errorMessage = e->name;
126 if (e == nullptr)
127 {
128 messages::internalError(asyncResp->res);
129 return;
130 }
131
132 if (strcmp(errorMessage,
133 "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
134 {
135 messages::resourceAlreadyExists(asyncResp->res,
Gunnar Mills8114bd42020-06-11 20:55:21 -0500136 "#ManagerAccount.v1_4_0.ManagerAccount",
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000137 "UserName", newUser);
138 }
139 else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
140 "UserNameDoesNotExist") == 0)
141 {
142 messages::resourceNotFound(
Gunnar Mills8114bd42020-06-11 20:55:21 -0500143 asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount", username);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +0000144 }
145 else if (strcmp(errorMessage,
146 "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
147 {
148 messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
149 }
150 else if (strcmp(errorMessage,
151 "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
152 {
153 messages::createLimitReachedForResource(asyncResp->res);
154 }
155 else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
156 "UserNameGroupFail") == 0)
157 {
158 messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
159 }
160 else
161 {
162 messages::internalError(asyncResp->res);
163 }
164
165 return;
166}
167
Ratan Gupta6973a582018-12-13 18:25:44 +0530168void parseLDAPConfigData(nlohmann::json& json_response,
Ratan Guptaab828d72019-04-22 14:18:33 +0530169 const LDAPConfigData& confData,
170 const std::string& ldapType)
Ratan Gupta6973a582018-12-13 18:25:44 +0530171{
Ratan Guptaab828d72019-04-22 14:18:33 +0530172 std::string service =
173 (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
Marri Devender Rao37cce912019-02-20 01:05:22 -0600174 nlohmann::json ldap = {
Ratan Gupta6973a582018-12-13 18:25:44 +0530175 {"ServiceEnabled", confData.serviceEnabled},
176 {"ServiceAddresses", nlohmann::json::array({confData.uri})},
177 {"Authentication",
178 {{"AuthenticationType", "UsernameAndPassword"},
Ratan Gupta6973a582018-12-13 18:25:44 +0530179 {"Username", confData.bindDN},
180 {"Password", nullptr}}},
181 {"LDAPService",
182 {{"SearchSettings",
183 {{"BaseDistinguishedNames",
184 nlohmann::json::array({confData.baseDN})},
185 {"UsernameAttribute", confData.userNameAttribute},
186 {"GroupsAttribute", confData.groupAttribute}}}}},
187 };
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600188
Marri Devender Rao37cce912019-02-20 01:05:22 -0600189 json_response[ldapType].update(std::move(ldap));
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600190
191 nlohmann::json& roleMapArray = json_response[ldapType]["RemoteRoleMapping"];
192 roleMapArray = nlohmann::json::array();
193 for (auto& obj : confData.groupRoleList)
194 {
195 BMCWEB_LOG_DEBUG << "Pushing the data groupName="
196 << obj.second.groupName << "\n";
197 roleMapArray.push_back(
198 {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
199 nlohmann::json::array(
200 {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
201 }
Ratan Gupta6973a582018-12-13 18:25:44 +0530202}
203
204/**
Ratan Gupta06785242019-07-26 22:30:16 +0530205 * @brief validates given JSON input and then calls appropriate method to
206 * create, to delete or to set Rolemapping object based on the given input.
207 *
208 */
209static void handleRoleMapPatch(
210 const std::shared_ptr<AsyncResp>& asyncResp,
211 const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
212 const std::string& serverType, std::vector<nlohmann::json>& input)
213{
214 for (size_t index = 0; index < input.size(); index++)
215 {
216 nlohmann::json& thisJson = input[index];
217
218 if (thisJson.is_null())
219 {
220 // delete the existing object
221 if (index < roleMapObjData.size())
222 {
223 crow::connections::systemBus->async_method_call(
224 [asyncResp, roleMapObjData, serverType,
225 index](const boost::system::error_code ec) {
226 if (ec)
227 {
228 BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
229 messages::internalError(asyncResp->res);
230 return;
231 }
232 asyncResp->res
233 .jsonValue[serverType]["RemoteRoleMapping"][index] =
234 nullptr;
235 },
236 ldapDbusService, roleMapObjData[index].first,
237 "xyz.openbmc_project.Object.Delete", "Delete");
238 }
239 else
240 {
241 BMCWEB_LOG_ERROR << "Can't delete the object";
242 messages::propertyValueTypeError(
243 asyncResp->res, thisJson.dump(),
244 "RemoteRoleMapping/" + std::to_string(index));
245 return;
246 }
247 }
248 else if (thisJson.empty())
249 {
250 // Don't do anything for the empty objects,parse next json
251 // eg {"RemoteRoleMapping",[{}]}
252 }
253 else
254 {
255 // update/create the object
256 std::optional<std::string> remoteGroup;
257 std::optional<std::string> localRole;
258
259 if (!json_util::readJson(thisJson, asyncResp->res, "RemoteGroup",
260 remoteGroup, "LocalRole", localRole))
261 {
262 continue;
263 }
264
265 // Update existing RoleMapping Object
266 if (index < roleMapObjData.size())
267 {
268 BMCWEB_LOG_DEBUG << "Update Role Map Object";
269 // If "RemoteGroup" info is provided
270 if (remoteGroup)
271 {
272 crow::connections::systemBus->async_method_call(
273 [asyncResp, roleMapObjData, serverType, index,
274 remoteGroup](const boost::system::error_code ec) {
275 if (ec)
276 {
277 BMCWEB_LOG_ERROR << "DBUS response error: "
278 << ec;
279 messages::internalError(asyncResp->res);
280 return;
281 }
282 asyncResp->res
283 .jsonValue[serverType]["RemoteRoleMapping"]
284 [index]["RemoteGroup"] = *remoteGroup;
285 },
286 ldapDbusService, roleMapObjData[index].first,
287 propertyInterface, "Set",
288 "xyz.openbmc_project.User.PrivilegeMapperEntry",
289 "GroupName",
290 std::variant<std::string>(std::move(*remoteGroup)));
291 }
292
293 // If "LocalRole" info is provided
294 if (localRole)
295 {
296 crow::connections::systemBus->async_method_call(
297 [asyncResp, roleMapObjData, serverType, index,
298 localRole](const boost::system::error_code ec) {
299 if (ec)
300 {
301 BMCWEB_LOG_ERROR << "DBUS response error: "
302 << ec;
303 messages::internalError(asyncResp->res);
304 return;
305 }
306 asyncResp->res
307 .jsonValue[serverType]["RemoteRoleMapping"]
308 [index]["LocalRole"] = *localRole;
309 },
310 ldapDbusService, roleMapObjData[index].first,
311 propertyInterface, "Set",
312 "xyz.openbmc_project.User.PrivilegeMapperEntry",
313 "Privilege",
314 std::variant<std::string>(
315 getPrivilegeFromRoleId(std::move(*localRole))));
316 }
317 }
318 // Create a new RoleMapping Object.
319 else
320 {
321 BMCWEB_LOG_DEBUG
322 << "setRoleMappingProperties: Creating new Object";
323 std::string pathString =
324 "RemoteRoleMapping/" + std::to_string(index);
325
326 if (!localRole)
327 {
328 messages::propertyMissing(asyncResp->res,
329 pathString + "/LocalRole");
330 continue;
331 }
332 if (!remoteGroup)
333 {
334 messages::propertyMissing(asyncResp->res,
335 pathString + "/RemoteGroup");
336 continue;
337 }
338
339 std::string dbusObjectPath;
340 if (serverType == "ActiveDirectory")
341 {
342 dbusObjectPath = ADConfigObject;
343 }
344 else if (serverType == "LDAP")
345 {
346 dbusObjectPath = ldapConfigObject;
347 }
348
349 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
350 << ",LocalRole=" << *localRole;
351
352 crow::connections::systemBus->async_method_call(
Ed Tanous271584a2019-07-09 16:24:22 -0700353 [asyncResp, serverType, localRole,
Ratan Gupta06785242019-07-26 22:30:16 +0530354 remoteGroup](const boost::system::error_code ec) {
355 if (ec)
356 {
357 BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
358 messages::internalError(asyncResp->res);
359 return;
360 }
361 nlohmann::json& remoteRoleJson =
362 asyncResp->res
363 .jsonValue[serverType]["RemoteRoleMapping"];
364 remoteRoleJson.push_back(
365 {{"LocalRole", *localRole},
366 {"RemoteGroup", *remoteGroup}});
367 },
368 ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
369 "Create", std::move(*remoteGroup),
370 getPrivilegeFromRoleId(std::move(*localRole)));
371 }
372 }
373 }
374}
375
376/**
Ratan Gupta6973a582018-12-13 18:25:44 +0530377 * Function that retrieves all properties for LDAP config object
378 * into JSON
379 */
380template <typename CallbackFunc>
381inline void getLDAPConfigData(const std::string& ldapType,
382 CallbackFunc&& callback)
383{
Ratan Guptaab828d72019-04-22 14:18:33 +0530384
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600385 const std::array<const char*, 2> interfaces = {ldapEnableInterface,
Ratan Gupta6973a582018-12-13 18:25:44 +0530386 ldapConfigInterface};
387
388 crow::connections::systemBus->async_method_call(
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600389 [callback, ldapType](const boost::system::error_code ec,
390 const GetObjectType& resp) {
391 LDAPConfigData confData{};
392 if (ec || resp.empty())
393 {
394 BMCWEB_LOG_ERROR << "DBUS response error during getting of "
395 "service name: "
396 << ec;
397 callback(false, confData, ldapType);
398 return;
399 }
400 std::string service = resp.begin()->first;
401 crow::connections::systemBus->async_method_call(
402 [callback, ldapType](const boost::system::error_code error_code,
403 const ManagedObjectType& ldapObjects) {
404 LDAPConfigData confData{};
405 if (error_code)
406 {
407 callback(false, confData, ldapType);
408 BMCWEB_LOG_ERROR << "D-Bus responses error: "
409 << error_code;
410 return;
411 }
412
413 std::string ldapDbusType;
414 std::string searchString;
415
416 if (ldapType == "LDAP")
417 {
418 ldapDbusType = "xyz.openbmc_project.User.Ldap.Config."
419 "Type.OpenLdap";
420 searchString = "openldap";
421 }
422 else if (ldapType == "ActiveDirectory")
423 {
424 ldapDbusType =
425 "xyz.openbmc_project.User.Ldap.Config.Type."
426 "ActiveDirectory";
427 searchString = "active_directory";
428 }
429 else
430 {
431 BMCWEB_LOG_ERROR
432 << "Can't get the DbusType for the given type="
433 << ldapType;
434 callback(false, confData, ldapType);
435 return;
436 }
437
438 std::string ldapEnableInterfaceStr = ldapEnableInterface;
439 std::string ldapConfigInterfaceStr = ldapConfigInterface;
440
441 for (const auto& object : ldapObjects)
442 {
443 // let's find the object whose ldap type is equal to the
444 // given type
445 if (object.first.str.find(searchString) ==
446 std::string::npos)
447 {
448 continue;
449 }
450
451 for (const auto& interface : object.second)
452 {
453 if (interface.first == ldapEnableInterfaceStr)
454 {
455 // rest of the properties are string.
456 for (const auto& property : interface.second)
457 {
458 if (property.first == "Enabled")
459 {
460 const bool* value =
461 std::get_if<bool>(&property.second);
462 if (value == nullptr)
463 {
464 continue;
465 }
466 confData.serviceEnabled = *value;
467 break;
468 }
469 }
470 }
471 else if (interface.first == ldapConfigInterfaceStr)
472 {
473
474 for (const auto& property : interface.second)
475 {
Ed Tanous271584a2019-07-09 16:24:22 -0700476 const std::string* strValue =
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600477 std::get_if<std::string>(
478 &property.second);
Ed Tanous271584a2019-07-09 16:24:22 -0700479 if (strValue == nullptr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600480 {
481 continue;
482 }
483 if (property.first == "LDAPServerURI")
484 {
Ed Tanous271584a2019-07-09 16:24:22 -0700485 confData.uri = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600486 }
487 else if (property.first == "LDAPBindDN")
488 {
Ed Tanous271584a2019-07-09 16:24:22 -0700489 confData.bindDN = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600490 }
491 else if (property.first == "LDAPBaseDN")
492 {
Ed Tanous271584a2019-07-09 16:24:22 -0700493 confData.baseDN = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600494 }
495 else if (property.first ==
496 "LDAPSearchScope")
497 {
Ed Tanous271584a2019-07-09 16:24:22 -0700498 confData.searchScope = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600499 }
500 else if (property.first ==
501 "GroupNameAttribute")
502 {
Ed Tanous271584a2019-07-09 16:24:22 -0700503 confData.groupAttribute = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600504 }
505 else if (property.first ==
506 "UserNameAttribute")
507 {
Ed Tanous271584a2019-07-09 16:24:22 -0700508 confData.userNameAttribute = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600509 }
510 else if (property.first == "LDAPType")
511 {
Ed Tanous271584a2019-07-09 16:24:22 -0700512 confData.serverType = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600513 }
514 }
515 }
516 else if (interface.first ==
517 "xyz.openbmc_project.User."
518 "PrivilegeMapperEntry")
519 {
520 LDAPRoleMapData roleMapData{};
521 for (const auto& property : interface.second)
522 {
Ed Tanous271584a2019-07-09 16:24:22 -0700523 const std::string* strValue =
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600524 std::get_if<std::string>(
525 &property.second);
526
Ed Tanous271584a2019-07-09 16:24:22 -0700527 if (strValue == nullptr)
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600528 {
529 continue;
530 }
531
532 if (property.first == "GroupName")
533 {
Ed Tanous271584a2019-07-09 16:24:22 -0700534 roleMapData.groupName = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600535 }
536 else if (property.first == "Privilege")
537 {
Ed Tanous271584a2019-07-09 16:24:22 -0700538 roleMapData.privilege = *strValue;
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600539 }
540 }
541
Ed Tanous0f0353b2019-10-24 11:37:51 -0700542 confData.groupRoleList.emplace_back(
543 object.first.str, roleMapData);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -0600544 }
545 }
546 }
547 callback(true, confData, ldapType);
548 },
549 service, ldapRootObject, dbusObjManagerIntf,
550 "GetManagedObjects");
551 },
552 mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
553 ldapConfigObject, interfaces);
Ratan Gupta6973a582018-12-13 18:25:44 +0530554}
555
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556class AccountService : public Node
557{
558 public:
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100559 AccountService(CrowApp& app) :
560 Node(app, "/redfish/v1/AccountService/"), app(app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562 entityPrivileges = {
Gunnar Mills3c5a3762020-01-29 15:21:30 -0600563 {boost::beast::http::verb::get, {{"Login"}}},
Ed Tanous1abe55e2018-09-05 08:30:59 -0700564 {boost::beast::http::verb::head, {{"Login"}}},
565 {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
566 {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
567 {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
568 {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
569 }
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +0100570
Ed Tanous1abe55e2018-09-05 08:30:59 -0700571 private:
Ratan Gupta8a07d282019-03-16 08:33:47 +0530572 /**
573 * @brief parses the authentication section under the LDAP
574 * @param input JSON data
575 * @param asyncResp pointer to the JSON response
576 * @param userName userName to be filled from the given JSON.
577 * @param password password to be filled from the given JSON.
578 */
579 void
580 parseLDAPAuthenticationJson(nlohmann::json input,
581 const std::shared_ptr<AsyncResp>& asyncResp,
582 std::optional<std::string>& username,
583 std::optional<std::string>& password)
584 {
585 std::optional<std::string> authType;
586
587 if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
588 authType, "Username", username, "Password",
589 password))
590 {
591 return;
592 }
593 if (!authType)
594 {
595 return;
596 }
597 if (*authType != "UsernameAndPassword")
598 {
599 messages::propertyValueNotInList(asyncResp->res, *authType,
600 "AuthenticationType");
601 return;
602 }
603 }
604 /**
605 * @brief parses the LDAPService section under the LDAP
606 * @param input JSON data
607 * @param asyncResp pointer to the JSON response
608 * @param baseDNList baseDN to be filled from the given JSON.
609 * @param userNameAttribute userName to be filled from the given JSON.
610 * @param groupaAttribute password to be filled from the given JSON.
611 */
612
613 void parseLDAPServiceJson(
614 nlohmann::json input, const std::shared_ptr<AsyncResp>& asyncResp,
615 std::optional<std::vector<std::string>>& baseDNList,
616 std::optional<std::string>& userNameAttribute,
617 std::optional<std::string>& groupsAttribute)
618 {
619 std::optional<nlohmann::json> searchSettings;
620
621 if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
622 searchSettings))
623 {
624 return;
625 }
626 if (!searchSettings)
627 {
628 return;
629 }
630 if (!json_util::readJson(*searchSettings, asyncResp->res,
631 "BaseDistinguishedNames", baseDNList,
632 "UsernameAttribute", userNameAttribute,
633 "GroupsAttribute", groupsAttribute))
634 {
635 return;
636 }
637 }
638 /**
639 * @brief updates the LDAP server address and updates the
640 json response with the new value.
641 * @param serviceAddressList address to be updated.
642 * @param asyncResp pointer to the JSON response
643 * @param ldapServerElementName Type of LDAP
644 server(openLDAP/ActiveDirectory)
645 */
646
647 void handleServiceAddressPatch(
648 const std::vector<std::string>& serviceAddressList,
649 const std::shared_ptr<AsyncResp>& asyncResp,
650 const std::string& ldapServerElementName,
651 const std::string& ldapConfigObject)
652 {
653 crow::connections::systemBus->async_method_call(
654 [asyncResp, ldapServerElementName,
655 serviceAddressList](const boost::system::error_code ec) {
656 if (ec)
657 {
658 BMCWEB_LOG_DEBUG
659 << "Error Occured in updating the service address";
660 messages::internalError(asyncResp->res);
661 return;
662 }
663 std::vector<std::string> modifiedserviceAddressList = {
664 serviceAddressList.front()};
665 asyncResp->res
666 .jsonValue[ldapServerElementName]["ServiceAddresses"] =
667 modifiedserviceAddressList;
668 if ((serviceAddressList).size() > 1)
669 {
670 messages::propertyValueModified(asyncResp->res,
671 "ServiceAddresses",
672 serviceAddressList.front());
673 }
674 BMCWEB_LOG_DEBUG << "Updated the service address";
675 },
676 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
677 ldapConfigInterface, "LDAPServerURI",
678 std::variant<std::string>(serviceAddressList.front()));
679 }
680 /**
681 * @brief updates the LDAP Bind DN and updates the
682 json response with the new value.
683 * @param username name of the user which needs to be updated.
684 * @param asyncResp pointer to the JSON response
685 * @param ldapServerElementName Type of LDAP
686 server(openLDAP/ActiveDirectory)
687 */
688
689 void handleUserNamePatch(const std::string& username,
690 const std::shared_ptr<AsyncResp>& asyncResp,
691 const std::string& ldapServerElementName,
692 const std::string& ldapConfigObject)
693 {
694 crow::connections::systemBus->async_method_call(
695 [asyncResp, username,
696 ldapServerElementName](const boost::system::error_code ec) {
697 if (ec)
698 {
699 BMCWEB_LOG_DEBUG
700 << "Error occured in updating the username";
701 messages::internalError(asyncResp->res);
702 return;
703 }
704 asyncResp->res.jsonValue[ldapServerElementName]
705 ["Authentication"]["Username"] =
706 username;
707 BMCWEB_LOG_DEBUG << "Updated the username";
708 },
709 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
710 ldapConfigInterface, "LDAPBindDN",
711 std::variant<std::string>(username));
712 }
713
714 /**
715 * @brief updates the LDAP password
716 * @param password : ldap password which needs to be updated.
717 * @param asyncResp pointer to the JSON response
718 * @param ldapServerElementName Type of LDAP
719 * server(openLDAP/ActiveDirectory)
720 */
721
722 void handlePasswordPatch(const std::string& password,
723 const std::shared_ptr<AsyncResp>& asyncResp,
724 const std::string& ldapServerElementName,
725 const std::string& ldapConfigObject)
726 {
727 crow::connections::systemBus->async_method_call(
728 [asyncResp, password,
729 ldapServerElementName](const boost::system::error_code ec) {
730 if (ec)
731 {
732 BMCWEB_LOG_DEBUG
733 << "Error occured in updating the password";
734 messages::internalError(asyncResp->res);
735 return;
736 }
737 asyncResp->res.jsonValue[ldapServerElementName]
738 ["Authentication"]["Password"] = "";
739 BMCWEB_LOG_DEBUG << "Updated the password";
740 },
741 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
742 ldapConfigInterface, "LDAPBindDNPassword",
743 std::variant<std::string>(password));
744 }
745
746 /**
747 * @brief updates the LDAP BaseDN and updates the
748 json response with the new value.
749 * @param baseDNList baseDN list which needs to be updated.
750 * @param asyncResp pointer to the JSON response
751 * @param ldapServerElementName Type of LDAP
752 server(openLDAP/ActiveDirectory)
753 */
754
755 void handleBaseDNPatch(const std::vector<std::string>& baseDNList,
756 const std::shared_ptr<AsyncResp>& asyncResp,
757 const std::string& ldapServerElementName,
758 const std::string& ldapConfigObject)
759 {
760 crow::connections::systemBus->async_method_call(
761 [asyncResp, baseDNList,
762 ldapServerElementName](const boost::system::error_code ec) {
763 if (ec)
764 {
765 BMCWEB_LOG_DEBUG << "Error Occured in Updating the base DN";
766 messages::internalError(asyncResp->res);
767 return;
768 }
769 auto& serverTypeJson =
770 asyncResp->res.jsonValue[ldapServerElementName];
771 auto& searchSettingsJson =
772 serverTypeJson["LDAPService"]["SearchSettings"];
773 std::vector<std::string> modifiedBaseDNList = {
774 baseDNList.front()};
775 searchSettingsJson["BaseDistinguishedNames"] =
776 modifiedBaseDNList;
777 if (baseDNList.size() > 1)
778 {
779 messages::propertyValueModified(asyncResp->res,
780 "BaseDistinguishedNames",
781 baseDNList.front());
782 }
783 BMCWEB_LOG_DEBUG << "Updated the base DN";
784 },
785 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
786 ldapConfigInterface, "LDAPBaseDN",
787 std::variant<std::string>(baseDNList.front()));
788 }
789 /**
790 * @brief updates the LDAP user name attribute and updates the
791 json response with the new value.
792 * @param userNameAttribute attribute to be updated.
793 * @param asyncResp pointer to the JSON response
794 * @param ldapServerElementName Type of LDAP
795 server(openLDAP/ActiveDirectory)
796 */
797
798 void handleUserNameAttrPatch(const std::string& userNameAttribute,
799 const std::shared_ptr<AsyncResp>& asyncResp,
800 const std::string& ldapServerElementName,
801 const std::string& ldapConfigObject)
802 {
803 crow::connections::systemBus->async_method_call(
804 [asyncResp, userNameAttribute,
805 ldapServerElementName](const boost::system::error_code ec) {
806 if (ec)
807 {
808 BMCWEB_LOG_DEBUG << "Error Occured in Updating the "
809 "username attribute";
810 messages::internalError(asyncResp->res);
811 return;
812 }
813 auto& serverTypeJson =
814 asyncResp->res.jsonValue[ldapServerElementName];
815 auto& searchSettingsJson =
816 serverTypeJson["LDAPService"]["SearchSettings"];
817 searchSettingsJson["UsernameAttribute"] = userNameAttribute;
818 BMCWEB_LOG_DEBUG << "Updated the user name attr.";
819 },
820 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
821 ldapConfigInterface, "UserNameAttribute",
822 std::variant<std::string>(userNameAttribute));
823 }
824 /**
825 * @brief updates the LDAP group attribute and updates the
826 json response with the new value.
827 * @param groupsAttribute attribute to be updated.
828 * @param asyncResp pointer to the JSON response
829 * @param ldapServerElementName Type of LDAP
830 server(openLDAP/ActiveDirectory)
831 */
832
833 void handleGroupNameAttrPatch(const std::string& groupsAttribute,
834 const std::shared_ptr<AsyncResp>& asyncResp,
835 const std::string& ldapServerElementName,
836 const std::string& ldapConfigObject)
837 {
838 crow::connections::systemBus->async_method_call(
839 [asyncResp, groupsAttribute,
840 ldapServerElementName](const boost::system::error_code ec) {
841 if (ec)
842 {
843 BMCWEB_LOG_DEBUG << "Error Occured in Updating the "
844 "groupname attribute";
845 messages::internalError(asyncResp->res);
846 return;
847 }
848 auto& serverTypeJson =
849 asyncResp->res.jsonValue[ldapServerElementName];
850 auto& searchSettingsJson =
851 serverTypeJson["LDAPService"]["SearchSettings"];
852 searchSettingsJson["GroupsAttribute"] = groupsAttribute;
853 BMCWEB_LOG_DEBUG << "Updated the groupname attr";
854 },
855 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
856 ldapConfigInterface, "GroupNameAttribute",
857 std::variant<std::string>(groupsAttribute));
858 }
859 /**
860 * @brief updates the LDAP service enable and updates the
861 json response with the new value.
862 * @param input JSON data.
863 * @param asyncResp pointer to the JSON response
864 * @param ldapServerElementName Type of LDAP
865 server(openLDAP/ActiveDirectory)
866 */
867
868 void handleServiceEnablePatch(bool serviceEnabled,
869 const std::shared_ptr<AsyncResp>& asyncResp,
870 const std::string& ldapServerElementName,
871 const std::string& ldapConfigObject)
872 {
873 crow::connections::systemBus->async_method_call(
874 [asyncResp, serviceEnabled,
875 ldapServerElementName](const boost::system::error_code ec) {
876 if (ec)
877 {
878 BMCWEB_LOG_DEBUG
879 << "Error Occured in Updating the service enable";
880 messages::internalError(asyncResp->res);
881 return;
882 }
883 asyncResp->res
884 .jsonValue[ldapServerElementName]["ServiceEnabled"] =
885 serviceEnabled;
886 BMCWEB_LOG_DEBUG << "Updated Service enable = "
887 << serviceEnabled;
888 },
889 ldapDbusService, ldapConfigObject, propertyInterface, "Set",
890 ldapEnableInterface, "Enabled", std::variant<bool>(serviceEnabled));
891 }
892
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100893 void handleAuthMethodsPatch(nlohmann::json& input,
894 const std::shared_ptr<AsyncResp>& asyncResp)
895 {
896 std::optional<bool> basicAuth;
897 std::optional<bool> cookie;
898 std::optional<bool> sessionToken;
899 std::optional<bool> xToken;
Zbigniew Kurzynski501f1e52019-10-02 11:22:11 +0200900 std::optional<bool> tls;
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100901
902 if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
903 "Cookie", cookie, "SessionToken", sessionToken,
Zbigniew Kurzynski501f1e52019-10-02 11:22:11 +0200904 "XToken", xToken, "TLS", tls))
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100905 {
906 BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
907 return;
908 }
909
910 // Make a copy of methods configuration
911 crow::persistent_data::AuthConfigMethods authMethodsConfig =
912 crow::persistent_data::SessionStore::getInstance()
913 .getAuthMethodsConfig();
914
915 if (basicAuth)
916 {
917 authMethodsConfig.basic = *basicAuth;
918 }
919
920 if (cookie)
921 {
922 authMethodsConfig.cookie = *cookie;
923 }
924
925 if (sessionToken)
926 {
927 authMethodsConfig.sessionToken = *sessionToken;
928 }
929
930 if (xToken)
931 {
932 authMethodsConfig.xtoken = *xToken;
933 }
934
Zbigniew Kurzynski501f1e52019-10-02 11:22:11 +0200935 if (tls)
936 {
937 authMethodsConfig.tls = *tls;
938 }
939
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100940 if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
Zbigniew Kurzynski501f1e52019-10-02 11:22:11 +0200941 !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
942 !authMethodsConfig.tls)
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100943 {
944 // Do not allow user to disable everything
945 messages::actionNotSupported(asyncResp->res,
946 "of disabling all available methods");
947 return;
948 }
949
950 crow::persistent_data::SessionStore::getInstance()
951 .updateAuthMethodsConfig(authMethodsConfig);
952 // Save configuration immediately
953 app.template getMiddleware<crow::persistent_data::Middleware>()
954 .writeData();
955
956 messages::success(asyncResp->res);
957 }
958
Ratan Gupta8a07d282019-03-16 08:33:47 +0530959 /**
960 * @brief Get the required values from the given JSON, validates the
961 * value and create the LDAP config object.
962 * @param input JSON data
963 * @param asyncResp pointer to the JSON response
964 * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
965 */
966
967 void handleLDAPPatch(nlohmann::json& input,
968 const std::shared_ptr<AsyncResp>& asyncResp,
969 const crow::Request& req,
970 const std::vector<std::string>& params,
971 const std::string& serverType)
972 {
Ratan Guptaeb2bbe52019-04-22 14:27:01 +0530973 std::string dbusObjectPath;
974 if (serverType == "ActiveDirectory")
975 {
976 dbusObjectPath = ADConfigObject;
977 }
978 else if (serverType == "LDAP")
979 {
980 dbusObjectPath = ldapConfigObject;
981 }
982
Ratan Gupta8a07d282019-03-16 08:33:47 +0530983 std::optional<nlohmann::json> authentication;
984 std::optional<nlohmann::json> ldapService;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530985 std::optional<std::vector<std::string>> serviceAddressList;
986 std::optional<bool> serviceEnabled;
987 std::optional<std::vector<std::string>> baseDNList;
988 std::optional<std::string> userNameAttribute;
989 std::optional<std::string> groupsAttribute;
990 std::optional<std::string> userName;
991 std::optional<std::string> password;
Ratan Gupta06785242019-07-26 22:30:16 +0530992 std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
Ratan Gupta8a07d282019-03-16 08:33:47 +0530993
994 if (!json_util::readJson(input, asyncResp->res, "Authentication",
995 authentication, "LDAPService", ldapService,
996 "ServiceAddresses", serviceAddressList,
Ratan Gupta06785242019-07-26 22:30:16 +0530997 "ServiceEnabled", serviceEnabled,
998 "RemoteRoleMapping", remoteRoleMapData))
Ratan Gupta8a07d282019-03-16 08:33:47 +0530999 {
1000 return;
1001 }
1002
1003 if (authentication)
1004 {
1005 parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
1006 password);
1007 }
1008 if (ldapService)
1009 {
1010 parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
1011 userNameAttribute, groupsAttribute);
1012 }
Ratan Gupta8a07d282019-03-16 08:33:47 +05301013 if (serviceAddressList)
1014 {
1015 if ((*serviceAddressList).size() == 0)
1016 {
1017 messages::propertyValueNotInList(asyncResp->res, "[]",
1018 "ServiceAddress");
1019 return;
1020 }
1021 }
1022 if (baseDNList)
1023 {
1024 if ((*baseDNList).size() == 0)
1025 {
1026 messages::propertyValueNotInList(asyncResp->res, "[]",
1027 "BaseDistinguishedNames");
1028 return;
1029 }
1030 }
1031
1032 // nothing to update, then return
1033 if (!userName && !password && !serviceAddressList && !baseDNList &&
Ratan Gupta06785242019-07-26 22:30:16 +05301034 !userNameAttribute && !groupsAttribute && !serviceEnabled &&
1035 !remoteRoleMapData)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301036 {
1037 return;
1038 }
1039
1040 // Get the existing resource first then keep modifying
1041 // whenever any property gets updated.
Ratan Guptaab828d72019-04-22 14:18:33 +05301042 getLDAPConfigData(serverType, [this, asyncResp, userName, password,
1043 baseDNList, userNameAttribute,
Gunnar Millsba9dd4a2020-02-20 10:40:18 -06001044 groupsAttribute, serviceAddressList,
1045 serviceEnabled, dbusObjectPath,
1046 remoteRoleMapData](
Ratan Guptaab828d72019-04-22 14:18:33 +05301047 bool success, LDAPConfigData confData,
1048 const std::string& serverType) {
1049 if (!success)
1050 {
1051 messages::internalError(asyncResp->res);
1052 return;
1053 }
1054 parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverType);
1055 if (confData.serviceEnabled)
1056 {
1057 // Disable the service first and update the rest of
1058 // the properties.
1059 handleServiceEnablePatch(false, asyncResp, serverType,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301060 dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301061 }
Ratan Gupta8a07d282019-03-16 08:33:47 +05301062
Ratan Guptaab828d72019-04-22 14:18:33 +05301063 if (serviceAddressList)
1064 {
1065 handleServiceAddressPatch(*serviceAddressList, asyncResp,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301066 serverType, dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301067 }
1068 if (userName)
1069 {
1070 handleUserNamePatch(*userName, asyncResp, serverType,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301071 dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301072 }
1073 if (password)
1074 {
1075 handlePasswordPatch(*password, asyncResp, serverType,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301076 dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301077 }
Ratan Gupta8a07d282019-03-16 08:33:47 +05301078
Ratan Guptaab828d72019-04-22 14:18:33 +05301079 if (baseDNList)
1080 {
1081 handleBaseDNPatch(*baseDNList, asyncResp, serverType,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301082 dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301083 }
1084 if (userNameAttribute)
1085 {
1086 handleUserNameAttrPatch(*userNameAttribute, asyncResp,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301087 serverType, dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301088 }
1089 if (groupsAttribute)
1090 {
1091 handleGroupNameAttrPatch(*groupsAttribute, asyncResp,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301092 serverType, dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301093 }
1094 if (serviceEnabled)
1095 {
1096 // if user has given the value as true then enable
1097 // the service. if user has given false then no-op
1098 // as service is already stopped.
1099 if (*serviceEnabled)
Ratan Gupta8a07d282019-03-16 08:33:47 +05301100 {
Ratan Guptaab828d72019-04-22 14:18:33 +05301101 handleServiceEnablePatch(*serviceEnabled, asyncResp,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301102 serverType, dbusObjectPath);
Ratan Gupta8a07d282019-03-16 08:33:47 +05301103 }
Ratan Guptaab828d72019-04-22 14:18:33 +05301104 }
1105 else
1106 {
1107 // if user has not given the service enabled value
1108 // then revert it to the same state as it was
1109 // before.
1110 handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301111 serverType, dbusObjectPath);
Ratan Guptaab828d72019-04-22 14:18:33 +05301112 }
Ratan Gupta06785242019-07-26 22:30:16 +05301113
1114 if (remoteRoleMapData)
1115 {
1116 std::vector<nlohmann::json> remoteRoleMap =
1117 std::move(*remoteRoleMapData);
1118
1119 handleRoleMapPatch(asyncResp, confData.groupRoleList,
1120 serverType, remoteRoleMap);
1121 }
Ratan Guptaab828d72019-04-22 14:18:33 +05301122 });
Ratan Gupta8a07d282019-03-16 08:33:47 +05301123 }
Ed Tanousd4b54432019-07-17 22:51:55 +00001124
Ed Tanous1abe55e2018-09-05 08:30:59 -07001125 void doGet(crow::Response& res, const crow::Request& req,
1126 const std::vector<std::string>& params) override
1127 {
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001128 const crow::persistent_data::AuthConfigMethods& authMethodsConfig =
1129 crow::persistent_data::SessionStore::getInstance()
1130 .getAuthMethodsConfig();
1131
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301132 auto asyncResp = std::make_shared<AsyncResp>(res);
1133 res.jsonValue = {
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301134 {"@odata.id", "/redfish/v1/AccountService"},
1135 {"@odata.type", "#AccountService."
Gunnar Millsba9dd4a2020-02-20 10:40:18 -06001136 "v1_5_0.AccountService"},
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301137 {"Id", "AccountService"},
1138 {"Name", "Account Service"},
1139 {"Description", "Account Service"},
1140 {"ServiceEnabled", true},
AppaRao Puli343ff2e2019-03-24 00:42:13 +05301141 {"MaxPasswordLength", 20},
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301142 {"Accounts",
1143 {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
Marri Devender Rao37cce912019-02-20 01:05:22 -06001144 {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}},
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001145 {"Oem",
1146 {{"OpenBMC",
1147 {{"@odata.type", "#OemAccountService.v1_0_0.AccountService"},
1148 {"AuthMethods",
1149 {
1150 {"BasicAuth", authMethodsConfig.basic},
1151 {"SessionToken", authMethodsConfig.sessionToken},
1152 {"XToken", authMethodsConfig.xtoken},
1153 {"Cookie", authMethodsConfig.cookie},
Zbigniew Kurzynski501f1e52019-10-02 11:22:11 +02001154 {"TLS", authMethodsConfig.tls},
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001155 }}}}}},
Marri Devender Rao37cce912019-02-20 01:05:22 -06001156 {"LDAP",
1157 {{"Certificates",
1158 {{"@odata.id",
1159 "/redfish/v1/AccountService/LDAP/Certificates"}}}}}};
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301160 crow::connections::systemBus->async_method_call(
1161 [asyncResp](
1162 const boost::system::error_code ec,
1163 const std::vector<std::pair<
Ed Tanousabf2add2019-01-22 16:40:12 -08001164 std::string, std::variant<uint32_t, uint16_t, uint8_t>>>&
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301165 propertiesList) {
1166 if (ec)
1167 {
1168 messages::internalError(asyncResp->res);
1169 return;
1170 }
1171 BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
1172 << "properties for AccountService";
1173 for (const std::pair<std::string,
Ed Tanousabf2add2019-01-22 16:40:12 -08001174 std::variant<uint32_t, uint16_t, uint8_t>>&
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301175 property : propertiesList)
1176 {
1177 if (property.first == "MinPasswordLength")
1178 {
1179 const uint8_t* value =
Ed Tanousabf2add2019-01-22 16:40:12 -08001180 std::get_if<uint8_t>(&property.second);
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301181 if (value != nullptr)
1182 {
1183 asyncResp->res.jsonValue["MinPasswordLength"] =
1184 *value;
1185 }
1186 }
1187 if (property.first == "AccountUnlockTimeout")
1188 {
1189 const uint32_t* value =
Ed Tanousabf2add2019-01-22 16:40:12 -08001190 std::get_if<uint32_t>(&property.second);
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301191 if (value != nullptr)
1192 {
1193 asyncResp->res.jsonValue["AccountLockoutDuration"] =
1194 *value;
1195 }
1196 }
1197 if (property.first == "MaxLoginAttemptBeforeLockout")
1198 {
1199 const uint16_t* value =
Ed Tanousabf2add2019-01-22 16:40:12 -08001200 std::get_if<uint16_t>(&property.second);
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301201 if (value != nullptr)
1202 {
1203 asyncResp->res
1204 .jsonValue["AccountLockoutThreshold"] = *value;
1205 }
1206 }
1207 }
1208 },
1209 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1210 "org.freedesktop.DBus.Properties", "GetAll",
1211 "xyz.openbmc_project.User.AccountPolicy");
Ratan Gupta6973a582018-12-13 18:25:44 +05301212
Ratan Guptaab828d72019-04-22 14:18:33 +05301213 auto callback = [asyncResp](bool success, LDAPConfigData& confData,
1214 const std::string& ldapType) {
1215 parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1216 };
1217
1218 getLDAPConfigData("LDAP", callback);
1219 getLDAPConfigData("ActiveDirectory", callback);
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301220 }
Ratan Gupta6973a582018-12-13 18:25:44 +05301221
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301222 void doPatch(crow::Response& res, const crow::Request& req,
1223 const std::vector<std::string>& params) override
1224 {
1225 auto asyncResp = std::make_shared<AsyncResp>(res);
1226
1227 std::optional<uint32_t> unlockTimeout;
1228 std::optional<uint16_t> lockoutThreshold;
Ratan Gupta19fb6e72019-03-04 13:30:50 +05301229 std::optional<uint16_t> minPasswordLength;
1230 std::optional<uint16_t> maxPasswordLength;
Ratan Gupta8a07d282019-03-16 08:33:47 +05301231 std::optional<nlohmann::json> ldapObject;
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301232 std::optional<nlohmann::json> activeDirectoryObject;
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001233 std::optional<nlohmann::json> oemObject;
Ratan Gupta19fb6e72019-03-04 13:30:50 +05301234
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001235 if (!json_util::readJson(
1236 req, res, "AccountLockoutDuration", unlockTimeout,
1237 "AccountLockoutThreshold", lockoutThreshold,
1238 "MaxPasswordLength", maxPasswordLength, "MinPasswordLength",
1239 minPasswordLength, "LDAP", ldapObject, "ActiveDirectory",
1240 activeDirectoryObject, "Oem", oemObject))
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301241 {
1242 return;
1243 }
Ratan Gupta19fb6e72019-03-04 13:30:50 +05301244
1245 if (minPasswordLength)
1246 {
1247 messages::propertyNotWritable(asyncResp->res, "MinPasswordLength");
1248 }
1249
1250 if (maxPasswordLength)
1251 {
1252 messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1253 }
1254
Ratan Gupta8a07d282019-03-16 08:33:47 +05301255 if (ldapObject)
1256 {
1257 handleLDAPPatch(*ldapObject, asyncResp, req, params, "LDAP");
1258 }
1259
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001260 if (std::optional<nlohmann::json> oemOpenBMCObject;
1261 oemObject &&
1262 json_util::readJson(*oemObject, res, "OpenBMC", oemOpenBMCObject))
1263 {
1264 if (std::optional<nlohmann::json> authMethodsObject;
1265 oemOpenBMCObject &&
1266 json_util::readJson(*oemOpenBMCObject, res, "AuthMethods",
1267 authMethodsObject))
1268 {
1269 if (authMethodsObject)
1270 {
1271 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1272 }
1273 }
1274 }
1275
Ratan Guptaeb2bbe52019-04-22 14:27:01 +05301276 if (activeDirectoryObject)
1277 {
1278 handleLDAPPatch(*activeDirectoryObject, asyncResp, req, params,
1279 "ActiveDirectory");
1280 }
1281
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301282 if (unlockTimeout)
1283 {
1284 crow::connections::systemBus->async_method_call(
1285 [asyncResp](const boost::system::error_code ec) {
1286 if (ec)
1287 {
1288 messages::internalError(asyncResp->res);
1289 return;
1290 }
Ratan Guptaadd61332019-02-13 20:49:16 +05301291 messages::success(asyncResp->res);
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301292 },
1293 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1294 "org.freedesktop.DBus.Properties", "Set",
1295 "xyz.openbmc_project.User.AccountPolicy",
Ed Tanousabf2add2019-01-22 16:40:12 -08001296 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301297 }
1298 if (lockoutThreshold)
1299 {
1300 crow::connections::systemBus->async_method_call(
1301 [asyncResp](const boost::system::error_code ec) {
1302 if (ec)
1303 {
1304 messages::internalError(asyncResp->res);
1305 return;
1306 }
Ratan Guptaadd61332019-02-13 20:49:16 +05301307 messages::success(asyncResp->res);
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301308 },
1309 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1310 "org.freedesktop.DBus.Properties", "Set",
1311 "xyz.openbmc_project.User.AccountPolicy",
1312 "MaxLoginAttemptBeforeLockout",
Ed Tanousabf2add2019-01-22 16:40:12 -08001313 std::variant<uint16_t>(*lockoutThreshold));
AppaRao Puli3d958bb2018-12-25 12:45:54 +05301314 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001315 }
Zbigniew Kurzynski78158632019-11-05 12:57:37 +01001316
1317 CrowApp& app;
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +01001318};
Tanousf00032d2018-11-05 01:18:10 -03001319
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001320class AccountsCollection : public Node
1321{
1322 public:
1323 AccountsCollection(CrowApp& app) :
1324 Node(app, "/redfish/v1/AccountService/Accounts/")
1325 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001326 entityPrivileges = {
Gunnar Millsf3659102020-01-29 16:23:28 -06001327 // According to the PrivilegeRegistry, GET should actually be
1328 // "Login". A "Login" only privilege would return an empty "Members"
1329 // list. Not going to worry about this since none of the defined
1330 // roles are just "Login". E.g. Readonly is {"Login",
1331 // "ConfigureSelf"}. In the rare event anyone defines a role that
1332 // has Login but not ConfigureSelf, implement this.
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001333 {boost::beast::http::verb::get,
Gunnar Millsf3659102020-01-29 16:23:28 -06001334 {{"ConfigureUsers"}, {"ConfigureSelf"}}},
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001335 {boost::beast::http::verb::head, {{"Login"}}},
1336 {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
1337 {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
1338 {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
1339 {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
1340 }
1341
1342 private:
1343 void doGet(crow::Response& res, const crow::Request& req,
1344 const std::vector<std::string>& params) override
1345 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001346 auto asyncResp = std::make_shared<AsyncResp>(res);
Gunnar Mills94400962020-02-14 11:56:41 -06001347 res.jsonValue = {{"@odata.id", "/redfish/v1/AccountService/Accounts"},
Ed Tanous0f74e642018-11-12 15:17:05 -08001348 {"@odata.type", "#ManagerAccountCollection."
1349 "ManagerAccountCollection"},
1350 {"Name", "Accounts Collection"},
1351 {"Description", "BMC User Accounts"}};
1352
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001353 crow::connections::systemBus->async_method_call(
Gunnar Millsf3659102020-01-29 16:23:28 -06001354 [asyncResp, &req, this](const boost::system::error_code ec,
1355 const ManagedObjectType& users) {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001356 if (ec)
1357 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001358 messages::internalError(asyncResp->res);
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001359 return;
1360 }
1361
1362 nlohmann::json& memberArray =
1363 asyncResp->res.jsonValue["Members"];
1364 memberArray = nlohmann::json::array();
1365
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001366 for (auto& user : users)
1367 {
1368 const std::string& path =
1369 static_cast<const std::string&>(user.first);
1370 std::size_t lastIndex = path.rfind("/");
1371 if (lastIndex == std::string::npos)
1372 {
1373 lastIndex = 0;
1374 }
1375 else
1376 {
1377 lastIndex += 1;
1378 }
Gunnar Millsf3659102020-01-29 16:23:28 -06001379
1380 // As clarified by Redfish here:
1381 // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
1382 // Users without ConfigureUsers, only see their own account.
1383 // Users with ConfigureUsers, see all accounts.
1384 if (req.session->username == path.substr(lastIndex) ||
1385 isAllowedWithoutConfigureSelf(req))
1386 {
1387 memberArray.push_back(
1388 {{"@odata.id",
1389 "/redfish/v1/AccountService/Accounts/" +
1390 path.substr(lastIndex)}});
1391 }
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001392 }
Gunnar Millsf3659102020-01-29 16:23:28 -06001393 asyncResp->res.jsonValue["Members@odata.count"] =
1394 memberArray.size();
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001395 },
1396 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1397 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1398 }
Ed Tanous04ae99e2018-09-20 15:54:36 -07001399 void doPost(crow::Response& res, const crow::Request& req,
1400 const std::vector<std::string>& params) override
1401 {
1402 auto asyncResp = std::make_shared<AsyncResp>(res);
1403
Ed Tanous9712f8a2018-09-21 13:38:49 -07001404 std::string username;
1405 std::string password;
Ed Tanousa24526d2018-12-10 15:17:59 -08001406 std::optional<std::string> roleId("User");
1407 std::optional<bool> enabled = true;
Ed Tanous9712f8a2018-09-21 13:38:49 -07001408 if (!json_util::readJson(req, res, "UserName", username, "Password",
1409 password, "RoleId", roleId, "Enabled",
1410 enabled))
Ed Tanous04ae99e2018-09-20 15:54:36 -07001411 {
1412 return;
1413 }
1414
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001415 std::string priv = getPrivilegeFromRoleId(*roleId);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301416 if (priv.empty())
Ed Tanous04ae99e2018-09-20 15:54:36 -07001417 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001418 messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
Ed Tanous04ae99e2018-09-20 15:54:36 -07001419 return;
1420 }
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001421 // TODO: Following override will be reverted once support in
1422 // phosphor-user-manager is added. In order to avoid dependency issues,
1423 // this is added in bmcweb, which will removed, once
1424 // phosphor-user-manager supports priv-noaccess.
1425 if (priv == "priv-noaccess")
1426 {
1427 roleId = "";
1428 }
1429 else
1430 {
1431 roleId = priv;
1432 }
Ed Tanous04ae99e2018-09-20 15:54:36 -07001433
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001434 // Reading AllGroups property
Ed Tanous04ae99e2018-09-20 15:54:36 -07001435 crow::connections::systemBus->async_method_call(
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001436 [asyncResp, username, password{std::move(password)}, roleId,
1437 enabled](const boost::system::error_code ec,
1438 const std::variant<std::vector<std::string>>& allGroups) {
Ed Tanous04ae99e2018-09-20 15:54:36 -07001439 if (ec)
1440 {
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001441 BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1442 messages::internalError(asyncResp->res);
Ed Tanous04ae99e2018-09-20 15:54:36 -07001443 return;
1444 }
1445
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001446 const std::vector<std::string>* allGroupsList =
1447 std::get_if<std::vector<std::string>>(&allGroups);
1448
1449 if (allGroupsList == nullptr || allGroupsList->empty())
Ed Tanous04ae99e2018-09-20 15:54:36 -07001450 {
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001451 messages::internalError(asyncResp->res);
Ed Tanous04ae99e2018-09-20 15:54:36 -07001452 return;
1453 }
1454
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001455 crow::connections::systemBus->async_method_call(
1456 [asyncResp, username, password{std::move(password)}](
anil kumar appana0d4197e2019-06-13 15:06:23 +00001457 const boost::system::error_code ec,
1458 sdbusplus::message::message& m) {
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001459 if (ec)
1460 {
anil kumar appana0d4197e2019-06-13 15:06:23 +00001461 userErrorMessageHandler(m.get_error(), asyncResp,
1462 username, "");
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001463 return;
1464 }
1465
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001466 if (pamUpdatePassword(username, password) !=
1467 PAM_SUCCESS)
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001468 {
1469 // At this point we have a user that's been created,
1470 // but the password set failed.Something is wrong,
1471 // so delete the user that we've already created
1472 crow::connections::systemBus->async_method_call(
anil kumar appana0d4197e2019-06-13 15:06:23 +00001473 [asyncResp,
1474 password](const boost::system::error_code ec) {
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001475 if (ec)
1476 {
1477 messages::internalError(asyncResp->res);
1478 return;
1479 }
1480
anil kumar appana0d4197e2019-06-13 15:06:23 +00001481 // If password is invalid
1482 messages::propertyValueFormatError(
1483 asyncResp->res, password, "Password");
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001484 },
1485 "xyz.openbmc_project.User.Manager",
1486 "/xyz/openbmc_project/user/" + username,
1487 "xyz.openbmc_project.Object.Delete", "Delete");
1488
1489 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
1490 return;
1491 }
1492
1493 messages::created(asyncResp->res);
1494 asyncResp->res.addHeader(
1495 "Location",
1496 "/redfish/v1/AccountService/Accounts/" + username);
1497 },
1498 "xyz.openbmc_project.User.Manager",
1499 "/xyz/openbmc_project/user",
1500 "xyz.openbmc_project.User.Manager", "CreateUser", username,
1501 *allGroupsList, *roleId, *enabled);
Ed Tanous04ae99e2018-09-20 15:54:36 -07001502 },
1503 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
Ayushi Smriti599c71d2019-08-23 17:43:18 +00001504 "org.freedesktop.DBus.Properties", "Get",
1505 "xyz.openbmc_project.User.Manager", "AllGroups");
Ed Tanous04ae99e2018-09-20 15:54:36 -07001506 }
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001507};
1508
1509class ManagerAccount : public Node
1510{
1511 public:
1512 ManagerAccount(CrowApp& app) :
1513 Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
1514 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001515 entityPrivileges = {
1516 {boost::beast::http::verb::get,
1517 {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
1518 {boost::beast::http::verb::head, {{"Login"}}},
Joseph Reynolds900f9492019-11-25 15:37:29 -06001519 {boost::beast::http::verb::patch,
1520 {{"ConfigureUsers"}, {"ConfigureSelf"}}},
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001521 {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
1522 {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
1523 {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
1524 }
1525
1526 private:
1527 void doGet(crow::Response& res, const crow::Request& req,
1528 const std::vector<std::string>& params) override
1529 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001530 auto asyncResp = std::make_shared<AsyncResp>(res);
1531
1532 if (params.size() != 1)
1533 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001534 messages::internalError(asyncResp->res);
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001535 return;
1536 }
1537
Joseph Reynolds900f9492019-11-25 15:37:29 -06001538 // Perform a proper ConfigureSelf authority check. If the
1539 // user is operating on an account not their own, then their
1540 // ConfigureSelf privilege does not apply. In this case,
1541 // perform the authority check again without the user's
1542 // ConfigureSelf privilege.
1543 if (req.session->username != params[0])
1544 {
1545 if (!isAllowedWithoutConfigureSelf(req))
1546 {
1547 BMCWEB_LOG_DEBUG << "GET Account denied access";
1548 messages::insufficientPrivilege(asyncResp->res);
1549 return;
1550 }
1551 }
1552
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001553 crow::connections::systemBus->async_method_call(
1554 [asyncResp, accountName{std::string(params[0])}](
1555 const boost::system::error_code ec,
1556 const ManagedObjectType& users) {
1557 if (ec)
1558 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001559 messages::internalError(asyncResp->res);
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001560 return;
1561 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301562 auto userIt = users.begin();
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001563
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301564 for (; userIt != users.end(); userIt++)
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001565 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301566 if (boost::ends_with(userIt->first.str, "/" + accountName))
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001567 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301568 break;
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001569 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301570 }
1571 if (userIt == users.end())
1572 {
1573 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1574 accountName);
1575 return;
1576 }
Ayushi Smriti4e68c452019-09-04 14:37:55 +05301577
1578 asyncResp->res.jsonValue = {
Gunnar Mills8114bd42020-06-11 20:55:21 -05001579 {"@odata.type", "#ManagerAccount.v1_4_0.ManagerAccount"},
Ayushi Smriti4e68c452019-09-04 14:37:55 +05301580 {"Name", "User Account"},
1581 {"Description", "User Account"},
Gunnar Mills8114bd42020-06-11 20:55:21 -05001582 {"Password", nullptr},
1583 {"AccountTypes", {"Redfish"}}};
Ayushi Smriti4e68c452019-09-04 14:37:55 +05301584
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301585 for (const auto& interface : userIt->second)
1586 {
1587 if (interface.first ==
1588 "xyz.openbmc_project.User.Attributes")
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001589 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301590 for (const auto& property : interface.second)
Ed Tanous65b0dc32018-09-19 16:04:03 -07001591 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301592 if (property.first == "UserEnabled")
Ed Tanous65b0dc32018-09-19 16:04:03 -07001593 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301594 const bool* userEnabled =
Ed Tanousabf2add2019-01-22 16:40:12 -08001595 std::get_if<bool>(&property.second);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301596 if (userEnabled == nullptr)
Ed Tanous65b0dc32018-09-19 16:04:03 -07001597 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301598 BMCWEB_LOG_ERROR
1599 << "UserEnabled wasn't a bool";
1600 messages::internalError(asyncResp->res);
1601 return;
Ed Tanous65b0dc32018-09-19 16:04:03 -07001602 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301603 asyncResp->res.jsonValue["Enabled"] =
1604 *userEnabled;
1605 }
1606 else if (property.first ==
1607 "UserLockedForFailedAttempt")
1608 {
1609 const bool* userLocked =
Ed Tanousabf2add2019-01-22 16:40:12 -08001610 std::get_if<bool>(&property.second);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301611 if (userLocked == nullptr)
1612 {
1613 BMCWEB_LOG_ERROR << "UserLockedForF"
1614 "ailedAttempt "
1615 "wasn't a bool";
1616 messages::internalError(asyncResp->res);
1617 return;
1618 }
1619 asyncResp->res.jsonValue["Locked"] =
1620 *userLocked;
Ratan Gupta24c85422019-01-30 19:41:24 +05301621 asyncResp->res.jsonValue
1622 ["Locked@Redfish.AllowableValues"] = {
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001623 "false"}; // can only unlock accounts
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301624 }
1625 else if (property.first == "UserPrivilege")
1626 {
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001627 const std::string* userPrivPtr =
Ed Tanousabf2add2019-01-22 16:40:12 -08001628 std::get_if<std::string>(&property.second);
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001629 if (userPrivPtr == nullptr)
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301630 {
1631 BMCWEB_LOG_ERROR
1632 << "UserPrivilege wasn't a "
1633 "string";
1634 messages::internalError(asyncResp->res);
1635 return;
1636 }
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001637 std::string role =
1638 getRoleIdFromPrivilege(*userPrivPtr);
1639 if (role.empty())
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301640 {
1641 BMCWEB_LOG_ERROR << "Invalid user role";
1642 messages::internalError(asyncResp->res);
1643 return;
1644 }
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001645 asyncResp->res.jsonValue["RoleId"] = role;
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301646
1647 asyncResp->res.jsonValue["Links"]["Role"] = {
1648 {"@odata.id", "/redfish/v1/AccountService/"
1649 "Roles/" +
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001650 role}};
Ed Tanous65b0dc32018-09-19 16:04:03 -07001651 }
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001652 else if (property.first == "UserPasswordExpired")
1653 {
1654 const bool* userPasswordExpired =
1655 std::get_if<bool>(&property.second);
1656 if (userPasswordExpired == nullptr)
1657 {
1658 BMCWEB_LOG_ERROR << "UserPassword"
1659 "Expired "
1660 "wasn't a bool";
1661 messages::internalError(asyncResp->res);
1662 return;
1663 }
1664 asyncResp->res
1665 .jsonValue["PasswordChangeRequired"] =
1666 *userPasswordExpired;
1667 }
Ed Tanous65b0dc32018-09-19 16:04:03 -07001668 }
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001669 }
1670 }
1671
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301672 asyncResp->res.jsonValue["@odata.id"] =
1673 "/redfish/v1/AccountService/Accounts/" + accountName;
1674 asyncResp->res.jsonValue["Id"] = accountName;
1675 asyncResp->res.jsonValue["UserName"] = accountName;
Ed Tanousb9b2e0b2018-09-13 13:47:50 -07001676 },
1677 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1678 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1679 }
Ed Tanousa8408792018-09-05 16:08:38 -07001680
1681 void doPatch(crow::Response& res, const crow::Request& req,
1682 const std::vector<std::string>& params) override
1683 {
1684 auto asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanousa8408792018-09-05 16:08:38 -07001685 if (params.size() != 1)
1686 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001687 messages::internalError(asyncResp->res);
Ed Tanousa8408792018-09-05 16:08:38 -07001688 return;
1689 }
1690
Ed Tanousa24526d2018-12-10 15:17:59 -08001691 std::optional<std::string> newUserName;
1692 std::optional<std::string> password;
1693 std::optional<bool> enabled;
1694 std::optional<std::string> roleId;
Ratan Gupta24c85422019-01-30 19:41:24 +05301695 std::optional<bool> locked;
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301696 if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
Ratan Gupta24c85422019-01-30 19:41:24 +05301697 password, "RoleId", roleId, "Enabled", enabled,
1698 "Locked", locked))
Ed Tanousa8408792018-09-05 16:08:38 -07001699 {
1700 return;
1701 }
1702
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301703 const std::string& username = params[0];
Ed Tanousa8408792018-09-05 16:08:38 -07001704
Joseph Reynolds900f9492019-11-25 15:37:29 -06001705 // Perform a proper ConfigureSelf authority check. If the
1706 // session is being used to PATCH a property other than
1707 // Password, then the ConfigureSelf privilege does not apply.
1708 // If the user is operating on an account not their own, then
1709 // their ConfigureSelf privilege does not apply. In either
1710 // case, perform the authority check again without the user's
1711 // ConfigureSelf privilege.
1712 if ((username != req.session->username) ||
1713 (newUserName || enabled || roleId || locked))
1714 {
1715 if (!isAllowedWithoutConfigureSelf(req))
1716 {
1717 BMCWEB_LOG_WARNING << "PATCH Password denied access";
1718 asyncResp->res.clear();
1719 messages::insufficientPrivilege(asyncResp->res);
1720 return;
1721 }
1722 }
1723
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001724 // if user name is not provided in the patch method or if it
1725 // matches the user name in the URI, then we are treating it as updating
1726 // user properties other then username. If username provided doesn't
1727 // match the URI, then we are treating this as user rename request.
1728 if (!newUserName || (newUserName.value() == username))
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301729 {
Ratan Gupta24c85422019-01-30 19:41:24 +05301730 updateUserProperties(asyncResp, username, password, enabled, roleId,
1731 locked);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301732 return;
1733 }
1734 else
1735 {
1736 crow::connections::systemBus->async_method_call(
1737 [this, asyncResp, username, password(std::move(password)),
1738 roleId(std::move(roleId)), enabled(std::move(enabled)),
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001739 newUser{std::string(*newUserName)},
1740 locked(std::move(locked))](const boost::system::error_code ec,
1741 sdbusplus::message::message& m) {
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301742 if (ec)
Ed Tanousa8408792018-09-05 16:08:38 -07001743 {
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001744 userErrorMessageHandler(m.get_error(), asyncResp,
1745 newUser, username);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301746 return;
1747 }
1748
1749 updateUserProperties(asyncResp, newUser, password, enabled,
Ratan Gupta24c85422019-01-30 19:41:24 +05301750 roleId, locked);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301751 },
1752 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1753 "xyz.openbmc_project.User.Manager", "RenameUser", username,
1754 *newUserName);
1755 }
1756 }
1757
1758 void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
1759 const std::string& username,
Ed Tanousa24526d2018-12-10 15:17:59 -08001760 std::optional<std::string> password,
1761 std::optional<bool> enabled,
Ratan Gupta24c85422019-01-30 19:41:24 +05301762 std::optional<std::string> roleId,
1763 std::optional<bool> locked)
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301764 {
Ratan Gupta24c85422019-01-30 19:41:24 +05301765 std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
1766 dbus::utility::escapePathForDbus(dbusObjectPath);
1767
Ratan Gupta22c33712019-05-03 21:50:28 +05301768 dbus::utility::checkDbusPathExists(
Ratan Gupta24c85422019-01-30 19:41:24 +05301769 dbusObjectPath,
1770 [dbusObjectPath(std::move(dbusObjectPath)), username,
1771 password(std::move(password)), roleId(std::move(roleId)),
1772 enabled(std::move(enabled)), locked(std::move(locked)),
1773 asyncResp{std::move(asyncResp)}](int rc) {
1774 if (!rc)
1775 {
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001776 messages::resourceNotFound(
Gunnar Mills8114bd42020-06-11 20:55:21 -05001777 asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001778 username);
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301779 return;
Ratan Gupta24c85422019-01-30 19:41:24 +05301780 }
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001781
1782 if (password)
1783 {
1784 int retval = pamUpdatePassword(username, *password);
1785
1786 if (retval == PAM_USER_UNKNOWN)
1787 {
1788 messages::resourceNotFound(
1789 asyncResp->res,
Gunnar Mills8114bd42020-06-11 20:55:21 -05001790 "#ManagerAccount.v1_4_0.ManagerAccount", username);
jayaprakash Mutyala66b5ca72019-08-07 20:26:37 +00001791 }
1792 else if (retval == PAM_AUTHTOK_ERR)
1793 {
1794 // If password is invalid
1795 messages::propertyValueFormatError(
1796 asyncResp->res, *password, "Password");
1797 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
1798 }
1799 else if (retval != PAM_SUCCESS)
1800 {
1801 messages::internalError(asyncResp->res);
1802 return;
1803 }
1804 }
1805
Ratan Gupta24c85422019-01-30 19:41:24 +05301806 if (enabled)
1807 {
1808 crow::connections::systemBus->async_method_call(
1809 [asyncResp](const boost::system::error_code ec) {
1810 if (ec)
1811 {
1812 BMCWEB_LOG_ERROR << "D-Bus responses error: "
1813 << ec;
1814 messages::internalError(asyncResp->res);
1815 return;
1816 }
1817 messages::success(asyncResp->res);
1818 return;
1819 },
1820 "xyz.openbmc_project.User.Manager",
1821 dbusObjectPath.c_str(),
1822 "org.freedesktop.DBus.Properties", "Set",
1823 "xyz.openbmc_project.User.Attributes", "UserEnabled",
1824 std::variant<bool>{*enabled});
1825 }
Ed Tanous9712f8a2018-09-21 13:38:49 -07001826
Ratan Gupta24c85422019-01-30 19:41:24 +05301827 if (roleId)
1828 {
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001829 std::string priv = getPrivilegeFromRoleId(*roleId);
Ratan Gupta24c85422019-01-30 19:41:24 +05301830 if (priv.empty())
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301831 {
Ratan Gupta24c85422019-01-30 19:41:24 +05301832 messages::propertyValueNotInList(asyncResp->res,
1833 *roleId, "RoleId");
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301834 return;
1835 }
jayaprakash Mutyala96200602020-04-08 11:09:10 +00001836 if (priv == "priv-noaccess")
1837 {
1838 priv = "";
1839 }
Ratan Gupta24c85422019-01-30 19:41:24 +05301840
1841 crow::connections::systemBus->async_method_call(
1842 [asyncResp](const boost::system::error_code ec) {
1843 if (ec)
1844 {
1845 BMCWEB_LOG_ERROR << "D-Bus responses error: "
1846 << ec;
1847 messages::internalError(asyncResp->res);
1848 return;
1849 }
1850 messages::success(asyncResp->res);
1851 },
1852 "xyz.openbmc_project.User.Manager",
1853 dbusObjectPath.c_str(),
1854 "org.freedesktop.DBus.Properties", "Set",
1855 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1856 std::variant<std::string>{priv});
1857 }
1858
1859 if (locked)
1860 {
1861 // admin can unlock the account which is locked by
Nagaraju Goruganti54fc5872019-01-30 05:11:00 -06001862 // successive authentication failures but admin should
1863 // not be allowed to lock an account.
Ratan Gupta24c85422019-01-30 19:41:24 +05301864 if (*locked)
1865 {
1866 messages::propertyValueNotInList(asyncResp->res, "true",
1867 "Locked");
1868 return;
1869 }
1870
1871 crow::connections::systemBus->async_method_call(
1872 [asyncResp](const boost::system::error_code ec) {
1873 if (ec)
1874 {
1875 BMCWEB_LOG_ERROR << "D-Bus responses error: "
1876 << ec;
1877 messages::internalError(asyncResp->res);
1878 return;
1879 }
1880 messages::success(asyncResp->res);
1881 return;
1882 },
1883 "xyz.openbmc_project.User.Manager",
1884 dbusObjectPath.c_str(),
1885 "org.freedesktop.DBus.Properties", "Set",
1886 "xyz.openbmc_project.User.Attributes",
1887 "UserLockedForFailedAttempt",
Patrick Williams19bd78d2020-05-13 17:38:24 -05001888 std::variant<bool>{*locked});
Ratan Gupta24c85422019-01-30 19:41:24 +05301889 }
1890 });
Ed Tanousa8408792018-09-05 16:08:38 -07001891 }
Ed Tanous06e086d2018-09-19 17:19:52 -07001892
1893 void doDelete(crow::Response& res, const crow::Request& req,
1894 const std::vector<std::string>& params) override
1895 {
1896 auto asyncResp = std::make_shared<AsyncResp>(res);
1897
1898 if (params.size() != 1)
1899 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001900 messages::internalError(asyncResp->res);
Ed Tanous06e086d2018-09-19 17:19:52 -07001901 return;
1902 }
1903
1904 const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
1905
1906 crow::connections::systemBus->async_method_call(
1907 [asyncResp, username{std::move(params[0])}](
1908 const boost::system::error_code ec) {
1909 if (ec)
1910 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001911 messages::resourceNotFound(
Gunnar Mills8114bd42020-06-11 20:55:21 -05001912 asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
Jason M. Billsf12894f2018-10-09 12:45:45 -07001913 username);
Ed Tanous06e086d2018-09-19 17:19:52 -07001914 return;
1915 }
1916
Jason M. Billsf12894f2018-10-09 12:45:45 -07001917 messages::accountRemoved(asyncResp->res);
Ed Tanous06e086d2018-09-19 17:19:52 -07001918 },
1919 "xyz.openbmc_project.User.Manager", userPath,
1920 "xyz.openbmc_project.Object.Delete", "Delete");
1921 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +05301922};
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +01001923
Ed Tanous1abe55e2018-09-05 08:30:59 -07001924} // namespace redfish