ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
znc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2018 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 {
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();
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);
76  CString GetUptime() const;
78  void ClearBindHosts() {}
80  bool AddBindHost(const CString& sHost) { return false; }
82  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  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
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;
148  CString GetUserPath() const;
149  CString GetModPath() const;
150  CString GetPemLocation() const;
151  CString GetKeyLocation() const;
152  CString GetDHParamLocation() 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);
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  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 
233  void AddNetworkToQueue(CIRCNetwork* pNetwork);
234  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  CFile* InitPidFile();
257 
258  bool ReadConfig(CConfig& config, CString& sError);
259  bool LoadGlobal(CConfig& config, CString& sError);
260  bool LoadUsers(CConfig& config, CString& sError);
261  bool LoadListeners(CConfig& config, CString& sError);
262  void UnloadRemovedModules(const MCString& msModules);
263 
264  bool HandleUserDeletion();
265  CString MakeConfigHeader();
266  bool AddListener(const CString& sLine, CString& sError);
267  bool AddListener(CConfig* pConfig, CString& sError);
268 
269  protected:
271 
273  std::mutex m_mutexConfigState;
274 
275  std::vector<CListener*> m_vpListeners;
276  std::map<CString, CUser*> m_msUsers;
277  std::map<CString, CUser*> m_msDelUsers;
279 
282 
292  VCString m_vsBindHosts; // TODO: remove (deprecated in 1.7.0)
296  unsigned int m_uiConnectDelay;
297  unsigned int m_uiAnonIPLimit;
298  unsigned int m_uiMaxBufferSize;
301  unsigned long long m_uBytesRead;
302  unsigned long long m_uBytesWritten;
303  std::list<CIRCNetwork*> m_lpConnectQueue;
304  CConnectQueueTimer* m_pConnectQueueTimer;
305  unsigned int m_uiConnectPaused;
306  unsigned int m_uiForceEncoding;
312  unsigned int m_uiConfigWriteDelay;
313  CConfigWriteTimer* m_pConfigTimer;
314 };
315 
316 #endif // !ZNC_H
const std::vector< CListener * > & GetListeners() const
Definition: znc.h:158
unsigned int GetConnectDelay() const
Definition: znc.h:167
unsigned int GetMaxBufferSize() const
Definition: znc.h:162
bool AddListener(CListener *)
unsigned long long m_uBytesRead
Definition: znc.h:301
void SetAuthOnlyViaModule(bool b)
Definition: znc.h:126
Definition: User.h:37
const CString & GetConfigFile() const
Definition: znc.h:153
CString m_sPidFile
Definition: znc.h:286
void SetProtectWebSessions(bool b)
Definition: znc.h:124
EAddrType
Definition: Socket.h:73
const CString & GetCurPath() const
unsigned int GetAnonIPLimit() const
Definition: znc.h:163
const CSockManager & GetManager() const
Definition: znc.h:140
void SetConfigWriteDelay(unsigned int i)
Definition: znc.h:131
void DeleteUsers()
CFile * m_pLockFile
Definition: znc.h:295
void AddItem(const K &Item)
This function adds an item to the cache using the default time-to-live value.
Definition: Utils.h:275
CString GetPemLocation() const
unsigned int m_uiConnectDelay
Definition: znc.h:296
CString m_sSSLDHParamFile
Definition: znc.h:289
V * GetItem(const K &Item)
Performs a Cleanup() and returns a pointer to the object, or nullptr.
Definition: Utils.h:323
std::map< CString, TrafficStatsPair > TrafficStatsMap
Definition: znc.h:95
void LeakConnectQueueTimer(CConnectQueueTimer *pTimer)
unsigned int GetConfigWriteDelay() const
Definition: znc.h:178
CString GetSSLCertFile() const
Definition: znc.h:176
static CZNC & Get()
const VCString & GetBindHosts() const
Definition: znc.h:156
VCString m_vsMotd
Definition: znc.h:294
TCacheMap< CString > m_sConnectThrottle
Definition: znc.h:307
unsigned long long m_uBytesWritten
Definition: znc.h:302
std::mutex m_mutexConfigState
Definition: znc.h:273
void SetSSLCiphers(const CString &sCiphers)
Definition: znc.h:128
void AuthUser(std::shared_ptr< CAuthBase > AuthClass)
void SetAnonIPLimit(unsigned int i)
Definition: znc.h:120
CString m_sSSLCiphers
Definition: znc.h:290
unsigned int GetTTL() const
Definition: Utils.h:373
bool DelListener(CListener *)
std::pair< unsigned long long, unsigned long long > TrafficStatsPair
Definition: znc.h:94
void PauseConnectQueue()
Definition: Client.h:99
unsigned long long BytesWritten() const
Definition: znc.h:91
bool WriteConfig()
const CString & GetHomePath() const
std::list< CIRCNetwork * > m_lpConnectQueue
Definition: znc.h:303
void ClearTrustedProxies()
EAcceptType
Definition: Listener.h:29
static CString GetCompileOptionsString()
void SetTTL(unsigned int u)
Definition: Utils.h:370
void SetMaxBufferSize(unsigned int i)
Definition: znc.h:119
CString FixupEncoding(const CString &sEncoding) const
enum ConfigState m_eConfigState
Definition: znc.h:272
CString GetSkinName() const
Definition: znc.h:142
const std::map< CString, CUser * > & GetUserMap() const
Definition: znc.h:200
CString m_sSSLProtocols
Definition: znc.h:291
const VCString & GetTrustedProxies() const
Definition: znc.h:157
Definition: Modules.h:1383
bool RemTrustedProxy(const CString &sHost)
CListener * FindListener(u_short uPort, const CString &BindHost, EAddrType eAddr)
bool RehashConfig(CString &sError)
Definition: Socket.h:75
CString m_sStatusPrefix
Definition: znc.h:285
bool GetAuthOnlyViaModule() const
Definition: znc.h:170
TrafficStatsMap GetNetworkTrafficStats(const CString &sUsername, TrafficStatsPair &Total)
const CString & GetZNCPath() const
CString GetConfPath(bool bAllowMkDir=true) const
unsigned int GetServerThrottle() const
Definition: znc.h:164
void SetConfigState(enum ConfigState e)
Definition: znc.h:111
unsigned int m_uiMaxBufferSize
Definition: znc.h:298
VCString m_vsTrustedProxies
Definition: znc.h:293
CString GetSSLProtocols() const
Definition: znc.h:172
void SetStatusPrefix(const CString &s)
Definition: znc.h:116
bool IsForcingEncoding() const
void AddBytesRead(unsigned long long u)
Definition: znc.h:88
time_t TimeStarted() const
Definition: znc.h:161
unsigned long long BytesRead() const
Definition: znc.h:90
std::map< CString, CUser * > m_msUsers
Definition: znc.h:276
TrafficStatsMap GetTrafficStats(TrafficStatsPair &Users, TrafficStatsPair &ZNC, TrafficStatsPair &Total)
void AddBytesWritten(unsigned long long u)
Definition: znc.h:89
CString GetDHParamLocation() const
bool m_bAuthOnlyViaModule
Definition: znc.h:310
void SetSSLCertFile(const CString &sFile)
Definition: znc.h:130
Definition: znc.h:38
void Broadcast(const CString &sMessage, bool bAdminOnly=false, CUser *pSkipUser=nullptr, CClient *pSkipClient=nullptr)
Definition: FileUtils.h:30
time_t m_TimeStarted
Definition: znc.h:270
Definition: Translation.h:61
Definition: IRCNetwork.h:40
std::vector< CString > VCString
Definition: ZNCString.h:38
bool DeleteUser(const CString &sUsername)
CString GetKeyLocation() const
CSockManager m_Manager
Definition: znc.h:278
Definition: znc.h:52
bool ParseConfig(const CString &sConfig, CString &sError)
void SetConnectDelay(unsigned int i)
static void DumpConfig(const CConfig *Config)
void DisableConfigTimer()
bool GetServerThrottle(CString sName)
Definition: znc.h:228
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool AddTrustedProxy(const CString &sHost)
void UnforceEncoding()
bool WriteNewConfig(const CString &sConfigFile)
static CString GetVersion()
void SetSkinName(const CString &s)
Definition: znc.h:115
void SetServerThrottle(unsigned int i)
Definition: znc.h:121
bool AddBindHost(const CString &sHost)
Definition: znc.h:80
CString m_sSkinName
Definition: znc.h:284
bool WritePemFile()
ConfigState
Definition: znc.h:46
CString m_sSSLKeyFile
Definition: znc.h:288
CString ExpandConfigPath(const CString &sConfigFile, bool bAllowMkDir=true)
The base class for your own ZNC modules.
Definition: Modules.h:407
bool m_bHideVersion
Definition: znc.h:309
Definition: znc.h:48
void InitDirs(const CString &sArgvPath, const CString &sDataDir)
bool UpdateModule(const CString &sModule)
Reload a module everywhere.
EDisableProtocol
Definition: Csocket.h:627
CConfigWriteTimer * m_pConfigTimer
Definition: znc.h:313
void EnableConnectQueue()
static CString GetTag(bool bIncludeVersion=true, bool bHTML=false)
void AddMotd(const CString &sMessage)
Definition: znc.h:216
Definition: Listener.h:27
Csock::EDisableProtocol GetDisabledSSLProtocols() const
Definition: znc.h:173
void Loop()
CTranslationDomainRefHolder m_Translation
Definition: znc.h:311
void ResumeConnectQueue()
CConnectQueueTimer * m_pConnectQueueTimer
Definition: znc.h:304
bool SetSSLProtocols(const CString &sProtocols)
void ClearBindHosts()
Definition: znc.h:78
CString GetUptime() const
bool GetHideVersion() const
Definition: znc.h:169
std::vector< CListener * > m_vpListeners
Definition: znc.h:275
A dictionary for strings.
Definition: ZNCString.h:595
CUser * FindUser(const CString &sUsername)
CString m_sSSLCertFile
Definition: znc.h:287
void DisableConnectQueue()
CSockManager & GetManager()
Definition: znc.h:139
bool RemBindHost(const CString &sHost)
Definition: znc.h:82
CString GetSSLCiphers() const
Definition: znc.h:171
bool DeletePidFile()
CModules * m_pModules
Definition: znc.h:300
unsigned int m_uiConnectPaused
Definition: znc.h:305
VCString m_vsBindHosts
Definition: znc.h:292
unsigned int m_uiConfigWriteDelay
Definition: znc.h:312
Definition: znc.h:49
bool WaitForChildLock()
enum ConfigState GetConfigState()
Definition: znc.h:135
std::map< CString, CUser * > m_msDelUsers
Definition: znc.h:277
const VCString & GetMotd() const
Definition: znc.h:222
bool IsHostAllowed(const CString &sHostMask) const
void SetMotd(const CString &sMessage)
Definition: znc.h:212
Definition: znc.h:47
bool m_bProtectWebSessions
Definition: znc.h:308
unsigned int m_uiForceEncoding
Definition: znc.h:306
void AddNetworkToQueue(CIRCNetwork *pNetwork)
bool GetProtectWebSessions() const
Definition: znc.h:168
CZNC & operator=(const CZNC &)=delete
const CString & GetStatusPrefix() const
Definition: znc.h:143
static VCString GetAvailableSSLProtocols()
static void CreateInstance()
CString m_sZNCPath
Definition: znc.h:281
CModules & GetModules()
Definition: znc.h:141
CString m_sCurPath
Definition: znc.h:280
void ForceEncoding()
CModule * FindModule(const CString &sModName, const CString &sUsername)
std::list< CIRCNetwork * > & GetConnectionQueue()
Definition: znc.h:234
bool AddUser(CUser *pUser, CString &sErrorRet, bool bStartup=false)
void SetHideVersion(bool b)
Definition: znc.h:125
void BackupConfigOnce(const CString &sSuffix)
unsigned int m_uiAnonIPLimit
Definition: znc.h:297
unsigned int m_uDisabledSSLProtocols
Definition: znc.h:299
CString GetModPath() const
static void DestroyInstance()
void ClearMotd()
Definition: znc.h:221
Definition: Config.h:36
bool WritePidFile(int iPid)
CString GetUserPath() const
CString m_sConfigFile
Definition: znc.h:283
bool AllowConnectionFrom(const CString &sIP) const
void AddServerThrottle(CString sName)
Definition: znc.h:225
bool OnBoot()
Definition: znc.h:51