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 | |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 22 | LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path, |
| 23 | const char *filePath) : |
| 24 | MapperMgrIface(bus, path), |
| 25 | bus(bus), path(path), persistPath(filePath) |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 26 | { |
| 27 | } |
| 28 | |
| 29 | ObjectPath 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 Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 42 | bus, mapperObject.c_str(), persistPath.c_str(), groupName, privilege, |
| 43 | *this); |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 44 | |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 45 | serialize(*entry, entryId, persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 46 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 47 | PrivilegeMapperList.emplace(entryId, std::move(entry)); |
| 48 | |
| 49 | return mapperObject; |
| 50 | } |
| 51 | |
| 52 | void LDAPMapperMgr::deletePrivilegeMapper(Id id) |
| 53 | { |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 54 | // Delete the persistent representation of the privilege mapper. |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 55 | fs::path mapperPath(persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 56 | mapperPath /= std::to_string(id); |
| 57 | fs::remove(mapperPath); |
| 58 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 59 | PrivilegeMapperList.erase(id); |
| 60 | } |
| 61 | |
| 62 | void 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 | |
| 81 | void 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 Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 98 | void LDAPMapperMgr::restore() |
| 99 | { |
| 100 | namespace fs = std::experimental::filesystem; |
| 101 | |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 102 | fs::path dir(persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 103 | 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 Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 114 | bus, entryPath.c_str(), persistPath.c_str(), *this); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 115 | 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 Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 127 | } // namespace user |
| 128 | } // namespace phosphor |