InspIRCd  3.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 
68 enum UserType {
69  USERTYPE_LOCAL = 1,
70  USERTYPE_REMOTE = 2,
71  USERTYPE_SERVER = 3
72 };
73 
75 class CoreExport ConnectClass : public refcountbase
76 {
77  public:
80 
82  std::string host;
83 
85  std::vector<std::string> hosts;
86 
88  std::string name;
89 
91  std::string password;
92 
94  std::string passwordhash;
95 
98 
100  char type;
101 
103  bool fakelag:1;
104 
106  bool maxconnwarn:1;
107 
110 
113 
115  unsigned int maxchans;
116 
118  unsigned int penaltythreshold;
119 
121  unsigned int pingtime;
122 
124  unsigned int registration_timeout;
125 
127  unsigned int commandrate;
128 
130  unsigned long hardsendqmax;
131 
133  unsigned long limit;
134 
136  unsigned long maxglobal;
137 
139  unsigned long maxlocal;
140 
142  unsigned long recvqmax;
143 
145  unsigned long softsendqmax;
146 
148  ConnectClass(ConfigTag* tag, char type, const std::string& mask);
149 
151  ConnectClass(ConfigTag* tag, char type, const std::string& mask, const ConnectClass& parent);
152 
154  void Update(const ConnectClass* newSettings);
155 
157  const std::string& GetName() const { return name; }
158 
160  const std::string& GetHost() const { return host; }
161 
163  const std::vector<std::string>& GetHosts() const { return hosts; }
164 
167  time_t GetRegTimeout()
168  {
169  return (registration_timeout ? registration_timeout : 90);
170  }
171 
174  unsigned int GetPingTime()
175  {
176  return (pingtime ? pingtime : 120);
177  }
178 
182  unsigned long GetSendqSoftMax()
183  {
184  return (softsendqmax ? softsendqmax : 4096);
185  }
186 
189  unsigned long GetSendqHardMax()
190  {
191  return (hardsendqmax ? hardsendqmax : 0x100000);
192  }
193 
196  unsigned long GetRecvqMax()
197  {
198  return (recvqmax ? recvqmax : 4096);
199  }
200 
203  unsigned int GetPenaltyThreshold()
204  {
205  return penaltythreshold ? penaltythreshold : (fakelag ? 10 : 20);
206  }
207 
208  unsigned int GetCommandRate()
209  {
210  return commandrate ? commandrate : 1000;
211  }
212 
215  unsigned long GetMaxLocal()
216  {
217  return maxlocal;
218  }
219 
222  unsigned long GetMaxGlobal()
223  {
224  return maxglobal;
225  }
226 };
227 
229 typedef unsigned int already_sent_t;
230 
236 class CoreExport User : public Extensible
237 {
238  private:
241  std::string cached_fullhost;
242 
245  std::string cached_hostip;
246 
249  std::string cached_makehost;
250 
253  std::string cached_fullrealhost;
254 
257  std::string cachedip;
258 
260  std::string displayhost;
261 
263  std::string realhost;
264 
266  std::string realname;
267 
274  std::bitset<ModeParser::MODEID_MAX> modes;
275 
276  public:
281  {
282  public:
287  virtual void Execute(LocalUser* user) = 0;
288  };
289 
293 
296  time_t age;
297 
302  time_t signon;
303 
309 
314  std::string nick;
315 
319  const std::string uuid;
320 
324  std::string ident;
325 
329  std::bitset<64> snomasks;
330 
334 
338 
342  std::string awaymsg;
343 
347  time_t awaytime;
348 
352 
357  unsigned int registered:3;
358 
364  unsigned int quitting:1;
365 
368 
370  const unsigned int usertype:2;
371 
375  const std::string& GetIPString();
376 
380  const std::string& GetHost(bool uncloak) const;
381 
383  const std::string& GetBanIdent() const;
384 
386  const std::string& GetDisplayedHost() const;
387 
389  const std::string& GetRealHost() const;
390 
392  const std::string& GetRealName() const;
393 
396  irc::sockets::cidr_mask GetCIDRMask();
397 
401  virtual bool SetClientIP(const std::string& address);
402 
403  virtual void SetClientIP(const irc::sockets::sockaddrs& sa);
404 
408  User(const std::string& uid, Server* srv, UserType objtype);
409 
415  virtual const std::string& GetFullHost();
416 
423  virtual const std::string& GetFullRealHost();
424 
428  void InvalidateCache();
429 
434  bool IsAway() const { return (!awaymsg.empty()); }
435 
440  bool IsOper() const { return oper; }
441 
446  bool IsNoticeMaskSet(unsigned char sm);
447 
453  std::string GetModeLetters(bool includeparams = false) const;
454 
459  bool IsModeSet(unsigned char m) const;
460  bool IsModeSet(const ModeHandler* mh) const;
461  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
462  bool IsModeSet(UserModeReference& moderef) const;
463 
468  void SetMode(ModeHandler* mh, bool value);
469  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
470 
477  virtual bool HasCommandPermission(const std::string& command);
478 
486  virtual bool HasPrivPermission(const std::string& privstr);
487 
494  virtual bool HasModePermission(const ModeHandler* mh) const;
495 
500  virtual bool HasSnomaskPermission(char chr) const;
501 
506  const std::string& MakeHost();
507 
512  const std::string& MakeHostIP();
513 
517  void Oper(OperInfo* info);
518 
522  void UnOper();
523 
527  void WriteNotice(const std::string& text);
528 
532  virtual void WriteRemoteNotice(const std::string& text);
533 
534  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
535 
536  template <typename T1>
537  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
538  {
539  Numeric::Numeric n(numeric);
540  n.push(p1);
541  WriteRemoteNumeric(n);
542  }
543 
544  template <typename T1, typename T2>
545  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
546  {
547  Numeric::Numeric n(numeric);
548  n.push(p1);
549  n.push(p2);
550  WriteRemoteNumeric(n);
551  }
552 
553  template <typename T1, typename T2, typename T3>
554  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
555  {
556  Numeric::Numeric n(numeric);
557  n.push(p1);
558  n.push(p2);
559  n.push(p3);
560  WriteRemoteNumeric(n);
561  }
562 
563  template <typename T1, typename T2, typename T3, typename T4>
564  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
565  {
566  Numeric::Numeric n(numeric);
567  n.push(p1);
568  n.push(p2);
569  n.push(p3);
570  n.push(p4);
571  WriteRemoteNumeric(n);
572  }
573 
574  template <typename T1, typename T2, typename T3, typename T4, typename T5>
575  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
576  {
577  Numeric::Numeric n(numeric);
578  n.push(p1);
579  n.push(p2);
580  n.push(p3);
581  n.push(p4);
582  n.push(p5);
583  WriteRemoteNumeric(n);
584  }
585 
586  void WriteNumeric(const Numeric::Numeric& numeric);
587 
588  template <typename T1>
589  void WriteNumeric(unsigned int numeric, T1 p1)
590  {
591  Numeric::Numeric n(numeric);
592  n.push(p1);
593  WriteNumeric(n);
594  }
595 
596  template <typename T1, typename T2>
597  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
598  {
599  Numeric::Numeric n(numeric);
600  n.push(p1);
601  n.push(p2);
602  WriteNumeric(n);
603  }
604 
605  template <typename T1, typename T2, typename T3>
606  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
607  {
608  Numeric::Numeric n(numeric);
609  n.push(p1);
610  n.push(p2);
611  n.push(p3);
612  WriteNumeric(n);
613  }
614 
615  template <typename T1, typename T2, typename T3, typename T4>
616  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
617  {
618  Numeric::Numeric n(numeric);
619  n.push(p1);
620  n.push(p2);
621  n.push(p3);
622  n.push(p4);
623  WriteNumeric(n);
624  }
625 
626  template <typename T1, typename T2, typename T3, typename T4, typename T5>
627  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
628  {
629  Numeric::Numeric n(numeric);
630  n.push(p1);
631  n.push(p2);
632  n.push(p3);
633  n.push(p4);
634  n.push(p5);
635  WriteNumeric(n);
636  }
637 
642  void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
643 
652  already_sent_t ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
653 
658  bool SharesChannelWith(User *other);
659 
664  bool ChangeDisplayedHost(const std::string& host);
665 
670  void ChangeRealHost(const std::string& host, bool resetdisplay);
671 
679  bool ChangeIdent(const std::string& newident);
680 
685  bool ChangeRealName(const std::string& real);
686 
692  bool ChangeNick(const std::string& newnick, time_t newts = 0);
693 
697  void PurgeEmptyChannels();
698 
701  virtual ~User();
702  CullResult cull() CXX11_OVERRIDE;
703 
705  bool Deserialize(Data& data) CXX11_OVERRIDE;
706 
708  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
709 };
710 
711 class CoreExport UserIOHandler : public StreamSocket
712 {
713  private:
714  size_t checked_until;
715  public:
716  LocalUser* const user;
718  : StreamSocket(StreamSocket::SS_USER)
719  , checked_until(0)
720  , user(me)
721  {
722  }
723  void OnDataReady() CXX11_OVERRIDE;
724  bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
725  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
726 
732  void AddWriteBuf(const std::string &data);
733 
737  void SwapInternals(UserIOHandler& other);
738 };
739 
740 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
741 {
745  void Write(const ClientProtocol::SerializedMessage& serialized);
746 
752  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
753 
756  static ClientProtocol::MessageList sendmsglist;
757 
758  public:
760  LocalUser(int fd, const std::string& uuid, Serializable::Data& data);
761 
762  CullResult cull() CXX11_OVERRIDE;
763 
764  UserIOHandler eh;
765 
769 
772  unsigned int bytes_in;
773 
776  unsigned int bytes_out;
777 
780  unsigned int cmds_in;
781 
784  unsigned int cmds_out;
785 
790  std::string password;
791 
795 
799  ConnectClass* GetClass() const { return MyClass; }
800 
803  void CheckClass(bool clone_count = true);
804 
808 
813  unsigned int quitting_sendq:1;
814 
817  unsigned int lastping:1;
818 
821  unsigned int exempt:1;
822 
824  time_t nextping;
825 
828  time_t idle_lastmsg;
829 
833  unsigned int CommandFloodPenalty;
834 
835  already_sent_t already_sent;
836 
841  bool CheckLines(bool doZline = false);
842 
846  void FullConnect();
847 
851  void SetClass(const std::string &explicit_name = "");
852 
853  bool SetClientIP(const std::string& address) CXX11_OVERRIDE;
854 
855  void SetClientIP(const irc::sockets::sockaddrs& sa) CXX11_OVERRIDE;
856 
861  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
862 
869  bool HasCommandPermission(const std::string& command) CXX11_OVERRIDE;
870 
878  bool HasPrivPermission(const std::string& privstr) CXX11_OVERRIDE;
879 
886  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
887 
889  bool HasSnomaskPermission(char chr) const CXX11_OVERRIDE;
890 
895  void OverruleNick();
896 
900  void Send(ClientProtocol::Event& protoev);
901 
906  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
907 
909  bool Deserialize(Data& data) CXX11_OVERRIDE;
910 
912  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
913 };
914 
915 class RemoteUser : public User
916 {
917  public:
918  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
919  {
920  }
921 };
922 
923 class CoreExport FakeUser : public User
924 {
925  public:
926  FakeUser(const std::string& uid, Server* srv)
927  : User(uid, srv, USERTYPE_SERVER)
928  {
929  nick = srv->GetName();
930  }
931 
932  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
933  : User(uid, new Server(uid, sname, sdesc), USERTYPE_SERVER)
934  {
935  nick = sname;
936  }
937 
938  CullResult cull() CXX11_OVERRIDE;
939  const std::string& GetFullHost() CXX11_OVERRIDE;
940  const std::string& GetFullRealHost() CXX11_OVERRIDE;
941 };
942 
943 /* Faster than dynamic_cast */
945 inline LocalUser* IS_LOCAL(User* u)
946 {
947  return (u != NULL && u->usertype == USERTYPE_LOCAL) ? static_cast<LocalUser*>(u) : NULL;
948 }
950 inline RemoteUser* IS_REMOTE(User* u)
951 {
952  return (u != NULL && u->usertype == USERTYPE_REMOTE) ? static_cast<RemoteUser*>(u) : NULL;
953 }
955 inline FakeUser* IS_SERVER(User* u)
956 {
957  return (u != NULL && u->usertype == USERTYPE_SERVER) ? static_cast<FakeUser*>(u) : NULL;
958 }
959 
960 inline bool User::IsModeSet(const ModeHandler* mh) const
961 {
962  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
963 }
964 
965 inline bool User::IsModeSet(UserModeReference& moderef) const
966 {
967  if (!moderef)
968  return false;
969  return IsModeSet(*moderef);
970 }
971 
972 inline void User::SetMode(ModeHandler* mh, bool value)
973 {
974  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
975  modes[mh->GetId()] = value;
976 }
Definition: clientprotocol.h:465
Definition: clientprotocol.h:628
Definition: clientprotocol.h:182
Definition: clientprotocol.h:682
Definition: configreader.h:38
Definition: users.h:76
bool maxconnwarn
Definition: users.h:106
reference< ConfigTag > config
Definition: users.h:79
unsigned int maxchans
Definition: users.h:115
std::string passwordhash
Definition: users.h:94
unsigned int commandrate
Definition: users.h:127
insp::flat_set< int > ports
Definition: users.h:97
unsigned long GetSendqSoftMax()
Definition: users.h:182
unsigned long GetSendqHardMax()
Definition: users.h:189
bool resolvehostnames
Definition: users.h:109
unsigned int pingtime
Definition: users.h:121
unsigned long GetMaxLocal()
Definition: users.h:215
unsigned long recvqmax
Definition: users.h:142
std::string host
Definition: users.h:82
bool fakelag
Definition: users.h:103
const std::vector< std::string > & GetHosts() const
Definition: users.h:163
const std::string & GetName() const
Definition: users.h:157
unsigned long limit
Definition: users.h:133
bool uniqueusername
Definition: users.h:112
std::string password
Definition: users.h:91
unsigned long maxlocal
Definition: users.h:139
unsigned int penaltythreshold
Definition: users.h:118
unsigned int GetPenaltyThreshold()
Definition: users.h:203
unsigned long maxglobal
Definition: users.h:136
unsigned int registration_timeout
Definition: users.h:124
std::vector< std::string > hosts
Definition: users.h:85
unsigned int GetPingTime()
Definition: users.h:174
unsigned long GetMaxGlobal()
Definition: users.h:222
time_t GetRegTimeout()
Definition: users.h:167
unsigned long softsendqmax
Definition: users.h:145
unsigned long hardsendqmax
Definition: users.h:130
char type
Definition: users.h:100
unsigned long GetRecvqMax()
Definition: users.h:196
const std::string & GetHost() const
Definition: users.h:160
std::string name
Definition: users.h:88
Definition: base.h:36
Definition: extensible.h:142
Definition: users.h:924
Definition: users.h:741
ConnectClass * GetClass() const
Definition: users.h:799
time_t idle_lastmsg
Definition: users.h:828
unsigned int bytes_in
Definition: users.h:772
unsigned int cmds_in
Definition: users.h:780
unsigned int exempt
Definition: users.h:821
unsigned int bytes_out
Definition: users.h:776
unsigned int CommandFloodPenalty
Definition: users.h:833
reference< ConnectClass > MyClass
Definition: users.h:794
unsigned int cmds_out
Definition: users.h:784
irc::sockets::sockaddrs server_sa
Definition: users.h:807
unsigned int quitting_sendq
Definition: users.h:813
ClientProtocol::Serializer * serializer
Definition: users.h:768
time_t nextping
Definition: users.h:824
unsigned int lastping
Definition: users.h:817
std::string password
Definition: users.h:790
Definition: mode.h:100
Id GetId() const
Definition: mode.h:248
static const ModeHandler::Id MODEID_MAX
Definition: mode.h:570
Definition: numeric.h:31
Definition: configreader.h:174
Definition: users.h:916
Definition: serialize.h:31
Definition: serialize.h:24
Definition: server.h:25
const std::string & GetName() const
Definition: server.h:76
Definition: inspsocket.h:115
Definition: users.h:281
virtual void Execute(LocalUser *user)=0
Definition: users.h:237
bool IsOper() const
Definition: users.h:440
ChanList chans
Definition: users.h:333
irc::sockets::sockaddrs client_sa
Definition: users.h:308
time_t signon
Definition: users.h:302
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:972
bool IsAway() const
Definition: users.h:434
std::string nick
Definition: users.h:314
unsigned int quitting
Definition: users.h:364
unsigned int registered
Definition: users.h:357
time_t age
Definition: users.h:296
Server * server
Definition: users.h:337
std::string ident
Definition: users.h:324
reference< OperInfo > oper
Definition: users.h:351
std::bitset< 64 > snomasks
Definition: users.h:329
const unsigned int usertype
Definition: users.h:370
const std::string uuid
Definition: users.h:319
std::string awaymsg
Definition: users.h:342
time_t awaytime
Definition: users.h:347
insp::intrusive_list< Membership > ChanList
Definition: users.h:292
bool uniqueusername
Definition: users.h:367
bool IsModeSet(unsigned char m) const
Definition: users.cpp:49
Definition: users.h:712
Definition: dynref.h:131
Definition: intrusive_list.h:34
Definition: base.h:90
Definition: socket.h:83
Definition: socket.h:63