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> |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
| 13 | |
| 14 | #include <experimental/filesystem> |
| 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 | |
| 27 | ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) : |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 28 | details::CreateIface(bus, objPath, true), |
| 29 | dbusPersistentLocation(SNMP_CONF_PERSIST_PATH), bus(bus), |
| 30 | objectPath(objPath) |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 31 | {} |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 32 | |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 33 | std::string ConfManager::client(std::string address, uint16_t port) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 34 | { |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 35 | // will throw exception if it is already configured. |
| 36 | checkClientConfigured(address, port); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 37 | |
| 38 | lastClientId++; |
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 | { |
| 46 | log<level::ERR>("Not a valid address"), |
| 47 | entry("ADDRESS=%s", address.c_str()); |
| 48 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), |
| 49 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 50 | } |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 51 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 52 | // create the D-Bus object |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 53 | std::experimental::filesystem::path objPath; |
| 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 | { |
| 72 | log<level::ERR>("Invalid address"); |
| 73 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"), |
| 74 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 75 | } |
| 76 | |
Asmitha Karunanithi | 9e60ef5 | 2021-05-31 05:52:48 -0500 | [diff] [blame] | 77 | unsigned char buf[sizeof(struct in6_addr)]; |
| 78 | int isValid = inet_pton(AF_INET, address.c_str(), buf); |
| 79 | if (isValid < 1) |
| 80 | { |
| 81 | log<level::ERR>("Invalid address"); |
| 82 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"), |
| 83 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 84 | } |
| 85 | |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 86 | for (const auto& val : clients) |
| 87 | { |
| 88 | if (val.second.get()->address() == address && |
| 89 | val.second.get()->port() == port) |
| 90 | { |
| 91 | log<level::ERR>("Client already exist"); |
| 92 | // TODO Add the error(Object already exist) in the D-Bus interface |
| 93 | // then make the change here,meanwhile send the Internal Failure. |
| 94 | elog<InvalidArgument>( |
| 95 | Argument::ARGUMENT_NAME("ADDRESS"), |
| 96 | Argument::ARGUMENT_VALUE("Client already exist.")); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 101 | void ConfManager::deleteSNMPClient(Id id) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 102 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 103 | auto it = clients.find(id); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 104 | if (it == clients.end()) |
| 105 | { |
| 106 | log<level::ERR>("Unable to delete the snmp client.", |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 107 | entry("ID=%d", id)); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 108 | return; |
| 109 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 110 | |
| 111 | std::error_code ec; |
| 112 | // remove the persistent file |
| 113 | fs::path fileName = dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 114 | fileName /= std::to_string(id); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 115 | |
| 116 | if (fs::exists(fileName)) |
| 117 | { |
| 118 | if (!fs::remove(fileName, ec)) |
| 119 | { |
| 120 | log<level::ERR>("Unable to delete the file", |
| 121 | entry("FILE=%s", fileName.c_str()), |
| 122 | entry("ERROR=%d", ec.value())); |
| 123 | } |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | log<level::ERR>("File doesn't exist", |
| 128 | entry("FILE=%s", fileName.c_str())); |
| 129 | } |
| 130 | // remove the D-Bus Object. |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 131 | this->clients.erase(it); |
| 132 | } |
| 133 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 134 | void ConfManager::restoreClients() |
| 135 | { |
| 136 | if (!fs::exists(dbusPersistentLocation) || |
| 137 | fs::is_empty(dbusPersistentLocation)) |
| 138 | { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | for (auto& confFile : |
| 143 | fs::recursive_directory_iterator(dbusPersistentLocation)) |
| 144 | { |
| 145 | if (!fs::is_regular_file(confFile)) |
| 146 | { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | auto managerID = confFile.path().filename().string(); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 151 | Id idNum = std::stol(managerID); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 152 | |
| 153 | fs::path objPath = objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 154 | objPath /= managerID; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 155 | auto manager = |
| 156 | std::make_unique<Client>(bus, objPath.string().c_str(), *this); |
| 157 | if (deserialize(confFile.path(), *manager)) |
| 158 | { |
| 159 | manager->emit_object_added(); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 160 | this->clients.emplace(idNum, std::move(manager)); |
| 161 | if (idNum > lastClientId) |
| 162 | { |
| 163 | lastClientId = idNum; |
| 164 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 169 | } // namespace snmp |
| 170 | } // namespace network |
| 171 | } // namespace phosphor |