blob: e8da82e7ac65adab9c714cec9048519e079573c6 [file] [log] [blame]
Deepak Kodihallic4966192018-08-23 02:19:58 -05001#include "server-conf.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07002
Deepak Kodihallic4966192018-08-23 02:19:58 -05003#include "utils.hpp"
Deepak Kodihalli4db81462018-08-27 06:01:46 -05004#include "xyz/openbmc_project/Common/error.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07005
Deepak Kodihallic4966192018-08-23 02:19:58 -05006#include <fstream>
Deepak Kodihalli4db81462018-08-27 06:01:46 -05007#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 Kodihalli4db81462018-08-27 06:01:46 -050013#include <arpa/inet.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070014#include <netdb.h>
Deepak Kodihallic4966192018-08-23 02:19:58 -050015
Patrick Venture30047bf2018-11-01 18:52:15 -070016#include <string>
17
Deepak Kodihallie165ea92019-01-11 03:47:25 -060018#if __has_include(<filesystem>)
19#include <filesystem>
20#elif __has_include(<experimental/filesystem>)
21#include <experimental/filesystem>
22namespace std
23{
24// splice experimental::filesystem into std
25namespace filesystem = std::experimental::filesystem;
26} // namespace std
27#else
28#error filesystem not available
29#endif
30
Deepak Kodihallic4966192018-08-23 02:19:58 -050031namespace phosphor
32{
33namespace rsyslog_config
34{
35
36namespace utils = phosphor::rsyslog_utils;
Deepak Kodihalli4db81462018-08-27 06:01:46 -050037using namespace phosphor::logging;
38using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Deepak Kodihallie165ea92019-01-11 03:47:25 -060039namespace fs = std::filesystem;
Deepak Kodihallic4966192018-08-23 02:19:58 -050040
41std::string Server::address(std::string value)
42{
Deepak Kodihalli4db81462018-08-27 06:01:46 -050043 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Patrick Venturef18bf832018-10-26 18:14:00 -070044 std::string result{};
Deepak Kodihalli4db81462018-08-27 06:01:46 -050045
46 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050047 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050048 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 Kodihalli0febd262018-08-27 05:45:02 -050075 }
76
Deepak Kodihallic4966192018-08-23 02:19:58 -050077 return result;
78}
79
80uint16_t Server::port(uint16_t value)
81{
Patrick Venturef18bf832018-10-26 18:14:00 -070082 uint16_t result{};
Deepak Kodihalli4db81462018-08-27 06:01:46 -050083
84 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050085 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050086 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 Kodihalli0febd262018-08-27 05:45:02 -0500103 }
104
Deepak Kodihallic4966192018-08-23 02:19:58 -0500105 return result;
106}
107
Patrick Venturef18bf832018-10-26 18:14:00 -0700108void Server::writeConfig(const std::string& serverAddress, uint16_t serverPort,
109 const char* filePath)
Deepak Kodihallic4966192018-08-23 02:19:58 -0500110{
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500111 std::fstream stream(filePath, std::fstream::out);
112
Deepak Kodihallic4966192018-08-23 02:19:58 -0500113 if (serverPort && !serverAddress.empty())
114 {
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500115 // write '*.* @@<remote-host>:<port>'
Deepak Kodihallic4966192018-08-23 02:19:58 -0500116 stream << "*.* @@" << serverAddress << ":" << serverPort;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500117 }
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500118 else // this is a disable request
119 {
Deepak Kodihallie165ea92019-01-11 03:47:25 -0600120 fs::remove(filePath);
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500121 }
122
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -0500123 restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -0500124}
125
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500126bool 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 Venturef18bf832018-10-26 18:14:00 -0700137 log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()),
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500138 entry("ERRNO=%d", result));
139 return false;
140 }
141 return true;
142}
143
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500144void Server::restore(const char* filePath)
145{
Deepak Kodihallie165ea92019-01-11 03:47:25 -0600146 if (!fs::exists(filePath))
147 {
148 return;
149 }
150
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500151 std::fstream stream(filePath, std::fstream::in);
152 std::string line;
153
Patrick Venture30047bf2018-11-01 18:52:15 -0700154 std::getline(stream, line);
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500155
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 Kodihalli2ce7b2c2018-08-31 04:22:55 -0500172void Server::restart()
173{
174 utils::restart();
175}
176
Deepak Kodihallic4966192018-08-23 02:19:58 -0500177} // namespace rsyslog_config
178} // namespace phosphor