blob: 61bfaf09dd0b385c3a705ce63e94419058f5a00c [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 Kodihallic4966192018-08-23 02:19:58 -050018namespace phosphor
19{
20namespace rsyslog_config
21{
22
23namespace utils = phosphor::rsyslog_utils;
Deepak Kodihalli4db81462018-08-27 06:01:46 -050024using namespace phosphor::logging;
25using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Deepak Kodihallic4966192018-08-23 02:19:58 -050026
27std::string Server::address(std::string value)
28{
Deepak Kodihalli4db81462018-08-27 06:01:46 -050029 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Patrick Venturef18bf832018-10-26 18:14:00 -070030 std::string result{};
Deepak Kodihalli4db81462018-08-27 06:01:46 -050031
32 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050033 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050034 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 Kodihalli0febd262018-08-27 05:45:02 -050061 }
62
Deepak Kodihallic4966192018-08-23 02:19:58 -050063 return result;
64}
65
66uint16_t Server::port(uint16_t value)
67{
Patrick Venturef18bf832018-10-26 18:14:00 -070068 uint16_t result{};
Deepak Kodihalli4db81462018-08-27 06:01:46 -050069
70 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050071 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050072 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 Kodihalli0febd262018-08-27 05:45:02 -050089 }
90
Deepak Kodihallic4966192018-08-23 02:19:58 -050091 return result;
92}
93
Patrick Venturef18bf832018-10-26 18:14:00 -070094void Server::writeConfig(const std::string& serverAddress, uint16_t serverPort,
95 const char* filePath)
Deepak Kodihallic4966192018-08-23 02:19:58 -050096{
Deepak Kodihalli0febd262018-08-27 05:45:02 -050097 std::fstream stream(filePath, std::fstream::out);
98
Deepak Kodihallic4966192018-08-23 02:19:58 -050099 if (serverPort && !serverAddress.empty())
100 {
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500101 // write '*.* @@<remote-host>:<port>'
Deepak Kodihallic4966192018-08-23 02:19:58 -0500102 stream << "*.* @@" << serverAddress << ":" << serverPort;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500103 }
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500104 else // this is a disable request
105 {
Santosh Puranik0a0b5ea2019-05-22 05:25:30 -0500106 // write '*.* ~' - this causes rsyslog to discard all messages
107 stream << "*.* ~";
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500108 }
109
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -0500110 restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -0500111}
112
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500113bool 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 Venturef18bf832018-10-26 18:14:00 -0700124 log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()),
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500125 entry("ERRNO=%d", result));
126 return false;
127 }
Patrick Williamsa40c2162021-04-22 09:11:10 -0500128
129 freeaddrinfo(res);
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500130 return true;
131}
132
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500133void Server::restore(const char* filePath)
134{
135 std::fstream stream(filePath, std::fstream::in);
136 std::string line;
137
Patrick Venture30047bf2018-11-01 18:52:15 -0700138 std::getline(stream, line);
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500139
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 Kodihalli2ce7b2c2018-08-31 04:22:55 -0500156void Server::restart()
157{
158 utils::restart();
159}
160
Deepak Kodihallic4966192018-08-23 02:19:58 -0500161} // namespace rsyslog_config
162} // namespace phosphor