blob: d3ea15987d64078b7a8ae7449039b697aea17c1b [file] [log] [blame]
#include "config.h"
#include "local_users.hpp"
#include <host-ipmid/ipmid-host-cmd.hpp>
#include <phosphor-logging/log.hpp>
namespace local
{
namespace users
{
using namespace phosphor::logging;
constexpr auto userIface = "xyz.openbmc_project.User.Attributes";
constexpr auto propIface = "org.freedesktop.DBus.Properties";
using DbusObjectPath = std::string;
using DbusService = std::string;
using DbusInterface = std::string;
using ObjectTree =
std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
/**
* @brief Gets a list of all local users in the form of GetSubTree
* results.
*
* @param[out] users - Filled in with the results of a
* GetSubTree call that returns all users.
*/
void getUsers(ObjectTree& users)
{
auto& bus = ipmid_get_sdbus_plus_handler();
try
{
auto method = bus->new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
MAPPER_IFACE, "GetSubTree");
method.append("/xyz/openbmc_project/user/", 0,
std::vector<std::string>{userIface});
auto reply = bus->call(method);
if (reply.is_method_error())
{
throw std::runtime_error("Method error on GetSubTree call");
}
reply.read(users);
}
catch (sdbusplus::exception::SdBusError& e)
{
throw std::runtime_error(e.what());
}
}
/**
* @brief Enables the user passed in
*
* @param[in] path - The user object path
* @param[in] service - The service hosting the user
*/
void enableUser(const std::string& path, const std::string& service)
{
auto& bus = ipmid_get_sdbus_plus_handler();
try
{
auto method = bus->new_method_call(service.c_str(), path.c_str(),
propIface, "Set");
sdbusplus::message::variant<bool> enabled{true};
method.append(userIface, "UserEnabled", enabled);
auto reply = bus->call(method);
if (reply.is_method_error())
{
throw std::runtime_error("Method error on property set call");
}
}
catch (sdbusplus::exception::SdBusError& e)
{
throw std::runtime_error(e.what());
}
}
ipmi_ret_t enableUsers()
{
ObjectTree users;
try
{
getUsers(users);
for (const auto& user : users)
{
enableUser(user.first, user.second.begin()->first);
}
}
catch (std::runtime_error& e)
{
log<level::ERR>("Failed enabling local users",
entry("ERROR=%s", e.what()));
return IPMI_CC_UNSPECIFIED_ERROR;
}
return IPMI_CC_OK;
}
} // namespace users
} // namespace local