1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/server/object.hpp>
5 #include <string>
6 #include <phosphor-logging/log.hpp>
7 #include "xyz/openbmc_project/Network/Client/server.hpp"
8 
9 namespace phosphor
10 {
11 namespace rsyslog_config
12 {
13 
14 using namespace phosphor::logging;
15 using NetworkClient = sdbusplus::xyz::openbmc_project::Network::server::Client;
16 using Iface = sdbusplus::server::object::object<NetworkClient>;
17 
18 /** @class Server
19  *  @brief Configuration for rsyslog server
20  *  @details A concrete implementation of the
21  *  xyz.openbmc_project.Network.Client API, in order to
22  *  provide remote rsyslog server's address and port.
23  */
24 class Server : public Iface
25 {
26     public:
27         Server() = delete;
28         Server(const Server&) = delete;
29         Server& operator=(const Server&) = delete;
30         Server(Server&&) = delete;
31         Server& operator=(Server&&) = delete;
32         virtual ~Server() = default;
33 
34         /** @brief Constructor to put object onto bus at a dbus path.
35          *  @param[in] bus - Bus to attach to.
36          *  @param[in] path - Path to attach at.
37          *  @param[in] filePath - rsyslog remote logging config file
38          */
39         Server(sdbusplus::bus::bus& bus,
40                const std::string& path,
41                const char* filePath) :
42             Iface(bus, path.c_str(), true),
43             configFilePath(filePath)
44         {
45             try
46             {
47                 restore(configFilePath.c_str());
48             }
49             catch(const std::exception& e)
50             {
51                 log<level::ERR>(e.what());
52             }
53 
54             emit_object_added();
55         }
56 
57         using NetworkClient::address;
58         using NetworkClient::port;
59 
60         /** @brief Override that updates rsyslog config file as well
61          *  @param[in] value - remote server address
62          *  @returns value of changed address
63          */
64         virtual std::string address(std::string value) override;
65 
66         /** @brief Override that updates rsyslog config file as well
67          *  @param[in] value - remote server port
68          *  @returns value of changed port
69          */
70         virtual uint16_t port(uint16_t value) override;
71 
72         /** @brief Restart rsyslog's systemd unit
73          */
74         virtual void restart();
75 
76     private:
77         /** @brief Update remote server address and port in
78          *         rsyslog config file.
79          *  @param[in] serverAddress - remote server address
80          *  @param[in] serverPort - remote server port
81          *  @param[in] filePath - rsyslog config file path
82          */
83         void writeConfig(
84                  const std::string& serverAddress,
85                  uint16_t serverPort,
86                  const char* filePath);
87 
88         /** @brief Checks if input IP address is valid (uses getaddrinfo)
89          *  @param[in] address - server address
90          *  @returns true if valid, false otherwise
91          */
92         bool addressValid(const std::string& address);
93 
94         /** @brief Populate existing config into D-Bus properties
95          *  @param[in] filePath - rsyslog config file path
96          */
97         void restore(const char* filePath);
98 
99         std::string configFilePath{};
100 };
101 
102 } // namespace rsyslog_config
103 } // namespace phosphor
104