Matt Spinler | 34aca01 | 2018-09-25 11:21:06 -0500 | [diff] [blame^] | 1 | #include "config.h" |
| 2 | #include "local_users.hpp" |
| 3 | #include <host-ipmid/ipmid-host-cmd.hpp> |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | |
| 6 | namespace local |
| 7 | { |
| 8 | namespace users |
| 9 | { |
| 10 | |
| 11 | using namespace phosphor::logging; |
| 12 | |
| 13 | constexpr auto userIface = "xyz.openbmc_project.User.Attributes"; |
| 14 | constexpr auto propIface = "org.freedesktop.DBus.Properties"; |
| 15 | |
| 16 | using DbusObjectPath = std::string; |
| 17 | using DbusService = std::string; |
| 18 | using DbusInterface = std::string; |
| 19 | using ObjectTree = std::map<DbusObjectPath, |
| 20 | std::map<DbusService, std::vector<DbusInterface>>>; |
| 21 | |
| 22 | /** |
| 23 | * @brief Gets a list of all local users in the form of GetSubTree |
| 24 | * results. |
| 25 | * |
| 26 | * @param[out] users - Filled in with the results of a |
| 27 | * GetSubTree call that returns all users. |
| 28 | */ |
| 29 | void getUsers(ObjectTree& users) |
| 30 | { |
| 31 | auto& bus = ipmid_get_sdbus_plus_handler(); |
| 32 | |
| 33 | try |
| 34 | { |
| 35 | auto method = bus->new_method_call(MAPPER_BUS_NAME, |
| 36 | MAPPER_OBJ, |
| 37 | MAPPER_IFACE, |
| 38 | "GetSubTree"); |
| 39 | method.append("/xyz/openbmc_project/user/", 0, |
| 40 | std::vector<std::string>{userIface}); |
| 41 | |
| 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 | */ |
| 62 | void enableUser(const std::string& path, const std::string& service) |
| 63 | { |
| 64 | auto& bus = ipmid_get_sdbus_plus_handler(); |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | auto method = bus->new_method_call(service.c_str(), |
| 69 | path.c_str(), |
| 70 | propIface, |
| 71 | "Set"); |
| 72 | sdbusplus::message::variant<bool> enabled{true}; |
| 73 | method.append(userIface, "UserEnabled", enabled); |
| 74 | |
| 75 | auto reply = bus->call(method); |
| 76 | if (reply.is_method_error()) |
| 77 | { |
| 78 | throw std::runtime_error("Method error on property set call"); |
| 79 | } |
| 80 | } |
| 81 | catch (sdbusplus::exception::SdBusError& e) |
| 82 | { |
| 83 | throw std::runtime_error(e.what()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ipmi_ret_t enableUsers() |
| 88 | { |
| 89 | ObjectTree users; |
| 90 | |
| 91 | try |
| 92 | { |
| 93 | getUsers(users); |
| 94 | |
| 95 | for (const auto& user : users) |
| 96 | { |
| 97 | enableUser(user.first, user.second.begin()->first); |
| 98 | } |
| 99 | } |
| 100 | catch (std::runtime_error& e) |
| 101 | { |
| 102 | log<level::ERR>("Failed enabling local users", |
| 103 | entry("ERROR=%s", e.what())); |
| 104 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 105 | } |
| 106 | |
| 107 | return IPMI_CC_OK; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | } |