InspIRCd  4.0
configreader.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 Matt Schatz <[email protected]>
5  * Copyright (C) 2018 Chris Novakovic <[email protected]>
6  * Copyright (C) 2013-2014, 2016 Attila Molnar <[email protected]>
7  * Copyright (C) 2013 Daniel Vassdal <[email protected]>
8  * Copyright (C) 2012-2014, 2016-2021 Sadie Powell <[email protected]>
9  * Copyright (C) 2012, 2019 Robby <[email protected]>
10  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
11  * Copyright (C) 2008 Thomas Stagner <[email protected]>
12  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
13  * Copyright (C) 2007 Dennis Friis <[email protected]>
14  * Copyright (C) 2006-2008 Craig Edwards <[email protected]>
15  * Copyright (C) 2006 Oliver Lupton <[email protected]>
16  *
17  * This file is part of InspIRCd. InspIRCd is free software: you can
18  * redistribute it and/or modify it under the terms of the GNU General Public
19  * License as published by the Free Software Foundation, version 2.
20  *
21  * This program is distributed in the hope that it will be useful, but WITHOUT
22  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24  * details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29 
30 
31 #pragma once
32 
33 #include "inspircd.h"
34 #include "token_list.h"
35 
37 class CoreExport ConfigTag
38 {
39 public:
42 
43  private:
44  Items items;
45 
46  public:
48  const std::string name;
49 
52 
57  ConfigTag(const std::string& Name, const FilePosition& Source);
58 
60  std::string getString(const std::string& key, const std::string& def, const std::function<bool(const std::string&)>& validator) const;
62  std::string getString(const std::string& key, const std::string& def = "", size_t minlen = 0, size_t maxlen = UINT32_MAX) const;
64  long getInt(const std::string& key, long def, long min = LONG_MIN, long max = LONG_MAX) const;
66  unsigned long getUInt(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX) const;
68  double getFloat(const std::string& key, double def, double min = DBL_MIN, double max = DBL_MAX) const;
70  bool getBool(const std::string& key, bool def = false) const;
71 
80  unsigned long getDuration(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX) const;
81 
82  template<typename TReturn>
83  TReturn getEnum(const std::string& key, TReturn def, std::initializer_list<std::pair<const char*, TReturn>> enumvals)
84  {
85  const std::string val = getString(key);
86  if (val.empty())
87  return def;
88 
89  for (const std::pair<const char*, TReturn>& enumval : enumvals)
90  if (stdalgo::string::equalsci(val, enumval.first))
91  return enumval.second;
92 
93  std::vector<const char*> enumkeys;
94  std::transform(enumvals.begin(), enumvals.end(), std::back_inserter(enumkeys), [](const std::pair<const char*, TReturn>& ev) { return ev.first; });
95  throw ModuleException(val + " is an invalid value for <" + name + ":" + key + ">; acceptable values are " +
96  stdalgo::string::join(enumkeys, ' ') + ", at " + source.str());
97  }
98 
105  bool readString(const std::string& key, std::string& value, bool allow_newline = false) const;
106 
108  inline const Items& GetItems() const { return items; }
109  inline Items& GetItems() { return items; }
110 };
111 
116 {
117  public:
119  size_t MaxLine;
121  size_t MaxNick;
123  size_t MaxChannel;
125  size_t MaxModes;
127  size_t MaxUser;
129  size_t MaxQuit;
131  size_t MaxTopic;
133  size_t MaxKick;
135  size_t MaxReal;
137  size_t MaxAway;
139  size_t MaxHost;
140 
144  ServerLimits(std::shared_ptr<ConfigTag> tag);
145 
147  size_t GetMaxMask() const { return MaxNick + 1 + MaxUser + 1 + MaxHost; }
148 };
149 
151 {
157  bool nofork;
158 
166 
175  bool writelog;
176 
187  bool writepid;
188 
189  /* Whether the --runasroot option was specified at boot. */
190  bool runasroot;
191 
193  int argc;
194 
196  char** argv;
197 };
198 
199 class CoreExport OperInfo
200 {
201  public:
202  TokenList AllowedOperCommands;
203  TokenList AllowedPrivs;
204 
206  std::bitset<64> AllowedUserModes;
207 
209  std::bitset<64> AllowedChanModes;
210 
212  std::bitset<64> AllowedSnomasks;
213 
215  std::shared_ptr<ConfigTag> oper_block;
217  std::shared_ptr<ConfigTag> type_block;
219  std::vector<std::shared_ptr<ConfigTag> > class_blocks;
221  std::string name;
222 
226  OperInfo(const std::string& Name);
227 
229  std::string getConfig(const std::string& key);
230  void init();
231 };
232 
238 class CoreExport ServerConfig
239 {
240  private:
241  void ApplyModules(User* user);
242  void CrossCheckConnectBlocks(ServerConfig* current);
243  void CrossCheckOperClassType();
244  void Fill();
245 
246  public:
249  {
252 
255 
257  BUT_RESTRICT_NOTIFY
258  };
259 
261  {
262  public:
264  std::string Config;
265 
267  std::string Data;
268 
270  std::string Log;
271 
273  std::string Module;
274 
276  std::string Runtime;
277 
278  ServerPaths(std::shared_ptr<ConfigTag> tag);
279 
280  std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
281  std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
282  std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
283  std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
284  std::string PrependRuntime(const std::string& fn) const { return FileSystem::ExpandPath(Runtime, fn); }
285  };
286 
289  typedef std::vector<std::shared_ptr<ConnectClass>> ClassVector;
290 
294 
296  typedef std::multimap<std::string, std::shared_ptr<ConfigTag>, irc::insensitive_swo> TagMap;
297 
300 
306  std::shared_ptr<ConfigTag> ConfValue(const std::string& tag, std::shared_ptr<ConfigTag> def = nullptr);
307 
313  TagList ConfTags(const std::string& tag, std::optional<TagList> def = std::nullopt);
314 
316  std::shared_ptr<ConfigTag> EmptyTag;
317 
320  std::stringstream errstr;
321 
323  bool valid;
324 
327 
332 
336  ConfigFileCache Files;
337 
341 
344 
348 
352  unsigned char c_ipv4_range;
353 
357  unsigned char c_ipv6_range;
358 
362  std::string ServerName;
363 
366  std::string XLineMessage;
367 
368  /* Holds the network name the local server
369  * belongs to. This is an arbitrary field defined
370  * by the administrator.
371  */
372  std::string Network;
373 
377  std::string ServerDesc;
378 
381 
386  unsigned long NetBufferSize;
387 
391  int MaxConn;
392 
399 
404  unsigned long SoftLimit;
405 
409  unsigned long MaxTargets;
410 
412  time_t TimeSkipWarn;
413 
417  bool HideBans;
418 
420  bool RawLog = false;
421 
423  std::string HideServer;
424 
428 
431  std::string DefaultModes;
432 
435  std::string CustomVersion;
436 
440 
443  std::string CaseMapping;
444 
449 
453 
457 
463  std::string sid;
464 
467  ServerConfig();
468 
471  const std::string& GetSID() const { return sid; }
472 
474  const std::string& GetServerName() const { return HideServer.empty() ? ServerName : HideServer; }
475 
477  const std::string& GetServerDesc() const { return HideServer.empty() ? ServerDesc : HideServer; }
478 
483  void Read();
484 
487  void Apply(ServerConfig* old, const std::string &useruid);
488 
492  static std::string Escape(const std::string& str);
493 
496  bool NoSnoticeStack = false;
497 };
498 
502 class CoreExport ConfigReaderThread : public Thread
503 {
504  private:
506  ServerConfig* Config = new ServerConfig();
507 
509  std::atomic_bool done = { false };
510 
511  protected:
513  void OnStart() override;
514 
516  void OnStop() override;
517 
518  public:
519  const std::string UUID;
520 
521  ConfigReaderThread(const std::string& uuid)
522  : UUID(uuid)
523  {
524  }
525 
526  ~ConfigReaderThread() override
527  {
528  delete Config;
529  }
530 
532  bool IsDone() { return done.load(); }
533 };
534 
536 class CoreExport ConfigStatus
537 {
538  public:
540  bool const initial;
541 
543  User* const srcuser;
544 
549  ConfigStatus(User* user = NULL, bool isinitial = false)
550  : initial(isinitial)
551  , srcuser(user)
552  {
553  }
554 };
Definition: configreader.h:503
bool IsDone()
Definition: configreader.h:532
Definition: configreader.h:537
User *const srcuser
Definition: configreader.h:543
ConfigStatus(User *user=NULL, bool isinitial=false)
Definition: configreader.h:549
bool const initial
Definition: configreader.h:540
Definition: configreader.h:38
const Items & GetItems() const
Definition: configreader.h:108
const FilePosition source
Definition: configreader.h:51
insp::flat_map< std::string, std::string, irc::insensitive_swo > Items
Definition: configreader.h:41
const std::string name
Definition: configreader.h:48
Definition: fileutils.h:25
std::string str() const
Definition: fileutils.cpp:36
static std::string ExpandPath(const std::string &base, const std::string &fragment)
Definition: fileutils.cpp:83
Definition: base.h:178
Definition: modules.h:286
Definition: configreader.h:200
std::shared_ptr< ConfigTag > oper_block
Definition: configreader.h:215
std::string name
Definition: configreader.h:221
std::bitset< 64 > AllowedSnomasks
Definition: configreader.h:212
std::shared_ptr< ConfigTag > type_block
Definition: configreader.h:217
std::bitset< 64 > AllowedChanModes
Definition: configreader.h:209
std::bitset< 64 > AllowedUserModes
Definition: configreader.h:206
std::vector< std::shared_ptr< ConfigTag > > class_blocks
Definition: configreader.h:219
Definition: configreader.h:261
std::string Module
Definition: configreader.h:273
std::string Runtime
Definition: configreader.h:276
std::string Log
Definition: configreader.h:270
std::string Data
Definition: configreader.h:267
std::string Config
Definition: configreader.h:264
Definition: configreader.h:239
insp::flat_map< std::string, std::shared_ptr< OperInfo > > OperIndex
Definition: configreader.h:293
TagMap config_data
Definition: configreader.h:331
ClassVector Classes
Definition: configreader.h:427
OperIndex oper_blocks
Definition: configreader.h:452
unsigned long NetBufferSize
Definition: configreader.h:386
ServerLimits Limits
Definition: configreader.h:340
std::string XLineMessage
Definition: configreader.h:366
BannedUserTreatment
Definition: configreader.h:249
@ BUT_RESTRICT_SILENT
Definition: configreader.h:254
@ BUT_NORMAL
Definition: configreader.h:251
int MaxConn
Definition: configreader.h:391
const std::string & GetServerDesc() const
Definition: configreader.h:477
std::shared_ptr< ConfigTag > EmptyTag
Definition: configreader.h:316
const std::string & GetSID() const
Definition: configreader.h:471
bool CCOnConnect
Definition: configreader.h:398
bool WildcardIPv6
Definition: configreader.h:326
std::stringstream errstr
Definition: configreader.h:320
std::string ServerDesc
Definition: configreader.h:377
unsigned long MaxTargets
Definition: configreader.h:409
time_t TimeSkipWarn
Definition: configreader.h:412
ConfigFileCache Files
Definition: configreader.h:336
std::string CaseMapping
Definition: configreader.h:443
bool FullHostInTopic
Definition: configreader.h:448
insp::iterator_range< TagMap::const_iterator > TagList
Definition: configreader.h:299
std::string HideServer
Definition: configreader.h:423
OperIndex OperTypes
Definition: configreader.h:456
std::string ServerName
Definition: configreader.h:362
CommandLineConf cmdline
Definition: configreader.h:347
BannedUserTreatment RestrictBannedUsers
Definition: configreader.h:380
unsigned char c_ipv6_range
Definition: configreader.h:357
std::string sid
Definition: configreader.h:463
bool HideBans
Definition: configreader.h:417
bool valid
Definition: configreader.h:323
unsigned long SoftLimit
Definition: configreader.h:404
ServerPaths Paths
Definition: configreader.h:343
unsigned char c_ipv4_range
Definition: configreader.h:352
bool SyntaxHints
Definition: configreader.h:439
std::multimap< std::string, std::shared_ptr< ConfigTag >, irc::insensitive_swo > TagMap
Definition: configreader.h:296
std::string DefaultModes
Definition: configreader.h:431
std::string CustomVersion
Definition: configreader.h:435
std::vector< std::shared_ptr< ConnectClass > > ClassVector
Definition: configreader.h:289
const std::string & GetServerName() const
Definition: configreader.h:474
Definition: configreader.h:116
size_t MaxUser
Definition: configreader.h:127
ServerLimits(std::shared_ptr< ConfigTag > tag)
Definition: configreader.cpp:41
size_t MaxModes
Definition: configreader.h:125
size_t MaxChannel
Definition: configreader.h:123
size_t GetMaxMask() const
Definition: configreader.h:147
size_t MaxTopic
Definition: configreader.h:131
size_t MaxNick
Definition: configreader.h:121
size_t MaxLine
Definition: configreader.h:119
size_t MaxQuit
Definition: configreader.h:129
size_t MaxHost
Definition: configreader.h:139
size_t MaxAway
Definition: configreader.h:137
size_t MaxKick
Definition: configreader.h:133
size_t MaxReal
Definition: configreader.h:135
Definition: thread.h:25
Definition: token_list.h:25
Definition: users.h:238
Definition: iterator_range.h:50
Definition: configreader.h:151
bool forcedebug
Definition: configreader.h:165
int argc
Definition: configreader.h:193
bool writelog
Definition: configreader.h:175
bool nofork
Definition: configreader.h:157
char ** argv
Definition: configreader.h:196
bool writepid
Definition: configreader.h:187
Definition: hashcomp.h:101