Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 1 | #include <filesystem> |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 2 | #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 Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 9 | #include "ldap_mapper_serialize.hpp" |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace user |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | using InvalidArgument = |
| 18 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 19 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 20 | using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: |
| 21 | Error::PrivilegeMappingExists; |
| 22 | |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 23 | LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path, |
| 24 | const char *filePath) : |
| 25 | MapperMgrIface(bus, path), |
| 26 | bus(bus), path(path), persistPath(filePath) |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
| 30 | ObjectPath 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 Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 43 | bus, mapperObject.c_str(), persistPath.c_str(), groupName, privilege, |
| 44 | *this); |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 45 | |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 46 | serialize(*entry, entryId, persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 47 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 48 | PrivilegeMapperList.emplace(entryId, std::move(entry)); |
| 49 | |
| 50 | return mapperObject; |
| 51 | } |
| 52 | |
| 53 | void LDAPMapperMgr::deletePrivilegeMapper(Id id) |
| 54 | { |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 55 | // Delete the persistent representation of the privilege mapper. |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 56 | std::filesystem::path mapperPath(persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 57 | mapperPath /= std::to_string(id); |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 58 | std::filesystem::remove(mapperPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 59 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 60 | PrivilegeMapperList.erase(id); |
| 61 | } |
| 62 | |
| 63 | void 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 | |
| 82 | void 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 Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 99 | void LDAPMapperMgr::restore() |
| 100 | { |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 101 | std::filesystem::path dir(persistPath); |
| 102 | if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 103 | { |
| 104 | return; |
| 105 | } |
| 106 | |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame^] | 107 | for (auto &file : std::filesystem::directory_iterator(dir)) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 108 | { |
| 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 Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 113 | bus, entryPath.c_str(), persistPath.c_str(), *this); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 114 | 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 Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 126 | } // namespace user |
| 127 | } // namespace phosphor |