Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "ldap_mapper_mgr.hpp" |
| 4 | |
| 5 | #include "ldap_mapper_serialize.hpp" |
| 6 | |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/elog.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | #include <xyz/openbmc_project/User/Common/error.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 12 | |
| 13 | #include <filesystem> |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace user |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
| 21 | using InvalidArgument = |
| 22 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 23 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 24 | using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: |
| 25 | Error::PrivilegeMappingExists; |
| 26 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 27 | LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus& bus, const char* path, |
| 28 | const char* filePath) : |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 29 | MapperMgrIface(bus, path), |
| 30 | bus(bus), path(path), persistPath(filePath) |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 31 | {} |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 32 | |
| 33 | ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege) |
| 34 | { |
| 35 | checkPrivilegeMapper(groupName); |
| 36 | checkPrivilegeLevel(privilege); |
| 37 | |
| 38 | entryId++; |
| 39 | |
| 40 | // Object path for the LDAP group privilege mapper entry |
| 41 | auto mapperObject = |
| 42 | std::string(mapperMgrRoot) + "/" + std::to_string(entryId); |
| 43 | |
| 44 | // Create mapping for LDAP privilege mapper entry |
| 45 | auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>( |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 46 | bus, mapperObject.c_str(), persistPath.c_str(), groupName, privilege, |
| 47 | *this); |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 48 | |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 49 | serialize(*entry, entryId, persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 50 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 51 | PrivilegeMapperList.emplace(entryId, std::move(entry)); |
| 52 | |
| 53 | return mapperObject; |
| 54 | } |
| 55 | |
| 56 | void LDAPMapperMgr::deletePrivilegeMapper(Id id) |
| 57 | { |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 58 | // Delete the persistent representation of the privilege mapper. |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 59 | std::filesystem::path mapperPath(persistPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 60 | mapperPath /= std::to_string(id); |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 61 | std::filesystem::remove(mapperPath); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 62 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 63 | PrivilegeMapperList.erase(id); |
| 64 | } |
| 65 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 66 | void LDAPMapperMgr::checkPrivilegeMapper(const std::string& groupName) |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 67 | { |
| 68 | if (groupName.empty()) |
| 69 | { |
| 70 | log<level::ERR>("Group name is empty"); |
| 71 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group name"), |
| 72 | Argument::ARGUMENT_VALUE("Null")); |
| 73 | } |
| 74 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 75 | for (const auto& val : PrivilegeMapperList) |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 76 | { |
| 77 | if (val.second.get()->groupName() == groupName) |
| 78 | { |
| 79 | log<level::ERR>("Group name already exists"); |
| 80 | elog<PrivilegeMappingExists>(); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 85 | void LDAPMapperMgr::checkPrivilegeLevel(const std::string& privilege) |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 86 | { |
| 87 | if (privilege.empty()) |
| 88 | { |
| 89 | log<level::ERR>("Privilege level is empty"); |
| 90 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"), |
| 91 | Argument::ARGUMENT_VALUE("Null")); |
| 92 | } |
| 93 | |
| 94 | if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end()) |
| 95 | { |
| 96 | log<level::ERR>("Invalid privilege"); |
| 97 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"), |
| 98 | Argument::ARGUMENT_VALUE(privilege.c_str())); |
| 99 | } |
| 100 | } |
| 101 | |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 102 | void LDAPMapperMgr::restore() |
| 103 | { |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 104 | std::filesystem::path dir(persistPath); |
| 105 | if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 106 | { |
| 107 | return; |
| 108 | } |
| 109 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 110 | for (auto& file : std::filesystem::directory_iterator(dir)) |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 111 | { |
| 112 | std::string id = file.path().filename().c_str(); |
| 113 | size_t idNum = std::stol(id); |
| 114 | auto entryPath = std::string(mapperMgrRoot) + '/' + id; |
| 115 | auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>( |
Tom Joseph | f5bd891 | 2018-11-19 09:49:21 +0530 | [diff] [blame] | 116 | bus, entryPath.c_str(), persistPath.c_str(), *this); |
Tom Joseph | 0b14c47 | 2018-09-30 01:42:59 +0530 | [diff] [blame] | 117 | if (deserialize(file.path(), *entry)) |
| 118 | { |
| 119 | entry->Ifaces::emit_object_added(); |
| 120 | PrivilegeMapperList.emplace(idNum, std::move(entry)); |
| 121 | if (idNum > entryId) |
| 122 | { |
| 123 | entryId = idNum; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame] | 129 | } // namespace user |
| 130 | } // namespace phosphor |