#include #include #include #include #include #include "config.h" #include "ldap_mapper_mgr.hpp" #include "ldap_mapper_serialize.hpp" namespace phosphor { namespace user { using namespace phosphor::logging; using InvalidArgument = sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; using Argument = xyz::openbmc_project::Common::InvalidArgument; using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: Error::PrivilegeMappingExists; LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path) : MapperMgrIface(bus, path), bus(bus), path(path) { } ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege) { checkPrivilegeMapper(groupName); checkPrivilegeLevel(privilege); entryId++; // Object path for the LDAP group privilege mapper entry auto mapperObject = std::string(mapperMgrRoot) + "/" + std::to_string(entryId); // Create mapping for LDAP privilege mapper entry auto entry = std::make_unique( bus, mapperObject.c_str(), groupName, privilege, *this); serialize(*entry, entryId); PrivilegeMapperList.emplace(entryId, std::move(entry)); return mapperObject; } void LDAPMapperMgr::deletePrivilegeMapper(Id id) { // Delete the persistent representation of the privilege mapper. fs::path mapperPath(LDAP_MAPPER_PERSIST_PATH); mapperPath /= std::to_string(id); fs::remove(mapperPath); PrivilegeMapperList.erase(id); } void LDAPMapperMgr::checkPrivilegeMapper(const std::string &groupName) { if (groupName.empty()) { log("Group name is empty"); elog(Argument::ARGUMENT_NAME("Group name"), Argument::ARGUMENT_VALUE("Null")); } for (const auto &val : PrivilegeMapperList) { if (val.second.get()->groupName() == groupName) { log("Group name already exists"); elog(); } } } void LDAPMapperMgr::checkPrivilegeLevel(const std::string &privilege) { if (privilege.empty()) { log("Privilege level is empty"); elog(Argument::ARGUMENT_NAME("Privilege level"), Argument::ARGUMENT_VALUE("Null")); } if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end()) { log("Invalid privilege"); elog(Argument::ARGUMENT_NAME("Privilege level"), Argument::ARGUMENT_VALUE(privilege.c_str())); } } void LDAPMapperMgr::restore() { namespace fs = std::experimental::filesystem; fs::path dir(LDAP_MAPPER_PERSIST_PATH); if (!fs::exists(dir) || fs::is_empty(dir)) { return; } for (auto &file : fs::directory_iterator(dir)) { std::string id = file.path().filename().c_str(); size_t idNum = std::stol(id); auto entryPath = std::string(mapperMgrRoot) + '/' + id; auto entry = std::make_unique( bus, entryPath.c_str(), *this); if (deserialize(file.path(), *entry)) { entry->Ifaces::emit_object_added(); PrivilegeMapperList.emplace(idNum, std::move(entry)); if (idNum > entryId) { entryId = idNum; } } } } } // namespace user } // namespace phosphor