ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utils.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_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 
121  virtual ~CException() {}
122 
123  EType GetType() const { return m_eType; }
124 
125  private:
126  protected:
128 };
129 
130 
156 class CTable : protected std::vector<std::vector<CString>> {
157  public:
158  CTable() {}
159  virtual ~CTable() {}
160 
167  bool AddColumn(const CString& sName);
168 
173  size_type AddRow();
174 
182  bool SetCell(const CString& sColumn, const CString& sValue,
183  size_type uRowIdx = ~0);
184 
190  bool GetLine(unsigned int uIdx, CString& sLine) const;
191 
198  CString::size_type GetColumnWidth(unsigned int uIdx) const;
199 
201  void Clear();
202 
204  using std::vector<std::vector<CString>>::size;
205 
207  using std::vector<std::vector<CString>>::empty;
208 
209  private:
210  unsigned int GetColumnIndex(const CString& sName) const;
211 
212  protected:
213  std::vector<CString> m_vsHeaders;
214  // Used to cache the width of a column
215  std::map<CString, CString::size_type> m_msuWidths;
216 };
217 
218 #ifdef HAVE_LIBSSL
219 #include <openssl/aes.h>
220 #include <openssl/blowfish.h>
221 #include <openssl/md5.h>
223 class CBlowfish {
224  public:
230  CBlowfish(const CString& sPassword, int iEncrypt,
231  const CString& sIvec = "");
232  ~CBlowfish();
233 
234  CBlowfish(const CBlowfish&) = default;
235  CBlowfish& operator=(const CBlowfish&) = default;
236 
238  static unsigned char* MD5(const unsigned char* input, unsigned int ilen);
239 
241  static CString MD5(const CString& sInput, bool bHexEncode = false);
242 
244  void Crypt(unsigned char* input, unsigned char* output,
245  unsigned int ibytes);
246 
248  unsigned char* Crypt(unsigned char* input, unsigned int ibytes);
249  CString Crypt(const CString& sData);
250 
251  private:
252  unsigned char* m_ivec;
253  BF_KEY m_bkey;
254  int m_iEncrypt, m_num;
255 };
256 
257 #endif /* HAVE_LIBSSL */
258 
264 template <typename K, typename V = bool>
265 class TCacheMap {
266  public:
267  TCacheMap(unsigned int uTTL = 5000) : m_mItems(), m_uTTL(uTTL) {}
268 
269  virtual ~TCacheMap() {}
270 
275  void AddItem(const K& Item) { AddItem(Item, m_uTTL); }
276 
282  void AddItem(const K& Item, unsigned int uTTL) { AddItem(Item, V(), uTTL); }
283 
289  void AddItem(const K& Item, const V& Val) { AddItem(Item, Val, m_uTTL); }
290 
297  void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
298  if (!uTTL) {
299  // If time-to-live is zero we don't want to waste our time adding
300  // it
301  RemItem(Item); // Remove the item incase it already exists
302  return;
303  }
304 
305  m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
306  }
307 
313  bool HasItem(const K& Item) {
314  Cleanup();
315  return (m_mItems.find(Item) != m_mItems.end());
316  }
317 
323  V* GetItem(const K& Item) {
324  Cleanup();
325  iterator it = m_mItems.find(Item);
326  if (it == m_mItems.end()) return nullptr;
327  return &it->second.second;
328  }
329 
335  bool RemItem(const K& Item) { return (m_mItems.erase(Item) != 0); }
336 
340  void Cleanup() {
341  iterator it = m_mItems.begin();
342 
343  while (it != m_mItems.end()) {
344  if (CUtils::GetMillTime() > (it->second.first)) {
345  m_mItems.erase(it++);
346  } else {
347  ++it;
348  }
349  }
350  }
351 
355  void Clear() { m_mItems.clear(); }
356 
360  std::map<K, V> GetItems() {
361  Cleanup();
362  std::map<K, V> mItems;
363  for (const auto& it : m_mItems) {
364  mItems[it.first] = it.second.second;
365  }
366  return mItems;
367  }
368 
369  // Setters
370  void SetTTL(unsigned int u) { m_uTTL = u; }
371  // !Setters
372  // Getters
373  unsigned int GetTTL() const { return m_uTTL; }
374  // !Getters
375  protected:
376  typedef std::pair<unsigned long long, V> value;
377  typedef typename std::map<K, value>::iterator iterator;
378  std::map<K, value>
380  unsigned int m_uTTL;
381 };
382 
383 #endif // !ZNC_UTILS_H
std::map< K, V > GetItems()
Returns all entries.
Definition: Utils.h:360
void Cleanup()
Cycles through the queue removing all of the stale entries.
Definition: Utils.h:340
bool HasItem(const K &Item)
Performs a Cleanup() and then checks to see if your item exists.
Definition: Utils.h:313
void Crypt(unsigned char *input, unsigned char *output, unsigned int ibytes)
output must be the same size as input
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:275
V * GetItem(const K &Item)
Performs a Cleanup() and returns a pointer to the object, or nullptr.
Definition: Utils.h:323
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.
Definition: Utils.h:118
does Blowfish w/64 bit feedback, no padding
Definition: Utils.h:223
unsigned int GetTTL() const
Definition: Utils.h:373
std::map< CString, CString::size_type > m_msuWidths
Definition: Utils.h:215
virtual ~CTable()
Definition: Utils.h:159
void SetTTL(unsigned int u)
Definition: Utils.h:370
static unsigned long long GetMillTime()
static void SetMessageTags(CString &sLine, const MCString &mssTags)
Definition: Utils.h:118
std::vector< CString > m_vsHeaders
Definition: Utils.h:213
static void PrintStatus(bool bSuccess, const CString &sMessage="")
Definition: Utils.h:40
static CString GetIP(unsigned long addr)
EType GetType() const
Definition: Utils.h:123
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)
CBlowfish & operator=(const CBlowfish &)=default
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
bool AddColumn(const CString &sName)
Adds a new column to the table.
virtual ~TCacheMap()
Definition: Utils.h:269
bool GetLine(unsigned int uIdx, CString &sLine) const
Get a line of the table's output.
static CString SaltedSHA256Hash(const CString &sPass, const CString &sSalt)
bool RemItem(const K &Item)
Removes a specific item from the cache.
Definition: Utils.h:335
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:379
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:289
A dictionary for strings.
Definition: ZNCString.h:595
static CString GetPass(const CString &sPrompt)
std::pair< unsigned long long, V > value
Definition: Utils.h:376
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:282
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:297
void Clear()
Completely clear the table.
Insert an object with a time-to-live and check later if it still exists.
Definition: Utils.h:265
static unsigned char * MD5(const unsigned char *input, unsigned int ilen)
output must be freed
unsigned int m_uTTL
Default time-to-live duration.
Definition: Utils.h:380
static timeval ParseServerTime(const CString &sTime)
TCacheMap(unsigned int uTTL=5000)
Definition: Utils.h:267
static timeval GetTime()
void Clear()
Clear all entries.
Definition: Utils.h:355
CBlowfish(const CString &sPassword, int iEncrypt, const CString &sIvec="")
CTable()
Definition: Utils.h:158
std::map< CString, value >::iterator iterator
Definition: Utils.h:377
EType
Definition: Utils.h:118
Generate a grid-like output from a given input.
Definition: Utils.h:156
size_type AddRow()
Adds a new row to the table.
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="")
CString::size_type GetColumnWidth(unsigned int uIdx) const
Return the width of the given column.
static void PrintPrompt(const CString &sMessage)
bool SetCell(const CString &sColumn, const CString &sValue, size_type uRowIdx=~0)
Sets a given cell in the table to a value.