blob: b8d755b3407ee18176f1fded41ffa3f760495e35 [file] [log] [blame]
Gunnar Mills703131f2020-10-28 14:26:33 -05001#include <filesystem>
Tom Joseph536ea322018-09-14 10:02:20 +05302#include <xyz/openbmc_project/Common/error.hpp>
3#include <xyz/openbmc_project/User/Common/error.hpp>
4#include <phosphor-logging/log.hpp>
5#include <phosphor-logging/elog.hpp>
6#include <phosphor-logging/elog-errors.hpp>
7#include "config.h"
8#include "ldap_mapper_mgr.hpp"
Tom Joseph0b14c472018-09-30 01:42:59 +05309#include "ldap_mapper_serialize.hpp"
Tom Joseph536ea322018-09-14 10:02:20 +053010
11namespace phosphor
12{
13namespace user
14{
15
16using namespace phosphor::logging;
17using InvalidArgument =
18 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
19using Argument = xyz::openbmc_project::Common::InvalidArgument;
20using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common::
21 Error::PrivilegeMappingExists;
22
Tom Josephf5bd8912018-11-19 09:49:21 +053023LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path,
24 const char *filePath) :
25 MapperMgrIface(bus, path),
26 bus(bus), path(path), persistPath(filePath)
Tom Joseph536ea322018-09-14 10:02:20 +053027{
28}
29
30ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege)
31{
32 checkPrivilegeMapper(groupName);
33 checkPrivilegeLevel(privilege);
34
35 entryId++;
36
37 // Object path for the LDAP group privilege mapper entry
38 auto mapperObject =
39 std::string(mapperMgrRoot) + "/" + std::to_string(entryId);
40
41 // Create mapping for LDAP privilege mapper entry
42 auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
Tom Josephf5bd8912018-11-19 09:49:21 +053043 bus, mapperObject.c_str(), persistPath.c_str(), groupName, privilege,
44 *this);
Tom Joseph536ea322018-09-14 10:02:20 +053045
Tom Josephf5bd8912018-11-19 09:49:21 +053046 serialize(*entry, entryId, persistPath);
Tom Joseph0b14c472018-09-30 01:42:59 +053047
Tom Joseph536ea322018-09-14 10:02:20 +053048 PrivilegeMapperList.emplace(entryId, std::move(entry));
49
50 return mapperObject;
51}
52
53void LDAPMapperMgr::deletePrivilegeMapper(Id id)
54{
Tom Joseph0b14c472018-09-30 01:42:59 +053055 // Delete the persistent representation of the privilege mapper.
Gunnar Mills703131f2020-10-28 14:26:33 -050056 std::filesystem::path mapperPath(persistPath);
Tom Joseph0b14c472018-09-30 01:42:59 +053057 mapperPath /= std::to_string(id);
Gunnar Mills703131f2020-10-28 14:26:33 -050058 std::filesystem::remove(mapperPath);
Tom Joseph0b14c472018-09-30 01:42:59 +053059
Tom Joseph536ea322018-09-14 10:02:20 +053060 PrivilegeMapperList.erase(id);
61}
62
63void LDAPMapperMgr::checkPrivilegeMapper(const std::string &groupName)
64{
65 if (groupName.empty())
66 {
67 log<level::ERR>("Group name is empty");
68 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group name"),
69 Argument::ARGUMENT_VALUE("Null"));
70 }
71
72 for (const auto &val : PrivilegeMapperList)
73 {
74 if (val.second.get()->groupName() == groupName)
75 {
76 log<level::ERR>("Group name already exists");
77 elog<PrivilegeMappingExists>();
78 }
79 }
80}
81
82void LDAPMapperMgr::checkPrivilegeLevel(const std::string &privilege)
83{
84 if (privilege.empty())
85 {
86 log<level::ERR>("Privilege level is empty");
87 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"),
88 Argument::ARGUMENT_VALUE("Null"));
89 }
90
91 if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end())
92 {
93 log<level::ERR>("Invalid privilege");
94 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"),
95 Argument::ARGUMENT_VALUE(privilege.c_str()));
96 }
97}
98
Tom Joseph0b14c472018-09-30 01:42:59 +053099void LDAPMapperMgr::restore()
100{
Gunnar Mills703131f2020-10-28 14:26:33 -0500101 std::filesystem::path dir(persistPath);
102 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Tom Joseph0b14c472018-09-30 01:42:59 +0530103 {
104 return;
105 }
106
Gunnar Mills703131f2020-10-28 14:26:33 -0500107 for (auto &file : std::filesystem::directory_iterator(dir))
Tom Joseph0b14c472018-09-30 01:42:59 +0530108 {
109 std::string id = file.path().filename().c_str();
110 size_t idNum = std::stol(id);
111 auto entryPath = std::string(mapperMgrRoot) + '/' + id;
112 auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
Tom Josephf5bd8912018-11-19 09:49:21 +0530113 bus, entryPath.c_str(), persistPath.c_str(), *this);
Tom Joseph0b14c472018-09-30 01:42:59 +0530114 if (deserialize(file.path(), *entry))
115 {
116 entry->Ifaces::emit_object_added();
117 PrivilegeMapperList.emplace(idNum, std::move(entry));
118 if (idNum > entryId)
119 {
120 entryId = idNum;
121 }
122 }
123 }
124}
125
Tom Joseph536ea322018-09-14 10:02:20 +0530126} // namespace user
127} // namespace phosphor