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