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 | 40a7406 | 2019-01-14 03:34:35 -0600 | [diff] [blame] | 111 | fs::create_directory(fs::path(filePath).parent_path()); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 112 | std::fstream stream(filePath, std::fstream::out); |
| 113 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 114 | if (serverPort && !serverAddress.empty()) |
| 115 | { |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 116 | // write '*.* @@<remote-host>:<port>' |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 117 | stream << "*.* @@" << serverAddress << ":" << serverPort; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 118 | } |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 119 | else // this is a disable request |
| 120 | { |
Deepak Kodihalli | e165ea9 | 2019-01-11 03:47:25 -0600 | [diff] [blame] | 121 | fs::remove(filePath); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 124 | restart(); |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 127 | bool Server::addressValid(const std::string& address) |
| 128 | { |
| 129 | addrinfo hints{}; |
| 130 | addrinfo* res = nullptr; |
| 131 | hints.ai_family = AF_UNSPEC; |
| 132 | hints.ai_socktype = SOCK_STREAM; |
| 133 | hints.ai_flags |= AI_CANONNAME; |
| 134 | |
| 135 | auto result = getaddrinfo(address.c_str(), nullptr, &hints, &res); |
| 136 | if (result) |
| 137 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 138 | log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()), |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 139 | entry("ERRNO=%d", result)); |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 145 | void Server::restore(const char* filePath) |
| 146 | { |
Deepak Kodihalli | e165ea9 | 2019-01-11 03:47:25 -0600 | [diff] [blame] | 147 | if (!fs::exists(filePath)) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 152 | std::fstream stream(filePath, std::fstream::in); |
| 153 | std::string line; |
| 154 | |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 155 | std::getline(stream, line); |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 156 | |
| 157 | // Ignore if line is commented |
| 158 | if ('#' != line.at(0)) |
| 159 | { |
| 160 | auto pos = line.find(':'); |
| 161 | if (pos != std::string::npos) |
| 162 | { |
| 163 | //"*.* @@<address>:<port>" |
| 164 | constexpr auto start = 6; // Skip "*.* @@" |
| 165 | auto serverAddress = line.substr(start, pos - start); |
| 166 | auto serverPort = line.substr(pos + 1); |
| 167 | NetworkClient::address(std::move(serverAddress)); |
| 168 | NetworkClient::port(std::stoul(serverPort)); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 173 | void Server::restart() |
| 174 | { |
| 175 | utils::restart(); |
| 176 | } |
| 177 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 178 | } // namespace rsyslog_config |
| 179 | } // namespace phosphor |