blob: d3ea15987d64078b7a8ae7449039b697aea17c1b [file] [log] [blame]
Matt Spinler34aca012018-09-25 11:21:06 -05001#include "config.h"
Patrick Venture02261c02018-10-31 15:16:23 -07002
Matt Spinler34aca012018-09-25 11:21:06 -05003#include "local_users.hpp"
Patrick Venture02261c02018-10-31 15:16:23 -07004
Matt Spinler34aca012018-09-25 11:21:06 -05005#include <host-ipmid/ipmid-host-cmd.hpp>
6#include <phosphor-logging/log.hpp>
7
8namespace local
9{
10namespace users
11{
12
13using namespace phosphor::logging;
14
15constexpr auto userIface = "xyz.openbmc_project.User.Attributes";
16constexpr auto propIface = "org.freedesktop.DBus.Properties";
17
18using DbusObjectPath = std::string;
19using DbusService = std::string;
20using DbusInterface = std::string;
Patrick Venture02261c02018-10-31 15:16:23 -070021using ObjectTree =
22 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Matt Spinler34aca012018-09-25 11:21:06 -050023
24/**
25 * @brief Gets a list of all local users in the form of GetSubTree
26 * results.
27 *
28 * @param[out] users - Filled in with the results of a
29 * GetSubTree call that returns all users.
30 */
31void getUsers(ObjectTree& users)
32{
33 auto& bus = ipmid_get_sdbus_plus_handler();
34
35 try
36 {
Patrick Venture02261c02018-10-31 15:16:23 -070037 auto method = bus->new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
38 MAPPER_IFACE, "GetSubTree");
Matt Spinler34aca012018-09-25 11:21:06 -050039 method.append("/xyz/openbmc_project/user/", 0,
Patrick Venture02261c02018-10-31 15:16:23 -070040 std::vector<std::string>{userIface});
Matt Spinler34aca012018-09-25 11:21:06 -050041
42 auto reply = bus->call(method);
43 if (reply.is_method_error())
44 {
45 throw std::runtime_error("Method error on GetSubTree call");
46 }
47
48 reply.read(users);
49 }
50 catch (sdbusplus::exception::SdBusError& e)
51 {
52 throw std::runtime_error(e.what());
53 }
54}
55
56/**
57 * @brief Enables the user passed in
58 *
59 * @param[in] path - The user object path
60 * @param[in] service - The service hosting the user
61 */
62void enableUser(const std::string& path, const std::string& service)
63{
64 auto& bus = ipmid_get_sdbus_plus_handler();
65
66 try
67 {
Patrick Venture02261c02018-10-31 15:16:23 -070068 auto method = bus->new_method_call(service.c_str(), path.c_str(),
69 propIface, "Set");
Matt Spinler34aca012018-09-25 11:21:06 -050070 sdbusplus::message::variant<bool> enabled{true};
71 method.append(userIface, "UserEnabled", enabled);
72
73 auto reply = bus->call(method);
74 if (reply.is_method_error())
75 {
76 throw std::runtime_error("Method error on property set call");
77 }
78 }
79 catch (sdbusplus::exception::SdBusError& e)
80 {
81 throw std::runtime_error(e.what());
82 }
83}
84
85ipmi_ret_t enableUsers()
86{
87 ObjectTree users;
88
89 try
90 {
91 getUsers(users);
92
93 for (const auto& user : users)
94 {
95 enableUser(user.first, user.second.begin()->first);
96 }
97 }
98 catch (std::runtime_error& e)
99 {
100 log<level::ERR>("Failed enabling local users",
Patrick Venture02261c02018-10-31 15:16:23 -0700101 entry("ERROR=%s", e.what()));
Matt Spinler34aca012018-09-25 11:21:06 -0500102 return IPMI_CC_UNSPECIFIED_ERROR;
103 }
104
105 return IPMI_CC_OK;
106}
107
Patrick Venture02261c02018-10-31 15:16:23 -0700108} // namespace users
109} // namespace local