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