blob: 3a383f6c0c18c3bbfba0a864eea1047bf72f793a [file] [log] [blame]
Patrick Williams9638afb2021-02-22 17:16:24 -06001#include "config.h"
2
3#include "ldap_mapper_mgr.hpp"
4
5#include "ldap_mapper_serialize.hpp"
6
7#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
Tom Joseph536ea322018-09-14 10:02:20 +053010#include <xyz/openbmc_project/Common/error.hpp>
11#include <xyz/openbmc_project/User/Common/error.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060012
13#include <filesystem>
Tom Joseph536ea322018-09-14 10:02:20 +053014
15namespace phosphor
16{
17namespace user
18{
19
20using namespace phosphor::logging;
21using InvalidArgument =
22 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
23using Argument = xyz::openbmc_project::Common::InvalidArgument;
24using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common::
25 Error::PrivilegeMappingExists;
26
Patrick Williams9638afb2021-02-22 17:16:24 -060027LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus& bus, const char* path,
28 const char* filePath) :
Tom Josephf5bd8912018-11-19 09:49:21 +053029 MapperMgrIface(bus, path),
30 bus(bus), path(path), persistPath(filePath)
Patrick Williams9638afb2021-02-22 17:16:24 -060031{}
Tom Joseph536ea322018-09-14 10:02:20 +053032
33ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege)
34{
35 checkPrivilegeMapper(groupName);
36 checkPrivilegeLevel(privilege);
37
38 entryId++;
39
40 // Object path for the LDAP group privilege mapper entry
41 auto mapperObject =
42 std::string(mapperMgrRoot) + "/" + std::to_string(entryId);
43
44 // Create mapping for LDAP privilege mapper entry
45 auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
Tom Josephf5bd8912018-11-19 09:49:21 +053046 bus, mapperObject.c_str(), persistPath.c_str(), groupName, privilege,
47 *this);
Tom Joseph536ea322018-09-14 10:02:20 +053048
Tom Josephf5bd8912018-11-19 09:49:21 +053049 serialize(*entry, entryId, persistPath);
Tom Joseph0b14c472018-09-30 01:42:59 +053050
Tom Joseph536ea322018-09-14 10:02:20 +053051 PrivilegeMapperList.emplace(entryId, std::move(entry));
52
53 return mapperObject;
54}
55
56void LDAPMapperMgr::deletePrivilegeMapper(Id id)
57{
Tom Joseph0b14c472018-09-30 01:42:59 +053058 // Delete the persistent representation of the privilege mapper.
Gunnar Mills703131f2020-10-28 14:26:33 -050059 std::filesystem::path mapperPath(persistPath);
Tom Joseph0b14c472018-09-30 01:42:59 +053060 mapperPath /= std::to_string(id);
Gunnar Mills703131f2020-10-28 14:26:33 -050061 std::filesystem::remove(mapperPath);
Tom Joseph0b14c472018-09-30 01:42:59 +053062
Tom Joseph536ea322018-09-14 10:02:20 +053063 PrivilegeMapperList.erase(id);
64}
65
Patrick Williams9638afb2021-02-22 17:16:24 -060066void LDAPMapperMgr::checkPrivilegeMapper(const std::string& groupName)
Tom Joseph536ea322018-09-14 10:02:20 +053067{
68 if (groupName.empty())
69 {
70 log<level::ERR>("Group name is empty");
71 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group name"),
72 Argument::ARGUMENT_VALUE("Null"));
73 }
74
Patrick Williams9638afb2021-02-22 17:16:24 -060075 for (const auto& val : PrivilegeMapperList)
Tom Joseph536ea322018-09-14 10:02:20 +053076 {
77 if (val.second.get()->groupName() == groupName)
78 {
79 log<level::ERR>("Group name already exists");
80 elog<PrivilegeMappingExists>();
81 }
82 }
83}
84
Patrick Williams9638afb2021-02-22 17:16:24 -060085void LDAPMapperMgr::checkPrivilegeLevel(const std::string& privilege)
Tom Joseph536ea322018-09-14 10:02:20 +053086{
87 if (privilege.empty())
88 {
89 log<level::ERR>("Privilege level is empty");
90 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"),
91 Argument::ARGUMENT_VALUE("Null"));
92 }
93
94 if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end())
95 {
96 log<level::ERR>("Invalid privilege");
97 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"),
98 Argument::ARGUMENT_VALUE(privilege.c_str()));
99 }
100}
101
Tom Joseph0b14c472018-09-30 01:42:59 +0530102void LDAPMapperMgr::restore()
103{
Gunnar Mills703131f2020-10-28 14:26:33 -0500104 std::filesystem::path dir(persistPath);
105 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Tom Joseph0b14c472018-09-30 01:42:59 +0530106 {
107 return;
108 }
109
Patrick Williams9638afb2021-02-22 17:16:24 -0600110 for (auto& file : std::filesystem::directory_iterator(dir))
Tom Joseph0b14c472018-09-30 01:42:59 +0530111 {
112 std::string id = file.path().filename().c_str();
113 size_t idNum = std::stol(id);
114 auto entryPath = std::string(mapperMgrRoot) + '/' + id;
115 auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
Tom Josephf5bd8912018-11-19 09:49:21 +0530116 bus, entryPath.c_str(), persistPath.c_str(), *this);
Tom Joseph0b14c472018-09-30 01:42:59 +0530117 if (deserialize(file.path(), *entry))
118 {
119 entry->Ifaces::emit_object_added();
120 PrivilegeMapperList.emplace(idNum, std::move(entry));
121 if (idNum > entryId)
122 {
123 entryId = idNum;
124 }
125 }
126 }
127}
128
Tom Joseph536ea322018-09-14 10:02:20 +0530129} // namespace user
130} // namespace phosphor