ZNC  trunk
znc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2024 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 {
52  ECONFIG_NEED_QUIT, // Not really config...
53  };
54 
55  void DeleteUsers();
56  void Loop();
57  bool WritePidFile(int iPid);
58  bool DeletePidFile();
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);
76  CString GetUptime() const;
78  void ClearBindHosts() {}
80  bool AddBindHost(const CString& sHost) { return false; }
82  bool RemBindHost(const CString& sHost) { return false; }
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  void AddBytesRead(unsigned long long u) { m_uBytesRead += u; }
89  void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }
90  unsigned long long BytesRead() const { return m_uBytesRead; }
91  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.
101  TrafficStatsPair& ZNC,
102  TrafficStatsPair& Total);
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
112  std::lock_guard<std::mutex> guard(m_mutexConfigState);
113  m_eConfigState = e;
114  }
115  void SetSkinName(const CString& s) { m_sSkinName = s; }
116  void SetStatusPrefix(const CString& s) {
117  m_sStatusPrefix = (s.empty()) ? "*" : s;
118  }
119  void SetMaxBufferSize(unsigned int i) { m_uiMaxBufferSize = i; }
120  void SetAnonIPLimit(unsigned int i) { m_uiAnonIPLimit = i; }
121  void SetServerThrottle(unsigned int i) {
122  m_sConnectThrottle.SetTTL(i * 1000);
123  }
125  void SetHideVersion(bool b) { m_bHideVersion = b; }
127  void SetConnectDelay(unsigned int i);
128  void SetSSLCiphers(const CString& sCiphers) { m_sSSLCiphers = sCiphers; }
129  bool SetSSLProtocols(const CString& sProtocols);
130  void SetSSLCertFile(const CString& sFile) { m_sSSLCertFile = sFile; }
131  void SetConfigWriteDelay(unsigned int i) { m_uiConfigWriteDelay = i; }
132  // !Setters
133 
134  // Getters
135  enum ConfigState GetConfigState() {
136  std::lock_guard<std::mutex> guard(m_mutexConfigState);
137  return m_eConfigState;
138  }
140  const CSockManager& GetManager() const { return m_Manager; }
142  CString GetSkinName() const { return m_sSkinName; }
143  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;
153  const CString& GetConfigFile() const { return m_sConfigFile; }
154  bool WritePemFile();
156  const VCString& GetBindHosts() const { return m_vsBindHosts; }
157  const VCString& GetTrustedProxies() const { return m_vsTrustedProxies; }
158  const std::vector<CListener*>& GetListeners() const {
159  return m_vpListeners;
160  }
161  time_t TimeStarted() const { return m_TimeStarted; }
162  unsigned int GetMaxBufferSize() const { return m_uiMaxBufferSize; }
163  unsigned int GetAnonIPLimit() const { return m_uiAnonIPLimit; }
164  unsigned int GetServerThrottle() const {
165  return m_sConnectThrottle.GetTTL() / 1000;
166  }
167  unsigned int GetConnectDelay() const { return m_uiConnectDelay; }
169  bool GetHideVersion() const { return m_bHideVersion; }
171  CString GetSSLCiphers() const { return m_sSSLCiphers; }
175  }
178  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 
196  bool UpdateModule(const CString& sModule);
197 
198  bool DeleteUser(const CString& sUsername);
199  bool AddUser(CUser* pUser, CString& sErrorRet, bool bStartup = false);
200  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);
206  bool AddListener(unsigned short uPort, const CString& sBindHost,
207  const CString& sURIPrefix, bool bSSL, EAddrType eAddr,
208  CListener::EAcceptType eAccept, CString& sError);
210 
211  // Message of the Day
212  void SetMotd(const CString& sMessage) {
213  ClearMotd();
214  AddMotd(sMessage);
215  }
216  void AddMotd(const CString& sMessage) {
217  if (!sMessage.empty()) {
218  m_vsMotd.push_back(sMessage);
219  }
220  }
221  void ClearMotd() { m_vsMotd.clear(); }
222  const VCString& GetMotd() const { return m_vsMotd; }
223  // !MOTD
224 
226  m_sConnectThrottle.AddItem(sName, true);
227  }
229  bool* b = m_sConnectThrottle.GetItem(sName);
230  return (b && *b);
231  }
232 
234  std::list<CIRCNetwork*>& GetConnectionQueue() { return m_lpConnectQueue; }
235 
236  // This creates a CConnectQueueTimer if we haven't got one yet
239 
242 
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 
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:
273 
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;
281 
284 
294  VCString m_vsBindHosts; // TODO: remove (deprecated in 1.7.0)
298  unsigned int m_uiConnectDelay;
299  unsigned int m_uiAnonIPLimit;
300  unsigned int m_uiMaxBufferSize;
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;
314  unsigned int m_uiConfigWriteDelay;
315  CConfigWriteTimer* m_pConfigTimer;
316 };
317 
318 #endif // !ZNC_H
EAddrType
Definition: Socket.h:77
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: Client.h:99
Definition: Config.h:36
Definition: Translation.h:103
Definition: FileUtils.h:30
Definition: IRCNetwork.h:40
Definition: Listener.h:27
EAcceptType
Definition: Listener.h:29
The base class for your own ZNC modules.
Definition: Modules.h:420
Definition: Modules.h:1464
Definition: Socket.h:80
String class that is used inside ZNC.
Definition: ZNCString.h:68
Definition: User.h:38
Definition: znc.h:38
bool WritePemFile()
void Loop()
bool AddListener(unsigned short uPort, const CString &sBindHost, const CString &sURIPrefix, bool bSSL, EAddrType eAddr, CListener::EAcceptType eAccept, CString &sError)
unsigned int m_uiForceEncoding
Definition: znc.h:308
const VCString & GetBindHosts() const
Definition: znc.h:156
void SetSkinName(const CString &s)
Definition: znc.h:115
bool m_bProtectWebSessions
Definition: znc.h:310
CTranslationDomainRefHolder m_Translation
Definition: znc.h:313
void SetAuthOnlyViaModule(bool b)
Definition: znc.h:126
unsigned int m_uiConnectPaused
Definition: znc.h:307
unsigned int m_uDisabledSSLProtocols
Definition: znc.h:301
void ClearMotd()
Definition: znc.h:221
CString GetSSLProtocols() const
Definition: znc.h:172
CString m_sSkinName
Definition: znc.h:286
void Broadcast(const CString &sMessage, bool bAdminOnly=false, CUser *pSkipUser=nullptr, CClient *pSkipClient=nullptr)
const CString & GetHomePath() const
TrafficStatsMap GetNetworkTrafficStats(const CString &sUsername, TrafficStatsPair &Total)
void SetAnonIPLimit(unsigned int i)
Definition: znc.h:120
CString GetKeyLocation() const
unsigned long long BytesWritten() const
Definition: znc.h:91
std::vector< CListener * > m_vpListeners
Definition: znc.h:277
CString m_sStatusPrefix
Definition: znc.h:287
void AddServerThrottle(CString sName)
Definition: znc.h:225
CString GetSSLCertFile() const
Definition: znc.h:176
void SetProtectWebSessions(bool b)
Definition: znc.h:124
CString m_sZNCPath
Definition: znc.h:283
void AddNetworkToQueue(CIRCNetwork *pNetwork)
const CString & GetZNCPath() const
void ResumeConnectQueue()
TrafficStatsMap GetTrafficStats(TrafficStatsPair &Users, TrafficStatsPair &ZNC, TrafficStatsPair &Total)
void LeakConnectQueueTimer(CConnectQueueTimer *pTimer)
CString GetSkinName() const
Definition: znc.h:142
bool OnBoot()
CZNC & operator=(const CZNC &)=delete
unsigned int m_uiAnonIPLimit
Definition: znc.h:299
CSockManager & GetManager()
Definition: znc.h:139
void ForceEncoding()
void AuthUser(std::shared_ptr< CAuthBase > AuthClass)
enum ConfigState GetConfigState()
Definition: znc.h:135
CSockManager m_Manager
Definition: znc.h:280
bool AllowConnectionFrom(const CString &sIP) const
CString FixupEncoding(const CString &sEncoding) const
CString GetSSLCiphers() const
Definition: znc.h:171
CConnectQueueTimer * m_pConnectQueueTimer
Definition: znc.h:306
unsigned int m_uiMaxBufferSize
Definition: znc.h:300
bool AddListener(CListener *)
static CString GetCompileOptionsString()
void SetServerThrottle(unsigned int i)
Definition: znc.h:121
TCacheMap< CString > m_sConnectThrottle
Definition: znc.h:309
CModule * FindModule(const CString &sModName, const CString &sUsername)
bool WritePidFile(int iPid)
bool ParseConfig(const CString &sConfig, CString &sError)
void SetSSLCiphers(const CString &sCiphers)
Definition: znc.h:128
time_t m_TimeStarted
Definition: znc.h:272
unsigned int m_uiConfigWriteDelay
Definition: znc.h:314
CString GetModPath() const
unsigned int GetServerThrottle() const
Definition: znc.h:164
void DeleteUsers()
unsigned int m_uiConnectDelay
Definition: znc.h:298
static CZNC & Get()
const std::vector< CListener * > & GetListeners() const
Definition: znc.h:158
static CString GetTag(bool bIncludeVersion=true, bool bHTML=false)
bool GetAuthOnlyViaModule() const
Definition: znc.h:170
std::map< CString, TrafficStatsPair > TrafficStatsMap
Definition: znc.h:95
bool RehashConfig(CString &sError)
bool AddTrustedProxy(const CString &sHost)
void AddMotd(const CString &sMessage)
Definition: znc.h:216
unsigned int GetMaxBufferSize() const
Definition: znc.h:162
bool m_bHideVersion
Definition: znc.h:311
time_t TimeStarted() const
Definition: znc.h:161
bool AddBindHost(const CString &sHost)
Definition: znc.h:80
CString m_sSSLProtocols
Definition: znc.h:293
void SetConnectDelay(unsigned int i)
void DisableConnectQueue()
bool AddUser(CUser *pUser, CString &sErrorRet, bool bStartup=false)
unsigned int GetConfigWriteDelay() const
Definition: znc.h:178
const std::map< CString, CUser * > & GetUserMap() const
Definition: znc.h:200
CString m_sSSLCiphers
Definition: znc.h:292
void SetConfigWriteDelay(unsigned int i)
Definition: znc.h:131
bool RemTrustedProxy(const CString &sHost)
void ClearBindHosts()
Definition: znc.h:78
VCString m_vsMotd
Definition: znc.h:296
std::mutex m_mutexConfigState
Definition: znc.h:275
CFile * m_pLockFile
Definition: znc.h:297
CZNC(const CZNC &)=delete
VCString m_vsTrustedProxies
Definition: znc.h:295
unsigned int GetConnectDelay() const
Definition: znc.h:167
CString GetUptime() const
void SetStatusPrefix(const CString &s)
Definition: znc.h:116
bool DeletePidFile()
std::list< CIRCNetwork * > & GetConnectionQueue()
Definition: znc.h:234
CString m_sSSLDHParamFile
Definition: znc.h:291
VCString m_vsBindHosts
Definition: znc.h:294
ConfigState
Definition: znc.h:46
@ ECONFIG_NEED_QUIT
Definition: znc.h:52
@ ECONFIG_NOTHING
Definition: znc.h:47
@ ECONFIG_NEED_REHASH
Definition: znc.h:48
@ ECONFIG_NEED_VERBOSE_WRITE
Definition: znc.h:50
@ ECONFIG_NEED_WRITE
Definition: znc.h:49
@ ECONFIG_DELAYED_WRITE
Definition: znc.h:51
std::pair< unsigned long long, unsigned long long > TrafficStatsPair
Definition: znc.h:94
bool GetProtectWebSessions() const
Definition: znc.h:168
CString GetDHParamLocation() const
void SetSSLCertFile(const CString &sFile)
Definition: znc.h:130
bool DelListener(CListener *)
CModules & GetModules()
Definition: znc.h:141
static void CreateInstance()
void InitDirs(const CString &sArgvPath, const CString &sDataDir)
static CString GetVersion()
static void DumpConfig(const CConfig *Config)
CString GetConfPath(bool bAllowMkDir=true) const
bool WaitForChildLock()
const CString & GetStatusPrefix() const
Definition: znc.h:143
const VCString & GetMotd() const
Definition: znc.h:222
void EnableConnectQueue()
std::map< CString, CUser * > m_msUsers
Definition: znc.h:278
const CString & GetCurPath() const
void AddBytesRead(unsigned long long u)
Definition: znc.h:88
Csock::EDisableProtocol GetDisabledSSLProtocols() const
Definition: znc.h:173
void SetMaxBufferSize(unsigned int i)
Definition: znc.h:119
CModules * m_pModules
Definition: znc.h:302
unsigned long long m_uBytesRead
Definition: znc.h:303
bool WriteNewConfig(const CString &sConfigFile)
bool DeleteUser(const CString &sUsername)
CString m_sSSLCertFile
Definition: znc.h:289
CString m_sSSLKeyFile
Definition: znc.h:290
CListener * FindListener(u_short uPort, const CString &BindHost, EAddrType eAddr)
std::map< CString, CUser * > m_msDelUsers
Definition: znc.h:279
void DisableConfigTimer()
CString GetPemLocation() const
CString m_sConfigFile
Definition: znc.h:285
CString m_sCurPath
Definition: znc.h:282
bool RemBindHost(const CString &sHost)
Definition: znc.h:82
bool GetServerThrottle(CString sName)
Definition: znc.h:228
CUser * FindUser(const CString &sUsername)
static void DestroyInstance()
std::list< CIRCNetwork * > m_lpConnectQueue
Definition: znc.h:305
bool GetHideVersion() const
Definition: znc.h:169
void UnforceEncoding()
void SetHideVersion(bool b)
Definition: znc.h:125
CConfigWriteTimer * m_pConfigTimer
Definition: znc.h:315
bool SetSSLProtocols(const CString &sProtocols)
void AddBytesWritten(unsigned long long u)
Definition: znc.h:89
unsigned long long BytesRead() const
Definition: znc.h:90
bool IsHostAllowed(const CString &sHostMask) const
CModule * FindModule(const CString &sModName, CUser *pUser)
bool m_bAuthOnlyViaModule
Definition: znc.h:312
void BackupConfigOnce(const CString &sSuffix)
void PauseConnectQueue()
CString m_sPidFile
Definition: znc.h:288
void SetMotd(const CString &sMessage)
Definition: znc.h:212
bool IsForcingEncoding() const
void SetConfigState(enum ConfigState e)
Definition: znc.h:111
const VCString & GetTrustedProxies() const
Definition: znc.h:157
const CSockManager & GetManager() const
Definition: znc.h:140
static VCString GetAvailableSSLProtocols()
bool UpdateModule(const CString &sModule)
Reload a module everywhere.
bool WriteConfig()
CString ExpandConfigPath(const CString &sConfigFile, bool bAllowMkDir=true)
CString GetUserPath() const
const CString & GetConfigFile() const
Definition: znc.h:153
unsigned int GetAnonIPLimit() const
Definition: znc.h:163
enum ConfigState m_eConfigState
Definition: znc.h:274
void ClearTrustedProxies()
unsigned long long m_uBytesWritten
Definition: znc.h:304
EDisableProtocol
Definition: Csocket.h:627
A dictionary for strings.
Definition: ZNCString.h:595
void AddItem(const K &Item)
This function adds an item to the cache using the default time-to-live value.
Definition: Utils.h:301
unsigned int GetTTL() const
Definition: Utils.h:399
void SetTTL(unsigned int u)
Definition: Utils.h:396
V * GetItem(const K &Item)
Performs a Cleanup() and returns a pointer to the object, or nullptr.
Definition: Utils.h:349
Definition: Translation.h:62