ZNC
Versions: latest readthedocs-test
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2017 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);
82  static CString FormatServerTime(const timeval& tv);
83  static timeval ParseServerTime(const CString& sTime);
84  static SCString GetTimezones();
85  static SCString GetEncodings();
90  static bool CheckCIDR(const CString& sIP, const CString& sRange);
91 
93  static MCString GetMessageTags(const CString& sLine);
95  static void SetMessageTags(CString& sLine, const MCString& mssTags);
96 
97  private:
98  protected:
99 };
100 
101 class CException {
102  public:
103  typedef enum { EX_Shutdown, EX_Restart } EType;
104 
105  CException(EType e) : m_eType(e) {}
106  virtual ~CException() {}
107 
108  EType GetType() const { return m_eType; }
109 
110  private:
111  protected:
112  EType m_eType;
113 };
114 
115 
141 class CTable : protected std::vector<std::vector<CString>> {
142  public:
143  CTable() {}
144  virtual ~CTable() {}
145 
152  bool AddColumn(const CString& sName);
153 
158  size_type AddRow();
159 
167  bool SetCell(const CString& sColumn, const CString& sValue,
168  size_type uRowIdx = ~0);
169 
175  bool GetLine(unsigned int uIdx, CString& sLine) const;
176 
183  CString::size_type GetColumnWidth(unsigned int uIdx) const;
184 
186  void Clear();
187 
189  using std::vector<std::vector<CString>>::size;
190 
192  using std::vector<std::vector<CString>>::empty;
193 
194  private:
195  unsigned int GetColumnIndex(const CString& sName) const;
196 
197  protected:
198  std::vector<CString> m_vsHeaders;
199  // Used to cache the width of a column
200  std::map<CString, CString::size_type> m_msuWidths;
201 };
202 
203 #ifdef HAVE_LIBSSL
204 #include <openssl/aes.h>
205 #include <openssl/blowfish.h>
206 #include <openssl/md5.h>
208 class CBlowfish {
209  public:
215  CBlowfish(const CString& sPassword, int iEncrypt,
216  const CString& sIvec = "");
217  ~CBlowfish();
218 
219  CBlowfish(const CBlowfish&) = default;
220  CBlowfish& operator=(const CBlowfish&) = default;
221 
223  static unsigned char* MD5(const unsigned char* input, unsigned int ilen);
224 
226  static CString MD5(const CString& sInput, bool bHexEncode = false);
227 
229  void Crypt(unsigned char* input, unsigned char* output,
230  unsigned int ibytes);
231 
233  unsigned char* Crypt(unsigned char* input, unsigned int ibytes);
234  CString Crypt(const CString& sData);
235 
236  private:
237  unsigned char* m_ivec;
238  BF_KEY m_bkey;
239  int m_iEncrypt, m_num;
240 };
241 
242 #endif /* HAVE_LIBSSL */
243 
249 template <typename K, typename V = bool>
250 class TCacheMap {
251  public:
252  TCacheMap(unsigned int uTTL = 5000) : m_mItems(), m_uTTL(uTTL) {}
253 
254  virtual ~TCacheMap() {}
255 
260  void AddItem(const K& Item) { AddItem(Item, m_uTTL); }
261 
267  void AddItem(const K& Item, unsigned int uTTL) { AddItem(Item, V(), uTTL); }
268 
274  void AddItem(const K& Item, const V& Val) { AddItem(Item, Val, m_uTTL); }
275 
282  void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
283  if (!uTTL) {
284  // If time-to-live is zero we don't want to waste our time adding
285  // it
286  RemItem(Item); // Remove the item incase it already exists
287  return;
288  }
289 
290  m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
291  }
292 
298  bool HasItem(const K& Item) {
299  Cleanup();
300  return (m_mItems.find(Item) != m_mItems.end());
301  }
302 
308  V* GetItem(const K& Item) {
309  Cleanup();
310  iterator it = m_mItems.find(Item);
311  if (it == m_mItems.end()) return nullptr;
312  return &it->second.second;
313  }
314 
320  bool RemItem(const K& Item) { return (m_mItems.erase(Item) != 0); }
321 
325  void Cleanup() {
326  iterator it = m_mItems.begin();
327 
328  while (it != m_mItems.end()) {
329  if (CUtils::GetMillTime() > (it->second.first)) {
330  m_mItems.erase(it++);
331  } else {
332  ++it;
333  }
334  }
335  }
336 
340  void Clear() { m_mItems.clear(); }
341 
345  std::map<K, V> GetItems() {
346  Cleanup();
347  std::map<K, V> mItems;
348  for (const auto& it : m_mItems) {
349  mItems[it.first] = it.second.second;
350  }
351  return mItems;
352  }
353 
354  // Setters
355  void SetTTL(unsigned int u) { m_uTTL = u; }
356  // !Setters
357  // Getters
358  unsigned int GetTTL() const { return m_uTTL; }
359  // !Getters
360  protected:
361  typedef std::pair<unsigned long long, V> value;
362  typedef typename std::map<K, value>::iterator iterator;
363  std::map<K, value>
365  unsigned int m_uTTL;
366 };
367 
368 #endif // !ZNC_UTILS_H
std::map< K, V > GetItems()
Returns all entries.
Definition: Utils.h:345
void Cleanup()
Cycles through the queue removing all of the stale entries.
Definition: Utils.h:325
bool HasItem(const K &Item)
Performs a Cleanup() and then checks to see if your item exists.
Definition: Utils.h:298
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:260
V * GetItem(const K &Item)
Performs a Cleanup() and returns a pointer to the object, or nullptr.
Definition: Utils.h:308
EType m_eType
Definition: Utils.h:112
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:208
unsigned int GetTTL() const
Definition: Utils.h:358
std::map< CString, CString::size_type > m_msuWidths
Definition: Utils.h:200
virtual ~CTable()
Definition: Utils.h:144
void SetTTL(unsigned int u)
Definition: Utils.h:355
static unsigned long long GetMillTime()
static void SetMessageTags(CString &sLine, const MCString &mssTags)
Definition: Utils.h:103
std::vector< CString > m_vsHeaders
Definition: Utils.h:198
static void PrintStatus(bool bSuccess, const CString &sMessage="")
Definition: Utils.h:40
static CString GetIP(unsigned long addr)
EType GetType() const
Definition: Utils.h:108
std::set< CString > SCString
Definition: ZNCString.h:35
static unsigned long GetLongIP(const CString &sIP)
static CString GetSalt()
virtual ~CException()
Definition: Utils.h:106
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:105
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:254
static CString SaltedSHA256Hash(const CString &sPass, const CString &sSalt)
bool RemItem(const K &Item)
Removes a specific item from the cache.
Definition: Utils.h:320
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:364
static void PrintAction(const CString &sMessage)
static void PrintError(const CString &sMessage)
Definition: Utils.h:101
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:274
A dictionary for strings.
Definition: ZNCString.h:595
static CString GetPass(const CString &sPrompt)
std::pair< unsigned long long, bool > value
Definition: Utils.h:361
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:267
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:282
Insert an object with a time-to-live and check later if it still exists.
Definition: Utils.h:250
unsigned int m_uTTL
Default time-to-live duration.
Definition: Utils.h:365
static timeval ParseServerTime(const CString &sTime)
TCacheMap(unsigned int uTTL=5000)
Definition: Utils.h:252
static timeval GetTime()
void Clear()
Clear all entries.
Definition: Utils.h:340
CTable()
Definition: Utils.h:143
std::map< CString, value >::iterator iterator
Definition: Utils.h:362
EType
Definition: Utils.h:103
Generate a grid-like output from a given input.
Definition: Utils.h:141
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)