blob: 4a38185669593afdc6631f896e188fb7574882ad [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>
Ed Tanousabf2add2019-01-22 16:40:12 -080023#include <variant>
Ed Tanousb9b2e0b2018-09-13 13:47:50 -070024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025namespace redfish
26{
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +010027
Ratan Gupta6973a582018-12-13 18:25:44 +053028constexpr const char* ldapConfigObject =
29 "/xyz/openbmc_project/user/ldap/openldap";
30constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
31constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
32constexpr const char* ldapConfigInterface =
33 "xyz.openbmc_project.User.Ldap.Config";
34constexpr const char* ldapCreateInterface =
35 "xyz.openbmc_project.User.Ldap.Create";
36constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
37constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
38constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
39constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
40constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
41constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
42
43struct LDAPConfigData
44{
45 std::string uri{};
46 std::string bindDN{};
47 std::string baseDN{};
48 std::string searchScope{};
49 std::string serverType{};
50 bool serviceEnabled = false;
51 std::string userNameAttribute{};
52 std::string groupAttribute{};
53};
54
Ed Tanousb9b2e0b2018-09-13 13:47:50 -070055using ManagedObjectType = std::vector<std::pair<
56 sdbusplus::message::object_path,
57 boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080058 std::string, boost::container::flat_map<
59 std::string, std::variant<bool, std::string>>>>>;
Ratan Gupta6973a582018-12-13 18:25:44 +053060using GetObjectType =
61 std::vector<std::pair<std::string, std::vector<std::string>>>;
AppaRao Puli84e12cb2018-10-11 01:28:15 +053062
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050063inline std::string getPrivilegeFromRoleId(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053064{
65 if (role == "priv-admin")
66 {
67 return "Administrator";
68 }
69 else if (role == "priv-callback")
70 {
71 return "Callback";
72 }
73 else if (role == "priv-user")
74 {
75 return "User";
76 }
77 else if (role == "priv-operator")
78 {
79 return "Operator";
80 }
81 return "";
82}
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050083inline std::string getRoleIdFromPrivilege(std::string_view role)
AppaRao Puli84e12cb2018-10-11 01:28:15 +053084{
85 if (role == "Administrator")
86 {
87 return "priv-admin";
88 }
89 else if (role == "Callback")
90 {
91 return "priv-callback";
92 }
93 else if (role == "User")
94 {
95 return "priv-user";
96 }
97 else if (role == "Operator")
98 {
99 return "priv-operator";
100 }
101 return "";
102}
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700103
Ratan Gupta6973a582018-12-13 18:25:44 +0530104void parseLDAPConfigData(nlohmann::json& json_response,
105 const LDAPConfigData& confData)
106{
107 std::string service = "LDAPService";
108 json_response["LDAP"] = {
109 {"AccountProviderType", service},
110 {"AccountProviderType@Redfish.AllowableValues",
111 nlohmann::json::array({service})},
112 {"ServiceEnabled", confData.serviceEnabled},
113 {"ServiceAddresses", nlohmann::json::array({confData.uri})},
114 {"Authentication",
115 {{"AuthenticationType", "UsernameAndPassword"},
116 {"AuthenticationType@Redfish.AllowableValues",
117 nlohmann::json::array({"UsernameAndPassword"})},
118 {"Username", confData.bindDN},
119 {"Password", nullptr}}},
120 {"LDAPService",
121 {{"SearchSettings",
122 {{"BaseDistinguishedNames",
123 nlohmann::json::array({confData.baseDN})},
124 {"UsernameAttribute", confData.userNameAttribute},
125 {"GroupsAttribute", confData.groupAttribute}}}}},
126 };
127}
128
129/**
130 * Function that retrieves all properties for LDAP config object
131 * into JSON
132 */
133template <typename CallbackFunc>
134inline void getLDAPConfigData(const std::string& ldapType,
135 CallbackFunc&& callback)
136{
137 auto getConfig = [callback,
138 ldapType](const boost::system::error_code error_code,
139 const ManagedObjectType& ldapObjects) {
140 LDAPConfigData confData{};
141 if (error_code)
142 {
143 callback(false, confData);
144 BMCWEB_LOG_ERROR << "D-Bus responses error: " << error_code;
145 return;
146 }
147 std::string ldapConfigObjectStr = std::string(ldapConfigObject);
148 std::string ldapEnableInterfaceStr = std::string(ldapEnableInterface);
149 std::string ldapConfigInterfaceStr = std::string(ldapConfigInterface);
150 for (const auto& object : ldapObjects)
151 {
152 if (object.first == ldapConfigObjectStr)
153 {
154 for (const auto& interface : object.second)
155 {
156 if (interface.first == ldapEnableInterfaceStr)
157 {
158 // rest of the properties are string.
159 for (const auto& property : interface.second)
160 {
161 if (property.first == "Enabled")
162 {
163 const bool* value =
164 std::get_if<bool>(&property.second);
165 if (value == nullptr)
166 {
167 continue;
168 }
169 confData.serviceEnabled = *value;
170 break;
171 }
172 }
173 }
174 else if (interface.first == ldapConfigInterfaceStr)
175 {
176
177 for (const auto& property : interface.second)
178 {
179 const std::string* value =
180 std::get_if<std::string>(&property.second);
181 if (value == nullptr)
182 {
183 continue;
184 }
185 if (property.first == "LDAPServerURI")
186 {
187 confData.uri = *value;
188 }
189 else if (property.first == "LDAPBindDN")
190 {
191 confData.bindDN = *value;
192 }
193 else if (property.first == "LDAPBaseDN")
194 {
195 confData.baseDN = *value;
196 }
197 else if (property.first == "LDAPSearchScope")
198 {
199 confData.searchScope = *value;
200 }
201 else if (property.first == "LDAPType")
202 {
203 confData.serverType = *value;
204 }
205 else if (property.first == "GroupNameAttribute")
206 {
207 confData.groupAttribute = *value;
208 }
209 else if (property.first == "UserNameAttribute")
210 {
211 confData.userNameAttribute = *value;
212 }
213 }
214 }
215 }
216
217 callback(true, confData);
218 break;
219 }
220 }
221 };
222 auto getServiceName = [callback, getConfig(std::move(getConfig))](
223 const boost::system::error_code ec,
224 const GetObjectType& resp) {
225 LDAPConfigData confData{};
226 if (ec || resp.empty())
227 {
228 BMCWEB_LOG_ERROR
229 << "DBUS response error during getting of service name: " << ec;
230 callback(false, confData);
231 return;
232 }
233 std::string service = resp.begin()->first;
234 crow::connections::systemBus->async_method_call(
235 std::move(getConfig), service, ldapRootObject, dbusObjManagerIntf,
236 "GetManagedObjects");
237 };
238
239 const std::array<std::string, 2> interfaces = {ldapEnableInterface,
240 ldapConfigInterface};
241
242 crow::connections::systemBus->async_method_call(
243 std::move(getServiceName), mapperBusName, mapperObjectPath, mapperIntf,
244 "GetObject", ldapConfigObject, interfaces);
245}
246
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247class AccountService : public Node
248{
249 public:
250 AccountService(CrowApp& app) : Node(app, "/redfish/v1/AccountService/")
251 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 entityPrivileges = {
253 {boost::beast::http::verb::get,
254 {{"ConfigureUsers"}, {"ConfigureManager"}}},
255 {boost::beast::http::verb::head, {{"Login"}}},
256 {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
257 {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
258 {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
259 {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
260 }
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +0100261
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 private:
263 void doGet(crow::Response& res, const crow::Request& req,
264 const std::vector<std::string>& params) override
265 {
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530266 auto asyncResp = std::make_shared<AsyncResp>(res);
267 res.jsonValue = {
268 {"@odata.context", "/redfish/v1/"
269 "$metadata#AccountService.AccountService"},
270 {"@odata.id", "/redfish/v1/AccountService"},
271 {"@odata.type", "#AccountService."
Ratan Gupta6973a582018-12-13 18:25:44 +0530272 "v1_3_1.AccountService"},
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530273 {"Id", "AccountService"},
274 {"Name", "Account Service"},
275 {"Description", "Account Service"},
276 {"ServiceEnabled", true},
AppaRao Puli343ff2e2019-03-24 00:42:13 +0530277 {"MaxPasswordLength", 20},
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530278 {"Accounts",
279 {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
280 {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}}};
Ed Tanous0f74e642018-11-12 15:17:05 -0800281
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530282 crow::connections::systemBus->async_method_call(
283 [asyncResp](
284 const boost::system::error_code ec,
285 const std::vector<std::pair<
Ed Tanousabf2add2019-01-22 16:40:12 -0800286 std::string, std::variant<uint32_t, uint16_t, uint8_t>>>&
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530287 propertiesList) {
288 if (ec)
289 {
290 messages::internalError(asyncResp->res);
291 return;
292 }
293 BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
294 << "properties for AccountService";
295 for (const std::pair<std::string,
Ed Tanousabf2add2019-01-22 16:40:12 -0800296 std::variant<uint32_t, uint16_t, uint8_t>>&
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530297 property : propertiesList)
298 {
299 if (property.first == "MinPasswordLength")
300 {
301 const uint8_t* value =
Ed Tanousabf2add2019-01-22 16:40:12 -0800302 std::get_if<uint8_t>(&property.second);
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530303 if (value != nullptr)
304 {
305 asyncResp->res.jsonValue["MinPasswordLength"] =
306 *value;
307 }
308 }
309 if (property.first == "AccountUnlockTimeout")
310 {
311 const uint32_t* value =
Ed Tanousabf2add2019-01-22 16:40:12 -0800312 std::get_if<uint32_t>(&property.second);
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530313 if (value != nullptr)
314 {
315 asyncResp->res.jsonValue["AccountLockoutDuration"] =
316 *value;
317 }
318 }
319 if (property.first == "MaxLoginAttemptBeforeLockout")
320 {
321 const uint16_t* value =
Ed Tanousabf2add2019-01-22 16:40:12 -0800322 std::get_if<uint16_t>(&property.second);
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530323 if (value != nullptr)
324 {
325 asyncResp->res
326 .jsonValue["AccountLockoutThreshold"] = *value;
327 }
328 }
329 }
330 },
331 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
332 "org.freedesktop.DBus.Properties", "GetAll",
333 "xyz.openbmc_project.User.AccountPolicy");
Ratan Gupta6973a582018-12-13 18:25:44 +0530334
335 std::string ldapType = "LDAP";
336 getLDAPConfigData(
337 ldapType,
338 [asyncResp, ldapType](bool success, LDAPConfigData& confData) {
339 parseLDAPConfigData(asyncResp->res.jsonValue, confData);
340 });
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530341 }
Ratan Gupta6973a582018-12-13 18:25:44 +0530342
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530343 void doPatch(crow::Response& res, const crow::Request& req,
344 const std::vector<std::string>& params) override
345 {
346 auto asyncResp = std::make_shared<AsyncResp>(res);
347
348 std::optional<uint32_t> unlockTimeout;
349 std::optional<uint16_t> lockoutThreshold;
Ratan Gupta19fb6e72019-03-04 13:30:50 +0530350 std::optional<uint16_t> minPasswordLength;
351 std::optional<uint16_t> maxPasswordLength;
352
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530353 if (!json_util::readJson(req, res, "AccountLockoutDuration",
354 unlockTimeout, "AccountLockoutThreshold",
Ratan Gupta19fb6e72019-03-04 13:30:50 +0530355 lockoutThreshold, "MaxPasswordLength",
356 maxPasswordLength, "MinPasswordLength",
357 minPasswordLength))
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530358 {
359 return;
360 }
Ratan Gupta19fb6e72019-03-04 13:30:50 +0530361
362 if (minPasswordLength)
363 {
364 messages::propertyNotWritable(asyncResp->res, "MinPasswordLength");
365 }
366
367 if (maxPasswordLength)
368 {
369 messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
370 }
371
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530372 if (unlockTimeout)
373 {
374 crow::connections::systemBus->async_method_call(
375 [asyncResp](const boost::system::error_code ec) {
376 if (ec)
377 {
378 messages::internalError(asyncResp->res);
379 return;
380 }
Ratan Guptaadd61332019-02-13 20:49:16 +0530381 messages::success(asyncResp->res);
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530382 },
383 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
384 "org.freedesktop.DBus.Properties", "Set",
385 "xyz.openbmc_project.User.AccountPolicy",
Ed Tanousabf2add2019-01-22 16:40:12 -0800386 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530387 }
388 if (lockoutThreshold)
389 {
390 crow::connections::systemBus->async_method_call(
391 [asyncResp](const boost::system::error_code ec) {
392 if (ec)
393 {
394 messages::internalError(asyncResp->res);
395 return;
396 }
Ratan Guptaadd61332019-02-13 20:49:16 +0530397 messages::success(asyncResp->res);
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530398 },
399 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
400 "org.freedesktop.DBus.Properties", "Set",
401 "xyz.openbmc_project.User.AccountPolicy",
402 "MaxLoginAttemptBeforeLockout",
Ed Tanousabf2add2019-01-22 16:40:12 -0800403 std::variant<uint16_t>(*lockoutThreshold));
AppaRao Puli3d958bb2018-12-25 12:45:54 +0530404 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700405 }
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +0100406};
Tanousf00032d2018-11-05 01:18:10 -0300407
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700408class AccountsCollection : public Node
409{
410 public:
411 AccountsCollection(CrowApp& app) :
412 Node(app, "/redfish/v1/AccountService/Accounts/")
413 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700414 entityPrivileges = {
415 {boost::beast::http::verb::get,
416 {{"ConfigureUsers"}, {"ConfigureManager"}}},
417 {boost::beast::http::verb::head, {{"Login"}}},
418 {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
419 {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
420 {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
421 {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
422 }
423
424 private:
425 void doGet(crow::Response& res, const crow::Request& req,
426 const std::vector<std::string>& params) override
427 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700428 auto asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous0f74e642018-11-12 15:17:05 -0800429 res.jsonValue = {{"@odata.context",
430 "/redfish/v1/"
431 "$metadata#ManagerAccountCollection."
432 "ManagerAccountCollection"},
433 {"@odata.id", "/redfish/v1/AccountService/Accounts"},
434 {"@odata.type", "#ManagerAccountCollection."
435 "ManagerAccountCollection"},
436 {"Name", "Accounts Collection"},
437 {"Description", "BMC User Accounts"}};
438
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700439 crow::connections::systemBus->async_method_call(
440 [asyncResp](const boost::system::error_code ec,
441 const ManagedObjectType& users) {
442 if (ec)
443 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700444 messages::internalError(asyncResp->res);
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700445 return;
446 }
447
448 nlohmann::json& memberArray =
449 asyncResp->res.jsonValue["Members"];
450 memberArray = nlohmann::json::array();
451
452 asyncResp->res.jsonValue["Members@odata.count"] = users.size();
453 for (auto& user : users)
454 {
455 const std::string& path =
456 static_cast<const std::string&>(user.first);
457 std::size_t lastIndex = path.rfind("/");
458 if (lastIndex == std::string::npos)
459 {
460 lastIndex = 0;
461 }
462 else
463 {
464 lastIndex += 1;
465 }
466 memberArray.push_back(
467 {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
468 path.substr(lastIndex)}});
469 }
470 },
471 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
472 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
473 }
Ed Tanous04ae99e2018-09-20 15:54:36 -0700474 void doPost(crow::Response& res, const crow::Request& req,
475 const std::vector<std::string>& params) override
476 {
477 auto asyncResp = std::make_shared<AsyncResp>(res);
478
Ed Tanous9712f8a2018-09-21 13:38:49 -0700479 std::string username;
480 std::string password;
Ed Tanousa24526d2018-12-10 15:17:59 -0800481 std::optional<std::string> roleId("User");
482 std::optional<bool> enabled = true;
Ed Tanous9712f8a2018-09-21 13:38:49 -0700483 if (!json_util::readJson(req, res, "UserName", username, "Password",
484 password, "RoleId", roleId, "Enabled",
485 enabled))
Ed Tanous04ae99e2018-09-20 15:54:36 -0700486 {
487 return;
488 }
489
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530490 std::string priv = getRoleIdFromPrivilege(*roleId);
491 if (priv.empty())
Ed Tanous04ae99e2018-09-20 15:54:36 -0700492 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700493 messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
Ed Tanous04ae99e2018-09-20 15:54:36 -0700494 return;
495 }
Ed Tanous9712f8a2018-09-21 13:38:49 -0700496 roleId = priv;
Ed Tanous04ae99e2018-09-20 15:54:36 -0700497
498 crow::connections::systemBus->async_method_call(
Ed Tanous9712f8a2018-09-21 13:38:49 -0700499 [asyncResp, username, password{std::move(password)}](
Ed Tanous04ae99e2018-09-20 15:54:36 -0700500 const boost::system::error_code ec) {
501 if (ec)
502 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700503 messages::resourceAlreadyExists(
504 asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
505 "UserName", username);
Ed Tanous04ae99e2018-09-20 15:54:36 -0700506 return;
507 }
508
509 if (!pamUpdatePassword(username, password))
510 {
511 // At this point we have a user that's been created, but the
512 // password set failed. Something is wrong, so delete the
513 // user that we've already created
514 crow::connections::systemBus->async_method_call(
515 [asyncResp](const boost::system::error_code ec) {
516 if (ec)
517 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700518 messages::internalError(asyncResp->res);
Ed Tanous04ae99e2018-09-20 15:54:36 -0700519 return;
520 }
521
Jason M. Billsf12894f2018-10-09 12:45:45 -0700522 messages::invalidObject(asyncResp->res, "Password");
Ed Tanous04ae99e2018-09-20 15:54:36 -0700523 },
524 "xyz.openbmc_project.User.Manager",
525 "/xyz/openbmc_project/user/" + username,
526 "xyz.openbmc_project.Object.Delete", "Delete");
527
528 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
529 return;
530 }
531
Jason M. Billsf12894f2018-10-09 12:45:45 -0700532 messages::created(asyncResp->res);
Ed Tanous04ae99e2018-09-20 15:54:36 -0700533 asyncResp->res.addHeader(
534 "Location",
535 "/redfish/v1/AccountService/Accounts/" + username);
536 },
537 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
Ed Tanous9712f8a2018-09-21 13:38:49 -0700538 "xyz.openbmc_project.User.Manager", "CreateUser", username,
Ed Tanous04ae99e2018-09-20 15:54:36 -0700539 std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"},
Ed Tanous9712f8a2018-09-21 13:38:49 -0700540 *roleId, *enabled);
Ed Tanous04ae99e2018-09-20 15:54:36 -0700541 }
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700542};
543
Ed Tanousa8408792018-09-05 16:08:38 -0700544template <typename Callback>
545inline void checkDbusPathExists(const std::string& path, Callback&& callback)
546{
547 using GetObjectType =
548 std::vector<std::pair<std::string, std::vector<std::string>>>;
549
550 crow::connections::systemBus->async_method_call(
551 [callback{std::move(callback)}](const boost::system::error_code ec,
552 const GetObjectType& object_names) {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530553 callback(!ec && object_names.size() != 0);
Ed Tanousa8408792018-09-05 16:08:38 -0700554 },
555 "xyz.openbmc_project.ObjectMapper",
556 "/xyz/openbmc_project/object_mapper",
557 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
558 std::array<std::string, 0>());
559}
560
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700561class ManagerAccount : public Node
562{
563 public:
564 ManagerAccount(CrowApp& app) :
565 Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
566 {
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700567 entityPrivileges = {
568 {boost::beast::http::verb::get,
569 {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
570 {boost::beast::http::verb::head, {{"Login"}}},
571 {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
572 {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
573 {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
574 {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
575 }
576
577 private:
578 void doGet(crow::Response& res, const crow::Request& req,
579 const std::vector<std::string>& params) override
580 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800581 res.jsonValue = {
582 {"@odata.context",
583 "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
584 {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
Ed Tanous0f74e642018-11-12 15:17:05 -0800585 {"Name", "User Account"},
586 {"Description", "User Account"},
587 {"Password", nullptr},
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530588 {"RoleId", "Administrator"}};
Ed Tanous0f74e642018-11-12 15:17:05 -0800589
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700590 auto asyncResp = std::make_shared<AsyncResp>(res);
591
592 if (params.size() != 1)
593 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700594 messages::internalError(asyncResp->res);
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700595 return;
596 }
597
598 crow::connections::systemBus->async_method_call(
599 [asyncResp, accountName{std::string(params[0])}](
600 const boost::system::error_code ec,
601 const ManagedObjectType& users) {
602 if (ec)
603 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700604 messages::internalError(asyncResp->res);
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700605 return;
606 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530607 auto userIt = users.begin();
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700608
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530609 for (; userIt != users.end(); userIt++)
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700610 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530611 if (boost::ends_with(userIt->first.str, "/" + accountName))
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700612 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530613 break;
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700614 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530615 }
616 if (userIt == users.end())
617 {
618 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
619 accountName);
620 return;
621 }
622 for (const auto& interface : userIt->second)
623 {
624 if (interface.first ==
625 "xyz.openbmc_project.User.Attributes")
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700626 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530627 for (const auto& property : interface.second)
Ed Tanous65b0dc32018-09-19 16:04:03 -0700628 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530629 if (property.first == "UserEnabled")
Ed Tanous65b0dc32018-09-19 16:04:03 -0700630 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530631 const bool* userEnabled =
Ed Tanousabf2add2019-01-22 16:40:12 -0800632 std::get_if<bool>(&property.second);
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530633 if (userEnabled == nullptr)
Ed Tanous65b0dc32018-09-19 16:04:03 -0700634 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530635 BMCWEB_LOG_ERROR
636 << "UserEnabled wasn't a bool";
637 messages::internalError(asyncResp->res);
638 return;
Ed Tanous65b0dc32018-09-19 16:04:03 -0700639 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530640 asyncResp->res.jsonValue["Enabled"] =
641 *userEnabled;
642 }
643 else if (property.first ==
644 "UserLockedForFailedAttempt")
645 {
646 const bool* userLocked =
Ed Tanousabf2add2019-01-22 16:40:12 -0800647 std::get_if<bool>(&property.second);
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530648 if (userLocked == nullptr)
649 {
650 BMCWEB_LOG_ERROR << "UserLockedForF"
651 "ailedAttempt "
652 "wasn't a bool";
653 messages::internalError(asyncResp->res);
654 return;
655 }
656 asyncResp->res.jsonValue["Locked"] =
657 *userLocked;
Ratan Gupta24c85422019-01-30 19:41:24 +0530658 asyncResp->res.jsonValue
659 ["Locked@Redfish.AllowableValues"] = {
Gunnar Mills4d64ce32019-03-29 16:34:56 -0500660 "false"};
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530661 }
662 else if (property.first == "UserPrivilege")
663 {
664 const std::string* userRolePtr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800665 std::get_if<std::string>(&property.second);
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530666 if (userRolePtr == nullptr)
667 {
668 BMCWEB_LOG_ERROR
669 << "UserPrivilege wasn't a "
670 "string";
671 messages::internalError(asyncResp->res);
672 return;
673 }
674 std::string priv =
675 getPrivilegeFromRoleId(*userRolePtr);
676 if (priv.empty())
677 {
678 BMCWEB_LOG_ERROR << "Invalid user role";
679 messages::internalError(asyncResp->res);
680 return;
681 }
682 asyncResp->res.jsonValue["RoleId"] = priv;
683
684 asyncResp->res.jsonValue["Links"]["Role"] = {
685 {"@odata.id", "/redfish/v1/AccountService/"
686 "Roles/" +
687 priv}};
Ed Tanous65b0dc32018-09-19 16:04:03 -0700688 }
689 }
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700690 }
691 }
692
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530693 asyncResp->res.jsonValue["@odata.id"] =
694 "/redfish/v1/AccountService/Accounts/" + accountName;
695 asyncResp->res.jsonValue["Id"] = accountName;
696 asyncResp->res.jsonValue["UserName"] = accountName;
Ed Tanousb9b2e0b2018-09-13 13:47:50 -0700697 },
698 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
699 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
700 }
Ed Tanousa8408792018-09-05 16:08:38 -0700701
702 void doPatch(crow::Response& res, const crow::Request& req,
703 const std::vector<std::string>& params) override
704 {
705 auto asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanousa8408792018-09-05 16:08:38 -0700706 if (params.size() != 1)
707 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700708 messages::internalError(asyncResp->res);
Ed Tanousa8408792018-09-05 16:08:38 -0700709 return;
710 }
711
Ed Tanousa24526d2018-12-10 15:17:59 -0800712 std::optional<std::string> newUserName;
713 std::optional<std::string> password;
714 std::optional<bool> enabled;
715 std::optional<std::string> roleId;
Ratan Gupta24c85422019-01-30 19:41:24 +0530716 std::optional<bool> locked;
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530717 if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
Ratan Gupta24c85422019-01-30 19:41:24 +0530718 password, "RoleId", roleId, "Enabled", enabled,
719 "Locked", locked))
Ed Tanousa8408792018-09-05 16:08:38 -0700720 {
721 return;
722 }
723
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530724 const std::string& username = params[0];
Ed Tanousa8408792018-09-05 16:08:38 -0700725
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530726 if (!newUserName)
727 {
728 // If the username isn't being updated, we can update the properties
729 // directly
Ratan Gupta24c85422019-01-30 19:41:24 +0530730 updateUserProperties(asyncResp, username, password, enabled, roleId,
731 locked);
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530732 return;
733 }
734 else
735 {
736 crow::connections::systemBus->async_method_call(
737 [this, asyncResp, username, password(std::move(password)),
738 roleId(std::move(roleId)), enabled(std::move(enabled)),
Ratan Gupta24c85422019-01-30 19:41:24 +0530739 newUser{std::string(*newUserName)}, locked(std::move(locked))](
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530740 const boost::system::error_code ec) {
741 if (ec)
Ed Tanousa8408792018-09-05 16:08:38 -0700742 {
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530743 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
744 messages::resourceNotFound(
745 asyncResp->res,
746 "#ManagerAccount.v1_0_3.ManagerAccount", username);
747 return;
748 }
749
750 updateUserProperties(asyncResp, newUser, password, enabled,
Ratan Gupta24c85422019-01-30 19:41:24 +0530751 roleId, locked);
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530752 },
753 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
754 "xyz.openbmc_project.User.Manager", "RenameUser", username,
755 *newUserName);
756 }
757 }
758
759 void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
760 const std::string& username,
Ed Tanousa24526d2018-12-10 15:17:59 -0800761 std::optional<std::string> password,
762 std::optional<bool> enabled,
Ratan Gupta24c85422019-01-30 19:41:24 +0530763 std::optional<std::string> roleId,
764 std::optional<bool> locked)
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530765 {
766 if (password)
767 {
768 if (!pamUpdatePassword(username, *password))
769 {
770 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
771 messages::internalError(asyncResp->res);
772 return;
773 }
774 }
775
Ratan Gupta24c85422019-01-30 19:41:24 +0530776 std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
777 dbus::utility::escapePathForDbus(dbusObjectPath);
778
779 checkDbusPathExists(
780 dbusObjectPath,
781 [dbusObjectPath(std::move(dbusObjectPath)), username,
782 password(std::move(password)), roleId(std::move(roleId)),
783 enabled(std::move(enabled)), locked(std::move(locked)),
784 asyncResp{std::move(asyncResp)}](int rc) {
785 if (!rc)
786 {
787 messages::invalidObject(asyncResp->res, username.c_str());
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530788 return;
Ratan Gupta24c85422019-01-30 19:41:24 +0530789 }
790 if (enabled)
791 {
792 crow::connections::systemBus->async_method_call(
793 [asyncResp](const boost::system::error_code ec) {
794 if (ec)
795 {
796 BMCWEB_LOG_ERROR << "D-Bus responses error: "
797 << ec;
798 messages::internalError(asyncResp->res);
799 return;
800 }
801 messages::success(asyncResp->res);
802 return;
803 },
804 "xyz.openbmc_project.User.Manager",
805 dbusObjectPath.c_str(),
806 "org.freedesktop.DBus.Properties", "Set",
807 "xyz.openbmc_project.User.Attributes", "UserEnabled",
808 std::variant<bool>{*enabled});
809 }
Ed Tanous9712f8a2018-09-21 13:38:49 -0700810
Ratan Gupta24c85422019-01-30 19:41:24 +0530811 if (roleId)
812 {
813 std::string priv = getRoleIdFromPrivilege(*roleId);
814 if (priv.empty())
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530815 {
Ratan Gupta24c85422019-01-30 19:41:24 +0530816 messages::propertyValueNotInList(asyncResp->res,
817 *roleId, "RoleId");
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530818 return;
819 }
Ratan Gupta24c85422019-01-30 19:41:24 +0530820
821 crow::connections::systemBus->async_method_call(
822 [asyncResp](const boost::system::error_code ec) {
823 if (ec)
824 {
825 BMCWEB_LOG_ERROR << "D-Bus responses error: "
826 << ec;
827 messages::internalError(asyncResp->res);
828 return;
829 }
830 messages::success(asyncResp->res);
831 },
832 "xyz.openbmc_project.User.Manager",
833 dbusObjectPath.c_str(),
834 "org.freedesktop.DBus.Properties", "Set",
835 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
836 std::variant<std::string>{priv});
837 }
838
839 if (locked)
840 {
841 // admin can unlock the account which is locked by
842 // successive authentication failures but admin should not
843 // be allowed to lock an account.
844 if (*locked)
845 {
846 messages::propertyValueNotInList(asyncResp->res, "true",
847 "Locked");
848 return;
849 }
850
851 crow::connections::systemBus->async_method_call(
852 [asyncResp](const boost::system::error_code ec) {
853 if (ec)
854 {
855 BMCWEB_LOG_ERROR << "D-Bus responses error: "
856 << ec;
857 messages::internalError(asyncResp->res);
858 return;
859 }
860 messages::success(asyncResp->res);
861 return;
862 },
863 "xyz.openbmc_project.User.Manager",
864 dbusObjectPath.c_str(),
865 "org.freedesktop.DBus.Properties", "Set",
866 "xyz.openbmc_project.User.Attributes",
867 "UserLockedForFailedAttempt",
868 sdbusplus::message::variant<bool>{*locked});
869 }
870 });
Ed Tanousa8408792018-09-05 16:08:38 -0700871 }
Ed Tanous06e086d2018-09-19 17:19:52 -0700872
873 void doDelete(crow::Response& res, const crow::Request& req,
874 const std::vector<std::string>& params) override
875 {
876 auto asyncResp = std::make_shared<AsyncResp>(res);
877
878 if (params.size() != 1)
879 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700880 messages::internalError(asyncResp->res);
Ed Tanous06e086d2018-09-19 17:19:52 -0700881 return;
882 }
883
884 const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
885
886 crow::connections::systemBus->async_method_call(
887 [asyncResp, username{std::move(params[0])}](
888 const boost::system::error_code ec) {
889 if (ec)
890 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700891 messages::resourceNotFound(
892 asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
893 username);
Ed Tanous06e086d2018-09-19 17:19:52 -0700894 return;
895 }
896
Jason M. Billsf12894f2018-10-09 12:45:45 -0700897 messages::accountRemoved(asyncResp->res);
Ed Tanous06e086d2018-09-19 17:19:52 -0700898 },
899 "xyz.openbmc_project.User.Manager", userPath,
900 "xyz.openbmc_project.Object.Delete", "Delete");
901 }
AppaRao Puli84e12cb2018-10-11 01:28:15 +0530902};
Lewanczyk, Dawid88d16c92018-02-02 14:51:09 +0100903
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904} // namespace redfish