ZNC trunk
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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
31static 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
38static const char g_HexDigits[] = "0123456789abcdef";
39
40class CUtils {
41 public:
44
45 static CString GetIP(unsigned long addr);
46 static unsigned long GetLongIP(const CString& sIP);
48
49 static void PrintError(const CString& sMessage);
50 static void PrintMessage(const CString& sMessage, bool bStrong = false);
51 static void PrintPrompt(const CString& sMessage);
52 static void PrintAction(const CString& sMessage);
53 static void PrintStatus(bool bSuccess, const CString& sMessage = "");
54
60
61 static CString GetSalt();
62 static CString SaltedMD5Hash(const CString& sPass, const CString& sSalt);
63 static CString SaltedSHA256Hash(const CString& sPass, const CString& sSalt);
64 static CString SaltedHash(const CString& sPass, const CString& sSalt);
69 static bool ConstantTimeEquals(const CString& a, const CString& b);
70 static CString GetPass(const CString& sPrompt);
71 static bool GetInput(const CString& sPrompt, CString& sRet,
72 const CString& sDefault = "",
73 const CString& sHint = "");
74 static bool GetBoolInput(const CString& sPrompt, bool bDefault);
75 static bool GetBoolInput(const CString& sPrompt, bool* pbDefault = nullptr);
76 static bool GetNumInput(const CString& sPrompt, unsigned int& uRet,
77 unsigned int uMin = 0, unsigned int uMax = ~0,
78 unsigned int uDefault = ~0);
79
80 static timeval GetTime();
81 static unsigned long long GetMillTime();
82#ifdef HAVE_LIBSSL
83 static void GenerateCert(FILE* pOut);
84#endif /* HAVE_LIBSSL */
85
86 static CString CTime(time_t t, const CString& sTZ);
87 static CString FormatTime(time_t t, const CString& sFormat,
88 const CString& sTZ);
102 static CString FormatTime(const timeval& tv, const CString& sFormat,
103 const CString& sTZ);
104 static CString FormatServerTime(const timeval& tv);
105 static timeval ParseServerTime(const CString& sTime);
112 static bool CheckCIDR(const CString& sIP, const CString& sRange);
113
115 static MCString GetMessageTags(const CString& sLine);
117 static void SetMessageTags(CString& sLine, const MCString& mssTags);
118
119 private:
120 protected:
121};
122
124 public:
125 typedef enum { EX_Shutdown, EX_Restart } EType;
126
128 virtual ~CException() {}
129
130 EType GetType() const { return m_eType; }
131
132 private:
133 protected:
135};
136
137
178class CTable : protected std::vector<std::vector<CString>> {
179 public:
182 virtual ~CTable() {}
183
191 bool AddColumn(const CString& sName);
192
198 bool SetStyle(EStyle eNewStyle);
199
204 size_type AddRow();
205
213 bool SetCell(const CString& sColumn, const CString& sValue,
214 size_type uRowIdx = ~0);
215
221 bool GetLine(unsigned int uIdx, CString& sLine) const;
222
229 CString::size_type GetColumnWidth(unsigned int uIdx) const;
230
232 void Clear();
233
235 using std::vector<std::vector<CString>>::size;
236
238 using std::vector<std::vector<CString>>::empty;
239
240 private:
241 unsigned int GetColumnIndex(const CString& sName) const;
242
243 protected:
244 std::vector<CString> m_vsHeaders;
245 // Used to cache the width of a column
246 std::map<CString, CString::size_type> m_msuWidths;
248};
249
250#ifdef HAVE_LIBSSL
251#include <openssl/aes.h>
252#include <openssl/blowfish.h>
253#include <openssl/md5.h>
256 public:
262 CBlowfish(const CString& sPassword, int iEncrypt,
263 const CString& sIvec = "");
265
266 CBlowfish(const CBlowfish&) = default;
267 CBlowfish& operator=(const CBlowfish&) = default;
268
270 static unsigned char* MD5(const unsigned char* input, unsigned int ilen);
271
273 static CString MD5(const CString& sInput, bool bHexEncode = false);
274
276 void Crypt(unsigned char* input, unsigned char* output,
277 unsigned int ibytes);
278
280 unsigned char* Crypt(unsigned char* input, unsigned int ibytes);
281 CString Crypt(const CString& sData);
282
283 private:
284 unsigned char* m_ivec;
285 BF_KEY m_bkey;
286 int m_iEncrypt, m_num;
287};
288
289#endif /* HAVE_LIBSSL */
290
296template <typename K, typename V = bool>
298 public:
299 TCacheMap(unsigned int uTTL = 5000) : m_mItems(), m_uTTL(uTTL) {}
300
301 virtual ~TCacheMap() {}
302
307 void AddItem(const K& Item) { AddItem(Item, m_uTTL); }
308
314 void AddItem(const K& Item, unsigned int uTTL) { AddItem(Item, V(), uTTL); }
315
321 void AddItem(const K& Item, const V& Val) { AddItem(Item, Val, m_uTTL); }
322
329 void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
330 if (!uTTL) {
331 // If time-to-live is zero we don't want to waste our time adding
332 // it
333 RemItem(Item); // Remove the item incase it already exists
334 return;
335 }
336
337 m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
338 }
339
345 bool HasItem(const K& Item) {
346 Cleanup();
347 return (m_mItems.find(Item) != m_mItems.end());
348 }
349
355 V* GetItem(const K& Item) {
356 Cleanup();
357 iterator it = m_mItems.find(Item);
358 if (it == m_mItems.end()) return nullptr;
359 return &it->second.second;
360 }
361
367 bool RemItem(const K& Item) { return (m_mItems.erase(Item) != 0); }
368
372 void Cleanup() {
373 iterator it = m_mItems.begin();
374
375 while (it != m_mItems.end()) {
376 if (CUtils::GetMillTime() > (it->second.first)) {
377 m_mItems.erase(it++);
378 } else {
379 ++it;
380 }
381 }
382 }
383
387 void Clear() { m_mItems.clear(); }
388
392 std::map<K, V> GetItems() {
393 Cleanup();
394 std::map<K, V> mItems;
395 for (const auto& it : m_mItems) {
396 mItems[it.first] = it.second.second;
397 }
398 return mItems;
399 }
400
401 // Setters
402 void SetTTL(unsigned int u) { m_uTTL = u; }
403 // !Setters
404 // Getters
405 unsigned int GetTTL() const { return m_uTTL; }
406 // !Getters
407 protected:
408 typedef std::pair<unsigned long long, V> value;
409 typedef typename std::map<K, value>::iterator iterator;
410 std::map<K, value>
412 unsigned int m_uTTL;
413};
414
415#endif // !ZNC_UTILS_H
std::set< CString > SCString
Definition ZNCString.h:37
does Blowfish w/64 bit feedback, no padding
Definition Utils.h:255
CBlowfish(const CBlowfish &)=default
void Crypt(unsigned char *input, unsigned char *output, unsigned int ibytes)
output must be the same size as input
static unsigned char * MD5(const unsigned char *input, unsigned int ilen)
output must be freed
CBlowfish(const CString &sPassword, int iEncrypt, const CString &sIvec="")
unsigned char * Crypt(unsigned char *input, unsigned int ibytes)
must free result
CBlowfish & operator=(const CBlowfish &)=default
static CString MD5(const CString &sInput, bool bHexEncode=false)
returns an md5 of the CString (not hex encoded)
CString Crypt(const CString &sData)
Definition Utils.h:123
EType GetType() const
Definition Utils.h:130
virtual ~CException()
Definition Utils.h:128
EType
Definition Utils.h:125
@ EX_Shutdown
Definition Utils.h:125
@ EX_Restart
Definition Utils.h:125
EType m_eType
Definition Utils.h:134
CException(EType e)
Definition Utils.h:127
String class that is used inside ZNC.
Definition ZNCString.h:50
Generate a grid-like or list-like output from a given input.
Definition Utils.h:178
bool AddColumn(const CString &sName)
Adds a new column to the table.
std::map< CString, CString::size_type > m_msuWidths
Definition Utils.h:246
EStyle eStyle
Definition Utils.h:247
std::vector< CString > m_vsHeaders
Definition Utils.h:244
virtual ~CTable()
Definition Utils.h:182
bool SetStyle(EStyle eNewStyle)
Selects the output style of the table.
bool SetCell(const CString &sColumn, const CString &sValue, size_type uRowIdx=~0)
Sets a given cell in the table to a value.
bool GetLine(unsigned int uIdx, CString &sLine) const
Get a line of the table's output.
CString::size_type GetColumnWidth(unsigned int uIdx) const
Return the width of the given column.
EStyle
Definition Utils.h:180
@ GridStyle
Definition Utils.h:180
@ ListStyle
Definition Utils.h:180
size_type AddRow()
Adds a new row to the table.
CTable()
Definition Utils.h:181
void Clear()
Completely clear the table.
Definition Utils.h:40
static SCString GetEncodings()
static bool CheckCIDR(const CString &sIP, const CString &sRange)
CIDR notation checker, e.g.
static CString FormatTime(time_t t, const CString &sFormat, const CString &sTZ)
static CString GetHostName()
static CString GetIP(unsigned long addr)
static CString GetPass(const CString &sPrompt)
static CString SaltedMD5Hash(const CString &sPass, const CString &sSalt)
static unsigned long GetLongIP(const CString &sIP)
static CString FormatServerTime(const timeval &tv)
static void PrintError(const CString &sMessage)
static CString SaltedSHA256Hash(const CString &sPass, const CString &sSalt)
static CString CTime(time_t t, const CString &sTZ)
static CString AskSaltedHashPassForConfig()
Asks password from stdin, with confirmation.
static void PrintPrompt(const CString &sMessage)
static CString SaltedHash(const CString &sPass, const CString &sSalt)
static bool GetNumInput(const CString &sPrompt, unsigned int &uRet, unsigned int uMin=0, unsigned int uMax=~0, unsigned int uDefault=~0)
static SCString GetTimezones()
static void SetMessageTags(CString &sLine, const MCString &mssTags)
static CString GetSalt()
static timeval GetTime()
static CString FormatTime(const timeval &tv, const CString &sFormat, const CString &sTZ)
Supports an additional format specifier for formatting sub-second values:
static bool ConstantTimeEquals(const CString &a, const CString &b)
Byte-wise compare in time proportional to the shorter of the two lengths, without short-circuiting on...
static void PrintStatus(bool bSuccess, const CString &sMessage="")
static MCString GetMessageTags(const CString &sLine)
static bool GetBoolInput(const CString &sPrompt, bool *pbDefault=nullptr)
static bool GetInput(const CString &sPrompt, CString &sRet, const CString &sDefault="", const CString &sHint="")
static void GenerateCert(FILE *pOut)
static void PrintAction(const CString &sMessage)
static unsigned long long GetMillTime()
static bool GetBoolInput(const CString &sPrompt, bool bDefault)
static void PrintMessage(const CString &sMessage, bool bStrong=false)
static timeval ParseServerTime(const CString &sTime)
A dictionary for strings.
Definition ZNCString.h:577
Insert an object with a time-to-live and check later if it still exists.
Definition Utils.h:297
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:314
bool HasItem(const K &Item)
Performs a Cleanup() and then checks to see if your item exists.
Definition Utils.h:345
unsigned int m_uTTL
Default time-to-live duration.
Definition Utils.h:412
void Cleanup()
Cycles through the queue removing all of the stale entries.
Definition Utils.h:372
std::map< K, value >::iterator iterator
Definition Utils.h:409
bool RemItem(const K &Item)
Removes a specific item from the cache.
Definition Utils.h:367
void AddItem(const K &Item)
This function adds an item to the cache using the default time-to-live value.
Definition Utils.h:307
unsigned int GetTTL() const
Definition Utils.h:405
std::map< K, V > GetItems()
Returns all entries.
Definition Utils.h:392
std::map< K, value > m_mItems
Map of cached items.
Definition Utils.h:411
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:329
void Clear()
Clear all entries.
Definition Utils.h:387
virtual ~TCacheMap()
Definition Utils.h:301
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:321
void SetTTL(unsigned int u)
Definition Utils.h:402
std::pair< unsigned long long, V > value
Definition Utils.h:408
TCacheMap(unsigned int uTTL=5000)
Definition Utils.h:299
V * GetItem(const K &Item)
Performs a Cleanup() and returns a pointer to the object, or nullptr.
Definition Utils.h:355