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 | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 6 | #include <fstream> |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog.hpp> |
| 8 | #if __has_include("../../usr/include/phosphor-logging/elog-errors.hpp") |
| 9 | #include "../../usr/include/phosphor-logging/elog-errors.hpp" |
| 10 | #else |
| 11 | #include <phosphor-logging/elog-errors.hpp> |
| 12 | #endif |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 13 | #include <arpa/inet.h> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 14 | #include <netdb.h> |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 15 | |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace rsyslog_config |
| 21 | { |
| 22 | |
| 23 | namespace utils = phosphor::rsyslog_utils; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 24 | using namespace phosphor::logging; |
| 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 26 | |
| 27 | std::string Server::address(std::string value) |
| 28 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 29 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 30 | std::string result{}; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 31 | |
| 32 | try |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 33 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 34 | auto serverAddress = address(); |
| 35 | if (serverAddress == value) |
| 36 | { |
| 37 | return serverAddress; |
| 38 | } |
| 39 | |
| 40 | if (!value.empty() && !addressValid(value)) |
| 41 | { |
| 42 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), |
| 43 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 44 | } |
| 45 | |
| 46 | writeConfig(value, port(), configFilePath.c_str()); |
| 47 | result = std::move(NetworkClient::address(value)); |
| 48 | } |
| 49 | catch (const InvalidArgument& e) |
| 50 | { |
| 51 | throw; |
| 52 | } |
| 53 | catch (const InternalFailure& e) |
| 54 | { |
| 55 | throw; |
| 56 | } |
| 57 | catch (const std::exception& e) |
| 58 | { |
| 59 | log<level::ERR>(e.what()); |
| 60 | elog<InternalFailure>(); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 63 | return result; |
| 64 | } |
| 65 | |
| 66 | uint16_t Server::port(uint16_t value) |
| 67 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 68 | uint16_t result{}; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 69 | |
| 70 | try |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 71 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 72 | auto serverPort = port(); |
| 73 | if (serverPort == value) |
| 74 | { |
| 75 | return serverPort; |
| 76 | } |
| 77 | |
| 78 | writeConfig(address(), value, configFilePath.c_str()); |
| 79 | result = NetworkClient::port(value); |
| 80 | } |
| 81 | catch (const InternalFailure& e) |
| 82 | { |
| 83 | throw; |
| 84 | } |
| 85 | catch (const std::exception& e) |
| 86 | { |
| 87 | log<level::ERR>(e.what()); |
| 88 | elog<InternalFailure>(); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 91 | return result; |
| 92 | } |
| 93 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 94 | void Server::writeConfig(const std::string& serverAddress, uint16_t serverPort, |
| 95 | const char* filePath) |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 96 | { |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 97 | std::fstream stream(filePath, std::fstream::out); |
| 98 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 99 | if (serverPort && !serverAddress.empty()) |
| 100 | { |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 101 | // write '*.* @@<remote-host>:<port>' |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 102 | stream << "*.* @@" << serverAddress << ":" << serverPort; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 103 | } |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 104 | else // this is a disable request |
| 105 | { |
Santosh Puranik | 0a0b5ea | 2019-05-22 05:25:30 -0500 | [diff] [blame] | 106 | // write '*.* ~' - this causes rsyslog to discard all messages |
| 107 | stream << "*.* ~"; |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 110 | restart(); |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 113 | bool Server::addressValid(const std::string& address) |
| 114 | { |
| 115 | addrinfo hints{}; |
| 116 | addrinfo* res = nullptr; |
| 117 | hints.ai_family = AF_UNSPEC; |
| 118 | hints.ai_socktype = SOCK_STREAM; |
| 119 | hints.ai_flags |= AI_CANONNAME; |
| 120 | |
| 121 | auto result = getaddrinfo(address.c_str(), nullptr, &hints, &res); |
| 122 | if (result) |
| 123 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 124 | log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()), |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 125 | entry("ERRNO=%d", result)); |
| 126 | return false; |
| 127 | } |
Patrick Williams | a40c216 | 2021-04-22 09:11:10 -0500 | [diff] [blame] | 128 | |
| 129 | freeaddrinfo(res); |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 133 | void Server::restore(const char* filePath) |
| 134 | { |
| 135 | std::fstream stream(filePath, std::fstream::in); |
| 136 | std::string line; |
| 137 | |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 138 | std::getline(stream, line); |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 139 | |
| 140 | // Ignore if line is commented |
| 141 | if ('#' != line.at(0)) |
| 142 | { |
| 143 | auto pos = line.find(':'); |
| 144 | if (pos != std::string::npos) |
| 145 | { |
| 146 | //"*.* @@<address>:<port>" |
| 147 | constexpr auto start = 6; // Skip "*.* @@" |
| 148 | auto serverAddress = line.substr(start, pos - start); |
| 149 | auto serverPort = line.substr(pos + 1); |
| 150 | NetworkClient::address(std::move(serverAddress)); |
| 151 | NetworkClient::port(std::stoul(serverPort)); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 156 | void Server::restart() |
| 157 | { |
| 158 | utils::restart(); |
| 159 | } |
| 160 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 161 | } // namespace rsyslog_config |
| 162 | } // namespace phosphor |