ZNC  trunk
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2020 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_UTILS_H
18 #define ZNC_UTILS_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/ZNCString.h>
22 #include <assert.h>
23 #include <cstdio>
24 #include <fcntl.h>
25 #include <map>
26 #include <sys/file.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #include <vector>
30 
31 static inline void SetFdCloseOnExec(int fd) {
32  int flags = fcntl(fd, F_GETFD, 0);
33  if (flags < 0) return; // Ignore errors
34  // When we execve() a new process this fd is now automatically closed.
35  fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
36 }
37 
38 static const char g_HexDigits[] = "0123456789abcdef";
39 
40 class CUtils {
41  public:
42  CUtils();
43  ~CUtils();
44 
45  static CString GetIP(unsigned long addr);
46  static unsigned long GetLongIP(const CString& sIP);
47 
48  static void PrintError(const CString& sMessage);
49  static void PrintMessage(const CString& sMessage, bool bStrong = false);
50  static void PrintPrompt(const CString& sMessage);
51  static void PrintAction(const CString& sMessage);
52  static void PrintStatus(bool bSuccess, const CString& sMessage = "");
53 
54 #ifndef SWIGPERL
55  // TODO refactor this
56  static const CString sDefaultHash;
57 #endif
58 
59  static CString GetSaltedHashPass(CString& sSalt);
60  static CString GetSalt();
61  static CString SaltedMD5Hash(const CString& sPass, const CString& sSalt);
62  static CString SaltedSHA256Hash(const CString& sPass, const CString& sSalt);
63  static CString GetPass(const CString& sPrompt);
64  static bool GetInput(const CString& sPrompt, CString& sRet,
65  const CString& sDefault = "",
66  const CString& sHint = "");
67  static bool GetBoolInput(const CString& sPrompt, bool bDefault);
68  static bool GetBoolInput(const CString& sPrompt, bool* pbDefault = nullptr);
69  static bool GetNumInput(const CString& sPrompt, unsigned int& uRet,
70  unsigned int uMin = 0, unsigned int uMax = ~0,
71  unsigned int uDefault = ~0);
72 
73  static timeval GetTime();
74  static unsigned long long GetMillTime();
75 #ifdef HAVE_LIBSSL
76  static void GenerateCert(FILE* pOut, const CString& sHost = "");
77 #endif /* HAVE_LIBSSL */
78 
79  static CString CTime(time_t t, const CString& sTZ);
80  static CString FormatTime(time_t t, const CString& sFormat,
81  const CString& sTZ);
95  static CString FormatTime(const timeval& tv, const CString& sFormat,
96  const CString& sTZ);
97  static CString FormatServerTime(const timeval& tv);
98  static timeval ParseServerTime(const CString& sTime);
99  static SCString GetTimezones();
100  static SCString GetEncodings();
105  static bool CheckCIDR(const CString& sIP, const CString& sRange);
106 
108  static MCString GetMessageTags(const CString& sLine);
110  static void SetMessageTags(CString& sLine, const MCString& mssTags);
111 
112  private:
113  protected:
114 };
115 
116 class CException {
117  public:
118  typedef enum { EX_Shutdown, EX_Restart } EType;
119 
120  CException(EType e) : m_eType(e) {}
121  virtual ~CException() {}
122 
123  EType GetType() const { return m_eType; }
124 
125  private:
126  protected:
127  EType m_eType;
128 };
129 
130 
171 class CTable : protected std::vector<std::vector<CString>> {
172  public:
173  enum EStyle { GridStyle, ListStyle };
174  CTable() {}
175  virtual ~CTable() {}
176 
184  bool AddColumn(const CString& sName);
185 
191  bool SetStyle(EStyle eNewStyle);
192 
197  size_type AddRow();
198 
206  bool SetCell(const CString& sColumn, const CString& sValue,
207  size_type uRowIdx = ~0);
208 
214  bool GetLine(unsigned int uIdx, CString& sLine) const;
215 
222  CString::size_type GetColumnWidth(unsigned int uIdx) const;
223 
225  void Clear();
226 
228  using std::vector<std::vector<CString>>::size;
229 
231  using std::vector<std::vector<CString>>::empty;
232 
233  private:
234  unsigned int GetColumnIndex(const CString& sName) const;
235 
236  protected:
237  std::vector<CString> m_vsHeaders;
238  // Used to cache the width of a column
239  std::map<CString, CString::size_type> m_msuWidths;
240  EStyle eStyle = GridStyle;
241 };
242 
243 #ifdef HAVE_LIBSSL
244 #include <openssl/aes.h>
245 #include <openssl/blowfish.h>
246 #include <openssl/md5.h>
248 class CBlowfish {
249  public:
255  CBlowfish(const CString& sPassword, int iEncrypt,
256  const CString& sIvec = "");
257  ~CBlowfish();
258 
259  CBlowfish(const CBlowfish&) = default;
260  CBlowfish& operator=(const CBlowfish&) = default;
261 
263  static unsigned char* MD5(const unsigned char* input, unsigned int ilen);
264 
266  static CString MD5(const CString& sInput, bool bHexEncode = false);
267 
269  void Crypt(unsigned char* input, unsigned char* output,
270  unsigned int ibytes);
271 
273  unsigned char* Crypt(unsigned char* input, unsigned int ibytes);
274  CString Crypt(const CString& sData);
275 
276  private:
277  unsigned char* m_ivec;
278  BF_KEY m_bkey;
279  int m_iEncrypt, m_num;
280 };
281 
282 #endif /* HAVE_LIBSSL */
283 
289 template <typename K, typename V = bool>
290 class TCacheMap {
291  public:
292  TCacheMap(unsigned int uTTL = 5000) : m_mItems(), m_uTTL(uTTL) {}
293 
294  virtual ~TCacheMap() {}
295 
300  void AddItem(const K& Item) { AddItem(Item, m_uTTL); }
301 
307  void AddItem(const K& Item, unsigned int uTTL) { AddItem(Item, V(), uTTL); }
308 
314  void AddItem(const K& Item, const V& Val) { AddItem(Item, Val, m_uTTL); }
315 
322  void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
323  if (!uTTL) {
324  // If time-to-live is zero we don't want to waste our time adding
325  // it
326  RemItem(Item); // Remove the item incase it already exists
327  return;
328  }
329 
330  m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
331  }
332 
338  bool HasItem(const K& Item) {
339  Cleanup();
340  return (m_mItems.find(Item) != m_mItems.end());
341  }
342 
348  V* GetItem(const K& Item) {
349  Cleanup();
350  iterator it = m_mItems.find(Item);
351  if (it == m_mItems.end()) return nullptr;
352  return &it->second.second;
353  }
354 
360  bool RemItem(const K& Item) { return (m_mItems.erase(Item) != 0); }
361 
365  void Cleanup() {
366  iterator it = m_mItems.begin();
367 
368  while (it != m_mItems.end()) {
369  if (CUtils::GetMillTime() > (it->second.first)) {
370  m_mItems.erase(it++);
371  } else {
372  ++it;
373  }
374  }
375  }
376 
380  void Clear() { m_mItems.clear(); }
381 
385  std::map<K, V> GetItems() {
386  Cleanup();
387  std::map<K, V> mItems;
388  for (const auto& it : m_mItems) {
389  mItems[it.first] = it.second.second;
390  }
391  return mItems;
392  }
393 
394  // Setters
395  void SetTTL(unsigned int u) { m_uTTL = u; }
396  // !Setters
397  // Getters
398  unsigned int GetTTL() const { return m_uTTL; }
399  // !Getters
400  protected:
401  typedef std::pair<unsigned long long, V> value;
402  typedef typename std::map<K, value>::iterator iterator;
403  std::map<K, value>
405  unsigned int m_uTTL;
406 };
407 
408 #endif // !ZNC_UTILS_H
EType GetType() const
Definition: Utils.h:123
std::map< K, V > GetItems()
Returns all entries.
Definition: Utils.h:385
void Cleanup()
Cycles through the queue removing all of the stale entries.
Definition: Utils.h:365
bool HasItem(const K &Item)
Performs a Cleanup() and then checks to see if your item exists.
Definition: Utils.h:338
static void PrintMessage(const CString &sMessage, bool bStrong=false)
void AddItem(const K &Item)
This function adds an item to the cache using the default time-to-live value.
Definition: Utils.h:300
V * GetItem(const K &Item)
Performs a Cleanup() and returns a pointer to the object, or nullptr.
Definition: Utils.h:348
unsigned int GetTTL() const
Definition: Utils.h:398
EType m_eType
Definition: Utils.h:127
static CString GetSaltedHashPass(CString &sSalt)
static bool CheckCIDR(const CString &sIP, const CString &sRange)
CIDR notation checker, e.g.
does Blowfish w/64 bit feedback, no padding
Definition: Utils.h:248
std::map< CString, CString::size_type > m_msuWidths
Definition: Utils.h:239
virtual ~CTable()
Definition: Utils.h:175
void SetTTL(unsigned int u)
Definition: Utils.h:395
static unsigned long long GetMillTime()
static void SetMessageTags(CString &sLine, const MCString &mssTags)
Definition: Utils.h:118
EStyle
Definition: Utils.h:173
std::vector< CString > m_vsHeaders
Definition: Utils.h:237
static void PrintStatus(bool bSuccess, const CString &sMessage="")
Definition: Utils.h:40
static CString GetIP(unsigned long addr)
std::set< CString > SCString
Definition: ZNCString.h:35
static unsigned long GetLongIP(const CString &sIP)
static CString GetSalt()
virtual ~CException()
Definition: Utils.h:121
static bool GetNumInput(const CString &sPrompt, unsigned int &uRet, unsigned int uMin=0, unsigned int uMax=~0, unsigned int uDefault=~0)
static const CString sDefaultHash
Definition: Utils.h:56
static SCString GetEncodings()
static MCString GetMessageTags(const CString &sLine)
CException(EType e)
Definition: Utils.h:120
static CString FormatTime(time_t t, const CString &sFormat, const CString &sTZ)
String class that is used inside ZNC.
Definition: ZNCString.h:68
virtual ~TCacheMap()
Definition: Utils.h:294
static CString SaltedSHA256Hash(const CString &sPass, const CString &sSalt)
bool RemItem(const K &Item)
Removes a specific item from the cache.
Definition: Utils.h:360
static CString FormatServerTime(const timeval &tv)
static SCString GetTimezones()
static bool GetBoolInput(const CString &sPrompt, bool bDefault)
std::map< K, value > m_mItems
Map of cached items.
Definition: Utils.h:404
static void PrintAction(const CString &sMessage)
static void PrintError(const CString &sMessage)
Definition: Utils.h:116
void AddItem(const K &Item, const V &Val)
This function adds an item to the cache using the default time-to-live value.
Definition: Utils.h:314
A dictionary for strings.
Definition: ZNCString.h:595
static CString GetPass(const CString &sPrompt)
std::pair< unsigned long long, bool > value
Definition: Utils.h:401
void AddItem(const K &Item, unsigned int uTTL)
This function adds an item to the cache using a custom time-to-live value.
Definition: Utils.h:307
static CString CTime(time_t t, const CString &sTZ)
void AddItem(const K &Item, const V &Val, unsigned int uTTL)
This function adds an item to the cache using a custom time-to-live value.
Definition: Utils.h:322
Insert an object with a time-to-live and check later if it still exists.
Definition: Utils.h:290
unsigned int m_uTTL
Default time-to-live duration.
Definition: Utils.h:405
static timeval ParseServerTime(const CString &sTime)
TCacheMap(unsigned int uTTL=5000)
Definition: Utils.h:292
static timeval GetTime()
void Clear()
Clear all entries.
Definition: Utils.h:380
CTable()
Definition: Utils.h:174
std::map< CString, value >::iterator iterator
Definition: Utils.h:402
EType
Definition: Utils.h:118
Generate a grid-like or list-like output from a given input.
Definition: Utils.h:171
static void GenerateCert(FILE *pOut, const CString &sHost="")
static CString SaltedMD5Hash(const CString &sPass, const CString &sSalt)
static bool GetInput(const CString &sPrompt, CString &sRet, const CString &sDefault="", const CString &sHint="")
static void PrintPrompt(const CString &sMessage)