1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "remote/apilistener.thpp"
24 #include "remote/jsonrpcconnection.hpp"
25 #include "remote/httpserverconnection.hpp"
26 #include "remote/endpoint.hpp"
27 #include "remote/messageorigin.hpp"
28 #include "base/configobject.hpp"
29 #include "base/timer.hpp"
30 #include "base/workqueue.hpp"
31 #include "base/tcpsocket.hpp"
32 #include "base/tlsstream.hpp"
38 class JsonRpcConnection;
43 struct ConfigDirInformation
45 Dictionary::Ptr UpdateV1;
46 Dictionary::Ptr UpdateV2;
52 class ApiListener final : public ObjectImpl<ApiListener>
55 DECLARE_OBJECT(ApiListener);
56 DECLARE_OBJECTNAME(ApiListener);
58 static boost::signals2::signal<void(bool)> OnMasterChanged;
62 static String GetApiDir();
63 static String GetCertsDir();
64 static String GetCaDir();
65 static String GetCertificateRequestsDir();
67 void UpdateSSLContext();
69 static ApiListener::Ptr GetInstance();
71 Endpoint::Ptr GetMaster() const;
72 bool IsMaster() const;
74 Endpoint::Ptr GetLocalEndpoint() const;
76 void SyncSendMessage(const Endpoint::Ptr& endpoint, const Dictionary::Ptr& message);
77 void RelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
79 static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
80 std::pair<Dictionary::Ptr, Dictionary::Ptr> GetStatus();
82 void AddAnonymousClient(const JsonRpcConnection::Ptr& aclient);
83 void RemoveAnonymousClient(const JsonRpcConnection::Ptr& aclient);
84 std::set<JsonRpcConnection::Ptr> GetAnonymousClients() const;
86 void AddHttpClient(const HttpServerConnection::Ptr& aclient);
87 void RemoveHttpClient(const HttpServerConnection::Ptr& aclient);
88 std::set<HttpServerConnection::Ptr> GetHttpClients() const;
90 static double CalculateZoneLag(const Endpoint::Ptr& endpoint);
93 static Value ConfigUpdateHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
96 static void ConfigUpdateObjectHandler(const ConfigObject::Ptr& object, const Value& cookie);
97 static Value ConfigUpdateObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
98 static Value ConfigDeleteObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
100 static Value HelloAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
102 static void UpdateObjectAuthority();
104 static bool IsHACluster();
105 static String GetFromZoneName(const Zone::Ptr& fromZone);
107 static String GetDefaultCertPath();
108 static String GetDefaultKeyPath();
109 static String GetDefaultCaPath();
112 void OnConfigLoaded() override;
113 void OnAllConfigLoaded() override;
114 void Start(bool runtimeCreated) override;
115 void Stop(bool runtimeDeleted) override;
117 void ValidateTlsProtocolmin(const Lazy<String>& lvalue, const ValidationUtils& utils) override;
120 std::shared_ptr<SSL_CTX> m_SSLContext;
121 std::set<TcpSocket::Ptr> m_Servers;
123 mutable boost::mutex m_AnonymousClientsLock;
124 mutable boost::mutex m_HttpClientsLock;
125 std::set<JsonRpcConnection::Ptr> m_AnonymousClients;
126 std::set<HttpServerConnection::Ptr> m_HttpClients;
129 Timer::Ptr m_ReconnectTimer;
130 Timer::Ptr m_AuthorityTimer;
131 Timer::Ptr m_CleanupCertificateRequestsTimer;
132 Endpoint::Ptr m_LocalEndpoint;
134 static ApiListener::Ptr m_Instance;
136 void ApiTimerHandler();
137 void ApiReconnectTimerHandler();
138 void CleanupCertificateRequestsTimerHandler();
140 bool AddListener(const String& node, const String& service);
141 void AddConnection(const Endpoint::Ptr& endpoint);
143 void NewClientHandler(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
144 void NewClientHandlerInternal(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
145 void ListenerThreadProc(const Socket::Ptr& server);
147 WorkQueue m_RelayQueue;
148 WorkQueue m_SyncQueue{0, 4};
150 boost::mutex m_LogLock;
151 Stream::Ptr m_LogFile;
152 size_t m_LogMessageCount{0};
154 bool RelayMessageOne(const Zone::Ptr& zone, const MessageOrigin::Ptr& origin, const Dictionary::Ptr& message, const Endpoint::Ptr& currentMaster);
155 void SyncRelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
156 void PersistMessage(const Dictionary::Ptr& message, const ConfigObject::Ptr& secobj);
159 void RotateLogFile();
161 static void LogGlobHandler(std::vector<int>& files, const String& file);
162 void ReplayLog(const JsonRpcConnection::Ptr& client);
164 static void CopyCertificateFile(const String& oldCertPath, const String& newCertPath);
167 static ConfigDirInformation LoadConfigDir(const String& dir);
168 static Dictionary::Ptr MergeConfigUpdate(const ConfigDirInformation& config);
169 static bool UpdateConfigDir(const ConfigDirInformation& oldConfig, const ConfigDirInformation& newConfig, const String& configDir, bool authoritative);
171 void SyncZoneDirs() const;
172 void SyncZoneDir(const Zone::Ptr& zone) const;
174 static void ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file);
175 void SendConfigUpdate(const JsonRpcConnection::Ptr& aclient);
178 void UpdateConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
179 const JsonRpcConnection::Ptr& client = nullptr);
180 void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
181 const JsonRpcConnection::Ptr& client = nullptr);
182 void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);
184 void SyncClient(const JsonRpcConnection::Ptr& aclient, const Endpoint::Ptr& endpoint, bool needSync);
189 #endif /* APILISTENER_H */