InspIRCd  4.0
users.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019-2020 Matt Schatz <[email protected]>
5  * Copyright (C) 2019 linuxdaemon <[email protected]>
6  * Copyright (C) 2013 Daniel Vassdal <[email protected]>
7  * Copyright (C) 2012-2016, 2018 Attila Molnar <[email protected]>
8  * Copyright (C) 2012-2013, 2016-2021 Sadie Powell <[email protected]services>
9  * Copyright (C) 2012, 2019 Robby <[email protected]>
10  * Copyright (C) 2012 DjSlash <[email protected]>
11  * Copyright (C) 2012 ChrisTX <[email protected]>
12  * Copyright (C) 2011 jackmcbarn <[email protected]>
13  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
14  * Copyright (C) 2009 Uli Schlachter <[email protected]>
15  * Copyright (C) 2008 Thomas Stagner <[email protected]>
16  * Copyright (C) 2008 John Brooks <[email protected]>
17  * Copyright (C) 2007, 2009 Dennis Friis <[email protected]>
18  * Copyright (C) 2006-2009 Robin Burchell <[email protected]>
19  * Copyright (C) 2003-2008 Craig Edwards <[email protected]>
20  *
21  * This file is part of InspIRCd. InspIRCd is free software: you can
22  * redistribute it and/or modify it under the terms of the GNU General Public
23  * License as published by the Free Software Foundation, version 2.
24  *
25  * This program is distributed in the hope that it will be useful, but WITHOUT
26  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
27  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
28  * details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 
34 
35 #pragma once
36 
37 #include "socket.h"
38 #include "inspsocket.h"
39 #include "mode.h"
40 #include "membership.h"
41 
44 enum ClassTypes {
46  CC_ALLOW = 0,
48  CC_DENY = 1,
50  CC_NAMED = 2
51 };
52 
56 enum RegistrationState {
57 
58 #ifndef _WIN32 // Burlex: This is already defined in win32, luckily it is still 0.
59  REG_NONE = 0, /* Has sent nothing */
60 #endif
61 
62  REG_USER = 1, /* Has sent USER */
63  REG_NICK = 2, /* Has sent NICK */
64  REG_NICKUSER = 3, /* Bitwise combination of REG_NICK and REG_USER */
65  REG_ALL = 7 /* REG_NICKUSER plus next bit along */
66 };
67 
70 struct CoreExport ConnectClass
71 {
72  std::shared_ptr<ConfigTag> config;
75  char type;
76 
78  bool fakelag = true;
79 
82  std::string name;
83 
86  unsigned long registration_timeout = 0;
87 
89  std::vector<std::string> hosts;
90 
93  unsigned long pingtime = 0;
94 
98  unsigned long softsendqmax = 0;
99 
103  unsigned long hardsendqmax = 0;
104 
107  unsigned long recvqmax = 0;
108 
111  unsigned long penaltythreshold = 0;
112 
114  unsigned long commandrate = 0;
115 
118  unsigned long maxlocal = 0;
119 
122  unsigned long maxglobal = 0;
123 
126  bool maxconnwarn = true;
127 
130  unsigned long maxchans = 20;
131 
135  unsigned long limit = 0;
136 
139  bool resolvehostnames = true;
140 
145 
147  std::string password;
148 
150  std::string passwordhash;
151 
154  ConnectClass(std::shared_ptr<ConfigTag> tag, char type, const std::vector<std::string>& masks);
155 
158  ConnectClass(std::shared_ptr<ConfigTag> tag, char type, const std::vector<std::string>& masks, std::shared_ptr<ConnectClass> parent);
159 
161  void Update(const std::shared_ptr<ConnectClass> newSettings);
162 
164  const std::string& GetName() const { return name; }
165 
167  const std::vector<std::string>& GetHosts() const { return hosts; }
168 
171  time_t GetRegTimeout()
172  {
173  return (registration_timeout ? registration_timeout : 90);
174  }
175 
178  unsigned long GetPingTime()
179  {
180  return (pingtime ? pingtime : 120L);
181  }
182 
186  unsigned long GetSendqSoftMax()
187  {
188  return (softsendqmax ? softsendqmax : 4096L);
189  }
190 
193  unsigned long GetSendqHardMax()
194  {
195  return (hardsendqmax ? hardsendqmax : 0x100000L);
196  }
197 
200  unsigned long GetRecvqMax()
201  {
202  return (recvqmax ? recvqmax : 4096L);
203  }
204 
207  unsigned long GetPenaltyThreshold()
208  {
209  return penaltythreshold ? penaltythreshold : (fakelag ? 10L : 20L);
210  }
211 
212  unsigned long GetCommandRate()
213  {
214  return commandrate ? commandrate : 1000L;
215  }
216 
219  unsigned long GetMaxLocal()
220  {
221  return maxlocal;
222  }
223 
226  unsigned long GetMaxGlobal()
227  {
228  return maxglobal;
229  }
230 };
231 
237 class CoreExport User : public Extensible
238 {
239  private:
242  std::string cached_fullhost;
243 
246  std::string cached_hostip;
247 
250  std::string cached_makehost;
251 
254  std::string cached_fullrealhost;
255 
258  std::string cachedip;
259 
261  std::string displayhost;
262 
264  std::string realhost;
265 
267  std::string realname;
268 
275  std::bitset<ModeParser::MODEID_MAX> modes;
276 
277  public:
282  {
283  public:
288  virtual void Execute(LocalUser* user) = 0;
289  };
290 
292  enum Type : uint8_t
293  {
295  TYPE_LOCAL = 0,
296 
298  TYPE_REMOTE = 1,
299 
301  TYPE_SERVER = 2,
302  };
303 
307 
310  time_t age;
311 
316  time_t signon = 0;
317 
323 
328  std::string nick;
329 
333  const std::string uuid;
334 
338  std::string ident;
339 
343  std::bitset<64> snomasks;
344 
348 
352 
356  std::string awaymsg;
357 
361  time_t awaytime;
362 
365  std::shared_ptr<OperInfo> oper;
366 
371  unsigned int registered:3;
372 
378  unsigned int quitting:1;
379 
381  const uint8_t usertype:2;
382 
386  const std::string& GetIPString();
387 
391  const std::string& GetHost(bool uncloak) const;
392 
394  const std::string& GetDisplayedHost() const;
395 
397  const std::string& GetRealHost() const;
398 
400  const std::string& GetRealName() const;
401 
404  irc::sockets::cidr_mask GetCIDRMask();
405 
409  virtual bool SetClientIP(const std::string& address);
410 
411  virtual void SetClientIP(const irc::sockets::sockaddrs& sa);
412 
416  User(const std::string& uid, Server* srv, Type objtype);
417 
423  virtual const std::string& GetFullHost();
424 
431  virtual const std::string& GetFullRealHost();
432 
436  void InvalidateCache();
437 
442  bool IsAway() const { return (!awaymsg.empty()); }
443 
448  bool IsOper() const { return !!oper; }
449 
454  bool IsNoticeMaskSet(unsigned char sm);
455 
461  std::string GetModeLetters(bool includeparams = false) const;
462 
467  bool IsModeSet(unsigned char m) const;
468  bool IsModeSet(const ModeHandler* mh) const;
469  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
470  bool IsModeSet(UserModeReference& moderef) const;
471 
476  void SetMode(ModeHandler* mh, bool value);
477  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
478 
485  virtual bool HasCommandPermission(const std::string& command);
486 
494  virtual bool HasPrivPermission(const std::string& privstr);
495 
502  virtual bool HasModePermission(const ModeHandler* mh) const;
503 
508  virtual bool HasSnomaskPermission(char chr) const;
509 
514  const std::string& MakeHost();
515 
520  const std::string& MakeHostIP();
521 
525  void Oper(std::shared_ptr<OperInfo> info);
526 
530  void UnOper();
531 
535  void WriteNotice(const std::string& text);
536 
540  virtual void WriteRemoteNotice(const std::string& text);
541 
542  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
543 
544  template <typename... Param>
545  void WriteRemoteNumeric(unsigned int numeric, Param... p)
546  {
547  Numeric::Numeric n(numeric);
548  n.push(std::forward<Param>(p)...);
549  WriteRemoteNumeric(n);
550  }
551 
552  void WriteNumeric(const Numeric::Numeric& numeric);
553 
554  template <typename... Param>
555  void WriteNumeric(unsigned int numeric, Param... p)
556  {
557  Numeric::Numeric n(numeric);
558  n.push(std::forward<Param>(p)...);
559  WriteNumeric(n);
560  }
561 
566  void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
567 
576  void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
577 
582  bool SharesChannelWith(User *other);
583 
588  bool ChangeDisplayedHost(const std::string& host);
589 
594  void ChangeRealHost(const std::string& host, bool resetdisplay);
595 
603  bool ChangeIdent(const std::string& newident);
604 
609  bool ChangeRealName(const std::string& real);
610 
616  bool ChangeNick(const std::string& newnick, time_t newts = 0);
617 
621  void PurgeEmptyChannels();
622 
623  Cullable::Result Cull() override;
624 
626  bool Deserialize(Data& data) override;
627 
629  bool Serialize(Serializable::Data& data) override;
630 };
631 
632 class CoreExport UserIOHandler : public StreamSocket
633 {
634  private:
635  size_t checked_until;
636  public:
637  LocalUser* const user;
639  : StreamSocket(StreamSocket::SS_USER)
640  , checked_until(0)
641  , user(me)
642  {
643  }
644  void OnDataReady() override;
645  bool OnSetLocalEndPoint(const irc::sockets::sockaddrs& ep) override;
646  bool OnSetRemoteEndPoint(const irc::sockets::sockaddrs& ep) override;
647  void OnError(BufferedSocketError error) override;
648 
654  void AddWriteBuf(const std::string &data);
655 };
656 
657 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
658 {
659  private:
661  std::shared_ptr<ConnectClass> connectclass;
662 
664  static ClientProtocol::MessageList sendmsglist;
665 
669  void Write(const ClientProtocol::SerializedMessage& serialized);
670 
676  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
677 
678  public:
680  LocalUser(int fd, const std::string& uuid, Serializable::Data& data);
681 
682  Cullable::Result Cull() override;
683 
684  UserIOHandler eh;
685 
688  ClientProtocol::Serializer* serializer = nullptr;
689 
692  unsigned int bytes_in = 0;
693 
696  unsigned int bytes_out = 0;
697 
700  unsigned int cmds_in = 0;
701 
704  unsigned int cmds_out = 0;
705 
710  std::string password;
711 
715  std::shared_ptr<ConnectClass> GetClass() const { return connectclass; }
716 
719  void CheckClass(bool clone_count = true);
720 
724 
729  unsigned int quitting_sendq:1;
730 
733  unsigned int lastping:1;
734 
737  unsigned int exempt:1;
738 
740  time_t nextping = 0;
741 
744  time_t idle_lastmsg = 0;
745 
749  unsigned int CommandFloodPenalty = 0;
750 
751  uint64_t already_sent = 0;
752 
757  bool CheckLines(bool doZline = false);
758 
762  void FullConnect();
763 
767  void SetClass(const std::string &explicit_name = "");
768 
769  bool SetClientIP(const std::string& address) override;
770 
771  void SetClientIP(const irc::sockets::sockaddrs& sa) override;
772 
777  void WriteRemoteNotice(const std::string& text) override;
778 
785  bool HasCommandPermission(const std::string& command) override;
786 
794  bool HasPrivPermission(const std::string& privstr) override;
795 
802  bool HasModePermission(const ModeHandler* mh) const override;
803 
805  bool HasSnomaskPermission(char chr) const override;
806 
811  void OverruleNick();
812 
816  void Send(ClientProtocol::Event& protoev);
817 
822  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
823 
825  bool Deserialize(Data& data) override;
826 
828  bool Serialize(Serializable::Data& data) override;
829 };
830 
831 class RemoteUser : public User
832 {
833  public:
834  RemoteUser(const std::string& uid, Server* srv)
835  : User(uid, srv, TYPE_REMOTE)
836  {
837  }
838 };
839 
840 class CoreExport FakeUser : public User
841 {
842  public:
843  FakeUser(const std::string& uid, Server* srv)
844  : User(uid, srv, TYPE_SERVER)
845  {
846  nick = srv->GetName();
847  }
848 
849  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
850  : User(uid, new Server(uid, sname, sdesc), TYPE_SERVER)
851  {
852  nick = sname;
853  }
854 
855  Cullable::Result Cull() override;
856  const std::string& GetFullHost() override;
857  const std::string& GetFullRealHost() override;
858 };
859 
860 /* Faster than dynamic_cast */
862 inline LocalUser* IS_LOCAL(User* u)
863 {
864  return (u != nullptr && u->usertype == User::TYPE_LOCAL) ? static_cast<LocalUser*>(u) : nullptr;
865 }
867 inline RemoteUser* IS_REMOTE(User* u)
868 {
869  return (u != nullptr && u->usertype == User::TYPE_REMOTE) ? static_cast<RemoteUser*>(u) : nullptr;
870 }
872 inline FakeUser* IS_SERVER(User* u)
873 {
874  return (u != nullptr && u->usertype == User::TYPE_SERVER) ? static_cast<FakeUser*>(u) : nullptr;
875 }
876 
877 inline bool User::IsModeSet(const ModeHandler* mh) const
878 {
879  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
880 }
881 
882 inline bool User::IsModeSet(UserModeReference& moderef) const
883 {
884  if (!moderef)
885  return false;
886  return IsModeSet(*moderef);
887 }
888 
889 inline void User::SetMode(ModeHandler* mh, bool value)
890 {
891  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
892  modes[mh->GetId()] = value;
893 }
Definition: clientprotocol.h:470
Definition: clientprotocol.h:628
Definition: clientprotocol.h:182
Definition: clientprotocol.h:682
Definition: cull.h:38
Definition: extensible.h:132
Definition: users.h:841
Definition: users.h:658
unsigned int exempt
Definition: users.h:737
irc::sockets::sockaddrs server_sa
Definition: users.h:723
unsigned int quitting_sendq
Definition: users.h:729
unsigned int lastping
Definition: users.h:733
std::string password
Definition: users.h:710
std::shared_ptr< ConnectClass > GetClass() const
Definition: users.h:715
Definition: mode.h:100
Id GetId() const
Definition: mode.h:247
static const ModeHandler::Id MODEID_MAX
Definition: mode.h:561
Definition: numeric.h:31
Definition: users.h:832
Definition: serialize.h:31
Definition: server.h:25
const std::string & GetName() const
Definition: server.h:64
Definition: inspsocket.h:115
Definition: users.h:282
virtual void Execute(LocalUser *user)=0
Definition: users.h:238
const uint8_t usertype
Definition: users.h:381
User(const std::string &uid, Server *srv, Type objtype)
Definition: users.cpp:79
bool IsOper() const
Definition: users.h:448
ChanList chans
Definition: users.h:347
irc::sockets::sockaddrs client_sa
Definition: users.h:322
Type
Definition: users.h:293
@ TYPE_REMOTE
Definition: users.h:298
@ TYPE_LOCAL
Definition: users.h:295
@ TYPE_SERVER
Definition: users.h:301
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:889
bool IsAway() const
Definition: users.h:442
std::string nick
Definition: users.h:328
unsigned int quitting
Definition: users.h:378
unsigned int registered
Definition: users.h:371
time_t age
Definition: users.h:310
Server * server
Definition: users.h:351
std::string ident
Definition: users.h:338
std::bitset< 64 > snomasks
Definition: users.h:343
const std::string uuid
Definition: users.h:333
std::string awaymsg
Definition: users.h:356
time_t awaytime
Definition: users.h:361
insp::intrusive_list< Membership > ChanList
Definition: users.h:306
std::shared_ptr< OperInfo > oper
Definition: users.h:365
bool IsModeSet(unsigned char m) const
Definition: users.cpp:49
Definition: users.h:633
Definition: dynref.h:133
Definition: intrusive_list.h:34
Definition: users.h:71
unsigned long GetPingTime()
Definition: users.h:178
std::string passwordhash
Definition: users.h:150
insp::flat_set< int > ports
Definition: users.h:144
unsigned long GetSendqSoftMax()
Definition: users.h:186
unsigned long GetSendqHardMax()
Definition: users.h:193
unsigned long GetMaxLocal()
Definition: users.h:219
const std::vector< std::string > & GetHosts() const
Definition: users.h:167
const std::string & GetName() const
Definition: users.h:164
std::string password
Definition: users.h:147
std::vector< std::string > hosts
Definition: users.h:89
unsigned long GetMaxGlobal()
Definition: users.h:226
time_t GetRegTimeout()
Definition: users.h:171
char type
Definition: users.h:75
unsigned long GetRecvqMax()
Definition: users.h:200
std::string name
Definition: users.h:82
unsigned long GetPenaltyThreshold()
Definition: users.h:207
Definition: socket.h:83
Definition: socket.h:63