blob: a32133152b1a65b377ce0c83aaad344ff638aaba [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
22LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path) :
23 MapperMgrIface(bus, path), bus(bus), path(path)
24{
25}
26
27ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege)
28{
29 checkPrivilegeMapper(groupName);
30 checkPrivilegeLevel(privilege);
31
32 entryId++;
33
34 // Object path for the LDAP group privilege mapper entry
35 auto mapperObject =
36 std::string(mapperMgrRoot) + "/" + std::to_string(entryId);
37
38 // Create mapping for LDAP privilege mapper entry
39 auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
40 bus, mapperObject.c_str(), groupName, privilege, *this);
41
Tom Joseph0b14c472018-09-30 01:42:59 +053042 serialize(*entry, entryId);
43
Tom Joseph536ea322018-09-14 10:02:20 +053044 PrivilegeMapperList.emplace(entryId, std::move(entry));
45
46 return mapperObject;
47}
48
49void LDAPMapperMgr::deletePrivilegeMapper(Id id)
50{
Tom Joseph0b14c472018-09-30 01:42:59 +053051 // Delete the persistent representation of the privilege mapper.
52 fs::path mapperPath(LDAP_MAPPER_PERSIST_PATH);
53 mapperPath /= std::to_string(id);
54 fs::remove(mapperPath);
55
Tom Joseph536ea322018-09-14 10:02:20 +053056 PrivilegeMapperList.erase(id);
57}
58
59void LDAPMapperMgr::checkPrivilegeMapper(const std::string &groupName)
60{
61 if (groupName.empty())
62 {
63 log<level::ERR>("Group name is empty");
64 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group name"),
65 Argument::ARGUMENT_VALUE("Null"));
66 }
67
68 for (const auto &val : PrivilegeMapperList)
69 {
70 if (val.second.get()->groupName() == groupName)
71 {
72 log<level::ERR>("Group name already exists");
73 elog<PrivilegeMappingExists>();
74 }
75 }
76}
77
78void LDAPMapperMgr::checkPrivilegeLevel(const std::string &privilege)
79{
80 if (privilege.empty())
81 {
82 log<level::ERR>("Privilege level is empty");
83 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"),
84 Argument::ARGUMENT_VALUE("Null"));
85 }
86
87 if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end())
88 {
89 log<level::ERR>("Invalid privilege");
90 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"),
91 Argument::ARGUMENT_VALUE(privilege.c_str()));
92 }
93}
94
Tom Joseph0b14c472018-09-30 01:42:59 +053095void LDAPMapperMgr::restore()
96{
97 namespace fs = std::experimental::filesystem;
98
99 fs::path dir(LDAP_MAPPER_PERSIST_PATH);
100 if (!fs::exists(dir) || fs::is_empty(dir))
101 {
102 return;
103 }
104
105 for (auto &file : fs::directory_iterator(dir))
106 {
107 std::string id = file.path().filename().c_str();
108 size_t idNum = std::stol(id);
109 auto entryPath = std::string(mapperMgrRoot) + '/' + id;
110 auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>(
111 bus, entryPath.c_str(), *this);
112 if (deserialize(file.path(), *entry))
113 {
114 entry->Ifaces::emit_object_added();
115 PrivilegeMapperList.emplace(idNum, std::move(entry));
116 if (idNum > entryId)
117 {
118 entryId = idNum;
119 }
120 }
121 }
122}
123
Tom Joseph536ea322018-09-14 10:02:20 +0530124} // namespace user
125} // namespace phosphor