/src/znc/include/znc/znc.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2004-2023 ZNC, see the NOTICE file for details. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #ifndef ZNC_H |
18 | | #define ZNC_H |
19 | | |
20 | | #include <znc/zncconfig.h> |
21 | | #include <znc/Client.h> |
22 | | #include <znc/Modules.h> |
23 | | #include <znc/Socket.h> |
24 | | #include <znc/Listener.h> |
25 | | #include <znc/Translation.h> |
26 | | #include <mutex> |
27 | | #include <map> |
28 | | #include <list> |
29 | | |
30 | | class CListener; |
31 | | class CUser; |
32 | | class CIRCNetwork; |
33 | | class CConnectQueueTimer; |
34 | | class CConfigWriteTimer; |
35 | | class CConfig; |
36 | | class CFile; |
37 | | |
38 | | class CZNC : private CCoreTranslationMixin { |
39 | | public: |
40 | | CZNC(); |
41 | | ~CZNC(); |
42 | | |
43 | | CZNC(const CZNC&) = delete; |
44 | | CZNC& operator=(const CZNC&) = delete; |
45 | | |
46 | | enum ConfigState { |
47 | | ECONFIG_NOTHING, |
48 | | ECONFIG_NEED_REHASH, |
49 | | ECONFIG_NEED_WRITE, |
50 | | ECONFIG_NEED_VERBOSE_WRITE, |
51 | | ECONFIG_DELAYED_WRITE, |
52 | | ECONFIG_NEED_QUIT, // Not really config... |
53 | | }; |
54 | | |
55 | | void DeleteUsers(); |
56 | | void Loop(); |
57 | | bool WritePidFile(int iPid); |
58 | | bool DeletePidFile(); |
59 | | bool WaitForChildLock(); |
60 | | bool IsHostAllowed(const CString& sHostMask) const; |
61 | | // This returns false if there are too many anonymous connections from this |
62 | | // ip |
63 | | bool AllowConnectionFrom(const CString& sIP) const; |
64 | | void InitDirs(const CString& sArgvPath, const CString& sDataDir); |
65 | | bool OnBoot(); |
66 | | CString ExpandConfigPath(const CString& sConfigFile, |
67 | | bool bAllowMkDir = true); |
68 | | bool WriteNewConfig(const CString& sConfigFile); |
69 | | bool WriteConfig(); |
70 | | bool ParseConfig(const CString& sConfig, CString& sError); |
71 | | bool RehashConfig(CString& sError); |
72 | | void BackupConfigOnce(const CString& sSuffix); |
73 | | static CString GetVersion(); |
74 | | static CString GetTag(bool bIncludeVersion = true, bool bHTML = false); |
75 | | static CString GetCompileOptionsString(); |
76 | | CString GetUptime() const; |
77 | | /** @deprecated Since 1.7.0. List of allowed bind hosts was a flawed design. */ |
78 | 0 | void ClearBindHosts() {} |
79 | | /** @deprecated Since 1.7.0. List of allowed bind hosts was a flawed design. */ |
80 | 0 | bool AddBindHost(const CString& sHost) { return false; } |
81 | | /** @deprecated Since 1.7.0. List of allowed bind hosts was a flawed design. */ |
82 | 0 | bool RemBindHost(const CString& sHost) { return false; } |
83 | | void ClearTrustedProxies(); |
84 | | bool AddTrustedProxy(const CString& sHost); |
85 | | bool RemTrustedProxy(const CString& sHost); |
86 | | void Broadcast(const CString& sMessage, bool bAdminOnly = false, |
87 | | CUser* pSkipUser = nullptr, CClient* pSkipClient = nullptr); |
88 | 0 | void AddBytesRead(unsigned long long u) { m_uBytesRead += u; } |
89 | 0 | void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; } |
90 | 0 | unsigned long long BytesRead() const { return m_uBytesRead; } |
91 | 0 | unsigned long long BytesWritten() const { return m_uBytesWritten; } |
92 | | |
93 | | // Traffic fun |
94 | | typedef std::pair<unsigned long long, unsigned long long> TrafficStatsPair; |
95 | | typedef std::map<CString, TrafficStatsPair> TrafficStatsMap; |
96 | | // Returns a map which maps user names to <traffic in, traffic out> |
97 | | // while also providing the traffic of all users together, traffic which |
98 | | // couldn't be accounted to any particular user and the total traffic |
99 | | // generated through ZNC. |
100 | | TrafficStatsMap GetTrafficStats(TrafficStatsPair& Users, |
101 | | TrafficStatsPair& ZNC, |
102 | | TrafficStatsPair& Total); |
103 | | TrafficStatsMap GetNetworkTrafficStats(const CString& sUsername, |
104 | | TrafficStatsPair& Total); |
105 | | |
106 | | // Authenticate a user. |
107 | | // The result is passed back via callbacks to CAuthBase. |
108 | | void AuthUser(std::shared_ptr<CAuthBase> AuthClass); |
109 | | |
110 | | // Setters |
111 | 0 | void SetConfigState(enum ConfigState e) { |
112 | 0 | std::lock_guard<std::mutex> guard(m_mutexConfigState); |
113 | 0 | m_eConfigState = e; |
114 | 0 | } |
115 | 0 | void SetSkinName(const CString& s) { m_sSkinName = s; } |
116 | 0 | void SetStatusPrefix(const CString& s) { |
117 | 0 | m_sStatusPrefix = (s.empty()) ? "*" : s; |
118 | 0 | } |
119 | 0 | void SetMaxBufferSize(unsigned int i) { m_uiMaxBufferSize = i; } |
120 | 0 | void SetAnonIPLimit(unsigned int i) { m_uiAnonIPLimit = i; } |
121 | 0 | void SetServerThrottle(unsigned int i) { |
122 | 0 | m_sConnectThrottle.SetTTL(i * 1000); |
123 | 0 | } |
124 | 0 | void SetProtectWebSessions(bool b) { m_bProtectWebSessions = b; } |
125 | 0 | void SetHideVersion(bool b) { m_bHideVersion = b; } |
126 | 0 | void SetAuthOnlyViaModule(bool b) { m_bAuthOnlyViaModule = b; } |
127 | | void SetConnectDelay(unsigned int i); |
128 | 0 | void SetSSLCiphers(const CString& sCiphers) { m_sSSLCiphers = sCiphers; } |
129 | | bool SetSSLProtocols(const CString& sProtocols); |
130 | 0 | void SetSSLCertFile(const CString& sFile) { m_sSSLCertFile = sFile; } |
131 | 0 | void SetConfigWriteDelay(unsigned int i) { m_uiConfigWriteDelay = i; } |
132 | | // !Setters |
133 | | |
134 | | // Getters |
135 | 0 | enum ConfigState GetConfigState() { |
136 | 0 | std::lock_guard<std::mutex> guard(m_mutexConfigState); |
137 | 0 | return m_eConfigState; |
138 | 0 | } |
139 | 0 | CSockManager& GetManager() { return m_Manager; } |
140 | 0 | const CSockManager& GetManager() const { return m_Manager; } |
141 | 0 | CModules& GetModules() { return *m_pModules; } |
142 | 0 | CString GetSkinName() const { return m_sSkinName; } |
143 | 0 | const CString& GetStatusPrefix() const { return m_sStatusPrefix; } |
144 | | const CString& GetCurPath() const; |
145 | | const CString& GetHomePath() const; |
146 | | const CString& GetZNCPath() const; |
147 | | CString GetConfPath(bool bAllowMkDir = true) const; |
148 | | CString GetUserPath() const; |
149 | | CString GetModPath() const; |
150 | | CString GetPemLocation() const; |
151 | | CString GetKeyLocation() const; |
152 | | CString GetDHParamLocation() const; |
153 | 0 | const CString& GetConfigFile() const { return m_sConfigFile; } |
154 | | bool WritePemFile(); |
155 | | /** @deprecated Since 1.7.0. List of allowed bind hosts was a flawed design. */ |
156 | 0 | const VCString& GetBindHosts() const { return m_vsBindHosts; } |
157 | 0 | const VCString& GetTrustedProxies() const { return m_vsTrustedProxies; } |
158 | 0 | const std::vector<CListener*>& GetListeners() const { |
159 | 0 | return m_vpListeners; |
160 | 0 | } |
161 | 0 | time_t TimeStarted() const { return m_TimeStarted; } |
162 | 0 | unsigned int GetMaxBufferSize() const { return m_uiMaxBufferSize; } |
163 | 0 | unsigned int GetAnonIPLimit() const { return m_uiAnonIPLimit; } |
164 | 0 | unsigned int GetServerThrottle() const { |
165 | 0 | return m_sConnectThrottle.GetTTL() / 1000; |
166 | 0 | } |
167 | 0 | unsigned int GetConnectDelay() const { return m_uiConnectDelay; } |
168 | 0 | bool GetProtectWebSessions() const { return m_bProtectWebSessions; } |
169 | 0 | bool GetHideVersion() const { return m_bHideVersion; } |
170 | 0 | bool GetAuthOnlyViaModule() const { return m_bAuthOnlyViaModule; } |
171 | 0 | CString GetSSLCiphers() const { return m_sSSLCiphers; } |
172 | 0 | CString GetSSLProtocols() const { return m_sSSLProtocols; } |
173 | 0 | Csock::EDisableProtocol GetDisabledSSLProtocols() const { |
174 | 0 | return static_cast<Csock::EDisableProtocol>(m_uDisabledSSLProtocols); |
175 | 0 | } |
176 | 0 | CString GetSSLCertFile() const { return m_sSSLCertFile; } |
177 | | static VCString GetAvailableSSLProtocols(); |
178 | 0 | unsigned int GetConfigWriteDelay() const { return m_uiConfigWriteDelay; } |
179 | | // !Getters |
180 | | |
181 | | // Static allocator |
182 | | static void CreateInstance(); |
183 | | static CZNC& Get(); |
184 | | static void DestroyInstance(); |
185 | | CUser* FindUser(const CString& sUsername); |
186 | | CModule* FindModule(const CString& sModName, const CString& sUsername); |
187 | | CModule* FindModule(const CString& sModName, CUser* pUser); |
188 | | |
189 | | /** Reload a module everywhere |
190 | | * |
191 | | * This method will unload a module globally, for a user and for each |
192 | | * network. It will then reload them all again. |
193 | | * |
194 | | * @param sModule The name of the module to reload |
195 | | */ |
196 | | bool UpdateModule(const CString& sModule); |
197 | | |
198 | | bool DeleteUser(const CString& sUsername); |
199 | | bool AddUser(CUser* pUser, CString& sErrorRet, bool bStartup = false); |
200 | 0 | const std::map<CString, CUser*>& GetUserMap() const { return (m_msUsers); } |
201 | | |
202 | | // Listener yummy |
203 | | CListener* FindListener(u_short uPort, const CString& BindHost, |
204 | | EAddrType eAddr); |
205 | | bool AddListener(CListener*); |
206 | | bool AddListener(unsigned short uPort, const CString& sBindHost, |
207 | | const CString& sURIPrefix, bool bSSL, EAddrType eAddr, |
208 | | CListener::EAcceptType eAccept, CString& sError); |
209 | | bool DelListener(CListener*); |
210 | | |
211 | | // Message of the Day |
212 | 0 | void SetMotd(const CString& sMessage) { |
213 | 0 | ClearMotd(); |
214 | 0 | AddMotd(sMessage); |
215 | 0 | } |
216 | 0 | void AddMotd(const CString& sMessage) { |
217 | 0 | if (!sMessage.empty()) { |
218 | 0 | m_vsMotd.push_back(sMessage); |
219 | 0 | } |
220 | 0 | } |
221 | 0 | void ClearMotd() { m_vsMotd.clear(); } |
222 | 0 | const VCString& GetMotd() const { return m_vsMotd; } |
223 | | // !MOTD |
224 | | |
225 | 0 | void AddServerThrottle(CString sName) { |
226 | 0 | m_sConnectThrottle.AddItem(sName, true); |
227 | 0 | } |
228 | 0 | bool GetServerThrottle(CString sName) { |
229 | 0 | bool* b = m_sConnectThrottle.GetItem(sName); |
230 | 0 | return (b && *b); |
231 | 0 | } |
232 | | |
233 | | void AddNetworkToQueue(CIRCNetwork* pNetwork); |
234 | 0 | std::list<CIRCNetwork*>& GetConnectionQueue() { return m_lpConnectQueue; } |
235 | | |
236 | | // This creates a CConnectQueueTimer if we haven't got one yet |
237 | | void EnableConnectQueue(); |
238 | | void DisableConnectQueue(); |
239 | | |
240 | | void PauseConnectQueue(); |
241 | | void ResumeConnectQueue(); |
242 | | |
243 | | void ForceEncoding(); |
244 | | void UnforceEncoding(); |
245 | | bool IsForcingEncoding() const; |
246 | | CString FixupEncoding(const CString& sEncoding) const; |
247 | | |
248 | | // Never call this unless you are CConnectQueueTimer::~CConnectQueueTimer() |
249 | | void LeakConnectQueueTimer(CConnectQueueTimer* pTimer); |
250 | | |
251 | | void DisableConfigTimer(); |
252 | | |
253 | | static void DumpConfig(const CConfig* Config); |
254 | | |
255 | | private: |
256 | | static CString FormatBindError(); |
257 | | |
258 | | CFile* InitPidFile(); |
259 | | |
260 | | bool ReadConfig(CConfig& config, CString& sError); |
261 | | bool LoadGlobal(CConfig& config, CString& sError); |
262 | | bool LoadUsers(CConfig& config, CString& sError); |
263 | | bool LoadListeners(CConfig& config, CString& sError); |
264 | | void UnloadRemovedModules(const MCString& msModules); |
265 | | |
266 | | bool HandleUserDeletion(); |
267 | | CString MakeConfigHeader(); |
268 | | bool AddListener(const CString& sLine, CString& sError); |
269 | | bool AddListener(CConfig* pConfig, CString& sError); |
270 | | |
271 | | protected: |
272 | | time_t m_TimeStarted; |
273 | | |
274 | | enum ConfigState m_eConfigState; |
275 | | std::mutex m_mutexConfigState; |
276 | | |
277 | | std::vector<CListener*> m_vpListeners; |
278 | | std::map<CString, CUser*> m_msUsers; |
279 | | std::map<CString, CUser*> m_msDelUsers; |
280 | | CSockManager m_Manager; |
281 | | |
282 | | CString m_sCurPath; |
283 | | CString m_sZNCPath; |
284 | | |
285 | | CString m_sConfigFile; |
286 | | CString m_sSkinName; |
287 | | CString m_sStatusPrefix; |
288 | | CString m_sPidFile; |
289 | | CString m_sSSLCertFile; |
290 | | CString m_sSSLKeyFile; |
291 | | CString m_sSSLDHParamFile; |
292 | | CString m_sSSLCiphers; |
293 | | CString m_sSSLProtocols; |
294 | | VCString m_vsBindHosts; // TODO: remove (deprecated in 1.7.0) |
295 | | VCString m_vsTrustedProxies; |
296 | | VCString m_vsMotd; |
297 | | CFile* m_pLockFile; |
298 | | unsigned int m_uiConnectDelay; |
299 | | unsigned int m_uiAnonIPLimit; |
300 | | unsigned int m_uiMaxBufferSize; |
301 | | unsigned int m_uDisabledSSLProtocols; |
302 | | CModules* m_pModules; |
303 | | unsigned long long m_uBytesRead; |
304 | | unsigned long long m_uBytesWritten; |
305 | | std::list<CIRCNetwork*> m_lpConnectQueue; |
306 | | CConnectQueueTimer* m_pConnectQueueTimer; |
307 | | unsigned int m_uiConnectPaused; |
308 | | unsigned int m_uiForceEncoding; |
309 | | TCacheMap<CString> m_sConnectThrottle; |
310 | | bool m_bProtectWebSessions; |
311 | | bool m_bHideVersion; |
312 | | bool m_bAuthOnlyViaModule; |
313 | | CTranslationDomainRefHolder m_Translation; |
314 | | unsigned int m_uiConfigWriteDelay; |
315 | | CConfigWriteTimer* m_pConfigTimer; |
316 | | }; |
317 | | |
318 | | #endif // !ZNC_H |