Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 1 | #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 Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame^] | 8 | #include "ldap_mapper_serialize.hpp" |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace user |
| 13 | { |
| 14 | |
| 15 | using namespace phosphor::logging; |
| 16 | using InvalidArgument = |
| 17 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 18 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 19 | using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: |
| 20 | Error::PrivilegeMappingExists; |
| 21 | |
| 22 | LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path) : |
| 23 | MapperMgrIface(bus, path), bus(bus), path(path) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | ObjectPath 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 Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame^] | 42 | serialize(*entry, entryId); |
| 43 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 44 | PrivilegeMapperList.emplace(entryId, std::move(entry)); |
| 45 | |
| 46 | return mapperObject; |
| 47 | } |
| 48 | |
| 49 | void LDAPMapperMgr::deletePrivilegeMapper(Id id) |
| 50 | { |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame^] | 51 | // 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 Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 56 | PrivilegeMapperList.erase(id); |
| 57 | } |
| 58 | |
| 59 | void 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 | |
| 78 | void 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 Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame^] | 95 | void 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 Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 124 | } // namespace user |
| 125 | } // namespace phosphor |