blob: 16955fcf51650469da95b26eed41e011b4623aba [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 Kodihalli40a74062019-01-14 03:34:35 -0600111 fs::create_directory(fs::path(filePath).parent_path());
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500112 std::fstream stream(filePath, std::fstream::out);
113
Deepak Kodihallic4966192018-08-23 02:19:58 -0500114 if (serverPort && !serverAddress.empty())
115 {
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500116 // write '*.* @@<remote-host>:<port>'
Deepak Kodihallic4966192018-08-23 02:19:58 -0500117 stream << "*.* @@" << serverAddress << ":" << serverPort;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500118 }
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500119 else // this is a disable request
120 {
Deepak Kodihallie165ea92019-01-11 03:47:25 -0600121 fs::remove(filePath);
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500122 }
123
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -0500124 restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -0500125}
126
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500127bool 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 Venturef18bf832018-10-26 18:14:00 -0700138 log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()),
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500139 entry("ERRNO=%d", result));
140 return false;
141 }
142 return true;
143}
144
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500145void Server::restore(const char* filePath)
146{
Deepak Kodihallie165ea92019-01-11 03:47:25 -0600147 if (!fs::exists(filePath))
148 {
149 return;
150 }
151
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500152 std::fstream stream(filePath, std::fstream::in);
153 std::string line;
154
Patrick Venture30047bf2018-11-01 18:52:15 -0700155 std::getline(stream, line);
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500156
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 Kodihalli2ce7b2c2018-08-31 04:22:55 -0500173void Server::restart()
174{
175 utils::restart();
176}
177
Deepak Kodihallic4966192018-08-23 02:19:58 -0500178} // namespace rsyslog_config
179} // namespace phosphor