Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 1 | #include "config.h" |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 2 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 3 | #include "snmp_conf_manager.hpp" |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 4 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 5 | #include "snmp_serialize.hpp" |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 6 | #include "snmp_util.hpp" |
| 7 | #include "xyz/openbmc_project/Common/error.hpp" |
| 8 | |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 9 | #include <arpa/inet.h> |
| 10 | |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 13 | |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 14 | #include <filesystem> |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | namespace snmp |
| 21 | { |
| 22 | |
| 23 | using namespace phosphor::logging; |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 24 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 25 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 26 | |
Patrick Williams | 87d3edd | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 27 | ConfManager::ConfManager(sdbusplus::bus_t& bus, const char* objPath) : |
Patrick Williams | b9b4c7d | 2022-04-05 16:20:37 -0500 | [diff] [blame] | 28 | details::CreateIface(bus, objPath, |
| 29 | details::CreateIface::action::defer_emit), |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 30 | dbusPersistentLocation(SNMP_CONF_PERSIST_PATH), bus(bus), |
| 31 | objectPath(objPath) |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 32 | {} |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 33 | |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 34 | std::string ConfManager::client(std::string address, uint16_t port) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 35 | { |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 36 | // will throw exception if it is already configured. |
| 37 | checkClientConfigured(address, port); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 38 | |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 39 | try |
| 40 | { |
| 41 | // just to check whether given address is valid or not. |
| 42 | resolveAddress(address); |
| 43 | } |
Patrick Williams | c3600d8 | 2021-10-06 12:38:25 -0500 | [diff] [blame] | 44 | catch (const InternalFailure& e) |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 45 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 46 | lg2::error("{ADDRESS} is not a valid address", "ADDRESS", address); |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 47 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), |
| 48 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 49 | } |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 50 | |
George Liu | 54d83f5 | 2022-07-28 14:42:01 +0800 | [diff] [blame] | 51 | lastClientId++; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 52 | // create the D-Bus object |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 53 | std::filesystem::path objPath; |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 54 | objPath /= objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 55 | objPath /= std::to_string(lastClientId); |
| 56 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 57 | auto client = std::make_unique<phosphor::network::snmp::Client>( |
| 58 | bus, objPath.string().c_str(), *this, address, port); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 59 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 60 | // save the D-Bus object |
| 61 | serialize(lastClientId, *client, dbusPersistentLocation); |
| 62 | |
| 63 | this->clients.emplace(lastClientId, std::move(client)); |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 64 | return objPath.string(); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 65 | } |
| 66 | |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 67 | void ConfManager::checkClientConfigured(const std::string& address, |
| 68 | uint16_t port) |
| 69 | { |
| 70 | if (address.empty()) |
| 71 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 72 | lg2::error("{ADDRESS} is not a valid address", "ADDRESS", address); |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 73 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"), |
| 74 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 75 | } |
| 76 | |
| 77 | for (const auto& val : clients) |
| 78 | { |
| 79 | if (val.second.get()->address() == address && |
| 80 | val.second.get()->port() == port) |
| 81 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 82 | lg2::error("Client already exist"); |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 83 | // TODO Add the error(Object already exist) in the D-Bus interface |
| 84 | // then make the change here,meanwhile send the Internal Failure. |
| 85 | elog<InvalidArgument>( |
| 86 | Argument::ARGUMENT_NAME("ADDRESS"), |
| 87 | Argument::ARGUMENT_VALUE("Client already exist.")); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 92 | void ConfManager::deleteSNMPClient(Id id) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 93 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 94 | auto it = clients.find(id); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 95 | if (it == clients.end()) |
| 96 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 97 | lg2::error("Unable to delete the snmp client: {ID}", "ID", id); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 98 | return; |
| 99 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 100 | |
| 101 | std::error_code ec; |
| 102 | // remove the persistent file |
| 103 | fs::path fileName = dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 104 | fileName /= std::to_string(id); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 105 | |
| 106 | if (fs::exists(fileName)) |
| 107 | { |
| 108 | if (!fs::remove(fileName, ec)) |
| 109 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 110 | lg2::error("Unable to delete {FILE}: {EC}", "FILE", fileName, "EC", |
| 111 | ec.value()); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 116 | lg2::error("{FILE} doesn't exist", "FILE", fileName); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 117 | } |
| 118 | // remove the D-Bus Object. |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 119 | this->clients.erase(it); |
| 120 | } |
| 121 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 122 | void ConfManager::restoreClients() |
| 123 | { |
| 124 | if (!fs::exists(dbusPersistentLocation) || |
| 125 | fs::is_empty(dbusPersistentLocation)) |
| 126 | { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | for (auto& confFile : |
| 131 | fs::recursive_directory_iterator(dbusPersistentLocation)) |
| 132 | { |
| 133 | if (!fs::is_regular_file(confFile)) |
| 134 | { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | auto managerID = confFile.path().filename().string(); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 139 | Id idNum = std::stol(managerID); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 140 | |
| 141 | fs::path objPath = objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 142 | objPath /= managerID; |
Patrick Williams | 9fa30b9 | 2024-08-16 15:20:25 -0400 | [diff] [blame] | 143 | auto manager = |
| 144 | std::make_unique<Client>(bus, objPath.string().c_str(), *this); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 145 | if (deserialize(confFile.path(), *manager)) |
| 146 | { |
| 147 | manager->emit_object_added(); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 148 | this->clients.emplace(idNum, std::move(manager)); |
| 149 | if (idNum > lastClientId) |
| 150 | { |
| 151 | lastClientId = idNum; |
| 152 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 157 | } // namespace snmp |
| 158 | } // namespace network |
| 159 | } // namespace phosphor |