Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 1 | #include "server-conf.hpp" |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 2 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 3 | #include "utils.hpp" |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 5 | |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/elog.hpp> |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 7 | |
| 8 | #include <fstream> |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 9 | #if __has_include("../../usr/include/phosphor-logging/elog-errors.hpp") |
| 10 | #include "../../usr/include/phosphor-logging/elog-errors.hpp" |
| 11 | #else |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
| 13 | #endif |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 14 | #include <arpa/inet.h> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 15 | #include <netdb.h> |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 16 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 17 | #include <optional> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 20 | namespace phosphor |
| 21 | { |
| 22 | namespace rsyslog_config |
| 23 | { |
| 24 | |
| 25 | namespace utils = phosphor::rsyslog_utils; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 26 | using namespace phosphor::logging; |
| 27 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 28 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 29 | namespace internal |
| 30 | { |
| 31 | |
| 32 | bool isIPv6Address(const std::string& addr) |
| 33 | { |
| 34 | struct in6_addr result; |
| 35 | return inet_pton(AF_INET6, addr.c_str(), &result) == 1; |
| 36 | } |
| 37 | |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 38 | std::optional< |
| 39 | std::tuple<std::string, uint32_t, NetworkClient::TransportProtocol>> |
| 40 | parseConfig(std::istream& ss) |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 41 | { |
| 42 | std::string line; |
| 43 | std::getline(ss, line); |
| 44 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 45 | std::string serverAddress; |
| 46 | std::string serverPort; |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 47 | NetworkClient::TransportProtocol serverTransportProtocol = |
| 48 | NetworkClient::TransportProtocol::TCP; |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 49 | |
| 50 | // Ignore if line is commented |
| 51 | if (!line.empty() && '#' != line.at(0)) |
| 52 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 53 | //"*.* @@<address>:<port>" or |
| 54 | //"*.* @@[<ipv6-address>:<port>" |
| 55 | auto start = line.find('@'); |
| 56 | if (start == std::string::npos) |
| 57 | return {}; |
| 58 | |
| 59 | // Skip "*.* @@" or "*.* @" |
| 60 | if (line.at(start + 1) == '@') |
| 61 | { |
| 62 | serverTransportProtocol = NetworkClient::TransportProtocol::TCP; |
| 63 | start += 2; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | serverTransportProtocol = NetworkClient::TransportProtocol::UDP; |
| 68 | start++; |
| 69 | } |
| 70 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 71 | // Check if there is "[]", and make IPv6 address from it |
| 72 | auto posColonLeft = line.find('['); |
| 73 | auto posColonRight = line.find(']'); |
| 74 | if (posColonLeft != std::string::npos || |
| 75 | posColonRight != std::string::npos) |
| 76 | { |
| 77 | // It contains [ or ], so it should be an IPv6 address |
| 78 | if (posColonLeft == std::string::npos || |
| 79 | posColonRight == std::string::npos) |
| 80 | { |
| 81 | // There either '[' or ']', invalid config |
| 82 | return {}; |
| 83 | } |
| 84 | if (line.size() < posColonRight + 2 || |
| 85 | line.at(posColonRight + 1) != ':') |
| 86 | { |
| 87 | // There is no ':', or no more content after ':', invalid config |
| 88 | return {}; |
| 89 | } |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 90 | serverAddress = line.substr(posColonLeft + 1, |
| 91 | posColonRight - posColonLeft - 1); |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 92 | serverPort = line.substr(posColonRight + 2); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | auto pos = line.find(':'); |
| 97 | if (pos == std::string::npos) |
| 98 | { |
| 99 | // There is no ':', invalid config |
| 100 | return {}; |
| 101 | } |
| 102 | serverAddress = line.substr(start, pos - start); |
| 103 | serverPort = line.substr(pos + 1); |
| 104 | } |
| 105 | } |
| 106 | if (serverAddress.empty() || serverPort.empty()) |
| 107 | { |
| 108 | return {}; |
| 109 | } |
| 110 | try |
| 111 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 112 | return std::make_tuple(std::move(serverAddress), std::stoul(serverPort), |
| 113 | serverTransportProtocol); |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 114 | } |
| 115 | catch (const std::exception& ex) |
| 116 | { |
| 117 | log<level::ERR>("Invalid config", entry("ERR=%s", ex.what())); |
| 118 | return {}; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } // namespace internal |
| 123 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 124 | std::string Server::address(std::string value) |
| 125 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 126 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 127 | std::string result{}; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 128 | |
| 129 | try |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 130 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 131 | auto serverAddress = address(); |
| 132 | if (serverAddress == value) |
| 133 | { |
| 134 | return serverAddress; |
| 135 | } |
| 136 | |
| 137 | if (!value.empty() && !addressValid(value)) |
| 138 | { |
| 139 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), |
| 140 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 141 | } |
| 142 | |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 143 | writeConfig(value, port(), transportProtocol(), configFilePath.c_str()); |
Patrick Williams | 511a475 | 2021-08-28 15:23:26 -0500 | [diff] [blame] | 144 | result = NetworkClient::address(value); |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 145 | } |
| 146 | catch (const InvalidArgument& e) |
| 147 | { |
| 148 | throw; |
| 149 | } |
| 150 | catch (const InternalFailure& e) |
| 151 | { |
| 152 | throw; |
| 153 | } |
| 154 | catch (const std::exception& e) |
| 155 | { |
| 156 | log<level::ERR>(e.what()); |
| 157 | elog<InternalFailure>(); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 160 | return result; |
| 161 | } |
| 162 | |
| 163 | uint16_t Server::port(uint16_t value) |
| 164 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 165 | uint16_t result{}; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 166 | |
| 167 | try |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 168 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 169 | auto serverPort = port(); |
| 170 | if (serverPort == value) |
| 171 | { |
| 172 | return serverPort; |
| 173 | } |
| 174 | |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 175 | writeConfig(address(), value, transportProtocol(), |
| 176 | configFilePath.c_str()); |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 177 | result = NetworkClient::port(value); |
| 178 | } |
| 179 | catch (const InternalFailure& e) |
| 180 | { |
| 181 | throw; |
| 182 | } |
| 183 | catch (const std::exception& e) |
| 184 | { |
| 185 | log<level::ERR>(e.what()); |
| 186 | elog<InternalFailure>(); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 189 | return result; |
| 190 | } |
| 191 | |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 192 | NetworkClient::TransportProtocol |
| 193 | Server::transportProtocol(NetworkClient::TransportProtocol value) |
| 194 | { |
| 195 | TransportProtocol result{}; |
| 196 | |
| 197 | try |
| 198 | { |
| 199 | auto serverTransportProtocol = transportProtocol(); |
| 200 | if (serverTransportProtocol == value) |
| 201 | { |
| 202 | return serverTransportProtocol; |
| 203 | } |
| 204 | |
| 205 | writeConfig(address(), port(), value, configFilePath.c_str()); |
| 206 | result = NetworkClient::transportProtocol(value); |
| 207 | } |
| 208 | catch (const InternalFailure& e) |
| 209 | { |
| 210 | throw; |
| 211 | } |
| 212 | catch (const std::exception& e) |
| 213 | { |
| 214 | log<level::ERR>(e.what()); |
| 215 | elog<InternalFailure>(); |
| 216 | } |
| 217 | |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | void Server::writeConfig( |
| 222 | const std::string& serverAddress, uint16_t serverPort, |
| 223 | NetworkClient::TransportProtocol serverTransportProtocol, |
| 224 | const char* filePath) |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 225 | { |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 226 | std::fstream stream(filePath, std::fstream::out); |
| 227 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 228 | if (serverPort && !serverAddress.empty()) |
| 229 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 230 | std::string type = |
| 231 | (serverTransportProtocol == NetworkClient::TransportProtocol::UDP) |
| 232 | ? "@" |
| 233 | : "@@"; |
| 234 | // write '*.* @@<remote-host>:<port>' or '*.* @<remote-host>:<port>' |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 235 | if (internal::isIPv6Address(serverAddress)) |
| 236 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 237 | stream << "*.* " << type << "[" << serverAddress |
| 238 | << "]:" << serverPort; |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 242 | stream << "*.* " << type << serverAddress << ":" << serverPort; |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 243 | } |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 244 | } |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 245 | else // this is a disable request |
| 246 | { |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 247 | // dummy action to avoid error 2103 on startup |
| 248 | stream << "*.* /dev/null"; |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 251 | stream << std::endl; |
| 252 | |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 253 | restart(); |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 256 | bool Server::addressValid(const std::string& address) |
| 257 | { |
| 258 | addrinfo hints{}; |
| 259 | addrinfo* res = nullptr; |
| 260 | hints.ai_family = AF_UNSPEC; |
| 261 | hints.ai_socktype = SOCK_STREAM; |
| 262 | hints.ai_flags |= AI_CANONNAME; |
| 263 | |
| 264 | auto result = getaddrinfo(address.c_str(), nullptr, &hints, &res); |
| 265 | if (result) |
| 266 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 267 | log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()), |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 268 | entry("ERRNO=%d", result)); |
| 269 | return false; |
| 270 | } |
Patrick Williams | a40c216 | 2021-04-22 09:11:10 -0500 | [diff] [blame] | 271 | |
| 272 | freeaddrinfo(res); |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 276 | void Server::restore(const char* filePath) |
| 277 | { |
| 278 | std::fstream stream(filePath, std::fstream::in); |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 279 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 280 | auto ret = internal::parseConfig(stream); |
| 281 | if (ret) |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 282 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 283 | NetworkClient::address(std::get<0>(*ret)); |
| 284 | NetworkClient::port(std::get<1>(*ret)); |
| 285 | NetworkClient::transportProtocol(std::get<2>(*ret)); |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 289 | void Server::restart() |
| 290 | { |
| 291 | utils::restart(); |
| 292 | } |
| 293 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 294 | } // namespace rsyslog_config |
| 295 | } // namespace phosphor |